mktree.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. unsigned int tmpbuf[IMGBLK / sizeof(unsigned int)];
  37. int main(int argc, char *argv[])
  38. {
  39. int in_fd, out_fd;
  40. int nblks, i;
  41. unsigned int cksum, *cp;
  42. struct stat st;
  43. boot_block_t bt;
  44. if (argc < 5) {
  45. fprintf(stderr, "usage: %s <zImage-file> <boot-image> <load address> <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. bt.bb_dest = htonl(strtoul(argv[3], NULL, 0));
  56. bt.bb_entry_point = htonl(strtoul(argv[4], NULL, 0));
  57. /* We know these from the linker command.
  58. * ...and then move it up into memory a little more so the
  59. * relocation can happen.
  60. */
  61. bt.bb_num_512blocks = htonl(nblks);
  62. bt.bb_debug_flag = 0;
  63. bt.bb_checksum = 0;
  64. /* To be neat and tidy :-).
  65. */
  66. bt.reserved[0] = 0;
  67. bt.reserved[1] = 0;
  68. if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
  69. perror("zImage open");
  70. exit(3);
  71. }
  72. if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
  73. perror("bootfile open");
  74. exit(3);
  75. }
  76. cksum = 0;
  77. cp = (void *)&bt;
  78. for (i = 0; i < sizeof(bt) / sizeof(unsigned int); i++)
  79. cksum += *cp++;
  80. /* Assume zImage is an ELF file, and skip the 64K header.
  81. */
  82. if (read(in_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
  83. fprintf(stderr, "%s is too small to be an ELF image\n",
  84. argv[1]);
  85. exit(4);
  86. }
  87. if (tmpbuf[0] != htonl(0x7f454c46)) {
  88. fprintf(stderr, "%s is not an ELF image\n", argv[1]);
  89. exit(4);
  90. }
  91. if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
  92. fprintf(stderr, "%s failed to seek in ELF image\n", argv[1]);
  93. exit(4);
  94. }
  95. nblks -= (64 * 1024) / IMGBLK;
  96. /* And away we go......
  97. */
  98. if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
  99. perror("boot-image write");
  100. exit(5);
  101. }
  102. while (nblks-- > 0) {
  103. if (read(in_fd, tmpbuf, sizeof(tmpbuf)) < 0) {
  104. perror("zImage read");
  105. exit(5);
  106. }
  107. cp = tmpbuf;
  108. for (i = 0; i < sizeof(tmpbuf) / sizeof(unsigned int); i++)
  109. cksum += *cp++;
  110. if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
  111. perror("boot-image write");
  112. exit(5);
  113. }
  114. }
  115. /* rewrite the header with the computed checksum.
  116. */
  117. bt.bb_checksum = htonl(cksum);
  118. if (lseek(out_fd, 0, SEEK_SET) < 0) {
  119. perror("rewrite seek");
  120. exit(1);
  121. }
  122. if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
  123. perror("boot-image rewrite");
  124. exit(1);
  125. }
  126. exit(0);
  127. }