firmware-utils: tplink-safeloader: increase RE350 kernel size
[openwrt/openwrt.git] / tools / firmware-utils / src / tplink-safeloader.c
1 /*
2 Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27 /*
28 tplink-safeloader
29
30 Image generation tool for the TP-LINK SafeLoader as seen on
31 TP-LINK Pharos devices (CPE210/220/510/520)
32 */
33
34
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 #include <arpa/inet.h>
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <limits.h>
50
51 #include "md5.h"
52
53
54 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
55
56
57 #define MAX_PARTITIONS 32
58
59 /** An image partition table entry */
60 struct image_partition_entry {
61 const char *name;
62 size_t size;
63 uint8_t *data;
64 };
65
66 /** A flash partition table entry */
67 struct flash_partition_entry {
68 const char *name;
69 uint32_t base;
70 uint32_t size;
71 };
72
73 /** Firmware layout description */
74 struct device_info {
75 const char *id;
76 const char *vendor;
77 const char *support_list;
78 char support_trail;
79 const char *soft_ver;
80 const struct flash_partition_entry partitions[MAX_PARTITIONS+1];
81 const char *first_sysupgrade_partition;
82 const char *last_sysupgrade_partition;
83 };
84
85 /** The content of the soft-version structure */
86 struct __attribute__((__packed__)) soft_version {
87 uint32_t magic;
88 uint32_t zero;
89 uint8_t pad1;
90 uint8_t version_major;
91 uint8_t version_minor;
92 uint8_t version_patch;
93 uint8_t year_hi;
94 uint8_t year_lo;
95 uint8_t month;
96 uint8_t day;
97 uint32_t rev;
98 uint8_t pad2;
99 };
100
101
102 static const uint8_t jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
103
104
105 /**
106 Salt for the MD5 hash
107
108 Fortunately, TP-LINK seems to use the same salt for most devices which use
109 the new image format.
110 */
111 static const uint8_t md5_salt[16] = {
112 0x7a, 0x2b, 0x15, 0xed,
113 0x9b, 0x98, 0x59, 0x6d,
114 0xe5, 0x04, 0xab, 0x44,
115 0xac, 0x2a, 0x9f, 0x4e,
116 };
117
118
119 /** Firmware layout table */
120 static struct device_info boards[] = {
121 /** Firmware layout for the CPE210/220 */
122 {
123 .id = "CPE210",
124 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
125 .support_list =
126 "SupportList:\r\n"
127 "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
128 "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
129 "CPE210(TP-LINK|US|N300-2):1.1\r\n"
130 "CPE210(TP-LINK|EU|N300-2):1.1\r\n"
131 "CPE220(TP-LINK|UN|N300-2):1.1\r\n"
132 "CPE220(TP-LINK|US|N300-2):1.1\r\n"
133 "CPE220(TP-LINK|EU|N300-2):1.1\r\n",
134 .support_trail = '\xff',
135 .soft_ver = NULL,
136
137 .partitions = {
138 {"fs-uboot", 0x00000, 0x20000},
139 {"partition-table", 0x20000, 0x02000},
140 {"default-mac", 0x30000, 0x00020},
141 {"product-info", 0x31100, 0x00100},
142 {"signature", 0x32000, 0x00400},
143 {"os-image", 0x40000, 0x1c0000},
144 {"file-system", 0x200000, 0x5b0000},
145 {"soft-version", 0x7b0000, 0x00100},
146 {"support-list", 0x7b1000, 0x00400},
147 {"user-config", 0x7c0000, 0x10000},
148 {"default-config", 0x7d0000, 0x10000},
149 {"log", 0x7e0000, 0x10000},
150 {"radio", 0x7f0000, 0x10000},
151 {NULL, 0, 0}
152 },
153
154 .first_sysupgrade_partition = "os-image",
155 .last_sysupgrade_partition = "support-list",
156 },
157
158 /** Firmware layout for the CPE510/520 */
159 {
160 .id = "CPE510",
161 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
162 .support_list =
163 "SupportList:\r\n"
164 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
165 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
166 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
167 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
168 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
169 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
170 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
171 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
172 .support_trail = '\xff',
173 .soft_ver = NULL,
174
175 .partitions = {
176 {"fs-uboot", 0x00000, 0x20000},
177 {"partition-table", 0x20000, 0x02000},
178 {"default-mac", 0x30000, 0x00020},
179 {"product-info", 0x31100, 0x00100},
180 {"signature", 0x32000, 0x00400},
181 {"os-image", 0x40000, 0x1c0000},
182 {"file-system", 0x200000, 0x5b0000},
183 {"soft-version", 0x7b0000, 0x00100},
184 {"support-list", 0x7b1000, 0x00400},
185 {"user-config", 0x7c0000, 0x10000},
186 {"default-config", 0x7d0000, 0x10000},
187 {"log", 0x7e0000, 0x10000},
188 {"radio", 0x7f0000, 0x10000},
189 {NULL, 0, 0}
190 },
191
192 .first_sysupgrade_partition = "os-image",
193 .last_sysupgrade_partition = "support-list",
194 },
195
196 {
197 .id = "WBS210",
198 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
199 .support_list =
200 "SupportList:\r\n"
201 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
202 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
203 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
204 .support_trail = '\xff',
205 .soft_ver = NULL,
206
207 .partitions = {
208 {"fs-uboot", 0x00000, 0x20000},
209 {"partition-table", 0x20000, 0x02000},
210 {"default-mac", 0x30000, 0x00020},
211 {"product-info", 0x31100, 0x00100},
212 {"signature", 0x32000, 0x00400},
213 {"os-image", 0x40000, 0x1c0000},
214 {"file-system", 0x200000, 0x5b0000},
215 {"soft-version", 0x7b0000, 0x00100},
216 {"support-list", 0x7b1000, 0x00400},
217 {"user-config", 0x7c0000, 0x10000},
218 {"default-config", 0x7d0000, 0x10000},
219 {"log", 0x7e0000, 0x10000},
220 {"radio", 0x7f0000, 0x10000},
221 {NULL, 0, 0}
222 },
223
224 .first_sysupgrade_partition = "os-image",
225 .last_sysupgrade_partition = "support-list",
226 },
227
228 {
229 .id = "WBS510",
230 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
231 .support_list =
232 "SupportList:\r\n"
233 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
234 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
235 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
236 .support_trail = '\xff',
237 .soft_ver = NULL,
238
239 .partitions = {
240 {"fs-uboot", 0x00000, 0x20000},
241 {"partition-table", 0x20000, 0x02000},
242 {"default-mac", 0x30000, 0x00020},
243 {"product-info", 0x31100, 0x00100},
244 {"signature", 0x32000, 0x00400},
245 {"os-image", 0x40000, 0x1c0000},
246 {"file-system", 0x200000, 0x5b0000},
247 {"soft-version", 0x7b0000, 0x00100},
248 {"support-list", 0x7b1000, 0x00400},
249 {"user-config", 0x7c0000, 0x10000},
250 {"default-config", 0x7d0000, 0x10000},
251 {"log", 0x7e0000, 0x10000},
252 {"radio", 0x7f0000, 0x10000},
253 {NULL, 0, 0}
254 },
255
256 .first_sysupgrade_partition = "os-image",
257 .last_sysupgrade_partition = "support-list",
258 },
259
260 /** Firmware layout for the C2600 */
261 {
262 .id = "C2600",
263 .vendor = "",
264 .support_list =
265 "SupportList:\r\n"
266 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
267 .support_trail = '\x00',
268 .soft_ver = NULL,
269
270 .partitions = {
271 {"SBL1", 0x00000, 0x20000},
272 {"MIBIB", 0x20000, 0x20000},
273 {"SBL2", 0x40000, 0x20000},
274 {"SBL3", 0x60000, 0x30000},
275 {"DDRCONFIG", 0x90000, 0x10000},
276 {"SSD", 0xa0000, 0x10000},
277 {"TZ", 0xb0000, 0x30000},
278 {"RPM", 0xe0000, 0x20000},
279 {"fs-uboot", 0x100000, 0x70000},
280 {"uboot-env", 0x170000, 0x40000},
281 {"radio", 0x1b0000, 0x40000},
282 {"os-image", 0x1f0000, 0x200000},
283 {"file-system", 0x3f0000, 0x1b00000},
284 {"default-mac", 0x1ef0000, 0x00200},
285 {"pin", 0x1ef0200, 0x00200},
286 {"product-info", 0x1ef0400, 0x0fc00},
287 {"partition-table", 0x1f00000, 0x10000},
288 {"soft-version", 0x1f10000, 0x10000},
289 {"support-list", 0x1f20000, 0x10000},
290 {"profile", 0x1f30000, 0x10000},
291 {"default-config", 0x1f40000, 0x10000},
292 {"user-config", 0x1f50000, 0x40000},
293 {"qos-db", 0x1f90000, 0x40000},
294 {"usb-config", 0x1fd0000, 0x10000},
295 {"log", 0x1fe0000, 0x20000},
296 {NULL, 0, 0}
297 },
298
299 .first_sysupgrade_partition = "os-image",
300 .last_sysupgrade_partition = "file-system"
301 },
302
303 /** Firmware layout for the C25v1 */
304 {
305 .id = "ARCHER-C25-V1",
306 .support_list =
307 "SupportList:\n"
308 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
309 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
310 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
311 .support_trail = '\x00',
312 .soft_ver = "soft_ver:1.0.0\n",
313
314 /**
315 We use a bigger os-image partition than the stock images (and thus
316 smaller file-system), as our kernel doesn't fit in the stock firmware's
317 1MB os-image.
318 */
319 .partitions = {
320 {"factory-boot", 0x00000, 0x20000},
321 {"fs-uboot", 0x20000, 0x10000},
322 {"os-image", 0x30000, 0x180000}, /* Stock: base 0x30000 size 0x100000 */
323 {"file-system", 0x1b0000, 0x620000}, /* Stock: base 0x130000 size 0x6a0000 */
324 {"user-config", 0x7d0000, 0x04000},
325 {"default-mac", 0x7e0000, 0x00100},
326 {"device-id", 0x7e0100, 0x00100},
327 {"extra-para", 0x7e0200, 0x00100},
328 {"pin", 0x7e0300, 0x00100},
329 {"support-list", 0x7e0400, 0x00400},
330 {"soft-version", 0x7e0800, 0x00400},
331 {"product-info", 0x7e0c00, 0x01400},
332 {"partition-table", 0x7e2000, 0x01000},
333 {"profile", 0x7e3000, 0x01000},
334 {"default-config", 0x7e4000, 0x04000},
335 {"merge-config", 0x7ec000, 0x02000},
336 {"qos-db", 0x7ee000, 0x02000},
337 {"radio", 0x7f0000, 0x10000},
338 {NULL, 0, 0}
339 },
340
341 .first_sysupgrade_partition = "os-image",
342 .last_sysupgrade_partition = "file-system",
343 },
344
345 /** Firmware layout for the C58v1 */
346 {
347 .id = "ARCHER-C58-V1",
348 .vendor = "",
349 .support_list =
350 "SupportList:\r\n"
351 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
352 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
353 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
354 .support_trail = '\x00',
355 .soft_ver = "soft_ver:1.0.0\n",
356
357 .partitions = {
358 {"fs-uboot", 0x00000, 0x10000},
359 {"default-mac", 0x10000, 0x00200},
360 {"pin", 0x10200, 0x00200},
361 {"product-info", 0x10400, 0x00100},
362 {"partition-table", 0x10500, 0x00800},
363 {"soft-version", 0x11300, 0x00200},
364 {"support-list", 0x11500, 0x00100},
365 {"device-id", 0x11600, 0x00100},
366 {"profile", 0x11700, 0x03900},
367 {"default-config", 0x15000, 0x04000},
368 {"user-config", 0x19000, 0x04000},
369 {"os-image", 0x20000, 0x180000},
370 {"file-system", 0x1a0000, 0x648000},
371 {"certyficate", 0x7e8000, 0x08000},
372 {"radio", 0x7f0000, 0x10000},
373 {NULL, 0, 0}
374 },
375
376 .first_sysupgrade_partition = "os-image",
377 .last_sysupgrade_partition = "file-system",
378 },
379
380 /** Firmware layout for the C59v1 */
381 {
382 .id = "ARCHER-C59-V1",
383 .vendor = "",
384 .support_list =
385 "SupportList:\r\n"
386 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
387 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
388 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
389 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
390 .support_trail = '\x00',
391 .soft_ver = "soft_ver:1.0.0\n",
392
393 .partitions = {
394 {"fs-uboot", 0x00000, 0x10000},
395 {"default-mac", 0x10000, 0x00200},
396 {"pin", 0x10200, 0x00200},
397 {"device-id", 0x10400, 0x00100},
398 {"product-info", 0x10500, 0x0fb00},
399 {"os-image", 0x20000, 0x180000},
400 {"file-system", 0x1a0000, 0xcb0000},
401 {"partition-table", 0xe50000, 0x10000},
402 {"soft-version", 0xe60000, 0x10000},
403 {"support-list", 0xe70000, 0x10000},
404 {"profile", 0xe80000, 0x10000},
405 {"default-config", 0xe90000, 0x10000},
406 {"user-config", 0xea0000, 0x40000},
407 {"usb-config", 0xee0000, 0x10000},
408 {"certificate", 0xef0000, 0x10000},
409 {"qos-db", 0xf00000, 0x40000},
410 {"log", 0xfe0000, 0x10000},
411 {"radio", 0xff0000, 0x10000},
412 {NULL, 0, 0}
413 },
414
415 .first_sysupgrade_partition = "os-image",
416 .last_sysupgrade_partition = "file-system",
417 },
418
419 /** Firmware layout for the C60v1 */
420 {
421 .id = "ARCHER-C60-V1",
422 .vendor = "",
423 .support_list =
424 "SupportList:\r\n"
425 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
426 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
427 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
428 .support_trail = '\x00',
429 .soft_ver = "soft_ver:1.0.0\n",
430
431 .partitions = {
432 {"fs-uboot", 0x00000, 0x10000},
433 {"default-mac", 0x10000, 0x00200},
434 {"pin", 0x10200, 0x00200},
435 {"product-info", 0x10400, 0x00100},
436 {"partition-table", 0x10500, 0x00800},
437 {"soft-version", 0x11300, 0x00200},
438 {"support-list", 0x11500, 0x00100},
439 {"device-id", 0x11600, 0x00100},
440 {"profile", 0x11700, 0x03900},
441 {"default-config", 0x15000, 0x04000},
442 {"user-config", 0x19000, 0x04000},
443 {"os-image", 0x20000, 0x180000},
444 {"file-system", 0x1a0000, 0x648000},
445 {"certyficate", 0x7e8000, 0x08000},
446 {"radio", 0x7f0000, 0x10000},
447 {NULL, 0, 0}
448 },
449
450 .first_sysupgrade_partition = "os-image",
451 .last_sysupgrade_partition = "file-system",
452 },
453
454 /** Firmware layout for the C60v2 */
455 {
456 .id = "ARCHER-C60-V2",
457 .vendor = "",
458 .support_list =
459 "SupportList:\r\n"
460 "{product_name:Archer C60,product_ver:2.0.0,special_id:42520000}\r\n"
461 "{product_name:Archer C60,product_ver:2.0.0,special_id:45550000}\r\n"
462 "{product_name:Archer C60,product_ver:2.0.0,special_id:55530000}\r\n",
463 .support_trail = '\x00',
464 .soft_ver = "soft_ver:2.0.0\n",
465
466 .partitions = {
467 {"factory-boot", 0x00000, 0x1fb00},
468 {"default-mac", 0x1fb00, 0x00200},
469 {"pin", 0x1fd00, 0x00100},
470 {"product-info", 0x1fe00, 0x00100},
471 {"device-id", 0x1ff00, 0x00100},
472 {"fs-uboot", 0x20000, 0x10000},
473 {"os-image", 0x30000, 0x180000},
474 {"file-system", 0x1b0000, 0x620000},
475 {"soft-version", 0x7d9500, 0x00100},
476 {"support-list", 0x7d9600, 0x00100},
477 {"extra-para", 0x7d9700, 0x00100},
478 {"profile", 0x7d9800, 0x03000},
479 {"default-config", 0x7dc800, 0x03000},
480 {"partition-table", 0x7df800, 0x00800},
481 {"user-config", 0x7e0000, 0x0c000},
482 {"certificate", 0x7ec000, 0x04000},
483 {"radio", 0x7f0000, 0x10000},
484 {NULL, 0, 0}
485 },
486
487 .first_sysupgrade_partition = "os-image",
488 .last_sysupgrade_partition = "file-system",
489 },
490
491 /** Firmware layout for the C5 */
492 {
493 .id = "ARCHER-C5-V2",
494 .vendor = "",
495 .support_list =
496 "SupportList:\r\n"
497 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
498 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
499 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
500 .support_trail = '\x00',
501 .soft_ver = NULL,
502
503 .partitions = {
504 {"fs-uboot", 0x00000, 0x40000},
505 {"os-image", 0x40000, 0x200000},
506 {"file-system", 0x240000, 0xc00000},
507 {"default-mac", 0xe40000, 0x00200},
508 {"pin", 0xe40200, 0x00200},
509 {"product-info", 0xe40400, 0x00200},
510 {"partition-table", 0xe50000, 0x10000},
511 {"soft-version", 0xe60000, 0x00200},
512 {"support-list", 0xe61000, 0x0f000},
513 {"profile", 0xe70000, 0x10000},
514 {"default-config", 0xe80000, 0x10000},
515 {"user-config", 0xe90000, 0x50000},
516 {"log", 0xee0000, 0x100000},
517 {"radio_bk", 0xfe0000, 0x10000},
518 {"radio", 0xff0000, 0x10000},
519 {NULL, 0, 0}
520 },
521
522 .first_sysupgrade_partition = "os-image",
523 .last_sysupgrade_partition = "file-system"
524 },
525
526 /** Firmware layout for the C7 */
527 {
528 .id = "ARCHER-C7-V4",
529 .support_list =
530 "SupportList:\n"
531 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
532 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
533 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
534 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
535 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
536 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
537 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
538 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
539 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
540 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
541 .support_trail = '\x00',
542 .soft_ver = "soft_ver:1.0.0\n",
543
544 /**
545 We use a bigger os-image partition than the stock images (and thus
546 smaller file-system), as our kernel doesn't fit in the stock firmware's
547 1MB os-image.
548 */
549 .partitions = {
550 {"factory-boot", 0x00000, 0x20000},
551 {"fs-uboot", 0x20000, 0x20000},
552 {"os-image", 0x40000, 0x180000}, /* Stock: base 0x40000 size 0x120000 */
553 {"file-system", 0x1c0000, 0xd40000}, /* Stock: base 0x160000 size 0xda0000 */
554 {"default-mac", 0xf00000, 0x00200},
555 {"pin", 0xf00200, 0x00200},
556 {"device-id", 0xf00400, 0x00100},
557 {"product-info", 0xf00500, 0x0fb00},
558 {"soft-version", 0xf10000, 0x00100},
559 {"extra-para", 0xf11000, 0x01000},
560 {"support-list", 0xf12000, 0x0a000},
561 {"profile", 0xf1c000, 0x04000},
562 {"default-config", 0xf20000, 0x10000},
563 {"user-config", 0xf30000, 0x40000},
564 {"qos-db", 0xf70000, 0x40000},
565 {"certificate", 0xfb0000, 0x10000},
566 {"partition-table", 0xfc0000, 0x10000},
567 {"log", 0xfd0000, 0x20000},
568 {"radio", 0xff0000, 0x10000},
569 {NULL, 0, 0}
570 },
571
572 .first_sysupgrade_partition = "os-image",
573 .last_sysupgrade_partition = "file-system",
574 },
575
576 /** Firmware layout for the C9 */
577 {
578 .id = "ARCHERC9",
579 .vendor = "",
580 .support_list =
581 "SupportList:\n"
582 "{product_name:ArcherC9,"
583 "product_ver:1.0.0,"
584 "special_id:00000000}\n",
585 .support_trail = '\x00',
586 .soft_ver = NULL,
587
588 .partitions = {
589 {"fs-uboot", 0x00000, 0x40000},
590 {"os-image", 0x40000, 0x200000},
591 {"file-system", 0x240000, 0xc00000},
592 {"default-mac", 0xe40000, 0x00200},
593 {"pin", 0xe40200, 0x00200},
594 {"product-info", 0xe40400, 0x00200},
595 {"partition-table", 0xe50000, 0x10000},
596 {"soft-version", 0xe60000, 0x00200},
597 {"support-list", 0xe61000, 0x0f000},
598 {"profile", 0xe70000, 0x10000},
599 {"default-config", 0xe80000, 0x10000},
600 {"user-config", 0xe90000, 0x50000},
601 {"log", 0xee0000, 0x100000},
602 {"radio_bk", 0xfe0000, 0x10000},
603 {"radio", 0xff0000, 0x10000},
604 {NULL, 0, 0}
605 },
606
607 .first_sysupgrade_partition = "os-image",
608 .last_sysupgrade_partition = "file-system"
609 },
610
611 /** Firmware layout for the EAP120 */
612 {
613 .id = "EAP120",
614 .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
615 .support_list =
616 "SupportList:\r\n"
617 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
618 .support_trail = '\xff',
619 .soft_ver = NULL,
620
621 .partitions = {
622 {"fs-uboot", 0x00000, 0x20000},
623 {"partition-table", 0x20000, 0x02000},
624 {"default-mac", 0x30000, 0x00020},
625 {"support-list", 0x31000, 0x00100},
626 {"product-info", 0x31100, 0x00100},
627 {"soft-version", 0x32000, 0x00100},
628 {"os-image", 0x40000, 0x180000},
629 {"file-system", 0x1c0000, 0x600000},
630 {"user-config", 0x7c0000, 0x10000},
631 {"backup-config", 0x7d0000, 0x10000},
632 {"log", 0x7e0000, 0x10000},
633 {"radio", 0x7f0000, 0x10000},
634 {NULL, 0, 0}
635 },
636
637 .first_sysupgrade_partition = "os-image",
638 .last_sysupgrade_partition = "file-system"
639 },
640
641 /** Firmware layout for the TL-WA850RE v2 */
642 {
643 .id = "TLWA850REV2",
644 .vendor = "",
645 .support_list =
646 "SupportList:\n"
647 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
648 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
649 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
650 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
651 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
652 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
653 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
654 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
655 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
656 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
657 .support_trail = '\x00',
658 .soft_ver = NULL,
659
660 /**
661 576KB were moved from file-system to os-image
662 in comparison to the stock image
663 */
664 .partitions = {
665 {"fs-uboot", 0x00000, 0x20000},
666 {"os-image", 0x20000, 0x150000},
667 {"file-system", 0x170000, 0x240000},
668 {"partition-table", 0x3b0000, 0x02000},
669 {"default-mac", 0x3c0000, 0x00020},
670 {"pin", 0x3c0100, 0x00020},
671 {"product-info", 0x3c1000, 0x01000},
672 {"soft-version", 0x3c2000, 0x00100},
673 {"support-list", 0x3c3000, 0x01000},
674 {"profile", 0x3c4000, 0x08000},
675 {"user-config", 0x3d0000, 0x10000},
676 {"default-config", 0x3e0000, 0x10000},
677 {"radio", 0x3f0000, 0x10000},
678 {NULL, 0, 0}
679 },
680
681 .first_sysupgrade_partition = "os-image",
682 .last_sysupgrade_partition = "file-system"
683 },
684
685 /** Firmware layout for the TL-WA855RE v1 */
686 {
687 .id = "TLWA855REV1",
688 .vendor = "",
689 .support_list =
690 "SupportList:\n"
691 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
692 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
693 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
694 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
695 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
696 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
697 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
698 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
699 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
700 .support_trail = '\x00',
701 .soft_ver = NULL,
702
703 .partitions = {
704 {"fs-uboot", 0x00000, 0x20000},
705 {"os-image", 0x20000, 0x150000},
706 {"file-system", 0x170000, 0x240000},
707 {"partition-table", 0x3b0000, 0x02000},
708 {"default-mac", 0x3c0000, 0x00020},
709 {"pin", 0x3c0100, 0x00020},
710 {"product-info", 0x3c1000, 0x01000},
711 {"soft-version", 0x3c2000, 0x00100},
712 {"support-list", 0x3c3000, 0x01000},
713 {"profile", 0x3c4000, 0x08000},
714 {"user-config", 0x3d0000, 0x10000},
715 {"default-config", 0x3e0000, 0x10000},
716 {"radio", 0x3f0000, 0x10000},
717 {NULL, 0, 0}
718 },
719
720 .first_sysupgrade_partition = "os-image",
721 .last_sysupgrade_partition = "file-system"
722 },
723
724 /** Firmware layout for the TL-WR1043 v5 */
725 {
726 .id = "TLWR1043NV5",
727 .vendor = "",
728 .support_list =
729 "SupportList:\n"
730 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
731 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
732 .support_trail = '\x00',
733 .soft_ver = "soft_ver:1.0.0\n",
734 .partitions = {
735 {"factory-boot", 0x00000, 0x20000},
736 {"fs-uboot", 0x20000, 0x20000},
737 {"os-image", 0x40000, 0x180000},
738 {"file-system", 0x1c0000, 0xd40000},
739 {"default-mac", 0xf00000, 0x00200},
740 {"pin", 0xf00200, 0x00200},
741 {"device-id", 0xf00400, 0x00100},
742 {"product-info", 0xf00500, 0x0fb00},
743 {"soft-version", 0xf10000, 0x01000},
744 {"extra-para", 0xf11000, 0x01000},
745 {"support-list", 0xf12000, 0x0a000},
746 {"profile", 0xf1c000, 0x04000},
747 {"default-config", 0xf20000, 0x10000},
748 {"user-config", 0xf30000, 0x40000},
749 {"qos-db", 0xf70000, 0x40000},
750 {"certificate", 0xfb0000, 0x10000},
751 {"partition-table", 0xfc0000, 0x10000},
752 {"log", 0xfd0000, 0x20000},
753 {"radio", 0xff0000, 0x10000},
754 {NULL, 0, 0}
755 },
756 .first_sysupgrade_partition = "os-image",
757 .last_sysupgrade_partition = "file-system"
758 },
759
760 /** Firmware layout for the TL-WR1043 v4 */
761 {
762 .id = "TLWR1043NDV4",
763 .vendor = "",
764 .support_list =
765 "SupportList:\n"
766 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
767 .support_trail = '\x00',
768 .soft_ver = NULL,
769
770 /**
771 We use a bigger os-image partition than the stock images (and thus
772 smaller file-system), as our kernel doesn't fit in the stock firmware's
773 1MB os-image.
774 */
775 .partitions = {
776 {"fs-uboot", 0x00000, 0x20000},
777 {"os-image", 0x20000, 0x180000},
778 {"file-system", 0x1a0000, 0xdb0000},
779 {"default-mac", 0xf50000, 0x00200},
780 {"pin", 0xf50200, 0x00200},
781 {"product-info", 0xf50400, 0x0fc00},
782 {"soft-version", 0xf60000, 0x0b000},
783 {"support-list", 0xf6b000, 0x04000},
784 {"profile", 0xf70000, 0x04000},
785 {"default-config", 0xf74000, 0x0b000},
786 {"user-config", 0xf80000, 0x40000},
787 {"partition-table", 0xfc0000, 0x10000},
788 {"log", 0xfd0000, 0x20000},
789 {"radio", 0xff0000, 0x10000},
790 {NULL, 0, 0}
791 },
792
793 .first_sysupgrade_partition = "os-image",
794 .last_sysupgrade_partition = "file-system"
795 },
796
797 /** Firmware layout for the TL-WR902AC v1 */
798 {
799 .id = "TL-WR902AC-V1",
800 .vendor = "",
801 .support_list =
802 "SupportList:\n"
803 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
804 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
805 .support_trail = '\x00',
806 .soft_ver = NULL,
807
808 /**
809 384KB were moved from file-system to os-image
810 in comparison to the stock image
811 */
812 .partitions = {
813 {"fs-uboot", 0x00000, 0x20000},
814 {"os-image", 0x20000, 0x180000},
815 {"file-system", 0x1a0000, 0x5b0000},
816 {"default-mac", 0x750000, 0x00200},
817 {"pin", 0x750200, 0x00200},
818 {"product-info", 0x750400, 0x0fc00},
819 {"soft-version", 0x760000, 0x0b000},
820 {"support-list", 0x76b000, 0x04000},
821 {"profile", 0x770000, 0x04000},
822 {"default-config", 0x774000, 0x0b000},
823 {"user-config", 0x780000, 0x40000},
824 {"partition-table", 0x7c0000, 0x10000},
825 {"log", 0x7d0000, 0x20000},
826 {"radio", 0x7f0000, 0x10000},
827 {NULL, 0, 0}
828 },
829
830 .first_sysupgrade_partition = "os-image",
831 .last_sysupgrade_partition = "file-system",
832 },
833
834 /** Firmware layout for the TL-WR942N V1 */
835 {
836 .id = "TLWR942NV1",
837 .vendor = "",
838 .support_list =
839 "SupportList:\r\n"
840 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
841 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
842 .support_trail = '\x00',
843 .soft_ver = NULL,
844
845 .partitions = {
846 {"fs-uboot", 0x00000, 0x20000},
847 {"os-image", 0x20000, 0x180000},
848 {"file-system", 0x1a0000, 0xca0000},
849 {"default-mac", 0xe40000, 0x00200},
850 {"pin", 0xe40200, 0x00200},
851 {"product-info", 0xe40400, 0x0fc00},
852 {"partition-table", 0xe50000, 0x10000},
853 {"soft-version", 0xe60000, 0x10000},
854 {"support-list", 0xe70000, 0x10000},
855 {"profile", 0xe80000, 0x10000},
856 {"default-config", 0xe90000, 0x10000},
857 {"user-config", 0xea0000, 0x40000},
858 {"qos-db", 0xee0000, 0x40000},
859 {"certificate", 0xf20000, 0x10000},
860 {"usb-config", 0xfb0000, 0x10000},
861 {"log", 0xfc0000, 0x20000},
862 {"radio-bk", 0xfe0000, 0x10000},
863 {"radio", 0xff0000, 0x10000},
864 {NULL, 0, 0}
865 },
866
867 .first_sysupgrade_partition = "os-image",
868 .last_sysupgrade_partition = "file-system",
869 },
870
871 /** Firmware layout for the RE350 v1 */
872 {
873 .id = "RE350-V1",
874 .vendor = "",
875 .support_list =
876 "SupportList:\n"
877 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
878 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
879 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
880 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
881 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
882 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
883 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
884 .support_trail = '\x00',
885 .soft_ver = NULL,
886
887 /**
888 The original os-image partition is too small,
889 so we enlarge it to 1.75M
890 */
891 .partitions = {
892 {"fs-uboot", 0x00000, 0x20000},
893 {"os-image", 0x20000, 0x1c0000},
894 {"file-system", 0x1e0000, 0x420000},
895 {"partition-table", 0x600000, 0x02000},
896 {"default-mac", 0x610000, 0x00020},
897 {"pin", 0x610100, 0x00020},
898 {"product-info", 0x611100, 0x01000},
899 {"soft-version", 0x620000, 0x01000},
900 {"support-list", 0x621000, 0x01000},
901 {"profile", 0x622000, 0x08000},
902 {"user-config", 0x630000, 0x10000},
903 {"default-config", 0x640000, 0x10000},
904 {"radio", 0x7f0000, 0x10000},
905 {NULL, 0, 0}
906 },
907
908 .first_sysupgrade_partition = "os-image",
909 .last_sysupgrade_partition = "file-system"
910 },
911
912 /** Firmware layout for the RE355 */
913 {
914 .id = "RE355",
915 .vendor = "",
916 .support_list =
917 "SupportList:\r\n"
918 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
919 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
920 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
921 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
922 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
923 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
924 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
925 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
926 .support_trail = '\x00',
927 .soft_ver = NULL,
928
929 /**
930 The flash partition table for RE355;
931 it is almost the same as the one used by the stock images,
932 576KB were moved from file-system to os-image.
933 */
934 .partitions = {
935 {"fs-uboot", 0x00000, 0x20000},
936 {"os-image", 0x20000, 0x180000},
937 {"file-system", 0x1a0000, 0x460000},
938 {"partition-table", 0x600000, 0x02000},
939 {"default-mac", 0x610000, 0x00020},
940 {"pin", 0x610100, 0x00020},
941 {"product-info", 0x611100, 0x01000},
942 {"soft-version", 0x620000, 0x01000},
943 {"support-list", 0x621000, 0x01000},
944 {"profile", 0x622000, 0x08000},
945 {"user-config", 0x630000, 0x10000},
946 {"default-config", 0x640000, 0x10000},
947 {"radio", 0x7f0000, 0x10000},
948 {NULL, 0, 0}
949 },
950
951 .first_sysupgrade_partition = "os-image",
952 .last_sysupgrade_partition = "file-system"
953 },
954
955 /** Firmware layout for the RE450 */
956 {
957 .id = "RE450",
958 .vendor = "",
959 .support_list =
960 "SupportList:\r\n"
961 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
962 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
963 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
964 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
965 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
966 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
967 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
968 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
969 .support_trail = '\x00',
970 .soft_ver = NULL,
971
972 /**
973 The flash partition table for RE450;
974 it is almost the same as the one used by the stock images,
975 576KB were moved from file-system to os-image.
976 */
977 .partitions = {
978 {"fs-uboot", 0x00000, 0x20000},
979 {"os-image", 0x20000, 0x180000},
980 {"file-system", 0x1a0000, 0x460000},
981 {"partition-table", 0x600000, 0x02000},
982 {"default-mac", 0x610000, 0x00020},
983 {"pin", 0x610100, 0x00020},
984 {"product-info", 0x611100, 0x01000},
985 {"soft-version", 0x620000, 0x01000},
986 {"support-list", 0x621000, 0x01000},
987 {"profile", 0x622000, 0x08000},
988 {"user-config", 0x630000, 0x10000},
989 {"default-config", 0x640000, 0x10000},
990 {"radio", 0x7f0000, 0x10000},
991 {NULL, 0, 0}
992 },
993
994 .first_sysupgrade_partition = "os-image",
995 .last_sysupgrade_partition = "file-system"
996 },
997
998 {}
999 };
1000
1001 #define error(_ret, _errno, _str, ...) \
1002 do { \
1003 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1004 strerror(_errno)); \
1005 if (_ret) \
1006 exit(_ret); \
1007 } while (0)
1008
1009
1010 /** Stores a uint32 as big endian */
1011 static inline void put32(uint8_t *buf, uint32_t val) {
1012 buf[0] = val >> 24;
1013 buf[1] = val >> 16;
1014 buf[2] = val >> 8;
1015 buf[3] = val;
1016 }
1017
1018 /** Allocates a new image partition */
1019 static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
1020 struct image_partition_entry entry = {name, len, malloc(len)};
1021 if (!entry.data)
1022 error(1, errno, "malloc");
1023
1024 return entry;
1025 }
1026
1027 /** Frees an image partition */
1028 static void free_image_partition(struct image_partition_entry entry) {
1029 free(entry.data);
1030 }
1031
1032 static time_t source_date_epoch = -1;
1033 static void set_source_date_epoch() {
1034 char *env = getenv("SOURCE_DATE_EPOCH");
1035 char *endptr = env;
1036 errno = 0;
1037 if (env && *env) {
1038 source_date_epoch = strtoull(env, &endptr, 10);
1039 if (errno || (endptr && *endptr != '\0')) {
1040 fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
1041 exit(1);
1042 }
1043 }
1044 }
1045
1046 /** Generates the partition-table partition */
1047 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
1048 struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
1049
1050 char *s = (char *)entry.data, *end = (char *)(s+entry.size);
1051
1052 *(s++) = 0x00;
1053 *(s++) = 0x04;
1054 *(s++) = 0x00;
1055 *(s++) = 0x00;
1056
1057 size_t i;
1058 for (i = 0; p[i].name; i++) {
1059 size_t len = end-s;
1060 size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
1061
1062 if (w > len-1)
1063 error(1, 0, "flash partition table overflow?");
1064
1065 s += w;
1066 }
1067
1068 s++;
1069
1070 memset(s, 0xff, end-s);
1071
1072 return entry;
1073 }
1074
1075
1076 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1077 static inline uint8_t bcd(uint8_t v) {
1078 return 0x10 * (v/10) + v%10;
1079 }
1080
1081
1082 /** Generates the soft-version partition */
1083 static struct image_partition_entry make_soft_version(uint32_t rev) {
1084 struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
1085 struct soft_version *s = (struct soft_version *)entry.data;
1086
1087 time_t t;
1088
1089 if (source_date_epoch != -1)
1090 t = source_date_epoch;
1091 else if (time(&t) == (time_t)(-1))
1092 error(1, errno, "time");
1093
1094 struct tm *tm = localtime(&t);
1095
1096 s->magic = htonl(0x0000000c);
1097 s->zero = 0;
1098 s->pad1 = 0xff;
1099
1100 s->version_major = 0;
1101 s->version_minor = 0;
1102 s->version_patch = 0;
1103
1104 s->year_hi = bcd((1900+tm->tm_year)/100);
1105 s->year_lo = bcd(tm->tm_year%100);
1106 s->month = bcd(tm->tm_mon+1);
1107 s->day = bcd(tm->tm_mday);
1108 s->rev = htonl(rev);
1109
1110 s->pad2 = 0xff;
1111
1112 return entry;
1113 }
1114
1115 static struct image_partition_entry make_soft_version_from_string(const char *soft_ver) {
1116 /** String length _including_ the terminating zero byte */
1117 uint32_t ver_len = strlen(soft_ver) + 1;
1118 /** Partition contains 64 bit header, the version string, and one additional null byte */
1119 size_t partition_len = 2*sizeof(uint32_t) + ver_len + 1;
1120 struct image_partition_entry entry = alloc_image_partition("soft-version", partition_len);
1121
1122 uint32_t *len = (uint32_t *)entry.data;
1123 len[0] = htonl(ver_len);
1124 len[1] = 0;
1125 memcpy(&len[2], soft_ver, ver_len);
1126
1127 entry.data[partition_len - 1] = 0;
1128
1129 return entry;
1130 }
1131
1132 /** Generates the support-list partition */
1133 static struct image_partition_entry make_support_list(const struct device_info *info) {
1134 size_t len = strlen(info->support_list);
1135 struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
1136
1137 put32(entry.data, len);
1138 memset(entry.data+4, 0, 4);
1139 memcpy(entry.data+8, info->support_list, len);
1140 entry.data[len+8] = info->support_trail;
1141
1142 return entry;
1143 }
1144
1145 /** Creates a new image partition with an arbitrary name from a file */
1146 static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof) {
1147 struct stat statbuf;
1148
1149 if (stat(filename, &statbuf) < 0)
1150 error(1, errno, "unable to stat file `%s'", filename);
1151
1152 size_t len = statbuf.st_size;
1153
1154 if (add_jffs2_eof)
1155 len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
1156
1157 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1158
1159 FILE *file = fopen(filename, "rb");
1160 if (!file)
1161 error(1, errno, "unable to open file `%s'", filename);
1162
1163 if (fread(entry.data, statbuf.st_size, 1, file) != 1)
1164 error(1, errno, "unable to read file `%s'", filename);
1165
1166 if (add_jffs2_eof) {
1167 uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
1168
1169 memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
1170 memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
1171 }
1172
1173 fclose(file);
1174
1175 return entry;
1176 }
1177
1178 /** Creates a new image partition from arbitrary data */
1179 static struct image_partition_entry put_data(const char *part_name, const char *datain, size_t len) {
1180
1181 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1182
1183 memcpy(entry.data, datain, len);
1184
1185 return entry;
1186 }
1187
1188 /**
1189 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1190
1191 Example image partition table:
1192
1193 fwup-ptn partition-table base 0x00800 size 0x00800
1194 fwup-ptn os-image base 0x01000 size 0x113b45
1195 fwup-ptn file-system base 0x114b45 size 0x1d0004
1196 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1197
1198 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1199 the end of the partition table is marked with a zero byte.
1200
1201 The firmware image must contain at least the partition-table and support-list partitions
1202 to be accepted. There aren't any alignment constraints for the image partitions.
1203
1204 The partition-table partition contains the actual flash layout; partitions
1205 from the image partition table are mapped to the corresponding flash partitions during
1206 the firmware upgrade. The support-list partition contains a list of devices supported by
1207 the firmware image.
1208
1209 The base offsets in the firmware partition table are relative to the end
1210 of the vendor information block, so the partition-table partition will
1211 actually start at offset 0x1814 of the image.
1212
1213 I think partition-table must be the first partition in the firmware image.
1214 */
1215 static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
1216 size_t i, j;
1217 char *image_pt = (char *)buffer, *end = image_pt + 0x800;
1218
1219 size_t base = 0x800;
1220 for (i = 0; parts[i].name; i++) {
1221 for (j = 0; flash_parts[j].name; j++) {
1222 if (!strcmp(flash_parts[j].name, parts[i].name)) {
1223 if (parts[i].size > flash_parts[j].size)
1224 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
1225 break;
1226 }
1227 }
1228
1229 assert(flash_parts[j].name);
1230
1231 memcpy(buffer + base, parts[i].data, parts[i].size);
1232
1233 size_t len = end-image_pt;
1234 size_t w = snprintf(image_pt, len, "fwup-ptn %s base 0x%05x size 0x%05x\t\r\n", parts[i].name, (unsigned)base, (unsigned)parts[i].size);
1235
1236 if (w > len-1)
1237 error(1, 0, "image partition table overflow?");
1238
1239 image_pt += w;
1240
1241 base += parts[i].size;
1242 }
1243 }
1244
1245 /** Generates and writes the image MD5 checksum */
1246 static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
1247 MD5_CTX ctx;
1248
1249 MD5_Init(&ctx);
1250 MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
1251 MD5_Update(&ctx, buffer, len);
1252 MD5_Final(md5, &ctx);
1253 }
1254
1255
1256 /**
1257 Generates the firmware image in factory format
1258
1259 Image format:
1260
1261 Bytes (hex) Usage
1262 ----------- -----
1263 0000-0003 Image size (4 bytes, big endian)
1264 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1265 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1266 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1267 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1268 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1269 1814-xxxx Firmware partitions
1270 */
1271 static void * generate_factory_image(const struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
1272 *len = 0x1814;
1273
1274 size_t i;
1275 for (i = 0; parts[i].name; i++)
1276 *len += parts[i].size;
1277
1278 uint8_t *image = malloc(*len);
1279 if (!image)
1280 error(1, errno, "malloc");
1281
1282 memset(image, 0xff, *len);
1283 put32(image, *len);
1284
1285 if (info->vendor) {
1286 size_t vendor_len = strlen(info->vendor);
1287 put32(image+0x14, vendor_len);
1288 memcpy(image+0x18, info->vendor, vendor_len);
1289 }
1290
1291 put_partitions(image + 0x1014, info->partitions, parts);
1292 put_md5(image+0x04, image+0x14, *len-0x14);
1293
1294 return image;
1295 }
1296
1297 /**
1298 Generates the firmware image in sysupgrade format
1299
1300 This makes some assumptions about the provided flash and image partition tables and
1301 should be generalized when TP-LINK starts building its safeloader into hardware with
1302 different flash layouts.
1303 */
1304 static void * generate_sysupgrade_image(const struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
1305 size_t i, j;
1306 size_t flash_first_partition_index = 0;
1307 size_t flash_last_partition_index = 0;
1308 const struct flash_partition_entry *flash_first_partition = NULL;
1309 const struct flash_partition_entry *flash_last_partition = NULL;
1310 const struct image_partition_entry *image_last_partition = NULL;
1311
1312 /** Find first and last partitions */
1313 for (i = 0; info->partitions[i].name; i++) {
1314 if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
1315 flash_first_partition = &info->partitions[i];
1316 flash_first_partition_index = i;
1317 } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
1318 flash_last_partition = &info->partitions[i];
1319 flash_last_partition_index = i;
1320 }
1321 }
1322
1323 assert(flash_first_partition && flash_last_partition);
1324 assert(flash_first_partition_index < flash_last_partition_index);
1325
1326 /** Find last partition from image to calculate needed size */
1327 for (i = 0; image_parts[i].name; i++) {
1328 if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
1329 image_last_partition = &image_parts[i];
1330 break;
1331 }
1332 }
1333
1334 assert(image_last_partition);
1335
1336 *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
1337
1338 uint8_t *image = malloc(*len);
1339 if (!image)
1340 error(1, errno, "malloc");
1341
1342 memset(image, 0xff, *len);
1343
1344 for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
1345 for (j = 0; image_parts[j].name; j++) {
1346 if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
1347 if (image_parts[j].size > info->partitions[i].size)
1348 error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
1349 memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
1350 break;
1351 }
1352
1353 assert(image_parts[j].name);
1354 }
1355 }
1356
1357 return image;
1358 }
1359
1360 /** Generates an image according to a given layout and writes it to a file */
1361 static void build_image(const char *output,
1362 const char *kernel_image,
1363 const char *rootfs_image,
1364 uint32_t rev,
1365 bool add_jffs2_eof,
1366 bool sysupgrade,
1367 const struct device_info *info) {
1368
1369 struct image_partition_entry parts[7] = {};
1370
1371 parts[0] = make_partition_table(info->partitions);
1372 if (info->soft_ver)
1373 parts[1] = make_soft_version_from_string(info->soft_ver);
1374 else
1375 parts[1] = make_soft_version(rev);
1376
1377 parts[2] = make_support_list(info);
1378 parts[3] = read_file("os-image", kernel_image, false);
1379 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
1380
1381 /* Some devices need the extra-para partition to accept the firmware */
1382 if (strcasecmp(info->id, "ARCHER-C25-V1") == 0 ||
1383 strcasecmp(info->id, "ARCHER-C60-V2") == 0 ||
1384 strcasecmp(info->id, "TLWR1043NV5") == 0) {
1385 const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1386 parts[5] = put_data("extra-para", mdat, 11);
1387 } else if (strcasecmp(info->id, "ARCHER-C7-V4") == 0) {
1388 const char mdat[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1389 parts[5] = put_data("extra-para", mdat, 11);
1390 }
1391
1392 size_t len;
1393 void *image;
1394 if (sysupgrade)
1395 image = generate_sysupgrade_image(info, parts, &len);
1396 else
1397 image = generate_factory_image(info, parts, &len);
1398
1399 FILE *file = fopen(output, "wb");
1400 if (!file)
1401 error(1, errno, "unable to open output file");
1402
1403 if (fwrite(image, len, 1, file) != 1)
1404 error(1, 0, "unable to write output file");
1405
1406 fclose(file);
1407
1408 free(image);
1409
1410 size_t i;
1411 for (i = 0; parts[i].name; i++)
1412 free_image_partition(parts[i]);
1413 }
1414
1415 /** Usage output */
1416 static void usage(const char *argv0) {
1417 fprintf(stderr,
1418 "Usage: %s [OPTIONS...]\n"
1419 "\n"
1420 "Options:\n"
1421 " -h show this help\n"
1422 "\n"
1423 "Create a new image:\n"
1424 " -B <board> create image for the board specified with <board>\n"
1425 " -k <file> read kernel image from the file <file>\n"
1426 " -r <file> read rootfs image from the file <file>\n"
1427 " -o <file> write output to the file <file>\n"
1428 " -V <rev> sets the revision number to <rev>\n"
1429 " -j add jffs2 end-of-filesystem markers\n"
1430 " -S create sysupgrade instead of factory image\n"
1431 "Extract an old image:\n"
1432 " -x <file> extract all oem firmware partition\n"
1433 " -d <dir> destination to extract the firmware partition\n"
1434 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1435 argv0
1436 );
1437 };
1438
1439
1440 static const struct device_info *find_board(const char *id)
1441 {
1442 struct device_info *board = NULL;
1443
1444 for (board = boards; board->id != NULL; board++)
1445 if (strcasecmp(id, board->id) == 0)
1446 return board;
1447
1448 return NULL;
1449 }
1450
1451 static int add_flash_partition(
1452 struct flash_partition_entry *part_list,
1453 size_t max_entries,
1454 const char *name,
1455 unsigned long base,
1456 unsigned long size)
1457 {
1458 int ptr;
1459 /* check if the list has a free entry */
1460 for (ptr = 0; ptr < max_entries; ptr++, part_list++) {
1461 if (part_list->name == NULL &&
1462 part_list->base == 0 &&
1463 part_list->size == 0)
1464 break;
1465 }
1466
1467 if (ptr == max_entries) {
1468 error(1, 0, "No free flash part entry available.");
1469 }
1470
1471 part_list->name = calloc(1, strlen(name) + 1);
1472 memcpy((char *)part_list->name, name, strlen(name));
1473 part_list->base = base;
1474 part_list->size = size;
1475
1476 return 0;
1477 }
1478
1479 /** read the partition table into struct flash_partition_entry */
1480 static int read_partition_table(
1481 FILE *file, long offset,
1482 struct flash_partition_entry *entries, size_t max_entries,
1483 int type)
1484 {
1485 char buf[2048];
1486 char *ptr, *end;
1487 const char *parthdr = NULL;
1488 const char *fwuphdr = "fwup-ptn";
1489 const char *flashhdr = "partition";
1490
1491 /* TODO: search for the partition table */
1492
1493 switch(type) {
1494 case 0:
1495 parthdr = fwuphdr;
1496 break;
1497 case 1:
1498 parthdr = flashhdr;
1499 break;
1500 default:
1501 error(1, 0, "Invalid partition table");
1502 }
1503
1504 if (fseek(file, offset, SEEK_SET) < 0)
1505 error(1, errno, "Can not seek in the firmware");
1506
1507 if (fread(buf, 1, 2048, file) < 0)
1508 error(1, errno, "Can not read fwup-ptn from the firmware");
1509
1510 buf[2047] = '\0';
1511
1512 /* look for the partition header */
1513 if (memcmp(buf, parthdr, strlen(parthdr)) != 0) {
1514 fprintf(stderr, "DEBUG: can not find fwuphdr\n");
1515 return 1;
1516 }
1517
1518 ptr = buf;
1519 end = buf + sizeof(buf);
1520 while ((ptr + strlen(parthdr)) < end &&
1521 memcmp(ptr, parthdr, strlen(parthdr)) == 0) {
1522 char *end_part;
1523 char *end_element;
1524
1525 char name[32] = { 0 };
1526 int name_len = 0;
1527 unsigned long base = 0;
1528 unsigned long size = 0;
1529
1530 end_part = memchr(ptr, '\n', (end - ptr));
1531 if (end_part == NULL) {
1532 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
1533 break;
1534 }
1535
1536 for (int i = 0; i <= 4; i++) {
1537 if (end_part <= ptr)
1538 break;
1539
1540 end_element = memchr(ptr, 0x20, (end_part - ptr));
1541 if (end_element == NULL) {
1542 error(1, errno, "Ignoring the rest of the partition entries.");
1543 break;
1544 }
1545
1546 switch (i) {
1547 /* partition header */
1548 case 0:
1549 ptr = end_element + 1;
1550 continue;
1551 /* name */
1552 case 1:
1553 name_len = (end_element - ptr) > 31 ? 31 : (end_element - ptr);
1554 strncpy(name, ptr, name_len);
1555 name[name_len] = '\0';
1556 ptr = end_element + 1;
1557 continue;
1558
1559 /* string "base" */
1560 case 2:
1561 ptr = end_element + 1;
1562 continue;
1563
1564 /* actual base */
1565 case 3:
1566 base = strtoul(ptr, NULL, 16);
1567 ptr = end_element + 1;
1568 continue;
1569
1570 /* string "size" */
1571 case 4:
1572 ptr = end_element + 1;
1573 /* actual size. The last element doesn't have a sepeartor */
1574 size = strtoul(ptr, NULL, 16);
1575 /* the part ends with 0x09, 0x0d, 0x0a */
1576 ptr = end_part + 1;
1577 add_flash_partition(entries, max_entries, name, base, size);
1578 continue;
1579 }
1580 }
1581 }
1582
1583 return 0;
1584 }
1585
1586 static void write_partition(
1587 FILE *input_file,
1588 size_t firmware_offset,
1589 struct flash_partition_entry *entry,
1590 FILE *output_file)
1591 {
1592 char buf[4096];
1593 size_t offset;
1594
1595 fseek(input_file, entry->base + firmware_offset, SEEK_SET);
1596
1597 for (offset = 0; sizeof(buf) + offset <= entry->size; offset += sizeof(buf)) {
1598 if (fread(buf, sizeof(buf), 1, input_file) < 0)
1599 error(1, errno, "Can not read partition from input_file");
1600
1601 if (fwrite(buf, sizeof(buf), 1, output_file) < 0)
1602 error(1, errno, "Can not write partition to output_file");
1603 }
1604 /* write last chunk smaller than buffer */
1605 if (offset < entry->size) {
1606 offset = entry->size - offset;
1607 if (fread(buf, offset, 1, input_file) < 0)
1608 error(1, errno, "Can not read partition from input_file");
1609 if (fwrite(buf, offset, 1, output_file) < 0)
1610 error(1, errno, "Can not write partition to output_file");
1611 }
1612 }
1613
1614 static int extract_firmware_partition(FILE *input_file, size_t firmware_offset, struct flash_partition_entry *entry, const char *output_directory)
1615 {
1616 FILE *output_file;
1617 char output[PATH_MAX];
1618
1619 snprintf(output, PATH_MAX, "%s/%s", output_directory, entry->name);
1620 output_file = fopen(output, "wb+");
1621 if (output_file == NULL) {
1622 error(1, errno, "Can not open output file %s", output);
1623 }
1624
1625 write_partition(input_file, firmware_offset, entry, output_file);
1626
1627 fclose(output_file);
1628
1629 return 0;
1630 }
1631
1632 /** extract all partitions from the firmware file */
1633 static int extract_firmware(const char *input, const char *output_directory)
1634 {
1635 struct flash_partition_entry entries[16] = { 0 };
1636 size_t max_entries = 16;
1637 size_t firmware_offset = 0x1014;
1638 FILE *input_file;
1639
1640 struct stat statbuf;
1641
1642 /* check input file */
1643 if (stat(input, &statbuf)) {
1644 error(1, errno, "Can not read input firmware %s", input);
1645 }
1646
1647 /* check if output directory exists */
1648 if (stat(output_directory, &statbuf)) {
1649 error(1, errno, "Failed to stat output directory %s", output_directory);
1650 }
1651
1652 if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
1653 error(1, errno, "Given output directory is not a directory %s", output_directory);
1654 }
1655
1656 input_file = fopen(input, "rb");
1657
1658 if (read_partition_table(input_file, firmware_offset, entries, 16, 0) != 0) {
1659 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1660 }
1661
1662 for (int i = 0; i < max_entries; i++) {
1663 if (entries[i].name == NULL &&
1664 entries[i].base == 0 &&
1665 entries[i].size == 0)
1666 continue;
1667
1668 extract_firmware_partition(input_file, firmware_offset, &entries[i], output_directory);
1669 }
1670
1671 return 0;
1672 }
1673
1674 static struct flash_partition_entry *find_partition(
1675 struct flash_partition_entry *entries, size_t max_entries,
1676 const char *name, const char *error_msg)
1677 {
1678 for (int i = 0; i < max_entries; i++, entries++) {
1679 if (strcmp(entries->name, name) == 0)
1680 return entries;
1681 }
1682
1683 error(1, 0, "%s", error_msg);
1684 return NULL;
1685 }
1686
1687 static void write_ff(FILE *output_file, size_t size)
1688 {
1689 char buf[4096];
1690 int offset;
1691
1692 memset(buf, 0xff, sizeof(buf));
1693
1694 for (offset = 0; offset + sizeof(buf) < size ; offset += sizeof(buf)) {
1695 if (fwrite(buf, sizeof(buf), 1, output_file) < 0)
1696 error(1, errno, "Can not write 0xff to output_file");
1697 }
1698
1699 /* write last chunk smaller than buffer */
1700 if (offset < size) {
1701 offset = size - offset;
1702 if (fwrite(buf, offset, 1, output_file) < 0)
1703 error(1, errno, "Can not write partition to output_file");
1704 }
1705 }
1706
1707 static void convert_firmware(const char *input, const char *output)
1708 {
1709 struct flash_partition_entry fwup[MAX_PARTITIONS] = { 0 };
1710 struct flash_partition_entry flash[MAX_PARTITIONS] = { 0 };
1711 struct flash_partition_entry *fwup_os_image = NULL, *fwup_file_system = NULL;
1712 struct flash_partition_entry *flash_os_image = NULL, *flash_file_system = NULL;
1713 struct flash_partition_entry *fwup_partition_table = NULL;
1714 size_t firmware_offset = 0x1014;
1715 FILE *input_file, *output_file;
1716
1717 struct stat statbuf;
1718
1719 /* check input file */
1720 if (stat(input, &statbuf)) {
1721 error(1, errno, "Can not read input firmware %s", input);
1722 }
1723
1724 input_file = fopen(input, "rb");
1725 if (!input_file)
1726 error(1, 0, "Can not open input firmware %s", input);
1727
1728 output_file = fopen(output, "wb");
1729 if (!output_file)
1730 error(1, 0, "Can not open output firmware %s", output);
1731
1732 if (read_partition_table(input_file, firmware_offset, fwup, MAX_PARTITIONS, 0) != 0) {
1733 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1734 }
1735
1736 fwup_os_image = find_partition(fwup, MAX_PARTITIONS,
1737 "os-image", "Error can not find os-image partition (fwup)");
1738 fwup_file_system = find_partition(fwup, MAX_PARTITIONS,
1739 "file-system", "Error can not find file-system partition (fwup)");
1740 fwup_partition_table = find_partition(fwup, MAX_PARTITIONS,
1741 "partition-table", "Error can not find partition-table partition");
1742
1743 /* the flash partition table has a 0x00000004 magic haeder */
1744 if (read_partition_table(input_file, firmware_offset + fwup_partition_table->base + 4, flash, MAX_PARTITIONS, 1) != 0)
1745 error(1, 0, "Error can not read the partition table (flash)");
1746
1747 flash_os_image = find_partition(flash, MAX_PARTITIONS,
1748 "os-image", "Error can not find os-image partition (flash)");
1749 flash_file_system = find_partition(flash, MAX_PARTITIONS,
1750 "file-system", "Error can not find file-system partition (flash)");
1751
1752 /* write os_image to 0x0 */
1753 write_partition(input_file, firmware_offset, fwup_os_image, output_file);
1754 write_ff(output_file, flash_os_image->size - fwup_os_image->size);
1755
1756 /* write file-system behind os_image */
1757 fseek(output_file, flash_file_system->base - flash_os_image->base, SEEK_SET);
1758 write_partition(input_file, firmware_offset, fwup_file_system, output_file);
1759 write_ff(output_file, flash_file_system->size - fwup_file_system->size);
1760
1761 fclose(output_file);
1762 fclose(input_file);
1763 }
1764
1765 int main(int argc, char *argv[]) {
1766 const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
1767 const char *extract_image = NULL, *output_directory = NULL, *convert_image = NULL;
1768 bool add_jffs2_eof = false, sysupgrade = false;
1769 unsigned rev = 0;
1770 const struct device_info *info;
1771 set_source_date_epoch();
1772
1773 while (true) {
1774 int c;
1775
1776 c = getopt(argc, argv, "B:k:r:o:V:jSh:x:d:z:");
1777 if (c == -1)
1778 break;
1779
1780 switch (c) {
1781 case 'B':
1782 board = optarg;
1783 break;
1784
1785 case 'k':
1786 kernel_image = optarg;
1787 break;
1788
1789 case 'r':
1790 rootfs_image = optarg;
1791 break;
1792
1793 case 'o':
1794 output = optarg;
1795 break;
1796
1797 case 'V':
1798 sscanf(optarg, "r%u", &rev);
1799 break;
1800
1801 case 'j':
1802 add_jffs2_eof = true;
1803 break;
1804
1805 case 'S':
1806 sysupgrade = true;
1807 break;
1808
1809 case 'h':
1810 usage(argv[0]);
1811 return 0;
1812
1813 case 'd':
1814 output_directory = optarg;
1815 break;
1816
1817 case 'x':
1818 extract_image = optarg;
1819 break;
1820
1821 case 'z':
1822 convert_image = optarg;
1823 break;
1824
1825 default:
1826 usage(argv[0]);
1827 return 1;
1828 }
1829 }
1830
1831 if (extract_image || output_directory) {
1832 if (!extract_image)
1833 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
1834 if (!output_directory)
1835 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
1836 extract_firmware(extract_image, output_directory);
1837 } else if (convert_image) {
1838 if (!output)
1839 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
1840 convert_firmware(convert_image, output);
1841 } else {
1842 if (!board)
1843 error(1, 0, "no board has been specified");
1844 if (!kernel_image)
1845 error(1, 0, "no kernel image has been specified");
1846 if (!rootfs_image)
1847 error(1, 0, "no rootfs image has been specified");
1848 if (!output)
1849 error(1, 0, "no output filename has been specified");
1850
1851 info = find_board(board);
1852
1853 if (info == NULL)
1854 error(1, 0, "unsupported board %s", board);
1855
1856 build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);
1857 }
1858
1859 return 0;
1860 }