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