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>
16 #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
, \
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
;
95 const uint32_t deadcode
= htonl(DEADCODE
);
100 memset(&tag
, 0, sizeof(struct bcm_tag
));
102 if (!kernel
|| !rootfs
) {
103 fprintf(stderr
, "imagetag can't create an image without both kernel and rootfs\n");
106 if (kernel
&& !(kernelfile
= fopen(kernel
, "rb"))) {
107 fprintf(stderr
, "Unable to open kernel \"%s\"\n", kernel
);
111 if (rootfs
&& !(rootfsfile
= fopen(rootfs
, "rb"))) {
112 fprintf(stderr
, "Unable to open rootfs \"%s\"\n", rootfs
);
116 if (!bin
|| !(binfile
= fopen(bin
, "wb+"))) {
117 fprintf(stderr
, "Unable to open output file \"%s\"\n", bin
);
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
);
127 fwaddr
= flash_start
+ image_offset
;
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
);
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
);
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
);
149 kernellen
= getlen(kernelfile
);
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
);
157 /* Increase the kernel size by the header size */
158 kernellen
+= sizeof(khdr
);
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
;
167 rootfsoff
= (rootfsoff
% 4) > 0 ? (((rootfsoff
/ 4) + 1) * 4) : rootfsoff
;
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
);
177 /* Seek to the start of the kernel */
178 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
180 /* Write the kernel header */
181 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
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
);
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
);
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
);
201 oldrootfslen
= rootfslen
;
202 if (args
->pad_given
) {
203 uint32_t allfs
= 0xffffffff;
204 uint32_t pad_size
= args
->pad_arg
* 1024 * 1024;
206 printf("Padding image to %d bytes ...\n", pad_size
);
207 while (imagelen
< pad_size
) {
208 fwrite(&allfs
, sizeof(uint32_t), 1, binfile
);
214 /* Flush the binfile buffer so that when we read from file, it contains
215 * everything in the buffer
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
234 rootfscrc
= compute_crc32(rootfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, rootfslen
+ sizeof(deadcode
));
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
;
245 kerneloff
= rootfsoff
+ rootfslen
;
246 kernellen
= getlen(kernelfile
);
248 imagelen
= cfelen
+ rootfslen
+ kernellen
;
250 /* Seek to the start of the kernel */
251 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
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
);
259 /* Write the kernel header */
260 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
262 /* Increase the kernel size by the header size */
263 kernellen
+= sizeof(khdr
);
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
);
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
);
279 /* Flush the binfile buffer so that when we read from file, it contains
280 * everything in the buffer
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
);
292 /* Close the files */
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
);
308 if (args
->cfe_given
) {
309 sprintf(tag
.cfeAddress
, "%" PRIu32
, flash_start
);
310 sprintf(tag
.cfeLength
, "%lu", cfelen
);
312 /* We don't include CFE */
313 strcpy(tag
.cfeAddress
, "0");
314 strcpy(tag
.cfeLength
, "0");
317 sprintf(tag
.kernelAddress
, "%lu", kerneloff
);
318 sprintf(tag
.kernelLength
, "%lu", kernellen
+ rootfsoffpadlen
);
320 if (args
->root_first_flag
) {
321 sprintf(tag
.flashImageStart
, "%lu", rootfsoff
);
322 sprintf(tag
.flashRootLength
, "%lu", rootfslen
);
324 sprintf(tag
.flashImageStart
, "%lu", kerneloff
);
325 sprintf(tag
.flashRootLength
, "%lu", rootfslen
+ sizeof(deadcode
));
327 int2tag(tag
.rootLength
, oldrootfslen
+ sizeof(deadcode
));
329 if (args
->rsa_signature_given
) {
330 strncpy(tag
.rsa_signature
, args
->rsa_signature_arg
, RSASIG_LEN
);
333 if (args
->layoutver_given
) {
334 strncpy(tag
.flashLayoutVer
, args
->layoutver_arg
, TAGLAYOUT_LEN
);
337 if (args
->info1_given
) {
338 strncpy(tag
.information1
, args
->info1_arg
, TAGINFO1_LEN
);
341 if (args
->info2_given
) {
342 strncpy(tag
.information2
, args
->info2_arg
, TAGINFO2_LEN
);
345 if (args
->reserved2_given
) {
346 strncpy(tag
.reserved2
, args
->reserved2_arg
, 16);
349 if (args
->altinfo_given
) {
350 strncpy(tag
.information1
, args
->altinfo_arg
, TAGINFO1_LEN
);
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
);
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
);
365 for (i
= 0; i
< NUM_PIRELLI
; i
++) {
366 if (strncmp(args
->boardid_arg
, pirellitab
[i
], BOARDID_LEN
) == 0) {
373 int2tag(tag
.imageCRC
, kernelfscrc
);
375 int2tag(tag
.imageCRC
, kernelcrc
);
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));
383 fseek(binfile
, 0L, SEEK_SET
);
384 fwrite(&tag
, sizeof(uint8_t), sizeof(tag
), binfile
);
392 int main(int argc
, char **argv
)
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
;
400 kernel
= rootfs
= bin
= NULL
;
402 if (imagetag_cmdline(argc
, argv
, &parsed_args
)) {
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");
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");
418 if (strlen(parsed_args
.boardid_arg
) >= BOARDID_LEN
) {
419 fprintf(stderr
, "Error: Board ID (boardid,b) too long.\n");
422 if (strlen(parsed_args
.chipid_arg
) >= CHIPID_LEN
) {
423 fprintf(stderr
, "Error: Chip ID (chipid,c) too long.\n");
426 if (strlen(parsed_args
.signature_arg
) >= SIG1_LEN
) {
427 fprintf(stderr
, "Error: Magic string (signature,a) too long.\n");
430 if (strlen(parsed_args
.signature2_arg
) >= SIG2_LEN
) {
431 fprintf(stderr
, "Error: Second magic string (signature2,m) too long.\n");
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");
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");
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");
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");
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");
468 if (parsed_args
.pad_given
) {
469 if (parsed_args
.pad_arg
< 0) {
470 fprintf(stderr
, "Error: pad size must be positive.\r");
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);
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");
486 fprintf(stderr
, "Error: Invalid value for entry\n");
490 return tagfile(kernel
, rootfs
, bin
, &parsed_args
, flash_start
, image_offset
, block_size
, load_address
, entry
);