mkplanexfw.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <unistd.h> /* for unlink() */
  14. #include <libgen.h>
  15. #include <getopt.h> /* for getopt() */
  16. #include <stdarg.h>
  17. #include <errno.h>
  18. #include <sys/stat.h>
  19. #include "sha1.h"
  20. #if (__BYTE_ORDER == __BIG_ENDIAN)
  21. # define HOST_TO_BE32(x) (x)
  22. # define BE32_TO_HOST(x) (x)
  23. #else
  24. # define HOST_TO_BE32(x) bswap_32(x)
  25. # define BE32_TO_HOST(x) bswap_32(x)
  26. #endif
  27. struct planex_hdr {
  28. uint8_t sha1sum[20];
  29. char version[8];
  30. uint8_t unk1[2];
  31. uint32_t datalen;
  32. } __attribute__ ((packed));
  33. struct board_info {
  34. char *id;
  35. uint32_t seed;
  36. uint8_t unk[2];
  37. uint32_t datalen;
  38. };
  39. /*
  40. * Globals
  41. */
  42. static char *ifname;
  43. static char *progname;
  44. static char *ofname;
  45. static char *version = "1.00.00";
  46. static char *board_id;
  47. static struct board_info *board;
  48. static struct board_info boards[] = {
  49. {
  50. .id = "MZK-W04NU",
  51. .seed = 2,
  52. .unk = {0x04, 0x08},
  53. .datalen = 0x770000,
  54. }, {
  55. .id = "MZK-W300NH",
  56. .seed = 4,
  57. .unk = {0x00, 0x00},
  58. .datalen = 0x770000,
  59. }, {
  60. /* terminating entry */
  61. }
  62. };
  63. /*
  64. * Message macros
  65. */
  66. #define ERR(fmt, ...) do { \
  67. fflush(0); \
  68. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  69. progname, ## __VA_ARGS__ ); \
  70. } while (0)
  71. #define ERRS(fmt, ...) do { \
  72. int save = errno; \
  73. fflush(0); \
  74. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  75. progname, ## __VA_ARGS__, strerror(save)); \
  76. } while (0)
  77. static struct board_info *find_board(char *id)
  78. {
  79. struct board_info *ret;
  80. struct board_info *board;
  81. ret = NULL;
  82. for (board = boards; board->id != NULL; board++){
  83. if (strcasecmp(id, board->id) == 0) {
  84. ret = board;
  85. break;
  86. }
  87. };
  88. return ret;
  89. }
  90. void usage(int status)
  91. {
  92. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  93. struct board_info *board;
  94. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  95. fprintf(stream,
  96. "\n"
  97. "Options:\n"
  98. " -B <board> create image for the board specified with <board>\n"
  99. " -i <file> read input from the file <file>\n"
  100. " -o <file> write output to the file <file>\n"
  101. " -v <version> set image version to <version>\n"
  102. " -h show this screen\n"
  103. );
  104. exit(status);
  105. }
  106. int main(int argc, char *argv[])
  107. {
  108. int res = EXIT_FAILURE;
  109. int buflen;
  110. int err;
  111. struct stat st;
  112. char *buf;
  113. struct planex_hdr *hdr;
  114. sha1_context ctx;
  115. uint32_t seed;
  116. FILE *outfile, *infile;
  117. progname = basename(argv[0]);
  118. while ( 1 ) {
  119. int c;
  120. c = getopt(argc, argv, "B:i:o:v:h");
  121. if (c == -1)
  122. break;
  123. switch (c) {
  124. case 'B':
  125. board_id = optarg;
  126. break;
  127. case 'i':
  128. ifname = optarg;
  129. break;
  130. case 'o':
  131. ofname = optarg;
  132. break;
  133. case 'v':
  134. version = optarg;
  135. break;
  136. case 'h':
  137. usage(EXIT_SUCCESS);
  138. break;
  139. default:
  140. usage(EXIT_FAILURE);
  141. break;
  142. }
  143. }
  144. if (board_id == NULL) {
  145. ERR("no board specified");
  146. goto err;
  147. }
  148. board = find_board(board_id);
  149. if (board == NULL) {
  150. ERR("unknown board '%s'", board_id);
  151. goto err;
  152. };
  153. if (ifname == NULL) {
  154. ERR("no input file specified");
  155. goto err;
  156. }
  157. if (ofname == NULL) {
  158. ERR("no output file specified");
  159. goto err;
  160. }
  161. err = stat(ifname, &st);
  162. if (err){
  163. ERRS("stat failed on %s", ifname);
  164. goto err;
  165. }
  166. if (st.st_size > board->datalen) {
  167. ERR("file '%s' is too big - max size: 0x%08X (exceeds %lu bytes)\n",
  168. ifname, board->datalen, st.st_size - board->datalen);
  169. goto err;
  170. }
  171. buflen = board->datalen + 0x10000;
  172. buf = malloc(buflen);
  173. if (!buf) {
  174. ERR("no memory for buffer\n");
  175. goto err;
  176. }
  177. memset(buf, 0xff, buflen);
  178. hdr = (struct planex_hdr *)buf;
  179. hdr->datalen = HOST_TO_BE32(board->datalen);
  180. hdr->unk1[0] = board->unk[0];
  181. hdr->unk1[1] = board->unk[1];
  182. snprintf(hdr->version, sizeof(hdr->version), "%s", version);
  183. infile = fopen(ifname, "r");
  184. if (infile == NULL) {
  185. ERRS("could not open \"%s\" for reading", ifname);
  186. goto err_free;
  187. }
  188. errno = 0;
  189. fread(buf + sizeof(*hdr), st.st_size, 1, infile);
  190. if (errno != 0) {
  191. ERRS("unable to read from file %s", ifname);
  192. goto err_close_in;
  193. }
  194. seed = HOST_TO_BE32(board->seed);
  195. sha1_starts(&ctx);
  196. sha1_update(&ctx, (uchar *) &seed, sizeof(seed));
  197. sha1_update(&ctx, buf + sizeof(*hdr), board->datalen);
  198. sha1_finish(&ctx, hdr->sha1sum);
  199. outfile = fopen(ofname, "w");
  200. if (outfile == NULL) {
  201. ERRS("could not open \"%s\" for writing", ofname);
  202. goto err_close_in;
  203. }
  204. errno = 0;
  205. fwrite(buf, buflen, 1, outfile);
  206. if (errno) {
  207. ERRS("unable to write to file %s", ofname);
  208. goto err_close_out;
  209. }
  210. res = EXIT_SUCCESS;
  211. out_flush:
  212. fflush(outfile);
  213. err_close_out:
  214. fclose(outfile);
  215. if (res != EXIT_SUCCESS) {
  216. unlink(ofname);
  217. }
  218. err_close_in:
  219. fclose(infile);
  220. err_free:
  221. free(buf);
  222. err:
  223. return res;
  224. }