addpattern.c 8.2 KB

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