addpattern.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 = "E2100L",
  98. .pattern = "NL1X",
  99. .hw_ver = 0x00,
  100. .sn = 0x0f,
  101. .flags = {0x3f, 0x00},
  102. },
  103. {
  104. .id = "WRT160NL",
  105. .pattern = "NL16",
  106. .hw_ver = 0x00,
  107. .sn = 0x0f,
  108. .flags = {0x3f, 0x00},
  109. },
  110. {
  111. .id = "mynet-rext",
  112. .pattern = "WDHNSTFH",
  113. .hw_ver = 0x00,
  114. .sn = 0x00,
  115. .flags = {0x3f, 0x00},
  116. }, {
  117. /* Terminating entry */
  118. .id = NULL,
  119. }
  120. };
  121. /**********************************************************************/
  122. void usage(void) __attribute__ (( __noreturn__ ));
  123. void usage(void)
  124. {
  125. 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");
  126. exit(EXIT_FAILURE);
  127. }
  128. struct board_info *find_board(char *id)
  129. {
  130. struct board_info *board;
  131. for (board = boards; board->id != NULL; board++)
  132. if (strcasecmp(id, board->id) == 0)
  133. return board;
  134. return NULL;
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. char buf[1024]; /* keep this at 1k or adjust garbage calc below */
  139. struct code_header *hdr;
  140. FILE *in = stdin;
  141. FILE *out = stdout;
  142. char *ifn = NULL;
  143. char *ofn = NULL;
  144. char *pattern = CODE_PATTERN;
  145. char *pbotpat = PBOT_PATTERN;
  146. char *version = CYBERTAN_VERSION;
  147. char *board_id = NULL;
  148. struct board_info *board = NULL;
  149. int gflag = 0;
  150. int pbotflag = 0;
  151. int c;
  152. int v0, v1, v2;
  153. size_t off, n;
  154. time_t t;
  155. struct tm *ptm;
  156. fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
  157. hdr = (struct code_header *) buf;
  158. memset(hdr, 0, sizeof(struct code_header));
  159. while ((c = getopt(argc, argv, "i:o:p:s:gbv:01245hr:B:")) != -1) {
  160. switch (c) {
  161. case 'i':
  162. ifn = optarg;
  163. break;
  164. case 'o':
  165. ofn = optarg;
  166. break;
  167. case 'p':
  168. pattern = optarg;
  169. break;
  170. case 's':
  171. hdr->sn = (unsigned char) atoi (optarg);
  172. break;
  173. case 'g':
  174. gflag = 1;
  175. break;
  176. case 'b':
  177. pbotflag = 1;
  178. break;
  179. case 'v': /* extension to allow setting version */
  180. version = optarg;
  181. break;
  182. case '0':
  183. hdr->hw_ver = 0;
  184. break;
  185. case '1':
  186. hdr->hw_ver = 1;
  187. break;
  188. case '2': /* new 54G v2.2 and 54GS v1.1 flags */
  189. hdr->hw_ver = 1;
  190. hdr->flags[0] |= SUPPORT_4712_CHIP;
  191. hdr->flags[0] |= SUPPORT_INTEL_FLASH;
  192. hdr->flags[0] |= SUPPORT_5325E_SWITCH;
  193. break;
  194. case '4':
  195. /* V4 firmware sets the flags to 0x1f */
  196. hdr->hw_ver = 0;
  197. hdr->flags[0] = 0x1f;
  198. break;
  199. case '5':
  200. /* V5 is appended to trxV2 image */
  201. hdr->stable[0] = 0x73; // force image to be stable
  202. hdr->stable[1] = 0x00;
  203. hdr->try1[0] = 0x74; // force try1 to be set
  204. hdr->try1[1] = 0x00;
  205. hdr->try2[0] = hdr->try2[1] = 0xFF;
  206. hdr->try3[0] = hdr->try3[1] = 0xFF;
  207. break;
  208. case 'r':
  209. hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
  210. break;
  211. case 'B':
  212. board_id = optarg;
  213. break;
  214. case 'h':
  215. default:
  216. usage();
  217. }
  218. }
  219. if (optind != argc || optind == 1) {
  220. fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
  221. usage();
  222. }
  223. if (board_id) {
  224. board = find_board(board_id);
  225. if (board == NULL) {
  226. fprintf(stderr, "unknown board \"%s\"\n", board_id);
  227. usage();
  228. }
  229. pattern = board->pattern;
  230. hdr->hw_ver = board->hw_ver;
  231. hdr->sn = board->sn;
  232. hdr->flags[0] = board->flags[0];
  233. hdr->flags[1] = board->flags[1];
  234. }
  235. if (strlen(pattern) > 8) {
  236. fprintf(stderr, "illegal pattern \"%s\"\n", pattern);
  237. usage();
  238. }
  239. if (ifn && !(in = fopen(ifn, "r"))) {
  240. fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
  241. usage();
  242. }
  243. if (ofn && !(out = fopen(ofn, "w"))) {
  244. fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
  245. usage();
  246. }
  247. if (time(&t) == (time_t)(-1)) {
  248. fprintf(stderr, "time call failed\n");
  249. return EXIT_FAILURE;
  250. }
  251. ptm = localtime(&t);
  252. if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
  253. fprintf(stderr, "bad version string \"%s\"\n", version);
  254. return EXIT_FAILURE;
  255. }
  256. memcpy(hdr->magic, pattern, strlen(pattern));
  257. if (pbotflag)
  258. memcpy(&hdr->magic[4], pbotpat, 4);
  259. hdr->fwdate[0] = ptm->tm_year % 100;
  260. hdr->fwdate[1] = ptm->tm_mon + 1;
  261. hdr->fwdate[2] = ptm->tm_mday;
  262. hdr->fwvern[0] = v0;
  263. hdr->fwvern[1] = v1;
  264. hdr->fwvern[2] = v2;
  265. memcpy(hdr->id, CODE_ID, strlen(CODE_ID));
  266. off = sizeof(struct code_header);
  267. fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
  268. v0, v1, v2,
  269. hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
  270. while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
  271. off = 0;
  272. if (n < sizeof(buf)) {
  273. if (ferror(in)) {
  274. FREAD_ERROR:
  275. fprintf(stderr, "fread error\n");
  276. return EXIT_FAILURE;
  277. }
  278. if (gflag) {
  279. gflag = sizeof(buf) - n;
  280. memset(buf + n, 0xff, gflag);
  281. fprintf(stderr, "adding %d bytes of garbage\n", gflag);
  282. n = sizeof(buf);
  283. }
  284. }
  285. if (!fwrite(buf, n, 1, out)) {
  286. FWRITE_ERROR:
  287. fprintf(stderr, "fwrite error\n");
  288. return EXIT_FAILURE;
  289. }
  290. }
  291. if (ferror(in)) {
  292. goto FREAD_ERROR;
  293. }
  294. if (fflush(out)) {
  295. goto FWRITE_ERROR;
  296. }
  297. fclose(in);
  298. fclose(out);
  299. return EXIT_SUCCESS;
  300. }