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
6 * Copyright (C) 2008 Axel Gembe <ago@bastart.eu.org>
7 * Copyright (C) 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
17 #include <netinet/in.h>
20 #include "imagetag_cmdline.h"
23 #define DEADCODE 0xDEADC0DE
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 */
32 static char pirellitab
[NUM_PIRELLI
][BOARDID_LEN
] = PIRELLI_BOARDS
;
34 void int2tag(char *tag
, uint32_t value
) {
35 uint32_t network
= htonl(value
);
36 memcpy(tag
, (char *)(&network
), 4);
39 uint32_t compute_crc32(uint32_t crc
, FILE *binfile
, size_t compute_start
, size_t compute_len
)
41 uint8_t readbuf
[1024];
44 fseek(binfile
, compute_start
, SEEK_SET
);
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
;
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
);
62 size_t getlen(FILE *fp
)
64 size_t retval
, curpos
;
70 fseek(fp
, 0, SEEK_END
);
72 fseek(fp
, curpos
, SEEK_SET
);
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
)
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
;
94 const uint32_t deadcode
= htonl(DEADCODE
);
99 memset(&tag
, 0, sizeof(struct bcm_tag
));
101 if (!kernel
|| !rootfs
) {
102 fprintf(stderr
, "imagetag can't create an image without both kernel and rootfs\n");
105 if (kernel
&& !(kernelfile
= fopen(kernel
, "rb"))) {
106 fprintf(stderr
, "Unable to open kernel \"%s\"\n", kernel
);
110 if (rootfs
&& !(rootfsfile
= fopen(rootfs
, "rb"))) {
111 fprintf(stderr
, "Unable to open rootfs \"%s\"\n", rootfs
);
115 if (!bin
|| !(binfile
= fopen(bin
, "wb+"))) {
116 fprintf(stderr
, "Unable to open output file \"%s\"\n", bin
);
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
);
126 fwaddr
= flash_start
+ image_offset
;
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
);
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
);
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
);
148 kernellen
= getlen(kernelfile
);
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
);
156 /* Increase the kernel size by the header size */
157 kernellen
+= sizeof(khdr
);
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
);
168 /* Seek to the start of the kernel */
169 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
171 /* Write the kernel header */
172 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
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
);
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
);
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
);
192 oldrootfslen
= rootfslen
;
193 if (args
->pad_given
) {
194 uint32_t allfs
= 0xffffffff;
195 uint32_t pad_size
= args
->pad_arg
* 1024 * 1024;
197 printf("Padding image to %d bytes ...\n", pad_size
);
198 while (imagelen
< pad_size
) {
199 fwrite(&allfs
, sizeof(uint32_t), 1, binfile
);
205 /* Flush the binfile buffer so that when we read from file, it contains
206 * everything in the buffer
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
225 rootfscrc
= compute_crc32(rootfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, rootfslen
+ sizeof(deadcode
));
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
;
236 kerneloff
= rootfsoff
+ rootfslen
;
237 kernellen
= getlen(kernelfile
);
239 imagelen
= cfelen
+ rootfslen
+ kernellen
;
241 /* Seek to the start of the kernel */
242 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
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
);
250 /* Write the kernel header */
251 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
253 /* Increase the kernel size by the header size */
254 kernellen
+= sizeof(khdr
);
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
);
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
);
270 /* Flush the binfile buffer so that when we read from file, it contains
271 * everything in the buffer
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
);
283 /* Close the files */
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
);
299 if (args
->cfe_given
) {
300 sprintf(tag
.cfeAddress
, "%lu", flash_start
);
301 sprintf(tag
.cfeLength
, "%lu", cfelen
);
303 /* We don't include CFE */
304 strcpy(tag
.cfeAddress
, "0");
305 strcpy(tag
.cfeLength
, "0");
308 sprintf(tag
.kernelAddress
, "%lu", kerneloff
);
309 sprintf(tag
.kernelLength
, "%lu", kernellen
+ rootfsoffpadlen
);
311 if (args
->root_first_flag
) {
312 sprintf(tag
.flashImageStart
, "%lu", rootfsoff
);
313 sprintf(tag
.flashRootLength
, "%lu", rootfslen
);
315 sprintf(tag
.flashImageStart
, "%lu", kerneloff
);
316 sprintf(tag
.flashRootLength
, "%lu", rootfslen
+ sizeof(deadcode
));
318 int2tag(tag
.rootLength
, oldrootfslen
+ sizeof(deadcode
));
320 if (args
->rsa_signature_given
) {
321 strncpy(tag
.rsa_signature
, args
->rsa_signature_arg
, RSASIG_LEN
);
324 if (args
->layoutver_given
) {
325 strncpy(tag
.flashLayoutVer
, args
->layoutver_arg
, TAGLAYOUT_LEN
);
328 if (args
->info1_given
) {
329 strncpy(tag
.information1
, args
->info1_arg
, TAGINFO1_LEN
);
332 if (args
->info2_given
) {
333 strncpy(tag
.information2
, args
->info2_arg
, TAGINFO2_LEN
);
336 if (args
->reserved2_given
) {
337 strncpy(tag
.reserved2
, args
->reserved2_arg
, 16);
340 if (args
->altinfo_given
) {
341 strncpy(&tag
.information1
[0], args
->altinfo_arg
, ALTTAGINFO_LEN
);
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
);
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
);
356 for (i
= 0; i
< NUM_PIRELLI
; i
++) {
357 if (strncmp(args
->boardid_arg
, pirellitab
[i
], BOARDID_LEN
) == 0) {
364 int2tag(tag
.imageCRC
, kernelfscrc
);
366 int2tag(tag
.imageCRC
, kernelcrc
);
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));
374 fseek(binfile
, 0L, SEEK_SET
);
375 fwrite(&tag
, sizeof(uint8_t), sizeof(tag
), binfile
);
383 int main(int argc
, char **argv
)
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
;
391 kernel
= rootfs
= bin
= NULL
;
393 if (cmdline_parser(argc
, argv
, &parsed_args
)) {
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");
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");
409 if (strlen(parsed_args
.boardid_arg
) >= BOARDID_LEN
) {
410 fprintf(stderr
, "Error: Board ID (boardid,b) too long.\n");
413 if (strlen(parsed_args
.chipid_arg
) >= CHIPID_LEN
) {
414 fprintf(stderr
, "Error: Chip ID (chipid,c) too long.\n");
417 if (strlen(parsed_args
.signature_arg
) >= SIG1_LEN
) {
418 fprintf(stderr
, "Error: Magic string (signature,a) too long.\n");
421 if (strlen(parsed_args
.signature2_arg
) >= SIG2_LEN
) {
422 fprintf(stderr
, "Error: Second magic string (signature2,m) too long.\n");
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");
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");
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");
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");
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");
459 if (parsed_args
.pad_given
) {
460 if (parsed_args
.pad_arg
< 0) {
461 fprintf(stderr
, "Error: pad size must be positive.\r");
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);
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");
477 fprintf(stderr
, "Error: Invalid value for entry\n");
481 return tagfile(kernel
, rootfs
, bin
, &parsed_args
, flash_start
, image_offset
, block_size
, load_address
, entry
);