addpattern.c 5.8 KB

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