mktree.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  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.
  5. *
  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.
  9. */
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #include <netinet/in.h>
  17. #ifdef __sun__
  18. #include <inttypes.h>
  19. #else
  20. #include <stdint.h>
  21. #endif
  22. /* This gets tacked on the front of the image. There are also a few
  23. * bytes allocated after the _start label used by the boot rom (see
  24. * head.S for details).
  25. */
  26. typedef struct boot_block {
  27. uint32_t bb_magic; /* 0x0052504F */
  28. uint32_t bb_dest; /* Target address of the image */
  29. uint32_t bb_num_512blocks; /* Size, rounded-up, in 512 byte blks */
  30. uint32_t bb_debug_flag; /* Run debugger or image after load */
  31. uint32_t bb_entry_point; /* The image address to start */
  32. uint32_t bb_checksum; /* 32 bit checksum including header */
  33. uint32_t reserved[2];
  34. } boot_block_t;
  35. #define IMGBLK 512
  36. char tmpbuf[IMGBLK];
  37. int main(int argc, char *argv[])
  38. {
  39. int in_fd, out_fd;
  40. int nblks, i;
  41. uint cksum, *cp;
  42. struct stat st;
  43. boot_block_t bt;
  44. if (argc < 3) {
  45. fprintf(stderr, "usage: %s <zImage-file> <boot-image> [entry-point]\n",argv[0]);
  46. exit(1);
  47. }
  48. if (stat(argv[1], &st) < 0) {
  49. perror("stat");
  50. exit(2);
  51. }
  52. nblks = (st.st_size + IMGBLK) / IMGBLK;
  53. bt.bb_magic = htonl(0x0052504F);
  54. /* If we have the optional entry point parameter, use it */
  55. if (argc == 4)
  56. bt.bb_dest = bt.bb_entry_point = htonl(strtoul(argv[3], NULL, 0));
  57. else
  58. bt.bb_dest = bt.bb_entry_point = htonl(0x500000);
  59. /* We know these from the linker command.
  60. * ...and then move it up into memory a little more so the
  61. * relocation can happen.
  62. */
  63. bt.bb_num_512blocks = htonl(nblks);
  64. bt.bb_debug_flag = 0;
  65. bt.bb_checksum = 0;
  66. /* To be neat and tidy :-).
  67. */
  68. bt.reserved[0] = 0;
  69. bt.reserved[1] = 0;
  70. if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
  71. perror("zImage open");
  72. exit(3);
  73. }
  74. if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
  75. perror("bootfile open");
  76. exit(3);
  77. }
  78. cksum = 0;
  79. cp = (void *)&bt;
  80. for (i=0; i<sizeof(bt)/sizeof(uint); i++)
  81. cksum += *cp++;
  82. /* Assume zImage is an ELF file, and skip the 64K header.
  83. */
  84. if (read(in_fd, tmpbuf, IMGBLK) != IMGBLK) {
  85. fprintf(stderr, "%s is too small to be an ELF image\n",
  86. argv[1]);
  87. exit(4);
  88. }
  89. if ((*(uint *)tmpbuf) != htonl(0x7f454c46)) {
  90. fprintf(stderr, "%s is not an ELF image\n", argv[1]);
  91. exit(4);
  92. }
  93. if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
  94. fprintf(stderr, "%s failed to seek in ELF image\n", argv[1]);
  95. exit(4);
  96. }
  97. nblks -= (64 * 1024) / IMGBLK;
  98. /* And away we go......
  99. */
  100. if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
  101. perror("boot-image write");
  102. exit(5);
  103. }
  104. while (nblks-- > 0) {
  105. if (read(in_fd, tmpbuf, IMGBLK) < 0) {
  106. perror("zImage read");
  107. exit(5);
  108. }
  109. cp = (uint *)tmpbuf;
  110. for (i=0; i<sizeof(tmpbuf)/sizeof(uint); i++)
  111. cksum += *cp++;
  112. if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
  113. perror("boot-image write");
  114. exit(5);
  115. }
  116. }
  117. /* rewrite the header with the computed checksum.
  118. */
  119. bt.bb_checksum = htonl(cksum);
  120. if (lseek(out_fd, 0, SEEK_SET) < 0) {
  121. perror("rewrite seek");
  122. exit(1);
  123. }
  124. if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
  125. perror("boot-image rewrite");
  126. exit(1);
  127. }
  128. exit(0);
  129. }