ath79: add support for TP-Link Archer C60 v3
[openwrt/staging/chunkeey.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 {"firmware", 0x40000, 0x770000},
186 {"soft-version", 0x7b0000, 0x00100},
187 {"support-list", 0x7b1000, 0x01000},
188 {"user-config", 0x7c0000, 0x10000},
189 {"default-config", 0x7d0000, 0x10000},
190 {"log", 0x7e0000, 0x10000},
191 {"radio", 0x7f0000, 0x10000},
192 {NULL, 0, 0}
193 },
194
195 .first_sysupgrade_partition = "os-image",
196 .last_sysupgrade_partition = "support-list",
197 },
198
199 /** Firmware layout for the CPE210 V3 */
200 {
201 .id = "CPE210V3",
202 .vendor = "CPE210(TP-LINK|UN|N300-2|00000000):3.0\r\n",
203 .support_list =
204 "SupportList:\r\n"
205 "CPE210(TP-LINK|EU|N300-2|45550000):3.0\r\n"
206 "CPE210(TP-LINK|UN|N300-2|00000000):3.0\r\n"
207 "CPE210(TP-LINK|UN|N300-2):3.0\r\n"
208 "CPE210(TP-LINK|EU|N300-2):3.0\r\n",
209 .support_trail = '\xff',
210 .soft_ver = NULL,
211
212 .partitions = {
213 {"fs-uboot", 0x00000, 0x20000},
214 {"partition-table", 0x20000, 0x01000},
215 {"default-mac", 0x30000, 0x00020},
216 {"product-info", 0x31100, 0x00100},
217 {"device-info", 0x31400, 0x00400},
218 {"signature", 0x32000, 0x00400},
219 {"device-id", 0x33000, 0x00100},
220 {"firmware", 0x40000, 0x770000},
221 {"soft-version", 0x7b0000, 0x00100},
222 {"support-list", 0x7b1000, 0x01000},
223 {"user-config", 0x7c0000, 0x10000},
224 {"default-config", 0x7d0000, 0x10000},
225 {"log", 0x7e0000, 0x10000},
226 {"radio", 0x7f0000, 0x10000},
227 {NULL, 0, 0}
228 },
229
230 .first_sysupgrade_partition = "os-image",
231 .last_sysupgrade_partition = "support-list",
232 },
233
234 /** Firmware layout for the CPE220 V3 */
235 {
236 .id = "CPE220V3",
237 .vendor = "CPE220(TP-LINK|UN|N300-2|00000000):3.0\r\n",
238 .support_list =
239 "SupportList:\r\n"
240 "CPE220(TP-LINK|EU|N300-2|00000000):3.0\r\n"
241 "CPE220(TP-LINK|EU|N300-2|45550000):3.0\r\n"
242 "CPE220(TP-LINK|EU|N300-2|55530000):3.0\r\n"
243 "CPE220(TP-LINK|UN|N300-2|00000000):3.0\r\n"
244 "CPE220(TP-LINK|UN|N300-2|45550000):3.0\r\n"
245 "CPE220(TP-LINK|UN|N300-2|55530000):3.0\r\n"
246 "CPE220(TP-LINK|US|N300-2|55530000):3.0\r\n"
247 "CPE220(TP-LINK|UN|N300-2):3.0\r\n"
248 "CPE220(TP-LINK|EU|N300-2):3.0\r\n"
249 "CPE220(TP-LINK|US|N300-2):3.0\r\n",
250 .support_trail = '\xff',
251 .soft_ver = NULL,
252
253 .partitions = {
254 {"fs-uboot", 0x00000, 0x20000},
255 {"partition-table", 0x20000, 0x02000},
256 {"default-mac", 0x30000, 0x00020},
257 {"product-info", 0x31100, 0x00100},
258 {"device-info", 0x31400, 0x00400},
259 {"signature", 0x32000, 0x00400},
260 {"device-id", 0x33000, 0x00100},
261 {"firmware", 0x40000, 0x770000},
262 {"soft-version", 0x7b0000, 0x00100},
263 {"support-list", 0x7b1000, 0x01000},
264 {"user-config", 0x7c0000, 0x10000},
265 {"default-config", 0x7d0000, 0x10000},
266 {"log", 0x7e0000, 0x10000},
267 {"radio", 0x7f0000, 0x10000},
268 {NULL, 0, 0}
269 },
270
271 .first_sysupgrade_partition = "os-image",
272 .last_sysupgrade_partition = "support-list",
273 },
274
275 /** Firmware layout for the CPE510/520 V1 */
276 {
277 .id = "CPE510",
278 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
279 .support_list =
280 "SupportList:\r\n"
281 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
282 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
283 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
284 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
285 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
286 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
287 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
288 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
289 .support_trail = '\xff',
290 .soft_ver = NULL,
291
292 .partitions = {
293 {"fs-uboot", 0x00000, 0x20000},
294 {"partition-table", 0x20000, 0x02000},
295 {"default-mac", 0x30000, 0x00020},
296 {"product-info", 0x31100, 0x00100},
297 {"signature", 0x32000, 0x00400},
298 {"os-image", 0x40000, 0x1c0000},
299 {"file-system", 0x200000, 0x5b0000},
300 {"soft-version", 0x7b0000, 0x00100},
301 {"support-list", 0x7b1000, 0x00400},
302 {"user-config", 0x7c0000, 0x10000},
303 {"default-config", 0x7d0000, 0x10000},
304 {"log", 0x7e0000, 0x10000},
305 {"radio", 0x7f0000, 0x10000},
306 {NULL, 0, 0}
307 },
308
309 .first_sysupgrade_partition = "os-image",
310 .last_sysupgrade_partition = "support-list",
311 },
312
313 /** Firmware layout for the CPE510 V2 */
314 {
315 .id = "CPE510V2",
316 .vendor = "CPE510(TP-LINK|UN|N300-5):2.0\r\n",
317 .support_list =
318 "SupportList:\r\n"
319 "CPE510(TP-LINK|EU|N300-5|00000000):2.0\r\n"
320 "CPE510(TP-LINK|EU|N300-5|45550000):2.0\r\n"
321 "CPE510(TP-LINK|EU|N300-5|55530000):2.0\r\n"
322 "CPE510(TP-LINK|UN|N300-5|00000000):2.0\r\n"
323 "CPE510(TP-LINK|UN|N300-5|45550000):2.0\r\n"
324 "CPE510(TP-LINK|UN|N300-5|55530000):2.0\r\n"
325 "CPE510(TP-LINK|US|N300-5|00000000):2.0\r\n"
326 "CPE510(TP-LINK|US|N300-5|45550000):2.0\r\n"
327 "CPE510(TP-LINK|US|N300-5|55530000):2.0\r\n"
328 "CPE510(TP-LINK|UN|N300-5):2.0\r\n"
329 "CPE510(TP-LINK|EU|N300-5):2.0\r\n"
330 "CPE510(TP-LINK|US|N300-5):2.0\r\n",
331 .support_trail = '\xff',
332 .soft_ver = NULL,
333
334 .partitions = {
335 {"fs-uboot", 0x00000, 0x20000},
336 {"partition-table", 0x20000, 0x02000},
337 {"default-mac", 0x30000, 0x00020},
338 {"product-info", 0x31100, 0x00100},
339 {"signature", 0x32000, 0x00400},
340 {"os-image", 0x40000, 0x1c0000},
341 {"file-system", 0x200000, 0x5b0000},
342 {"soft-version", 0x7b0000, 0x00100},
343 {"support-list", 0x7b1000, 0x00400},
344 {"user-config", 0x7c0000, 0x10000},
345 {"default-config", 0x7d0000, 0x10000},
346 {"log", 0x7e0000, 0x10000},
347 {"radio", 0x7f0000, 0x10000},
348 {NULL, 0, 0}
349 },
350
351 .first_sysupgrade_partition = "os-image",
352 .last_sysupgrade_partition = "support-list",
353 },
354
355 {
356 .id = "WBS210",
357 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
358 .support_list =
359 "SupportList:\r\n"
360 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
361 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
362 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
363 .support_trail = '\xff',
364 .soft_ver = NULL,
365
366 .partitions = {
367 {"fs-uboot", 0x00000, 0x20000},
368 {"partition-table", 0x20000, 0x02000},
369 {"default-mac", 0x30000, 0x00020},
370 {"product-info", 0x31100, 0x00100},
371 {"signature", 0x32000, 0x00400},
372 {"os-image", 0x40000, 0x1c0000},
373 {"file-system", 0x200000, 0x5b0000},
374 {"soft-version", 0x7b0000, 0x00100},
375 {"support-list", 0x7b1000, 0x00400},
376 {"user-config", 0x7c0000, 0x10000},
377 {"default-config", 0x7d0000, 0x10000},
378 {"log", 0x7e0000, 0x10000},
379 {"radio", 0x7f0000, 0x10000},
380 {NULL, 0, 0}
381 },
382
383 .first_sysupgrade_partition = "os-image",
384 .last_sysupgrade_partition = "support-list",
385 },
386
387 {
388 .id = "WBS510",
389 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
390 .support_list =
391 "SupportList:\r\n"
392 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
393 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
394 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
395 .support_trail = '\xff',
396 .soft_ver = NULL,
397
398 .partitions = {
399 {"fs-uboot", 0x00000, 0x20000},
400 {"partition-table", 0x20000, 0x02000},
401 {"default-mac", 0x30000, 0x00020},
402 {"product-info", 0x31100, 0x00100},
403 {"signature", 0x32000, 0x00400},
404 {"os-image", 0x40000, 0x1c0000},
405 {"file-system", 0x200000, 0x5b0000},
406 {"soft-version", 0x7b0000, 0x00100},
407 {"support-list", 0x7b1000, 0x00400},
408 {"user-config", 0x7c0000, 0x10000},
409 {"default-config", 0x7d0000, 0x10000},
410 {"log", 0x7e0000, 0x10000},
411 {"radio", 0x7f0000, 0x10000},
412 {NULL, 0, 0}
413 },
414
415 .first_sysupgrade_partition = "os-image",
416 .last_sysupgrade_partition = "support-list",
417 },
418
419 /** Firmware layout for the C2600 */
420 {
421 .id = "C2600",
422 .vendor = "",
423 .support_list =
424 "SupportList:\r\n"
425 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
426 .support_trail = '\x00',
427 .soft_ver = NULL,
428
429 /**
430 We use a bigger os-image partition than the stock images (and thus
431 smaller file-system), as our kernel doesn't fit in the stock firmware's
432 2 MB os-image since kernel 4.14.
433 */
434 .partitions = {
435 {"SBL1", 0x00000, 0x20000},
436 {"MIBIB", 0x20000, 0x20000},
437 {"SBL2", 0x40000, 0x20000},
438 {"SBL3", 0x60000, 0x30000},
439 {"DDRCONFIG", 0x90000, 0x10000},
440 {"SSD", 0xa0000, 0x10000},
441 {"TZ", 0xb0000, 0x30000},
442 {"RPM", 0xe0000, 0x20000},
443 {"fs-uboot", 0x100000, 0x70000},
444 {"uboot-env", 0x170000, 0x40000},
445 {"radio", 0x1b0000, 0x40000},
446 {"os-image", 0x1f0000, 0x400000}, /* Stock: base 0x1f0000 size 0x200000 */
447 {"file-system", 0x5f0000, 0x1900000}, /* Stock: base 0x3f0000 size 0x1b00000 */
448 {"default-mac", 0x1ef0000, 0x00200},
449 {"pin", 0x1ef0200, 0x00200},
450 {"product-info", 0x1ef0400, 0x0fc00},
451 {"partition-table", 0x1f00000, 0x10000},
452 {"soft-version", 0x1f10000, 0x10000},
453 {"support-list", 0x1f20000, 0x10000},
454 {"profile", 0x1f30000, 0x10000},
455 {"default-config", 0x1f40000, 0x10000},
456 {"user-config", 0x1f50000, 0x40000},
457 {"qos-db", 0x1f90000, 0x40000},
458 {"usb-config", 0x1fd0000, 0x10000},
459 {"log", 0x1fe0000, 0x20000},
460 {NULL, 0, 0}
461 },
462
463 .first_sysupgrade_partition = "os-image",
464 .last_sysupgrade_partition = "file-system"
465 },
466
467 /** Firmware layout for the A7-V5 */
468 {
469 .id = "ARCHER-A7-V5",
470 .support_list =
471 "SupportList:\n"
472 "{product_name:Archer A7,product_ver:5.0.0,special_id:45550000}\n"
473 "{product_name:Archer A7,product_ver:5.0.0,special_id:55530000}\n"
474 "{product_name:Archer A7,product_ver:5.0.0,special_id:43410000}\n"
475 "{product_name:Archer A7,product_ver:5.0.0,special_id:4A500000}\n"
476 "{product_name:Archer A7,product_ver:5.0.0,special_id:54570000}\n",
477 .support_trail = '\x00',
478 .soft_ver = "soft_ver:1.0.0\n",
479
480 /* We're using a dynamic kernel/rootfs split here */
481 .partitions = {
482 {"factory-boot", 0x00000, 0x20000},
483 {"fs-uboot", 0x20000, 0x20000},
484 {"firmware", 0x40000, 0xec0000}, /* Stock: name os-image base 0x40000 size 0x120000 */
485 /* Stock: name file-system base 0x160000 size 0xda0000 */
486 {"default-mac", 0xf40000, 0x00200},
487 {"pin", 0xf40200, 0x00200},
488 {"device-id", 0xf40400, 0x00100},
489 {"product-info", 0xf40500, 0x0fb00},
490 {"soft-version", 0xf50000, 0x00100},
491 {"extra-para", 0xf51000, 0x01000},
492 {"support-list", 0xf52000, 0x0a000},
493 {"profile", 0xf5c000, 0x04000},
494 {"default-config", 0xf60000, 0x10000},
495 {"user-config", 0xf70000, 0x40000},
496 {"certificate", 0xfb0000, 0x10000},
497 {"partition-table", 0xfc0000, 0x10000},
498 {"log", 0xfd0000, 0x20000},
499 {"radio", 0xff0000, 0x10000},
500 {NULL, 0, 0}
501 },
502
503 .first_sysupgrade_partition = "os-image",
504 .last_sysupgrade_partition = "file-system",
505 },
506
507 /** Firmware layout for the C2v3 */
508 {
509 .id = "ARCHER-C2-V3",
510 .support_list =
511 "SupportList:\n"
512 "{product_name:ArcherC2,product_ver:3.0.0,special_id:00000000}\n"
513 "{product_name:ArcherC2,product_ver:3.0.0,special_id:55530000}\n"
514 "{product_name:ArcherC2,product_ver:3.0.0,special_id:45550000}\n",
515 .support_trail = '\x00',
516 .soft_ver = "soft_ver:3.0.1\n",
517
518 /** We're using a dynamic kernel/rootfs split here */
519
520 .partitions = {
521 {"factory-boot", 0x00000, 0x20000},
522 {"fs-uboot", 0x20000, 0x10000},
523 {"firmware", 0x30000, 0x7a0000},
524 {"user-config", 0x7d0000, 0x04000},
525 {"default-mac", 0x7e0000, 0x00100},
526 {"device-id", 0x7e0100, 0x00100},
527 {"extra-para", 0x7e0200, 0x00100},
528 {"pin", 0x7e0300, 0x00100},
529 {"support-list", 0x7e0400, 0x00400},
530 {"soft-version", 0x7e0800, 0x00400},
531 {"product-info", 0x7e0c00, 0x01400},
532 {"partition-table", 0x7e2000, 0x01000},
533 {"profile", 0x7e3000, 0x01000},
534 {"default-config", 0x7e4000, 0x04000},
535 {"merge-config", 0x7ec000, 0x02000},
536 {"qos-db", 0x7ee000, 0x02000},
537 {"radio", 0x7f0000, 0x10000},
538 {NULL, 0, 0}
539 },
540
541 .first_sysupgrade_partition = "os-image",
542 .last_sysupgrade_partition = "file-system",
543 },
544
545 /** Firmware layout for the C25v1 */
546 {
547 .id = "ARCHER-C25-V1",
548 .support_list =
549 "SupportList:\n"
550 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
551 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
552 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
553 .support_trail = '\x00',
554 .soft_ver = "soft_ver:1.0.0\n",
555
556 /* We're using a dynamic kernel/rootfs split here */
557 .partitions = {
558 {"factory-boot", 0x00000, 0x20000},
559 {"fs-uboot", 0x20000, 0x10000},
560 {"firmware", 0x30000, 0x7a0000}, /* Stock: name os-image base 0x30000 size 0x100000 */
561 /* Stock: name file-system base 0x130000 size 0x6a0000 */
562 {"user-config", 0x7d0000, 0x04000},
563 {"default-mac", 0x7e0000, 0x00100},
564 {"device-id", 0x7e0100, 0x00100},
565 {"extra-para", 0x7e0200, 0x00100},
566 {"pin", 0x7e0300, 0x00100},
567 {"support-list", 0x7e0400, 0x00400},
568 {"soft-version", 0x7e0800, 0x00400},
569 {"product-info", 0x7e0c00, 0x01400},
570 {"partition-table", 0x7e2000, 0x01000},
571 {"profile", 0x7e3000, 0x01000},
572 {"default-config", 0x7e4000, 0x04000},
573 {"merge-config", 0x7ec000, 0x02000},
574 {"qos-db", 0x7ee000, 0x02000},
575 {"radio", 0x7f0000, 0x10000},
576 {NULL, 0, 0}
577 },
578
579 .first_sysupgrade_partition = "os-image",
580 .last_sysupgrade_partition = "file-system",
581 },
582
583 /** Firmware layout for the C58v1 */
584 {
585 .id = "ARCHER-C58-V1",
586 .vendor = "",
587 .support_list =
588 "SupportList:\r\n"
589 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
590 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
591 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
592 .support_trail = '\x00',
593 .soft_ver = "soft_ver:1.0.0\n",
594
595 .partitions = {
596 {"fs-uboot", 0x00000, 0x10000},
597 {"default-mac", 0x10000, 0x00200},
598 {"pin", 0x10200, 0x00200},
599 {"product-info", 0x10400, 0x00100},
600 {"partition-table", 0x10500, 0x00800},
601 {"soft-version", 0x11300, 0x00200},
602 {"support-list", 0x11500, 0x00100},
603 {"device-id", 0x11600, 0x00100},
604 {"profile", 0x11700, 0x03900},
605 {"default-config", 0x15000, 0x04000},
606 {"user-config", 0x19000, 0x04000},
607 {"firmware", 0x20000, 0x7c8000},
608 {"certyficate", 0x7e8000, 0x08000},
609 {"radio", 0x7f0000, 0x10000},
610 {NULL, 0, 0}
611 },
612
613 .first_sysupgrade_partition = "os-image",
614 .last_sysupgrade_partition = "file-system",
615 },
616
617 /** Firmware layout for the C59v1 */
618 {
619 .id = "ARCHER-C59-V1",
620 .vendor = "",
621 .support_list =
622 "SupportList:\r\n"
623 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
624 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
625 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
626 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
627 .support_trail = '\x00',
628 .soft_ver = "soft_ver:1.0.0\n",
629
630 /* We're using a dynamic kernel/rootfs split here */
631 .partitions = {
632 {"fs-uboot", 0x00000, 0x10000},
633 {"default-mac", 0x10000, 0x00200},
634 {"pin", 0x10200, 0x00200},
635 {"device-id", 0x10400, 0x00100},
636 {"product-info", 0x10500, 0x0fb00},
637 {"firmware", 0x20000, 0xe30000},
638 {"partition-table", 0xe50000, 0x10000},
639 {"soft-version", 0xe60000, 0x10000},
640 {"support-list", 0xe70000, 0x10000},
641 {"profile", 0xe80000, 0x10000},
642 {"default-config", 0xe90000, 0x10000},
643 {"user-config", 0xea0000, 0x40000},
644 {"usb-config", 0xee0000, 0x10000},
645 {"certificate", 0xef0000, 0x10000},
646 {"qos-db", 0xf00000, 0x40000},
647 {"log", 0xfe0000, 0x10000},
648 {"radio", 0xff0000, 0x10000},
649 {NULL, 0, 0}
650 },
651
652 .first_sysupgrade_partition = "os-image",
653 .last_sysupgrade_partition = "file-system",
654 },
655
656 /** Firmware layout for the C59v2 */
657 {
658 .id = "ARCHER-C59-V2",
659 .vendor = "",
660 .support_list =
661 "SupportList:\r\n"
662 "{product_name:Archer C59,product_ver:2.0.0,special_id:00000000}\r\n"
663 "{product_name:Archer C59,product_ver:2.0.0,special_id:45550000}\r\n"
664 "{product_name:Archer C59,product_ver:2.0.0,special_id:55530000}\r\n",
665 .support_trail = '\x00',
666 .soft_ver = "soft_ver:2.0.0 Build 20161206 rel.7303\n",
667
668 /** We're using a dynamic kernel/rootfs split here */
669 .partitions = {
670 {"factory-boot", 0x00000, 0x20000},
671 {"fs-uboot", 0x20000, 0x10000},
672 {"default-mac", 0x30000, 0x00200},
673 {"pin", 0x30200, 0x00200},
674 {"device-id", 0x30400, 0x00100},
675 {"product-info", 0x30500, 0x0fb00},
676 {"firmware", 0x40000, 0xe10000},
677 {"partition-table", 0xe50000, 0x10000},
678 {"soft-version", 0xe60000, 0x10000},
679 {"support-list", 0xe70000, 0x10000},
680 {"profile", 0xe80000, 0x10000},
681 {"default-config", 0xe90000, 0x10000},
682 {"user-config", 0xea0000, 0x40000},
683 {"usb-config", 0xee0000, 0x10000},
684 {"certificate", 0xef0000, 0x10000},
685 {"extra-para", 0xf00000, 0x10000},
686 {"qos-db", 0xf10000, 0x30000},
687 {"log", 0xfe0000, 0x10000},
688 {"radio", 0xff0000, 0x10000},
689 {NULL, 0, 0}
690 },
691
692 .first_sysupgrade_partition = "os-image",
693 .last_sysupgrade_partition = "file-system",
694 },
695
696 /** Firmware layout for the C6v2 */
697 {
698 .id = "ARCHER-C6-V2",
699 .vendor = "",
700 .support_list =
701 "SupportList:\r\n"
702 "{product_name:Archer C6,product_ver:2.0.0,special_id:45550000}\r\n"
703 "{product_name:Archer C6,product_ver:2.0.0,special_id:52550000}\r\n"
704 "{product_name:Archer C6,product_ver:2.0.0,special_id:4A500000}\r\n",
705 .support_trail = '\x00',
706 .soft_ver = "soft_ver:1.1.1\n",
707
708 .partitions = {
709 {"fs-uboot", 0x00000, 0x20000},
710 {"default-mac", 0x20000, 0x00200},
711 {"pin", 0x20200, 0x00100},
712 {"product-info", 0x20300, 0x00200},
713 {"device-id", 0x20500, 0x0fb00},
714 {"firmware", 0x30000, 0x7a9400},
715 {"soft-version", 0x7d9400, 0x00100},
716 {"extra-para", 0x7d9500, 0x00100},
717 {"support-list", 0x7d9600, 0x00200},
718 {"profile", 0x7d9800, 0x03000},
719 {"default-config", 0x7dc800, 0x03000},
720 {"partition-table", 0x7df800, 0x00800},
721 {"user-config", 0x7e0000, 0x0c000},
722 {"certificate", 0x7ec000, 0x04000},
723 {"radio", 0x7f0000, 0x10000},
724 {NULL, 0, 0}
725 },
726
727 .first_sysupgrade_partition = "os-image",
728 .last_sysupgrade_partition = "file-system",
729 },
730
731
732 /** Firmware layout for the C60v1 */
733 {
734 .id = "ARCHER-C60-V1",
735 .vendor = "",
736 .support_list =
737 "SupportList:\r\n"
738 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
739 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
740 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
741 .support_trail = '\x00',
742 .soft_ver = "soft_ver:1.0.0\n",
743
744 .partitions = {
745 {"fs-uboot", 0x00000, 0x10000},
746 {"default-mac", 0x10000, 0x00200},
747 {"pin", 0x10200, 0x00200},
748 {"product-info", 0x10400, 0x00100},
749 {"partition-table", 0x10500, 0x00800},
750 {"soft-version", 0x11300, 0x00200},
751 {"support-list", 0x11500, 0x00100},
752 {"device-id", 0x11600, 0x00100},
753 {"profile", 0x11700, 0x03900},
754 {"default-config", 0x15000, 0x04000},
755 {"user-config", 0x19000, 0x04000},
756 {"firmware", 0x20000, 0x7c8000},
757 {"certyficate", 0x7e8000, 0x08000},
758 {"radio", 0x7f0000, 0x10000},
759 {NULL, 0, 0}
760 },
761
762 .first_sysupgrade_partition = "os-image",
763 .last_sysupgrade_partition = "file-system",
764 },
765
766 /** Firmware layout for the C60v2 */
767 {
768 .id = "ARCHER-C60-V2",
769 .vendor = "",
770 .support_list =
771 "SupportList:\r\n"
772 "{product_name:Archer C60,product_ver:2.0.0,special_id:42520000}\r\n"
773 "{product_name:Archer C60,product_ver:2.0.0,special_id:45550000}\r\n"
774 "{product_name:Archer C60,product_ver:2.0.0,special_id:55530000}\r\n",
775 .support_trail = '\x00',
776 .soft_ver = "soft_ver:2.0.0\n",
777
778 .partitions = {
779 {"factory-boot", 0x00000, 0x1fb00},
780 {"default-mac", 0x1fb00, 0x00200},
781 {"pin", 0x1fd00, 0x00100},
782 {"product-info", 0x1fe00, 0x00100},
783 {"device-id", 0x1ff00, 0x00100},
784 {"fs-uboot", 0x20000, 0x10000},
785 {"firmware", 0x30000, 0x7a0000},
786 {"soft-version", 0x7d9500, 0x00100},
787 {"support-list", 0x7d9600, 0x00100},
788 {"extra-para", 0x7d9700, 0x00100},
789 {"profile", 0x7d9800, 0x03000},
790 {"default-config", 0x7dc800, 0x03000},
791 {"partition-table", 0x7df800, 0x00800},
792 {"user-config", 0x7e0000, 0x0c000},
793 {"certificate", 0x7ec000, 0x04000},
794 {"radio", 0x7f0000, 0x10000},
795 {NULL, 0, 0}
796 },
797
798 .first_sysupgrade_partition = "os-image",
799 .last_sysupgrade_partition = "file-system",
800 },
801
802 /** Firmware layout for the C60v3 */
803 {
804 .id = "ARCHER-C60-V3",
805 .vendor = "",
806 .support_list =
807 "SupportList:\r\n"
808 "{product_name:Archer C60,product_ver:3.0.0,special_id:42520000}\r\n"
809 "{product_name:Archer C60,product_ver:3.0.0,special_id:45550000}\r\n"
810 "{product_name:Archer C60,product_ver:3.0.0,special_id:55530000}\r\n",
811 .support_trail = '\x00',
812 .soft_ver = "soft_ver:3.0.0\n",
813
814 .partitions = {
815 {"factory-boot", 0x00000, 0x1fb00},
816 {"default-mac", 0x1fb00, 0x00200},
817 {"pin", 0x1fd00, 0x00100},
818 {"product-info", 0x1fe00, 0x00100},
819 {"device-id", 0x1ff00, 0x00100},
820 {"fs-uboot", 0x20000, 0x10000},
821 {"firmware", 0x30000, 0x7a0000},
822 {"soft-version", 0x7d9500, 0x00100},
823 {"support-list", 0x7d9600, 0x00100},
824 {"extra-para", 0x7d9700, 0x00100},
825 {"profile", 0x7d9800, 0x03000},
826 {"default-config", 0x7dc800, 0x03000},
827 {"partition-table", 0x7df800, 0x00800},
828 {"user-config", 0x7e0000, 0x0c000},
829 {"certificate", 0x7ec000, 0x04000},
830 {"radio", 0x7f0000, 0x10000},
831 {NULL, 0, 0}
832 },
833
834 .first_sysupgrade_partition = "os-image",
835 .last_sysupgrade_partition = "file-system",
836 },
837
838 /** Firmware layout for the C5 */
839 {
840 .id = "ARCHER-C5-V2",
841 .vendor = "",
842 .support_list =
843 "SupportList:\r\n"
844 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
845 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
846 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
847 .support_trail = '\x00',
848 .soft_ver = NULL,
849
850 .partitions = {
851 {"fs-uboot", 0x00000, 0x40000},
852 {"os-image", 0x40000, 0x200000},
853 {"file-system", 0x240000, 0xc00000},
854 {"default-mac", 0xe40000, 0x00200},
855 {"pin", 0xe40200, 0x00200},
856 {"product-info", 0xe40400, 0x00200},
857 {"partition-table", 0xe50000, 0x10000},
858 {"soft-version", 0xe60000, 0x00200},
859 {"support-list", 0xe61000, 0x0f000},
860 {"profile", 0xe70000, 0x10000},
861 {"default-config", 0xe80000, 0x10000},
862 {"user-config", 0xe90000, 0x50000},
863 {"log", 0xee0000, 0x100000},
864 {"radio_bk", 0xfe0000, 0x10000},
865 {"radio", 0xff0000, 0x10000},
866 {NULL, 0, 0}
867 },
868
869 .first_sysupgrade_partition = "os-image",
870 .last_sysupgrade_partition = "file-system"
871 },
872
873 /** Firmware layout for the C7 */
874 {
875 .id = "ARCHER-C7-V4",
876 .support_list =
877 "SupportList:\n"
878 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
879 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
880 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
881 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
882 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
883 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
884 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
885 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
886 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
887 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
888 .support_trail = '\x00',
889 .soft_ver = "soft_ver:1.0.0\n",
890
891 /* We're using a dynamic kernel/rootfs split here */
892 .partitions = {
893 {"factory-boot", 0x00000, 0x20000},
894 {"fs-uboot", 0x20000, 0x20000},
895 {"firmware", 0x40000, 0xEC0000}, /* Stock: name os-image base 0x40000 size 0x120000 */
896 /* Stock: name file-system base 0x160000 size 0xda0000 */
897 {"default-mac", 0xf00000, 0x00200},
898 {"pin", 0xf00200, 0x00200},
899 {"device-id", 0xf00400, 0x00100},
900 {"product-info", 0xf00500, 0x0fb00},
901 {"soft-version", 0xf10000, 0x00100},
902 {"extra-para", 0xf11000, 0x01000},
903 {"support-list", 0xf12000, 0x0a000},
904 {"profile", 0xf1c000, 0x04000},
905 {"default-config", 0xf20000, 0x10000},
906 {"user-config", 0xf30000, 0x40000},
907 {"qos-db", 0xf70000, 0x40000},
908 {"certificate", 0xfb0000, 0x10000},
909 {"partition-table", 0xfc0000, 0x10000},
910 {"log", 0xfd0000, 0x20000},
911 {"radio", 0xff0000, 0x10000},
912 {NULL, 0, 0}
913 },
914
915 .first_sysupgrade_partition = "os-image",
916 .last_sysupgrade_partition = "file-system",
917 },
918
919 /** Firmware layout for the C7 v5*/
920 {
921 .id = "ARCHER-C7-V5",
922 .support_list =
923 "SupportList:\n"
924 "{product_name:Archer C7,product_ver:5.0.0,special_id:00000000}\n"
925 "{product_name:Archer C7,product_ver:5.0.0,special_id:45550000}\n"
926 "{product_name:Archer C7,product_ver:5.0.0,special_id:55530000}\n"
927 "{product_name:Archer C7,product_ver:5.0.0,special_id:43410000}\n"
928 "{product_name:Archer C7,product_ver:5.0.0,special_id:4A500000}\n"
929 "{product_name:Archer C7,product_ver:5.0.0,special_id:54570000}\n"
930 "{product_name:Archer C7,product_ver:5.0.0,special_id:52550000}\n"
931 "{product_name:Archer C7,product_ver:5.0.0,special_id:4B520000}\n",
932
933 .support_trail = '\x00',
934 .soft_ver = "soft_ver:1.0.0\n",
935
936 /* We're using a dynamic kernel/rootfs split here */
937 .partitions = {
938 {"factory-boot", 0x00000, 0x20000},
939 {"fs-uboot", 0x20000, 0x20000},
940 {"partition-table", 0x40000, 0x10000},
941 {"radio", 0x50000, 0x10000},
942 {"default-mac", 0x60000, 0x00200},
943 {"pin", 0x60200, 0x00200},
944 {"device-id", 0x60400, 0x00100},
945 {"product-info", 0x60500, 0x0fb00},
946 {"soft-version", 0x70000, 0x01000},
947 {"extra-para", 0x71000, 0x01000},
948 {"support-list", 0x72000, 0x0a000},
949 {"profile", 0x7c000, 0x04000},
950 {"user-config", 0x80000, 0x40000},
951
952
953 {"firmware", 0xc0000, 0xf00000}, /* Stock: name os-image base 0xc0000 size 0x120000 */
954 /* Stock: name file-system base 0x1e0000 size 0xde0000 */
955
956 {"log", 0xfc0000, 0x20000},
957 {"certificate", 0xfe0000, 0x10000},
958 {"default-config", 0xff0000, 0x10000},
959 {NULL, 0, 0}
960
961 },
962
963 .first_sysupgrade_partition = "os-image",
964 .last_sysupgrade_partition = "file-system",
965 },
966
967 /** Firmware layout for the C9 */
968 {
969 .id = "ARCHERC9",
970 .vendor = "",
971 .support_list =
972 "SupportList:\n"
973 "{product_name:ArcherC9,"
974 "product_ver:1.0.0,"
975 "special_id:00000000}\n",
976 .support_trail = '\x00',
977 .soft_ver = NULL,
978
979 .partitions = {
980 {"fs-uboot", 0x00000, 0x40000},
981 {"os-image", 0x40000, 0x200000},
982 {"file-system", 0x240000, 0xc00000},
983 {"default-mac", 0xe40000, 0x00200},
984 {"pin", 0xe40200, 0x00200},
985 {"product-info", 0xe40400, 0x00200},
986 {"partition-table", 0xe50000, 0x10000},
987 {"soft-version", 0xe60000, 0x00200},
988 {"support-list", 0xe61000, 0x0f000},
989 {"profile", 0xe70000, 0x10000},
990 {"default-config", 0xe80000, 0x10000},
991 {"user-config", 0xe90000, 0x50000},
992 {"log", 0xee0000, 0x100000},
993 {"radio_bk", 0xfe0000, 0x10000},
994 {"radio", 0xff0000, 0x10000},
995 {NULL, 0, 0}
996 },
997
998 .first_sysupgrade_partition = "os-image",
999 .last_sysupgrade_partition = "file-system"
1000 },
1001
1002 /** Firmware layout for the EAP120 */
1003 {
1004 .id = "EAP120",
1005 .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
1006 .support_list =
1007 "SupportList:\r\n"
1008 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
1009 .support_trail = '\xff',
1010 .soft_ver = NULL,
1011
1012 .partitions = {
1013 {"fs-uboot", 0x00000, 0x20000},
1014 {"partition-table", 0x20000, 0x02000},
1015 {"default-mac", 0x30000, 0x00020},
1016 {"support-list", 0x31000, 0x00100},
1017 {"product-info", 0x31100, 0x00100},
1018 {"soft-version", 0x32000, 0x00100},
1019 {"os-image", 0x40000, 0x180000},
1020 {"file-system", 0x1c0000, 0x600000},
1021 {"user-config", 0x7c0000, 0x10000},
1022 {"backup-config", 0x7d0000, 0x10000},
1023 {"log", 0x7e0000, 0x10000},
1024 {"radio", 0x7f0000, 0x10000},
1025 {NULL, 0, 0}
1026 },
1027
1028 .first_sysupgrade_partition = "os-image",
1029 .last_sysupgrade_partition = "file-system"
1030 },
1031
1032 /** Firmware layout for the TL-WA850RE v2 */
1033 {
1034 .id = "TLWA850REV2",
1035 .vendor = "",
1036 .support_list =
1037 "SupportList:\n"
1038 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
1039 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
1040 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
1041 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
1042 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
1043 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
1044 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
1045 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
1046 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
1047 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
1048 .support_trail = '\x00',
1049 .soft_ver = NULL,
1050
1051 /**
1052 576KB were moved from file-system to os-image
1053 in comparison to the stock image
1054 */
1055 .partitions = {
1056 {"fs-uboot", 0x00000, 0x20000},
1057 {"os-image", 0x20000, 0x150000},
1058 {"file-system", 0x170000, 0x240000},
1059 {"partition-table", 0x3b0000, 0x02000},
1060 {"default-mac", 0x3c0000, 0x00020},
1061 {"pin", 0x3c0100, 0x00020},
1062 {"product-info", 0x3c1000, 0x01000},
1063 {"soft-version", 0x3c2000, 0x00100},
1064 {"support-list", 0x3c3000, 0x01000},
1065 {"profile", 0x3c4000, 0x08000},
1066 {"user-config", 0x3d0000, 0x10000},
1067 {"default-config", 0x3e0000, 0x10000},
1068 {"radio", 0x3f0000, 0x10000},
1069 {NULL, 0, 0}
1070 },
1071
1072 .first_sysupgrade_partition = "os-image",
1073 .last_sysupgrade_partition = "file-system"
1074 },
1075
1076 /** Firmware layout for the TL-WA855RE v1 */
1077 {
1078 .id = "TLWA855REV1",
1079 .vendor = "",
1080 .support_list =
1081 "SupportList:\n"
1082 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
1083 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
1084 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
1085 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
1086 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
1087 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
1088 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
1089 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
1090 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
1091 .support_trail = '\x00',
1092 .soft_ver = NULL,
1093
1094 .partitions = {
1095 {"fs-uboot", 0x00000, 0x20000},
1096 {"os-image", 0x20000, 0x150000},
1097 {"file-system", 0x170000, 0x240000},
1098 {"partition-table", 0x3b0000, 0x02000},
1099 {"default-mac", 0x3c0000, 0x00020},
1100 {"pin", 0x3c0100, 0x00020},
1101 {"product-info", 0x3c1000, 0x01000},
1102 {"soft-version", 0x3c2000, 0x00100},
1103 {"support-list", 0x3c3000, 0x01000},
1104 {"profile", 0x3c4000, 0x08000},
1105 {"user-config", 0x3d0000, 0x10000},
1106 {"default-config", 0x3e0000, 0x10000},
1107 {"radio", 0x3f0000, 0x10000},
1108 {NULL, 0, 0}
1109 },
1110
1111 .first_sysupgrade_partition = "os-image",
1112 .last_sysupgrade_partition = "file-system"
1113 },
1114
1115 /** Firmware layout for the TL-WR1043 v5 */
1116 {
1117 .id = "TLWR1043NV5",
1118 .vendor = "",
1119 .support_list =
1120 "SupportList:\n"
1121 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
1122 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
1123 .support_trail = '\x00',
1124 .soft_ver = "soft_ver:1.0.0\n",
1125 .partitions = {
1126 {"factory-boot", 0x00000, 0x20000},
1127 {"fs-uboot", 0x20000, 0x20000},
1128 {"firmware", 0x40000, 0xec0000},
1129 {"default-mac", 0xf00000, 0x00200},
1130 {"pin", 0xf00200, 0x00200},
1131 {"device-id", 0xf00400, 0x00100},
1132 {"product-info", 0xf00500, 0x0fb00},
1133 {"soft-version", 0xf10000, 0x01000},
1134 {"extra-para", 0xf11000, 0x01000},
1135 {"support-list", 0xf12000, 0x0a000},
1136 {"profile", 0xf1c000, 0x04000},
1137 {"default-config", 0xf20000, 0x10000},
1138 {"user-config", 0xf30000, 0x40000},
1139 {"qos-db", 0xf70000, 0x40000},
1140 {"certificate", 0xfb0000, 0x10000},
1141 {"partition-table", 0xfc0000, 0x10000},
1142 {"log", 0xfd0000, 0x20000},
1143 {"radio", 0xff0000, 0x10000},
1144 {NULL, 0, 0}
1145 },
1146 .first_sysupgrade_partition = "os-image",
1147 .last_sysupgrade_partition = "file-system"
1148 },
1149
1150 /** Firmware layout for the TL-WR1043 v4 */
1151 {
1152 .id = "TLWR1043NDV4",
1153 .vendor = "",
1154 .support_list =
1155 "SupportList:\n"
1156 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
1157 .support_trail = '\x00',
1158 .soft_ver = NULL,
1159
1160 /* We're using a dynamic kernel/rootfs split here */
1161 .partitions = {
1162 {"fs-uboot", 0x00000, 0x20000},
1163 {"firmware", 0x20000, 0xf30000},
1164 {"default-mac", 0xf50000, 0x00200},
1165 {"pin", 0xf50200, 0x00200},
1166 {"product-info", 0xf50400, 0x0fc00},
1167 {"soft-version", 0xf60000, 0x0b000},
1168 {"support-list", 0xf6b000, 0x04000},
1169 {"profile", 0xf70000, 0x04000},
1170 {"default-config", 0xf74000, 0x0b000},
1171 {"user-config", 0xf80000, 0x40000},
1172 {"partition-table", 0xfc0000, 0x10000},
1173 {"log", 0xfd0000, 0x20000},
1174 {"radio", 0xff0000, 0x10000},
1175 {NULL, 0, 0}
1176 },
1177
1178 .first_sysupgrade_partition = "os-image",
1179 .last_sysupgrade_partition = "file-system"
1180 },
1181
1182 /** Firmware layout for the TL-WR902AC v1 */
1183 {
1184 .id = "TL-WR902AC-V1",
1185 .vendor = "",
1186 .support_list =
1187 "SupportList:\n"
1188 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
1189 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
1190 .support_trail = '\x00',
1191 .soft_ver = NULL,
1192
1193 /**
1194 384KB were moved from file-system to os-image
1195 in comparison to the stock image
1196 */
1197 .partitions = {
1198 {"fs-uboot", 0x00000, 0x20000},
1199 {"firmware", 0x20000, 0x730000},
1200 {"default-mac", 0x750000, 0x00200},
1201 {"pin", 0x750200, 0x00200},
1202 {"product-info", 0x750400, 0x0fc00},
1203 {"soft-version", 0x760000, 0x0b000},
1204 {"support-list", 0x76b000, 0x04000},
1205 {"profile", 0x770000, 0x04000},
1206 {"default-config", 0x774000, 0x0b000},
1207 {"user-config", 0x780000, 0x40000},
1208 {"partition-table", 0x7c0000, 0x10000},
1209 {"log", 0x7d0000, 0x20000},
1210 {"radio", 0x7f0000, 0x10000},
1211 {NULL, 0, 0}
1212 },
1213
1214 .first_sysupgrade_partition = "os-image",
1215 .last_sysupgrade_partition = "file-system",
1216 },
1217
1218 /** Firmware layout for the TL-WR942N V1 */
1219 {
1220 .id = "TLWR942NV1",
1221 .vendor = "",
1222 .support_list =
1223 "SupportList:\r\n"
1224 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
1225 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
1226 .support_trail = '\x00',
1227 .soft_ver = NULL,
1228
1229 .partitions = {
1230 {"fs-uboot", 0x00000, 0x20000},
1231 {"firmware", 0x20000, 0xe20000},
1232 {"default-mac", 0xe40000, 0x00200},
1233 {"pin", 0xe40200, 0x00200},
1234 {"product-info", 0xe40400, 0x0fc00},
1235 {"partition-table", 0xe50000, 0x10000},
1236 {"soft-version", 0xe60000, 0x10000},
1237 {"support-list", 0xe70000, 0x10000},
1238 {"profile", 0xe80000, 0x10000},
1239 {"default-config", 0xe90000, 0x10000},
1240 {"user-config", 0xea0000, 0x40000},
1241 {"qos-db", 0xee0000, 0x40000},
1242 {"certificate", 0xf20000, 0x10000},
1243 {"usb-config", 0xfb0000, 0x10000},
1244 {"log", 0xfc0000, 0x20000},
1245 {"radio-bk", 0xfe0000, 0x10000},
1246 {"radio", 0xff0000, 0x10000},
1247 {NULL, 0, 0}
1248 },
1249
1250 .first_sysupgrade_partition = "os-image",
1251 .last_sysupgrade_partition = "file-system",
1252 },
1253
1254 /** Firmware layout for the RE350 v1 */
1255 {
1256 .id = "RE350-V1",
1257 .vendor = "",
1258 .support_list =
1259 "SupportList:\n"
1260 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
1261 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
1262 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
1263 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
1264 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
1265 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
1266 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
1267 .support_trail = '\x00',
1268 .soft_ver = NULL,
1269
1270 /** We're using a dynamic kernel/rootfs split here */
1271 .partitions = {
1272 {"fs-uboot", 0x00000, 0x20000},
1273 {"firmware", 0x20000, 0x5e0000},
1274 {"partition-table", 0x600000, 0x02000},
1275 {"default-mac", 0x610000, 0x00020},
1276 {"pin", 0x610100, 0x00020},
1277 {"product-info", 0x611100, 0x01000},
1278 {"soft-version", 0x620000, 0x01000},
1279 {"support-list", 0x621000, 0x01000},
1280 {"profile", 0x622000, 0x08000},
1281 {"user-config", 0x630000, 0x10000},
1282 {"default-config", 0x640000, 0x10000},
1283 {"radio", 0x7f0000, 0x10000},
1284 {NULL, 0, 0}
1285 },
1286
1287 .first_sysupgrade_partition = "os-image",
1288 .last_sysupgrade_partition = "file-system"
1289 },
1290
1291 /** Firmware layout for the RE350K v1 */
1292 {
1293 .id = "RE350K-V1",
1294 .vendor = "",
1295 .support_list =
1296 "SupportList:\n"
1297 "{product_name:RE350K,product_ver:1.0.0,special_id:00000000,product_region:US}\n",
1298 .support_trail = '\x00',
1299 .soft_ver = NULL,
1300
1301 /** We're using a dynamic kernel/rootfs split here */
1302 .partitions = {
1303 {"fs-uboot", 0x00000, 0x20000},
1304 {"firmware", 0x20000, 0xd70000},
1305 {"partition-table", 0xd90000, 0x02000},
1306 {"default-mac", 0xda0000, 0x00020},
1307 {"pin", 0xda0100, 0x00020},
1308 {"product-info", 0xda1100, 0x01000},
1309 {"soft-version", 0xdb0000, 0x01000},
1310 {"support-list", 0xdb1000, 0x01000},
1311 {"profile", 0xdb2000, 0x08000},
1312 {"user-config", 0xdc0000, 0x10000},
1313 {"default-config", 0xdd0000, 0x10000},
1314 {"device-id", 0xde0000, 0x00108},
1315 {"radio", 0xff0000, 0x10000},
1316 {NULL, 0, 0}
1317 },
1318
1319 .first_sysupgrade_partition = "os-image",
1320 .last_sysupgrade_partition = "file-system"
1321 },
1322
1323 /** Firmware layout for the RE355 */
1324 {
1325 .id = "RE355",
1326 .vendor = "",
1327 .support_list =
1328 "SupportList:\r\n"
1329 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
1330 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
1331 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
1332 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
1333 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
1334 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
1335 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
1336 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
1337 .support_trail = '\x00',
1338 .soft_ver = NULL,
1339
1340 /* We're using a dynamic kernel/rootfs split here */
1341 .partitions = {
1342 {"fs-uboot", 0x00000, 0x20000},
1343 {"firmware", 0x20000, 0x5e0000},
1344 {"partition-table", 0x600000, 0x02000},
1345 {"default-mac", 0x610000, 0x00020},
1346 {"pin", 0x610100, 0x00020},
1347 {"product-info", 0x611100, 0x01000},
1348 {"soft-version", 0x620000, 0x01000},
1349 {"support-list", 0x621000, 0x01000},
1350 {"profile", 0x622000, 0x08000},
1351 {"user-config", 0x630000, 0x10000},
1352 {"default-config", 0x640000, 0x10000},
1353 {"radio", 0x7f0000, 0x10000},
1354 {NULL, 0, 0}
1355 },
1356
1357 .first_sysupgrade_partition = "os-image",
1358 .last_sysupgrade_partition = "file-system"
1359 },
1360
1361 /** Firmware layout for the RE450 */
1362 {
1363 .id = "RE450",
1364 .vendor = "",
1365 .support_list =
1366 "SupportList:\r\n"
1367 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
1368 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
1369 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
1370 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
1371 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
1372 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
1373 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
1374 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
1375 .support_trail = '\x00',
1376 .soft_ver = NULL,
1377
1378 /** We're using a dynamic kernel/rootfs split here */
1379 .partitions = {
1380 {"fs-uboot", 0x00000, 0x20000},
1381 {"firmware", 0x20000, 0x5e0000},
1382 {"partition-table", 0x600000, 0x02000},
1383 {"default-mac", 0x610000, 0x00020},
1384 {"pin", 0x610100, 0x00020},
1385 {"product-info", 0x611100, 0x01000},
1386 {"soft-version", 0x620000, 0x01000},
1387 {"support-list", 0x621000, 0x01000},
1388 {"profile", 0x622000, 0x08000},
1389 {"user-config", 0x630000, 0x10000},
1390 {"default-config", 0x640000, 0x10000},
1391 {"radio", 0x7f0000, 0x10000},
1392 {NULL, 0, 0}
1393 },
1394
1395 .first_sysupgrade_partition = "os-image",
1396 .last_sysupgrade_partition = "file-system"
1397 },
1398
1399 /** Firmware layout for the RE450 v2 */
1400 {
1401 .id = "RE450-V2",
1402 .vendor = "",
1403 .support_list =
1404 "SupportList:\r\n"
1405 "{product_name:RE450,product_ver:2.0.0,special_id:00000000}\r\n"
1406 "{product_name:RE450,product_ver:2.0.0,special_id:55530000}\r\n"
1407 "{product_name:RE450,product_ver:2.0.0,special_id:45550000}\r\n"
1408 "{product_name:RE450,product_ver:2.0.0,special_id:4A500000}\r\n"
1409 "{product_name:RE450,product_ver:2.0.0,special_id:43410000}\r\n"
1410 "{product_name:RE450,product_ver:2.0.0,special_id:41550000}\r\n"
1411 "{product_name:RE450,product_ver:2.0.0,special_id:41530000}\r\n"
1412 "{product_name:RE450,product_ver:2.0.0,special_id:4B520000}\r\n"
1413 "{product_name:RE450,product_ver:2.0.0,special_id:42520000}\r\n",
1414 .support_trail = '\x00',
1415 .soft_ver = NULL,
1416
1417 /* We're using a dynamic kernel/rootfs split here */
1418 .partitions = {
1419 {"fs-uboot", 0x00000, 0x20000},
1420 {"firmware", 0x20000, 0x5e0000},
1421 {"partition-table", 0x600000, 0x02000},
1422 {"default-mac", 0x610000, 0x00020},
1423 {"pin", 0x610100, 0x00020},
1424 {"product-info", 0x611100, 0x01000},
1425 {"soft-version", 0x620000, 0x01000},
1426 {"support-list", 0x621000, 0x01000},
1427 {"profile", 0x622000, 0x08000},
1428 {"user-config", 0x630000, 0x10000},
1429 {"default-config", 0x640000, 0x10000},
1430 {"radio", 0x7f0000, 0x10000},
1431
1432 {NULL, 0, 0}
1433 },
1434
1435 .first_sysupgrade_partition = "os-image",
1436 .last_sysupgrade_partition = "file-system"
1437 },
1438
1439 {}
1440 };
1441
1442 #define error(_ret, _errno, _str, ...) \
1443 do { \
1444 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1445 strerror(_errno)); \
1446 if (_ret) \
1447 exit(_ret); \
1448 } while (0)
1449
1450
1451 /** Stores a uint32 as big endian */
1452 static inline void put32(uint8_t *buf, uint32_t val) {
1453 buf[0] = val >> 24;
1454 buf[1] = val >> 16;
1455 buf[2] = val >> 8;
1456 buf[3] = val;
1457 }
1458
1459 /** Allocates a new image partition */
1460 static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
1461 struct image_partition_entry entry = {name, len, malloc(len)};
1462 if (!entry.data)
1463 error(1, errno, "malloc");
1464
1465 return entry;
1466 }
1467
1468 /** Frees an image partition */
1469 static void free_image_partition(struct image_partition_entry entry) {
1470 free(entry.data);
1471 }
1472
1473 static time_t source_date_epoch = -1;
1474 static void set_source_date_epoch() {
1475 char *env = getenv("SOURCE_DATE_EPOCH");
1476 char *endptr = env;
1477 errno = 0;
1478 if (env && *env) {
1479 source_date_epoch = strtoull(env, &endptr, 10);
1480 if (errno || (endptr && *endptr != '\0')) {
1481 fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
1482 exit(1);
1483 }
1484 }
1485 }
1486
1487 /** Generates the partition-table partition */
1488 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
1489 struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
1490
1491 char *s = (char *)entry.data, *end = (char *)(s+entry.size);
1492
1493 *(s++) = 0x00;
1494 *(s++) = 0x04;
1495 *(s++) = 0x00;
1496 *(s++) = 0x00;
1497
1498 size_t i;
1499 for (i = 0; p[i].name; i++) {
1500 size_t len = end-s;
1501 size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
1502
1503 if (w > len-1)
1504 error(1, 0, "flash partition table overflow?");
1505
1506 s += w;
1507 }
1508
1509 s++;
1510
1511 memset(s, 0xff, end-s);
1512
1513 return entry;
1514 }
1515
1516
1517 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1518 static inline uint8_t bcd(uint8_t v) {
1519 return 0x10 * (v/10) + v%10;
1520 }
1521
1522
1523 /** Generates the soft-version partition */
1524 static struct image_partition_entry make_soft_version(uint32_t rev) {
1525 struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
1526 struct soft_version *s = (struct soft_version *)entry.data;
1527
1528 time_t t;
1529
1530 if (source_date_epoch != -1)
1531 t = source_date_epoch;
1532 else if (time(&t) == (time_t)(-1))
1533 error(1, errno, "time");
1534
1535 struct tm *tm = localtime(&t);
1536
1537 s->magic = htonl(0x0000000c);
1538 s->zero = 0;
1539 s->pad1 = 0xff;
1540
1541 s->version_major = 0;
1542 s->version_minor = 0;
1543 s->version_patch = 0;
1544
1545 s->year_hi = bcd((1900+tm->tm_year)/100);
1546 s->year_lo = bcd(tm->tm_year%100);
1547 s->month = bcd(tm->tm_mon+1);
1548 s->day = bcd(tm->tm_mday);
1549 s->rev = htonl(rev);
1550
1551 s->pad2 = 0xff;
1552
1553 return entry;
1554 }
1555
1556 static struct image_partition_entry make_soft_version_from_string(const char *soft_ver) {
1557 /** String length _including_ the terminating zero byte */
1558 uint32_t ver_len = strlen(soft_ver) + 1;
1559 /** Partition contains 64 bit header, the version string, and one additional null byte */
1560 size_t partition_len = 2*sizeof(uint32_t) + ver_len + 1;
1561 struct image_partition_entry entry = alloc_image_partition("soft-version", partition_len);
1562
1563 uint32_t *len = (uint32_t *)entry.data;
1564 len[0] = htonl(ver_len);
1565 len[1] = 0;
1566 memcpy(&len[2], soft_ver, ver_len);
1567
1568 entry.data[partition_len - 1] = 0;
1569
1570 return entry;
1571 }
1572
1573 /** Generates the support-list partition */
1574 static struct image_partition_entry make_support_list(struct device_info *info) {
1575 size_t len = strlen(info->support_list);
1576 struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
1577
1578 put32(entry.data, len);
1579 memset(entry.data+4, 0, 4);
1580 memcpy(entry.data+8, info->support_list, len);
1581 entry.data[len+8] = info->support_trail;
1582
1583 return entry;
1584 }
1585
1586 /** Creates a new image partition with an arbitrary name from a file */
1587 static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof, struct flash_partition_entry *file_system_partition) {
1588 struct stat statbuf;
1589
1590 if (stat(filename, &statbuf) < 0)
1591 error(1, errno, "unable to stat file `%s'", filename);
1592
1593 size_t len = statbuf.st_size;
1594
1595 if (add_jffs2_eof) {
1596 if (file_system_partition)
1597 len = ALIGN(len + file_system_partition->base, 0x10000) + sizeof(jffs2_eof_mark) - file_system_partition->base;
1598 else
1599 len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
1600 }
1601
1602 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1603
1604 FILE *file = fopen(filename, "rb");
1605 if (!file)
1606 error(1, errno, "unable to open file `%s'", filename);
1607
1608 if (fread(entry.data, statbuf.st_size, 1, file) != 1)
1609 error(1, errno, "unable to read file `%s'", filename);
1610
1611 if (add_jffs2_eof) {
1612 uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
1613
1614 memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
1615 memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
1616 }
1617
1618 fclose(file);
1619
1620 return entry;
1621 }
1622
1623 /** Creates a new image partition from arbitrary data */
1624 static struct image_partition_entry put_data(const char *part_name, const char *datain, size_t len) {
1625
1626 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1627
1628 memcpy(entry.data, datain, len);
1629
1630 return entry;
1631 }
1632
1633 /**
1634 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1635
1636 Example image partition table:
1637
1638 fwup-ptn partition-table base 0x00800 size 0x00800
1639 fwup-ptn os-image base 0x01000 size 0x113b45
1640 fwup-ptn file-system base 0x114b45 size 0x1d0004
1641 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1642
1643 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1644 the end of the partition table is marked with a zero byte.
1645
1646 The firmware image must contain at least the partition-table and support-list partitions
1647 to be accepted. There aren't any alignment constraints for the image partitions.
1648
1649 The partition-table partition contains the actual flash layout; partitions
1650 from the image partition table are mapped to the corresponding flash partitions during
1651 the firmware upgrade. The support-list partition contains a list of devices supported by
1652 the firmware image.
1653
1654 The base offsets in the firmware partition table are relative to the end
1655 of the vendor information block, so the partition-table partition will
1656 actually start at offset 0x1814 of the image.
1657
1658 I think partition-table must be the first partition in the firmware image.
1659 */
1660 static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
1661 size_t i, j;
1662 char *image_pt = (char *)buffer, *end = image_pt + 0x800;
1663
1664 size_t base = 0x800;
1665 for (i = 0; parts[i].name; i++) {
1666 for (j = 0; flash_parts[j].name; j++) {
1667 if (!strcmp(flash_parts[j].name, parts[i].name)) {
1668 if (parts[i].size > flash_parts[j].size)
1669 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
1670 break;
1671 }
1672 }
1673
1674 assert(flash_parts[j].name);
1675
1676 memcpy(buffer + base, parts[i].data, parts[i].size);
1677
1678 size_t len = end-image_pt;
1679 size_t w = snprintf(image_pt, len, "fwup-ptn %s base 0x%05x size 0x%05x\t\r\n", parts[i].name, (unsigned)base, (unsigned)parts[i].size);
1680
1681 if (w > len-1)
1682 error(1, 0, "image partition table overflow?");
1683
1684 image_pt += w;
1685
1686 base += parts[i].size;
1687 }
1688 }
1689
1690 /** Generates and writes the image MD5 checksum */
1691 static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
1692 MD5_CTX ctx;
1693
1694 MD5_Init(&ctx);
1695 MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
1696 MD5_Update(&ctx, buffer, len);
1697 MD5_Final(md5, &ctx);
1698 }
1699
1700
1701 /**
1702 Generates the firmware image in factory format
1703
1704 Image format:
1705
1706 Bytes (hex) Usage
1707 ----------- -----
1708 0000-0003 Image size (4 bytes, big endian)
1709 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1710 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1711 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1712 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1713 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1714 1814-xxxx Firmware partitions
1715 */
1716 static void * generate_factory_image(struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
1717 *len = 0x1814;
1718
1719 size_t i;
1720 for (i = 0; parts[i].name; i++)
1721 *len += parts[i].size;
1722
1723 uint8_t *image = malloc(*len);
1724 if (!image)
1725 error(1, errno, "malloc");
1726
1727 memset(image, 0xff, *len);
1728 put32(image, *len);
1729
1730 if (info->vendor) {
1731 size_t vendor_len = strlen(info->vendor);
1732 put32(image+0x14, vendor_len);
1733 memcpy(image+0x18, info->vendor, vendor_len);
1734 }
1735
1736 put_partitions(image + 0x1014, info->partitions, parts);
1737 put_md5(image+0x04, image+0x14, *len-0x14);
1738
1739 return image;
1740 }
1741
1742 /**
1743 Generates the firmware image in sysupgrade format
1744
1745 This makes some assumptions about the provided flash and image partition tables and
1746 should be generalized when TP-LINK starts building its safeloader into hardware with
1747 different flash layouts.
1748 */
1749 static void * generate_sysupgrade_image(struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
1750 size_t i, j;
1751 size_t flash_first_partition_index = 0;
1752 size_t flash_last_partition_index = 0;
1753 const struct flash_partition_entry *flash_first_partition = NULL;
1754 const struct flash_partition_entry *flash_last_partition = NULL;
1755 const struct image_partition_entry *image_last_partition = NULL;
1756
1757 /** Find first and last partitions */
1758 for (i = 0; info->partitions[i].name; i++) {
1759 if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
1760 flash_first_partition = &info->partitions[i];
1761 flash_first_partition_index = i;
1762 } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
1763 flash_last_partition = &info->partitions[i];
1764 flash_last_partition_index = i;
1765 }
1766 }
1767
1768 assert(flash_first_partition && flash_last_partition);
1769 assert(flash_first_partition_index < flash_last_partition_index);
1770
1771 /** Find last partition from image to calculate needed size */
1772 for (i = 0; image_parts[i].name; i++) {
1773 if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
1774 image_last_partition = &image_parts[i];
1775 break;
1776 }
1777 }
1778
1779 assert(image_last_partition);
1780
1781 *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
1782
1783 uint8_t *image = malloc(*len);
1784 if (!image)
1785 error(1, errno, "malloc");
1786
1787 memset(image, 0xff, *len);
1788
1789 for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
1790 for (j = 0; image_parts[j].name; j++) {
1791 if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
1792 if (image_parts[j].size > info->partitions[i].size)
1793 error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
1794 memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
1795 break;
1796 }
1797
1798 assert(image_parts[j].name);
1799 }
1800 }
1801
1802 return image;
1803 }
1804
1805 /** Generates an image according to a given layout and writes it to a file */
1806 static void build_image(const char *output,
1807 const char *kernel_image,
1808 const char *rootfs_image,
1809 uint32_t rev,
1810 bool add_jffs2_eof,
1811 bool sysupgrade,
1812 struct device_info *info) {
1813
1814 size_t i;
1815
1816 struct image_partition_entry parts[7] = {};
1817
1818 struct flash_partition_entry *firmware_partition = NULL;
1819 struct flash_partition_entry *os_image_partition = NULL;
1820 struct flash_partition_entry *file_system_partition = NULL;
1821 size_t firmware_partition_index = 0;
1822
1823 for (i = 0; info->partitions[i].name; i++) {
1824 if (!strcmp(info->partitions[i].name, "firmware"))
1825 {
1826 firmware_partition = &info->partitions[i];
1827 firmware_partition_index = i;
1828 }
1829 }
1830
1831 if (firmware_partition)
1832 {
1833 os_image_partition = &info->partitions[firmware_partition_index];
1834 file_system_partition = &info->partitions[firmware_partition_index + 1];
1835
1836 struct stat kernel;
1837 if (stat(kernel_image, &kernel) < 0)
1838 error(1, errno, "unable to stat file `%s'", kernel_image);
1839
1840 if (kernel.st_size > firmware_partition->size)
1841 error(1, 0, "kernel overflowed firmware partition\n");
1842
1843 for (i = MAX_PARTITIONS-1; i >= firmware_partition_index + 1; i--)
1844 info->partitions[i+1] = info->partitions[i];
1845
1846 file_system_partition->name = "file-system";
1847 file_system_partition->base = firmware_partition->base + kernel.st_size;
1848
1849 /* Align partition start to erase blocks for factory images only */
1850 if (!sysupgrade)
1851 file_system_partition->base = ALIGN(firmware_partition->base + kernel.st_size, 0x10000);
1852
1853 file_system_partition->size = firmware_partition->size - file_system_partition->base;
1854
1855 os_image_partition->name = "os-image";
1856 os_image_partition->size = kernel.st_size;
1857 }
1858
1859 parts[0] = make_partition_table(info->partitions);
1860 if (info->soft_ver)
1861 parts[1] = make_soft_version_from_string(info->soft_ver);
1862 else
1863 parts[1] = make_soft_version(rev);
1864
1865 parts[2] = make_support_list(info);
1866 parts[3] = read_file("os-image", kernel_image, false, NULL);
1867 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof, file_system_partition);
1868
1869 /* Some devices need the extra-para partition to accept the firmware */
1870 if (strcasecmp(info->id, "ARCHER-C2-V3") == 0 ||
1871 strcasecmp(info->id, "ARCHER-C25-V1") == 0 ||
1872 strcasecmp(info->id, "ARCHER-C59-V2") == 0 ||
1873 strcasecmp(info->id, "ARCHER-C60-V2") == 0 ||
1874 strcasecmp(info->id, "ARCHER-C60-V3") == 0 ||
1875 strcasecmp(info->id, "TLWR1043NV5") == 0) {
1876 const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1877 parts[5] = put_data("extra-para", mdat, 11);
1878 } else if (strcasecmp(info->id, "ARCHER-A7-V5") == 0 || strcasecmp(info->id, "ARCHER-C7-V4") == 0 || strcasecmp(info->id, "ARCHER-C7-V5") == 0) {
1879 const char mdat[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1880 parts[5] = put_data("extra-para", mdat, 11);
1881 } else if (strcasecmp(info->id, "ARCHER-C6-V2") == 0) {
1882 const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00};
1883 parts[5] = put_data("extra-para", mdat, 11);
1884 }
1885
1886 size_t len;
1887 void *image;
1888 if (sysupgrade)
1889 image = generate_sysupgrade_image(info, parts, &len);
1890 else
1891 image = generate_factory_image(info, parts, &len);
1892
1893 FILE *file = fopen(output, "wb");
1894 if (!file)
1895 error(1, errno, "unable to open output file");
1896
1897 if (fwrite(image, len, 1, file) != 1)
1898 error(1, 0, "unable to write output file");
1899
1900 fclose(file);
1901
1902 free(image);
1903
1904 for (i = 0; parts[i].name; i++)
1905 free_image_partition(parts[i]);
1906 }
1907
1908 /** Usage output */
1909 static void usage(const char *argv0) {
1910 fprintf(stderr,
1911 "Usage: %s [OPTIONS...]\n"
1912 "\n"
1913 "Options:\n"
1914 " -h show this help\n"
1915 "\n"
1916 "Create a new image:\n"
1917 " -B <board> create image for the board specified with <board>\n"
1918 " -k <file> read kernel image from the file <file>\n"
1919 " -r <file> read rootfs image from the file <file>\n"
1920 " -o <file> write output to the file <file>\n"
1921 " -V <rev> sets the revision number to <rev>\n"
1922 " -j add jffs2 end-of-filesystem markers\n"
1923 " -S create sysupgrade instead of factory image\n"
1924 "Extract an old image:\n"
1925 " -x <file> extract all oem firmware partition\n"
1926 " -d <dir> destination to extract the firmware partition\n"
1927 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1928 argv0
1929 );
1930 };
1931
1932
1933 static struct device_info *find_board(const char *id)
1934 {
1935 struct device_info *board = NULL;
1936
1937 for (board = boards; board->id != NULL; board++)
1938 if (strcasecmp(id, board->id) == 0)
1939 return board;
1940
1941 return NULL;
1942 }
1943
1944 static int add_flash_partition(
1945 struct flash_partition_entry *part_list,
1946 size_t max_entries,
1947 const char *name,
1948 unsigned long base,
1949 unsigned long size)
1950 {
1951 size_t ptr;
1952 /* check if the list has a free entry */
1953 for (ptr = 0; ptr < max_entries; ptr++, part_list++) {
1954 if (part_list->name == NULL &&
1955 part_list->base == 0 &&
1956 part_list->size == 0)
1957 break;
1958 }
1959
1960 if (ptr == max_entries) {
1961 error(1, 0, "No free flash part entry available.");
1962 }
1963
1964 part_list->name = calloc(1, strlen(name) + 1);
1965 if (!part_list->name) {
1966 error(1, 0, "Unable to allocate memory");
1967 }
1968
1969 memcpy((char *)part_list->name, name, strlen(name));
1970 part_list->base = base;
1971 part_list->size = size;
1972
1973 return 0;
1974 }
1975
1976 /** read the partition table into struct flash_partition_entry */
1977 static int read_partition_table(
1978 FILE *file, long offset,
1979 struct flash_partition_entry *entries, size_t max_entries,
1980 int type)
1981 {
1982 char buf[2048];
1983 char *ptr, *end;
1984 const char *parthdr = NULL;
1985 const char *fwuphdr = "fwup-ptn";
1986 const char *flashhdr = "partition";
1987
1988 /* TODO: search for the partition table */
1989
1990 switch(type) {
1991 case 0:
1992 parthdr = fwuphdr;
1993 break;
1994 case 1:
1995 parthdr = flashhdr;
1996 break;
1997 default:
1998 error(1, 0, "Invalid partition table");
1999 }
2000
2001 if (fseek(file, offset, SEEK_SET) < 0)
2002 error(1, errno, "Can not seek in the firmware");
2003
2004 if (fread(buf, 2048, 1, file) != 1)
2005 error(1, errno, "Can not read fwup-ptn from the firmware");
2006
2007 buf[2047] = '\0';
2008
2009 /* look for the partition header */
2010 if (memcmp(buf, parthdr, strlen(parthdr)) != 0) {
2011 fprintf(stderr, "DEBUG: can not find fwuphdr\n");
2012 return 1;
2013 }
2014
2015 ptr = buf;
2016 end = buf + sizeof(buf);
2017 while ((ptr + strlen(parthdr)) < end &&
2018 memcmp(ptr, parthdr, strlen(parthdr)) == 0) {
2019 char *end_part;
2020 char *end_element;
2021
2022 char name[32] = { 0 };
2023 int name_len = 0;
2024 unsigned long base = 0;
2025 unsigned long size = 0;
2026
2027 end_part = memchr(ptr, '\n', (end - ptr));
2028 if (end_part == NULL) {
2029 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
2030 break;
2031 }
2032
2033 for (int i = 0; i <= 4; i++) {
2034 if (end_part <= ptr)
2035 break;
2036
2037 end_element = memchr(ptr, 0x20, (end_part - ptr));
2038 if (end_element == NULL) {
2039 error(1, errno, "Ignoring the rest of the partition entries.");
2040 break;
2041 }
2042
2043 switch (i) {
2044 /* partition header */
2045 case 0:
2046 ptr = end_element + 1;
2047 continue;
2048 /* name */
2049 case 1:
2050 name_len = (end_element - ptr) > 31 ? 31 : (end_element - ptr);
2051 strncpy(name, ptr, name_len);
2052 name[name_len] = '\0';
2053 ptr = end_element + 1;
2054 continue;
2055
2056 /* string "base" */
2057 case 2:
2058 ptr = end_element + 1;
2059 continue;
2060
2061 /* actual base */
2062 case 3:
2063 base = strtoul(ptr, NULL, 16);
2064 ptr = end_element + 1;
2065 continue;
2066
2067 /* string "size" */
2068 case 4:
2069 ptr = end_element + 1;
2070 /* actual size. The last element doesn't have a sepeartor */
2071 size = strtoul(ptr, NULL, 16);
2072 /* the part ends with 0x09, 0x0d, 0x0a */
2073 ptr = end_part + 1;
2074 add_flash_partition(entries, max_entries, name, base, size);
2075 continue;
2076 }
2077 }
2078 }
2079
2080 return 0;
2081 }
2082
2083 static void write_partition(
2084 FILE *input_file,
2085 size_t firmware_offset,
2086 struct flash_partition_entry *entry,
2087 FILE *output_file)
2088 {
2089 char buf[4096];
2090 size_t offset;
2091
2092 fseek(input_file, entry->base + firmware_offset, SEEK_SET);
2093
2094 for (offset = 0; sizeof(buf) + offset <= entry->size; offset += sizeof(buf)) {
2095 if (fread(buf, sizeof(buf), 1, input_file) != 1)
2096 error(1, errno, "Can not read partition from input_file");
2097
2098 if (fwrite(buf, sizeof(buf), 1, output_file) != 1)
2099 error(1, errno, "Can not write partition to output_file");
2100 }
2101 /* write last chunk smaller than buffer */
2102 if (offset < entry->size) {
2103 offset = entry->size - offset;
2104 if (fread(buf, offset, 1, input_file) != 1)
2105 error(1, errno, "Can not read partition from input_file");
2106 if (fwrite(buf, offset, 1, output_file) != 1)
2107 error(1, errno, "Can not write partition to output_file");
2108 }
2109 }
2110
2111 static int extract_firmware_partition(FILE *input_file, size_t firmware_offset, struct flash_partition_entry *entry, const char *output_directory)
2112 {
2113 FILE *output_file;
2114 char output[PATH_MAX];
2115
2116 snprintf(output, PATH_MAX, "%s/%s", output_directory, entry->name);
2117 output_file = fopen(output, "wb+");
2118 if (output_file == NULL) {
2119 error(1, errno, "Can not open output file %s", output);
2120 }
2121
2122 write_partition(input_file, firmware_offset, entry, output_file);
2123
2124 fclose(output_file);
2125
2126 return 0;
2127 }
2128
2129 /** extract all partitions from the firmware file */
2130 static int extract_firmware(const char *input, const char *output_directory)
2131 {
2132 struct flash_partition_entry entries[16] = { 0 };
2133 size_t max_entries = 16;
2134 size_t firmware_offset = 0x1014;
2135 FILE *input_file;
2136
2137 struct stat statbuf;
2138
2139 /* check input file */
2140 if (stat(input, &statbuf)) {
2141 error(1, errno, "Can not read input firmware %s", input);
2142 }
2143
2144 /* check if output directory exists */
2145 if (stat(output_directory, &statbuf)) {
2146 error(1, errno, "Failed to stat output directory %s", output_directory);
2147 }
2148
2149 if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
2150 error(1, errno, "Given output directory is not a directory %s", output_directory);
2151 }
2152
2153 input_file = fopen(input, "rb");
2154
2155 if (read_partition_table(input_file, firmware_offset, entries, 16, 0) != 0) {
2156 error(1, 0, "Error can not read the partition table (fwup-ptn)");
2157 }
2158
2159 for (size_t i = 0; i < max_entries; i++) {
2160 if (entries[i].name == NULL &&
2161 entries[i].base == 0 &&
2162 entries[i].size == 0)
2163 continue;
2164
2165 extract_firmware_partition(input_file, firmware_offset, &entries[i], output_directory);
2166 }
2167
2168 return 0;
2169 }
2170
2171 static struct flash_partition_entry *find_partition(
2172 struct flash_partition_entry *entries, size_t max_entries,
2173 const char *name, const char *error_msg)
2174 {
2175 for (size_t i = 0; i < max_entries; i++, entries++) {
2176 if (strcmp(entries->name, name) == 0)
2177 return entries;
2178 }
2179
2180 error(1, 0, "%s", error_msg);
2181 return NULL;
2182 }
2183
2184 static void write_ff(FILE *output_file, size_t size)
2185 {
2186 char buf[4096];
2187 size_t offset;
2188
2189 memset(buf, 0xff, sizeof(buf));
2190
2191 for (offset = 0; offset + sizeof(buf) < size ; offset += sizeof(buf)) {
2192 if (fwrite(buf, sizeof(buf), 1, output_file) != 1)
2193 error(1, errno, "Can not write 0xff to output_file");
2194 }
2195
2196 /* write last chunk smaller than buffer */
2197 if (offset < size) {
2198 offset = size - offset;
2199 if (fwrite(buf, offset, 1, output_file) != 1)
2200 error(1, errno, "Can not write partition to output_file");
2201 }
2202 }
2203
2204 static void convert_firmware(const char *input, const char *output)
2205 {
2206 struct flash_partition_entry fwup[MAX_PARTITIONS] = { 0 };
2207 struct flash_partition_entry flash[MAX_PARTITIONS] = { 0 };
2208 struct flash_partition_entry *fwup_os_image = NULL, *fwup_file_system = NULL;
2209 struct flash_partition_entry *flash_os_image = NULL, *flash_file_system = NULL;
2210 struct flash_partition_entry *fwup_partition_table = NULL;
2211 size_t firmware_offset = 0x1014;
2212 FILE *input_file, *output_file;
2213
2214 struct stat statbuf;
2215
2216 /* check input file */
2217 if (stat(input, &statbuf)) {
2218 error(1, errno, "Can not read input firmware %s", input);
2219 }
2220
2221 input_file = fopen(input, "rb");
2222 if (!input_file)
2223 error(1, 0, "Can not open input firmware %s", input);
2224
2225 output_file = fopen(output, "wb");
2226 if (!output_file)
2227 error(1, 0, "Can not open output firmware %s", output);
2228
2229 if (read_partition_table(input_file, firmware_offset, fwup, MAX_PARTITIONS, 0) != 0) {
2230 error(1, 0, "Error can not read the partition table (fwup-ptn)");
2231 }
2232
2233 fwup_os_image = find_partition(fwup, MAX_PARTITIONS,
2234 "os-image", "Error can not find os-image partition (fwup)");
2235 fwup_file_system = find_partition(fwup, MAX_PARTITIONS,
2236 "file-system", "Error can not find file-system partition (fwup)");
2237 fwup_partition_table = find_partition(fwup, MAX_PARTITIONS,
2238 "partition-table", "Error can not find partition-table partition");
2239
2240 /* the flash partition table has a 0x00000004 magic haeder */
2241 if (read_partition_table(input_file, firmware_offset + fwup_partition_table->base + 4, flash, MAX_PARTITIONS, 1) != 0)
2242 error(1, 0, "Error can not read the partition table (flash)");
2243
2244 flash_os_image = find_partition(flash, MAX_PARTITIONS,
2245 "os-image", "Error can not find os-image partition (flash)");
2246 flash_file_system = find_partition(flash, MAX_PARTITIONS,
2247 "file-system", "Error can not find file-system partition (flash)");
2248
2249 /* write os_image to 0x0 */
2250 write_partition(input_file, firmware_offset, fwup_os_image, output_file);
2251 write_ff(output_file, flash_os_image->size - fwup_os_image->size);
2252
2253 /* write file-system behind os_image */
2254 fseek(output_file, flash_file_system->base - flash_os_image->base, SEEK_SET);
2255 write_partition(input_file, firmware_offset, fwup_file_system, output_file);
2256 write_ff(output_file, flash_file_system->size - fwup_file_system->size);
2257
2258 fclose(output_file);
2259 fclose(input_file);
2260 }
2261
2262 int main(int argc, char *argv[]) {
2263 const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
2264 const char *extract_image = NULL, *output_directory = NULL, *convert_image = NULL;
2265 bool add_jffs2_eof = false, sysupgrade = false;
2266 unsigned rev = 0;
2267 struct device_info *info;
2268 set_source_date_epoch();
2269
2270 while (true) {
2271 int c;
2272
2273 c = getopt(argc, argv, "B:k:r:o:V:jSh:x:d:z:");
2274 if (c == -1)
2275 break;
2276
2277 switch (c) {
2278 case 'B':
2279 board = optarg;
2280 break;
2281
2282 case 'k':
2283 kernel_image = optarg;
2284 break;
2285
2286 case 'r':
2287 rootfs_image = optarg;
2288 break;
2289
2290 case 'o':
2291 output = optarg;
2292 break;
2293
2294 case 'V':
2295 sscanf(optarg, "r%u", &rev);
2296 break;
2297
2298 case 'j':
2299 add_jffs2_eof = true;
2300 break;
2301
2302 case 'S':
2303 sysupgrade = true;
2304 break;
2305
2306 case 'h':
2307 usage(argv[0]);
2308 return 0;
2309
2310 case 'd':
2311 output_directory = optarg;
2312 break;
2313
2314 case 'x':
2315 extract_image = optarg;
2316 break;
2317
2318 case 'z':
2319 convert_image = optarg;
2320 break;
2321
2322 default:
2323 usage(argv[0]);
2324 return 1;
2325 }
2326 }
2327
2328 if (extract_image || output_directory) {
2329 if (!extract_image)
2330 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
2331 if (!output_directory)
2332 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
2333 extract_firmware(extract_image, output_directory);
2334 } else if (convert_image) {
2335 if (!output)
2336 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
2337 convert_firmware(convert_image, output);
2338 } else {
2339 if (!board)
2340 error(1, 0, "no board has been specified");
2341 if (!kernel_image)
2342 error(1, 0, "no kernel image has been specified");
2343 if (!rootfs_image)
2344 error(1, 0, "no rootfs image has been specified");
2345 if (!output)
2346 error(1, 0, "no output filename has been specified");
2347
2348 info = find_board(board);
2349
2350 if (info == NULL)
2351 error(1, 0, "unsupported board %s", board);
2352
2353 build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);
2354 }
2355
2356 return 0;
2357 }