1 // SPDX-License-Identifier: GPL-2.0
3 * Makes a tree bootable image for IBM Evaluation boards.
4 * Basically, just take a zImage, skip the ELF header, and stuff
5 * a 32 byte header on the front.
7 * We use htonl, which is a network macro, to make sure we're doing
8 * The Right Thing on an LE machine. It's non-obvious, but it should
9 * work on anything BSD'ish.
18 #include <netinet/in.h>
25 /* This gets tacked on the front of the image. There are also a few
26 * bytes allocated after the _start label used by the boot rom (see
27 * head.S for details).
29 typedef struct boot_block
{
30 uint32_t bb_magic
; /* 0x0052504F */
31 uint32_t bb_dest
; /* Target address of the image */
32 uint32_t bb_num_512blocks
; /* Size, rounded-up, in 512 byte blks */
33 uint32_t bb_debug_flag
; /* Run debugger or image after load */
34 uint32_t bb_entry_point
; /* The image address to start */
35 uint32_t bb_checksum
; /* 32 bit checksum including header */
40 unsigned int tmpbuf
[IMGBLK
/ sizeof(unsigned int)];
42 int main(int argc
, char *argv
[])
46 unsigned int cksum
, *cp
;
51 fprintf(stderr
, "usage: %s <zImage-file> <boot-image> <load address> <entry point>\n",argv
[0]);
55 if (stat(argv
[1], &st
) < 0) {
60 nblks
= (st
.st_size
+ IMGBLK
) / IMGBLK
;
62 bt
.bb_magic
= htonl(0x0052504F);
64 /* If we have the optional entry point parameter, use it */
65 bt
.bb_dest
= htonl(strtoul(argv
[3], NULL
, 0));
66 bt
.bb_entry_point
= htonl(strtoul(argv
[4], NULL
, 0));
68 /* We know these from the linker command.
69 * ...and then move it up into memory a little more so the
70 * relocation can happen.
72 bt
.bb_num_512blocks
= htonl(nblks
);
77 /* To be neat and tidy :-).
82 if ((in_fd
= open(argv
[1], O_RDONLY
)) < 0) {
83 perror("zImage open");
87 if ((out_fd
= open(argv
[2], (O_RDWR
| O_CREAT
| O_TRUNC
), 0666)) < 0) {
88 perror("bootfile open");
94 for (i
= 0; i
< sizeof(bt
) / sizeof(unsigned int); i
++)
97 /* Assume zImage is an ELF file, and skip the 64K header.
99 if (read(in_fd
, tmpbuf
, sizeof(tmpbuf
)) != sizeof(tmpbuf
)) {
100 fprintf(stderr
, "%s is too small to be an ELF image\n",
105 if (tmpbuf
[0] != htonl(0x7f454c46)) {
106 fprintf(stderr
, "%s is not an ELF image\n", argv
[1]);
110 if (lseek(in_fd
, (64 * 1024), SEEK_SET
) < 0) {
111 fprintf(stderr
, "%s failed to seek in ELF image\n", argv
[1]);
115 nblks
-= (64 * 1024) / IMGBLK
;
117 /* And away we go......
119 if (write(out_fd
, &bt
, sizeof(bt
)) != sizeof(bt
)) {
120 perror("boot-image write");
124 while (nblks
-- > 0) {
125 if (read(in_fd
, tmpbuf
, sizeof(tmpbuf
)) < 0) {
126 perror("zImage read");
130 for (i
= 0; i
< sizeof(tmpbuf
) / sizeof(unsigned int); i
++)
132 if (write(out_fd
, tmpbuf
, sizeof(tmpbuf
)) != sizeof(tmpbuf
)) {
133 perror("boot-image write");
138 /* rewrite the header with the computed checksum.
140 bt
.bb_checksum
= htonl(cksum
);
141 if (lseek(out_fd
, 0, SEEK_SET
) < 0) {
142 perror("rewrite seek");
145 if (write(out_fd
, &bt
, sizeof(bt
)) != sizeof(bt
)) {
146 perror("boot-image rewrite");