firmware-utils: honor env SOURCE_DATE_EPOCH
[oweals/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 <unistd.h>
15 #include <sys/stat.h>
16 #include <netinet/in.h>
17 #include <inttypes.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           else
167                 rootfsoff = (rootfsoff % 4) > 0 ? (((rootfsoff / 4) + 1) * 4) : rootfsoff;
168
169           /* align the end */
170           rootfsend = rootfsoff + getlen(rootfsfile);
171           if ((rootfsend % block_size) > 0)
172                 rootfsend = (((rootfsend / block_size) + 1) * block_size);
173           rootfslen = rootfsend - rootfsoff;
174           imagelen = rootfsoff + rootfslen - kerneloff + sizeof(deadcode);
175           rootfsoffpadlen = rootfsoff - (kerneloff + kernellen);
176           
177           /* Seek to the start of the kernel */
178           fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
179           
180           /* Write the kernel header */
181           fwrite(&khdr, sizeof(khdr), 1, binfile);
182           
183           /* Write the kernel */
184           while (kernelfile && !feof(kernelfile) && !ferror(kernelfile)) {
185                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), kernelfile);
186                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
187           }
188
189           /* Write the RootFS */
190           fseek(binfile, rootfsoff - fwaddr + cfelen, SEEK_SET);
191           while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) {
192                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile);
193                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
194           }
195
196           /* Align image to specified erase block size and append deadc0de */
197           printf("Data alignment to %dk with 'deadc0de' appended\n", block_size/1024);
198           fseek(binfile, rootfsoff + rootfslen - fwaddr + cfelen, SEEK_SET);
199           fwrite(&deadcode, sizeof(uint32_t), 1, binfile);
200
201           oldrootfslen = rootfslen;
202           if (args->pad_given) {
203                 uint32_t allfs = 0xffffffff;
204                 uint32_t pad_size = args->pad_arg * 1024 * 1024;
205
206                 printf("Padding image to %d bytes ...\n", pad_size);
207                 while (imagelen < pad_size) {
208                         fwrite(&allfs, sizeof(uint32_t), 1, binfile);
209                         imagelen += 4;
210                         rootfslen += 4;
211                 }
212           }
213
214           /* Flush the binfile buffer so that when we read from file, it contains
215            * everything in the buffer
216            */
217           fflush(binfile);
218
219           /* Compute the crc32 of the entire image (deadC0de included) */
220           imagecrc = compute_crc32(imagecrc, binfile, kerneloff - fwaddr + cfelen, imagelen);
221           /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
222           kernelcrc = compute_crc32(kernelcrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen);
223           /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
224           kernelfscrc = compute_crc32(kernelfscrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen + rootfslen + sizeof(deadcode));
225           /* Compute the crc32 of the flashImageStart to rootLength.
226            * The broadcom firmware assumes the rootfs starts the image,
227            * therefore uses the rootfs start to determine where to flash
228            * the image.  Since we have the kernel first we have to give
229            * it the kernel address, but the crc uses the length
230            * associated with this address, which is added to the kernel
231            * length to determine the length of image to flash and thus
232            * needs to be rootfs + deadcode
233            */
234           rootfscrc = compute_crc32(rootfscrc, binfile, kerneloff - fwaddr + cfelen, rootfslen + sizeof(deadcode));
235
236         } else {
237           /* Build the kernel address and length (doesn't need to be aligned, read only) */
238           rootfsoff = fwaddr + sizeof(tag);
239           oldrootfslen = getlen(rootfsfile);
240           rootfslen = oldrootfslen;
241           rootfslen = ( (rootfslen % block_size) > 0 ? (((rootfslen / block_size) + 1) * block_size) : rootfslen );
242           kerneloffpadlen = rootfslen - oldrootfslen;
243           oldrootfslen = rootfslen;
244
245           kerneloff = rootfsoff + rootfslen;
246           kernellen = getlen(kernelfile);
247
248           imagelen = cfelen + rootfslen + kernellen;
249           
250           /* Seek to the start of the kernel */
251           fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
252           
253           if (!args->kernel_file_has_header_flag) {
254                 /* Build the kernel header */
255                 khdr.loadaddr   = htonl(load_address);
256                 khdr.entry      = htonl(entry);
257                 khdr.lzmalen    = htonl(kernellen);
258                 
259                 /* Write the kernel header */
260                 fwrite(&khdr, sizeof(khdr), 1, binfile);
261           
262                 /* Increase the kernel size by the header size */
263                 kernellen += sizeof(khdr);        
264           }
265           
266           /* Write the kernel */
267           while (kernelfile && !feof(kernelfile) && !ferror(kernelfile)) {
268                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), kernelfile);
269                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
270           }
271
272           /* Write the RootFS */
273           fseek(binfile, rootfsoff - fwaddr + cfelen, SEEK_SET);
274           while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) {
275                 read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile);
276                 fwrite(readbuf, sizeof(uint8_t), read, binfile);
277           }
278
279           /* Flush the binfile buffer so that when we read from file, it contains
280            * everything in the buffer
281            */
282           fflush(binfile);
283
284           /* Compute the crc32 of the entire image (deadC0de included) */
285           imagecrc = compute_crc32(imagecrc, binfile, sizeof(tag), imagelen);
286           /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
287           kernelcrc = compute_crc32(kernelcrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen);
288           kernelfscrc = compute_crc32(kernelfscrc, binfile, rootfsoff - fwaddr + cfelen, kernellen + rootfslen);
289           rootfscrc = compute_crc32(rootfscrc, binfile, rootfsoff - fwaddr + cfelen, rootfslen);
290         }
291
292         /* Close the files */
293         if (cfefile) {
294           fclose(cfefile);
295         }
296         fclose(kernelfile);
297         fclose(rootfsfile);
298
299         /* Build the tag */
300         strncpy(tag.tagVersion, args->tag_version_arg, sizeof(tag.tagVersion) - 1);
301         strncpy(tag.sig_1, args->signature_arg, sizeof(tag.sig_1) - 1);
302         strncpy(tag.sig_2, args->signature2_arg, sizeof(tag.sig_2) - 1);
303         strncpy(tag.chipid, args->chipid_arg, sizeof(tag.chipid) - 1);
304         strncpy(tag.boardid, args->boardid_arg, sizeof(tag.boardid) - 1);
305         strcpy(tag.big_endian, "1");
306         sprintf(tag.totalLength, "%lu", imagelen);
307
308         if (args->cfe_given) {
309           sprintf(tag.cfeAddress, "%" PRIu32, flash_start);
310           sprintf(tag.cfeLength, "%lu", cfelen);
311         } else {
312           /* We don't include CFE */
313           strcpy(tag.cfeAddress, "0");
314           strcpy(tag.cfeLength, "0");
315         }
316
317         sprintf(tag.kernelAddress, "%lu", kerneloff);
318         sprintf(tag.kernelLength, "%lu", kernellen + rootfsoffpadlen);
319
320         if (args->root_first_flag) {
321           sprintf(tag.flashImageStart, "%lu", rootfsoff);
322           sprintf(tag.flashRootLength, "%lu", rootfslen);         
323         } else {
324           sprintf(tag.flashImageStart, "%lu", kerneloff);
325           sprintf(tag.flashRootLength, "%lu", rootfslen + sizeof(deadcode));
326         }
327         int2tag(tag.rootLength, oldrootfslen + sizeof(deadcode));
328
329         if (args->rsa_signature_given) {
330             strncpy(tag.rsa_signature, args->rsa_signature_arg, RSASIG_LEN);
331         }
332
333         if (args->layoutver_given) {
334             strncpy(tag.flashLayoutVer, args->layoutver_arg, TAGLAYOUT_LEN);
335         }
336
337         if (args->info1_given) {
338           strncpy(tag.information1, args->info1_arg, TAGINFO1_LEN);
339         }
340
341         if (args->info2_given) {
342           strncpy(tag.information2, args->info2_arg, TAGINFO2_LEN);
343         }
344
345         if (args->reserved2_given) {
346           strncpy(tag.reserved2, args->reserved2_arg, 16);
347         }
348
349         if (args->altinfo_given) {
350           strncpy(tag.information1, args->altinfo_arg, TAGINFO1_LEN);
351         }
352
353         if (args->second_image_flag_given) {
354           if (strncmp(args->second_image_flag_arg, "2", DUALFLAG_LEN) != 0) {           
355                 strncpy(tag.dualImage, args->second_image_flag_arg, DUALFLAG_LEN);
356           }
357         }
358
359         if (args->inactive_given) {
360           if (strncmp(args->inactive_arg, "2", INACTIVEFLAG_LEN) != 0) {                
361                 strncpy(tag.inactiveFlag, args->second_image_flag_arg, INACTIVEFLAG_LEN);
362           }
363         }
364
365         for (i = 0; i < NUM_PIRELLI; i++) {
366                 if (strncmp(args->boardid_arg, pirellitab[i], BOARDID_LEN) == 0) {
367                         is_pirelli = 1;
368                         break;
369                 }
370         }
371
372         if ( !is_pirelli ) {
373           int2tag(tag.imageCRC, kernelfscrc);
374         } else {
375           int2tag(tag.imageCRC, kernelcrc);
376         }
377
378         int2tag(&(tag.rootfsCRC[0]), rootfscrc);
379         int2tag(tag.kernelCRC, kernelcrc);
380         int2tag(tag.fskernelCRC, kernelfscrc);
381         int2tag(tag.headerCRC, cyg_crc32_accumulate(IMAGETAG_CRC_START, (uint8_t*)&tag, sizeof(tag) - 20));
382
383         fseek(binfile, 0L, SEEK_SET);
384         fwrite(&tag, sizeof(uint8_t), sizeof(tag), binfile);
385
386     fflush(binfile);
387         fclose(binfile);
388
389         return 0;
390 }
391
392 int main(int argc, char **argv)
393 {
394     int c, i;
395         char *kernel, *rootfs, *bin;
396         uint32_t flash_start, image_offset, block_size, load_address, entry;
397         flash_start = image_offset = block_size = load_address = entry = 0;
398         struct gengetopt_args_info parsed_args;
399
400         kernel = rootfs = bin = NULL;
401
402         if (imagetag_cmdline(argc, argv, &parsed_args)) {
403           exit(1);
404         }
405
406         printf("Broadcom 63xx image tagger - v2.0.0\n");
407         printf("Copyright (C) 2008 Axel Gembe\n");
408         printf("Copyright (C) 2009-2010 Daniel Dickinson\n");
409         printf("Licensed under the terms of the Gnu General Public License\n");
410
411         kernel = parsed_args.kernel_arg;
412         rootfs = parsed_args.rootfs_arg;
413         bin = parsed_args.output_arg;
414         if (strlen(parsed_args.tag_version_arg) >= TAGVER_LEN) {
415           fprintf(stderr, "Error: Tag Version (tag_version,v) too long.\n");
416           exit(1);
417         }
418         if (strlen(parsed_args.boardid_arg) >= BOARDID_LEN) {
419           fprintf(stderr, "Error: Board ID (boardid,b) too long.\n");
420           exit(1);
421         }
422         if (strlen(parsed_args.chipid_arg) >= CHIPID_LEN) {
423           fprintf(stderr, "Error: Chip ID (chipid,c) too long.\n");
424           exit(1);
425         }
426         if (strlen(parsed_args.signature_arg) >= SIG1_LEN) {
427           fprintf(stderr, "Error: Magic string (signature,a) too long.\n");
428           exit(1);
429         }
430         if (strlen(parsed_args.signature2_arg) >= SIG2_LEN) {
431           fprintf(stderr, "Error: Second magic string (signature2,m) too long.\n");
432           exit(1);
433         }
434         if (parsed_args.layoutver_given) {
435           if (strlen(parsed_args.layoutver_arg) > FLASHLAYOUTVER_LEN) {
436                 fprintf(stderr, "Error: Flash layout version (layoutver,y) too long.\n");
437                 exit(1);
438           }
439         }
440         if (parsed_args.rsa_signature_given) {
441           if (strlen(parsed_args.rsa_signature_arg) > RSASIG_LEN) {
442                 fprintf(stderr, "Error: RSA Signature (rsa_signature,r) too long.\n");
443                 exit(1);
444           }
445         }
446
447         if (parsed_args.info1_given) {
448           if (strlen(parsed_args.info1_arg) >= TAGINFO1_LEN) {
449                 fprintf(stderr, "Error: Vendor Information 1 (info1) too long.\n");
450                 exit(1);
451           }
452         }
453
454         if (parsed_args.info2_given) {
455           if (strlen(parsed_args.info2_arg) >= TAGINFO2_LEN) {
456                 fprintf(stderr, "Error: Vendor Information 2 (info2) too long.\n");
457                 exit(1);
458           }
459         }
460
461         if (parsed_args.altinfo_given) {
462           if (strlen(parsed_args.altinfo_arg) >= ALTTAGINFO_LEN) {
463                 fprintf(stderr, "Error: Vendor Information 1 (info1) too long.\n");
464                 exit(1);
465           }
466         }
467
468         if (parsed_args.pad_given) {
469           if (parsed_args.pad_arg < 0) {
470                 fprintf(stderr, "Error: pad size must be positive.\r");
471                 exit(1);
472           }
473         }
474
475         flash_start = strtoul(parsed_args.flash_start_arg, NULL, 16);
476         image_offset = strtoul(parsed_args.image_offset_arg, NULL, 16);
477         block_size = strtoul(parsed_args.block_size_arg, NULL, 16);
478
479         if (!parsed_args.kernel_file_has_header_flag) {
480           load_address = strtoul(parsed_args.load_addr_arg, NULL, 16);
481           entry = strtoul(parsed_args.entry_arg, NULL, 16);
482           if (load_address == 0) {
483                 fprintf(stderr, "Error: Invalid value for load address\n");
484           }
485           if (entry == 0) {
486                 fprintf(stderr, "Error: Invalid value for entry\n");
487           }
488         }
489         
490         return tagfile(kernel, rootfs, bin, &parsed_args, flash_start, image_offset, block_size, load_address, entry);
491 }