mkbrnimg.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * mkbrnimg.c - partially based on OpenWrt's wndr3700.c
  3. *
  4. * Copyright (C) 2011 Tobias Diedrich <ranma+openwrt@tdiedrich.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License,
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <sys/mman.h>
  26. #include <string.h>
  27. #include <netinet/in.h>
  28. #include <inttypes.h>
  29. #define BPB 8 /* bits/byte */
  30. static uint32_t crc32[1<<BPB];
  31. static void init_crc32()
  32. {
  33. const uint32_t poly = ntohl(0x2083b8ed);
  34. int n;
  35. for (n = 0; n < 1<<BPB; n++) {
  36. uint32_t crc = n;
  37. int bit;
  38. for (bit = 0; bit < BPB; bit++)
  39. crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
  40. crc32[n] = crc;
  41. }
  42. }
  43. static uint32_t crc32buf(unsigned char *buf, size_t len)
  44. {
  45. uint32_t crc = 0xFFFFFFFF;
  46. for (; len; len--, buf++)
  47. crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
  48. return ~crc;
  49. }
  50. static void usage(const char *) __attribute__ (( __noreturn__ ));
  51. static void usage(const char *mess)
  52. {
  53. fprintf(stderr, "Error: %s\n", mess);
  54. fprintf(stderr, "Usage: mkbrnimg [-o output_file] [-m magic] [-s signature] kernel_file [additional files]\n");
  55. fprintf(stderr, "\n");
  56. exit(1);
  57. }
  58. static char *output_file = "default-brnImage";
  59. static uint32_t magic = 0x12345678;
  60. static char *signature = "BRNDTW502";
  61. static void parseopts(int *argc, char ***argv)
  62. {
  63. char *endptr;
  64. int res;
  65. while ((res = getopt(*argc, *argv, "o:m:s:")) != -1) {
  66. switch (res) {
  67. default:
  68. usage("Unknown option");
  69. break;
  70. case 'o':
  71. output_file = optarg;
  72. break;
  73. case 'm':
  74. magic = strtoul(optarg, &endptr, 0);
  75. if (endptr == optarg || *endptr != 0)
  76. usage("magic must be a decimal or hexadecimal 32-bit value");
  77. break;
  78. case 's':
  79. signature = optarg;
  80. break;
  81. }
  82. }
  83. *argc -= optind;
  84. *argv += optind;
  85. }
  86. static void appendfile(int outfd, char *path, int kernel) {
  87. int fd;
  88. size_t len, padded_len;
  89. char *input_file;
  90. uint32_t crc;
  91. char padding[0x400];
  92. char footer[12];
  93. memset(padding, 0xff, sizeof(padding));
  94. // mmap input_file
  95. if ((fd = open(path, O_RDONLY)) < 0
  96. || (len = lseek(fd, 0, SEEK_END)) < 0
  97. || (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
  98. || close(fd) < 0)
  99. {
  100. fprintf(stderr, "Error mapping file '%s': %s\n", path, strerror(errno));
  101. exit(1);
  102. }
  103. // kernel should be lzma compressed image, not uImage
  104. if (kernel &&
  105. (input_file[0] != (char)0x5d ||
  106. input_file[1] != (char)0x00 ||
  107. input_file[2] != (char)0x00 ||
  108. input_file[3] != (char)0x80)) {
  109. fprintf(stderr, "lzma signature not found on kernel image.\n");
  110. exit(1);
  111. }
  112. init_crc32();
  113. crc = crc32buf(input_file, len);
  114. fprintf(stderr, "crc32 for '%s' is %08x.\n", path, crc);
  115. // write the file
  116. write(outfd, input_file, len);
  117. // write padding
  118. padded_len = ((len + sizeof(footer) + sizeof(padding) - 1) & ~(sizeof(padding) - 1)) - sizeof(footer);
  119. fprintf(stderr, "len=%08x padded_len=%08x\n", len, padded_len);
  120. write(outfd, padding, padded_len - len);
  121. // write footer
  122. footer[0] = (len >> 0) & 0xff;
  123. footer[1] = (len >> 8) & 0xff;
  124. footer[2] = (len >> 16) & 0xff;
  125. footer[3] = (len >> 24) & 0xff;
  126. footer[4] = (magic >> 0) & 0xff;
  127. footer[5] = (magic >> 8) & 0xff;
  128. footer[6] = (magic >> 16) & 0xff;
  129. footer[7] = (magic >> 24) & 0xff;
  130. footer[8] = (crc >> 0) & 0xff;
  131. footer[9] = (crc >> 8) & 0xff;
  132. footer[10] = (crc >> 16) & 0xff;
  133. footer[11] = (crc >> 24) & 0xff;
  134. write(outfd, footer, sizeof(footer));
  135. munmap(input_file, len);
  136. }
  137. int main(int argc, char **argv)
  138. {
  139. int outfd;
  140. int i;
  141. parseopts(&argc, &argv);
  142. if (argc < 1)
  143. usage("wrong number of arguments");
  144. if ((outfd = open(output_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
  145. {
  146. fprintf(stderr, "Error opening '%s' for writing: %s\n", output_file, strerror(errno));
  147. exit(1);
  148. }
  149. for (i=0; i<argc; i++) {
  150. appendfile(outfd, argv[i], i == 0);
  151. }
  152. write(outfd, signature, strlen(signature)+1);
  153. close(outfd);
  154. return 0;
  155. }