ptgen: add Chromium OS kernel partition support
[project/firmware-utils.git] / src / addpattern.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
4 */
5
6 /* July 29, 2004
7 *
8 * This is a hacked replacement for the 'addpattern' utility used to
9 * create wrt54g .bin firmware files. It isn't pretty, but it does
10 * the job for me.
11 *
12 * Extensions:
13 * -v allows setting the version string on the command line.
14 * -{0|1} sets the (currently ignored) hw_ver flag in the header
15 * to 0 or 1 respectively.
16 */
17
18 /* January 12, 2005
19 *
20 * Modified by rodent at rodent dot za dot net
21 * Support added for the new WRT54G v2.2 and WRT54GS v1.1 "flags"
22 * Without the flags set to 0x7, the above units will refuse to flash.
23 *
24 * Extensions:
25 * -{0|1|2} sets {0|1} sets hw_ver flag to 0/1. {2} sets hw_ver to 1
26 * and adds the new hardware "flags" for the v2.2/v1.1 units
27 */
28
29 /* January 1, 2007
30 *
31 * Modified by juan.i.gonzalez at subdown dot net
32 * Support added for the AG241v2 and similar
33 *
34 * Extensions:
35 * -r #.# adds revision hardware flags. AG241v2 and similar.
36 *
37 * AG241V2 firmware sets the hw_ver to 0x44.
38 *
39 * Example: -r 2.0
40 *
41 * Convert 2.0 to 20 to be an integer, and add 0x30 to skip special ASCII
42 * #define HW_Version ((HW_REV * 10) + 0x30) -> from cyutils.h
43 */
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <time.h>
50 #include <unistd.h>
51 #include <sys/stat.h>
52
53 /**********************************************************************/
54
55 #define CODE_ID "U2ND" /* from code_pattern.h */
56 #define CODE_PATTERN "W54S" /* from code_pattern.h */
57 #define PBOT_PATTERN "PBOT"
58
59 #define CYBERTAN_VERSION "v3.37.2" /* from cyutils.h */
60
61 /* WRT54G v2.2 and WRT54GS v1.1 "flags" (from 3.37.32 firmware cyutils.h) */
62 #define SUPPORT_4712_CHIP 0x0001
63 #define SUPPORT_INTEL_FLASH 0x0002
64 #define SUPPORT_5325E_SWITCH 0x0004
65 /* (from 3.00.24 firmware cyutils.h) */
66 #define SUPPORT_4704_CHIP 0x0008
67 #define SUPPORT_5352E_CHIP 0x0010
68 /* (from WD My Net Wi-Fi Range Extender's cyutils.s) */
69 #define SUPPORT_4703_CHIP 0x0020
70
71 struct code_header { /* from cyutils.h */
72 char magic[8];
73 char fwdate[3];
74 char fwvern[3];
75 char id[4]; /* U2ND */
76 char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
77
78 unsigned char sn; // Serial Number
79 unsigned char flags[2]; /* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
80 unsigned char stable[2]; // The image is stable (for dual image)
81 unsigned char try1[2]; // Try to boot image first time (for dual image)
82 unsigned char try2[2]; // Try to boot image second time (for dual image)
83 unsigned char try3[2]; // Try to boot image third time (for dual_image)
84 unsigned char res3[2];
85 } ;
86
87 struct board_info {
88 char *id;
89 char *pattern;
90 char hw_ver;
91 char sn;
92 char flags[2];
93 };
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 /**********************************************************************/
123
124 void usage(void) __attribute__ (( __noreturn__ ));
125
126 void usage(void)
127 {
128 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");
129 exit(EXIT_FAILURE);
130 }
131
132 static time_t source_date_epoch = -1;
133 static void set_source_date_epoch() {
134 char *env = getenv("SOURCE_DATE_EPOCH");
135 char *endptr = env;
136 errno = 0;
137 if (env && *env) {
138 source_date_epoch = strtoull(env, &endptr, 10);
139 if (errno || (endptr && *endptr != '\0')) {
140 fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
141 exit(1);
142 }
143 }
144 }
145
146 struct board_info *find_board(char *id)
147 {
148 struct board_info *board;
149
150 for (board = boards; board->id != NULL; board++)
151 if (strcasecmp(id, board->id) == 0)
152 return board;
153
154 return NULL;
155 }
156
157 int main(int argc, char **argv)
158 {
159 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
160 struct code_header *hdr;
161 FILE *in = stdin;
162 FILE *out = stdout;
163 char *ifn = NULL;
164 char *ofn = NULL;
165 char *pattern = CODE_PATTERN;
166 char *pbotpat = PBOT_PATTERN;
167 char *version = CYBERTAN_VERSION;
168 char *board_id = NULL;
169 struct board_info *board = NULL;
170 int gflag = 0;
171 int pbotflag = 0;
172 int c;
173 int v0, v1, v2;
174 size_t off, n;
175 time_t t;
176 struct tm *ptm;
177
178 fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
179
180 hdr = (struct code_header *) buf;
181 memset(hdr, 0, sizeof(struct code_header));
182
183 while ((c = getopt(argc, argv, "i:o:p:s:gbv:01245hr:B:")) != -1) {
184 switch (c) {
185 case 'i':
186 ifn = optarg;
187 break;
188 case 'o':
189 ofn = optarg;
190 break;
191 case 'p':
192 pattern = optarg;
193 break;
194 case 's':
195 hdr->sn = (unsigned char) atoi (optarg);
196 break;
197 case 'g':
198 gflag = 1;
199 break;
200 case 'b':
201 pbotflag = 1;
202 break;
203 case 'v': /* extension to allow setting version */
204 version = optarg;
205 break;
206 case '0':
207 hdr->hw_ver = 0;
208 break;
209 case '1':
210 hdr->hw_ver = 1;
211 break;
212 case '2': /* new 54G v2.2 and 54GS v1.1 flags */
213 hdr->hw_ver = 1;
214 hdr->flags[0] |= SUPPORT_4712_CHIP;
215 hdr->flags[0] |= SUPPORT_INTEL_FLASH;
216 hdr->flags[0] |= SUPPORT_5325E_SWITCH;
217 break;
218 case '4':
219 /* V4 firmware sets the flags to 0x1f */
220 hdr->hw_ver = 0;
221 hdr->flags[0] = 0x1f;
222 break;
223 case '5':
224 /* V5 is appended to trxV2 image */
225 hdr->stable[0] = 0x73; // force image to be stable
226 hdr->stable[1] = 0x00;
227 hdr->try1[0] = 0x74; // force try1 to be set
228 hdr->try1[1] = 0x00;
229 hdr->try2[0] = hdr->try2[1] = 0xFF;
230 hdr->try3[0] = hdr->try3[1] = 0xFF;
231 break;
232 case 'r':
233 hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
234 break;
235 case 'B':
236 board_id = optarg;
237 break;
238
239 case 'h':
240 default:
241 usage();
242 }
243 }
244
245 if (optind != argc || optind == 1) {
246 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
247 usage();
248 }
249
250 if (board_id) {
251 board = find_board(board_id);
252 if (board == NULL) {
253 fprintf(stderr, "unknown board \"%s\"\n", board_id);
254 usage();
255 }
256 pattern = board->pattern;
257 hdr->hw_ver = board->hw_ver;
258 hdr->sn = board->sn;
259 hdr->flags[0] = board->flags[0];
260 hdr->flags[1] = board->flags[1];
261 }
262
263 if (strlen(pattern) > 8) {
264 fprintf(stderr, "illegal pattern \"%s\"\n", pattern);
265 usage();
266 }
267
268 if (ifn && !(in = fopen(ifn, "r"))) {
269 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
270 usage();
271 }
272
273 if (ofn && !(out = fopen(ofn, "w"))) {
274 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
275 usage();
276 }
277
278 set_source_date_epoch();
279 if (source_date_epoch != -1) {
280 t = source_date_epoch;
281 } else if ((time(&t) == (time_t)(-1))) {
282 fprintf(stderr, "time call failed\n");
283 return EXIT_FAILURE;
284 }
285
286 ptm = gmtime(&t);
287
288 if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
289 fprintf(stderr, "bad version string \"%s\"\n", version);
290 return EXIT_FAILURE;
291 }
292
293 memcpy(hdr->magic, pattern, strlen(pattern));
294 if (pbotflag)
295 memcpy(&hdr->magic[4], pbotpat, 4);
296 hdr->fwdate[0] = ptm->tm_year % 100;
297 hdr->fwdate[1] = ptm->tm_mon + 1;
298 hdr->fwdate[2] = ptm->tm_mday;
299 hdr->fwvern[0] = v0;
300 hdr->fwvern[1] = v1;
301 hdr->fwvern[2] = v2;
302 memcpy(hdr->id, CODE_ID, strlen(CODE_ID));
303
304 off = sizeof(struct code_header);
305
306 fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
307 v0, v1, v2,
308 hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
309
310
311 while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
312 off = 0;
313 if (n < sizeof(buf)) {
314 if (ferror(in)) {
315 FREAD_ERROR:
316 fprintf(stderr, "fread error\n");
317 return EXIT_FAILURE;
318 }
319 if (gflag) {
320 gflag = sizeof(buf) - n;
321 memset(buf + n, 0xff, gflag);
322 fprintf(stderr, "adding %d bytes of garbage\n", gflag);
323 n = sizeof(buf);
324 }
325 }
326 if (!fwrite(buf, n, 1, out)) {
327 FWRITE_ERROR:
328 fprintf(stderr, "fwrite error\n");
329 return EXIT_FAILURE;
330 }
331 }
332
333 if (ferror(in)) {
334 goto FREAD_ERROR;
335 }
336
337 if (fflush(out)) {
338 goto FWRITE_ERROR;
339 }
340
341 fclose(in);
342 fclose(out);
343
344 return EXIT_SUCCESS;
345 }