2 * Makes a Motorola PPCBUG ROM bootable image which can be flashed
3 * into one of the FLASH banks on a Motorola PowerPlus board.
5 * Author: Matt Porter <mporter@mvista.com>
7 * 2001 (c) MontaVista, Software, Inc. This file is licensed under
8 * the terms of the GNU General Public License version 2. This program
9 * is licensed "as is" without any warranty of any kind, whether express
13 #define ELF_HEADER_SIZE 65536
29 #define cpu_to_be32(x) le32_to_cpu(x)
30 #define cpu_to_be16(x) le16_to_cpu(x)
32 #define cpu_to_be32(x) (x)
33 #define cpu_to_be16(x) (x)
36 #define cpu_to_le32(x) le32_to_cpu((x))
37 unsigned long le32_to_cpu(unsigned long x)
39 return (((x & 0x000000ffU) << 24) |
40 ((x & 0x0000ff00U) << 8) |
41 ((x & 0x00ff0000U) >> 8) |
42 ((x & 0xff000000U) >> 24));
45 #define cpu_to_le16(x) le16_to_cpu((x))
46 unsigned short le16_to_cpu(unsigned short x)
48 return (((x & 0x00ff) << 8) |
52 /* size of read buffer */
55 /* PPCBUG ROM boot header */
56 typedef struct bug_boot_header {
57 uint8_t magic_word[4]; /* "BOOT" */
58 uint32_t entry_offset; /* Offset from top of header to code */
59 uint32_t routine_length; /* Length of code */
60 uint8_t routine_name[8]; /* Name of the boot code */
63 #define HEADER_SIZE sizeof(bug_boot_header_t)
65 uint32_t copy_image(int32_t in_fd, int32_t out_fd)
69 uint32_t image_size = 0;
72 lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET);
74 /* Copy an image while recording its size */
75 while ( (n = read(in_fd, buf, SIZE)) > 0 )
77 image_size = image_size + n;
78 write(out_fd, buf, n);
81 /* BUG romboot requires that our size is divisible by 2 */
82 /* align image to 2 byte boundary */
86 write(out_fd, &zero, 1);
92 void write_bugboot_header(int32_t out_fd, uint32_t boot_size)
94 uint8_t header_block[HEADER_SIZE];
95 bug_boot_header_t *bbh = (bug_boot_header_t *)&header_block[0];
97 memset(header_block, 0, HEADER_SIZE);
99 /* Fill in the PPCBUG ROM boot header */
100 strncpy(bbh->magic_word, "BOOT", 4); /* PPCBUG magic word */
101 bbh->entry_offset = cpu_to_be32(HEADER_SIZE); /* Entry address */
102 bbh->routine_length= cpu_to_be32(HEADER_SIZE+boot_size+2); /* Routine length */
103 strncpy(bbh->routine_name, "LINUXROM", 8); /* Routine name */
105 /* Output the header and bootloader to the file */
106 write(out_fd, header_block, HEADER_SIZE);
109 uint16_t calc_checksum(int32_t bug_fd)
111 uint32_t checksum_var = 0;
116 while ( (n = read(bug_fd, buf, 2) ) )
118 checksum_var = checksum_var + *(uint16_t *)buf;
120 /* If we carry out, mask it and add one to the checksum */
121 if (checksum_var >> 16)
122 checksum_var = (checksum_var & 0x0000ffff) + 1;
128 int main(int argc, char *argv[])
130 int32_t image_fd, bugboot_fd;
132 uint32_t kernel_size = 0;
133 uint16_t checksum = 0;
134 uint8_t bugbootname[256];
138 fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
144 /* kernel image file */
145 if ((image_fd = open( argv[argptr] , 0)) < 0)
150 if ( !strcmp( argv[argptr], "-" ) )
151 bugboot_fd = 1; /* stdout */
153 if ((bugboot_fd = creat( argv[argptr] , 0755)) < 0)
156 strcpy(bugbootname, argv[argptr]);
159 /* Set file position after ROM header block where zImage will be written */
160 lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
162 /* Copy kernel image into bugboot image */
163 kernel_size = copy_image(image_fd, bugboot_fd);
166 /* Set file position to beginning where header/romboot will be written */
167 lseek(bugboot_fd, 0, SEEK_SET);
169 /* Write out BUG header/romboot */
170 write_bugboot_header(bugboot_fd, kernel_size);
172 /* Close bugboot file */
175 /* Reopen it as read/write */
176 bugboot_fd = open(bugbootname, O_RDWR);
178 /* Calculate checksum */
179 checksum = calc_checksum(bugboot_fd);
181 /* Write out the calculated checksum */
182 write(bugboot_fd, &checksum, 2);