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