ar71xx: Backport support for Onion Omega
[15.05/openwrt.git] / tools / firmware-utils / src / imagetag.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2008 Axel Gembe <ago@bastart.eu.org>
7  * Copyright (C) 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <netinet/in.h>
18
19 #include "bcm_tag.h"
20 #include "imagetag_cmdline.h"
21 #include "cyg_crc.h"
22
23 #define DEADCODE                        0xDEADC0DE
24
25 /* Kernel header */
26 struct kernelhdr {
27         uint32_t                loadaddr;       /* Kernel load address */
28         uint32_t                entry;          /* Kernel entry point address */
29         uint32_t                lzmalen;        /* Compressed length of the LZMA data that follows */
30 };
31
32 static char pirellitab[NUM_PIRELLI][BOARDID_LEN] = PIRELLI_BOARDS;
33
34 void int2tag(char *tag, uint32_t value) {
35   uint32_t network = htonl(value);
36   memcpy(tag, (char *)(&network), 4);
37 }
38
39 uint32_t compute_crc32(uint32_t crc, FILE *binfile, size_t compute_start, size_t compute_len)
40 {
41         uint8_t readbuf[1024];
42         size_t read;
43
44         fseek(binfile, compute_start, SEEK_SET);
45
46         /* read block of 1024 bytes */
47         while (binfile && !feof(binfile) && !ferror(binfile) && (compute_len >= sizeof(readbuf))) {
48                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), binfile);
49                 crc = cyg_crc32_accumulate(crc, readbuf, read);
50                 compute_len = compute_len - read;
51         }
52
53         /* Less than 1024 bytes remains, read compute_len bytes */
54         if (binfile && !feof(binfile) && !ferror(binfile) && (compute_len > 0)) {
55                 read = fread(readbuf, sizeof(uint8_t), compute_len, binfile);
56                 crc = cyg_crc32_accumulate(crc, readbuf, read);
57         }
58
59         return crc;
60 }
61
62 size_t getlen(FILE *fp)
63 {
64         size_t retval, curpos;
65
66         if (!fp)
67                 return 0;
68
69         curpos = ftell(fp);
70         fseek(fp, 0, SEEK_END);
71         retval = ftell(fp);
72         fseek(fp, curpos, SEEK_SET);
73
74         return retval;
75 }
76
77 int tagfile(const char *kernel, const char *rootfs, const char *bin, \
78                         const struct gengetopt_args_info *args, \
79                         uint32_t flash_start, uint32_t image_offset, \
80                         uint32_t block_size, uint32_t load_address, uint32_t entry)
81 {
82         struct bcm_tag tag;
83         struct kernelhdr khdr;
84         FILE *kernelfile = NULL, *rootfsfile = NULL, *binfile = NULL, *cfefile = NULL;
85         size_t cfeoff, cfelen, kerneloff, kernellen, rootfsoff, rootfslen, \
86           read, imagelen, rootfsoffpadlen = 0, kernelfslen, kerneloffpadlen = 0, oldrootfslen, \
87           rootfsend;
88         uint8_t readbuf[1024];
89         uint32_t imagecrc = IMAGETAG_CRC_START;
90         uint32_t kernelcrc = IMAGETAG_CRC_START;
91         uint32_t rootfscrc = IMAGETAG_CRC_START;
92         uint32_t kernelfscrc = IMAGETAG_CRC_START;
93         uint32_t fwaddr = 0;
94         uint8_t crc_val;
95         const uint32_t deadcode = htonl(DEADCODE);
96         int i;
97         int is_pirelli = 0;
98
99
100         memset(&tag, 0, sizeof(struct bcm_tag));
101
102         if (!kernel || !rootfs) {
103                 fprintf(stderr, "imagetag can't create an image without both kernel and rootfs\n");
104         }
105
106         if (kernel && !(kernelfile = fopen(kernel, "rb"))) {
107                 fprintf(stderr, "Unable to open kernel \"%s\"\n", kernel);
108                 return 1;
109         }
110
111         if (rootfs && !(rootfsfile = fopen(rootfs, "rb"))) {
112                 fprintf(stderr, "Unable to open rootfs \"%s\"\n", rootfs);
113                 return 1;
114         }
115
116         if (!bin || !(binfile = fopen(bin, "wb+"))) {
117                 fprintf(stderr, "Unable to open output file \"%s\"\n", bin);
118                 return 1;
119         }
120
121         if ((args->cfe_given) && (args->cfe_arg)) {
122           if (!(cfefile = fopen(args->cfe_arg, "rb"))) {
123                 fprintf(stderr, "Unable to open CFE file \"%s\"\n", args->cfe_arg);
124           }
125         }
126
127         fwaddr = flash_start + image_offset;
128         if (cfefile) {
129           cfeoff = flash_start;           
130           cfelen = getlen(cfefile);
131           /* Seek to the start of the file after tag */
132           fseek(binfile, sizeof(tag), SEEK_SET);
133           
134           /* Write the cfe */
135           while (cfefile && !feof(cfefile) && !ferror(cfefile)) {
136                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), cfefile);
137                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
138           }
139
140         } else {
141           cfeoff = 0;
142           cfelen = 0;
143         }
144
145         if (!args->root_first_flag) {
146           /* Build the kernel address and length (doesn't need to be aligned, read only) */
147           kerneloff = fwaddr + sizeof(tag);
148           
149           kernellen = getlen(kernelfile);
150           
151           if (!args->kernel_file_has_header_flag) {
152                 /* Build the kernel header */
153                 khdr.loadaddr   = htonl(load_address);
154                 khdr.entry      = htonl(entry);
155                 khdr.lzmalen    = htonl(kernellen);
156                 
157                 /* Increase the kernel size by the header size */
158                 kernellen += sizeof(khdr);        
159           }
160           
161           /* Build the rootfs address and length */
162           rootfsoff = kerneloff + kernellen;
163           /* align the start if requested */
164           if (args->align_rootfs_flag)
165                 rootfsoff = (rootfsoff % block_size) > 0 ? (((rootfsoff / block_size) + 1) * block_size) : rootfsoff;
166
167           /* align the end */
168           rootfsend = rootfsoff + getlen(rootfsfile);
169           if ((rootfsend % block_size) > 0)
170                 rootfsend = (((rootfsend / block_size) + 1) * block_size);
171           rootfslen = rootfsend - rootfsoff;
172           imagelen = rootfsoff + rootfslen - kerneloff + sizeof(deadcode);
173           rootfsoffpadlen = rootfsoff - (kerneloff + kernellen);
174           
175           /* Seek to the start of the kernel */
176           fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
177           
178           /* Write the kernel header */
179           fwrite(&khdr, sizeof(khdr), 1, binfile);
180           
181           /* Write the kernel */
182           while (kernelfile && !feof(kernelfile) && !ferror(kernelfile)) {
183                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), kernelfile);
184                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
185           }
186
187           /* Write the RootFS */
188           fseek(binfile, rootfsoff - fwaddr + cfelen, SEEK_SET);
189           while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) {
190                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile);
191                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
192           }
193
194           /* Align image to specified erase block size and append deadc0de */
195           printf("Data alignment to %dk with 'deadc0de' appended\n", block_size/1024);
196           fseek(binfile, rootfsoff + rootfslen - fwaddr + cfelen, SEEK_SET);
197           fwrite(&deadcode, sizeof(uint32_t), 1, binfile);
198
199           oldrootfslen = rootfslen;
200           if (args->pad_given) {
201                 uint32_t allfs = 0xffffffff;
202                 uint32_t pad_size = args->pad_arg * 1024 * 1024;
203
204                 printf("Padding image to %d bytes ...\n", pad_size);
205                 while (imagelen < pad_size) {
206                         fwrite(&allfs, sizeof(uint32_t), 1, binfile);
207                         imagelen += 4;
208                         rootfslen += 4;
209                 }
210           }
211
212           /* Flush the binfile buffer so that when we read from file, it contains
213            * everything in the buffer
214            */
215           fflush(binfile);
216
217           /* Compute the crc32 of the entire image (deadC0de included) */
218           imagecrc = compute_crc32(imagecrc, binfile, kerneloff - fwaddr + cfelen, imagelen);
219           /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
220           kernelcrc = compute_crc32(kernelcrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen);
221           /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
222           kernelfscrc = compute_crc32(kernelfscrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen + rootfslen + sizeof(deadcode));
223           /* Compute the crc32 of the flashImageStart to rootLength.
224            * The broadcom firmware assumes the rootfs starts the image,
225            * therefore uses the rootfs start to determine where to flash
226            * the image.  Since we have the kernel first we have to give
227            * it the kernel address, but the crc uses the length
228            * associated with this address, which is added to the kernel
229            * length to determine the length of image to flash and thus
230            * needs to be rootfs + deadcode
231            */
232           rootfscrc = compute_crc32(rootfscrc, binfile, kerneloff - fwaddr + cfelen, rootfslen + sizeof(deadcode));
233
234         } else {
235           /* Build the kernel address and length (doesn't need to be aligned, read only) */
236           rootfsoff = fwaddr + sizeof(tag);
237           oldrootfslen = getlen(rootfsfile);
238           rootfslen = oldrootfslen;
239           rootfslen = ( (rootfslen % block_size) > 0 ? (((rootfslen / block_size) + 1) * block_size) : rootfslen );
240           kerneloffpadlen = rootfslen - oldrootfslen;
241           oldrootfslen = rootfslen;
242
243           kerneloff = rootfsoff + rootfslen;
244           kernellen = getlen(kernelfile);
245
246           imagelen = cfelen + rootfslen + kernellen;
247           
248           /* Seek to the start of the kernel */
249           fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
250           
251           if (!args->kernel_file_has_header_flag) {
252                 /* Build the kernel header */
253                 khdr.loadaddr   = htonl(load_address);
254                 khdr.entry      = htonl(entry);
255                 khdr.lzmalen    = htonl(kernellen);
256                 
257                 /* Write the kernel header */
258                 fwrite(&khdr, sizeof(khdr), 1, binfile);
259           
260                 /* Increase the kernel size by the header size */
261                 kernellen += sizeof(khdr);        
262           }
263           
264           /* Write the kernel */
265           while (kernelfile && !feof(kernelfile) && !ferror(kernelfile)) {
266                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), kernelfile);
267                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
268           }
269
270           /* Write the RootFS */
271           fseek(binfile, rootfsoff - fwaddr + cfelen, SEEK_SET);
272           while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) {
273                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile);
274                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
275           }
276
277           /* Flush the binfile buffer so that when we read from file, it contains
278            * everything in the buffer
279            */
280           fflush(binfile);
281
282           /* Compute the crc32 of the entire image (deadC0de included) */
283           imagecrc = compute_crc32(imagecrc, binfile, sizeof(tag), imagelen);
284           /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
285           kernelcrc = compute_crc32(kernelcrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen);
286           kernelfscrc = compute_crc32(kernelfscrc, binfile, rootfsoff - fwaddr + cfelen, kernellen + rootfslen);
287           rootfscrc = compute_crc32(rootfscrc, binfile, rootfsoff - fwaddr + cfelen, rootfslen);
288         }
289
290         /* Close the files */
291         if (cfefile) {
292           fclose(cfefile);
293         }
294         fclose(kernelfile);
295         fclose(rootfsfile);
296
297         /* Build the tag */
298         strncpy(tag.tagVersion, args->tag_version_arg, sizeof(tag.tagVersion) - 1);
299         strncpy(tag.sig_1, args->signature_arg, sizeof(tag.sig_1) - 1);
300         strncpy(tag.sig_2, args->signature2_arg, sizeof(tag.sig_2) - 1);
301         strncpy(tag.chipid, args->chipid_arg, sizeof(tag.chipid) - 1);
302         strncpy(tag.boardid, args->boardid_arg, sizeof(tag.boardid) - 1);
303         strcpy(tag.big_endian, "1");
304         sprintf(tag.totalLength, "%lu", imagelen);
305
306         if (args->cfe_given) {
307           sprintf(tag.cfeAddress, "%lu", flash_start);
308           sprintf(tag.cfeLength, "%lu", cfelen);
309         } else {
310           /* We don't include CFE */
311           strcpy(tag.cfeAddress, "0");
312           strcpy(tag.cfeLength, "0");
313         }
314
315         sprintf(tag.kernelAddress, "%lu", kerneloff);
316         sprintf(tag.kernelLength, "%lu", kernellen + rootfsoffpadlen);
317
318         if (args->root_first_flag) {
319           sprintf(tag.flashImageStart, "%lu", rootfsoff);
320           sprintf(tag.flashRootLength, "%lu", rootfslen);         
321         } else {
322           sprintf(tag.flashImageStart, "%lu", kerneloff);
323           sprintf(tag.flashRootLength, "%lu", rootfslen + sizeof(deadcode));
324         }
325         int2tag(tag.rootLength, oldrootfslen + sizeof(deadcode));
326
327         if (args->rsa_signature_given) {
328             strncpy(tag.rsa_signature, args->rsa_signature_arg, RSASIG_LEN);
329         }
330
331         if (args->layoutver_given) {
332             strncpy(tag.flashLayoutVer, args->layoutver_arg, TAGLAYOUT_LEN);
333         }
334
335         if (args->info1_given) {
336           strncpy(tag.information1, args->info1_arg, TAGINFO1_LEN);
337         }
338
339         if (args->info2_given) {
340           strncpy(tag.information2, args->info2_arg, TAGINFO2_LEN);
341         }
342
343         if (args->reserved2_given) {
344           strncpy(tag.reserved2, args->reserved2_arg, 16);
345         }
346
347         if (args->altinfo_given) {
348           strncpy(&tag.information1[0], args->altinfo_arg, ALTTAGINFO_LEN);
349         }
350
351         if (args->second_image_flag_given) {
352           if (strncmp(args->second_image_flag_arg, "2", DUALFLAG_LEN) != 0) {           
353                 strncpy(tag.dualImage, args->second_image_flag_arg, DUALFLAG_LEN);
354           }
355         }
356
357         if (args->inactive_given) {
358           if (strncmp(args->inactive_arg, "2", INACTIVEFLAG_LEN) != 0) {                
359                 strncpy(tag.inactiveFlag, args->second_image_flag_arg, INACTIVEFLAG_LEN);
360           }
361         }
362
363         for (i = 0; i < NUM_PIRELLI; i++) {
364                 if (strncmp(args->boardid_arg, pirellitab[i], BOARDID_LEN) == 0) {
365                         is_pirelli = 1;
366                         break;
367                 }
368         }
369
370         if ( !is_pirelli ) {
371           int2tag(tag.imageCRC, kernelfscrc);
372         } else {
373           int2tag(tag.imageCRC, kernelcrc);
374         }
375
376         int2tag(&(tag.rootfsCRC[0]), rootfscrc);
377         int2tag(tag.kernelCRC, kernelcrc);
378         int2tag(tag.fskernelCRC, kernelfscrc);
379         int2tag(tag.headerCRC, cyg_crc32_accumulate(IMAGETAG_CRC_START, (uint8_t*)&tag, sizeof(tag) - 20));
380
381         fseek(binfile, 0L, SEEK_SET);
382         fwrite(&tag, sizeof(uint8_t), sizeof(tag), binfile);
383
384     fflush(binfile);
385         fclose(binfile);
386
387         return 0;
388 }
389
390 int main(int argc, char **argv)
391 {
392     int c, i;
393         char *kernel, *rootfs, *bin;
394         uint32_t flash_start, image_offset, block_size, load_address, entry;
395         flash_start = image_offset = block_size = load_address = entry = 0;
396         struct gengetopt_args_info parsed_args;
397
398         kernel = rootfs = bin = NULL;
399
400         if (imagetag_cmdline(argc, argv, &parsed_args)) {
401           exit(1);
402         }
403
404         printf("Broadcom 63xx image tagger - v2.0.0\n");
405         printf("Copyright (C) 2008 Axel Gembe\n");
406         printf("Copyright (C) 2009-2010 Daniel Dickinson\n");
407         printf("Licensed under the terms of the Gnu General Public License\n");
408
409         kernel = parsed_args.kernel_arg;
410         rootfs = parsed_args.rootfs_arg;
411         bin = parsed_args.output_arg;
412         if (strlen(parsed_args.tag_version_arg) >= TAGVER_LEN) {
413           fprintf(stderr, "Error: Tag Version (tag_version,v) too long.\n");
414           exit(1);
415         }
416         if (strlen(parsed_args.boardid_arg) >= BOARDID_LEN) {
417           fprintf(stderr, "Error: Board ID (boardid,b) too long.\n");
418           exit(1);
419         }
420         if (strlen(parsed_args.chipid_arg) >= CHIPID_LEN) {
421           fprintf(stderr, "Error: Chip ID (chipid,c) too long.\n");
422           exit(1);
423         }
424         if (strlen(parsed_args.signature_arg) >= SIG1_LEN) {
425           fprintf(stderr, "Error: Magic string (signature,a) too long.\n");
426           exit(1);
427         }
428         if (strlen(parsed_args.signature2_arg) >= SIG2_LEN) {
429           fprintf(stderr, "Error: Second magic string (signature2,m) too long.\n");
430           exit(1);
431         }
432         if (parsed_args.layoutver_given) {
433           if (strlen(parsed_args.layoutver_arg) > FLASHLAYOUTVER_LEN) {
434                 fprintf(stderr, "Error: Flash layout version (layoutver,y) too long.\n");
435                 exit(1);
436           }
437         }
438         if (parsed_args.rsa_signature_given) {
439           if (strlen(parsed_args.rsa_signature_arg) > RSASIG_LEN) {
440                 fprintf(stderr, "Error: RSA Signature (rsa_signature,r) too long.\n");
441                 exit(1);
442           }
443         }
444
445         if (parsed_args.info1_given) {
446           if (strlen(parsed_args.info1_arg) >= TAGINFO1_LEN) {
447                 fprintf(stderr, "Error: Vendor Information 1 (info1) too long.\n");
448                 exit(1);
449           }
450         }
451
452         if (parsed_args.info2_given) {
453           if (strlen(parsed_args.info2_arg) >= TAGINFO2_LEN) {
454                 fprintf(stderr, "Error: Vendor Information 2 (info2) too long.\n");
455                 exit(1);
456           }
457         }
458
459         if (parsed_args.altinfo_given) {
460           if (strlen(parsed_args.altinfo_arg) >= ALTTAGINFO_LEN) {
461                 fprintf(stderr, "Error: Vendor Information 1 (info1) too long.\n");
462                 exit(1);
463           }
464         }
465
466         if (parsed_args.pad_given) {
467           if (parsed_args.pad_arg < 0) {
468                 fprintf(stderr, "Error: pad size must be positive.\r");
469                 exit(1);
470           }
471         }
472
473         flash_start = strtoul(parsed_args.flash_start_arg, NULL, 16);
474         image_offset = strtoul(parsed_args.image_offset_arg, NULL, 16);
475         block_size = strtoul(parsed_args.block_size_arg, NULL, 16);
476
477         if (!parsed_args.kernel_file_has_header_flag) {
478           load_address = strtoul(parsed_args.load_addr_arg, NULL, 16);
479           entry = strtoul(parsed_args.entry_arg, NULL, 16);
480           if (load_address == 0) {
481                 fprintf(stderr, "Error: Invalid value for load address\n");
482           }
483           if (entry == 0) {
484                 fprintf(stderr, "Error: Invalid value for entry\n");
485           }
486         }
487         
488         return tagfile(kernel, rootfs, bin, &parsed_args, flash_start, image_offset, block_size, load_address, entry);
489 }