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