mkdapimg.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <libgen.h>
  6. #include <stdarg.h>
  7. #include <getopt.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <netinet/in.h> // htonl
  11. // Usage: mkdapimg [-p] [-m <model>] -s <sig> -i <input> -o <output>
  12. //
  13. // e.g.: mkdapimg -s RT3052-AP-DAP1350-3 -i sysupgarde.bin -o factory.bin
  14. //
  15. // If the model string <model> is not given, we will assume that
  16. // the leading characters upto the first "-" is the model.
  17. //
  18. // The "-p" (patch) option is used to patch the exisiting image with the
  19. // specified model and signature.
  20. // The "-x" (fix) option will recalculate the payload size and checksum
  21. // during the patch mode operation.
  22. // The img_hdr_struct was taken from the D-Link SDK:
  23. // DAP-1350_A1_FW1.11NA_GPL/GPL_Source_Code/Uboot/DAP-1350/httpd/header.h
  24. #define MAX_MODEL_NAME_LEN 20
  25. #define MAX_SIG_LEN 30
  26. #define MAX_REGION_LEN 4
  27. #define MAX_VERSION_LEN 12
  28. struct img_hdr_struct {
  29. uint32_t checksum;
  30. char model[MAX_MODEL_NAME_LEN];
  31. char sig[MAX_SIG_LEN];
  32. uint8_t partition;
  33. uint8_t hdr_len;
  34. uint8_t rsv1;
  35. uint8_t rsv2;
  36. uint32_t flash_byte_cnt;
  37. } imghdr ;
  38. char *progname;
  39. void
  40. perrexit(int code, char *msg)
  41. {
  42. fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(errno));
  43. exit(code);
  44. }
  45. void
  46. usage()
  47. {
  48. fprintf(stderr, "usage: %s [-p] [-m model] [-r region] [-v version] -s signature -i input -o output\n", progname);
  49. exit(1);
  50. }
  51. int
  52. main(int ac, char *av[])
  53. {
  54. char model[MAX_MODEL_NAME_LEN+1];
  55. char signature[MAX_SIG_LEN+1];
  56. char region[MAX_REGION_LEN+1];
  57. char version[MAX_VERSION_LEN+1];
  58. int patchmode = 0;
  59. int fixmode = 0;
  60. int have_regionversion = 0;
  61. FILE *ifile, *ofile;
  62. int c;
  63. uint32_t cksum;
  64. uint32_t bcnt;
  65. progname = basename(av[0]);
  66. memset(model, 0, sizeof(model));
  67. memset(signature, 0, sizeof(signature));
  68. memset(region, 0, sizeof(region));
  69. memset(version, 0, sizeof(version));
  70. while ( 1 ) {
  71. int c;
  72. c = getopt(ac, av, "pxm:r:v:s:i:o:");
  73. if (c == -1)
  74. break;
  75. switch (c) {
  76. case 'p':
  77. patchmode = 1;
  78. break;
  79. case 'x':
  80. fixmode = 1;
  81. break;
  82. case 'm':
  83. if (strlen(optarg) > MAX_MODEL_NAME_LEN) {
  84. fprintf(stderr, "%s: model name exceeds %d chars\n",
  85. progname, MAX_MODEL_NAME_LEN);
  86. exit(1);
  87. }
  88. strcpy(model, optarg);
  89. break;
  90. case 'r':
  91. if (strlen(optarg) > MAX_REGION_LEN) {
  92. fprintf(stderr, "%s: region exceeds %d chars\n",
  93. progname, MAX_REGION_LEN);
  94. exit(1);
  95. }
  96. have_regionversion = 1;
  97. strcpy(region, optarg);
  98. break;
  99. case 'v':
  100. if (strlen(optarg) > MAX_VERSION_LEN) {
  101. fprintf(stderr, "%s: version exceeds %d chars\n",
  102. progname, MAX_VERSION_LEN);
  103. exit(1);
  104. }
  105. have_regionversion = 1;
  106. strcpy(version, optarg);
  107. break;
  108. case 's':
  109. if (strlen(optarg) > MAX_SIG_LEN) {
  110. fprintf(stderr, "%s: signature exceeds %d chars\n",
  111. progname, MAX_SIG_LEN);
  112. exit(1);
  113. }
  114. strcpy(signature, optarg);
  115. break;
  116. case 'i':
  117. if ((ifile = fopen(optarg, "r")) == NULL)
  118. perrexit(1, optarg);
  119. break;
  120. case 'o':
  121. if ((ofile = fopen(optarg, "w")) == NULL)
  122. perrexit(1, optarg);
  123. break;
  124. default:
  125. usage();
  126. }
  127. }
  128. if (signature[0] == 0 || ifile == NULL || ofile == NULL) {
  129. usage();
  130. }
  131. if (model[0] == 0) {
  132. char *p = strchr(signature, '-');
  133. if (p == NULL) {
  134. fprintf(stderr, "%s: model name unknown\n", progname);
  135. exit(1);
  136. }
  137. if (p - signature > MAX_MODEL_NAME_LEN) {
  138. *p = 0;
  139. fprintf(stderr, "%s: auto model name failed, string %s too long\n", progname, signature);
  140. exit(1);
  141. }
  142. strncpy(model, signature, p - signature);
  143. }
  144. if (patchmode) {
  145. if (fread(&imghdr, sizeof(imghdr), 1, ifile) < 0)
  146. perrexit(2, "fread on input");
  147. }
  148. for (bcnt = 0, cksum = 0 ; (c = fgetc(ifile)) != EOF ; bcnt++)
  149. cksum += c & 0xff;
  150. if (fseek(ifile, patchmode ? sizeof(imghdr) : 0, SEEK_SET) < 0)
  151. perrexit(2, "fseek on input");
  152. if (patchmode == 0) {
  153. // Fill in the header
  154. memset(&imghdr, 0, sizeof(imghdr));
  155. imghdr.checksum = htonl(cksum);
  156. imghdr.partition = 0 ; // don't care?
  157. imghdr.hdr_len = sizeof(imghdr);
  158. if (have_regionversion) {
  159. imghdr.hdr_len += MAX_REGION_LEN;
  160. imghdr.hdr_len += MAX_VERSION_LEN;
  161. }
  162. imghdr.flash_byte_cnt = htonl(bcnt);
  163. } else {
  164. if (ntohl(imghdr.checksum) != cksum) {
  165. fprintf(stderr, "%s: patch mode, checksum mismatch\n",
  166. progname);
  167. if (fixmode) {
  168. fprintf(stderr, "%s: fixing\n", progname);
  169. imghdr.checksum = htonl(cksum);
  170. } else
  171. exit(3);
  172. } else if (ntohl(imghdr.flash_byte_cnt) != bcnt) {
  173. fprintf(stderr, "%s: patch mode, size mismatch\n",
  174. progname);
  175. if (fixmode) {
  176. fprintf(stderr, "%s: fixing\n", progname);
  177. imghdr.flash_byte_cnt = htonl(bcnt);
  178. } else
  179. exit(3);
  180. }
  181. }
  182. strncpy(imghdr.model, model, MAX_MODEL_NAME_LEN);
  183. strncpy(imghdr.sig, signature, MAX_SIG_LEN);
  184. if (fwrite(&imghdr, sizeof(imghdr), 1, ofile) < 0)
  185. perrexit(2, "fwrite header on output");
  186. if (have_regionversion) {
  187. if (fwrite(&region, MAX_REGION_LEN, 1, ofile) < 0)
  188. perrexit(2, "fwrite header on output");
  189. if (fwrite(&version, MAX_VERSION_LEN, 1, ofile) < 0)
  190. perrexit(2, "fwrite header on output");
  191. }
  192. while ((c = fgetc(ifile)) != EOF) {
  193. if (fputc(c, ofile) == EOF)
  194. perrexit(2, "fputc on output");
  195. }
  196. if (ferror(ifile))
  197. perrexit(2, "fgetc on input");
  198. fclose(ofile);
  199. fclose(ifile);
  200. }