mktree.c 3.5 KB

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