addpattern.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. struct code_header { /* from cyutils.h */
  70. char magic[4];
  71. char res1[4]; /* for extra magic */
  72. char fwdate[3];
  73. char fwvern[3];
  74. char id[4]; /* U2ND */
  75. char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
  76. char unused;
  77. unsigned char flags[2]; /* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
  78. unsigned char res2[10];
  79. } ;
  80. /**********************************************************************/
  81. void usage(void) __attribute__ (( __noreturn__ ));
  82. void usage(void)
  83. {
  84. fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-p pattern] [-g] [-b] [-v v#.#.#] [-r #.#] [-{0|1|2|4}] -h\n");
  85. exit(EXIT_FAILURE);
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. char buf[1024]; /* keep this at 1k or adjust garbage calc below */
  90. struct code_header *hdr;
  91. FILE *in = stdin;
  92. FILE *out = stdout;
  93. char *ifn = NULL;
  94. char *ofn = NULL;
  95. char *pattern = CODE_PATTERN;
  96. char *pbotpat = PBOT_PATTERN;
  97. char *version = CYBERTAN_VERSION;
  98. int gflag = 0;
  99. int pbotflag = 0;
  100. int c;
  101. int v0, v1, v2;
  102. size_t off, n;
  103. time_t t;
  104. struct tm *ptm;
  105. hdr = (struct code_header *) buf;
  106. memset(hdr, 0, sizeof(struct code_header));
  107. while ((c = getopt(argc, argv, "i:o:p:gbv:0124hr:")) != -1) {
  108. switch (c) {
  109. case 'i':
  110. ifn = optarg;
  111. break;
  112. case 'o':
  113. ofn = optarg;
  114. break;
  115. case 'p':
  116. pattern = optarg;
  117. break;
  118. case 'g':
  119. gflag = 1;
  120. break;
  121. case 'b':
  122. pbotflag = 1;
  123. break;
  124. case 'v': /* extension to allow setting version */
  125. version = optarg;
  126. break;
  127. case '0':
  128. hdr->hw_ver = 0;
  129. break;
  130. case '1':
  131. hdr->hw_ver = 1;
  132. break;
  133. case '2': /* new 54G v2.2 and 54GS v1.1 flags */
  134. hdr->hw_ver = 1;
  135. hdr->flags[0] |= SUPPORT_4712_CHIP;
  136. hdr->flags[0] |= SUPPORT_INTEL_FLASH;
  137. hdr->flags[0] |= SUPPORT_5325E_SWITCH;
  138. break;
  139. case '4':
  140. /* V4 firmware sets the flags to 0x1f */
  141. hdr->hw_ver = 0;
  142. hdr->flags[0] = 0x1f;
  143. break;
  144. case 'r':
  145. hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
  146. break;
  147. case 'h':
  148. default:
  149. usage();
  150. }
  151. }
  152. if (optind != argc || optind == 1) {
  153. fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
  154. usage();
  155. }
  156. if (strlen(pattern) != 4) {
  157. fprintf(stderr, "illegal pattern \"%s\": length != 4\n", pattern);
  158. usage();
  159. }
  160. if (ifn && !(in = fopen(ifn, "r"))) {
  161. fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
  162. usage();
  163. }
  164. if (ofn && !(out = fopen(ofn, "w"))) {
  165. fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
  166. usage();
  167. }
  168. if (time(&t) == (time_t)(-1)) {
  169. fprintf(stderr, "time call failed\n");
  170. return EXIT_FAILURE;
  171. }
  172. ptm = localtime(&t);
  173. if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
  174. fprintf(stderr, "bad version string \"%s\"\n", version);
  175. return EXIT_FAILURE;
  176. }
  177. memcpy(&hdr->magic, pattern, 4);
  178. if (pbotflag)
  179. memcpy(&hdr->res1, pbotpat, 4);
  180. hdr->fwdate[0] = ptm->tm_year % 100;
  181. hdr->fwdate[1] = ptm->tm_mon + 1;
  182. hdr->fwdate[2] = ptm->tm_mday;
  183. hdr->fwvern[0] = v0;
  184. hdr->fwvern[1] = v1;
  185. hdr->fwvern[2] = v2;
  186. memcpy(&hdr->id, CODE_ID, strlen(CODE_ID));
  187. off = sizeof(struct code_header);
  188. fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
  189. v0, v1, v2,
  190. hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
  191. while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
  192. off = 0;
  193. if (n < sizeof(buf)) {
  194. if (ferror(in)) {
  195. FREAD_ERROR:
  196. fprintf(stderr, "fread error\n");
  197. return EXIT_FAILURE;
  198. }
  199. if (gflag) {
  200. gflag = sizeof(buf) - n;
  201. memset(buf + n, 0xff, gflag);
  202. n = sizeof(buf);
  203. }
  204. }
  205. if (!fwrite(buf, n, 1, out)) {
  206. FWRITE_ERROR:
  207. fprintf(stderr, "fwrite error\n");
  208. return EXIT_FAILURE;
  209. }
  210. }
  211. if (ferror(in)) {
  212. goto FREAD_ERROR;
  213. }
  214. if (fflush(out)) {
  215. goto FWRITE_ERROR;
  216. }
  217. fclose(in);
  218. fclose(out);
  219. return EXIT_SUCCESS;
  220. }