2 Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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.
30 Image generation tool for the TP-LINK SafeLoader as seen on
31 TP-LINK Pharos devices (CPE210/220/510/520)
45 #include <arpa/inet.h>
47 #include <sys/types.h>
54 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
57 #define MAX_PARTITIONS 32
59 /** An image partition table entry */
60 struct image_partition_entry
{
66 /** A flash partition table entry */
67 struct flash_partition_entry
{
73 /** Firmware layout description */
77 const char *support_list
;
80 struct flash_partition_entry partitions
[MAX_PARTITIONS
+1];
81 const char *first_sysupgrade_partition
;
82 const char *last_sysupgrade_partition
;
85 /** The content of the soft-version structure */
86 struct __attribute__((__packed__
)) soft_version
{
90 uint8_t version_major
;
91 uint8_t version_minor
;
92 uint8_t version_patch
;
102 static const uint8_t jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
106 Salt for the MD5 hash
108 Fortunately, TP-LINK seems to use the same salt for most devices which use
109 the new image format.
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,
119 /** Firmware layout table */
120 static struct device_info boards
[] = {
121 /** Firmware layout for the CPE210/220 */
124 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\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',
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},
154 .first_sysupgrade_partition
= "os-image",
155 .last_sysupgrade_partition
= "support-list",
158 /** Firmware layout for the CPE210 V2 */
161 .vendor
= "CPE210(TP-LINK|UN|N300-2|00000000):2.0\r\n",
164 "CPE210(TP-LINK|EU|N300-2|00000000):2.0\r\n"
165 "CPE210(TP-LINK|EU|N300-2|45550000):2.0\r\n"
166 "CPE210(TP-LINK|EU|N300-2|55530000):2.0\r\n"
167 "CPE210(TP-LINK|UN|N300-2|00000000):2.0\r\n"
168 "CPE210(TP-LINK|UN|N300-2|45550000):2.0\r\n"
169 "CPE210(TP-LINK|UN|N300-2|55530000):2.0\r\n"
170 "CPE210(TP-LINK|US|N300-2|55530000):2.0\r\n"
171 "CPE210(TP-LINK|UN|N300-2):2.0\r\n"
172 "CPE210(TP-LINK|EU|N300-2):2.0\r\n"
173 "CPE210(TP-LINK|US|N300-2):2.0\r\n",
174 .support_trail
= '\xff',
178 {"fs-uboot", 0x00000, 0x20000},
179 {"partition-table", 0x20000, 0x02000},
180 {"default-mac", 0x30000, 0x00020},
181 {"product-info", 0x31100, 0x00100},
182 {"device-info", 0x31400, 0x00400},
183 {"signature", 0x32000, 0x00400},
184 {"device-id", 0x33000, 0x00100},
185 {"firmware", 0x40000, 0x770000},
186 {"soft-version", 0x7b0000, 0x00100},
187 {"support-list", 0x7b1000, 0x01000},
188 {"user-config", 0x7c0000, 0x10000},
189 {"default-config", 0x7d0000, 0x10000},
190 {"log", 0x7e0000, 0x10000},
191 {"radio", 0x7f0000, 0x10000},
195 .first_sysupgrade_partition
= "os-image",
196 .last_sysupgrade_partition
= "support-list",
199 /** Firmware layout for the CPE210 V3 */
202 .vendor
= "CPE210(TP-LINK|UN|N300-2|00000000):3.0\r\n",
205 "CPE210(TP-LINK|EU|N300-2|45550000):3.0\r\n"
206 "CPE210(TP-LINK|UN|N300-2|00000000):3.0\r\n"
207 "CPE210(TP-LINK|UN|N300-2):3.0\r\n"
208 "CPE210(TP-LINK|EU|N300-2):3.0\r\n",
209 .support_trail
= '\xff',
213 {"fs-uboot", 0x00000, 0x20000},
214 {"partition-table", 0x20000, 0x01000},
215 {"default-mac", 0x30000, 0x00020},
216 {"product-info", 0x31100, 0x00100},
217 {"device-info", 0x31400, 0x00400},
218 {"signature", 0x32000, 0x00400},
219 {"device-id", 0x33000, 0x00100},
220 {"firmware", 0x40000, 0x770000},
221 {"soft-version", 0x7b0000, 0x00100},
222 {"support-list", 0x7b1000, 0x01000},
223 {"user-config", 0x7c0000, 0x10000},
224 {"default-config", 0x7d0000, 0x10000},
225 {"log", 0x7e0000, 0x10000},
226 {"radio", 0x7f0000, 0x10000},
230 .first_sysupgrade_partition
= "os-image",
231 .last_sysupgrade_partition
= "support-list",
234 /** Firmware layout for the CPE220 V3 */
237 .vendor
= "CPE220(TP-LINK|UN|N300-2|00000000):3.0\r\n",
240 "CPE220(TP-LINK|EU|N300-2|00000000):3.0\r\n"
241 "CPE220(TP-LINK|EU|N300-2|45550000):3.0\r\n"
242 "CPE220(TP-LINK|EU|N300-2|55530000):3.0\r\n"
243 "CPE220(TP-LINK|UN|N300-2|00000000):3.0\r\n"
244 "CPE220(TP-LINK|UN|N300-2|45550000):3.0\r\n"
245 "CPE220(TP-LINK|UN|N300-2|55530000):3.0\r\n"
246 "CPE220(TP-LINK|US|N300-2|55530000):3.0\r\n"
247 "CPE220(TP-LINK|UN|N300-2):3.0\r\n"
248 "CPE220(TP-LINK|EU|N300-2):3.0\r\n"
249 "CPE220(TP-LINK|US|N300-2):3.0\r\n",
250 .support_trail
= '\xff',
254 {"fs-uboot", 0x00000, 0x20000},
255 {"partition-table", 0x20000, 0x02000},
256 {"default-mac", 0x30000, 0x00020},
257 {"product-info", 0x31100, 0x00100},
258 {"device-info", 0x31400, 0x00400},
259 {"signature", 0x32000, 0x00400},
260 {"device-id", 0x33000, 0x00100},
261 {"firmware", 0x40000, 0x770000},
262 {"soft-version", 0x7b0000, 0x00100},
263 {"support-list", 0x7b1000, 0x01000},
264 {"user-config", 0x7c0000, 0x10000},
265 {"default-config", 0x7d0000, 0x10000},
266 {"log", 0x7e0000, 0x10000},
267 {"radio", 0x7f0000, 0x10000},
271 .first_sysupgrade_partition
= "os-image",
272 .last_sysupgrade_partition
= "support-list",
275 /** Firmware layout for the CPE510/520 V1 */
278 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
281 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
282 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
283 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
284 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
285 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
286 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
287 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
288 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
289 .support_trail
= '\xff',
293 {"fs-uboot", 0x00000, 0x20000},
294 {"partition-table", 0x20000, 0x02000},
295 {"default-mac", 0x30000, 0x00020},
296 {"product-info", 0x31100, 0x00100},
297 {"signature", 0x32000, 0x00400},
298 {"os-image", 0x40000, 0x1c0000},
299 {"file-system", 0x200000, 0x5b0000},
300 {"soft-version", 0x7b0000, 0x00100},
301 {"support-list", 0x7b1000, 0x00400},
302 {"user-config", 0x7c0000, 0x10000},
303 {"default-config", 0x7d0000, 0x10000},
304 {"log", 0x7e0000, 0x10000},
305 {"radio", 0x7f0000, 0x10000},
309 .first_sysupgrade_partition
= "os-image",
310 .last_sysupgrade_partition
= "support-list",
313 /** Firmware layout for the CPE510 V2 */
316 .vendor
= "CPE510(TP-LINK|UN|N300-5):2.0\r\n",
319 "CPE510(TP-LINK|EU|N300-5|00000000):2.0\r\n"
320 "CPE510(TP-LINK|EU|N300-5|45550000):2.0\r\n"
321 "CPE510(TP-LINK|EU|N300-5|55530000):2.0\r\n"
322 "CPE510(TP-LINK|UN|N300-5|00000000):2.0\r\n"
323 "CPE510(TP-LINK|UN|N300-5|45550000):2.0\r\n"
324 "CPE510(TP-LINK|UN|N300-5|55530000):2.0\r\n"
325 "CPE510(TP-LINK|US|N300-5|00000000):2.0\r\n"
326 "CPE510(TP-LINK|US|N300-5|45550000):2.0\r\n"
327 "CPE510(TP-LINK|US|N300-5|55530000):2.0\r\n"
328 "CPE510(TP-LINK|UN|N300-5):2.0\r\n"
329 "CPE510(TP-LINK|EU|N300-5):2.0\r\n"
330 "CPE510(TP-LINK|US|N300-5):2.0\r\n",
331 .support_trail
= '\xff',
335 {"fs-uboot", 0x00000, 0x20000},
336 {"partition-table", 0x20000, 0x02000},
337 {"default-mac", 0x30000, 0x00020},
338 {"product-info", 0x31100, 0x00100},
339 {"signature", 0x32000, 0x00400},
340 {"os-image", 0x40000, 0x1c0000},
341 {"file-system", 0x200000, 0x5b0000},
342 {"soft-version", 0x7b0000, 0x00100},
343 {"support-list", 0x7b1000, 0x00400},
344 {"user-config", 0x7c0000, 0x10000},
345 {"default-config", 0x7d0000, 0x10000},
346 {"log", 0x7e0000, 0x10000},
347 {"radio", 0x7f0000, 0x10000},
351 .first_sysupgrade_partition
= "os-image",
352 .last_sysupgrade_partition
= "support-list",
357 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
360 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
361 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
362 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
363 .support_trail
= '\xff',
367 {"fs-uboot", 0x00000, 0x20000},
368 {"partition-table", 0x20000, 0x02000},
369 {"default-mac", 0x30000, 0x00020},
370 {"product-info", 0x31100, 0x00100},
371 {"signature", 0x32000, 0x00400},
372 {"os-image", 0x40000, 0x1c0000},
373 {"file-system", 0x200000, 0x5b0000},
374 {"soft-version", 0x7b0000, 0x00100},
375 {"support-list", 0x7b1000, 0x00400},
376 {"user-config", 0x7c0000, 0x10000},
377 {"default-config", 0x7d0000, 0x10000},
378 {"log", 0x7e0000, 0x10000},
379 {"radio", 0x7f0000, 0x10000},
383 .first_sysupgrade_partition
= "os-image",
384 .last_sysupgrade_partition
= "support-list",
389 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
392 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
393 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
394 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
395 .support_trail
= '\xff',
399 {"fs-uboot", 0x00000, 0x20000},
400 {"partition-table", 0x20000, 0x02000},
401 {"default-mac", 0x30000, 0x00020},
402 {"product-info", 0x31100, 0x00100},
403 {"signature", 0x32000, 0x00400},
404 {"os-image", 0x40000, 0x1c0000},
405 {"file-system", 0x200000, 0x5b0000},
406 {"soft-version", 0x7b0000, 0x00100},
407 {"support-list", 0x7b1000, 0x00400},
408 {"user-config", 0x7c0000, 0x10000},
409 {"default-config", 0x7d0000, 0x10000},
410 {"log", 0x7e0000, 0x10000},
411 {"radio", 0x7f0000, 0x10000},
415 .first_sysupgrade_partition
= "os-image",
416 .last_sysupgrade_partition
= "support-list",
419 /** Firmware layout for the C2600 */
425 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
426 .support_trail
= '\x00',
430 We use a bigger os-image partition than the stock images (and thus
431 smaller file-system), as our kernel doesn't fit in the stock firmware's
432 2 MB os-image since kernel 4.14.
435 {"SBL1", 0x00000, 0x20000},
436 {"MIBIB", 0x20000, 0x20000},
437 {"SBL2", 0x40000, 0x20000},
438 {"SBL3", 0x60000, 0x30000},
439 {"DDRCONFIG", 0x90000, 0x10000},
440 {"SSD", 0xa0000, 0x10000},
441 {"TZ", 0xb0000, 0x30000},
442 {"RPM", 0xe0000, 0x20000},
443 {"fs-uboot", 0x100000, 0x70000},
444 {"uboot-env", 0x170000, 0x40000},
445 {"radio", 0x1b0000, 0x40000},
446 {"os-image", 0x1f0000, 0x400000}, /* Stock: base 0x1f0000 size 0x200000 */
447 {"file-system", 0x5f0000, 0x1900000}, /* Stock: base 0x3f0000 size 0x1b00000 */
448 {"default-mac", 0x1ef0000, 0x00200},
449 {"pin", 0x1ef0200, 0x00200},
450 {"product-info", 0x1ef0400, 0x0fc00},
451 {"partition-table", 0x1f00000, 0x10000},
452 {"soft-version", 0x1f10000, 0x10000},
453 {"support-list", 0x1f20000, 0x10000},
454 {"profile", 0x1f30000, 0x10000},
455 {"default-config", 0x1f40000, 0x10000},
456 {"user-config", 0x1f50000, 0x40000},
457 {"qos-db", 0x1f90000, 0x40000},
458 {"usb-config", 0x1fd0000, 0x10000},
459 {"log", 0x1fe0000, 0x20000},
463 .first_sysupgrade_partition
= "os-image",
464 .last_sysupgrade_partition
= "file-system"
467 /** Firmware layout for the A7-V5 */
469 .id
= "ARCHER-A7-V5",
472 "{product_name:Archer A7,product_ver:5.0.0,special_id:45550000}\n"
473 "{product_name:Archer A7,product_ver:5.0.0,special_id:55530000}\n"
474 "{product_name:Archer A7,product_ver:5.0.0,special_id:43410000}\n"
475 "{product_name:Archer A7,product_ver:5.0.0,special_id:4A500000}\n"
476 "{product_name:Archer A7,product_ver:5.0.0,special_id:54570000}\n",
477 .support_trail
= '\x00',
478 .soft_ver
= "soft_ver:1.0.0\n",
480 /* We're using a dynamic kernel/rootfs split here */
482 {"factory-boot", 0x00000, 0x20000},
483 {"fs-uboot", 0x20000, 0x20000},
484 {"firmware", 0x40000, 0xec0000}, /* Stock: name os-image base 0x40000 size 0x120000 */
485 /* Stock: name file-system base 0x160000 size 0xda0000 */
486 {"default-mac", 0xf40000, 0x00200},
487 {"pin", 0xf40200, 0x00200},
488 {"device-id", 0xf40400, 0x00100},
489 {"product-info", 0xf40500, 0x0fb00},
490 {"soft-version", 0xf50000, 0x00100},
491 {"extra-para", 0xf51000, 0x01000},
492 {"support-list", 0xf52000, 0x0a000},
493 {"profile", 0xf5c000, 0x04000},
494 {"default-config", 0xf60000, 0x10000},
495 {"user-config", 0xf70000, 0x40000},
496 {"certificate", 0xfb0000, 0x10000},
497 {"partition-table", 0xfc0000, 0x10000},
498 {"log", 0xfd0000, 0x20000},
499 {"radio", 0xff0000, 0x10000},
503 .first_sysupgrade_partition
= "os-image",
504 .last_sysupgrade_partition
= "file-system",
507 /** Firmware layout for the C2v3 */
509 .id
= "ARCHER-C2-V3",
512 "{product_name:ArcherC2,product_ver:3.0.0,special_id:00000000}\n"
513 "{product_name:ArcherC2,product_ver:3.0.0,special_id:55530000}\n"
514 "{product_name:ArcherC2,product_ver:3.0.0,special_id:45550000}\n",
515 .support_trail
= '\x00',
516 .soft_ver
= "soft_ver:3.0.1\n",
518 /** We're using a dynamic kernel/rootfs split here */
521 {"factory-boot", 0x00000, 0x20000},
522 {"fs-uboot", 0x20000, 0x10000},
523 {"firmware", 0x30000, 0x7a0000},
524 {"user-config", 0x7d0000, 0x04000},
525 {"default-mac", 0x7e0000, 0x00100},
526 {"device-id", 0x7e0100, 0x00100},
527 {"extra-para", 0x7e0200, 0x00100},
528 {"pin", 0x7e0300, 0x00100},
529 {"support-list", 0x7e0400, 0x00400},
530 {"soft-version", 0x7e0800, 0x00400},
531 {"product-info", 0x7e0c00, 0x01400},
532 {"partition-table", 0x7e2000, 0x01000},
533 {"profile", 0x7e3000, 0x01000},
534 {"default-config", 0x7e4000, 0x04000},
535 {"merge-config", 0x7ec000, 0x02000},
536 {"qos-db", 0x7ee000, 0x02000},
537 {"radio", 0x7f0000, 0x10000},
541 .first_sysupgrade_partition
= "os-image",
542 .last_sysupgrade_partition
= "file-system",
545 /** Firmware layout for the C25v1 */
547 .id
= "ARCHER-C25-V1",
550 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
551 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
552 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
553 .support_trail
= '\x00',
554 .soft_ver
= "soft_ver:1.0.0\n",
556 /* We're using a dynamic kernel/rootfs split here */
558 {"factory-boot", 0x00000, 0x20000},
559 {"fs-uboot", 0x20000, 0x10000},
560 {"firmware", 0x30000, 0x7a0000}, /* Stock: name os-image base 0x30000 size 0x100000 */
561 /* Stock: name file-system base 0x130000 size 0x6a0000 */
562 {"user-config", 0x7d0000, 0x04000},
563 {"default-mac", 0x7e0000, 0x00100},
564 {"device-id", 0x7e0100, 0x00100},
565 {"extra-para", 0x7e0200, 0x00100},
566 {"pin", 0x7e0300, 0x00100},
567 {"support-list", 0x7e0400, 0x00400},
568 {"soft-version", 0x7e0800, 0x00400},
569 {"product-info", 0x7e0c00, 0x01400},
570 {"partition-table", 0x7e2000, 0x01000},
571 {"profile", 0x7e3000, 0x01000},
572 {"default-config", 0x7e4000, 0x04000},
573 {"merge-config", 0x7ec000, 0x02000},
574 {"qos-db", 0x7ee000, 0x02000},
575 {"radio", 0x7f0000, 0x10000},
579 .first_sysupgrade_partition
= "os-image",
580 .last_sysupgrade_partition
= "file-system",
583 /** Firmware layout for the C58v1 */
585 .id
= "ARCHER-C58-V1",
589 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
590 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
591 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
592 .support_trail
= '\x00',
593 .soft_ver
= "soft_ver:1.0.0\n",
596 {"fs-uboot", 0x00000, 0x10000},
597 {"default-mac", 0x10000, 0x00200},
598 {"pin", 0x10200, 0x00200},
599 {"product-info", 0x10400, 0x00100},
600 {"partition-table", 0x10500, 0x00800},
601 {"soft-version", 0x11300, 0x00200},
602 {"support-list", 0x11500, 0x00100},
603 {"device-id", 0x11600, 0x00100},
604 {"profile", 0x11700, 0x03900},
605 {"default-config", 0x15000, 0x04000},
606 {"user-config", 0x19000, 0x04000},
607 {"firmware", 0x20000, 0x7c8000},
608 {"certyficate", 0x7e8000, 0x08000},
609 {"radio", 0x7f0000, 0x10000},
613 .first_sysupgrade_partition
= "os-image",
614 .last_sysupgrade_partition
= "file-system",
617 /** Firmware layout for the C59v1 */
619 .id
= "ARCHER-C59-V1",
623 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
624 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
625 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
626 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
627 .support_trail
= '\x00',
628 .soft_ver
= "soft_ver:1.0.0\n",
630 /* We're using a dynamic kernel/rootfs split here */
632 {"fs-uboot", 0x00000, 0x10000},
633 {"default-mac", 0x10000, 0x00200},
634 {"pin", 0x10200, 0x00200},
635 {"device-id", 0x10400, 0x00100},
636 {"product-info", 0x10500, 0x0fb00},
637 {"firmware", 0x20000, 0xe30000},
638 {"partition-table", 0xe50000, 0x10000},
639 {"soft-version", 0xe60000, 0x10000},
640 {"support-list", 0xe70000, 0x10000},
641 {"profile", 0xe80000, 0x10000},
642 {"default-config", 0xe90000, 0x10000},
643 {"user-config", 0xea0000, 0x40000},
644 {"usb-config", 0xee0000, 0x10000},
645 {"certificate", 0xef0000, 0x10000},
646 {"qos-db", 0xf00000, 0x40000},
647 {"log", 0xfe0000, 0x10000},
648 {"radio", 0xff0000, 0x10000},
652 .first_sysupgrade_partition
= "os-image",
653 .last_sysupgrade_partition
= "file-system",
656 /** Firmware layout for the C59v2 */
658 .id
= "ARCHER-C59-V2",
662 "{product_name:Archer C59,product_ver:2.0.0,special_id:00000000}\r\n"
663 "{product_name:Archer C59,product_ver:2.0.0,special_id:45550000}\r\n"
664 "{product_name:Archer C59,product_ver:2.0.0,special_id:55530000}\r\n",
665 .support_trail
= '\x00',
666 .soft_ver
= "soft_ver:2.0.0 Build 20161206 rel.7303\n",
668 /** We're using a dynamic kernel/rootfs split here */
670 {"factory-boot", 0x00000, 0x20000},
671 {"fs-uboot", 0x20000, 0x10000},
672 {"default-mac", 0x30000, 0x00200},
673 {"pin", 0x30200, 0x00200},
674 {"device-id", 0x30400, 0x00100},
675 {"product-info", 0x30500, 0x0fb00},
676 {"firmware", 0x40000, 0xe10000},
677 {"partition-table", 0xe50000, 0x10000},
678 {"soft-version", 0xe60000, 0x10000},
679 {"support-list", 0xe70000, 0x10000},
680 {"profile", 0xe80000, 0x10000},
681 {"default-config", 0xe90000, 0x10000},
682 {"user-config", 0xea0000, 0x40000},
683 {"usb-config", 0xee0000, 0x10000},
684 {"certificate", 0xef0000, 0x10000},
685 {"extra-para", 0xf00000, 0x10000},
686 {"qos-db", 0xf10000, 0x30000},
687 {"log", 0xfe0000, 0x10000},
688 {"radio", 0xff0000, 0x10000},
692 .first_sysupgrade_partition
= "os-image",
693 .last_sysupgrade_partition
= "file-system",
696 /** Firmware layout for the C6v2 */
698 .id
= "ARCHER-C6-V2",
702 "{product_name:Archer C6,product_ver:2.0.0,special_id:45550000}\r\n"
703 "{product_name:Archer C6,product_ver:2.0.0,special_id:52550000}\r\n"
704 "{product_name:Archer C6,product_ver:2.0.0,special_id:4A500000}\r\n",
705 .support_trail
= '\x00',
706 .soft_ver
= "soft_ver:1.1.1\n",
709 {"fs-uboot", 0x00000, 0x20000},
710 {"default-mac", 0x20000, 0x00200},
711 {"pin", 0x20200, 0x00100},
712 {"product-info", 0x20300, 0x00200},
713 {"device-id", 0x20500, 0x0fb00},
714 {"firmware", 0x30000, 0x7a9400},
715 {"soft-version", 0x7d9400, 0x00100},
716 {"extra-para", 0x7d9500, 0x00100},
717 {"support-list", 0x7d9600, 0x00200},
718 {"profile", 0x7d9800, 0x03000},
719 {"default-config", 0x7dc800, 0x03000},
720 {"partition-table", 0x7df800, 0x00800},
721 {"user-config", 0x7e0000, 0x0c000},
722 {"certificate", 0x7ec000, 0x04000},
723 {"radio", 0x7f0000, 0x10000},
727 .first_sysupgrade_partition
= "os-image",
728 .last_sysupgrade_partition
= "file-system",
732 /** Firmware layout for the C60v1 */
734 .id
= "ARCHER-C60-V1",
738 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
739 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
740 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
741 .support_trail
= '\x00',
742 .soft_ver
= "soft_ver:1.0.0\n",
745 {"fs-uboot", 0x00000, 0x10000},
746 {"default-mac", 0x10000, 0x00200},
747 {"pin", 0x10200, 0x00200},
748 {"product-info", 0x10400, 0x00100},
749 {"partition-table", 0x10500, 0x00800},
750 {"soft-version", 0x11300, 0x00200},
751 {"support-list", 0x11500, 0x00100},
752 {"device-id", 0x11600, 0x00100},
753 {"profile", 0x11700, 0x03900},
754 {"default-config", 0x15000, 0x04000},
755 {"user-config", 0x19000, 0x04000},
756 {"firmware", 0x20000, 0x7c8000},
757 {"certyficate", 0x7e8000, 0x08000},
758 {"radio", 0x7f0000, 0x10000},
762 .first_sysupgrade_partition
= "os-image",
763 .last_sysupgrade_partition
= "file-system",
766 /** Firmware layout for the C60v2 */
768 .id
= "ARCHER-C60-V2",
772 "{product_name:Archer C60,product_ver:2.0.0,special_id:42520000}\r\n"
773 "{product_name:Archer C60,product_ver:2.0.0,special_id:45550000}\r\n"
774 "{product_name:Archer C60,product_ver:2.0.0,special_id:55530000}\r\n",
775 .support_trail
= '\x00',
776 .soft_ver
= "soft_ver:2.0.0\n",
779 {"factory-boot", 0x00000, 0x1fb00},
780 {"default-mac", 0x1fb00, 0x00200},
781 {"pin", 0x1fd00, 0x00100},
782 {"product-info", 0x1fe00, 0x00100},
783 {"device-id", 0x1ff00, 0x00100},
784 {"fs-uboot", 0x20000, 0x10000},
785 {"firmware", 0x30000, 0x7a0000},
786 {"soft-version", 0x7d9500, 0x00100},
787 {"support-list", 0x7d9600, 0x00100},
788 {"extra-para", 0x7d9700, 0x00100},
789 {"profile", 0x7d9800, 0x03000},
790 {"default-config", 0x7dc800, 0x03000},
791 {"partition-table", 0x7df800, 0x00800},
792 {"user-config", 0x7e0000, 0x0c000},
793 {"certificate", 0x7ec000, 0x04000},
794 {"radio", 0x7f0000, 0x10000},
798 .first_sysupgrade_partition
= "os-image",
799 .last_sysupgrade_partition
= "file-system",
802 /** Firmware layout for the C60v3 */
804 .id
= "ARCHER-C60-V3",
808 "{product_name:Archer C60,product_ver:3.0.0,special_id:42520000}\r\n"
809 "{product_name:Archer C60,product_ver:3.0.0,special_id:45550000}\r\n"
810 "{product_name:Archer C60,product_ver:3.0.0,special_id:55530000}\r\n",
811 .support_trail
= '\x00',
812 .soft_ver
= "soft_ver:3.0.0\n",
815 {"factory-boot", 0x00000, 0x1fb00},
816 {"default-mac", 0x1fb00, 0x00200},
817 {"pin", 0x1fd00, 0x00100},
818 {"product-info", 0x1fe00, 0x00100},
819 {"device-id", 0x1ff00, 0x00100},
820 {"fs-uboot", 0x20000, 0x10000},
821 {"firmware", 0x30000, 0x7a0000},
822 {"soft-version", 0x7d9500, 0x00100},
823 {"support-list", 0x7d9600, 0x00100},
824 {"extra-para", 0x7d9700, 0x00100},
825 {"profile", 0x7d9800, 0x03000},
826 {"default-config", 0x7dc800, 0x03000},
827 {"partition-table", 0x7df800, 0x00800},
828 {"user-config", 0x7e0000, 0x0c000},
829 {"certificate", 0x7ec000, 0x04000},
830 {"radio", 0x7f0000, 0x10000},
834 .first_sysupgrade_partition
= "os-image",
835 .last_sysupgrade_partition
= "file-system",
838 /** Firmware layout for the C5 */
840 .id
= "ARCHER-C5-V2",
844 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
845 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
846 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
847 .support_trail
= '\x00',
851 {"fs-uboot", 0x00000, 0x40000},
852 {"os-image", 0x40000, 0x200000},
853 {"file-system", 0x240000, 0xc00000},
854 {"default-mac", 0xe40000, 0x00200},
855 {"pin", 0xe40200, 0x00200},
856 {"product-info", 0xe40400, 0x00200},
857 {"partition-table", 0xe50000, 0x10000},
858 {"soft-version", 0xe60000, 0x00200},
859 {"support-list", 0xe61000, 0x0f000},
860 {"profile", 0xe70000, 0x10000},
861 {"default-config", 0xe80000, 0x10000},
862 {"user-config", 0xe90000, 0x50000},
863 {"log", 0xee0000, 0x100000},
864 {"radio_bk", 0xfe0000, 0x10000},
865 {"radio", 0xff0000, 0x10000},
869 .first_sysupgrade_partition
= "os-image",
870 .last_sysupgrade_partition
= "file-system"
873 /** Firmware layout for the C7 */
875 .id
= "ARCHER-C7-V4",
878 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
879 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
880 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
881 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
882 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
883 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
884 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
885 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
886 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
887 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
888 .support_trail
= '\x00',
889 .soft_ver
= "soft_ver:1.0.0\n",
891 /* We're using a dynamic kernel/rootfs split here */
893 {"factory-boot", 0x00000, 0x20000},
894 {"fs-uboot", 0x20000, 0x20000},
895 {"firmware", 0x40000, 0xEC0000}, /* Stock: name os-image base 0x40000 size 0x120000 */
896 /* Stock: name file-system base 0x160000 size 0xda0000 */
897 {"default-mac", 0xf00000, 0x00200},
898 {"pin", 0xf00200, 0x00200},
899 {"device-id", 0xf00400, 0x00100},
900 {"product-info", 0xf00500, 0x0fb00},
901 {"soft-version", 0xf10000, 0x00100},
902 {"extra-para", 0xf11000, 0x01000},
903 {"support-list", 0xf12000, 0x0a000},
904 {"profile", 0xf1c000, 0x04000},
905 {"default-config", 0xf20000, 0x10000},
906 {"user-config", 0xf30000, 0x40000},
907 {"qos-db", 0xf70000, 0x40000},
908 {"certificate", 0xfb0000, 0x10000},
909 {"partition-table", 0xfc0000, 0x10000},
910 {"log", 0xfd0000, 0x20000},
911 {"radio", 0xff0000, 0x10000},
915 .first_sysupgrade_partition
= "os-image",
916 .last_sysupgrade_partition
= "file-system",
919 /** Firmware layout for the C7 v5*/
921 .id
= "ARCHER-C7-V5",
924 "{product_name:Archer C7,product_ver:5.0.0,special_id:00000000}\n"
925 "{product_name:Archer C7,product_ver:5.0.0,special_id:45550000}\n"
926 "{product_name:Archer C7,product_ver:5.0.0,special_id:55530000}\n"
927 "{product_name:Archer C7,product_ver:5.0.0,special_id:43410000}\n"
928 "{product_name:Archer C7,product_ver:5.0.0,special_id:4A500000}\n"
929 "{product_name:Archer C7,product_ver:5.0.0,special_id:54570000}\n"
930 "{product_name:Archer C7,product_ver:5.0.0,special_id:52550000}\n"
931 "{product_name:Archer C7,product_ver:5.0.0,special_id:4B520000}\n",
933 .support_trail
= '\x00',
934 .soft_ver
= "soft_ver:1.0.0\n",
936 /* We're using a dynamic kernel/rootfs split here */
938 {"factory-boot", 0x00000, 0x20000},
939 {"fs-uboot", 0x20000, 0x20000},
940 {"partition-table", 0x40000, 0x10000},
941 {"radio", 0x50000, 0x10000},
942 {"default-mac", 0x60000, 0x00200},
943 {"pin", 0x60200, 0x00200},
944 {"device-id", 0x60400, 0x00100},
945 {"product-info", 0x60500, 0x0fb00},
946 {"soft-version", 0x70000, 0x01000},
947 {"extra-para", 0x71000, 0x01000},
948 {"support-list", 0x72000, 0x0a000},
949 {"profile", 0x7c000, 0x04000},
950 {"user-config", 0x80000, 0x40000},
953 {"firmware", 0xc0000, 0xf00000}, /* Stock: name os-image base 0xc0000 size 0x120000 */
954 /* Stock: name file-system base 0x1e0000 size 0xde0000 */
956 {"log", 0xfc0000, 0x20000},
957 {"certificate", 0xfe0000, 0x10000},
958 {"default-config", 0xff0000, 0x10000},
963 .first_sysupgrade_partition
= "os-image",
964 .last_sysupgrade_partition
= "file-system",
967 /** Firmware layout for the C9 */
973 "{product_name:ArcherC9,"
975 "special_id:00000000}\n",
976 .support_trail
= '\x00',
980 {"fs-uboot", 0x00000, 0x40000},
981 {"os-image", 0x40000, 0x200000},
982 {"file-system", 0x240000, 0xc00000},
983 {"default-mac", 0xe40000, 0x00200},
984 {"pin", 0xe40200, 0x00200},
985 {"product-info", 0xe40400, 0x00200},
986 {"partition-table", 0xe50000, 0x10000},
987 {"soft-version", 0xe60000, 0x00200},
988 {"support-list", 0xe61000, 0x0f000},
989 {"profile", 0xe70000, 0x10000},
990 {"default-config", 0xe80000, 0x10000},
991 {"user-config", 0xe90000, 0x50000},
992 {"log", 0xee0000, 0x100000},
993 {"radio_bk", 0xfe0000, 0x10000},
994 {"radio", 0xff0000, 0x10000},
998 .first_sysupgrade_partition
= "os-image",
999 .last_sysupgrade_partition
= "file-system"
1002 /** Firmware layout for the EAP120 */
1005 .vendor
= "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
1008 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
1009 .support_trail
= '\xff',
1013 {"fs-uboot", 0x00000, 0x20000},
1014 {"partition-table", 0x20000, 0x02000},
1015 {"default-mac", 0x30000, 0x00020},
1016 {"support-list", 0x31000, 0x00100},
1017 {"product-info", 0x31100, 0x00100},
1018 {"soft-version", 0x32000, 0x00100},
1019 {"os-image", 0x40000, 0x180000},
1020 {"file-system", 0x1c0000, 0x600000},
1021 {"user-config", 0x7c0000, 0x10000},
1022 {"backup-config", 0x7d0000, 0x10000},
1023 {"log", 0x7e0000, 0x10000},
1024 {"radio", 0x7f0000, 0x10000},
1028 .first_sysupgrade_partition
= "os-image",
1029 .last_sysupgrade_partition
= "file-system"
1032 /** Firmware layout for the TL-WA850RE v2 */
1034 .id
= "TLWA850REV2",
1038 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
1039 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
1040 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
1041 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
1042 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
1043 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
1044 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
1045 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
1046 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
1047 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
1048 .support_trail
= '\x00',
1052 576KB were moved from file-system to os-image
1053 in comparison to the stock image
1056 {"fs-uboot", 0x00000, 0x20000},
1057 {"os-image", 0x20000, 0x150000},
1058 {"file-system", 0x170000, 0x240000},
1059 {"partition-table", 0x3b0000, 0x02000},
1060 {"default-mac", 0x3c0000, 0x00020},
1061 {"pin", 0x3c0100, 0x00020},
1062 {"product-info", 0x3c1000, 0x01000},
1063 {"soft-version", 0x3c2000, 0x00100},
1064 {"support-list", 0x3c3000, 0x01000},
1065 {"profile", 0x3c4000, 0x08000},
1066 {"user-config", 0x3d0000, 0x10000},
1067 {"default-config", 0x3e0000, 0x10000},
1068 {"radio", 0x3f0000, 0x10000},
1072 .first_sysupgrade_partition
= "os-image",
1073 .last_sysupgrade_partition
= "file-system"
1076 /** Firmware layout for the TL-WA855RE v1 */
1078 .id
= "TLWA855REV1",
1082 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
1083 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
1084 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
1085 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
1086 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
1087 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
1088 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
1089 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
1090 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
1091 .support_trail
= '\x00',
1095 {"fs-uboot", 0x00000, 0x20000},
1096 {"os-image", 0x20000, 0x150000},
1097 {"file-system", 0x170000, 0x240000},
1098 {"partition-table", 0x3b0000, 0x02000},
1099 {"default-mac", 0x3c0000, 0x00020},
1100 {"pin", 0x3c0100, 0x00020},
1101 {"product-info", 0x3c1000, 0x01000},
1102 {"soft-version", 0x3c2000, 0x00100},
1103 {"support-list", 0x3c3000, 0x01000},
1104 {"profile", 0x3c4000, 0x08000},
1105 {"user-config", 0x3d0000, 0x10000},
1106 {"default-config", 0x3e0000, 0x10000},
1107 {"radio", 0x3f0000, 0x10000},
1111 .first_sysupgrade_partition
= "os-image",
1112 .last_sysupgrade_partition
= "file-system"
1115 /** Firmware layout for the TL-WR1043 v5 */
1117 .id
= "TLWR1043NV5",
1121 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
1122 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
1123 .support_trail
= '\x00',
1124 .soft_ver
= "soft_ver:1.0.0\n",
1126 {"factory-boot", 0x00000, 0x20000},
1127 {"fs-uboot", 0x20000, 0x20000},
1128 {"firmware", 0x40000, 0xec0000},
1129 {"default-mac", 0xf00000, 0x00200},
1130 {"pin", 0xf00200, 0x00200},
1131 {"device-id", 0xf00400, 0x00100},
1132 {"product-info", 0xf00500, 0x0fb00},
1133 {"soft-version", 0xf10000, 0x01000},
1134 {"extra-para", 0xf11000, 0x01000},
1135 {"support-list", 0xf12000, 0x0a000},
1136 {"profile", 0xf1c000, 0x04000},
1137 {"default-config", 0xf20000, 0x10000},
1138 {"user-config", 0xf30000, 0x40000},
1139 {"qos-db", 0xf70000, 0x40000},
1140 {"certificate", 0xfb0000, 0x10000},
1141 {"partition-table", 0xfc0000, 0x10000},
1142 {"log", 0xfd0000, 0x20000},
1143 {"radio", 0xff0000, 0x10000},
1146 .first_sysupgrade_partition
= "os-image",
1147 .last_sysupgrade_partition
= "file-system"
1150 /** Firmware layout for the TL-WR1043 v4 */
1152 .id
= "TLWR1043NDV4",
1156 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
1157 .support_trail
= '\x00',
1160 /* We're using a dynamic kernel/rootfs split here */
1162 {"fs-uboot", 0x00000, 0x20000},
1163 {"firmware", 0x20000, 0xf30000},
1164 {"default-mac", 0xf50000, 0x00200},
1165 {"pin", 0xf50200, 0x00200},
1166 {"product-info", 0xf50400, 0x0fc00},
1167 {"soft-version", 0xf60000, 0x0b000},
1168 {"support-list", 0xf6b000, 0x04000},
1169 {"profile", 0xf70000, 0x04000},
1170 {"default-config", 0xf74000, 0x0b000},
1171 {"user-config", 0xf80000, 0x40000},
1172 {"partition-table", 0xfc0000, 0x10000},
1173 {"log", 0xfd0000, 0x20000},
1174 {"radio", 0xff0000, 0x10000},
1178 .first_sysupgrade_partition
= "os-image",
1179 .last_sysupgrade_partition
= "file-system"
1182 /** Firmware layout for the TL-WR902AC v1 */
1184 .id
= "TL-WR902AC-V1",
1188 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
1189 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
1190 .support_trail
= '\x00',
1194 384KB were moved from file-system to os-image
1195 in comparison to the stock image
1198 {"fs-uboot", 0x00000, 0x20000},
1199 {"firmware", 0x20000, 0x730000},
1200 {"default-mac", 0x750000, 0x00200},
1201 {"pin", 0x750200, 0x00200},
1202 {"product-info", 0x750400, 0x0fc00},
1203 {"soft-version", 0x760000, 0x0b000},
1204 {"support-list", 0x76b000, 0x04000},
1205 {"profile", 0x770000, 0x04000},
1206 {"default-config", 0x774000, 0x0b000},
1207 {"user-config", 0x780000, 0x40000},
1208 {"partition-table", 0x7c0000, 0x10000},
1209 {"log", 0x7d0000, 0x20000},
1210 {"radio", 0x7f0000, 0x10000},
1214 .first_sysupgrade_partition
= "os-image",
1215 .last_sysupgrade_partition
= "file-system",
1218 /** Firmware layout for the TL-WR942N V1 */
1224 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
1225 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
1226 .support_trail
= '\x00',
1230 {"fs-uboot", 0x00000, 0x20000},
1231 {"firmware", 0x20000, 0xe20000},
1232 {"default-mac", 0xe40000, 0x00200},
1233 {"pin", 0xe40200, 0x00200},
1234 {"product-info", 0xe40400, 0x0fc00},
1235 {"partition-table", 0xe50000, 0x10000},
1236 {"soft-version", 0xe60000, 0x10000},
1237 {"support-list", 0xe70000, 0x10000},
1238 {"profile", 0xe80000, 0x10000},
1239 {"default-config", 0xe90000, 0x10000},
1240 {"user-config", 0xea0000, 0x40000},
1241 {"qos-db", 0xee0000, 0x40000},
1242 {"certificate", 0xf20000, 0x10000},
1243 {"usb-config", 0xfb0000, 0x10000},
1244 {"log", 0xfc0000, 0x20000},
1245 {"radio-bk", 0xfe0000, 0x10000},
1246 {"radio", 0xff0000, 0x10000},
1250 .first_sysupgrade_partition
= "os-image",
1251 .last_sysupgrade_partition
= "file-system",
1254 /** Firmware layout for the RE350 v1 */
1260 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
1261 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
1262 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
1263 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
1264 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
1265 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
1266 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
1267 .support_trail
= '\x00',
1270 /** We're using a dynamic kernel/rootfs split here */
1272 {"fs-uboot", 0x00000, 0x20000},
1273 {"firmware", 0x20000, 0x5e0000},
1274 {"partition-table", 0x600000, 0x02000},
1275 {"default-mac", 0x610000, 0x00020},
1276 {"pin", 0x610100, 0x00020},
1277 {"product-info", 0x611100, 0x01000},
1278 {"soft-version", 0x620000, 0x01000},
1279 {"support-list", 0x621000, 0x01000},
1280 {"profile", 0x622000, 0x08000},
1281 {"user-config", 0x630000, 0x10000},
1282 {"default-config", 0x640000, 0x10000},
1283 {"radio", 0x7f0000, 0x10000},
1287 .first_sysupgrade_partition
= "os-image",
1288 .last_sysupgrade_partition
= "file-system"
1291 /** Firmware layout for the RE350K v1 */
1297 "{product_name:RE350K,product_ver:1.0.0,special_id:00000000,product_region:US}\n",
1298 .support_trail
= '\x00',
1301 /** We're using a dynamic kernel/rootfs split here */
1303 {"fs-uboot", 0x00000, 0x20000},
1304 {"firmware", 0x20000, 0xd70000},
1305 {"partition-table", 0xd90000, 0x02000},
1306 {"default-mac", 0xda0000, 0x00020},
1307 {"pin", 0xda0100, 0x00020},
1308 {"product-info", 0xda1100, 0x01000},
1309 {"soft-version", 0xdb0000, 0x01000},
1310 {"support-list", 0xdb1000, 0x01000},
1311 {"profile", 0xdb2000, 0x08000},
1312 {"user-config", 0xdc0000, 0x10000},
1313 {"default-config", 0xdd0000, 0x10000},
1314 {"device-id", 0xde0000, 0x00108},
1315 {"radio", 0xff0000, 0x10000},
1319 .first_sysupgrade_partition
= "os-image",
1320 .last_sysupgrade_partition
= "file-system"
1323 /** Firmware layout for the RE355 */
1329 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
1330 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
1331 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
1332 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
1333 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
1334 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
1335 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
1336 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
1337 .support_trail
= '\x00',
1340 /* We're using a dynamic kernel/rootfs split here */
1342 {"fs-uboot", 0x00000, 0x20000},
1343 {"firmware", 0x20000, 0x5e0000},
1344 {"partition-table", 0x600000, 0x02000},
1345 {"default-mac", 0x610000, 0x00020},
1346 {"pin", 0x610100, 0x00020},
1347 {"product-info", 0x611100, 0x01000},
1348 {"soft-version", 0x620000, 0x01000},
1349 {"support-list", 0x621000, 0x01000},
1350 {"profile", 0x622000, 0x08000},
1351 {"user-config", 0x630000, 0x10000},
1352 {"default-config", 0x640000, 0x10000},
1353 {"radio", 0x7f0000, 0x10000},
1357 .first_sysupgrade_partition
= "os-image",
1358 .last_sysupgrade_partition
= "file-system"
1361 /** Firmware layout for the RE450 */
1367 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
1368 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
1369 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
1370 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
1371 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
1372 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
1373 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
1374 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
1375 .support_trail
= '\x00',
1378 /** We're using a dynamic kernel/rootfs split here */
1380 {"fs-uboot", 0x00000, 0x20000},
1381 {"firmware", 0x20000, 0x5e0000},
1382 {"partition-table", 0x600000, 0x02000},
1383 {"default-mac", 0x610000, 0x00020},
1384 {"pin", 0x610100, 0x00020},
1385 {"product-info", 0x611100, 0x01000},
1386 {"soft-version", 0x620000, 0x01000},
1387 {"support-list", 0x621000, 0x01000},
1388 {"profile", 0x622000, 0x08000},
1389 {"user-config", 0x630000, 0x10000},
1390 {"default-config", 0x640000, 0x10000},
1391 {"radio", 0x7f0000, 0x10000},
1395 .first_sysupgrade_partition
= "os-image",
1396 .last_sysupgrade_partition
= "file-system"
1399 /** Firmware layout for the RE450 v2 */
1405 "{product_name:RE450,product_ver:2.0.0,special_id:00000000}\r\n"
1406 "{product_name:RE450,product_ver:2.0.0,special_id:55530000}\r\n"
1407 "{product_name:RE450,product_ver:2.0.0,special_id:45550000}\r\n"
1408 "{product_name:RE450,product_ver:2.0.0,special_id:4A500000}\r\n"
1409 "{product_name:RE450,product_ver:2.0.0,special_id:43410000}\r\n"
1410 "{product_name:RE450,product_ver:2.0.0,special_id:41550000}\r\n"
1411 "{product_name:RE450,product_ver:2.0.0,special_id:41530000}\r\n"
1412 "{product_name:RE450,product_ver:2.0.0,special_id:4B520000}\r\n"
1413 "{product_name:RE450,product_ver:2.0.0,special_id:42520000}\r\n",
1414 .support_trail
= '\x00',
1417 /* We're using a dynamic kernel/rootfs split here */
1419 {"fs-uboot", 0x00000, 0x20000},
1420 {"firmware", 0x20000, 0x5e0000},
1421 {"partition-table", 0x600000, 0x02000},
1422 {"default-mac", 0x610000, 0x00020},
1423 {"pin", 0x610100, 0x00020},
1424 {"product-info", 0x611100, 0x01000},
1425 {"soft-version", 0x620000, 0x01000},
1426 {"support-list", 0x621000, 0x01000},
1427 {"profile", 0x622000, 0x08000},
1428 {"user-config", 0x630000, 0x10000},
1429 {"default-config", 0x640000, 0x10000},
1430 {"radio", 0x7f0000, 0x10000},
1435 .first_sysupgrade_partition
= "os-image",
1436 .last_sysupgrade_partition
= "file-system"
1442 #define error(_ret, _errno, _str, ...) \
1444 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1445 strerror(_errno)); \
1451 /** Stores a uint32 as big endian */
1452 static inline void put32(uint8_t *buf
, uint32_t val
) {
1459 /** Allocates a new image partition */
1460 static struct image_partition_entry
alloc_image_partition(const char *name
, size_t len
) {
1461 struct image_partition_entry entry
= {name
, len
, malloc(len
)};
1463 error(1, errno
, "malloc");
1468 /** Frees an image partition */
1469 static void free_image_partition(struct image_partition_entry entry
) {
1473 static time_t source_date_epoch
= -1;
1474 static void set_source_date_epoch() {
1475 char *env
= getenv("SOURCE_DATE_EPOCH");
1479 source_date_epoch
= strtoull(env
, &endptr
, 10);
1480 if (errno
|| (endptr
&& *endptr
!= '\0')) {
1481 fprintf(stderr
, "Invalid SOURCE_DATE_EPOCH");
1487 /** Generates the partition-table partition */
1488 static struct image_partition_entry
make_partition_table(const struct flash_partition_entry
*p
) {
1489 struct image_partition_entry entry
= alloc_image_partition("partition-table", 0x800);
1491 char *s
= (char *)entry
.data
, *end
= (char *)(s
+entry
.size
);
1499 for (i
= 0; p
[i
].name
; i
++) {
1501 size_t w
= snprintf(s
, len
, "partition %s base 0x%05x size 0x%05x\n", p
[i
].name
, p
[i
].base
, p
[i
].size
);
1504 error(1, 0, "flash partition table overflow?");
1511 memset(s
, 0xff, end
-s
);
1517 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1518 static inline uint8_t bcd(uint8_t v
) {
1519 return 0x10 * (v
/10) + v
%10;
1523 /** Generates the soft-version partition */
1524 static struct image_partition_entry
make_soft_version(uint32_t rev
) {
1525 struct image_partition_entry entry
= alloc_image_partition("soft-version", sizeof(struct soft_version
));
1526 struct soft_version
*s
= (struct soft_version
*)entry
.data
;
1530 if (source_date_epoch
!= -1)
1531 t
= source_date_epoch
;
1532 else if (time(&t
) == (time_t)(-1))
1533 error(1, errno
, "time");
1535 struct tm
*tm
= localtime(&t
);
1537 s
->magic
= htonl(0x0000000c);
1541 s
->version_major
= 0;
1542 s
->version_minor
= 0;
1543 s
->version_patch
= 0;
1545 s
->year_hi
= bcd((1900+tm
->tm_year
)/100);
1546 s
->year_lo
= bcd(tm
->tm_year
%100);
1547 s
->month
= bcd(tm
->tm_mon
+1);
1548 s
->day
= bcd(tm
->tm_mday
);
1549 s
->rev
= htonl(rev
);
1556 static struct image_partition_entry
make_soft_version_from_string(const char *soft_ver
) {
1557 /** String length _including_ the terminating zero byte */
1558 uint32_t ver_len
= strlen(soft_ver
) + 1;
1559 /** Partition contains 64 bit header, the version string, and one additional null byte */
1560 size_t partition_len
= 2*sizeof(uint32_t) + ver_len
+ 1;
1561 struct image_partition_entry entry
= alloc_image_partition("soft-version", partition_len
);
1563 uint32_t *len
= (uint32_t *)entry
.data
;
1564 len
[0] = htonl(ver_len
);
1566 memcpy(&len
[2], soft_ver
, ver_len
);
1568 entry
.data
[partition_len
- 1] = 0;
1573 /** Generates the support-list partition */
1574 static struct image_partition_entry
make_support_list(struct device_info
*info
) {
1575 size_t len
= strlen(info
->support_list
);
1576 struct image_partition_entry entry
= alloc_image_partition("support-list", len
+ 9);
1578 put32(entry
.data
, len
);
1579 memset(entry
.data
+4, 0, 4);
1580 memcpy(entry
.data
+8, info
->support_list
, len
);
1581 entry
.data
[len
+8] = info
->support_trail
;
1586 /** Creates a new image partition with an arbitrary name from a file */
1587 static struct image_partition_entry
read_file(const char *part_name
, const char *filename
, bool add_jffs2_eof
, struct flash_partition_entry
*file_system_partition
) {
1588 struct stat statbuf
;
1590 if (stat(filename
, &statbuf
) < 0)
1591 error(1, errno
, "unable to stat file `%s'", filename
);
1593 size_t len
= statbuf
.st_size
;
1595 if (add_jffs2_eof
) {
1596 if (file_system_partition
)
1597 len
= ALIGN(len
+ file_system_partition
->base
, 0x10000) + sizeof(jffs2_eof_mark
) - file_system_partition
->base
;
1599 len
= ALIGN(len
, 0x10000) + sizeof(jffs2_eof_mark
);
1602 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1604 FILE *file
= fopen(filename
, "rb");
1606 error(1, errno
, "unable to open file `%s'", filename
);
1608 if (fread(entry
.data
, statbuf
.st_size
, 1, file
) != 1)
1609 error(1, errno
, "unable to read file `%s'", filename
);
1611 if (add_jffs2_eof
) {
1612 uint8_t *eof
= entry
.data
+ statbuf
.st_size
, *end
= entry
.data
+entry
.size
;
1614 memset(eof
, 0xff, end
- eof
- sizeof(jffs2_eof_mark
));
1615 memcpy(end
- sizeof(jffs2_eof_mark
), jffs2_eof_mark
, sizeof(jffs2_eof_mark
));
1623 /** Creates a new image partition from arbitrary data */
1624 static struct image_partition_entry
put_data(const char *part_name
, const char *datain
, size_t len
) {
1626 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1628 memcpy(entry
.data
, datain
, len
);
1634 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1636 Example image partition table:
1638 fwup-ptn partition-table base 0x00800 size 0x00800
1639 fwup-ptn os-image base 0x01000 size 0x113b45
1640 fwup-ptn file-system base 0x114b45 size 0x1d0004
1641 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1643 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1644 the end of the partition table is marked with a zero byte.
1646 The firmware image must contain at least the partition-table and support-list partitions
1647 to be accepted. There aren't any alignment constraints for the image partitions.
1649 The partition-table partition contains the actual flash layout; partitions
1650 from the image partition table are mapped to the corresponding flash partitions during
1651 the firmware upgrade. The support-list partition contains a list of devices supported by
1654 The base offsets in the firmware partition table are relative to the end
1655 of the vendor information block, so the partition-table partition will
1656 actually start at offset 0x1814 of the image.
1658 I think partition-table must be the first partition in the firmware image.
1660 static void put_partitions(uint8_t *buffer
, const struct flash_partition_entry
*flash_parts
, const struct image_partition_entry
*parts
) {
1662 char *image_pt
= (char *)buffer
, *end
= image_pt
+ 0x800;
1664 size_t base
= 0x800;
1665 for (i
= 0; parts
[i
].name
; i
++) {
1666 for (j
= 0; flash_parts
[j
].name
; j
++) {
1667 if (!strcmp(flash_parts
[j
].name
, parts
[i
].name
)) {
1668 if (parts
[i
].size
> flash_parts
[j
].size
)
1669 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts
[j
].name
, (unsigned)flash_parts
[j
].size
);
1674 assert(flash_parts
[j
].name
);
1676 memcpy(buffer
+ base
, parts
[i
].data
, parts
[i
].size
);
1678 size_t len
= end
-image_pt
;
1679 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
);
1682 error(1, 0, "image partition table overflow?");
1686 base
+= parts
[i
].size
;
1690 /** Generates and writes the image MD5 checksum */
1691 static void put_md5(uint8_t *md5
, uint8_t *buffer
, unsigned int len
) {
1695 MD5_Update(&ctx
, md5_salt
, (unsigned int)sizeof(md5_salt
));
1696 MD5_Update(&ctx
, buffer
, len
);
1697 MD5_Final(md5
, &ctx
);
1702 Generates the firmware image in factory format
1708 0000-0003 Image size (4 bytes, big endian)
1709 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1710 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1711 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1712 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1713 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1714 1814-xxxx Firmware partitions
1716 static void * generate_factory_image(struct device_info
*info
, const struct image_partition_entry
*parts
, size_t *len
) {
1720 for (i
= 0; parts
[i
].name
; i
++)
1721 *len
+= parts
[i
].size
;
1723 uint8_t *image
= malloc(*len
);
1725 error(1, errno
, "malloc");
1727 memset(image
, 0xff, *len
);
1731 size_t vendor_len
= strlen(info
->vendor
);
1732 put32(image
+0x14, vendor_len
);
1733 memcpy(image
+0x18, info
->vendor
, vendor_len
);
1736 put_partitions(image
+ 0x1014, info
->partitions
, parts
);
1737 put_md5(image
+0x04, image
+0x14, *len
-0x14);
1743 Generates the firmware image in sysupgrade format
1745 This makes some assumptions about the provided flash and image partition tables and
1746 should be generalized when TP-LINK starts building its safeloader into hardware with
1747 different flash layouts.
1749 static void * generate_sysupgrade_image(struct device_info
*info
, const struct image_partition_entry
*image_parts
, size_t *len
) {
1751 size_t flash_first_partition_index
= 0;
1752 size_t flash_last_partition_index
= 0;
1753 const struct flash_partition_entry
*flash_first_partition
= NULL
;
1754 const struct flash_partition_entry
*flash_last_partition
= NULL
;
1755 const struct image_partition_entry
*image_last_partition
= NULL
;
1757 /** Find first and last partitions */
1758 for (i
= 0; info
->partitions
[i
].name
; i
++) {
1759 if (!strcmp(info
->partitions
[i
].name
, info
->first_sysupgrade_partition
)) {
1760 flash_first_partition
= &info
->partitions
[i
];
1761 flash_first_partition_index
= i
;
1762 } else if (!strcmp(info
->partitions
[i
].name
, info
->last_sysupgrade_partition
)) {
1763 flash_last_partition
= &info
->partitions
[i
];
1764 flash_last_partition_index
= i
;
1768 assert(flash_first_partition
&& flash_last_partition
);
1769 assert(flash_first_partition_index
< flash_last_partition_index
);
1771 /** Find last partition from image to calculate needed size */
1772 for (i
= 0; image_parts
[i
].name
; i
++) {
1773 if (!strcmp(image_parts
[i
].name
, info
->last_sysupgrade_partition
)) {
1774 image_last_partition
= &image_parts
[i
];
1779 assert(image_last_partition
);
1781 *len
= flash_last_partition
->base
- flash_first_partition
->base
+ image_last_partition
->size
;
1783 uint8_t *image
= malloc(*len
);
1785 error(1, errno
, "malloc");
1787 memset(image
, 0xff, *len
);
1789 for (i
= flash_first_partition_index
; i
<= flash_last_partition_index
; i
++) {
1790 for (j
= 0; image_parts
[j
].name
; j
++) {
1791 if (!strcmp(info
->partitions
[i
].name
, image_parts
[j
].name
)) {
1792 if (image_parts
[j
].size
> info
->partitions
[i
].size
)
1793 error(1, 0, "%s partition too big (more than %u bytes)", info
->partitions
[i
].name
, (unsigned)info
->partitions
[i
].size
);
1794 memcpy(image
+ info
->partitions
[i
].base
- flash_first_partition
->base
, image_parts
[j
].data
, image_parts
[j
].size
);
1798 assert(image_parts
[j
].name
);
1805 /** Generates an image according to a given layout and writes it to a file */
1806 static void build_image(const char *output
,
1807 const char *kernel_image
,
1808 const char *rootfs_image
,
1812 struct device_info
*info
) {
1816 struct image_partition_entry parts
[7] = {};
1818 struct flash_partition_entry
*firmware_partition
= NULL
;
1819 struct flash_partition_entry
*os_image_partition
= NULL
;
1820 struct flash_partition_entry
*file_system_partition
= NULL
;
1821 size_t firmware_partition_index
= 0;
1823 for (i
= 0; info
->partitions
[i
].name
; i
++) {
1824 if (!strcmp(info
->partitions
[i
].name
, "firmware"))
1826 firmware_partition
= &info
->partitions
[i
];
1827 firmware_partition_index
= i
;
1831 if (firmware_partition
)
1833 os_image_partition
= &info
->partitions
[firmware_partition_index
];
1834 file_system_partition
= &info
->partitions
[firmware_partition_index
+ 1];
1837 if (stat(kernel_image
, &kernel
) < 0)
1838 error(1, errno
, "unable to stat file `%s'", kernel_image
);
1840 if (kernel
.st_size
> firmware_partition
->size
)
1841 error(1, 0, "kernel overflowed firmware partition\n");
1843 for (i
= MAX_PARTITIONS
-1; i
>= firmware_partition_index
+ 1; i
--)
1844 info
->partitions
[i
+1] = info
->partitions
[i
];
1846 file_system_partition
->name
= "file-system";
1847 file_system_partition
->base
= firmware_partition
->base
+ kernel
.st_size
;
1849 /* Align partition start to erase blocks for factory images only */
1851 file_system_partition
->base
= ALIGN(firmware_partition
->base
+ kernel
.st_size
, 0x10000);
1853 file_system_partition
->size
= firmware_partition
->size
- file_system_partition
->base
;
1855 os_image_partition
->name
= "os-image";
1856 os_image_partition
->size
= kernel
.st_size
;
1859 parts
[0] = make_partition_table(info
->partitions
);
1861 parts
[1] = make_soft_version_from_string(info
->soft_ver
);
1863 parts
[1] = make_soft_version(rev
);
1865 parts
[2] = make_support_list(info
);
1866 parts
[3] = read_file("os-image", kernel_image
, false, NULL
);
1867 parts
[4] = read_file("file-system", rootfs_image
, add_jffs2_eof
, file_system_partition
);
1869 /* Some devices need the extra-para partition to accept the firmware */
1870 if (strcasecmp(info
->id
, "ARCHER-C2-V3") == 0 ||
1871 strcasecmp(info
->id
, "ARCHER-C25-V1") == 0 ||
1872 strcasecmp(info
->id
, "ARCHER-C59-V2") == 0 ||
1873 strcasecmp(info
->id
, "ARCHER-C60-V2") == 0 ||
1874 strcasecmp(info
->id
, "ARCHER-C60-V3") == 0 ||
1875 strcasecmp(info
->id
, "TLWR1043NV5") == 0) {
1876 const char mdat
[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1877 parts
[5] = put_data("extra-para", mdat
, 11);
1878 } else if (strcasecmp(info
->id
, "ARCHER-A7-V5") == 0 || strcasecmp(info
->id
, "ARCHER-C7-V4") == 0 || strcasecmp(info
->id
, "ARCHER-C7-V5") == 0) {
1879 const char mdat
[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1880 parts
[5] = put_data("extra-para", mdat
, 11);
1881 } else if (strcasecmp(info
->id
, "ARCHER-C6-V2") == 0) {
1882 const char mdat
[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00};
1883 parts
[5] = put_data("extra-para", mdat
, 11);
1889 image
= generate_sysupgrade_image(info
, parts
, &len
);
1891 image
= generate_factory_image(info
, parts
, &len
);
1893 FILE *file
= fopen(output
, "wb");
1895 error(1, errno
, "unable to open output file");
1897 if (fwrite(image
, len
, 1, file
) != 1)
1898 error(1, 0, "unable to write output file");
1904 for (i
= 0; parts
[i
].name
; i
++)
1905 free_image_partition(parts
[i
]);
1909 static void usage(const char *argv0
) {
1911 "Usage: %s [OPTIONS...]\n"
1914 " -h show this help\n"
1916 "Create a new image:\n"
1917 " -B <board> create image for the board specified with <board>\n"
1918 " -k <file> read kernel image from the file <file>\n"
1919 " -r <file> read rootfs image from the file <file>\n"
1920 " -o <file> write output to the file <file>\n"
1921 " -V <rev> sets the revision number to <rev>\n"
1922 " -j add jffs2 end-of-filesystem markers\n"
1923 " -S create sysupgrade instead of factory image\n"
1924 "Extract an old image:\n"
1925 " -x <file> extract all oem firmware partition\n"
1926 " -d <dir> destination to extract the firmware partition\n"
1927 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1933 static struct device_info
*find_board(const char *id
)
1935 struct device_info
*board
= NULL
;
1937 for (board
= boards
; board
->id
!= NULL
; board
++)
1938 if (strcasecmp(id
, board
->id
) == 0)
1944 static int add_flash_partition(
1945 struct flash_partition_entry
*part_list
,
1952 /* check if the list has a free entry */
1953 for (ptr
= 0; ptr
< max_entries
; ptr
++, part_list
++) {
1954 if (part_list
->name
== NULL
&&
1955 part_list
->base
== 0 &&
1956 part_list
->size
== 0)
1960 if (ptr
== max_entries
) {
1961 error(1, 0, "No free flash part entry available.");
1964 part_list
->name
= calloc(1, strlen(name
) + 1);
1965 if (!part_list
->name
) {
1966 error(1, 0, "Unable to allocate memory");
1969 memcpy((char *)part_list
->name
, name
, strlen(name
));
1970 part_list
->base
= base
;
1971 part_list
->size
= size
;
1976 /** read the partition table into struct flash_partition_entry */
1977 static int read_partition_table(
1978 FILE *file
, long offset
,
1979 struct flash_partition_entry
*entries
, size_t max_entries
,
1984 const char *parthdr
= NULL
;
1985 const char *fwuphdr
= "fwup-ptn";
1986 const char *flashhdr
= "partition";
1988 /* TODO: search for the partition table */
1998 error(1, 0, "Invalid partition table");
2001 if (fseek(file
, offset
, SEEK_SET
) < 0)
2002 error(1, errno
, "Can not seek in the firmware");
2004 if (fread(buf
, 2048, 1, file
) != 1)
2005 error(1, errno
, "Can not read fwup-ptn from the firmware");
2009 /* look for the partition header */
2010 if (memcmp(buf
, parthdr
, strlen(parthdr
)) != 0) {
2011 fprintf(stderr
, "DEBUG: can not find fwuphdr\n");
2016 end
= buf
+ sizeof(buf
);
2017 while ((ptr
+ strlen(parthdr
)) < end
&&
2018 memcmp(ptr
, parthdr
, strlen(parthdr
)) == 0) {
2022 char name
[32] = { 0 };
2024 unsigned long base
= 0;
2025 unsigned long size
= 0;
2027 end_part
= memchr(ptr
, '\n', (end
- ptr
));
2028 if (end_part
== NULL
) {
2029 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
2033 for (int i
= 0; i
<= 4; i
++) {
2034 if (end_part
<= ptr
)
2037 end_element
= memchr(ptr
, 0x20, (end_part
- ptr
));
2038 if (end_element
== NULL
) {
2039 error(1, errno
, "Ignoring the rest of the partition entries.");
2044 /* partition header */
2046 ptr
= end_element
+ 1;
2050 name_len
= (end_element
- ptr
) > 31 ? 31 : (end_element
- ptr
);
2051 strncpy(name
, ptr
, name_len
);
2052 name
[name_len
] = '\0';
2053 ptr
= end_element
+ 1;
2058 ptr
= end_element
+ 1;
2063 base
= strtoul(ptr
, NULL
, 16);
2064 ptr
= end_element
+ 1;
2069 ptr
= end_element
+ 1;
2070 /* actual size. The last element doesn't have a sepeartor */
2071 size
= strtoul(ptr
, NULL
, 16);
2072 /* the part ends with 0x09, 0x0d, 0x0a */
2074 add_flash_partition(entries
, max_entries
, name
, base
, size
);
2083 static void write_partition(
2085 size_t firmware_offset
,
2086 struct flash_partition_entry
*entry
,
2092 fseek(input_file
, entry
->base
+ firmware_offset
, SEEK_SET
);
2094 for (offset
= 0; sizeof(buf
) + offset
<= entry
->size
; offset
+= sizeof(buf
)) {
2095 if (fread(buf
, sizeof(buf
), 1, input_file
) != 1)
2096 error(1, errno
, "Can not read partition from input_file");
2098 if (fwrite(buf
, sizeof(buf
), 1, output_file
) != 1)
2099 error(1, errno
, "Can not write partition to output_file");
2101 /* write last chunk smaller than buffer */
2102 if (offset
< entry
->size
) {
2103 offset
= entry
->size
- offset
;
2104 if (fread(buf
, offset
, 1, input_file
) != 1)
2105 error(1, errno
, "Can not read partition from input_file");
2106 if (fwrite(buf
, offset
, 1, output_file
) != 1)
2107 error(1, errno
, "Can not write partition to output_file");
2111 static int extract_firmware_partition(FILE *input_file
, size_t firmware_offset
, struct flash_partition_entry
*entry
, const char *output_directory
)
2114 char output
[PATH_MAX
];
2116 snprintf(output
, PATH_MAX
, "%s/%s", output_directory
, entry
->name
);
2117 output_file
= fopen(output
, "wb+");
2118 if (output_file
== NULL
) {
2119 error(1, errno
, "Can not open output file %s", output
);
2122 write_partition(input_file
, firmware_offset
, entry
, output_file
);
2124 fclose(output_file
);
2129 /** extract all partitions from the firmware file */
2130 static int extract_firmware(const char *input
, const char *output_directory
)
2132 struct flash_partition_entry entries
[16] = { 0 };
2133 size_t max_entries
= 16;
2134 size_t firmware_offset
= 0x1014;
2137 struct stat statbuf
;
2139 /* check input file */
2140 if (stat(input
, &statbuf
)) {
2141 error(1, errno
, "Can not read input firmware %s", input
);
2144 /* check if output directory exists */
2145 if (stat(output_directory
, &statbuf
)) {
2146 error(1, errno
, "Failed to stat output directory %s", output_directory
);
2149 if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
2150 error(1, errno
, "Given output directory is not a directory %s", output_directory
);
2153 input_file
= fopen(input
, "rb");
2155 if (read_partition_table(input_file
, firmware_offset
, entries
, 16, 0) != 0) {
2156 error(1, 0, "Error can not read the partition table (fwup-ptn)");
2159 for (size_t i
= 0; i
< max_entries
; i
++) {
2160 if (entries
[i
].name
== NULL
&&
2161 entries
[i
].base
== 0 &&
2162 entries
[i
].size
== 0)
2165 extract_firmware_partition(input_file
, firmware_offset
, &entries
[i
], output_directory
);
2171 static struct flash_partition_entry
*find_partition(
2172 struct flash_partition_entry
*entries
, size_t max_entries
,
2173 const char *name
, const char *error_msg
)
2175 for (size_t i
= 0; i
< max_entries
; i
++, entries
++) {
2176 if (strcmp(entries
->name
, name
) == 0)
2180 error(1, 0, "%s", error_msg
);
2184 static void write_ff(FILE *output_file
, size_t size
)
2189 memset(buf
, 0xff, sizeof(buf
));
2191 for (offset
= 0; offset
+ sizeof(buf
) < size
; offset
+= sizeof(buf
)) {
2192 if (fwrite(buf
, sizeof(buf
), 1, output_file
) != 1)
2193 error(1, errno
, "Can not write 0xff to output_file");
2196 /* write last chunk smaller than buffer */
2197 if (offset
< size
) {
2198 offset
= size
- offset
;
2199 if (fwrite(buf
, offset
, 1, output_file
) != 1)
2200 error(1, errno
, "Can not write partition to output_file");
2204 static void convert_firmware(const char *input
, const char *output
)
2206 struct flash_partition_entry fwup
[MAX_PARTITIONS
] = { 0 };
2207 struct flash_partition_entry flash
[MAX_PARTITIONS
] = { 0 };
2208 struct flash_partition_entry
*fwup_os_image
= NULL
, *fwup_file_system
= NULL
;
2209 struct flash_partition_entry
*flash_os_image
= NULL
, *flash_file_system
= NULL
;
2210 struct flash_partition_entry
*fwup_partition_table
= NULL
;
2211 size_t firmware_offset
= 0x1014;
2212 FILE *input_file
, *output_file
;
2214 struct stat statbuf
;
2216 /* check input file */
2217 if (stat(input
, &statbuf
)) {
2218 error(1, errno
, "Can not read input firmware %s", input
);
2221 input_file
= fopen(input
, "rb");
2223 error(1, 0, "Can not open input firmware %s", input
);
2225 output_file
= fopen(output
, "wb");
2227 error(1, 0, "Can not open output firmware %s", output
);
2229 if (read_partition_table(input_file
, firmware_offset
, fwup
, MAX_PARTITIONS
, 0) != 0) {
2230 error(1, 0, "Error can not read the partition table (fwup-ptn)");
2233 fwup_os_image
= find_partition(fwup
, MAX_PARTITIONS
,
2234 "os-image", "Error can not find os-image partition (fwup)");
2235 fwup_file_system
= find_partition(fwup
, MAX_PARTITIONS
,
2236 "file-system", "Error can not find file-system partition (fwup)");
2237 fwup_partition_table
= find_partition(fwup
, MAX_PARTITIONS
,
2238 "partition-table", "Error can not find partition-table partition");
2240 /* the flash partition table has a 0x00000004 magic haeder */
2241 if (read_partition_table(input_file
, firmware_offset
+ fwup_partition_table
->base
+ 4, flash
, MAX_PARTITIONS
, 1) != 0)
2242 error(1, 0, "Error can not read the partition table (flash)");
2244 flash_os_image
= find_partition(flash
, MAX_PARTITIONS
,
2245 "os-image", "Error can not find os-image partition (flash)");
2246 flash_file_system
= find_partition(flash
, MAX_PARTITIONS
,
2247 "file-system", "Error can not find file-system partition (flash)");
2249 /* write os_image to 0x0 */
2250 write_partition(input_file
, firmware_offset
, fwup_os_image
, output_file
);
2251 write_ff(output_file
, flash_os_image
->size
- fwup_os_image
->size
);
2253 /* write file-system behind os_image */
2254 fseek(output_file
, flash_file_system
->base
- flash_os_image
->base
, SEEK_SET
);
2255 write_partition(input_file
, firmware_offset
, fwup_file_system
, output_file
);
2256 write_ff(output_file
, flash_file_system
->size
- fwup_file_system
->size
);
2258 fclose(output_file
);
2262 int main(int argc
, char *argv
[]) {
2263 const char *board
= NULL
, *kernel_image
= NULL
, *rootfs_image
= NULL
, *output
= NULL
;
2264 const char *extract_image
= NULL
, *output_directory
= NULL
, *convert_image
= NULL
;
2265 bool add_jffs2_eof
= false, sysupgrade
= false;
2267 struct device_info
*info
;
2268 set_source_date_epoch();
2273 c
= getopt(argc
, argv
, "B:k:r:o:V:jSh:x:d:z:");
2283 kernel_image
= optarg
;
2287 rootfs_image
= optarg
;
2295 sscanf(optarg
, "r%u", &rev
);
2299 add_jffs2_eof
= true;
2311 output_directory
= optarg
;
2315 extract_image
= optarg
;
2319 convert_image
= optarg
;
2328 if (extract_image
|| output_directory
) {
2330 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
2331 if (!output_directory
)
2332 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
2333 extract_firmware(extract_image
, output_directory
);
2334 } else if (convert_image
) {
2336 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
2337 convert_firmware(convert_image
, output
);
2340 error(1, 0, "no board has been specified");
2342 error(1, 0, "no kernel image has been specified");
2344 error(1, 0, "no rootfs image has been specified");
2346 error(1, 0, "no output filename has been specified");
2348 info
= find_board(board
);
2351 error(1, 0, "unsupported board %s", board
);
2353 build_image(output
, kernel_image
, rootfs_image
, rev
, add_jffs2_eof
, sysupgrade
, info
);