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.0.0\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 C5 */
804 .id
= "ARCHER-C5-V2",
808 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
809 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
810 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
811 .support_trail
= '\x00',
815 {"fs-uboot", 0x00000, 0x40000},
816 {"os-image", 0x40000, 0x200000},
817 {"file-system", 0x240000, 0xc00000},
818 {"default-mac", 0xe40000, 0x00200},
819 {"pin", 0xe40200, 0x00200},
820 {"product-info", 0xe40400, 0x00200},
821 {"partition-table", 0xe50000, 0x10000},
822 {"soft-version", 0xe60000, 0x00200},
823 {"support-list", 0xe61000, 0x0f000},
824 {"profile", 0xe70000, 0x10000},
825 {"default-config", 0xe80000, 0x10000},
826 {"user-config", 0xe90000, 0x50000},
827 {"log", 0xee0000, 0x100000},
828 {"radio_bk", 0xfe0000, 0x10000},
829 {"radio", 0xff0000, 0x10000},
833 .first_sysupgrade_partition
= "os-image",
834 .last_sysupgrade_partition
= "file-system"
837 /** Firmware layout for the C7 */
839 .id
= "ARCHER-C7-V4",
842 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
843 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
844 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
845 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
846 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
847 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
848 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
849 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
850 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
851 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
852 .support_trail
= '\x00',
853 .soft_ver
= "soft_ver:1.0.0\n",
855 /* We're using a dynamic kernel/rootfs split here */
857 {"factory-boot", 0x00000, 0x20000},
858 {"fs-uboot", 0x20000, 0x20000},
859 {"firmware", 0x40000, 0xEC0000}, /* Stock: name os-image base 0x40000 size 0x120000 */
860 /* Stock: name file-system base 0x160000 size 0xda0000 */
861 {"default-mac", 0xf00000, 0x00200},
862 {"pin", 0xf00200, 0x00200},
863 {"device-id", 0xf00400, 0x00100},
864 {"product-info", 0xf00500, 0x0fb00},
865 {"soft-version", 0xf10000, 0x00100},
866 {"extra-para", 0xf11000, 0x01000},
867 {"support-list", 0xf12000, 0x0a000},
868 {"profile", 0xf1c000, 0x04000},
869 {"default-config", 0xf20000, 0x10000},
870 {"user-config", 0xf30000, 0x40000},
871 {"qos-db", 0xf70000, 0x40000},
872 {"certificate", 0xfb0000, 0x10000},
873 {"partition-table", 0xfc0000, 0x10000},
874 {"log", 0xfd0000, 0x20000},
875 {"radio", 0xff0000, 0x10000},
879 .first_sysupgrade_partition
= "os-image",
880 .last_sysupgrade_partition
= "file-system",
883 /** Firmware layout for the C7 v5*/
885 .id
= "ARCHER-C7-V5",
888 "{product_name:Archer C7,product_ver:5.0.0,special_id:00000000}\n"
889 "{product_name:Archer C7,product_ver:5.0.0,special_id:45550000}\n"
890 "{product_name:Archer C7,product_ver:5.0.0,special_id:55530000}\n"
891 "{product_name:Archer C7,product_ver:5.0.0,special_id:43410000}\n"
892 "{product_name:Archer C7,product_ver:5.0.0,special_id:4A500000}\n"
893 "{product_name:Archer C7,product_ver:5.0.0,special_id:54570000}\n"
894 "{product_name:Archer C7,product_ver:5.0.0,special_id:52550000}\n"
895 "{product_name:Archer C7,product_ver:5.0.0,special_id:4B520000}\n",
897 .support_trail
= '\x00',
898 .soft_ver
= "soft_ver:1.0.0\n",
900 /* We're using a dynamic kernel/rootfs split here */
902 {"factory-boot", 0x00000, 0x20000},
903 {"fs-uboot", 0x20000, 0x20000},
904 {"partition-table", 0x40000, 0x10000},
905 {"radio", 0x50000, 0x10000},
906 {"default-mac", 0x60000, 0x00200},
907 {"pin", 0x60200, 0x00200},
908 {"device-id", 0x60400, 0x00100},
909 {"product-info", 0x60500, 0x0fb00},
910 {"soft-version", 0x70000, 0x01000},
911 {"extra-para", 0x71000, 0x01000},
912 {"support-list", 0x72000, 0x0a000},
913 {"profile", 0x7c000, 0x04000},
914 {"user-config", 0x80000, 0x40000},
917 {"firmware", 0xc0000, 0xf00000}, /* Stock: name os-image base 0xc0000 size 0x120000 */
918 /* Stock: name file-system base 0x1e0000 size 0xde0000 */
920 {"log", 0xfc0000, 0x20000},
921 {"certificate", 0xfe0000, 0x10000},
922 {"default-config", 0xff0000, 0x10000},
927 .first_sysupgrade_partition
= "os-image",
928 .last_sysupgrade_partition
= "file-system",
931 /** Firmware layout for the C9 */
937 "{product_name:ArcherC9,"
939 "special_id:00000000}\n",
940 .support_trail
= '\x00',
944 {"fs-uboot", 0x00000, 0x40000},
945 {"os-image", 0x40000, 0x200000},
946 {"file-system", 0x240000, 0xc00000},
947 {"default-mac", 0xe40000, 0x00200},
948 {"pin", 0xe40200, 0x00200},
949 {"product-info", 0xe40400, 0x00200},
950 {"partition-table", 0xe50000, 0x10000},
951 {"soft-version", 0xe60000, 0x00200},
952 {"support-list", 0xe61000, 0x0f000},
953 {"profile", 0xe70000, 0x10000},
954 {"default-config", 0xe80000, 0x10000},
955 {"user-config", 0xe90000, 0x50000},
956 {"log", 0xee0000, 0x100000},
957 {"radio_bk", 0xfe0000, 0x10000},
958 {"radio", 0xff0000, 0x10000},
962 .first_sysupgrade_partition
= "os-image",
963 .last_sysupgrade_partition
= "file-system"
966 /** Firmware layout for the EAP120 */
969 .vendor
= "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
972 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
973 .support_trail
= '\xff',
977 {"fs-uboot", 0x00000, 0x20000},
978 {"partition-table", 0x20000, 0x02000},
979 {"default-mac", 0x30000, 0x00020},
980 {"support-list", 0x31000, 0x00100},
981 {"product-info", 0x31100, 0x00100},
982 {"soft-version", 0x32000, 0x00100},
983 {"os-image", 0x40000, 0x180000},
984 {"file-system", 0x1c0000, 0x600000},
985 {"user-config", 0x7c0000, 0x10000},
986 {"backup-config", 0x7d0000, 0x10000},
987 {"log", 0x7e0000, 0x10000},
988 {"radio", 0x7f0000, 0x10000},
992 .first_sysupgrade_partition
= "os-image",
993 .last_sysupgrade_partition
= "file-system"
996 /** Firmware layout for the TL-WA850RE v2 */
1002 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
1003 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
1004 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
1005 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
1006 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
1007 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
1008 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
1009 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
1010 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
1011 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
1012 .support_trail
= '\x00',
1016 576KB were moved from file-system to os-image
1017 in comparison to the stock image
1020 {"fs-uboot", 0x00000, 0x20000},
1021 {"os-image", 0x20000, 0x150000},
1022 {"file-system", 0x170000, 0x240000},
1023 {"partition-table", 0x3b0000, 0x02000},
1024 {"default-mac", 0x3c0000, 0x00020},
1025 {"pin", 0x3c0100, 0x00020},
1026 {"product-info", 0x3c1000, 0x01000},
1027 {"soft-version", 0x3c2000, 0x00100},
1028 {"support-list", 0x3c3000, 0x01000},
1029 {"profile", 0x3c4000, 0x08000},
1030 {"user-config", 0x3d0000, 0x10000},
1031 {"default-config", 0x3e0000, 0x10000},
1032 {"radio", 0x3f0000, 0x10000},
1036 .first_sysupgrade_partition
= "os-image",
1037 .last_sysupgrade_partition
= "file-system"
1040 /** Firmware layout for the TL-WA855RE v1 */
1042 .id
= "TLWA855REV1",
1046 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
1047 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
1048 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
1049 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
1050 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
1051 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
1052 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
1053 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
1054 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
1055 .support_trail
= '\x00',
1059 {"fs-uboot", 0x00000, 0x20000},
1060 {"os-image", 0x20000, 0x150000},
1061 {"file-system", 0x170000, 0x240000},
1062 {"partition-table", 0x3b0000, 0x02000},
1063 {"default-mac", 0x3c0000, 0x00020},
1064 {"pin", 0x3c0100, 0x00020},
1065 {"product-info", 0x3c1000, 0x01000},
1066 {"soft-version", 0x3c2000, 0x00100},
1067 {"support-list", 0x3c3000, 0x01000},
1068 {"profile", 0x3c4000, 0x08000},
1069 {"user-config", 0x3d0000, 0x10000},
1070 {"default-config", 0x3e0000, 0x10000},
1071 {"radio", 0x3f0000, 0x10000},
1075 .first_sysupgrade_partition
= "os-image",
1076 .last_sysupgrade_partition
= "file-system"
1079 /** Firmware layout for the TL-WR1043 v5 */
1081 .id
= "TLWR1043NV5",
1085 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
1086 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
1087 .support_trail
= '\x00',
1088 .soft_ver
= "soft_ver:1.0.0\n",
1090 {"factory-boot", 0x00000, 0x20000},
1091 {"fs-uboot", 0x20000, 0x20000},
1092 {"firmware", 0x40000, 0xec0000},
1093 {"default-mac", 0xf00000, 0x00200},
1094 {"pin", 0xf00200, 0x00200},
1095 {"device-id", 0xf00400, 0x00100},
1096 {"product-info", 0xf00500, 0x0fb00},
1097 {"soft-version", 0xf10000, 0x01000},
1098 {"extra-para", 0xf11000, 0x01000},
1099 {"support-list", 0xf12000, 0x0a000},
1100 {"profile", 0xf1c000, 0x04000},
1101 {"default-config", 0xf20000, 0x10000},
1102 {"user-config", 0xf30000, 0x40000},
1103 {"qos-db", 0xf70000, 0x40000},
1104 {"certificate", 0xfb0000, 0x10000},
1105 {"partition-table", 0xfc0000, 0x10000},
1106 {"log", 0xfd0000, 0x20000},
1107 {"radio", 0xff0000, 0x10000},
1110 .first_sysupgrade_partition
= "os-image",
1111 .last_sysupgrade_partition
= "file-system"
1114 /** Firmware layout for the TL-WR1043 v4 */
1116 .id
= "TLWR1043NDV4",
1120 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
1121 .support_trail
= '\x00',
1124 /* We're using a dynamic kernel/rootfs split here */
1126 {"fs-uboot", 0x00000, 0x20000},
1127 {"firmware", 0x20000, 0xf30000},
1128 {"default-mac", 0xf50000, 0x00200},
1129 {"pin", 0xf50200, 0x00200},
1130 {"product-info", 0xf50400, 0x0fc00},
1131 {"soft-version", 0xf60000, 0x0b000},
1132 {"support-list", 0xf6b000, 0x04000},
1133 {"profile", 0xf70000, 0x04000},
1134 {"default-config", 0xf74000, 0x0b000},
1135 {"user-config", 0xf80000, 0x40000},
1136 {"partition-table", 0xfc0000, 0x10000},
1137 {"log", 0xfd0000, 0x20000},
1138 {"radio", 0xff0000, 0x10000},
1142 .first_sysupgrade_partition
= "os-image",
1143 .last_sysupgrade_partition
= "file-system"
1146 /** Firmware layout for the TL-WR902AC v1 */
1148 .id
= "TL-WR902AC-V1",
1152 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
1153 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
1154 .support_trail
= '\x00',
1158 384KB were moved from file-system to os-image
1159 in comparison to the stock image
1162 {"fs-uboot", 0x00000, 0x20000},
1163 {"firmware", 0x20000, 0x730000},
1164 {"default-mac", 0x750000, 0x00200},
1165 {"pin", 0x750200, 0x00200},
1166 {"product-info", 0x750400, 0x0fc00},
1167 {"soft-version", 0x760000, 0x0b000},
1168 {"support-list", 0x76b000, 0x04000},
1169 {"profile", 0x770000, 0x04000},
1170 {"default-config", 0x774000, 0x0b000},
1171 {"user-config", 0x780000, 0x40000},
1172 {"partition-table", 0x7c0000, 0x10000},
1173 {"log", 0x7d0000, 0x20000},
1174 {"radio", 0x7f0000, 0x10000},
1178 .first_sysupgrade_partition
= "os-image",
1179 .last_sysupgrade_partition
= "file-system",
1182 /** Firmware layout for the TL-WR942N V1 */
1188 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
1189 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
1190 .support_trail
= '\x00',
1194 {"fs-uboot", 0x00000, 0x20000},
1195 {"firmware", 0x20000, 0xe20000},
1196 {"default-mac", 0xe40000, 0x00200},
1197 {"pin", 0xe40200, 0x00200},
1198 {"product-info", 0xe40400, 0x0fc00},
1199 {"partition-table", 0xe50000, 0x10000},
1200 {"soft-version", 0xe60000, 0x10000},
1201 {"support-list", 0xe70000, 0x10000},
1202 {"profile", 0xe80000, 0x10000},
1203 {"default-config", 0xe90000, 0x10000},
1204 {"user-config", 0xea0000, 0x40000},
1205 {"qos-db", 0xee0000, 0x40000},
1206 {"certificate", 0xf20000, 0x10000},
1207 {"usb-config", 0xfb0000, 0x10000},
1208 {"log", 0xfc0000, 0x20000},
1209 {"radio-bk", 0xfe0000, 0x10000},
1210 {"radio", 0xff0000, 0x10000},
1214 .first_sysupgrade_partition
= "os-image",
1215 .last_sysupgrade_partition
= "file-system",
1218 /** Firmware layout for the RE350 v1 */
1224 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
1225 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
1226 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
1227 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
1228 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
1229 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
1230 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
1231 .support_trail
= '\x00',
1234 /** We're using a dynamic kernel/rootfs split here */
1236 {"fs-uboot", 0x00000, 0x20000},
1237 {"firmware", 0x20000, 0x5e0000},
1238 {"partition-table", 0x600000, 0x02000},
1239 {"default-mac", 0x610000, 0x00020},
1240 {"pin", 0x610100, 0x00020},
1241 {"product-info", 0x611100, 0x01000},
1242 {"soft-version", 0x620000, 0x01000},
1243 {"support-list", 0x621000, 0x01000},
1244 {"profile", 0x622000, 0x08000},
1245 {"user-config", 0x630000, 0x10000},
1246 {"default-config", 0x640000, 0x10000},
1247 {"radio", 0x7f0000, 0x10000},
1251 .first_sysupgrade_partition
= "os-image",
1252 .last_sysupgrade_partition
= "file-system"
1255 /** Firmware layout for the RE350K v1 */
1261 "{product_name:RE350K,product_ver:1.0.0,special_id:00000000,product_region:US}\n",
1262 .support_trail
= '\x00',
1265 /** We're using a dynamic kernel/rootfs split here */
1267 {"fs-uboot", 0x00000, 0x20000},
1268 {"firmware", 0x20000, 0xd70000},
1269 {"partition-table", 0xd90000, 0x02000},
1270 {"default-mac", 0xda0000, 0x00020},
1271 {"pin", 0xda0100, 0x00020},
1272 {"product-info", 0xda1100, 0x01000},
1273 {"soft-version", 0xdb0000, 0x01000},
1274 {"support-list", 0xdb1000, 0x01000},
1275 {"profile", 0xdb2000, 0x08000},
1276 {"user-config", 0xdc0000, 0x10000},
1277 {"default-config", 0xdd0000, 0x10000},
1278 {"device-id", 0xde0000, 0x00108},
1279 {"radio", 0xff0000, 0x10000},
1283 .first_sysupgrade_partition
= "os-image",
1284 .last_sysupgrade_partition
= "file-system"
1287 /** Firmware layout for the RE355 */
1293 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
1294 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
1295 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
1296 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
1297 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
1298 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
1299 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
1300 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
1301 .support_trail
= '\x00',
1304 /* We're using a dynamic kernel/rootfs split here */
1306 {"fs-uboot", 0x00000, 0x20000},
1307 {"firmware", 0x20000, 0x5e0000},
1308 {"partition-table", 0x600000, 0x02000},
1309 {"default-mac", 0x610000, 0x00020},
1310 {"pin", 0x610100, 0x00020},
1311 {"product-info", 0x611100, 0x01000},
1312 {"soft-version", 0x620000, 0x01000},
1313 {"support-list", 0x621000, 0x01000},
1314 {"profile", 0x622000, 0x08000},
1315 {"user-config", 0x630000, 0x10000},
1316 {"default-config", 0x640000, 0x10000},
1317 {"radio", 0x7f0000, 0x10000},
1321 .first_sysupgrade_partition
= "os-image",
1322 .last_sysupgrade_partition
= "file-system"
1325 /** Firmware layout for the RE450 */
1331 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
1332 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
1333 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
1334 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
1335 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
1336 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
1337 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
1338 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
1339 .support_trail
= '\x00',
1342 /** We're using a dynamic kernel/rootfs split here */
1344 {"fs-uboot", 0x00000, 0x20000},
1345 {"firmware", 0x20000, 0x5e0000},
1346 {"partition-table", 0x600000, 0x02000},
1347 {"default-mac", 0x610000, 0x00020},
1348 {"pin", 0x610100, 0x00020},
1349 {"product-info", 0x611100, 0x01000},
1350 {"soft-version", 0x620000, 0x01000},
1351 {"support-list", 0x621000, 0x01000},
1352 {"profile", 0x622000, 0x08000},
1353 {"user-config", 0x630000, 0x10000},
1354 {"default-config", 0x640000, 0x10000},
1355 {"radio", 0x7f0000, 0x10000},
1359 .first_sysupgrade_partition
= "os-image",
1360 .last_sysupgrade_partition
= "file-system"
1363 /** Firmware layout for the RE450 v2 */
1369 "{product_name:RE450,product_ver:2.0.0,special_id:00000000}\r\n"
1370 "{product_name:RE450,product_ver:2.0.0,special_id:55530000}\r\n"
1371 "{product_name:RE450,product_ver:2.0.0,special_id:45550000}\r\n"
1372 "{product_name:RE450,product_ver:2.0.0,special_id:4A500000}\r\n"
1373 "{product_name:RE450,product_ver:2.0.0,special_id:43410000}\r\n"
1374 "{product_name:RE450,product_ver:2.0.0,special_id:41550000}\r\n"
1375 "{product_name:RE450,product_ver:2.0.0,special_id:41530000}\r\n"
1376 "{product_name:RE450,product_ver:2.0.0,special_id:4B520000}\r\n"
1377 "{product_name:RE450,product_ver:2.0.0,special_id:42520000}\r\n",
1378 .support_trail
= '\x00',
1381 /* We're using a dynamic kernel/rootfs split here */
1383 {"fs-uboot", 0x00000, 0x20000},
1384 {"firmware", 0x20000, 0x5e0000},
1385 {"partition-table", 0x600000, 0x02000},
1386 {"default-mac", 0x610000, 0x00020},
1387 {"pin", 0x610100, 0x00020},
1388 {"product-info", 0x611100, 0x01000},
1389 {"soft-version", 0x620000, 0x01000},
1390 {"support-list", 0x621000, 0x01000},
1391 {"profile", 0x622000, 0x08000},
1392 {"user-config", 0x630000, 0x10000},
1393 {"default-config", 0x640000, 0x10000},
1394 {"radio", 0x7f0000, 0x10000},
1399 .first_sysupgrade_partition
= "os-image",
1400 .last_sysupgrade_partition
= "file-system"
1406 #define error(_ret, _errno, _str, ...) \
1408 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1409 strerror(_errno)); \
1415 /** Stores a uint32 as big endian */
1416 static inline void put32(uint8_t *buf
, uint32_t val
) {
1423 /** Allocates a new image partition */
1424 static struct image_partition_entry
alloc_image_partition(const char *name
, size_t len
) {
1425 struct image_partition_entry entry
= {name
, len
, malloc(len
)};
1427 error(1, errno
, "malloc");
1432 /** Frees an image partition */
1433 static void free_image_partition(struct image_partition_entry entry
) {
1437 static time_t source_date_epoch
= -1;
1438 static void set_source_date_epoch() {
1439 char *env
= getenv("SOURCE_DATE_EPOCH");
1443 source_date_epoch
= strtoull(env
, &endptr
, 10);
1444 if (errno
|| (endptr
&& *endptr
!= '\0')) {
1445 fprintf(stderr
, "Invalid SOURCE_DATE_EPOCH");
1451 /** Generates the partition-table partition */
1452 static struct image_partition_entry
make_partition_table(const struct flash_partition_entry
*p
) {
1453 struct image_partition_entry entry
= alloc_image_partition("partition-table", 0x800);
1455 char *s
= (char *)entry
.data
, *end
= (char *)(s
+entry
.size
);
1463 for (i
= 0; p
[i
].name
; i
++) {
1465 size_t w
= snprintf(s
, len
, "partition %s base 0x%05x size 0x%05x\n", p
[i
].name
, p
[i
].base
, p
[i
].size
);
1468 error(1, 0, "flash partition table overflow?");
1475 memset(s
, 0xff, end
-s
);
1481 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1482 static inline uint8_t bcd(uint8_t v
) {
1483 return 0x10 * (v
/10) + v
%10;
1487 /** Generates the soft-version partition */
1488 static struct image_partition_entry
make_soft_version(uint32_t rev
) {
1489 struct image_partition_entry entry
= alloc_image_partition("soft-version", sizeof(struct soft_version
));
1490 struct soft_version
*s
= (struct soft_version
*)entry
.data
;
1494 if (source_date_epoch
!= -1)
1495 t
= source_date_epoch
;
1496 else if (time(&t
) == (time_t)(-1))
1497 error(1, errno
, "time");
1499 struct tm
*tm
= localtime(&t
);
1501 s
->magic
= htonl(0x0000000c);
1505 s
->version_major
= 0;
1506 s
->version_minor
= 0;
1507 s
->version_patch
= 0;
1509 s
->year_hi
= bcd((1900+tm
->tm_year
)/100);
1510 s
->year_lo
= bcd(tm
->tm_year
%100);
1511 s
->month
= bcd(tm
->tm_mon
+1);
1512 s
->day
= bcd(tm
->tm_mday
);
1513 s
->rev
= htonl(rev
);
1520 static struct image_partition_entry
make_soft_version_from_string(const char *soft_ver
) {
1521 /** String length _including_ the terminating zero byte */
1522 uint32_t ver_len
= strlen(soft_ver
) + 1;
1523 /** Partition contains 64 bit header, the version string, and one additional null byte */
1524 size_t partition_len
= 2*sizeof(uint32_t) + ver_len
+ 1;
1525 struct image_partition_entry entry
= alloc_image_partition("soft-version", partition_len
);
1527 uint32_t *len
= (uint32_t *)entry
.data
;
1528 len
[0] = htonl(ver_len
);
1530 memcpy(&len
[2], soft_ver
, ver_len
);
1532 entry
.data
[partition_len
- 1] = 0;
1537 /** Generates the support-list partition */
1538 static struct image_partition_entry
make_support_list(struct device_info
*info
) {
1539 size_t len
= strlen(info
->support_list
);
1540 struct image_partition_entry entry
= alloc_image_partition("support-list", len
+ 9);
1542 put32(entry
.data
, len
);
1543 memset(entry
.data
+4, 0, 4);
1544 memcpy(entry
.data
+8, info
->support_list
, len
);
1545 entry
.data
[len
+8] = info
->support_trail
;
1550 /** Creates a new image partition with an arbitrary name from a file */
1551 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
) {
1552 struct stat statbuf
;
1554 if (stat(filename
, &statbuf
) < 0)
1555 error(1, errno
, "unable to stat file `%s'", filename
);
1557 size_t len
= statbuf
.st_size
;
1559 if (add_jffs2_eof
) {
1560 if (file_system_partition
)
1561 len
= ALIGN(len
+ file_system_partition
->base
, 0x10000) + sizeof(jffs2_eof_mark
) - file_system_partition
->base
;
1563 len
= ALIGN(len
, 0x10000) + sizeof(jffs2_eof_mark
);
1566 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1568 FILE *file
= fopen(filename
, "rb");
1570 error(1, errno
, "unable to open file `%s'", filename
);
1572 if (fread(entry
.data
, statbuf
.st_size
, 1, file
) != 1)
1573 error(1, errno
, "unable to read file `%s'", filename
);
1575 if (add_jffs2_eof
) {
1576 uint8_t *eof
= entry
.data
+ statbuf
.st_size
, *end
= entry
.data
+entry
.size
;
1578 memset(eof
, 0xff, end
- eof
- sizeof(jffs2_eof_mark
));
1579 memcpy(end
- sizeof(jffs2_eof_mark
), jffs2_eof_mark
, sizeof(jffs2_eof_mark
));
1587 /** Creates a new image partition from arbitrary data */
1588 static struct image_partition_entry
put_data(const char *part_name
, const char *datain
, size_t len
) {
1590 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1592 memcpy(entry
.data
, datain
, len
);
1598 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1600 Example image partition table:
1602 fwup-ptn partition-table base 0x00800 size 0x00800
1603 fwup-ptn os-image base 0x01000 size 0x113b45
1604 fwup-ptn file-system base 0x114b45 size 0x1d0004
1605 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1607 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1608 the end of the partition table is marked with a zero byte.
1610 The firmware image must contain at least the partition-table and support-list partitions
1611 to be accepted. There aren't any alignment constraints for the image partitions.
1613 The partition-table partition contains the actual flash layout; partitions
1614 from the image partition table are mapped to the corresponding flash partitions during
1615 the firmware upgrade. The support-list partition contains a list of devices supported by
1618 The base offsets in the firmware partition table are relative to the end
1619 of the vendor information block, so the partition-table partition will
1620 actually start at offset 0x1814 of the image.
1622 I think partition-table must be the first partition in the firmware image.
1624 static void put_partitions(uint8_t *buffer
, const struct flash_partition_entry
*flash_parts
, const struct image_partition_entry
*parts
) {
1626 char *image_pt
= (char *)buffer
, *end
= image_pt
+ 0x800;
1628 size_t base
= 0x800;
1629 for (i
= 0; parts
[i
].name
; i
++) {
1630 for (j
= 0; flash_parts
[j
].name
; j
++) {
1631 if (!strcmp(flash_parts
[j
].name
, parts
[i
].name
)) {
1632 if (parts
[i
].size
> flash_parts
[j
].size
)
1633 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts
[j
].name
, (unsigned)flash_parts
[j
].size
);
1638 assert(flash_parts
[j
].name
);
1640 memcpy(buffer
+ base
, parts
[i
].data
, parts
[i
].size
);
1642 size_t len
= end
-image_pt
;
1643 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
);
1646 error(1, 0, "image partition table overflow?");
1650 base
+= parts
[i
].size
;
1654 /** Generates and writes the image MD5 checksum */
1655 static void put_md5(uint8_t *md5
, uint8_t *buffer
, unsigned int len
) {
1659 MD5_Update(&ctx
, md5_salt
, (unsigned int)sizeof(md5_salt
));
1660 MD5_Update(&ctx
, buffer
, len
);
1661 MD5_Final(md5
, &ctx
);
1666 Generates the firmware image in factory format
1672 0000-0003 Image size (4 bytes, big endian)
1673 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1674 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1675 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1676 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1677 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1678 1814-xxxx Firmware partitions
1680 static void * generate_factory_image(struct device_info
*info
, const struct image_partition_entry
*parts
, size_t *len
) {
1684 for (i
= 0; parts
[i
].name
; i
++)
1685 *len
+= parts
[i
].size
;
1687 uint8_t *image
= malloc(*len
);
1689 error(1, errno
, "malloc");
1691 memset(image
, 0xff, *len
);
1695 size_t vendor_len
= strlen(info
->vendor
);
1696 put32(image
+0x14, vendor_len
);
1697 memcpy(image
+0x18, info
->vendor
, vendor_len
);
1700 put_partitions(image
+ 0x1014, info
->partitions
, parts
);
1701 put_md5(image
+0x04, image
+0x14, *len
-0x14);
1707 Generates the firmware image in sysupgrade format
1709 This makes some assumptions about the provided flash and image partition tables and
1710 should be generalized when TP-LINK starts building its safeloader into hardware with
1711 different flash layouts.
1713 static void * generate_sysupgrade_image(struct device_info
*info
, const struct image_partition_entry
*image_parts
, size_t *len
) {
1715 size_t flash_first_partition_index
= 0;
1716 size_t flash_last_partition_index
= 0;
1717 const struct flash_partition_entry
*flash_first_partition
= NULL
;
1718 const struct flash_partition_entry
*flash_last_partition
= NULL
;
1719 const struct image_partition_entry
*image_last_partition
= NULL
;
1721 /** Find first and last partitions */
1722 for (i
= 0; info
->partitions
[i
].name
; i
++) {
1723 if (!strcmp(info
->partitions
[i
].name
, info
->first_sysupgrade_partition
)) {
1724 flash_first_partition
= &info
->partitions
[i
];
1725 flash_first_partition_index
= i
;
1726 } else if (!strcmp(info
->partitions
[i
].name
, info
->last_sysupgrade_partition
)) {
1727 flash_last_partition
= &info
->partitions
[i
];
1728 flash_last_partition_index
= i
;
1732 assert(flash_first_partition
&& flash_last_partition
);
1733 assert(flash_first_partition_index
< flash_last_partition_index
);
1735 /** Find last partition from image to calculate needed size */
1736 for (i
= 0; image_parts
[i
].name
; i
++) {
1737 if (!strcmp(image_parts
[i
].name
, info
->last_sysupgrade_partition
)) {
1738 image_last_partition
= &image_parts
[i
];
1743 assert(image_last_partition
);
1745 *len
= flash_last_partition
->base
- flash_first_partition
->base
+ image_last_partition
->size
;
1747 uint8_t *image
= malloc(*len
);
1749 error(1, errno
, "malloc");
1751 memset(image
, 0xff, *len
);
1753 for (i
= flash_first_partition_index
; i
<= flash_last_partition_index
; i
++) {
1754 for (j
= 0; image_parts
[j
].name
; j
++) {
1755 if (!strcmp(info
->partitions
[i
].name
, image_parts
[j
].name
)) {
1756 if (image_parts
[j
].size
> info
->partitions
[i
].size
)
1757 error(1, 0, "%s partition too big (more than %u bytes)", info
->partitions
[i
].name
, (unsigned)info
->partitions
[i
].size
);
1758 memcpy(image
+ info
->partitions
[i
].base
- flash_first_partition
->base
, image_parts
[j
].data
, image_parts
[j
].size
);
1762 assert(image_parts
[j
].name
);
1769 /** Generates an image according to a given layout and writes it to a file */
1770 static void build_image(const char *output
,
1771 const char *kernel_image
,
1772 const char *rootfs_image
,
1776 struct device_info
*info
) {
1780 struct image_partition_entry parts
[7] = {};
1782 struct flash_partition_entry
*firmware_partition
= NULL
;
1783 struct flash_partition_entry
*os_image_partition
= NULL
;
1784 struct flash_partition_entry
*file_system_partition
= NULL
;
1785 size_t firmware_partition_index
= 0;
1787 for (i
= 0; info
->partitions
[i
].name
; i
++) {
1788 if (!strcmp(info
->partitions
[i
].name
, "firmware"))
1790 firmware_partition
= &info
->partitions
[i
];
1791 firmware_partition_index
= i
;
1795 if (firmware_partition
)
1797 os_image_partition
= &info
->partitions
[firmware_partition_index
];
1798 file_system_partition
= &info
->partitions
[firmware_partition_index
+ 1];
1801 if (stat(kernel_image
, &kernel
) < 0)
1802 error(1, errno
, "unable to stat file `%s'", kernel_image
);
1804 if (kernel
.st_size
> firmware_partition
->size
)
1805 error(1, 0, "kernel overflowed firmware partition\n");
1807 for (i
= MAX_PARTITIONS
-1; i
>= firmware_partition_index
+ 1; i
--)
1808 info
->partitions
[i
+1] = info
->partitions
[i
];
1810 file_system_partition
->name
= "file-system";
1811 file_system_partition
->base
= firmware_partition
->base
+ kernel
.st_size
;
1813 /* Align partition start to erase blocks for factory images only */
1815 file_system_partition
->base
= ALIGN(firmware_partition
->base
+ kernel
.st_size
, 0x10000);
1817 file_system_partition
->size
= firmware_partition
->size
- file_system_partition
->base
;
1819 os_image_partition
->name
= "os-image";
1820 os_image_partition
->size
= kernel
.st_size
;
1823 parts
[0] = make_partition_table(info
->partitions
);
1825 parts
[1] = make_soft_version_from_string(info
->soft_ver
);
1827 parts
[1] = make_soft_version(rev
);
1829 parts
[2] = make_support_list(info
);
1830 parts
[3] = read_file("os-image", kernel_image
, false, NULL
);
1831 parts
[4] = read_file("file-system", rootfs_image
, add_jffs2_eof
, file_system_partition
);
1833 /* Some devices need the extra-para partition to accept the firmware */
1834 if (strcasecmp(info
->id
, "ARCHER-C2-V3") == 0 ||
1835 strcasecmp(info
->id
, "ARCHER-C25-V1") == 0 ||
1836 strcasecmp(info
->id
, "ARCHER-C59-V2") == 0 ||
1837 strcasecmp(info
->id
, "ARCHER-C60-V2") == 0 ||
1838 strcasecmp(info
->id
, "TLWR1043NV5") == 0) {
1839 const char mdat
[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1840 parts
[5] = put_data("extra-para", mdat
, 11);
1841 } else if (strcasecmp(info
->id
, "ARCHER-A7-V5") == 0 || strcasecmp(info
->id
, "ARCHER-C7-V4") == 0 || strcasecmp(info
->id
, "ARCHER-C7-V5") == 0) {
1842 const char mdat
[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1843 parts
[5] = put_data("extra-para", mdat
, 11);
1844 } else if (strcasecmp(info
->id
, "ARCHER-C6-V2") == 0) {
1845 const char mdat
[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00};
1846 parts
[5] = put_data("extra-para", mdat
, 11);
1852 image
= generate_sysupgrade_image(info
, parts
, &len
);
1854 image
= generate_factory_image(info
, parts
, &len
);
1856 FILE *file
= fopen(output
, "wb");
1858 error(1, errno
, "unable to open output file");
1860 if (fwrite(image
, len
, 1, file
) != 1)
1861 error(1, 0, "unable to write output file");
1867 for (i
= 0; parts
[i
].name
; i
++)
1868 free_image_partition(parts
[i
]);
1872 static void usage(const char *argv0
) {
1874 "Usage: %s [OPTIONS...]\n"
1877 " -h show this help\n"
1879 "Create a new image:\n"
1880 " -B <board> create image for the board specified with <board>\n"
1881 " -k <file> read kernel image from the file <file>\n"
1882 " -r <file> read rootfs image from the file <file>\n"
1883 " -o <file> write output to the file <file>\n"
1884 " -V <rev> sets the revision number to <rev>\n"
1885 " -j add jffs2 end-of-filesystem markers\n"
1886 " -S create sysupgrade instead of factory image\n"
1887 "Extract an old image:\n"
1888 " -x <file> extract all oem firmware partition\n"
1889 " -d <dir> destination to extract the firmware partition\n"
1890 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1896 static struct device_info
*find_board(const char *id
)
1898 struct device_info
*board
= NULL
;
1900 for (board
= boards
; board
->id
!= NULL
; board
++)
1901 if (strcasecmp(id
, board
->id
) == 0)
1907 static int add_flash_partition(
1908 struct flash_partition_entry
*part_list
,
1915 /* check if the list has a free entry */
1916 for (ptr
= 0; ptr
< max_entries
; ptr
++, part_list
++) {
1917 if (part_list
->name
== NULL
&&
1918 part_list
->base
== 0 &&
1919 part_list
->size
== 0)
1923 if (ptr
== max_entries
) {
1924 error(1, 0, "No free flash part entry available.");
1927 part_list
->name
= calloc(1, strlen(name
) + 1);
1928 if (!part_list
->name
) {
1929 error(1, 0, "Unable to allocate memory");
1932 memcpy((char *)part_list
->name
, name
, strlen(name
));
1933 part_list
->base
= base
;
1934 part_list
->size
= size
;
1939 /** read the partition table into struct flash_partition_entry */
1940 static int read_partition_table(
1941 FILE *file
, long offset
,
1942 struct flash_partition_entry
*entries
, size_t max_entries
,
1947 const char *parthdr
= NULL
;
1948 const char *fwuphdr
= "fwup-ptn";
1949 const char *flashhdr
= "partition";
1951 /* TODO: search for the partition table */
1961 error(1, 0, "Invalid partition table");
1964 if (fseek(file
, offset
, SEEK_SET
) < 0)
1965 error(1, errno
, "Can not seek in the firmware");
1967 if (fread(buf
, 2048, 1, file
) != 1)
1968 error(1, errno
, "Can not read fwup-ptn from the firmware");
1972 /* look for the partition header */
1973 if (memcmp(buf
, parthdr
, strlen(parthdr
)) != 0) {
1974 fprintf(stderr
, "DEBUG: can not find fwuphdr\n");
1979 end
= buf
+ sizeof(buf
);
1980 while ((ptr
+ strlen(parthdr
)) < end
&&
1981 memcmp(ptr
, parthdr
, strlen(parthdr
)) == 0) {
1985 char name
[32] = { 0 };
1987 unsigned long base
= 0;
1988 unsigned long size
= 0;
1990 end_part
= memchr(ptr
, '\n', (end
- ptr
));
1991 if (end_part
== NULL
) {
1992 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
1996 for (int i
= 0; i
<= 4; i
++) {
1997 if (end_part
<= ptr
)
2000 end_element
= memchr(ptr
, 0x20, (end_part
- ptr
));
2001 if (end_element
== NULL
) {
2002 error(1, errno
, "Ignoring the rest of the partition entries.");
2007 /* partition header */
2009 ptr
= end_element
+ 1;
2013 name_len
= (end_element
- ptr
) > 31 ? 31 : (end_element
- ptr
);
2014 strncpy(name
, ptr
, name_len
);
2015 name
[name_len
] = '\0';
2016 ptr
= end_element
+ 1;
2021 ptr
= end_element
+ 1;
2026 base
= strtoul(ptr
, NULL
, 16);
2027 ptr
= end_element
+ 1;
2032 ptr
= end_element
+ 1;
2033 /* actual size. The last element doesn't have a sepeartor */
2034 size
= strtoul(ptr
, NULL
, 16);
2035 /* the part ends with 0x09, 0x0d, 0x0a */
2037 add_flash_partition(entries
, max_entries
, name
, base
, size
);
2046 static void write_partition(
2048 size_t firmware_offset
,
2049 struct flash_partition_entry
*entry
,
2055 fseek(input_file
, entry
->base
+ firmware_offset
, SEEK_SET
);
2057 for (offset
= 0; sizeof(buf
) + offset
<= entry
->size
; offset
+= sizeof(buf
)) {
2058 if (fread(buf
, sizeof(buf
), 1, input_file
) != 1)
2059 error(1, errno
, "Can not read partition from input_file");
2061 if (fwrite(buf
, sizeof(buf
), 1, output_file
) != 1)
2062 error(1, errno
, "Can not write partition to output_file");
2064 /* write last chunk smaller than buffer */
2065 if (offset
< entry
->size
) {
2066 offset
= entry
->size
- offset
;
2067 if (fread(buf
, offset
, 1, input_file
) != 1)
2068 error(1, errno
, "Can not read partition from input_file");
2069 if (fwrite(buf
, offset
, 1, output_file
) != 1)
2070 error(1, errno
, "Can not write partition to output_file");
2074 static int extract_firmware_partition(FILE *input_file
, size_t firmware_offset
, struct flash_partition_entry
*entry
, const char *output_directory
)
2077 char output
[PATH_MAX
];
2079 snprintf(output
, PATH_MAX
, "%s/%s", output_directory
, entry
->name
);
2080 output_file
= fopen(output
, "wb+");
2081 if (output_file
== NULL
) {
2082 error(1, errno
, "Can not open output file %s", output
);
2085 write_partition(input_file
, firmware_offset
, entry
, output_file
);
2087 fclose(output_file
);
2092 /** extract all partitions from the firmware file */
2093 static int extract_firmware(const char *input
, const char *output_directory
)
2095 struct flash_partition_entry entries
[16] = { 0 };
2096 size_t max_entries
= 16;
2097 size_t firmware_offset
= 0x1014;
2100 struct stat statbuf
;
2102 /* check input file */
2103 if (stat(input
, &statbuf
)) {
2104 error(1, errno
, "Can not read input firmware %s", input
);
2107 /* check if output directory exists */
2108 if (stat(output_directory
, &statbuf
)) {
2109 error(1, errno
, "Failed to stat output directory %s", output_directory
);
2112 if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
2113 error(1, errno
, "Given output directory is not a directory %s", output_directory
);
2116 input_file
= fopen(input
, "rb");
2118 if (read_partition_table(input_file
, firmware_offset
, entries
, 16, 0) != 0) {
2119 error(1, 0, "Error can not read the partition table (fwup-ptn)");
2122 for (size_t i
= 0; i
< max_entries
; i
++) {
2123 if (entries
[i
].name
== NULL
&&
2124 entries
[i
].base
== 0 &&
2125 entries
[i
].size
== 0)
2128 extract_firmware_partition(input_file
, firmware_offset
, &entries
[i
], output_directory
);
2134 static struct flash_partition_entry
*find_partition(
2135 struct flash_partition_entry
*entries
, size_t max_entries
,
2136 const char *name
, const char *error_msg
)
2138 for (size_t i
= 0; i
< max_entries
; i
++, entries
++) {
2139 if (strcmp(entries
->name
, name
) == 0)
2143 error(1, 0, "%s", error_msg
);
2147 static void write_ff(FILE *output_file
, size_t size
)
2152 memset(buf
, 0xff, sizeof(buf
));
2154 for (offset
= 0; offset
+ sizeof(buf
) < size
; offset
+= sizeof(buf
)) {
2155 if (fwrite(buf
, sizeof(buf
), 1, output_file
) != 1)
2156 error(1, errno
, "Can not write 0xff to output_file");
2159 /* write last chunk smaller than buffer */
2160 if (offset
< size
) {
2161 offset
= size
- offset
;
2162 if (fwrite(buf
, offset
, 1, output_file
) != 1)
2163 error(1, errno
, "Can not write partition to output_file");
2167 static void convert_firmware(const char *input
, const char *output
)
2169 struct flash_partition_entry fwup
[MAX_PARTITIONS
] = { 0 };
2170 struct flash_partition_entry flash
[MAX_PARTITIONS
] = { 0 };
2171 struct flash_partition_entry
*fwup_os_image
= NULL
, *fwup_file_system
= NULL
;
2172 struct flash_partition_entry
*flash_os_image
= NULL
, *flash_file_system
= NULL
;
2173 struct flash_partition_entry
*fwup_partition_table
= NULL
;
2174 size_t firmware_offset
= 0x1014;
2175 FILE *input_file
, *output_file
;
2177 struct stat statbuf
;
2179 /* check input file */
2180 if (stat(input
, &statbuf
)) {
2181 error(1, errno
, "Can not read input firmware %s", input
);
2184 input_file
= fopen(input
, "rb");
2186 error(1, 0, "Can not open input firmware %s", input
);
2188 output_file
= fopen(output
, "wb");
2190 error(1, 0, "Can not open output firmware %s", output
);
2192 if (read_partition_table(input_file
, firmware_offset
, fwup
, MAX_PARTITIONS
, 0) != 0) {
2193 error(1, 0, "Error can not read the partition table (fwup-ptn)");
2196 fwup_os_image
= find_partition(fwup
, MAX_PARTITIONS
,
2197 "os-image", "Error can not find os-image partition (fwup)");
2198 fwup_file_system
= find_partition(fwup
, MAX_PARTITIONS
,
2199 "file-system", "Error can not find file-system partition (fwup)");
2200 fwup_partition_table
= find_partition(fwup
, MAX_PARTITIONS
,
2201 "partition-table", "Error can not find partition-table partition");
2203 /* the flash partition table has a 0x00000004 magic haeder */
2204 if (read_partition_table(input_file
, firmware_offset
+ fwup_partition_table
->base
+ 4, flash
, MAX_PARTITIONS
, 1) != 0)
2205 error(1, 0, "Error can not read the partition table (flash)");
2207 flash_os_image
= find_partition(flash
, MAX_PARTITIONS
,
2208 "os-image", "Error can not find os-image partition (flash)");
2209 flash_file_system
= find_partition(flash
, MAX_PARTITIONS
,
2210 "file-system", "Error can not find file-system partition (flash)");
2212 /* write os_image to 0x0 */
2213 write_partition(input_file
, firmware_offset
, fwup_os_image
, output_file
);
2214 write_ff(output_file
, flash_os_image
->size
- fwup_os_image
->size
);
2216 /* write file-system behind os_image */
2217 fseek(output_file
, flash_file_system
->base
- flash_os_image
->base
, SEEK_SET
);
2218 write_partition(input_file
, firmware_offset
, fwup_file_system
, output_file
);
2219 write_ff(output_file
, flash_file_system
->size
- fwup_file_system
->size
);
2221 fclose(output_file
);
2225 int main(int argc
, char *argv
[]) {
2226 const char *board
= NULL
, *kernel_image
= NULL
, *rootfs_image
= NULL
, *output
= NULL
;
2227 const char *extract_image
= NULL
, *output_directory
= NULL
, *convert_image
= NULL
;
2228 bool add_jffs2_eof
= false, sysupgrade
= false;
2230 struct device_info
*info
;
2231 set_source_date_epoch();
2236 c
= getopt(argc
, argv
, "B:k:r:o:V:jSh:x:d:z:");
2246 kernel_image
= optarg
;
2250 rootfs_image
= optarg
;
2258 sscanf(optarg
, "r%u", &rev
);
2262 add_jffs2_eof
= true;
2274 output_directory
= optarg
;
2278 extract_image
= optarg
;
2282 convert_image
= optarg
;
2291 if (extract_image
|| output_directory
) {
2293 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
2294 if (!output_directory
)
2295 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
2296 extract_firmware(extract_image
, output_directory
);
2297 } else if (convert_image
) {
2299 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
2300 convert_firmware(convert_image
, output
);
2303 error(1, 0, "no board has been specified");
2305 error(1, 0, "no kernel image has been specified");
2307 error(1, 0, "no rootfs image has been specified");
2309 error(1, 0, "no output filename has been specified");
2311 info
= find_board(board
);
2314 error(1, 0, "unsupported board %s", board
);
2316 build_image(output
, kernel_image
, rootfs_image
, rev
, add_jffs2_eof
, sysupgrade
, info
);