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 cfelen
, kerneloff
, kernellen
, rootfsoff
, rootfslen
, \
86 read
, imagelen
, rootfsoffpadlen
= 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
;
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 cfelen
= getlen(cfefile
);
129 /* Seek to the start of the file after tag */
130 fseek(binfile
, sizeof(tag
), SEEK_SET
);
133 while (cfefile
&& !feof(cfefile
) && !ferror(cfefile
)) {
134 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), cfefile
);
135 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
142 if (!args
->root_first_flag
) {
143 /* Build the kernel address and length (doesn't need to be aligned, read only) */
144 kerneloff
= fwaddr
+ sizeof(tag
);
146 kernellen
= getlen(kernelfile
);
148 if (!args
->kernel_file_has_header_flag
) {
149 /* Build the kernel header */
150 khdr
.loadaddr
= htonl(load_address
);
151 khdr
.entry
= htonl(entry
);
152 khdr
.lzmalen
= htonl(kernellen
);
154 /* Increase the kernel size by the header size */
155 kernellen
+= sizeof(khdr
);
158 /* Build the rootfs address and length */
159 rootfsoff
= kerneloff
+ kernellen
;
160 /* align the start if requested */
161 if (args
->align_rootfs_flag
)
162 rootfsoff
= (rootfsoff
% block_size
) > 0 ? (((rootfsoff
/ block_size
) + 1) * block_size
) : rootfsoff
;
164 rootfsoff
= (rootfsoff
% 4) > 0 ? (((rootfsoff
/ 4) + 1) * 4) : rootfsoff
;
167 rootfsend
= rootfsoff
+ getlen(rootfsfile
);
168 if ((rootfsend
% block_size
) > 0)
169 rootfsend
= (((rootfsend
/ block_size
) + 1) * block_size
);
170 rootfslen
= rootfsend
- rootfsoff
;
171 imagelen
= rootfsoff
+ rootfslen
- kerneloff
+ sizeof(deadcode
);
172 rootfsoffpadlen
= rootfsoff
- (kerneloff
+ kernellen
);
174 /* Seek to the start of the kernel */
175 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
177 /* Write the kernel header */
178 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
180 /* Write the kernel */
181 while (kernelfile
&& !feof(kernelfile
) && !ferror(kernelfile
)) {
182 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), kernelfile
);
183 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
186 /* Write the RootFS */
187 fseek(binfile
, rootfsoff
- fwaddr
+ cfelen
, SEEK_SET
);
188 while (rootfsfile
&& !feof(rootfsfile
) && !ferror(rootfsfile
)) {
189 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), rootfsfile
);
190 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
193 /* Align image to specified erase block size and append deadc0de */
194 printf("Data alignment to %dk with 'deadc0de' appended\n", block_size
/1024);
195 fseek(binfile
, rootfsoff
+ rootfslen
- fwaddr
+ cfelen
, SEEK_SET
);
196 fwrite(&deadcode
, sizeof(uint32_t), 1, binfile
);
198 oldrootfslen
= rootfslen
;
199 if (args
->pad_given
) {
200 uint32_t allfs
= 0xffffffff;
201 uint32_t pad_size
= args
->pad_arg
* 1024 * 1024;
203 printf("Padding image to %d bytes ...\n", pad_size
);
204 while (imagelen
< pad_size
) {
205 fwrite(&allfs
, sizeof(uint32_t), 1, binfile
);
211 /* Flush the binfile buffer so that when we read from file, it contains
212 * everything in the buffer
216 /* Compute the crc32 of the entire image (deadC0de included) */
217 imagecrc
= compute_crc32(imagecrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, imagelen
);
218 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
219 kernelcrc
= compute_crc32(kernelcrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
);
220 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
221 kernelfscrc
= compute_crc32(kernelfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
+ rootfslen
+ sizeof(deadcode
));
222 /* Compute the crc32 of the flashImageStart to rootLength.
223 * The broadcom firmware assumes the rootfs starts the image,
224 * therefore uses the rootfs start to determine where to flash
225 * the image. Since we have the kernel first we have to give
226 * it the kernel address, but the crc uses the length
227 * associated with this address, which is added to the kernel
228 * length to determine the length of image to flash and thus
229 * needs to be rootfs + deadcode
231 rootfscrc
= compute_crc32(rootfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, rootfslen
+ sizeof(deadcode
));
234 /* Build the kernel address and length (doesn't need to be aligned, read only) */
235 rootfsoff
= fwaddr
+ sizeof(tag
);
236 oldrootfslen
= getlen(rootfsfile
);
237 rootfslen
= oldrootfslen
;
238 rootfslen
= ( (rootfslen
% block_size
) > 0 ? (((rootfslen
/ block_size
) + 1) * block_size
) : rootfslen
);
239 oldrootfslen
= rootfslen
;
241 kerneloff
= rootfsoff
+ rootfslen
;
242 kernellen
= getlen(kernelfile
);
244 imagelen
= cfelen
+ rootfslen
+ kernellen
;
246 /* Seek to the start of the kernel */
247 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
249 if (!args
->kernel_file_has_header_flag
) {
250 /* Build the kernel header */
251 khdr
.loadaddr
= htonl(load_address
);
252 khdr
.entry
= htonl(entry
);
253 khdr
.lzmalen
= htonl(kernellen
);
255 /* Write the kernel header */
256 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
258 /* Increase the kernel size by the header size */
259 kernellen
+= sizeof(khdr
);
262 /* Write the kernel */
263 while (kernelfile
&& !feof(kernelfile
) && !ferror(kernelfile
)) {
264 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), kernelfile
);
265 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
268 /* Write the RootFS */
269 fseek(binfile
, rootfsoff
- fwaddr
+ cfelen
, SEEK_SET
);
270 while (rootfsfile
&& !feof(rootfsfile
) && !ferror(rootfsfile
)) {
271 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), rootfsfile
);
272 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
275 /* Flush the binfile buffer so that when we read from file, it contains
276 * everything in the buffer
280 /* Compute the crc32 of the entire image (deadC0de included) */
281 imagecrc
= compute_crc32(imagecrc
, binfile
, sizeof(tag
), imagelen
);
282 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
283 kernelcrc
= compute_crc32(kernelcrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
);
284 kernelfscrc
= compute_crc32(kernelfscrc
, binfile
, rootfsoff
- fwaddr
+ cfelen
, kernellen
+ rootfslen
);
285 rootfscrc
= compute_crc32(rootfscrc
, binfile
, rootfsoff
- fwaddr
+ cfelen
, rootfslen
);
288 /* Close the files */
296 strncpy(tag
.tagVersion
, args
->tag_version_arg
, sizeof(tag
.tagVersion
) - 1);
297 strncpy(tag
.sig_1
, args
->signature_arg
, sizeof(tag
.sig_1
) - 1);
298 strncpy(tag
.sig_2
, args
->signature2_arg
, sizeof(tag
.sig_2
) - 1);
299 strncpy(tag
.chipid
, args
->chipid_arg
, sizeof(tag
.chipid
) - 1);
300 strncpy(tag
.boardid
, args
->boardid_arg
, sizeof(tag
.boardid
) - 1);
301 strcpy(tag
.big_endian
, "1");
302 sprintf(tag
.totalLength
, "%lu", imagelen
);
304 if (args
->cfe_given
) {
305 sprintf(tag
.cfeAddress
, "%" PRIu32
, flash_start
);
306 sprintf(tag
.cfeLength
, "%lu", cfelen
);
308 /* We don't include CFE */
309 strcpy(tag
.cfeAddress
, "0");
310 strcpy(tag
.cfeLength
, "0");
313 sprintf(tag
.kernelAddress
, "%lu", kerneloff
);
314 sprintf(tag
.kernelLength
, "%lu", kernellen
+ rootfsoffpadlen
);
316 if (args
->root_first_flag
) {
317 sprintf(tag
.flashImageStart
, "%lu", rootfsoff
);
318 sprintf(tag
.flashRootLength
, "%lu", rootfslen
);
320 sprintf(tag
.flashImageStart
, "%lu", kerneloff
);
321 sprintf(tag
.flashRootLength
, "%lu", rootfslen
+ sizeof(deadcode
));
323 int2tag(tag
.rootLength
, oldrootfslen
+ sizeof(deadcode
));
325 if (args
->rsa_signature_given
) {
326 strncpy(tag
.rsa_signature
, args
->rsa_signature_arg
, RSASIG_LEN
);
329 if (args
->layoutver_given
) {
330 strncpy(tag
.flashLayoutVer
, args
->layoutver_arg
, TAGLAYOUT_LEN
);
333 if (args
->info1_given
) {
334 strncpy(tag
.information1
, args
->info1_arg
, TAGINFO1_LEN
);
337 if (args
->info2_given
) {
338 strncpy(tag
.information2
, args
->info2_arg
, TAGINFO2_LEN
);
341 if (args
->reserved2_given
) {
342 strncpy(tag
.reserved2
, args
->reserved2_arg
, 16);
345 if (args
->altinfo_given
) {
346 strncpy(tag
.information1
, args
->altinfo_arg
, TAGINFO1_LEN
);
349 if (args
->second_image_flag_given
) {
350 if (strncmp(args
->second_image_flag_arg
, "2", DUALFLAG_LEN
) != 0) {
351 strncpy(tag
.dualImage
, args
->second_image_flag_arg
, DUALFLAG_LEN
);
355 if (args
->inactive_given
) {
356 if (strncmp(args
->inactive_arg
, "2", INACTIVEFLAG_LEN
) != 0) {
357 strncpy(tag
.inactiveFlag
, args
->second_image_flag_arg
, INACTIVEFLAG_LEN
);
361 for (i
= 0; i
< NUM_PIRELLI
; i
++) {
362 if (strncmp(args
->boardid_arg
, pirellitab
[i
], BOARDID_LEN
) == 0) {
369 int2tag(tag
.imageCRC
, kernelfscrc
);
371 int2tag(tag
.imageCRC
, kernelcrc
);
374 int2tag(&(tag
.rootfsCRC
[0]), rootfscrc
);
375 int2tag(tag
.kernelCRC
, kernelcrc
);
376 int2tag(tag
.fskernelCRC
, kernelfscrc
);
377 int2tag(tag
.headerCRC
, cyg_crc32_accumulate(IMAGETAG_CRC_START
, (uint8_t*)&tag
, sizeof(tag
) - 20));
379 fseek(binfile
, 0L, SEEK_SET
);
380 fwrite(&tag
, sizeof(uint8_t), sizeof(tag
), binfile
);
388 int main(int argc
, char **argv
)
390 char *kernel
, *rootfs
, *bin
;
391 uint32_t flash_start
, image_offset
, block_size
, load_address
, entry
;
392 flash_start
= image_offset
= block_size
= load_address
= entry
= 0;
393 struct gengetopt_args_info parsed_args
;
395 kernel
= rootfs
= bin
= NULL
;
397 if (imagetag_cmdline(argc
, argv
, &parsed_args
)) {
401 printf("Broadcom 63xx image tagger - v2.0.0\n");
402 printf("Copyright (C) 2008 Axel Gembe\n");
403 printf("Copyright (C) 2009-2010 Daniel Dickinson\n");
404 printf("Licensed under the terms of the Gnu General Public License\n");
406 kernel
= parsed_args
.kernel_arg
;
407 rootfs
= parsed_args
.rootfs_arg
;
408 bin
= parsed_args
.output_arg
;
409 if (strlen(parsed_args
.tag_version_arg
) >= TAGVER_LEN
) {
410 fprintf(stderr
, "Error: Tag Version (tag_version,v) too long.\n");
413 if (strlen(parsed_args
.boardid_arg
) >= BOARDID_LEN
) {
414 fprintf(stderr
, "Error: Board ID (boardid,b) too long.\n");
417 if (strlen(parsed_args
.chipid_arg
) >= CHIPID_LEN
) {
418 fprintf(stderr
, "Error: Chip ID (chipid,c) too long.\n");
421 if (strlen(parsed_args
.signature_arg
) >= SIG1_LEN
) {
422 fprintf(stderr
, "Error: Magic string (signature,a) too long.\n");
425 if (strlen(parsed_args
.signature2_arg
) >= SIG2_LEN
) {
426 fprintf(stderr
, "Error: Second magic string (signature2,m) too long.\n");
429 if (parsed_args
.layoutver_given
) {
430 if (strlen(parsed_args
.layoutver_arg
) > FLASHLAYOUTVER_LEN
) {
431 fprintf(stderr
, "Error: Flash layout version (layoutver,y) too long.\n");
435 if (parsed_args
.rsa_signature_given
) {
436 if (strlen(parsed_args
.rsa_signature_arg
) > RSASIG_LEN
) {
437 fprintf(stderr
, "Error: RSA Signature (rsa_signature,r) too long.\n");
442 if (parsed_args
.info1_given
) {
443 if (strlen(parsed_args
.info1_arg
) >= TAGINFO1_LEN
) {
444 fprintf(stderr
, "Error: Vendor Information 1 (info1) too long.\n");
449 if (parsed_args
.info2_given
) {
450 if (strlen(parsed_args
.info2_arg
) >= TAGINFO2_LEN
) {
451 fprintf(stderr
, "Error: Vendor Information 2 (info2) too long.\n");
456 if (parsed_args
.altinfo_given
) {
457 if (strlen(parsed_args
.altinfo_arg
) >= ALTTAGINFO_LEN
) {
458 fprintf(stderr
, "Error: Vendor Information 1 (info1) too long.\n");
463 if (parsed_args
.pad_given
) {
464 if (parsed_args
.pad_arg
< 0) {
465 fprintf(stderr
, "Error: pad size must be positive.\r");
470 flash_start
= strtoul(parsed_args
.flash_start_arg
, NULL
, 16);
471 image_offset
= strtoul(parsed_args
.image_offset_arg
, NULL
, 16);
472 block_size
= strtoul(parsed_args
.block_size_arg
, NULL
, 16);
474 if (!parsed_args
.kernel_file_has_header_flag
) {
475 load_address
= strtoul(parsed_args
.load_addr_arg
, NULL
, 16);
476 entry
= strtoul(parsed_args
.entry_arg
, NULL
, 16);
477 if (load_address
== 0) {
478 fprintf(stderr
, "Error: Invalid value for load address\n");
481 fprintf(stderr
, "Error: Invalid value for entry\n");
485 return tagfile(kernel
, rootfs
, bin
, &parsed_args
, flash_start
, image_offset
, block_size
, load_address
, entry
);