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>
53 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
56 #define MAX_PARTITIONS 32
58 /** An image partition table entry */
59 struct image_partition_entry {
65 /** A flash partition table entry */
66 struct flash_partition_entry {
72 /** Firmware layout description */
76 const char *support_list;
79 const struct flash_partition_entry partitions[MAX_PARTITIONS+1];
80 const char *first_sysupgrade_partition;
81 const char *last_sysupgrade_partition;
84 /** The content of the soft-version structure */
85 struct __attribute__((__packed__)) soft_version {
89 uint8_t version_major;
90 uint8_t version_minor;
91 uint8_t version_patch;
101 static const uint8_t jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
105 Salt for the MD5 hash
107 Fortunately, TP-LINK seems to use the same salt for most devices which use
108 the new image format.
110 static const uint8_t md5_salt[16] = {
111 0x7a, 0x2b, 0x15, 0xed,
112 0x9b, 0x98, 0x59, 0x6d,
113 0xe5, 0x04, 0xab, 0x44,
114 0xac, 0x2a, 0x9f, 0x4e,
118 /** Firmware layout table */
119 static struct device_info boards[] = {
120 /** Firmware layout for the CPE210/220 */
123 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
126 "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
127 "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
128 "CPE210(TP-LINK|US|N300-2):1.1\r\n"
129 "CPE210(TP-LINK|EU|N300-2):1.1\r\n"
130 "CPE220(TP-LINK|UN|N300-2):1.1\r\n"
131 "CPE220(TP-LINK|US|N300-2):1.1\r\n"
132 "CPE220(TP-LINK|EU|N300-2):1.1\r\n",
133 .support_trail = '\xff',
137 {"fs-uboot", 0x00000, 0x20000},
138 {"partition-table", 0x20000, 0x02000},
139 {"default-mac", 0x30000, 0x00020},
140 {"product-info", 0x31100, 0x00100},
141 {"signature", 0x32000, 0x00400},
142 {"os-image", 0x40000, 0x170000},
143 {"soft-version", 0x1b0000, 0x00100},
144 {"support-list", 0x1b1000, 0x00400},
145 {"file-system", 0x1c0000, 0x600000},
146 {"user-config", 0x7c0000, 0x10000},
147 {"default-config", 0x7d0000, 0x10000},
148 {"log", 0x7e0000, 0x10000},
149 {"radio", 0x7f0000, 0x10000},
153 .first_sysupgrade_partition = "os-image",
154 .last_sysupgrade_partition = "file-system",
157 /** Firmware layout for the CPE510/520 */
160 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
163 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
164 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
165 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
166 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
167 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
168 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
169 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
170 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
171 .support_trail = '\xff',
175 {"fs-uboot", 0x00000, 0x20000},
176 {"partition-table", 0x20000, 0x02000},
177 {"default-mac", 0x30000, 0x00020},
178 {"product-info", 0x31100, 0x00100},
179 {"signature", 0x32000, 0x00400},
180 {"os-image", 0x40000, 0x170000},
181 {"soft-version", 0x1b0000, 0x00100},
182 {"support-list", 0x1b1000, 0x00400},
183 {"file-system", 0x1c0000, 0x600000},
184 {"user-config", 0x7c0000, 0x10000},
185 {"default-config", 0x7d0000, 0x10000},
186 {"log", 0x7e0000, 0x10000},
187 {"radio", 0x7f0000, 0x10000},
191 .first_sysupgrade_partition = "os-image",
192 .last_sysupgrade_partition = "file-system",
197 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
200 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
201 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
202 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
203 .support_trail = '\xff',
207 {"fs-uboot", 0x00000, 0x20000},
208 {"partition-table", 0x20000, 0x02000},
209 {"default-mac", 0x30000, 0x00020},
210 {"product-info", 0x31100, 0x00100},
211 {"signature", 0x32000, 0x00400},
212 {"os-image", 0x40000, 0x170000},
213 {"soft-version", 0x1b0000, 0x00100},
214 {"support-list", 0x1b1000, 0x00400},
215 {"file-system", 0x1c0000, 0x600000},
216 {"user-config", 0x7c0000, 0x10000},
217 {"default-config", 0x7d0000, 0x10000},
218 {"log", 0x7e0000, 0x10000},
219 {"radio", 0x7f0000, 0x10000},
223 .first_sysupgrade_partition = "os-image",
224 .last_sysupgrade_partition = "file-system",
229 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
232 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
233 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
234 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
235 .support_trail = '\xff',
239 {"fs-uboot", 0x00000, 0x20000},
240 {"partition-table", 0x20000, 0x02000},
241 {"default-mac", 0x30000, 0x00020},
242 {"product-info", 0x31100, 0x00100},
243 {"signature", 0x32000, 0x00400},
244 {"os-image", 0x40000, 0x170000},
245 {"soft-version", 0x1b0000, 0x00100},
246 {"support-list", 0x1b1000, 0x00400},
247 {"file-system", 0x1c0000, 0x600000},
248 {"user-config", 0x7c0000, 0x10000},
249 {"default-config", 0x7d0000, 0x10000},
250 {"log", 0x7e0000, 0x10000},
251 {"radio", 0x7f0000, 0x10000},
255 .first_sysupgrade_partition = "os-image",
256 .last_sysupgrade_partition = "file-system",
259 /** Firmware layout for the C2600 */
265 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
266 .support_trail = '\x00',
270 {"SBL1", 0x00000, 0x20000},
271 {"MIBIB", 0x20000, 0x20000},
272 {"SBL2", 0x40000, 0x20000},
273 {"SBL3", 0x60000, 0x30000},
274 {"DDRCONFIG", 0x90000, 0x10000},
275 {"SSD", 0xa0000, 0x10000},
276 {"TZ", 0xb0000, 0x30000},
277 {"RPM", 0xe0000, 0x20000},
278 {"fs-uboot", 0x100000, 0x70000},
279 {"uboot-env", 0x170000, 0x40000},
280 {"radio", 0x1b0000, 0x40000},
281 {"os-image", 0x1f0000, 0x200000},
282 {"file-system", 0x3f0000, 0x1b00000},
283 {"default-mac", 0x1ef0000, 0x00200},
284 {"pin", 0x1ef0200, 0x00200},
285 {"product-info", 0x1ef0400, 0x0fc00},
286 {"partition-table", 0x1f00000, 0x10000},
287 {"soft-version", 0x1f10000, 0x10000},
288 {"support-list", 0x1f20000, 0x10000},
289 {"profile", 0x1f30000, 0x10000},
290 {"default-config", 0x1f40000, 0x10000},
291 {"user-config", 0x1f50000, 0x40000},
292 {"qos-db", 0x1f90000, 0x40000},
293 {"usb-config", 0x1fd0000, 0x10000},
294 {"log", 0x1fe0000, 0x20000},
298 .first_sysupgrade_partition = "os-image",
299 .last_sysupgrade_partition = "file-system"
302 /** Firmware layout for the C25v1 */
304 .id = "ARCHER-C25-V1",
307 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
308 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
309 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
310 .support_trail = '\x00',
311 .soft_ver = "soft_ver:1.0.0\n",
314 We use a bigger os-image partition than the stock images (and thus
315 smaller file-system), as our kernel doesn't fit in the stock firmware's
319 {"factory-boot", 0x00000, 0x20000},
320 {"fs-uboot", 0x20000, 0x10000},
321 {"os-image", 0x30000, 0x180000}, /* Stock: base 0x30000 size 0x100000 */
322 {"file-system", 0x1b0000, 0x620000}, /* Stock: base 0x130000 size 0x6a0000 */
323 {"user-config", 0x7d0000, 0x04000},
324 {"default-mac", 0x7e0000, 0x00100},
325 {"device-id", 0x7e0100, 0x00100},
326 {"extra-para", 0x7e0200, 0x00100},
327 {"pin", 0x7e0300, 0x00100},
328 {"support-list", 0x7e0400, 0x00400},
329 {"soft-version", 0x7e0800, 0x00400},
330 {"product-info", 0x7e0c00, 0x01400},
331 {"partition-table", 0x7e2000, 0x01000},
332 {"profile", 0x7e3000, 0x01000},
333 {"default-config", 0x7e4000, 0x04000},
334 {"merge-config", 0x7ec000, 0x02000},
335 {"qos-db", 0x7ee000, 0x02000},
336 {"radio", 0x7f0000, 0x10000},
340 .first_sysupgrade_partition = "os-image",
341 .last_sysupgrade_partition = "file-system",
344 /** Firmware layout for the C58v1 */
346 .id = "ARCHER-C58-V1",
350 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
351 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
352 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
353 .support_trail = '\x00',
354 .soft_ver = "soft_ver:1.0.0\n",
357 {"fs-uboot", 0x00000, 0x10000},
358 {"default-mac", 0x10000, 0x00200},
359 {"pin", 0x10200, 0x00200},
360 {"product-info", 0x10400, 0x00100},
361 {"partition-table", 0x10500, 0x00800},
362 {"soft-version", 0x11300, 0x00200},
363 {"support-list", 0x11500, 0x00100},
364 {"device-id", 0x11600, 0x00100},
365 {"profile", 0x11700, 0x03900},
366 {"default-config", 0x15000, 0x04000},
367 {"user-config", 0x19000, 0x04000},
368 {"os-image", 0x20000, 0x150000},
369 {"file-system", 0x170000, 0x678000},
370 {"certyficate", 0x7e8000, 0x08000},
371 {"radio", 0x7f0000, 0x10000},
375 .first_sysupgrade_partition = "os-image",
376 .last_sysupgrade_partition = "file-system",
379 /** Firmware layout for the C59v1 */
381 .id = "ARCHER-C59-V1",
385 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
386 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
387 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
388 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
389 .support_trail = '\x00',
390 .soft_ver = "soft_ver:1.0.0\n",
393 {"fs-uboot", 0x00000, 0x10000},
394 {"default-mac", 0x10000, 0x00200},
395 {"pin", 0x10200, 0x00200},
396 {"device-id", 0x10400, 0x00100},
397 {"product-info", 0x10500, 0x0fb00},
398 {"os-image", 0x20000, 0x180000},
399 {"file-system", 0x1a0000, 0xcb0000},
400 {"partition-table", 0xe50000, 0x10000},
401 {"soft-version", 0xe60000, 0x10000},
402 {"support-list", 0xe70000, 0x10000},
403 {"profile", 0xe80000, 0x10000},
404 {"default-config", 0xe90000, 0x10000},
405 {"user-config", 0xea0000, 0x40000},
406 {"usb-config", 0xee0000, 0x10000},
407 {"certificate", 0xef0000, 0x10000},
408 {"qos-db", 0xf00000, 0x40000},
409 {"log", 0xfe0000, 0x10000},
410 {"radio", 0xff0000, 0x10000},
414 .first_sysupgrade_partition = "os-image",
415 .last_sysupgrade_partition = "file-system",
418 /** Firmware layout for the C60v1 */
420 .id = "ARCHER-C60-V1",
424 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
425 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
426 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
427 .support_trail = '\x00',
428 .soft_ver = "soft_ver:1.0.0\n",
431 {"fs-uboot", 0x00000, 0x10000},
432 {"default-mac", 0x10000, 0x00200},
433 {"pin", 0x10200, 0x00200},
434 {"product-info", 0x10400, 0x00100},
435 {"partition-table", 0x10500, 0x00800},
436 {"soft-version", 0x11300, 0x00200},
437 {"support-list", 0x11500, 0x00100},
438 {"device-id", 0x11600, 0x00100},
439 {"profile", 0x11700, 0x03900},
440 {"default-config", 0x15000, 0x04000},
441 {"user-config", 0x19000, 0x04000},
442 {"os-image", 0x20000, 0x150000},
443 {"file-system", 0x170000, 0x678000},
444 {"certyficate", 0x7e8000, 0x08000},
445 {"radio", 0x7f0000, 0x10000},
449 .first_sysupgrade_partition = "os-image",
450 .last_sysupgrade_partition = "file-system",
453 /** Firmware layout for the C5 */
455 .id = "ARCHER-C5-V2",
459 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
460 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
461 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
462 .support_trail = '\x00',
466 {"fs-uboot", 0x00000, 0x40000},
467 {"os-image", 0x40000, 0x200000},
468 {"file-system", 0x240000, 0xc00000},
469 {"default-mac", 0xe40000, 0x00200},
470 {"pin", 0xe40200, 0x00200},
471 {"product-info", 0xe40400, 0x00200},
472 {"partition-table", 0xe50000, 0x10000},
473 {"soft-version", 0xe60000, 0x00200},
474 {"support-list", 0xe61000, 0x0f000},
475 {"profile", 0xe70000, 0x10000},
476 {"default-config", 0xe80000, 0x10000},
477 {"user-config", 0xe90000, 0x50000},
478 {"log", 0xee0000, 0x100000},
479 {"radio_bk", 0xfe0000, 0x10000},
480 {"radio", 0xff0000, 0x10000},
484 .first_sysupgrade_partition = "os-image",
485 .last_sysupgrade_partition = "file-system"
488 /** Firmware layout for the C7 */
490 .id = "ARCHER-C7-V4",
493 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
494 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
495 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
496 .support_trail = '\x00',
497 .soft_ver = "soft_ver:1.0.0\n",
500 We use a bigger os-image partition than the stock images (and thus
501 smaller file-system), as our kernel doesn't fit in the stock firmware's
505 {"factory-boot", 0x00000, 0x20000},
506 {"fs-uboot", 0x20000, 0x20000},
507 {"os-image", 0x40000, 0x180000}, /* Stock: base 0x40000 size 0x120000 */
508 {"file-system", 0x1c0000, 0xd40000}, /* Stock: base 0x160000 size 0xda0000 */
509 {"default-mac", 0xf00000, 0x00200},
510 {"pin", 0xf00200, 0x00200},
511 {"device-id", 0xf00400, 0x00100},
512 {"product-info", 0xf00500, 0x0fb00},
513 {"soft-version", 0xf10000, 0x00100},
514 {"extra-para", 0xf11000, 0x01000},
515 {"support-list", 0xf12000, 0x0a000},
516 {"profile", 0xf1c000, 0x04000},
517 {"default-config", 0xf20000, 0x10000},
518 {"user-config", 0xf30000, 0x40000},
519 {"qos-db", 0xf70000, 0x40000},
520 {"certificate", 0xfb0000, 0x10000},
521 {"partition-table", 0xfc0000, 0x10000},
522 {"log", 0xfd0000, 0x20000},
523 {"radio", 0xff0000, 0x10000},
527 .first_sysupgrade_partition = "os-image",
528 .last_sysupgrade_partition = "file-system",
531 /** Firmware layout for the C9 */
537 "{product_name:ArcherC9,"
539 "special_id:00000000}\n",
540 .support_trail = '\x00',
544 {"fs-uboot", 0x00000, 0x40000},
545 {"os-image", 0x40000, 0x200000},
546 {"file-system", 0x240000, 0xc00000},
547 {"default-mac", 0xe40000, 0x00200},
548 {"pin", 0xe40200, 0x00200},
549 {"product-info", 0xe40400, 0x00200},
550 {"partition-table", 0xe50000, 0x10000},
551 {"soft-version", 0xe60000, 0x00200},
552 {"support-list", 0xe61000, 0x0f000},
553 {"profile", 0xe70000, 0x10000},
554 {"default-config", 0xe80000, 0x10000},
555 {"user-config", 0xe90000, 0x50000},
556 {"log", 0xee0000, 0x100000},
557 {"radio_bk", 0xfe0000, 0x10000},
558 {"radio", 0xff0000, 0x10000},
562 .first_sysupgrade_partition = "os-image",
563 .last_sysupgrade_partition = "file-system"
566 /** Firmware layout for the EAP120 */
569 .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
572 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
573 .support_trail = '\xff',
577 {"fs-uboot", 0x00000, 0x20000},
578 {"partition-table", 0x20000, 0x02000},
579 {"default-mac", 0x30000, 0x00020},
580 {"support-list", 0x31000, 0x00100},
581 {"product-info", 0x31100, 0x00100},
582 {"soft-version", 0x32000, 0x00100},
583 {"os-image", 0x40000, 0x180000},
584 {"file-system", 0x1c0000, 0x600000},
585 {"user-config", 0x7c0000, 0x10000},
586 {"backup-config", 0x7d0000, 0x10000},
587 {"log", 0x7e0000, 0x10000},
588 {"radio", 0x7f0000, 0x10000},
592 .first_sysupgrade_partition = "os-image",
593 .last_sysupgrade_partition = "file-system"
596 /** Firmware layout for the TL-WA850RE v2 */
602 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
603 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
604 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
605 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
606 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
607 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
608 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
609 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
610 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
611 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
612 .support_trail = '\x00',
616 576KB were moved from file-system to os-image
617 in comparison to the stock image
620 {"fs-uboot", 0x00000, 0x20000},
621 {"os-image", 0x20000, 0x150000},
622 {"file-system", 0x170000, 0x240000},
623 {"partition-table", 0x3b0000, 0x02000},
624 {"default-mac", 0x3c0000, 0x00020},
625 {"pin", 0x3c0100, 0x00020},
626 {"product-info", 0x3c1000, 0x01000},
627 {"soft-version", 0x3c2000, 0x00100},
628 {"support-list", 0x3c3000, 0x01000},
629 {"profile", 0x3c4000, 0x08000},
630 {"user-config", 0x3d0000, 0x10000},
631 {"default-config", 0x3e0000, 0x10000},
632 {"radio", 0x3f0000, 0x10000},
636 .first_sysupgrade_partition = "os-image",
637 .last_sysupgrade_partition = "file-system"
640 /** Firmware layout for the TL-WA855RE v1 */
646 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
647 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
648 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
649 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
650 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
651 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
652 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
653 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
654 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
655 .support_trail = '\x00',
659 {"fs-uboot", 0x00000, 0x20000},
660 {"os-image", 0x20000, 0x150000},
661 {"file-system", 0x170000, 0x240000},
662 {"partition-table", 0x3b0000, 0x02000},
663 {"default-mac", 0x3c0000, 0x00020},
664 {"pin", 0x3c0100, 0x00020},
665 {"product-info", 0x3c1000, 0x01000},
666 {"soft-version", 0x3c2000, 0x00100},
667 {"support-list", 0x3c3000, 0x01000},
668 {"profile", 0x3c4000, 0x08000},
669 {"user-config", 0x3d0000, 0x10000},
670 {"default-config", 0x3e0000, 0x10000},
671 {"radio", 0x3f0000, 0x10000},
675 .first_sysupgrade_partition = "os-image",
676 .last_sysupgrade_partition = "file-system"
679 /** Firmware layout for the TL-WR1043 v4 */
681 .id = "TLWR1043NDV4",
685 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
686 .support_trail = '\x00',
690 We use a bigger os-image partition than the stock images (and thus
691 smaller file-system), as our kernel doesn't fit in the stock firmware's
695 {"fs-uboot", 0x00000, 0x20000},
696 {"os-image", 0x20000, 0x180000},
697 {"file-system", 0x1a0000, 0xdb0000},
698 {"default-mac", 0xf50000, 0x00200},
699 {"pin", 0xf50200, 0x00200},
700 {"product-info", 0xf50400, 0x0fc00},
701 {"soft-version", 0xf60000, 0x0b000},
702 {"support-list", 0xf6b000, 0x04000},
703 {"profile", 0xf70000, 0x04000},
704 {"default-config", 0xf74000, 0x0b000},
705 {"user-config", 0xf80000, 0x40000},
706 {"partition-table", 0xfc0000, 0x10000},
707 {"log", 0xfd0000, 0x20000},
708 {"radio", 0xff0000, 0x10000},
712 .first_sysupgrade_partition = "os-image",
713 .last_sysupgrade_partition = "file-system"
716 /** Firmware layout for the TL-WR902AC v1 */
718 .id = "TL-WR902AC-V1",
722 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
723 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
724 .support_trail = '\x00',
728 384KB were moved from file-system to os-image
729 in comparison to the stock image
732 {"fs-uboot", 0x00000, 0x20000},
733 {"os-image", 0x20000, 0x160000},
734 {"file-system", 0x180000, 0x5d0000},
735 {"default-mac", 0x750000, 0x00200},
736 {"pin", 0x750200, 0x00200},
737 {"product-info", 0x750400, 0x0fc00},
738 {"soft-version", 0x760000, 0x0b000},
739 {"support-list", 0x76b000, 0x04000},
740 {"profile", 0x770000, 0x04000},
741 {"default-config", 0x774000, 0x0b000},
742 {"user-config", 0x780000, 0x40000},
743 {"partition-table", 0x7c0000, 0x10000},
744 {"log", 0x7d0000, 0x20000},
745 {"radio", 0x7f0000, 0x10000},
749 .first_sysupgrade_partition = "os-image",
750 .last_sysupgrade_partition = "file-system",
753 /** Firmware layout for the TL-WR942N V1 */
759 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
760 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
761 .support_trail = '\x00',
765 {"fs-uboot", 0x00000, 0x20000},
766 {"os-image", 0x20000, 0x150000},
767 {"file-system", 0x170000, 0xcd0000},
768 {"default-mac", 0xe40000, 0x00200},
769 {"pin", 0xe40200, 0x00200},
770 {"product-info", 0xe40400, 0x0fc00},
771 {"partition-table", 0xe50000, 0x10000},
772 {"soft-version", 0xe60000, 0x10000},
773 {"support-list", 0xe70000, 0x10000},
774 {"profile", 0xe80000, 0x10000},
775 {"default-config", 0xe90000, 0x10000},
776 {"user-config", 0xea0000, 0x40000},
777 {"qos-db", 0xee0000, 0x40000},
778 {"certificate", 0xf20000, 0x10000},
779 {"usb-config", 0xfb0000, 0x10000},
780 {"log", 0xfc0000, 0x20000},
781 {"radio-bk", 0xfe0000, 0x10000},
782 {"radio", 0xff0000, 0x10000},
786 .first_sysupgrade_partition = "os-image",
787 .last_sysupgrade_partition = "file-system",
790 /** Firmware layout for the RE350 v1 */
796 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
797 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
798 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
799 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
800 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
801 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
802 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
803 .support_trail = '\x00',
807 The original os-image partition is too small,
808 so we enlarge it to 1.6M
811 {"fs-uboot", 0x00000, 0x20000},
812 {"os-image", 0x20000, 0x1a0000},
813 {"file-system", 0x1c0000, 0x440000},
814 {"partition-table", 0x600000, 0x02000},
815 {"default-mac", 0x610000, 0x00020},
816 {"pin", 0x610100, 0x00020},
817 {"product-info", 0x611100, 0x01000},
818 {"soft-version", 0x620000, 0x01000},
819 {"support-list", 0x621000, 0x01000},
820 {"profile", 0x622000, 0x08000},
821 {"user-config", 0x630000, 0x10000},
822 {"default-config", 0x640000, 0x10000},
823 {"radio", 0x7f0000, 0x10000},
827 .first_sysupgrade_partition = "os-image",
828 .last_sysupgrade_partition = "file-system"
831 /** Firmware layout for the RE450 */
837 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
838 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
839 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
840 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
841 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
842 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
843 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
844 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
845 .support_trail = '\x00',
849 The flash partition table for RE450;
850 it is almost the same as the one used by the stock images,
851 576KB were moved from file-system to os-image.
854 {"fs-uboot", 0x00000, 0x20000},
855 {"os-image", 0x20000, 0x150000},
856 {"file-system", 0x170000, 0x4a0000},
857 {"partition-table", 0x600000, 0x02000},
858 {"default-mac", 0x610000, 0x00020},
859 {"pin", 0x610100, 0x00020},
860 {"product-info", 0x611100, 0x01000},
861 {"soft-version", 0x620000, 0x01000},
862 {"support-list", 0x621000, 0x01000},
863 {"profile", 0x622000, 0x08000},
864 {"user-config", 0x630000, 0x10000},
865 {"default-config", 0x640000, 0x10000},
866 {"radio", 0x7f0000, 0x10000},
870 .first_sysupgrade_partition = "os-image",
871 .last_sysupgrade_partition = "file-system"
877 #define error(_ret, _errno, _str, ...) \
879 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
886 /** Stores a uint32 as big endian */
887 static inline void put32(uint8_t *buf, uint32_t val) {
894 /** Allocates a new image partition */
895 static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
896 struct image_partition_entry entry = {name, len, malloc(len)};
898 error(1, errno, "malloc");
903 /** Frees an image partition */
904 static void free_image_partition(struct image_partition_entry entry) {
908 static time_t source_date_epoch = -1;
909 static void set_source_date_epoch() {
910 char *env = getenv("SOURCE_DATE_EPOCH");
914 source_date_epoch = strtoull(env, &endptr, 10);
915 if (errno || (endptr && *endptr != '\0')) {
916 fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
922 /** Generates the partition-table partition */
923 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
924 struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
926 char *s = (char *)entry.data, *end = (char *)(s+entry.size);
934 for (i = 0; p[i].name; i++) {
936 size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
939 error(1, 0, "flash partition table overflow?");
946 memset(s, 0xff, end-s);
952 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
953 static inline uint8_t bcd(uint8_t v) {
954 return 0x10 * (v/10) + v%10;
958 /** Generates the soft-version partition */
959 static struct image_partition_entry make_soft_version(uint32_t rev) {
960 struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
961 struct soft_version *s = (struct soft_version *)entry.data;
965 if (source_date_epoch != -1)
966 t = source_date_epoch;
967 else if (time(&t) == (time_t)(-1))
968 error(1, errno, "time");
970 struct tm *tm = localtime(&t);
972 s->magic = htonl(0x0000000c);
976 s->version_major = 0;
977 s->version_minor = 0;
978 s->version_patch = 0;
980 s->year_hi = bcd((1900+tm->tm_year)/100);
981 s->year_lo = bcd(tm->tm_year%100);
982 s->month = bcd(tm->tm_mon+1);
983 s->day = bcd(tm->tm_mday);
991 static struct image_partition_entry make_soft_version_from_string(const char *soft_ver) {
992 /** String length _including_ the terminating zero byte */
993 uint32_t ver_len = strlen(soft_ver) + 1;
994 /** Partition contains 64 bit header, the version string, and one additional null byte */
995 size_t partition_len = 2*sizeof(uint32_t) + ver_len + 1;
996 struct image_partition_entry entry = alloc_image_partition("soft-version", partition_len);
998 uint32_t *len = (uint32_t *)entry.data;
999 len[0] = htonl(ver_len);
1001 memcpy(&len[2], soft_ver, ver_len);
1003 entry.data[partition_len - 1] = 0;
1008 /** Generates the support-list partition */
1009 static struct image_partition_entry make_support_list(const struct device_info *info) {
1010 size_t len = strlen(info->support_list);
1011 struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
1013 put32(entry.data, len);
1014 memset(entry.data+4, 0, 4);
1015 memcpy(entry.data+8, info->support_list, len);
1016 entry.data[len+8] = info->support_trail;
1021 /** Creates a new image partition with an arbitrary name from a file */
1022 static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof) {
1023 struct stat statbuf;
1025 if (stat(filename, &statbuf) < 0)
1026 error(1, errno, "unable to stat file `%s'", filename);
1028 size_t len = statbuf.st_size;
1031 len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
1033 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1035 FILE *file = fopen(filename, "rb");
1037 error(1, errno, "unable to open file `%s'", filename);
1039 if (fread(entry.data, statbuf.st_size, 1, file) != 1)
1040 error(1, errno, "unable to read file `%s'", filename);
1042 if (add_jffs2_eof) {
1043 uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
1045 memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
1046 memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
1054 /** Creates a new image partition from arbitrary data */
1055 static struct image_partition_entry put_data(const char *part_name, const char *datain, size_t len) {
1057 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1059 memcpy(entry.data, datain, len);
1065 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1067 Example image partition table:
1069 fwup-ptn partition-table base 0x00800 size 0x00800
1070 fwup-ptn os-image base 0x01000 size 0x113b45
1071 fwup-ptn file-system base 0x114b45 size 0x1d0004
1072 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1074 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1075 the end of the partition table is marked with a zero byte.
1077 The firmware image must contain at least the partition-table and support-list partitions
1078 to be accepted. There aren't any alignment constraints for the image partitions.
1080 The partition-table partition contains the actual flash layout; partitions
1081 from the image partition table are mapped to the corresponding flash partitions during
1082 the firmware upgrade. The support-list partition contains a list of devices supported by
1085 The base offsets in the firmware partition table are relative to the end
1086 of the vendor information block, so the partition-table partition will
1087 actually start at offset 0x1814 of the image.
1089 I think partition-table must be the first partition in the firmware image.
1091 static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
1093 char *image_pt = (char *)buffer, *end = image_pt + 0x800;
1095 size_t base = 0x800;
1096 for (i = 0; parts[i].name; i++) {
1097 for (j = 0; flash_parts[j].name; j++) {
1098 if (!strcmp(flash_parts[j].name, parts[i].name)) {
1099 if (parts[i].size > flash_parts[j].size)
1100 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
1105 assert(flash_parts[j].name);
1107 memcpy(buffer + base, parts[i].data, parts[i].size);
1109 size_t len = end-image_pt;
1110 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);
1113 error(1, 0, "image partition table overflow?");
1117 base += parts[i].size;
1121 /** Generates and writes the image MD5 checksum */
1122 static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
1126 MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
1127 MD5_Update(&ctx, buffer, len);
1128 MD5_Final(md5, &ctx);
1133 Generates the firmware image in factory format
1139 0000-0003 Image size (4 bytes, big endian)
1140 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1141 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1142 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1143 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1144 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1145 1814-xxxx Firmware partitions
1147 static void * generate_factory_image(const struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
1151 for (i = 0; parts[i].name; i++)
1152 *len += parts[i].size;
1154 uint8_t *image = malloc(*len);
1156 error(1, errno, "malloc");
1158 memset(image, 0xff, *len);
1162 size_t vendor_len = strlen(info->vendor);
1163 put32(image+0x14, vendor_len);
1164 memcpy(image+0x18, info->vendor, vendor_len);
1167 put_partitions(image + 0x1014, info->partitions, parts);
1168 put_md5(image+0x04, image+0x14, *len-0x14);
1174 Generates the firmware image in sysupgrade format
1176 This makes some assumptions about the provided flash and image partition tables and
1177 should be generalized when TP-LINK starts building its safeloader into hardware with
1178 different flash layouts.
1180 static void * generate_sysupgrade_image(const struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
1182 size_t flash_first_partition_index = 0;
1183 size_t flash_last_partition_index = 0;
1184 const struct flash_partition_entry *flash_first_partition = NULL;
1185 const struct flash_partition_entry *flash_last_partition = NULL;
1186 const struct image_partition_entry *image_last_partition = NULL;
1188 /** Find first and last partitions */
1189 for (i = 0; info->partitions[i].name; i++) {
1190 if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
1191 flash_first_partition = &info->partitions[i];
1192 flash_first_partition_index = i;
1193 } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
1194 flash_last_partition = &info->partitions[i];
1195 flash_last_partition_index = i;
1199 assert(flash_first_partition && flash_last_partition);
1200 assert(flash_first_partition_index < flash_last_partition_index);
1202 /** Find last partition from image to calculate needed size */
1203 for (i = 0; image_parts[i].name; i++) {
1204 if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
1205 image_last_partition = &image_parts[i];
1210 assert(image_last_partition);
1212 *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
1214 uint8_t *image = malloc(*len);
1216 error(1, errno, "malloc");
1218 memset(image, 0xff, *len);
1220 for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
1221 for (j = 0; image_parts[j].name; j++) {
1222 if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
1223 if (image_parts[j].size > info->partitions[i].size)
1224 error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
1225 memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
1229 assert(image_parts[j].name);
1236 /** Generates an image according to a given layout and writes it to a file */
1237 static void build_image(const char *output,
1238 const char *kernel_image,
1239 const char *rootfs_image,
1243 const struct device_info *info) {
1245 struct image_partition_entry parts[7] = {};
1247 parts[0] = make_partition_table(info->partitions);
1249 parts[1] = make_soft_version_from_string(info->soft_ver);
1251 parts[1] = make_soft_version(rev);
1253 parts[2] = make_support_list(info);
1254 parts[3] = read_file("os-image", kernel_image, false);
1255 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
1257 if (strcasecmp(info->id, "ARCHER-C25-V1") == 0) {
1258 const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1259 parts[5] = put_data("extra-para", mdat, 11);
1260 } else if (strcasecmp(info->id, "ARCHER-C7-V4") == 0) {
1261 const char mdat[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1262 parts[5] = put_data("extra-para", mdat, 11);
1268 image = generate_sysupgrade_image(info, parts, &len);
1270 image = generate_factory_image(info, parts, &len);
1272 FILE *file = fopen(output, "wb");
1274 error(1, errno, "unable to open output file");
1276 if (fwrite(image, len, 1, file) != 1)
1277 error(1, 0, "unable to write output file");
1284 for (i = 0; parts[i].name; i++)
1285 free_image_partition(parts[i]);
1289 static void usage(const char *argv0) {
1291 "Usage: %s [OPTIONS...]\n"
1294 " -B <board> create image for the board specified with <board>\n"
1295 " -k <file> read kernel image from the file <file>\n"
1296 " -r <file> read rootfs image from the file <file>\n"
1297 " -o <file> write output to the file <file>\n"
1298 " -V <rev> sets the revision number to <rev>\n"
1299 " -j add jffs2 end-of-filesystem markers\n"
1300 " -S create sysupgrade instead of factory image\n"
1301 " -h show this help\n",
1307 static const struct device_info *find_board(const char *id)
1309 struct device_info *board = NULL;
1311 for (board = boards; board->id != NULL; board++)
1312 if (strcasecmp(id, board->id) == 0)
1318 int main(int argc, char *argv[]) {
1319 const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
1320 bool add_jffs2_eof = false, sysupgrade = false;
1322 const struct device_info *info;
1323 set_source_date_epoch();
1328 c = getopt(argc, argv, "B:k:r:o:V:jSh");
1338 kernel_image = optarg;
1342 rootfs_image = optarg;
1350 sscanf(optarg, "r%u", &rev);
1354 add_jffs2_eof = true;
1372 error(1, 0, "no board has been specified");
1374 error(1, 0, "no kernel image has been specified");
1376 error(1, 0, "no rootfs image has been specified");
1378 error(1, 0, "no output filename has been specified");
1380 info = find_board(board);
1383 error(1, 0, "unsupported board %s", board);
1385 build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);