mkheader_gemtek.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2014 Claudio Leite <leitec@staticky.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * Builds a proper flash image for routers using some Gemtek
  20. * OEM boards. These include the Airlink101 AR725W, the
  21. * Asante SmartHub 600 (AWRT-600N), and Linksys WRT100/110.
  22. *
  23. * The resulting image is compatible with the factory firmware
  24. * web upgrade and TFTP interface.
  25. *
  26. * To build:
  27. * gcc -O2 -o mkheader_gemtek mkheader_gemtek.c -lz
  28. *
  29. * Claudio Leite <leitec@staticky.com>
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdint.h>
  34. #include <string.h>
  35. #include <zlib.h> /* for crc32() */
  36. /*
  37. * The header is in little-endian format. In case
  38. * we are on a BE host, we need to swap binary
  39. * values.
  40. */
  41. #ifdef __APPLE__
  42. # include <libkern/OSByteOrder.h>
  43. # define le32 OSSwapHostToLittleInt32
  44. #else
  45. # if defined(__linux__)
  46. # include <endian.h>
  47. # if __BYTE_ORDER == __BIG_ENDIAN
  48. # define CPU_BIG_ENDIAN
  49. # endif
  50. # else
  51. # include <sys/endian.h> /* BSD's should have this */
  52. # if _BYTE_ORDER == _BIG_ENDIAN
  53. # define CPU_BIG_ENDIAN
  54. # endif
  55. # endif
  56. # ifdef CPU_BIG_ENDIAN
  57. # define le32(x) (((x & 0xff000000) >> 24) | \
  58. ((x & 0x00ff0000) >> 8) | \
  59. ((x & 0x0000ff00) << 8) | \
  60. ((x & 0x000000ff) << 24))
  61. # else
  62. # define le32(x) (x)
  63. # endif
  64. #endif
  65. struct gemtek_header {
  66. uint8_t magic[4];
  67. uint8_t version[4];
  68. uint32_t product_id;
  69. uint32_t imagesz;
  70. uint32_t checksum;
  71. uint32_t fast_checksum;
  72. uint8_t build[4];
  73. uint8_t lang[4];
  74. };
  75. #define HDRLEN sizeof(struct gemtek_header)
  76. struct machines {
  77. char *desc;
  78. char *id;
  79. uint32_t maxsize;
  80. struct gemtek_header header;
  81. };
  82. struct machines mach_def[] = {
  83. {"Airlink101 AR725W", "ar725w", 0x340000,
  84. {"GMTK", "1003", le32(0x03000001), 0, 0,
  85. 0, "01\0\0", "EN\0\0"}},
  86. {"Asante AWRT-600N", "awrt600n", 0x340000,
  87. {"A600", "1005", le32(0x03000001), 0, 0,
  88. 0, "01\0\0", "EN\0\0"}},
  89. {"Linksys WRT100", "wrt100", 0x320000,
  90. {"GMTK", "1007", le32(0x03040001), 0, 0,
  91. 0, "2\0\0\0", "EN\0\0"}},
  92. {"Linksys WRT110", "wrt110", 0x320000,
  93. {"GMTK", "1007", le32(0x03040001), 0, 0,
  94. 0, "2\0\0\0", "EN\0\0"}},
  95. {0}
  96. };
  97. int
  98. main(int argc, char *argv[])
  99. {
  100. unsigned long res, flen;
  101. struct gemtek_header my_hdr;
  102. FILE *f, *f_out;
  103. int image_type = -1, index;
  104. uint8_t *buf;
  105. uint32_t crc;
  106. if (argc < 3) {
  107. fprintf(stderr, "mkheader_gemtek <uImage> <webflash image> [machine ID]\n");
  108. fprintf(stderr, " where [machine ID] is one of:\n");
  109. for (index = 0; mach_def[index].desc != 0; index++) {
  110. fprintf(stderr, " %-10s %s", mach_def[index].id, mach_def[index].desc);
  111. if (index == 0)
  112. fprintf(stderr, " (default)\n");
  113. else
  114. fprintf(stderr, "\n");
  115. }
  116. exit(-1);
  117. }
  118. if (argc == 4) {
  119. for(index = 0; mach_def[index].id != 0; index++) {
  120. if(strcmp(mach_def[index].id, argv[3]) == 0) {
  121. image_type = index;
  122. break;
  123. }
  124. }
  125. if(image_type == -1) {
  126. fprintf(stderr, "\nERROR: invalid machine type\n");
  127. exit(-1);
  128. }
  129. } else
  130. image_type = 0;
  131. printf("Opening %s...\n", argv[1]);
  132. f = fopen(argv[1], "r");
  133. if(!f) {
  134. fprintf(stderr, "\nERROR: couldn't open input image\n");
  135. exit(-1);
  136. }
  137. fseek(f, 0, SEEK_END);
  138. flen = (unsigned long) ftell(f);
  139. printf(" %lu (0x%lX) bytes long\n", flen, flen);
  140. if (flen > mach_def[image_type].maxsize) {
  141. fprintf(stderr, "\nERROR: image exceeds maximum compatible size\n");
  142. goto f_error;
  143. }
  144. buf = malloc(flen + HDRLEN);
  145. if (!buf) {
  146. fprintf(stderr, "\nERROR: couldn't allocate buffer\n");
  147. goto f_error;
  148. }
  149. rewind(f);
  150. res = fread(buf + HDRLEN, 1, flen, f);
  151. if (res != flen) {
  152. perror("Couldn't read entire file: fread()");
  153. goto f_error;
  154. }
  155. fclose(f);
  156. printf("\nCreating %s...\n", argv[2]);
  157. memcpy(&my_hdr, &mach_def[image_type].header, HDRLEN);
  158. printf(" Using %s magic\n", mach_def[image_type].desc);
  159. my_hdr.imagesz = le32(flen + HDRLEN);
  160. memcpy(my_hdr.lang, "EN", 2);
  161. memcpy(buf, &my_hdr, HDRLEN);
  162. crc = crc32(0, buf, flen + HDRLEN);
  163. printf(" CRC32: %08X\n", crc);
  164. my_hdr.checksum = le32(crc);
  165. memcpy(buf, &my_hdr, HDRLEN);
  166. printf(" Writing...\n");
  167. f_out = fopen(argv[2], "w");
  168. if(!f_out) {
  169. fprintf(stderr, "\nERROR: couldn't open output image\n");
  170. exit(-1);
  171. }
  172. fwrite(buf, 1, flen + HDRLEN, f_out);
  173. fclose(f_out);
  174. free(buf);
  175. return 0;
  176. f_error:
  177. fclose(f);
  178. exit(-1);
  179. }