ar71xx: Add support to TP-Link EAP120
[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
50 #include "md5.h"
51
52
53 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
54
55
56 /** An image partition table entry */
57 struct image_partition_entry {
58 const char *name;
59 size_t size;
60 uint8_t *data;
61 };
62
63 /** A flash partition table entry */
64 struct flash_partition_entry {
65 const char *name;
66 uint32_t base;
67 uint32_t size;
68 };
69
70
71 /** The content of the soft-version structure */
72 struct __attribute__((__packed__)) soft_version {
73 uint32_t magic;
74 uint32_t zero;
75 uint8_t pad1;
76 uint8_t version_major;
77 uint8_t version_minor;
78 uint8_t version_patch;
79 uint8_t year_hi;
80 uint8_t year_lo;
81 uint8_t month;
82 uint8_t day;
83 uint32_t rev;
84 uint8_t pad2;
85 };
86
87
88 static const uint8_t jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
89
90
91 /**
92 Salt for the MD5 hash
93
94 Fortunately, TP-LINK seems to use the same salt for most devices which use
95 the new image format.
96 */
97 static const uint8_t md5_salt[16] = {
98 0x7a, 0x2b, 0x15, 0xed,
99 0x9b, 0x98, 0x59, 0x6d,
100 0xe5, 0x04, 0xab, 0x44,
101 0xac, 0x2a, 0x9f, 0x4e,
102 };
103
104
105 /** Vendor information for CPE210/220/510/520 */
106 static const char cpe510_vendor[] = "CPE510(TP-LINK|UN|N300-5):1.0\r\n";
107
108 /** Vendor information for C2600 */
109 static const char c2600_vendor[] = "";
110
111 /** Vendor information for EAP120 */
112 static const char eap120_vendor[] = "EAP120(TP-LINK|UN|N300-2):1.0\r\n";
113
114 /**
115 The flash partition table for CPE210/220/510/520;
116 it is the same as the one used by the stock images.
117 */
118 static const struct flash_partition_entry cpe510_partitions[] = {
119 {"fs-uboot", 0x00000, 0x20000},
120 {"partition-table", 0x20000, 0x02000},
121 {"default-mac", 0x30000, 0x00020},
122 {"product-info", 0x31100, 0x00100},
123 {"signature", 0x32000, 0x00400},
124 {"os-image", 0x40000, 0x170000},
125 {"soft-version", 0x1b0000, 0x00100},
126 {"support-list", 0x1b1000, 0x00400},
127 {"file-system", 0x1c0000, 0x600000},
128 {"user-config", 0x7c0000, 0x10000},
129 {"default-config", 0x7d0000, 0x10000},
130 {"log", 0x7e0000, 0x10000},
131 {"radio", 0x7f0000, 0x10000},
132 {NULL, 0, 0}
133 };
134
135 /**
136 The flash partition table for C2600;
137 it is the same as the one used by the stock images.
138 */
139 static const struct flash_partition_entry c2600_partitions[] = {
140 {"SBL1", 0x00000, 0x20000},
141 {"MIBIB", 0x20000, 0x20000},
142 {"SBL2", 0x40000, 0x20000},
143 {"SBL3", 0x60000, 0x30000},
144 {"DDRCONFIG", 0x90000, 0x10000},
145 {"SSD", 0xa0000, 0x10000},
146 {"TZ", 0xb0000, 0x30000},
147 {"RPM", 0xe0000, 0x20000},
148 {"fs-uboot", 0x100000, 0x70000},
149 {"uboot-env", 0x170000, 0x40000},
150 {"radio", 0x1b0000, 0x40000},
151 {"os-image", 0x1f0000, 0x200000},
152 {"file-system", 0x3f0000, 0x1b00000},
153 {"default-mac", 0x1ef0000, 0x00200},
154 {"pin", 0x1ef0200, 0x00200},
155 {"product-info", 0x1ef0400, 0x0fc00},
156 {"partition-table", 0x1f00000, 0x10000},
157 {"soft-version", 0x1f10000, 0x10000},
158 {"support-list", 0x1f20000, 0x10000},
159 {"profile", 0x1f30000, 0x10000},
160 {"default-config", 0x1f40000, 0x10000},
161 {"user-config", 0x1f50000, 0x40000},
162 {"qos-db", 0x1f90000, 0x40000},
163 {"usb-config", 0x1fd0000, 0x10000},
164 {"log", 0x1fe0000, 0x20000},
165 {NULL, 0, 0}
166 };
167
168 /** The flash partition table for EAP120;
169 it is the same as the one used by the stock images.
170 */
171 static const struct flash_partition_entry eap120_partitions[] = {
172 {"fs-uboot", 0x00000, 0x20000},
173 {"partition-table", 0x20000, 0x02000},
174 {"default-mac", 0x30000, 0x00020},
175 {"support-list", 0x31000, 0x00100},
176 {"product-info", 0x31100, 0x00100},
177 {"soft-version", 0x32000, 0x00100},
178 {"os-image", 0x40000, 0x180000},
179 {"file-system", 0x1c0000, 0x600000},
180 {"user-config", 0x7c0000, 0x10000},
181 {"backup-config", 0x7d0000, 0x10000},
182 {"log", 0x7e0000, 0x10000},
183 {"radio", 0x7f0000, 0x10000},
184 {NULL, 0, 0}
185 };
186
187 /**
188 The support list for CPE210/220
189 */
190 static const char cpe210_support_list[] =
191 "SupportList:\r\n"
192 "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
193 "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
194 "CPE220(TP-LINK|UN|N300-2):1.0\r\n"
195 "CPE220(TP-LINK|UN|N300-2):1.1\r\n";
196 /**
197 The support list for CPE210/220/510/520
198 */
199 static const char cpe510_support_list[] =
200 "SupportList:\r\n"
201 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
202 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
203 "CPE520(TP-LINK|UN|N300-5):1.0\r\n"
204 "CPE520(TP-LINK|UN|N300-5):1.1\r\n";
205
206 /**
207 The support list for C2600
208 */
209 static const char c2600_support_list[] =
210 "SupportList:\r\n"
211 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n";
212
213 /**
214 The support list for EAP120
215 */
216 static const char eap120_support_list[] =
217 "SupportList:\r\n"
218 "EAP120(TP-LINK|UN|N300-2):1.0\r\n";
219
220 #define error(_ret, _errno, _str, ...) \
221 do { \
222 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
223 strerror(_errno)); \
224 if (_ret) \
225 exit(_ret); \
226 } while (0)
227
228
229 /** Stores a uint32 as big endian */
230 static inline void put32(uint8_t *buf, uint32_t val) {
231 buf[0] = val >> 24;
232 buf[1] = val >> 16;
233 buf[2] = val >> 8;
234 buf[3] = val;
235 }
236
237 /** Allocates a new image partition */
238 static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
239 struct image_partition_entry entry = {name, len, malloc(len)};
240 if (!entry.data)
241 error(1, errno, "malloc");
242
243 return entry;
244 }
245
246 /** Frees an image partition */
247 static void free_image_partition(struct image_partition_entry entry) {
248 free(entry.data);
249 }
250
251 /** Generates the partition-table partition */
252 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
253 struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
254
255 char *s = (char *)entry.data, *end = (char *)(s+entry.size);
256
257 *(s++) = 0x00;
258 *(s++) = 0x04;
259 *(s++) = 0x00;
260 *(s++) = 0x00;
261
262 size_t i;
263 for (i = 0; p[i].name; i++) {
264 size_t len = end-s;
265 size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
266
267 if (w > len-1)
268 error(1, 0, "flash partition table overflow?");
269
270 s += w;
271 }
272
273 s++;
274
275 memset(s, 0xff, end-s);
276
277 return entry;
278 }
279
280
281 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
282 static inline uint8_t bcd(uint8_t v) {
283 return 0x10 * (v/10) + v%10;
284 }
285
286
287 /** Generates the soft-version partition */
288 static struct image_partition_entry make_soft_version(uint32_t rev) {
289 struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
290 struct soft_version *s = (struct soft_version *)entry.data;
291
292 time_t t;
293
294 if (time(&t) == (time_t)(-1))
295 error(1, errno, "time");
296
297 struct tm *tm = localtime(&t);
298
299 s->magic = htonl(0x0000000c);
300 s->zero = 0;
301 s->pad1 = 0xff;
302
303 s->version_major = 0;
304 s->version_minor = 0;
305 s->version_patch = 0;
306
307 s->year_hi = bcd((1900+tm->tm_year)/100);
308 s->year_lo = bcd(tm->tm_year%100);
309 s->month = bcd(tm->tm_mon+1);
310 s->day = bcd(tm->tm_mday);
311 s->rev = htonl(rev);
312
313 s->pad2 = 0xff;
314
315 return entry;
316 }
317
318 /** Generates the support-list partition */
319 static struct image_partition_entry make_support_list(const char *support_list, bool trailzero) {
320 size_t len = strlen(support_list);
321 struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
322
323 put32(entry.data, len);
324 memset(entry.data+4, 0, 4);
325 memcpy(entry.data+8, support_list, len);
326 entry.data[len+8] = trailzero ? '\x00' : '\xff';
327
328 return entry;
329 }
330
331 /** Creates a new image partition with an arbitrary name from a file */
332 static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof) {
333 struct stat statbuf;
334
335 if (stat(filename, &statbuf) < 0)
336 error(1, errno, "unable to stat file `%s'", filename);
337
338 size_t len = statbuf.st_size;
339
340 if (add_jffs2_eof)
341 len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
342
343 struct image_partition_entry entry = alloc_image_partition(part_name, len);
344
345 FILE *file = fopen(filename, "rb");
346 if (!file)
347 error(1, errno, "unable to open file `%s'", filename);
348
349 if (fread(entry.data, statbuf.st_size, 1, file) != 1)
350 error(1, errno, "unable to read file `%s'", filename);
351
352 if (add_jffs2_eof) {
353 uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
354
355 memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
356 memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
357 }
358
359 fclose(file);
360
361 return entry;
362 }
363
364
365 /**
366 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
367
368 Example image partition table:
369
370 fwup-ptn partition-table base 0x00800 size 0x00800
371 fwup-ptn os-image base 0x01000 size 0x113b45
372 fwup-ptn file-system base 0x114b45 size 0x1d0004
373 fwup-ptn support-list base 0x2e4b49 size 0x000d1
374
375 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
376 the end of the partition table is marked with a zero byte.
377
378 The firmware image must contain at least the partition-table and support-list partitions
379 to be accepted. There aren't any alignment constraints for the image partitions.
380
381 The partition-table partition contains the actual flash layout; partitions
382 from the image partition table are mapped to the corresponding flash partitions during
383 the firmware upgrade. The support-list partition contains a list of devices supported by
384 the firmware image.
385
386 The base offsets in the firmware partition table are relative to the end
387 of the vendor information block, so the partition-table partition will
388 actually start at offset 0x1814 of the image.
389
390 I think partition-table must be the first partition in the firmware image.
391 */
392 static void put_partitions(uint8_t *buffer, const struct image_partition_entry *parts) {
393 size_t i;
394 char *image_pt = (char *)buffer, *end = image_pt + 0x800;
395
396 size_t base = 0x800;
397 for (i = 0; parts[i].name; i++) {
398 memcpy(buffer + base, parts[i].data, parts[i].size);
399
400 size_t len = end-image_pt;
401 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);
402
403 if (w > len-1)
404 error(1, 0, "image partition table overflow?");
405
406 image_pt += w;
407
408 base += parts[i].size;
409 }
410
411 image_pt++;
412
413 memset(image_pt, 0xff, end-image_pt);
414 }
415
416 /** Generates and writes the image MD5 checksum */
417 static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
418 MD5_CTX ctx;
419
420 MD5_Init(&ctx);
421 MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
422 MD5_Update(&ctx, buffer, len);
423 MD5_Final(md5, &ctx);
424 }
425
426
427 /**
428 Generates the firmware image in factory format
429
430 Image format:
431
432 Bytes (hex) Usage
433 ----------- -----
434 0000-0003 Image size (4 bytes, big endian)
435 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
436 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
437 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
438 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
439 1014-1813 Image partition table (2048 bytes, padded with 0xff)
440 1814-xxxx Firmware partitions
441 */
442 static void * generate_factory_image(const char *vendor, const struct image_partition_entry *parts, size_t *len) {
443 *len = 0x1814;
444
445 size_t i;
446 for (i = 0; parts[i].name; i++)
447 *len += parts[i].size;
448
449 uint8_t *image = malloc(*len);
450 if (!image)
451 error(1, errno, "malloc");
452
453 put32(image, *len);
454
455 size_t vendor_len = strlen(vendor);
456 put32(image+0x14, vendor_len);
457 memcpy(image+0x18, vendor, vendor_len);
458 memset(image+0x18+vendor_len, 0xff, 4092-vendor_len);
459
460 put_partitions(image + 0x1014, parts);
461 put_md5(image+0x04, image+0x14, *len-0x14);
462
463 return image;
464 }
465
466 /**
467 Generates the firmware image in sysupgrade format
468
469 This makes some assumptions about the provided flash and image partition tables and
470 should be generalized when TP-LINK starts building its safeloader into hardware with
471 different flash layouts.
472 */
473 static void * generate_sysupgrade_image(const struct flash_partition_entry *flash_parts, const struct image_partition_entry *image_parts, size_t *len) {
474 const struct flash_partition_entry *flash_os_image = &flash_parts[5];
475 const struct flash_partition_entry *flash_soft_version = &flash_parts[6];
476 const struct flash_partition_entry *flash_support_list = &flash_parts[7];
477 const struct flash_partition_entry *flash_file_system = &flash_parts[8];
478
479 const struct image_partition_entry *image_os_image = &image_parts[3];
480 const struct image_partition_entry *image_soft_version = &image_parts[1];
481 const struct image_partition_entry *image_support_list = &image_parts[2];
482 const struct image_partition_entry *image_file_system = &image_parts[4];
483
484 assert(strcmp(flash_os_image->name, "os-image") == 0);
485 assert(strcmp(flash_soft_version->name, "soft-version") == 0);
486 assert(strcmp(flash_support_list->name, "support-list") == 0);
487 assert(strcmp(flash_file_system->name, "file-system") == 0);
488
489 assert(strcmp(image_os_image->name, "os-image") == 0);
490 assert(strcmp(image_soft_version->name, "soft-version") == 0);
491 assert(strcmp(image_support_list->name, "support-list") == 0);
492 assert(strcmp(image_file_system->name, "file-system") == 0);
493
494 if (image_os_image->size > flash_os_image->size)
495 error(1, 0, "kernel image too big (more than %u bytes)", (unsigned)flash_os_image->size);
496 if (image_file_system->size > flash_file_system->size)
497 error(1, 0, "rootfs image too big (more than %u bytes)", (unsigned)flash_file_system->size);
498
499 *len = flash_file_system->base - flash_os_image->base + image_file_system->size;
500
501 uint8_t *image = malloc(*len);
502 if (!image)
503 error(1, errno, "malloc");
504
505 memset(image, 0xff, *len);
506
507 memcpy(image, image_os_image->data, image_os_image->size);
508 memcpy(image + flash_soft_version->base - flash_os_image->base, image_soft_version->data, image_soft_version->size);
509 memcpy(image + flash_support_list->base - flash_os_image->base, image_support_list->data, image_support_list->size);
510 memcpy(image + flash_file_system->base - flash_os_image->base, image_file_system->data, image_file_system->size);
511
512 return image;
513 }
514
515 static void * generate_sysupgrade_image_c2600(const struct flash_partition_entry *flash_parts, const struct image_partition_entry *image_parts, size_t *len) {
516 const struct flash_partition_entry *flash_os_image = &flash_parts[11];
517 const struct flash_partition_entry *flash_file_system = &flash_parts[12];
518
519 const struct image_partition_entry *image_os_image = &image_parts[3];
520 const struct image_partition_entry *image_file_system = &image_parts[4];
521
522 assert(strcmp(flash_os_image->name, "os-image") == 0);
523 assert(strcmp(flash_file_system->name, "file-system") == 0);
524
525 assert(strcmp(image_os_image->name, "os-image") == 0);
526 assert(strcmp(image_file_system->name, "file-system") == 0);
527
528 if (image_os_image->size > flash_os_image->size)
529 error(1, 0, "kernel image too big (more than %u bytes)", (unsigned)flash_os_image->size);
530 if (image_file_system->size > flash_file_system->size)
531 error(1, 0, "rootfs image too big (more than %u bytes)", (unsigned)flash_file_system->size);
532
533 *len = flash_file_system->base - flash_os_image->base + image_file_system->size;
534
535 uint8_t *image = malloc(*len);
536 if (!image)
537 error(1, errno, "malloc");
538
539 memset(image, 0xff, *len);
540
541 memcpy(image, image_os_image->data, image_os_image->size);
542 memcpy(image + flash_file_system->base - flash_os_image->base, image_file_system->data, image_file_system->size);
543
544 return image;
545 }
546 static void *generate_sysupgrade_image_eap120(const struct flash_partition_entry *flash_parts, const struct image_partition_entry *image_parts, size_t *len)
547 {
548 const struct flash_partition_entry *flash_os_image = &flash_parts[6];
549 const struct flash_partition_entry *flash_file_system = &flash_parts[7];
550
551 const struct image_partition_entry *image_os_image = &image_parts[3];
552 const struct image_partition_entry *image_file_system = &image_parts[4];
553
554 assert(strcmp(flash_os_image->name, "os-image") == 0);
555 assert(strcmp(flash_file_system->name, "file-system") == 0);
556
557 assert(strcmp(image_os_image->name, "os-image") == 0);
558 assert(strcmp(image_file_system->name, "file-system") == 0);
559
560 if (image_os_image->size > flash_os_image->size)
561 error(1, 0, "kernel image too big (more than %u bytes)", (unsigned)flash_os_image->size);
562 if (image_file_system->size > flash_file_system->size)
563 error(1, 0, "rootfs image too big (more than %u bytes)", (unsigned)flash_file_system->size);
564
565 *len = flash_file_system->base - flash_os_image->base + image_file_system->size;
566
567 uint8_t *image = malloc(*len);
568 if (!image)
569 error(1, errno, "malloc");
570
571 memset(image, 0xff, *len);
572 memcpy(image, image_os_image->data, image_os_image->size);
573 memcpy(image + flash_file_system->base - flash_os_image->base, image_file_system->data, image_file_system->size);
574
575 return image;
576 }
577
578 /** Generates an image for CPE210/220/510/520 and writes it to a file */
579 static void do_cpe(const char *output,
580 const char *kernel_image,
581 const char *rootfs_image,
582 uint32_t rev,
583 bool add_jffs2_eof,
584 bool sysupgrade,
585 const char *support_list) {
586 struct image_partition_entry parts[6] = {};
587
588 parts[0] = make_partition_table(cpe510_partitions);
589 parts[1] = make_soft_version(rev);
590 parts[2] = make_support_list(support_list, false);
591 parts[3] = read_file("os-image", kernel_image, false);
592 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
593
594 size_t len;
595 void *image;
596 if (sysupgrade)
597 image = generate_sysupgrade_image(cpe510_partitions, parts, &len);
598 else
599 image = generate_factory_image(cpe510_vendor, parts, &len);
600
601 FILE *file = fopen(output, "wb");
602 if (!file)
603 error(1, errno, "unable to open output file");
604
605 if (fwrite(image, len, 1, file) != 1)
606 error(1, 0, "unable to write output file");
607
608 fclose(file);
609
610 free(image);
611
612 size_t i;
613 for (i = 0; parts[i].name; i++)
614 free_image_partition(parts[i]);
615 }
616
617 /** Generates an image for C2600 and writes it to a file */
618 static void do_c2600(const char *output, const char *kernel_image, const char *rootfs_image, uint32_t rev, bool add_jffs2_eof, bool sysupgrade) {
619 struct image_partition_entry parts[6] = {};
620
621 parts[0] = make_partition_table(c2600_partitions);
622 parts[1] = make_soft_version(rev);
623 parts[2] = make_support_list(c2600_support_list, true);
624 parts[3] = read_file("os-image", kernel_image, false);
625 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
626
627 size_t len;
628 void *image;
629 if (sysupgrade)
630 image = generate_sysupgrade_image_c2600(c2600_partitions, parts, &len);
631 else
632 image = generate_factory_image(c2600_vendor, parts, &len);
633
634 FILE *file = fopen(output, "wb");
635 if (!file)
636 error(1, errno, "unable to open output file");
637
638 if (fwrite(image, len, 1, file) != 1)
639 error(1, 0, "unable to write output file");
640
641 fclose(file);
642
643 free(image);
644
645 size_t i;
646 for (i = 0; parts[i].name; i++)
647 free_image_partition(parts[i]);
648 }
649
650
651 /** Generates an image for EAP120 and writes it to a file */
652 static void do_eap(const char *output,
653 const char *kernel_image,
654 const char *rootfs_image,
655 uint32_t rev,
656 bool add_jffs2_eof,
657 bool sysupgrade,
658 const char *support_list) {
659 struct image_partition_entry parts[6] = {};
660
661 parts[0] = make_partition_table(eap120_partitions);
662 parts[1] = make_soft_version(rev);
663 parts[2] = make_support_list(support_list, false);
664 parts[3] = read_file("os-image", kernel_image, false);
665 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
666
667 size_t len;
668 void *image;
669 if (sysupgrade)
670 image = generate_sysupgrade_image_eap120(eap120_partitions, parts, &len);
671 else
672 image = generate_factory_image(eap120_vendor, parts, &len);
673
674 FILE *file = fopen(output, "wb");
675 if (!file)
676 error(1, errno, "unable to open output file");
677
678 if (fwrite(image, len, 1, file) != 1)
679 error(1, 0, "unable to write output file");
680
681 fclose(file);
682
683 free(image);
684
685 size_t i;
686 for (i = 0; parts[i].name; i++)
687 free_image_partition(parts[i]);
688 }
689
690 /** Usage output */
691 static void usage(const char *argv0) {
692 fprintf(stderr,
693 "Usage: %s [OPTIONS...]\n"
694 "\n"
695 "Options:\n"
696 " -B <board> create image for the board specified with <board>\n"
697 " -k <file> read kernel image from the file <file>\n"
698 " -r <file> read rootfs image from the file <file>\n"
699 " -o <file> write output to the file <file>\n"
700 " -V <rev> sets the revision number to <rev>\n"
701 " -j add jffs2 end-of-filesystem markers\n"
702 " -S create sysupgrade instead of factory image\n"
703 " -h show this help\n",
704 argv0
705 );
706 };
707
708
709 int main(int argc, char *argv[]) {
710 const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
711 bool add_jffs2_eof = false, sysupgrade = false;
712 unsigned rev = 0;
713
714 while (true) {
715 int c;
716
717 c = getopt(argc, argv, "B:k:r:o:V:jSh");
718 if (c == -1)
719 break;
720
721 switch (c) {
722 case 'B':
723 board = optarg;
724 break;
725
726 case 'k':
727 kernel_image = optarg;
728 break;
729
730 case 'r':
731 rootfs_image = optarg;
732 break;
733
734 case 'o':
735 output = optarg;
736 break;
737
738 case 'V':
739 sscanf(optarg, "r%u", &rev);
740 break;
741
742 case 'j':
743 add_jffs2_eof = true;
744 break;
745
746 case 'S':
747 sysupgrade = true;
748 break;
749
750 case 'h':
751 usage(argv[0]);
752 return 0;
753
754 default:
755 usage(argv[0]);
756 return 1;
757 }
758 }
759
760 if (!board)
761 error(1, 0, "no board has been specified");
762 if (!kernel_image)
763 error(1, 0, "no kernel image has been specified");
764 if (!rootfs_image)
765 error(1, 0, "no rootfs image has been specified");
766 if (!output)
767 error(1, 0, "no output filename has been specified");
768
769 if (strcmp(board, "CPE210") == 0)
770 do_cpe(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, cpe210_support_list);
771 else if (strcmp(board, "CPE510") == 0)
772 do_cpe(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, cpe510_support_list);
773 else if (strcmp(board, "C2600") == 0)
774 do_c2600(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade);
775 else if (strcmp(board, "EAP120") == 0)
776 do_eap(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, eap120_support_list);
777 else
778 error(1, 0, "unsupported board %s", board);
779
780 return 0;
781 }