mkbugboot.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Makes a Motorola PPCBUG ROM bootable image which can be flashed
  3. * into one of the FLASH banks on a Motorola PowerPlus board.
  4. *
  5. * Author: Matt Porter <mporter@mvista.com>
  6. *
  7. * 2001 (c) MontaVista, Software, Inc. This file is licensed under
  8. * the terms of the GNU General Public License version 2. This program
  9. * is licensed "as is" without any warranty of any kind, whether express
  10. * or implied.
  11. */
  12. #define ELF_HEADER_SIZE 65536
  13. #include <unistd.h>
  14. #include <sys/stat.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <netinet/in.h>
  21. #ifdef __sun__
  22. #include <inttypes.h>
  23. #else
  24. #include <stdint.h>
  25. #endif
  26. /* size of read buffer */
  27. #define SIZE 0x1000
  28. /* PPCBUG ROM boot header */
  29. typedef struct bug_boot_header {
  30. uint8_t magic_word[4]; /* "BOOT" */
  31. uint32_t entry_offset; /* Offset from top of header to code */
  32. uint32_t routine_length; /* Length of code */
  33. uint8_t routine_name[8]; /* Name of the boot code */
  34. } bug_boot_header_t;
  35. #define HEADER_SIZE sizeof(bug_boot_header_t)
  36. void update_checksum(void *buf, size_t size, uint16_t *sum)
  37. {
  38. uint32_t csum = *sum;
  39. while (size) {
  40. csum += *(uint16_t *)buf;
  41. if (csum > 0xffff)
  42. csum -= 0xffff;
  43. buf = (uint16_t *)buf + 1;
  44. size -= 2;
  45. }
  46. *sum = csum;
  47. }
  48. uint32_t copy_image(int in_fd, int out_fd, uint16_t *sum)
  49. {
  50. uint8_t buf[SIZE];
  51. int offset = 0;
  52. int n;
  53. uint32_t image_size = 0;
  54. lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET);
  55. /* Copy an image while recording its size */
  56. while ( (n = read(in_fd, buf + offset, SIZE - offset)) > 0 ) {
  57. n += offset;
  58. offset = n & 1;
  59. n -= offset;
  60. image_size = image_size + n;
  61. /* who's going to deal with short writes? */
  62. write(out_fd, buf, n);
  63. update_checksum(buf, n, sum);
  64. if (offset)
  65. buf[0] = buf[n];
  66. }
  67. /* BUG romboot requires that our size is divisible by 2 */
  68. /* align image to 2 byte boundary */
  69. if (offset) {
  70. image_size += 2;
  71. buf[1] = '\0';
  72. write(out_fd, buf, 2);
  73. update_checksum(buf, 2, sum);
  74. }
  75. return image_size;
  76. }
  77. void write_bugboot_header(int out_fd, uint32_t boot_size, uint16_t *sum)
  78. {
  79. static bug_boot_header_t bbh = {
  80. .magic_word = "BOOT",
  81. .routine_name = "LINUXROM"
  82. };
  83. /* Fill in the PPCBUG ROM boot header */
  84. bbh.entry_offset = htonl(HEADER_SIZE); /* Entry address */
  85. bbh.routine_length= htonl(HEADER_SIZE+boot_size+2); /* Routine length */
  86. /* Output the header and bootloader to the file */
  87. write(out_fd, &bbh, sizeof(bug_boot_header_t));
  88. update_checksum(&bbh, sizeof(bug_boot_header_t), sum);
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. int image_fd, bugboot_fd;
  93. uint32_t kernel_size = 0;
  94. uint16_t checksum = 0;
  95. if (argc != 3) {
  96. fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
  97. exit(-1);
  98. }
  99. /* Get file args */
  100. /* kernel image file */
  101. if ((image_fd = open(argv[1] , 0)) < 0)
  102. exit(-1);
  103. /* bugboot file */
  104. if (!strcmp(argv[2], "-"))
  105. bugboot_fd = 1; /* stdout */
  106. else if ((bugboot_fd = creat(argv[2] , 0755)) < 0)
  107. exit(-1);
  108. /* Set file position after ROM header block where zImage will be written */
  109. lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
  110. /* Copy kernel image into bugboot image */
  111. kernel_size = copy_image(image_fd, bugboot_fd, &checksum);
  112. /* Set file position to beginning where header/romboot will be written */
  113. lseek(bugboot_fd, 0, SEEK_SET);
  114. /* Write out BUG header/romboot */
  115. write_bugboot_header(bugboot_fd, kernel_size, &checksum);
  116. /* Write out the calculated checksum */
  117. lseek(bugboot_fd, 0, SEEK_END);
  118. write(bugboot_fd, &checksum, 2);
  119. /* Close bugboot file */
  120. close(bugboot_fd);
  121. return 0;
  122. }