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