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