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 const 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, 0x170000},
144 {"soft-version", 0x1b0000, 0x00100},
145 {"support-list", 0x1b1000, 0x00400},
146 {"file-system", 0x1c0000, 0x600000},
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
= "file-system",
158 /** Firmware layout for the CPE510/520 */
161 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
164 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
165 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
166 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
167 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
168 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
169 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
170 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
171 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
172 .support_trail
= '\xff',
176 {"fs-uboot", 0x00000, 0x20000},
177 {"partition-table", 0x20000, 0x02000},
178 {"default-mac", 0x30000, 0x00020},
179 {"product-info", 0x31100, 0x00100},
180 {"signature", 0x32000, 0x00400},
181 {"os-image", 0x40000, 0x170000},
182 {"soft-version", 0x1b0000, 0x00100},
183 {"support-list", 0x1b1000, 0x00400},
184 {"file-system", 0x1c0000, 0x600000},
185 {"user-config", 0x7c0000, 0x10000},
186 {"default-config", 0x7d0000, 0x10000},
187 {"log", 0x7e0000, 0x10000},
188 {"radio", 0x7f0000, 0x10000},
192 .first_sysupgrade_partition
= "os-image",
193 .last_sysupgrade_partition
= "file-system",
198 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
201 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
202 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
203 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
204 .support_trail
= '\xff',
208 {"fs-uboot", 0x00000, 0x20000},
209 {"partition-table", 0x20000, 0x02000},
210 {"default-mac", 0x30000, 0x00020},
211 {"product-info", 0x31100, 0x00100},
212 {"signature", 0x32000, 0x00400},
213 {"os-image", 0x40000, 0x170000},
214 {"soft-version", 0x1b0000, 0x00100},
215 {"support-list", 0x1b1000, 0x00400},
216 {"file-system", 0x1c0000, 0x600000},
217 {"user-config", 0x7c0000, 0x10000},
218 {"default-config", 0x7d0000, 0x10000},
219 {"log", 0x7e0000, 0x10000},
220 {"radio", 0x7f0000, 0x10000},
224 .first_sysupgrade_partition
= "os-image",
225 .last_sysupgrade_partition
= "file-system",
230 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
233 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
234 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
235 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
236 .support_trail
= '\xff',
240 {"fs-uboot", 0x00000, 0x20000},
241 {"partition-table", 0x20000, 0x02000},
242 {"default-mac", 0x30000, 0x00020},
243 {"product-info", 0x31100, 0x00100},
244 {"signature", 0x32000, 0x00400},
245 {"os-image", 0x40000, 0x170000},
246 {"soft-version", 0x1b0000, 0x00100},
247 {"support-list", 0x1b1000, 0x00400},
248 {"file-system", 0x1c0000, 0x600000},
249 {"user-config", 0x7c0000, 0x10000},
250 {"default-config", 0x7d0000, 0x10000},
251 {"log", 0x7e0000, 0x10000},
252 {"radio", 0x7f0000, 0x10000},
256 .first_sysupgrade_partition
= "os-image",
257 .last_sysupgrade_partition
= "file-system",
260 /** Firmware layout for the C2600 */
266 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
267 .support_trail
= '\x00',
271 {"SBL1", 0x00000, 0x20000},
272 {"MIBIB", 0x20000, 0x20000},
273 {"SBL2", 0x40000, 0x20000},
274 {"SBL3", 0x60000, 0x30000},
275 {"DDRCONFIG", 0x90000, 0x10000},
276 {"SSD", 0xa0000, 0x10000},
277 {"TZ", 0xb0000, 0x30000},
278 {"RPM", 0xe0000, 0x20000},
279 {"fs-uboot", 0x100000, 0x70000},
280 {"uboot-env", 0x170000, 0x40000},
281 {"radio", 0x1b0000, 0x40000},
282 {"os-image", 0x1f0000, 0x200000},
283 {"file-system", 0x3f0000, 0x1b00000},
284 {"default-mac", 0x1ef0000, 0x00200},
285 {"pin", 0x1ef0200, 0x00200},
286 {"product-info", 0x1ef0400, 0x0fc00},
287 {"partition-table", 0x1f00000, 0x10000},
288 {"soft-version", 0x1f10000, 0x10000},
289 {"support-list", 0x1f20000, 0x10000},
290 {"profile", 0x1f30000, 0x10000},
291 {"default-config", 0x1f40000, 0x10000},
292 {"user-config", 0x1f50000, 0x40000},
293 {"qos-db", 0x1f90000, 0x40000},
294 {"usb-config", 0x1fd0000, 0x10000},
295 {"log", 0x1fe0000, 0x20000},
299 .first_sysupgrade_partition
= "os-image",
300 .last_sysupgrade_partition
= "file-system"
303 /** Firmware layout for the C25v1 */
305 .id
= "ARCHER-C25-V1",
308 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
309 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
310 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
311 .support_trail
= '\x00',
312 .soft_ver
= "soft_ver:1.0.0\n",
315 We use a bigger os-image partition than the stock images (and thus
316 smaller file-system), as our kernel doesn't fit in the stock firmware's
320 {"factory-boot", 0x00000, 0x20000},
321 {"fs-uboot", 0x20000, 0x10000},
322 {"os-image", 0x30000, 0x180000}, /* Stock: base 0x30000 size 0x100000 */
323 {"file-system", 0x1b0000, 0x620000}, /* Stock: base 0x130000 size 0x6a0000 */
324 {"user-config", 0x7d0000, 0x04000},
325 {"default-mac", 0x7e0000, 0x00100},
326 {"device-id", 0x7e0100, 0x00100},
327 {"extra-para", 0x7e0200, 0x00100},
328 {"pin", 0x7e0300, 0x00100},
329 {"support-list", 0x7e0400, 0x00400},
330 {"soft-version", 0x7e0800, 0x00400},
331 {"product-info", 0x7e0c00, 0x01400},
332 {"partition-table", 0x7e2000, 0x01000},
333 {"profile", 0x7e3000, 0x01000},
334 {"default-config", 0x7e4000, 0x04000},
335 {"merge-config", 0x7ec000, 0x02000},
336 {"qos-db", 0x7ee000, 0x02000},
337 {"radio", 0x7f0000, 0x10000},
341 .first_sysupgrade_partition
= "os-image",
342 .last_sysupgrade_partition
= "file-system",
345 /** Firmware layout for the C58v1 */
347 .id
= "ARCHER-C58-V1",
351 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
352 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
353 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
354 .support_trail
= '\x00',
355 .soft_ver
= "soft_ver:1.0.0\n",
358 {"fs-uboot", 0x00000, 0x10000},
359 {"default-mac", 0x10000, 0x00200},
360 {"pin", 0x10200, 0x00200},
361 {"product-info", 0x10400, 0x00100},
362 {"partition-table", 0x10500, 0x00800},
363 {"soft-version", 0x11300, 0x00200},
364 {"support-list", 0x11500, 0x00100},
365 {"device-id", 0x11600, 0x00100},
366 {"profile", 0x11700, 0x03900},
367 {"default-config", 0x15000, 0x04000},
368 {"user-config", 0x19000, 0x04000},
369 {"os-image", 0x20000, 0x180000},
370 {"file-system", 0x1a0000, 0x648000},
371 {"certyficate", 0x7e8000, 0x08000},
372 {"radio", 0x7f0000, 0x10000},
376 .first_sysupgrade_partition
= "os-image",
377 .last_sysupgrade_partition
= "file-system",
380 /** Firmware layout for the C59v1 */
382 .id
= "ARCHER-C59-V1",
386 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
387 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
388 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
389 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
390 .support_trail
= '\x00',
391 .soft_ver
= "soft_ver:1.0.0\n",
394 {"fs-uboot", 0x00000, 0x10000},
395 {"default-mac", 0x10000, 0x00200},
396 {"pin", 0x10200, 0x00200},
397 {"device-id", 0x10400, 0x00100},
398 {"product-info", 0x10500, 0x0fb00},
399 {"os-image", 0x20000, 0x180000},
400 {"file-system", 0x1a0000, 0xcb0000},
401 {"partition-table", 0xe50000, 0x10000},
402 {"soft-version", 0xe60000, 0x10000},
403 {"support-list", 0xe70000, 0x10000},
404 {"profile", 0xe80000, 0x10000},
405 {"default-config", 0xe90000, 0x10000},
406 {"user-config", 0xea0000, 0x40000},
407 {"usb-config", 0xee0000, 0x10000},
408 {"certificate", 0xef0000, 0x10000},
409 {"qos-db", 0xf00000, 0x40000},
410 {"log", 0xfe0000, 0x10000},
411 {"radio", 0xff0000, 0x10000},
415 .first_sysupgrade_partition
= "os-image",
416 .last_sysupgrade_partition
= "file-system",
419 /** Firmware layout for the C60v1 */
421 .id
= "ARCHER-C60-V1",
425 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
426 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
427 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
428 .support_trail
= '\x00',
429 .soft_ver
= "soft_ver:1.0.0\n",
432 {"fs-uboot", 0x00000, 0x10000},
433 {"default-mac", 0x10000, 0x00200},
434 {"pin", 0x10200, 0x00200},
435 {"product-info", 0x10400, 0x00100},
436 {"partition-table", 0x10500, 0x00800},
437 {"soft-version", 0x11300, 0x00200},
438 {"support-list", 0x11500, 0x00100},
439 {"device-id", 0x11600, 0x00100},
440 {"profile", 0x11700, 0x03900},
441 {"default-config", 0x15000, 0x04000},
442 {"user-config", 0x19000, 0x04000},
443 {"os-image", 0x20000, 0x180000},
444 {"file-system", 0x1a0000, 0x648000},
445 {"certyficate", 0x7e8000, 0x08000},
446 {"radio", 0x7f0000, 0x10000},
450 .first_sysupgrade_partition
= "os-image",
451 .last_sysupgrade_partition
= "file-system",
454 /** Firmware layout for the C60v2 */
456 .id
= "ARCHER-C60-V2",
460 "{product_name:Archer C60,product_ver:2.0.0,special_id:42520000}\r\n"
461 "{product_name:Archer C60,product_ver:2.0.0,special_id:45550000}\r\n"
462 "{product_name:Archer C60,product_ver:2.0.0,special_id:55530000}\r\n",
463 .support_trail
= '\x00',
464 .soft_ver
= "soft_ver:2.0.0\n",
467 {"factory-boot", 0x00000, 0x1fb00},
468 {"default-mac", 0x1fb00, 0x00200},
469 {"pin", 0x1fd00, 0x00100},
470 {"product-info", 0x1fe00, 0x00100},
471 {"device-id", 0x1ff00, 0x00100},
472 {"fs-uboot", 0x20000, 0x10000},
473 {"os-image", 0x30000, 0x180000},
474 {"file-system", 0x1b0000, 0x620000},
475 {"soft-version", 0x7d9500, 0x00100},
476 {"support-list", 0x7d9600, 0x00100},
477 {"extra-para", 0x7d9700, 0x00100},
478 {"profile", 0x7d9800, 0x03000},
479 {"default-config", 0x7dc800, 0x03000},
480 {"partition-table", 0x7df800, 0x00800},
481 {"user-config", 0x7e0000, 0x0c000},
482 {"certificate", 0x7ec000, 0x04000},
483 {"radio", 0x7f0000, 0x10000},
487 .first_sysupgrade_partition
= "os-image",
488 .last_sysupgrade_partition
= "file-system",
491 /** Firmware layout for the C5 */
493 .id
= "ARCHER-C5-V2",
497 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
498 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
499 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
500 .support_trail
= '\x00',
504 {"fs-uboot", 0x00000, 0x40000},
505 {"os-image", 0x40000, 0x200000},
506 {"file-system", 0x240000, 0xc00000},
507 {"default-mac", 0xe40000, 0x00200},
508 {"pin", 0xe40200, 0x00200},
509 {"product-info", 0xe40400, 0x00200},
510 {"partition-table", 0xe50000, 0x10000},
511 {"soft-version", 0xe60000, 0x00200},
512 {"support-list", 0xe61000, 0x0f000},
513 {"profile", 0xe70000, 0x10000},
514 {"default-config", 0xe80000, 0x10000},
515 {"user-config", 0xe90000, 0x50000},
516 {"log", 0xee0000, 0x100000},
517 {"radio_bk", 0xfe0000, 0x10000},
518 {"radio", 0xff0000, 0x10000},
522 .first_sysupgrade_partition
= "os-image",
523 .last_sysupgrade_partition
= "file-system"
526 /** Firmware layout for the C7 */
528 .id
= "ARCHER-C7-V4",
531 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
532 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
533 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
534 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
535 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
536 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
537 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
538 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
539 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
540 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
541 .support_trail
= '\x00',
542 .soft_ver
= "soft_ver:1.0.0\n",
545 We use a bigger os-image partition than the stock images (and thus
546 smaller file-system), as our kernel doesn't fit in the stock firmware's
550 {"factory-boot", 0x00000, 0x20000},
551 {"fs-uboot", 0x20000, 0x20000},
552 {"os-image", 0x40000, 0x180000}, /* Stock: base 0x40000 size 0x120000 */
553 {"file-system", 0x1c0000, 0xd40000}, /* Stock: base 0x160000 size 0xda0000 */
554 {"default-mac", 0xf00000, 0x00200},
555 {"pin", 0xf00200, 0x00200},
556 {"device-id", 0xf00400, 0x00100},
557 {"product-info", 0xf00500, 0x0fb00},
558 {"soft-version", 0xf10000, 0x00100},
559 {"extra-para", 0xf11000, 0x01000},
560 {"support-list", 0xf12000, 0x0a000},
561 {"profile", 0xf1c000, 0x04000},
562 {"default-config", 0xf20000, 0x10000},
563 {"user-config", 0xf30000, 0x40000},
564 {"qos-db", 0xf70000, 0x40000},
565 {"certificate", 0xfb0000, 0x10000},
566 {"partition-table", 0xfc0000, 0x10000},
567 {"log", 0xfd0000, 0x20000},
568 {"radio", 0xff0000, 0x10000},
572 .first_sysupgrade_partition
= "os-image",
573 .last_sysupgrade_partition
= "file-system",
576 /** Firmware layout for the C9 */
582 "{product_name:ArcherC9,"
584 "special_id:00000000}\n",
585 .support_trail
= '\x00',
589 {"fs-uboot", 0x00000, 0x40000},
590 {"os-image", 0x40000, 0x200000},
591 {"file-system", 0x240000, 0xc00000},
592 {"default-mac", 0xe40000, 0x00200},
593 {"pin", 0xe40200, 0x00200},
594 {"product-info", 0xe40400, 0x00200},
595 {"partition-table", 0xe50000, 0x10000},
596 {"soft-version", 0xe60000, 0x00200},
597 {"support-list", 0xe61000, 0x0f000},
598 {"profile", 0xe70000, 0x10000},
599 {"default-config", 0xe80000, 0x10000},
600 {"user-config", 0xe90000, 0x50000},
601 {"log", 0xee0000, 0x100000},
602 {"radio_bk", 0xfe0000, 0x10000},
603 {"radio", 0xff0000, 0x10000},
607 .first_sysupgrade_partition
= "os-image",
608 .last_sysupgrade_partition
= "file-system"
611 /** Firmware layout for the EAP120 */
614 .vendor
= "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
617 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
618 .support_trail
= '\xff',
622 {"fs-uboot", 0x00000, 0x20000},
623 {"partition-table", 0x20000, 0x02000},
624 {"default-mac", 0x30000, 0x00020},
625 {"support-list", 0x31000, 0x00100},
626 {"product-info", 0x31100, 0x00100},
627 {"soft-version", 0x32000, 0x00100},
628 {"os-image", 0x40000, 0x180000},
629 {"file-system", 0x1c0000, 0x600000},
630 {"user-config", 0x7c0000, 0x10000},
631 {"backup-config", 0x7d0000, 0x10000},
632 {"log", 0x7e0000, 0x10000},
633 {"radio", 0x7f0000, 0x10000},
637 .first_sysupgrade_partition
= "os-image",
638 .last_sysupgrade_partition
= "file-system"
641 /** Firmware layout for the TL-WA850RE v2 */
647 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
648 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
649 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
650 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
651 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
652 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
653 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
654 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
655 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
656 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
657 .support_trail
= '\x00',
661 576KB were moved from file-system to os-image
662 in comparison to the stock image
665 {"fs-uboot", 0x00000, 0x20000},
666 {"os-image", 0x20000, 0x150000},
667 {"file-system", 0x170000, 0x240000},
668 {"partition-table", 0x3b0000, 0x02000},
669 {"default-mac", 0x3c0000, 0x00020},
670 {"pin", 0x3c0100, 0x00020},
671 {"product-info", 0x3c1000, 0x01000},
672 {"soft-version", 0x3c2000, 0x00100},
673 {"support-list", 0x3c3000, 0x01000},
674 {"profile", 0x3c4000, 0x08000},
675 {"user-config", 0x3d0000, 0x10000},
676 {"default-config", 0x3e0000, 0x10000},
677 {"radio", 0x3f0000, 0x10000},
681 .first_sysupgrade_partition
= "os-image",
682 .last_sysupgrade_partition
= "file-system"
685 /** Firmware layout for the TL-WA855RE v1 */
691 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
692 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
693 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
694 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
695 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
696 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
697 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
698 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
699 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
700 .support_trail
= '\x00',
704 {"fs-uboot", 0x00000, 0x20000},
705 {"os-image", 0x20000, 0x150000},
706 {"file-system", 0x170000, 0x240000},
707 {"partition-table", 0x3b0000, 0x02000},
708 {"default-mac", 0x3c0000, 0x00020},
709 {"pin", 0x3c0100, 0x00020},
710 {"product-info", 0x3c1000, 0x01000},
711 {"soft-version", 0x3c2000, 0x00100},
712 {"support-list", 0x3c3000, 0x01000},
713 {"profile", 0x3c4000, 0x08000},
714 {"user-config", 0x3d0000, 0x10000},
715 {"default-config", 0x3e0000, 0x10000},
716 {"radio", 0x3f0000, 0x10000},
720 .first_sysupgrade_partition
= "os-image",
721 .last_sysupgrade_partition
= "file-system"
724 /** Firmware layout for the TL-WR1043 v5 */
730 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
731 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
732 .support_trail
= '\x00',
733 .soft_ver
= "soft_ver:1.0.0\n",
735 {"factory-boot", 0x00000, 0x20000},
736 {"fs-uboot", 0x20000, 0x20000},
737 {"os-image", 0x40000, 0x180000},
738 {"file-system", 0x1c0000, 0xd40000},
739 {"default-mac", 0xf00000, 0x00200},
740 {"pin", 0xf00200, 0x00200},
741 {"device-id", 0xf00400, 0x00100},
742 {"product-info", 0xf00500, 0x0fb00},
743 {"soft-version", 0xf10000, 0x01000},
744 {"extra-para", 0xf11000, 0x01000},
745 {"support-list", 0xf12000, 0x0a000},
746 {"profile", 0xf1c000, 0x04000},
747 {"default-config", 0xf20000, 0x10000},
748 {"user-config", 0xf30000, 0x40000},
749 {"qos-db", 0xf70000, 0x40000},
750 {"certificate", 0xfb0000, 0x10000},
751 {"partition-table", 0xfc0000, 0x10000},
752 {"log", 0xfd0000, 0x20000},
753 {"radio", 0xff0000, 0x10000},
756 .first_sysupgrade_partition
= "os-image",
757 .last_sysupgrade_partition
= "file-system"
760 /** Firmware layout for the TL-WR1043 v4 */
762 .id
= "TLWR1043NDV4",
766 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
767 .support_trail
= '\x00',
771 We use a bigger os-image partition than the stock images (and thus
772 smaller file-system), as our kernel doesn't fit in the stock firmware's
776 {"fs-uboot", 0x00000, 0x20000},
777 {"os-image", 0x20000, 0x180000},
778 {"file-system", 0x1a0000, 0xdb0000},
779 {"default-mac", 0xf50000, 0x00200},
780 {"pin", 0xf50200, 0x00200},
781 {"product-info", 0xf50400, 0x0fc00},
782 {"soft-version", 0xf60000, 0x0b000},
783 {"support-list", 0xf6b000, 0x04000},
784 {"profile", 0xf70000, 0x04000},
785 {"default-config", 0xf74000, 0x0b000},
786 {"user-config", 0xf80000, 0x40000},
787 {"partition-table", 0xfc0000, 0x10000},
788 {"log", 0xfd0000, 0x20000},
789 {"radio", 0xff0000, 0x10000},
793 .first_sysupgrade_partition
= "os-image",
794 .last_sysupgrade_partition
= "file-system"
797 /** Firmware layout for the TL-WR902AC v1 */
799 .id
= "TL-WR902AC-V1",
803 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
804 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
805 .support_trail
= '\x00',
809 384KB were moved from file-system to os-image
810 in comparison to the stock image
813 {"fs-uboot", 0x00000, 0x20000},
814 {"os-image", 0x20000, 0x180000},
815 {"file-system", 0x1a0000, 0x5b0000},
816 {"default-mac", 0x750000, 0x00200},
817 {"pin", 0x750200, 0x00200},
818 {"product-info", 0x750400, 0x0fc00},
819 {"soft-version", 0x760000, 0x0b000},
820 {"support-list", 0x76b000, 0x04000},
821 {"profile", 0x770000, 0x04000},
822 {"default-config", 0x774000, 0x0b000},
823 {"user-config", 0x780000, 0x40000},
824 {"partition-table", 0x7c0000, 0x10000},
825 {"log", 0x7d0000, 0x20000},
826 {"radio", 0x7f0000, 0x10000},
830 .first_sysupgrade_partition
= "os-image",
831 .last_sysupgrade_partition
= "file-system",
834 /** Firmware layout for the TL-WR942N V1 */
840 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
841 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
842 .support_trail
= '\x00',
846 {"fs-uboot", 0x00000, 0x20000},
847 {"os-image", 0x20000, 0x180000},
848 {"file-system", 0x1a0000, 0xca0000},
849 {"default-mac", 0xe40000, 0x00200},
850 {"pin", 0xe40200, 0x00200},
851 {"product-info", 0xe40400, 0x0fc00},
852 {"partition-table", 0xe50000, 0x10000},
853 {"soft-version", 0xe60000, 0x10000},
854 {"support-list", 0xe70000, 0x10000},
855 {"profile", 0xe80000, 0x10000},
856 {"default-config", 0xe90000, 0x10000},
857 {"user-config", 0xea0000, 0x40000},
858 {"qos-db", 0xee0000, 0x40000},
859 {"certificate", 0xf20000, 0x10000},
860 {"usb-config", 0xfb0000, 0x10000},
861 {"log", 0xfc0000, 0x20000},
862 {"radio-bk", 0xfe0000, 0x10000},
863 {"radio", 0xff0000, 0x10000},
867 .first_sysupgrade_partition
= "os-image",
868 .last_sysupgrade_partition
= "file-system",
871 /** Firmware layout for the RE350 v1 */
877 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
878 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
879 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
880 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
881 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
882 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
883 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
884 .support_trail
= '\x00',
888 The original os-image partition is too small,
889 so we enlarge it to 1.6M
892 {"fs-uboot", 0x00000, 0x20000},
893 {"os-image", 0x20000, 0x1a0000},
894 {"file-system", 0x1c0000, 0x440000},
895 {"partition-table", 0x600000, 0x02000},
896 {"default-mac", 0x610000, 0x00020},
897 {"pin", 0x610100, 0x00020},
898 {"product-info", 0x611100, 0x01000},
899 {"soft-version", 0x620000, 0x01000},
900 {"support-list", 0x621000, 0x01000},
901 {"profile", 0x622000, 0x08000},
902 {"user-config", 0x630000, 0x10000},
903 {"default-config", 0x640000, 0x10000},
904 {"radio", 0x7f0000, 0x10000},
908 .first_sysupgrade_partition
= "os-image",
909 .last_sysupgrade_partition
= "file-system"
912 /** Firmware layout for the RE355 */
918 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
919 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
920 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
921 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
922 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
923 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
924 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
925 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
926 .support_trail
= '\x00',
930 The flash partition table for RE355;
931 it is almost the same as the one used by the stock images,
932 576KB were moved from file-system to os-image.
935 {"fs-uboot", 0x00000, 0x20000},
936 {"os-image", 0x20000, 0x180000},
937 {"file-system", 0x1a0000, 0x460000},
938 {"partition-table", 0x600000, 0x02000},
939 {"default-mac", 0x610000, 0x00020},
940 {"pin", 0x610100, 0x00020},
941 {"product-info", 0x611100, 0x01000},
942 {"soft-version", 0x620000, 0x01000},
943 {"support-list", 0x621000, 0x01000},
944 {"profile", 0x622000, 0x08000},
945 {"user-config", 0x630000, 0x10000},
946 {"default-config", 0x640000, 0x10000},
947 {"radio", 0x7f0000, 0x10000},
951 .first_sysupgrade_partition
= "os-image",
952 .last_sysupgrade_partition
= "file-system"
955 /** Firmware layout for the RE450 */
961 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
962 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
963 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
964 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
965 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
966 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
967 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
968 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
969 .support_trail
= '\x00',
973 The flash partition table for RE450;
974 it is almost the same as the one used by the stock images,
975 576KB were moved from file-system to os-image.
978 {"fs-uboot", 0x00000, 0x20000},
979 {"os-image", 0x20000, 0x180000},
980 {"file-system", 0x1a0000, 0x460000},
981 {"partition-table", 0x600000, 0x02000},
982 {"default-mac", 0x610000, 0x00020},
983 {"pin", 0x610100, 0x00020},
984 {"product-info", 0x611100, 0x01000},
985 {"soft-version", 0x620000, 0x01000},
986 {"support-list", 0x621000, 0x01000},
987 {"profile", 0x622000, 0x08000},
988 {"user-config", 0x630000, 0x10000},
989 {"default-config", 0x640000, 0x10000},
990 {"radio", 0x7f0000, 0x10000},
994 .first_sysupgrade_partition
= "os-image",
995 .last_sysupgrade_partition
= "file-system"
1001 #define error(_ret, _errno, _str, ...) \
1003 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1004 strerror(_errno)); \
1010 /** Stores a uint32 as big endian */
1011 static inline void put32(uint8_t *buf
, uint32_t val
) {
1018 /** Allocates a new image partition */
1019 static struct image_partition_entry
alloc_image_partition(const char *name
, size_t len
) {
1020 struct image_partition_entry entry
= {name
, len
, malloc(len
)};
1022 error(1, errno
, "malloc");
1027 /** Frees an image partition */
1028 static void free_image_partition(struct image_partition_entry entry
) {
1032 static time_t source_date_epoch
= -1;
1033 static void set_source_date_epoch() {
1034 char *env
= getenv("SOURCE_DATE_EPOCH");
1038 source_date_epoch
= strtoull(env
, &endptr
, 10);
1039 if (errno
|| (endptr
&& *endptr
!= '\0')) {
1040 fprintf(stderr
, "Invalid SOURCE_DATE_EPOCH");
1046 /** Generates the partition-table partition */
1047 static struct image_partition_entry
make_partition_table(const struct flash_partition_entry
*p
) {
1048 struct image_partition_entry entry
= alloc_image_partition("partition-table", 0x800);
1050 char *s
= (char *)entry
.data
, *end
= (char *)(s
+entry
.size
);
1058 for (i
= 0; p
[i
].name
; i
++) {
1060 size_t w
= snprintf(s
, len
, "partition %s base 0x%05x size 0x%05x\n", p
[i
].name
, p
[i
].base
, p
[i
].size
);
1063 error(1, 0, "flash partition table overflow?");
1070 memset(s
, 0xff, end
-s
);
1076 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1077 static inline uint8_t bcd(uint8_t v
) {
1078 return 0x10 * (v
/10) + v
%10;
1082 /** Generates the soft-version partition */
1083 static struct image_partition_entry
make_soft_version(uint32_t rev
) {
1084 struct image_partition_entry entry
= alloc_image_partition("soft-version", sizeof(struct soft_version
));
1085 struct soft_version
*s
= (struct soft_version
*)entry
.data
;
1089 if (source_date_epoch
!= -1)
1090 t
= source_date_epoch
;
1091 else if (time(&t
) == (time_t)(-1))
1092 error(1, errno
, "time");
1094 struct tm
*tm
= localtime(&t
);
1096 s
->magic
= htonl(0x0000000c);
1100 s
->version_major
= 0;
1101 s
->version_minor
= 0;
1102 s
->version_patch
= 0;
1104 s
->year_hi
= bcd((1900+tm
->tm_year
)/100);
1105 s
->year_lo
= bcd(tm
->tm_year
%100);
1106 s
->month
= bcd(tm
->tm_mon
+1);
1107 s
->day
= bcd(tm
->tm_mday
);
1108 s
->rev
= htonl(rev
);
1115 static struct image_partition_entry
make_soft_version_from_string(const char *soft_ver
) {
1116 /** String length _including_ the terminating zero byte */
1117 uint32_t ver_len
= strlen(soft_ver
) + 1;
1118 /** Partition contains 64 bit header, the version string, and one additional null byte */
1119 size_t partition_len
= 2*sizeof(uint32_t) + ver_len
+ 1;
1120 struct image_partition_entry entry
= alloc_image_partition("soft-version", partition_len
);
1122 uint32_t *len
= (uint32_t *)entry
.data
;
1123 len
[0] = htonl(ver_len
);
1125 memcpy(&len
[2], soft_ver
, ver_len
);
1127 entry
.data
[partition_len
- 1] = 0;
1132 /** Generates the support-list partition */
1133 static struct image_partition_entry
make_support_list(const struct device_info
*info
) {
1134 size_t len
= strlen(info
->support_list
);
1135 struct image_partition_entry entry
= alloc_image_partition("support-list", len
+ 9);
1137 put32(entry
.data
, len
);
1138 memset(entry
.data
+4, 0, 4);
1139 memcpy(entry
.data
+8, info
->support_list
, len
);
1140 entry
.data
[len
+8] = info
->support_trail
;
1145 /** Creates a new image partition with an arbitrary name from a file */
1146 static struct image_partition_entry
read_file(const char *part_name
, const char *filename
, bool add_jffs2_eof
) {
1147 struct stat statbuf
;
1149 if (stat(filename
, &statbuf
) < 0)
1150 error(1, errno
, "unable to stat file `%s'", filename
);
1152 size_t len
= statbuf
.st_size
;
1155 len
= ALIGN(len
, 0x10000) + sizeof(jffs2_eof_mark
);
1157 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1159 FILE *file
= fopen(filename
, "rb");
1161 error(1, errno
, "unable to open file `%s'", filename
);
1163 if (fread(entry
.data
, statbuf
.st_size
, 1, file
) != 1)
1164 error(1, errno
, "unable to read file `%s'", filename
);
1166 if (add_jffs2_eof
) {
1167 uint8_t *eof
= entry
.data
+ statbuf
.st_size
, *end
= entry
.data
+entry
.size
;
1169 memset(eof
, 0xff, end
- eof
- sizeof(jffs2_eof_mark
));
1170 memcpy(end
- sizeof(jffs2_eof_mark
), jffs2_eof_mark
, sizeof(jffs2_eof_mark
));
1178 /** Creates a new image partition from arbitrary data */
1179 static struct image_partition_entry
put_data(const char *part_name
, const char *datain
, size_t len
) {
1181 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1183 memcpy(entry
.data
, datain
, len
);
1189 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1191 Example image partition table:
1193 fwup-ptn partition-table base 0x00800 size 0x00800
1194 fwup-ptn os-image base 0x01000 size 0x113b45
1195 fwup-ptn file-system base 0x114b45 size 0x1d0004
1196 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1198 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1199 the end of the partition table is marked with a zero byte.
1201 The firmware image must contain at least the partition-table and support-list partitions
1202 to be accepted. There aren't any alignment constraints for the image partitions.
1204 The partition-table partition contains the actual flash layout; partitions
1205 from the image partition table are mapped to the corresponding flash partitions during
1206 the firmware upgrade. The support-list partition contains a list of devices supported by
1209 The base offsets in the firmware partition table are relative to the end
1210 of the vendor information block, so the partition-table partition will
1211 actually start at offset 0x1814 of the image.
1213 I think partition-table must be the first partition in the firmware image.
1215 static void put_partitions(uint8_t *buffer
, const struct flash_partition_entry
*flash_parts
, const struct image_partition_entry
*parts
) {
1217 char *image_pt
= (char *)buffer
, *end
= image_pt
+ 0x800;
1219 size_t base
= 0x800;
1220 for (i
= 0; parts
[i
].name
; i
++) {
1221 for (j
= 0; flash_parts
[j
].name
; j
++) {
1222 if (!strcmp(flash_parts
[j
].name
, parts
[i
].name
)) {
1223 if (parts
[i
].size
> flash_parts
[j
].size
)
1224 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts
[j
].name
, (unsigned)flash_parts
[j
].size
);
1229 assert(flash_parts
[j
].name
);
1231 memcpy(buffer
+ base
, parts
[i
].data
, parts
[i
].size
);
1233 size_t len
= end
-image_pt
;
1234 size_t w
= snprintf(image_pt
, len
, "fwup-ptn %s base 0x%05x size 0x%05x\t\r\n", parts
[i
].name
, (unsigned)base
, (unsigned)parts
[i
].size
);
1237 error(1, 0, "image partition table overflow?");
1241 base
+= parts
[i
].size
;
1245 /** Generates and writes the image MD5 checksum */
1246 static void put_md5(uint8_t *md5
, uint8_t *buffer
, unsigned int len
) {
1250 MD5_Update(&ctx
, md5_salt
, (unsigned int)sizeof(md5_salt
));
1251 MD5_Update(&ctx
, buffer
, len
);
1252 MD5_Final(md5
, &ctx
);
1257 Generates the firmware image in factory format
1263 0000-0003 Image size (4 bytes, big endian)
1264 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1265 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1266 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1267 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1268 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1269 1814-xxxx Firmware partitions
1271 static void * generate_factory_image(const struct device_info
*info
, const struct image_partition_entry
*parts
, size_t *len
) {
1275 for (i
= 0; parts
[i
].name
; i
++)
1276 *len
+= parts
[i
].size
;
1278 uint8_t *image
= malloc(*len
);
1280 error(1, errno
, "malloc");
1282 memset(image
, 0xff, *len
);
1286 size_t vendor_len
= strlen(info
->vendor
);
1287 put32(image
+0x14, vendor_len
);
1288 memcpy(image
+0x18, info
->vendor
, vendor_len
);
1291 put_partitions(image
+ 0x1014, info
->partitions
, parts
);
1292 put_md5(image
+0x04, image
+0x14, *len
-0x14);
1298 Generates the firmware image in sysupgrade format
1300 This makes some assumptions about the provided flash and image partition tables and
1301 should be generalized when TP-LINK starts building its safeloader into hardware with
1302 different flash layouts.
1304 static void * generate_sysupgrade_image(const struct device_info
*info
, const struct image_partition_entry
*image_parts
, size_t *len
) {
1306 size_t flash_first_partition_index
= 0;
1307 size_t flash_last_partition_index
= 0;
1308 const struct flash_partition_entry
*flash_first_partition
= NULL
;
1309 const struct flash_partition_entry
*flash_last_partition
= NULL
;
1310 const struct image_partition_entry
*image_last_partition
= NULL
;
1312 /** Find first and last partitions */
1313 for (i
= 0; info
->partitions
[i
].name
; i
++) {
1314 if (!strcmp(info
->partitions
[i
].name
, info
->first_sysupgrade_partition
)) {
1315 flash_first_partition
= &info
->partitions
[i
];
1316 flash_first_partition_index
= i
;
1317 } else if (!strcmp(info
->partitions
[i
].name
, info
->last_sysupgrade_partition
)) {
1318 flash_last_partition
= &info
->partitions
[i
];
1319 flash_last_partition_index
= i
;
1323 assert(flash_first_partition
&& flash_last_partition
);
1324 assert(flash_first_partition_index
< flash_last_partition_index
);
1326 /** Find last partition from image to calculate needed size */
1327 for (i
= 0; image_parts
[i
].name
; i
++) {
1328 if (!strcmp(image_parts
[i
].name
, info
->last_sysupgrade_partition
)) {
1329 image_last_partition
= &image_parts
[i
];
1334 assert(image_last_partition
);
1336 *len
= flash_last_partition
->base
- flash_first_partition
->base
+ image_last_partition
->size
;
1338 uint8_t *image
= malloc(*len
);
1340 error(1, errno
, "malloc");
1342 memset(image
, 0xff, *len
);
1344 for (i
= flash_first_partition_index
; i
<= flash_last_partition_index
; i
++) {
1345 for (j
= 0; image_parts
[j
].name
; j
++) {
1346 if (!strcmp(info
->partitions
[i
].name
, image_parts
[j
].name
)) {
1347 if (image_parts
[j
].size
> info
->partitions
[i
].size
)
1348 error(1, 0, "%s partition too big (more than %u bytes)", info
->partitions
[i
].name
, (unsigned)info
->partitions
[i
].size
);
1349 memcpy(image
+ info
->partitions
[i
].base
- flash_first_partition
->base
, image_parts
[j
].data
, image_parts
[j
].size
);
1353 assert(image_parts
[j
].name
);
1360 /** Generates an image according to a given layout and writes it to a file */
1361 static void build_image(const char *output
,
1362 const char *kernel_image
,
1363 const char *rootfs_image
,
1367 const struct device_info
*info
) {
1369 struct image_partition_entry parts
[7] = {};
1371 parts
[0] = make_partition_table(info
->partitions
);
1373 parts
[1] = make_soft_version_from_string(info
->soft_ver
);
1375 parts
[1] = make_soft_version(rev
);
1377 parts
[2] = make_support_list(info
);
1378 parts
[3] = read_file("os-image", kernel_image
, false);
1379 parts
[4] = read_file("file-system", rootfs_image
, add_jffs2_eof
);
1381 /* Some devices need the extra-para partition to accept the firmware */
1382 if (strcasecmp(info
->id
, "ARCHER-C25-V1") == 0 ||
1383 strcasecmp(info
->id
, "ARCHER-C60-V2") == 0 ||
1384 strcasecmp(info
->id
, "TLWR1043NV5") == 0) {
1385 const char mdat
[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1386 parts
[5] = put_data("extra-para", mdat
, 11);
1387 } else if (strcasecmp(info
->id
, "ARCHER-C7-V4") == 0) {
1388 const char mdat
[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1389 parts
[5] = put_data("extra-para", mdat
, 11);
1395 image
= generate_sysupgrade_image(info
, parts
, &len
);
1397 image
= generate_factory_image(info
, parts
, &len
);
1399 FILE *file
= fopen(output
, "wb");
1401 error(1, errno
, "unable to open output file");
1403 if (fwrite(image
, len
, 1, file
) != 1)
1404 error(1, 0, "unable to write output file");
1411 for (i
= 0; parts
[i
].name
; i
++)
1412 free_image_partition(parts
[i
]);
1416 static void usage(const char *argv0
) {
1418 "Usage: %s [OPTIONS...]\n"
1421 " -h show this help\n"
1423 "Create a new image:\n"
1424 " -B <board> create image for the board specified with <board>\n"
1425 " -k <file> read kernel image from the file <file>\n"
1426 " -r <file> read rootfs image from the file <file>\n"
1427 " -o <file> write output to the file <file>\n"
1428 " -V <rev> sets the revision number to <rev>\n"
1429 " -j add jffs2 end-of-filesystem markers\n"
1430 " -S create sysupgrade instead of factory image\n"
1431 "Extract an old image:\n"
1432 " -x <file> extract all oem firmware partition\n"
1433 " -d <dir> destination to extract the firmware partition\n"
1434 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1440 static const struct device_info
*find_board(const char *id
)
1442 struct device_info
*board
= NULL
;
1444 for (board
= boards
; board
->id
!= NULL
; board
++)
1445 if (strcasecmp(id
, board
->id
) == 0)
1451 static int add_flash_partition(
1452 struct flash_partition_entry
*part_list
,
1459 /* check if the list has a free entry */
1460 for (ptr
= 0; ptr
< max_entries
; ptr
++, part_list
++) {
1461 if (part_list
->name
== NULL
&&
1462 part_list
->base
== 0 &&
1463 part_list
->size
== 0)
1467 if (ptr
== max_entries
) {
1468 error(1, 0, "No free flash part entry available.");
1471 part_list
->name
= calloc(1, strlen(name
) + 1);
1472 memcpy((char *)part_list
->name
, name
, strlen(name
));
1473 part_list
->base
= base
;
1474 part_list
->size
= size
;
1479 /** read the partition table into struct flash_partition_entry */
1480 static int read_partition_table(
1481 FILE *file
, long offset
,
1482 struct flash_partition_entry
*entries
, size_t max_entries
,
1487 const char *parthdr
= NULL
;
1488 const char *fwuphdr
= "fwup-ptn";
1489 const char *flashhdr
= "partition";
1491 /* TODO: search for the partition table */
1501 error(1, 0, "Invalid partition table");
1504 if (fseek(file
, offset
, SEEK_SET
) < 0)
1505 error(1, errno
, "Can not seek in the firmware");
1507 if (fread(buf
, 1, 2048, file
) < 0)
1508 error(1, errno
, "Can not read fwup-ptn from the firmware");
1512 /* look for the partition header */
1513 if (memcmp(buf
, parthdr
, strlen(parthdr
)) != 0) {
1514 fprintf(stderr
, "DEBUG: can not find fwuphdr\n");
1519 end
= buf
+ sizeof(buf
);
1520 while ((ptr
+ strlen(parthdr
)) < end
&&
1521 memcmp(ptr
, parthdr
, strlen(parthdr
)) == 0) {
1525 char name
[32] = { 0 };
1527 unsigned long base
= 0;
1528 unsigned long size
= 0;
1530 end_part
= memchr(ptr
, '\n', (end
- ptr
));
1531 if (end_part
== NULL
) {
1532 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
1536 for (int i
= 0; i
<= 4; i
++) {
1537 if (end_part
<= ptr
)
1540 end_element
= memchr(ptr
, 0x20, (end_part
- ptr
));
1541 if (end_element
== NULL
) {
1542 error(1, errno
, "Ignoring the rest of the partition entries.");
1547 /* partition header */
1549 ptr
= end_element
+ 1;
1553 name_len
= (end_element
- ptr
) > 31 ? 31 : (end_element
- ptr
);
1554 strncpy(name
, ptr
, name_len
);
1555 name
[name_len
] = '\0';
1556 ptr
= end_element
+ 1;
1561 ptr
= end_element
+ 1;
1566 base
= strtoul(ptr
, NULL
, 16);
1567 ptr
= end_element
+ 1;
1572 ptr
= end_element
+ 1;
1573 /* actual size. The last element doesn't have a sepeartor */
1574 size
= strtoul(ptr
, NULL
, 16);
1575 /* the part ends with 0x09, 0x0d, 0x0a */
1577 add_flash_partition(entries
, max_entries
, name
, base
, size
);
1586 static void write_partition(
1588 size_t firmware_offset
,
1589 struct flash_partition_entry
*entry
,
1595 fseek(input_file
, entry
->base
+ firmware_offset
, SEEK_SET
);
1597 for (offset
= 0; sizeof(buf
) + offset
<= entry
->size
; offset
+= sizeof(buf
)) {
1598 if (fread(buf
, sizeof(buf
), 1, input_file
) < 0)
1599 error(1, errno
, "Can not read partition from input_file");
1601 if (fwrite(buf
, sizeof(buf
), 1, output_file
) < 0)
1602 error(1, errno
, "Can not write partition to output_file");
1604 /* write last chunk smaller than buffer */
1605 if (offset
< entry
->size
) {
1606 offset
= entry
->size
- offset
;
1607 if (fread(buf
, offset
, 1, input_file
) < 0)
1608 error(1, errno
, "Can not read partition from input_file");
1609 if (fwrite(buf
, offset
, 1, output_file
) < 0)
1610 error(1, errno
, "Can not write partition to output_file");
1614 static int extract_firmware_partition(FILE *input_file
, size_t firmware_offset
, struct flash_partition_entry
*entry
, const char *output_directory
)
1617 char output
[PATH_MAX
];
1619 snprintf(output
, PATH_MAX
, "%s/%s", output_directory
, entry
->name
);
1620 output_file
= fopen(output
, "wb+");
1621 if (output_file
== NULL
) {
1622 error(1, errno
, "Can not open output file %s", output
);
1625 write_partition(input_file
, firmware_offset
, entry
, output_file
);
1627 fclose(output_file
);
1632 /** extract all partitions from the firmware file */
1633 static int extract_firmware(const char *input
, const char *output_directory
)
1635 struct flash_partition_entry entries
[16] = { 0 };
1636 size_t max_entries
= 16;
1637 size_t firmware_offset
= 0x1014;
1640 struct stat statbuf
;
1642 /* check input file */
1643 if (stat(input
, &statbuf
)) {
1644 error(1, errno
, "Can not read input firmware %s", input
);
1647 /* check if output directory exists */
1648 if (stat(output_directory
, &statbuf
)) {
1649 error(1, errno
, "Failed to stat output directory %s", output_directory
);
1652 if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
1653 error(1, errno
, "Given output directory is not a directory %s", output_directory
);
1656 input_file
= fopen(input
, "rb");
1658 if (read_partition_table(input_file
, firmware_offset
, entries
, 16, 0) != 0) {
1659 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1662 for (int i
= 0; i
< max_entries
; i
++) {
1663 if (entries
[i
].name
== NULL
&&
1664 entries
[i
].base
== 0 &&
1665 entries
[i
].size
== 0)
1668 extract_firmware_partition(input_file
, firmware_offset
, &entries
[i
], output_directory
);
1674 static struct flash_partition_entry
*find_partition(
1675 struct flash_partition_entry
*entries
, size_t max_entries
,
1676 const char *name
, const char *error_msg
)
1678 for (int i
= 0; i
< max_entries
; i
++, entries
++) {
1679 if (strcmp(entries
->name
, name
) == 0)
1683 error(1, 0, "%s", error_msg
);
1687 static void write_ff(FILE *output_file
, size_t size
)
1692 memset(buf
, 0xff, sizeof(buf
));
1694 for (offset
= 0; offset
+ sizeof(buf
) < size
; offset
+= sizeof(buf
)) {
1695 if (fwrite(buf
, sizeof(buf
), 1, output_file
) < 0)
1696 error(1, errno
, "Can not write 0xff to output_file");
1699 /* write last chunk smaller than buffer */
1700 if (offset
< size
) {
1701 offset
= size
- offset
;
1702 if (fwrite(buf
, offset
, 1, output_file
) < 0)
1703 error(1, errno
, "Can not write partition to output_file");
1707 static void convert_firmware(const char *input
, const char *output
)
1709 struct flash_partition_entry fwup
[MAX_PARTITIONS
] = { 0 };
1710 struct flash_partition_entry flash
[MAX_PARTITIONS
] = { 0 };
1711 struct flash_partition_entry
*fwup_os_image
= NULL
, *fwup_file_system
= NULL
;
1712 struct flash_partition_entry
*flash_os_image
= NULL
, *flash_file_system
= NULL
;
1713 struct flash_partition_entry
*fwup_partition_table
= NULL
;
1714 size_t firmware_offset
= 0x1014;
1715 FILE *input_file
, *output_file
;
1717 struct stat statbuf
;
1719 /* check input file */
1720 if (stat(input
, &statbuf
)) {
1721 error(1, errno
, "Can not read input firmware %s", input
);
1724 input_file
= fopen(input
, "rb");
1726 error(1, 0, "Can not open input firmware %s", input
);
1728 output_file
= fopen(output
, "wb");
1730 error(1, 0, "Can not open output firmware %s", output
);
1732 if (read_partition_table(input_file
, firmware_offset
, fwup
, MAX_PARTITIONS
, 0) != 0) {
1733 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1736 fwup_os_image
= find_partition(fwup
, MAX_PARTITIONS
,
1737 "os-image", "Error can not find os-image partition (fwup)");
1738 fwup_file_system
= find_partition(fwup
, MAX_PARTITIONS
,
1739 "file-system", "Error can not find file-system partition (fwup)");
1740 fwup_partition_table
= find_partition(fwup
, MAX_PARTITIONS
,
1741 "partition-table", "Error can not find partition-table partition");
1743 /* the flash partition table has a 0x00000004 magic haeder */
1744 if (read_partition_table(input_file
, firmware_offset
+ fwup_partition_table
->base
+ 4, flash
, MAX_PARTITIONS
, 1) != 0)
1745 error(1, 0, "Error can not read the partition table (flash)");
1747 flash_os_image
= find_partition(flash
, MAX_PARTITIONS
,
1748 "os-image", "Error can not find os-image partition (flash)");
1749 flash_file_system
= find_partition(flash
, MAX_PARTITIONS
,
1750 "file-system", "Error can not find file-system partition (flash)");
1752 /* write os_image to 0x0 */
1753 write_partition(input_file
, firmware_offset
, fwup_os_image
, output_file
);
1754 write_ff(output_file
, flash_os_image
->size
- fwup_os_image
->size
);
1756 /* write file-system behind os_image */
1757 fseek(output_file
, flash_file_system
->base
- flash_os_image
->base
, SEEK_SET
);
1758 write_partition(input_file
, firmware_offset
, fwup_file_system
, output_file
);
1759 write_ff(output_file
, flash_file_system
->size
- fwup_file_system
->size
);
1761 fclose(output_file
);
1765 int main(int argc
, char *argv
[]) {
1766 const char *board
= NULL
, *kernel_image
= NULL
, *rootfs_image
= NULL
, *output
= NULL
;
1767 const char *extract_image
= NULL
, *output_directory
= NULL
, *convert_image
= NULL
;
1768 bool add_jffs2_eof
= false, sysupgrade
= false;
1770 const struct device_info
*info
;
1771 set_source_date_epoch();
1776 c
= getopt(argc
, argv
, "B:k:r:o:V:jSh:x:d:z:");
1786 kernel_image
= optarg
;
1790 rootfs_image
= optarg
;
1798 sscanf(optarg
, "r%u", &rev
);
1802 add_jffs2_eof
= true;
1814 output_directory
= optarg
;
1818 extract_image
= optarg
;
1822 convert_image
= optarg
;
1831 if (extract_image
|| output_directory
) {
1833 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
1834 if (!output_directory
)
1835 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
1836 extract_firmware(extract_image
, output_directory
);
1837 } else if (convert_image
) {
1839 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
1840 convert_firmware(convert_image
, output
);
1843 error(1, 0, "no board has been specified");
1845 error(1, 0, "no kernel image has been specified");
1847 error(1, 0, "no rootfs image has been specified");
1849 error(1, 0, "no output filename has been specified");
1851 info
= find_board(board
);
1854 error(1, 0, "unsupported board %s", board
);
1856 build_image(output
, kernel_image
, rootfs_image
, rev
, add_jffs2_eof
, sysupgrade
, info
);