Linux-2.6.12-rc2
[linux-2.6/next.git] / arch / ppc / boot / utils / mkbugboot.c
blob886122283f39ae19ae0eab48c55874fb686bbd84
1 /*
2 * arch/ppc/boot/utils/mkbugboot.c
4 * Makes a Motorola PPCBUG ROM bootable image which can be flashed
5 * into one of the FLASH banks on a Motorola PowerPlus board.
7 * Author: Matt Porter <mporter@mvista.com>
9 * 2001 (c) MontaVista, Software, Inc. This file is licensed under
10 * the terms of the GNU General Public License version 2. This program
11 * is licensed "as is" without any warranty of any kind, whether express
12 * or implied.
15 #define ELF_HEADER_SIZE 65536
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #ifdef __sun__
25 #include <inttypes.h>
26 #else
27 #include <stdint.h>
28 #endif
30 #ifdef __i386__
31 #define cpu_to_be32(x) le32_to_cpu(x)
32 #define cpu_to_be16(x) le16_to_cpu(x)
33 #else
34 #define cpu_to_be32(x) (x)
35 #define cpu_to_be16(x) (x)
36 #endif
38 #define cpu_to_le32(x) le32_to_cpu((x))
39 unsigned long le32_to_cpu(unsigned long x)
41 return (((x & 0x000000ffU) << 24) |
42 ((x & 0x0000ff00U) << 8) |
43 ((x & 0x00ff0000U) >> 8) |
44 ((x & 0xff000000U) >> 24));
47 #define cpu_to_le16(x) le16_to_cpu((x))
48 unsigned short le16_to_cpu(unsigned short x)
50 return (((x & 0x00ff) << 8) |
51 ((x & 0xff00) >> 8));
54 /* size of read buffer */
55 #define SIZE 0x1000
57 /* PPCBUG ROM boot header */
58 typedef struct bug_boot_header {
59 uint8_t magic_word[4]; /* "BOOT" */
60 uint32_t entry_offset; /* Offset from top of header to code */
61 uint32_t routine_length; /* Length of code */
62 uint8_t routine_name[8]; /* Name of the boot code */
63 } bug_boot_header_t;
65 #define HEADER_SIZE sizeof(bug_boot_header_t)
67 uint32_t copy_image(int32_t in_fd, int32_t out_fd)
69 uint8_t buf[SIZE];
70 int n;
71 uint32_t image_size = 0;
72 uint8_t zero = 0;
74 lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET);
76 /* Copy an image while recording its size */
77 while ( (n = read(in_fd, buf, SIZE)) > 0 )
79 image_size = image_size + n;
80 write(out_fd, buf, n);
83 /* BUG romboot requires that our size is divisible by 2 */
84 /* align image to 2 byte boundary */
85 if (image_size % 2)
87 image_size++;
88 write(out_fd, &zero, 1);
91 return image_size;
94 void write_bugboot_header(int32_t out_fd, uint32_t boot_size)
96 uint8_t header_block[HEADER_SIZE];
97 bug_boot_header_t *bbh = (bug_boot_header_t *)&header_block[0];
99 memset(header_block, 0, HEADER_SIZE);
101 /* Fill in the PPCBUG ROM boot header */
102 strncpy(bbh->magic_word, "BOOT", 4); /* PPCBUG magic word */
103 bbh->entry_offset = cpu_to_be32(HEADER_SIZE); /* Entry address */
104 bbh->routine_length= cpu_to_be32(HEADER_SIZE+boot_size+2); /* Routine length */
105 strncpy(bbh->routine_name, "LINUXROM", 8); /* Routine name */
107 /* Output the header and bootloader to the file */
108 write(out_fd, header_block, HEADER_SIZE);
111 uint16_t calc_checksum(int32_t bug_fd)
113 uint32_t checksum_var = 0;
114 uint8_t buf[2];
115 int n;
117 /* Checksum loop */
118 while ( (n = read(bug_fd, buf, 2) ) )
120 checksum_var = checksum_var + *(uint16_t *)buf;
122 /* If we carry out, mask it and add one to the checksum */
123 if (checksum_var >> 16)
124 checksum_var = (checksum_var & 0x0000ffff) + 1;
127 return checksum_var;
130 int main(int argc, char *argv[])
132 int32_t image_fd, bugboot_fd;
133 int argptr = 1;
134 uint32_t kernel_size = 0;
135 uint16_t checksum = 0;
136 uint8_t bugbootname[256];
138 if ( (argc != 3) )
140 fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
141 exit(-1);
144 /* Get file args */
146 /* kernel image file */
147 if ((image_fd = open( argv[argptr] , 0)) < 0)
148 exit(-1);
149 argptr++;
151 /* bugboot file */
152 if ( !strcmp( argv[argptr], "-" ) )
153 bugboot_fd = 1; /* stdout */
154 else
155 if ((bugboot_fd = creat( argv[argptr] , 0755)) < 0)
156 exit(-1);
157 else
158 strcpy(bugbootname, argv[argptr]);
159 argptr++;
161 /* Set file position after ROM header block where zImage will be written */
162 lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
164 /* Copy kernel image into bugboot image */
165 kernel_size = copy_image(image_fd, bugboot_fd);
166 close(image_fd);
168 /* Set file position to beginning where header/romboot will be written */
169 lseek(bugboot_fd, 0, SEEK_SET);
171 /* Write out BUG header/romboot */
172 write_bugboot_header(bugboot_fd, kernel_size);
174 /* Close bugboot file */
175 close(bugboot_fd);
177 /* Reopen it as read/write */
178 bugboot_fd = open(bugbootname, O_RDWR);
180 /* Calculate checksum */
181 checksum = calc_checksum(bugboot_fd);
183 /* Write out the calculated checksum */
184 write(bugboot_fd, &checksum, 2);
186 return 0;