1
0

addpattern.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
  106. hdr = (struct code_header *) buf;
  107. memset(hdr, 0, sizeof(struct code_header));
  108. while ((c = getopt(argc, argv, "i:o:p:gbv:0124hr:")) != -1) {
  109. switch (c) {
  110. case 'i':
  111. ifn = optarg;
  112. break;
  113. case 'o':
  114. ofn = optarg;
  115. break;
  116. case 'p':
  117. pattern = optarg;
  118. break;
  119. case 'g':
  120. gflag = 1;
  121. break;
  122. case 'b':
  123. pbotflag = 1;
  124. break;
  125. case 'v': /* extension to allow setting version */
  126. version = optarg;
  127. break;
  128. case '0':
  129. hdr->hw_ver = 0;
  130. break;
  131. case '1':
  132. hdr->hw_ver = 1;
  133. break;
  134. case '2': /* new 54G v2.2 and 54GS v1.1 flags */
  135. hdr->hw_ver = 1;
  136. hdr->flags[0] |= SUPPORT_4712_CHIP;
  137. hdr->flags[0] |= SUPPORT_INTEL_FLASH;
  138. hdr->flags[0] |= SUPPORT_5325E_SWITCH;
  139. break;
  140. case '4':
  141. /* V4 firmware sets the flags to 0x1f */
  142. hdr->hw_ver = 0;
  143. hdr->flags[0] = 0x1f;
  144. break;
  145. case 'r':
  146. hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
  147. break;
  148. case 'h':
  149. default:
  150. usage();
  151. }
  152. }
  153. if (optind != argc || optind == 1) {
  154. fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
  155. usage();
  156. }
  157. if (strlen(pattern) != 4) {
  158. fprintf(stderr, "illegal pattern \"%s\": length != 4\n", pattern);
  159. usage();
  160. }
  161. if (ifn && !(in = fopen(ifn, "r"))) {
  162. fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
  163. usage();
  164. }
  165. if (ofn && !(out = fopen(ofn, "w"))) {
  166. fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
  167. usage();
  168. }
  169. if (time(&t) == (time_t)(-1)) {
  170. fprintf(stderr, "time call failed\n");
  171. return EXIT_FAILURE;
  172. }
  173. ptm = localtime(&t);
  174. if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
  175. fprintf(stderr, "bad version string \"%s\"\n", version);
  176. return EXIT_FAILURE;
  177. }
  178. memcpy(&hdr->magic, pattern, 4);
  179. if (pbotflag)
  180. memcpy(&hdr->res1, pbotpat, 4);
  181. hdr->fwdate[0] = ptm->tm_year % 100;
  182. hdr->fwdate[1] = ptm->tm_mon + 1;
  183. hdr->fwdate[2] = ptm->tm_mday;
  184. hdr->fwvern[0] = v0;
  185. hdr->fwvern[1] = v1;
  186. hdr->fwvern[2] = v2;
  187. memcpy(&hdr->id, CODE_ID, strlen(CODE_ID));
  188. off = sizeof(struct code_header);
  189. fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
  190. v0, v1, v2,
  191. hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
  192. while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
  193. off = 0;
  194. if (n < sizeof(buf)) {
  195. if (ferror(in)) {
  196. FREAD_ERROR:
  197. fprintf(stderr, "fread error\n");
  198. return EXIT_FAILURE;
  199. }
  200. if (gflag) {
  201. gflag = sizeof(buf) - n;
  202. memset(buf + n, 0xff, gflag);
  203. fprintf(stderr, "adding %d bytes of garbage\n", gflag);
  204. n = sizeof(buf);
  205. }
  206. }
  207. if (!fwrite(buf, n, 1, out)) {
  208. FWRITE_ERROR:
  209. fprintf(stderr, "fwrite error\n");
  210. return EXIT_FAILURE;
  211. }
  212. }
  213. if (ferror(in)) {
  214. goto FREAD_ERROR;
  215. }
  216. if (fflush(out)) {
  217. goto FWRITE_ERROR;
  218. }
  219. fclose(in);
  220. fclose(out);
  221. return EXIT_SUCCESS;
  222. }