2 * Makes a tree bootable image for IBM Evaluation boards.
3 * Basically, just take a zImage, skip the ELF header, and stuff
4 * a 32 byte header on the front.
6 * We use htonl, which is a network macro, to make sure we're doing
7 * The Right Thing on an LE machine. It's non-obvious, but it should
8 * work on anything BSD'ish.
17 #include <netinet/in.h>
20 /* This gets tacked on the front of the image. There are also a few
21 * bytes allocated after the _start label used by the boot rom (see
22 * head.S for details).
24 typedef struct boot_block {
25 uint32_t bb_magic; /* 0x0052504F */
26 uint32_t bb_dest; /* Target address of the image */
27 uint32_t bb_num_512blocks; /* Size, rounded-up, in 512 byte blks */
28 uint32_t bb_debug_flag; /* Run debugger or image after load */
29 uint32_t bb_entry_point; /* The image address to start */
30 uint32_t bb_checksum; /* 32 bit checksum including header */
37 int main(int argc, char *argv[])
46 fprintf(stderr, "usage: %s <zImage-file> <boot-image> [entry-point]\n",argv[0]);
50 if (stat(argv[1], &st) < 0) {
55 nblks = (st.st_size + IMGBLK) / IMGBLK;
57 bt.bb_magic = htonl(0x0052504F);
59 /* If we have the optional entry point parameter, use it */
61 bt.bb_dest = bt.bb_entry_point = htonl(strtoul(argv[3], NULL, 0));
63 bt.bb_dest = bt.bb_entry_point = htonl(0x500000);
65 /* We know these from the linker command.
66 * ...and then move it up into memory a little more so the
67 * relocation can happen.
69 bt.bb_num_512blocks = htonl(nblks);
74 /* To be neat and tidy :-).
79 if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
80 perror("zImage open");
84 if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
85 perror("bootfile open");
91 for (i=0; i<sizeof(bt)/sizeof(uint); i++)
94 /* Assume zImage is an ELF file, and skip the 64K header.
96 if (read(in_fd, tmpbuf, IMGBLK) != IMGBLK) {
97 fprintf(stderr, "%s is too small to be an ELF image\n",
102 if ((*(uint *)tmpbuf) != htonl(0x7f454c46)) {
103 fprintf(stderr, "%s is not an ELF image\n", argv[1]);
107 if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
108 fprintf(stderr, "%s failed to seek in ELF image\n", argv[1]);
112 nblks -= (64 * 1024) / IMGBLK;
114 /* And away we go......
116 if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
117 perror("boot-image write");
121 while (nblks-- > 0) {
122 if (read(in_fd, tmpbuf, IMGBLK) < 0) {
123 perror("zImage read");
127 for (i=0; i<sizeof(tmpbuf)/sizeof(uint); i++)
129 if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
130 perror("boot-image write");
135 /* rewrite the header with the computed checksum.
137 bt.bb_checksum = htonl(cksum);
138 if (lseek(out_fd, 0, SEEK_SET) < 0) {
139 perror("rewrite seek");
142 if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
143 perror("boot-image rewrite");