addpattern.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  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. /* July 29, 2004
  19. *
  20. * This is a hacked replacement for the 'addpattern' utility used to
  21. * create wrt54g .bin firmware files. It isn't pretty, but it does
  22. * the job for me.
  23. *
  24. * Extensions:
  25. * -v allows setting the version string on the command line.
  26. * -{0|1} sets the (currently ignored) hw_ver flag in the header
  27. * to 0 or 1 respectively.
  28. */
  29. /* January 12, 2005
  30. *
  31. * Modified by rodent at rodent dot za dot net
  32. * Support added for the new WRT54G v2.2 and WRT54GS v1.1 "flags"
  33. * Without the flags set to 0x7, the above units will refuse to flash.
  34. *
  35. * Extensions:
  36. * -{0|1|2} sets {0|1} sets hw_ver flag to 0/1. {2} sets hw_ver to 1
  37. * and adds the new hardware "flags" for the v2.2/v1.1 units
  38. */
  39. /* January 1, 2007
  40. *
  41. * Modified by juan.i.gonzalez at subdown dot net
  42. * Support added for the AG241v2 and similar
  43. *
  44. * Extensions:
  45. * -r #.# adds revision hardware flags. AG241v2 and similar.
  46. *
  47. * AG241V2 firmware sets the hw_ver to 0x44.
  48. *
  49. * Example: -r 2.0
  50. *
  51. * Convert 2.0 to 20 to be an integer, and add 0x30 to skip special ASCII
  52. * #define HW_Version ((HW_REV * 10) + 0x30) -> from cyutils.h
  53. */
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <time.h>
  58. #include <unistd.h>
  59. #include <sys/stat.h>
  60. /**********************************************************************/
  61. #define CODE_ID "U2ND" /* from code_pattern.h */
  62. #define CODE_PATTERN "W54S" /* from code_pattern.h */
  63. #define PBOT_PATTERN "PBOT"
  64. #define CYBERTAN_VERSION "v3.37.2" /* from cyutils.h */
  65. /* WRT54G v2.2 and WRT54GS v1.1 "flags" (from 3.37.32 firmware cyutils.h) */
  66. #define SUPPORT_4712_CHIP 0x0001
  67. #define SUPPORT_INTEL_FLASH 0x0002
  68. #define SUPPORT_5325E_SWITCH 0x0004
  69. /* (from 3.00.24 firmware cyutils.h) */
  70. #define SUPPORT_4704_CHIP 0x0008
  71. #define SUPPORT_5352E_CHIP 0x0010
  72. /* (from WD My Net Wi-Fi Range Extender's cyutils.s) */
  73. #define SUPPORT_4703_CHIP 0x0020
  74. struct code_header { /* from cyutils.h */
  75. char magic[8];
  76. char fwdate[3];
  77. char fwvern[3];
  78. char id[4]; /* U2ND */
  79. char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
  80. unsigned char sn; // Serial Number
  81. unsigned char flags[2]; /* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
  82. unsigned char stable[2]; // The image is stable (for dual image)
  83. unsigned char try1[2]; // Try to boot image first time (for dual image)
  84. unsigned char try2[2]; // Try to boot image second time (for dual image)
  85. unsigned char try3[2]; // Try to boot image third time (for dual_image)
  86. unsigned char res3[2];
  87. } ;
  88. struct board_info {
  89. char *id;
  90. char *pattern;
  91. char hw_ver;
  92. char sn;
  93. char flags[2];
  94. };
  95. struct board_info boards[] = {
  96. {
  97. .id = "WRT160NL",
  98. .pattern = "NL16",
  99. .hw_ver = 0x00,
  100. .sn = 0x0f,
  101. .flags = {0x3f, 0x00},
  102. },
  103. {
  104. .id = "mynet-rext",
  105. .pattern = "WDHNSTFH",
  106. .hw_ver = 0x00,
  107. .sn = 0x00,
  108. .flags = {0x3f, 0x00},
  109. }, {
  110. /* Terminating entry */
  111. .id = NULL,
  112. }
  113. };
  114. /**********************************************************************/
  115. void usage(void) __attribute__ (( __noreturn__ ));
  116. void usage(void)
  117. {
  118. fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-B board_id] [-p pattern] [-s serial] [-g] [-b] [-v v#.#.#] [-r #.#] [-{0|1|2|4|5}] -h\n");
  119. exit(EXIT_FAILURE);
  120. }
  121. struct board_info *find_board(char *id)
  122. {
  123. struct board_info *board;
  124. for (board = boards; board->id != NULL; board++)
  125. if (strcasecmp(id, board->id) == 0)
  126. return board;
  127. return NULL;
  128. }
  129. int main(int argc, char **argv)
  130. {
  131. char buf[1024]; /* keep this at 1k or adjust garbage calc below */
  132. struct code_header *hdr;
  133. FILE *in = stdin;
  134. FILE *out = stdout;
  135. char *ifn = NULL;
  136. char *ofn = NULL;
  137. char *pattern = CODE_PATTERN;
  138. char *pbotpat = PBOT_PATTERN;
  139. char *version = CYBERTAN_VERSION;
  140. char *board_id = NULL;
  141. struct board_info *board = NULL;
  142. int gflag = 0;
  143. int pbotflag = 0;
  144. int c;
  145. int v0, v1, v2;
  146. size_t off, n;
  147. time_t t;
  148. struct tm *ptm;
  149. fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
  150. hdr = (struct code_header *) buf;
  151. memset(hdr, 0, sizeof(struct code_header));
  152. while ((c = getopt(argc, argv, "i:o:p:s:gbv:01245hr:B:")) != -1) {
  153. switch (c) {
  154. case 'i':
  155. ifn = optarg;
  156. break;
  157. case 'o':
  158. ofn = optarg;
  159. break;
  160. case 'p':
  161. pattern = optarg;
  162. break;
  163. case 's':
  164. hdr->sn = (unsigned char) atoi (optarg);
  165. break;
  166. case 'g':
  167. gflag = 1;
  168. break;
  169. case 'b':
  170. pbotflag = 1;
  171. break;
  172. case 'v': /* extension to allow setting version */
  173. version = optarg;
  174. break;
  175. case '0':
  176. hdr->hw_ver = 0;
  177. break;
  178. case '1':
  179. hdr->hw_ver = 1;
  180. break;
  181. case '2': /* new 54G v2.2 and 54GS v1.1 flags */
  182. hdr->hw_ver = 1;
  183. hdr->flags[0] |= SUPPORT_4712_CHIP;
  184. hdr->flags[0] |= SUPPORT_INTEL_FLASH;
  185. hdr->flags[0] |= SUPPORT_5325E_SWITCH;
  186. break;
  187. case '4':
  188. /* V4 firmware sets the flags to 0x1f */
  189. hdr->hw_ver = 0;
  190. hdr->flags[0] = 0x1f;
  191. break;
  192. case '5':
  193. /* V5 is appended to trxV2 image */
  194. hdr->stable[0] = 0x73; // force image to be stable
  195. hdr->stable[1] = 0x00;
  196. hdr->try1[0] = 0x74; // force try1 to be set
  197. hdr->try1[1] = 0x00;
  198. hdr->try2[0] = hdr->try2[1] = 0xFF;
  199. hdr->try3[0] = hdr->try3[1] = 0xFF;
  200. break;
  201. case 'r':
  202. hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
  203. break;
  204. case 'B':
  205. board_id = optarg;
  206. break;
  207. case 'h':
  208. default:
  209. usage();
  210. }
  211. }
  212. if (optind != argc || optind == 1) {
  213. fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
  214. usage();
  215. }
  216. if (board_id) {
  217. board = find_board(board_id);
  218. if (board == NULL) {
  219. fprintf(stderr, "unknown board \"%s\"\n", board_id);
  220. usage();
  221. }
  222. pattern = board->pattern;
  223. hdr->hw_ver = board->hw_ver;
  224. hdr->sn = board->sn;
  225. hdr->flags[0] = board->flags[0];
  226. hdr->flags[1] = board->flags[1];
  227. }
  228. if (strlen(pattern) > 8) {
  229. fprintf(stderr, "illegal pattern \"%s\"\n", pattern);
  230. usage();
  231. }
  232. if (ifn && !(in = fopen(ifn, "r"))) {
  233. fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
  234. usage();
  235. }
  236. if (ofn && !(out = fopen(ofn, "w"))) {
  237. fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
  238. usage();
  239. }
  240. if (time(&t) == (time_t)(-1)) {
  241. fprintf(stderr, "time call failed\n");
  242. return EXIT_FAILURE;
  243. }
  244. ptm = localtime(&t);
  245. if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
  246. fprintf(stderr, "bad version string \"%s\"\n", version);
  247. return EXIT_FAILURE;
  248. }
  249. memcpy(hdr->magic, pattern, strlen(pattern));
  250. if (pbotflag)
  251. memcpy(&hdr->magic[4], pbotpat, 4);
  252. hdr->fwdate[0] = ptm->tm_year % 100;
  253. hdr->fwdate[1] = ptm->tm_mon + 1;
  254. hdr->fwdate[2] = ptm->tm_mday;
  255. hdr->fwvern[0] = v0;
  256. hdr->fwvern[1] = v1;
  257. hdr->fwvern[2] = v2;
  258. memcpy(hdr->id, CODE_ID, strlen(CODE_ID));
  259. off = sizeof(struct code_header);
  260. fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
  261. v0, v1, v2,
  262. hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
  263. while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
  264. off = 0;
  265. if (n < sizeof(buf)) {
  266. if (ferror(in)) {
  267. FREAD_ERROR:
  268. fprintf(stderr, "fread error\n");
  269. return EXIT_FAILURE;
  270. }
  271. if (gflag) {
  272. gflag = sizeof(buf) - n;
  273. memset(buf + n, 0xff, gflag);
  274. fprintf(stderr, "adding %d bytes of garbage\n", gflag);
  275. n = sizeof(buf);
  276. }
  277. }
  278. if (!fwrite(buf, n, 1, out)) {
  279. FWRITE_ERROR:
  280. fprintf(stderr, "fwrite error\n");
  281. return EXIT_FAILURE;
  282. }
  283. }
  284. if (ferror(in)) {
  285. goto FREAD_ERROR;
  286. }
  287. if (fflush(out)) {
  288. goto FWRITE_ERROR;
  289. }
  290. fclose(in);
  291. fclose(out);
  292. return EXIT_SUCCESS;
  293. }