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
15 #define ELF_HEADER_SIZE 65536
31 #define cpu_to_be32(x) le32_to_cpu(x)
32 #define cpu_to_be16(x) le16_to_cpu(x)
34 #define cpu_to_be32(x) (x)
35 #define cpu_to_be16(x) (x)
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) |
54 /* size of read buffer */
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 */
65 #define HEADER_SIZE sizeof(bug_boot_header_t)
67 uint32_t copy_image(int32_t in_fd
, int32_t out_fd
)
71 uint32_t image_size
= 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 */
88 write(out_fd
, &zero
, 1);
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;
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;
130 int main(int argc
, char *argv
[])
132 int32_t image_fd
, bugboot_fd
;
134 uint32_t kernel_size
= 0;
135 uint16_t checksum
= 0;
136 uint8_t bugbootname
[256];
140 fprintf(stderr
, "usage: %s <kernel_image> <bugboot>\n",argv
[0]);
146 /* kernel image file */
147 if ((image_fd
= open( argv
[argptr
] , 0)) < 0)
152 if ( !strcmp( argv
[argptr
], "-" ) )
153 bugboot_fd
= 1; /* stdout */
155 if ((bugboot_fd
= creat( argv
[argptr
] , 0755)) < 0)
158 strcpy(bugbootname
, argv
[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
);
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 */
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);