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>
21 #include "imagetag_cmdline.h"
24 #define DEADCODE 0xDEADC0DE
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 */
33 static char pirellitab
[NUM_PIRELLI
][BOARDID_LEN
] = PIRELLI_BOARDS
;
35 void int2tag(char *tag
, uint32_t value
) {
36 uint32_t network
= htonl(value
);
37 memcpy(tag
, (char *)(&network
), 4);
40 uint32_t compute_crc32(uint32_t crc
, FILE *binfile
, size_t compute_start
, size_t compute_len
)
42 uint8_t readbuf
[1024];
45 fseek(binfile
, compute_start
, SEEK_SET
);
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
;
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
);
63 size_t getlen(FILE *fp
)
65 size_t retval
, curpos
;
71 fseek(fp
, 0, SEEK_END
);
73 fseek(fp
, curpos
, SEEK_SET
);
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
)
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
, \
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
;
96 const uint32_t deadcode
= htonl(DEADCODE
);
101 memset(&tag
, 0, sizeof(struct bcm_tag
));
103 if (!kernel
|| !rootfs
) {
104 fprintf(stderr
, "imagetag can't create an image without both kernel and rootfs\n");
107 if (kernel
&& !(kernelfile
= fopen(kernel
, "rb"))) {
108 fprintf(stderr
, "Unable to open kernel \"%s\"\n", kernel
);
112 if (rootfs
&& !(rootfsfile
= fopen(rootfs
, "rb"))) {
113 fprintf(stderr
, "Unable to open rootfs \"%s\"\n", rootfs
);
117 if (!bin
|| !(binfile
= fopen(bin
, "wb+"))) {
118 fprintf(stderr
, "Unable to open output file \"%s\"\n", bin
);
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
);
128 fwaddr
= flash_start
+ image_offset
;
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
);
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
);
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
);
150 kernellen
= getlen(kernelfile
);
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
);
158 /* Increase the kernel size by the header size */
159 kernellen
+= sizeof(khdr
);
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
;
168 rootfsoff
= (rootfsoff
% 4) > 0 ? (((rootfsoff
/ 4) + 1) * 4) : rootfsoff
;
171 rootfsend
= rootfsoff
+ getlen(rootfsfile
);
172 if ((rootfsend
% block_size
) > 0)
173 rootfsend
= (((rootfsend
/ block_size
) + 1) * block_size
);
174 rootfslen
= rootfsend
- rootfsoff
;
175 imagelen
= rootfsoff
+ rootfslen
- kerneloff
+ sizeof(deadcode
);
176 rootfsoffpadlen
= rootfsoff
- (kerneloff
+ kernellen
);
178 /* Seek to the start of the kernel */
179 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
181 /* Write the kernel header */
182 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
184 /* Write the kernel */
185 while (kernelfile
&& !feof(kernelfile
) && !ferror(kernelfile
)) {
186 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), kernelfile
);
187 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
190 /* Write the RootFS */
191 fseek(binfile
, rootfsoff
- fwaddr
+ cfelen
, SEEK_SET
);
192 while (rootfsfile
&& !feof(rootfsfile
) && !ferror(rootfsfile
)) {
193 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), rootfsfile
);
194 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
197 /* Align image to specified erase block size and append deadc0de */
198 printf("Data alignment to %dk with 'deadc0de' appended\n", block_size
/1024);
199 fseek(binfile
, rootfsoff
+ rootfslen
- fwaddr
+ cfelen
, SEEK_SET
);
200 fwrite(&deadcode
, sizeof(uint32_t), 1, binfile
);
202 oldrootfslen
= rootfslen
;
203 if (args
->pad_given
) {
204 uint32_t allfs
= 0xffffffff;
205 uint32_t pad_size
= args
->pad_arg
* 1024 * 1024;
207 printf("Padding image to %d bytes ...\n", pad_size
);
208 while (imagelen
< pad_size
) {
209 fwrite(&allfs
, sizeof(uint32_t), 1, binfile
);
215 /* Flush the binfile buffer so that when we read from file, it contains
216 * everything in the buffer
220 /* Compute the crc32 of the entire image (deadC0de included) */
221 imagecrc
= compute_crc32(imagecrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, imagelen
);
222 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
223 kernelcrc
= compute_crc32(kernelcrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
);
224 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
225 kernelfscrc
= compute_crc32(kernelfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
+ rootfslen
+ sizeof(deadcode
));
226 /* Compute the crc32 of the flashImageStart to rootLength.
227 * The broadcom firmware assumes the rootfs starts the image,
228 * therefore uses the rootfs start to determine where to flash
229 * the image. Since we have the kernel first we have to give
230 * it the kernel address, but the crc uses the length
231 * associated with this address, which is added to the kernel
232 * length to determine the length of image to flash and thus
233 * needs to be rootfs + deadcode
235 rootfscrc
= compute_crc32(rootfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, rootfslen
+ sizeof(deadcode
));
238 /* Build the kernel address and length (doesn't need to be aligned, read only) */
239 rootfsoff
= fwaddr
+ sizeof(tag
);
240 oldrootfslen
= getlen(rootfsfile
);
241 rootfslen
= oldrootfslen
;
242 rootfslen
= ( (rootfslen
% block_size
) > 0 ? (((rootfslen
/ block_size
) + 1) * block_size
) : rootfslen
);
243 kerneloffpadlen
= rootfslen
- oldrootfslen
;
244 oldrootfslen
= rootfslen
;
246 kerneloff
= rootfsoff
+ rootfslen
;
247 kernellen
= getlen(kernelfile
);
249 imagelen
= cfelen
+ rootfslen
+ kernellen
;
251 /* Seek to the start of the kernel */
252 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
254 if (!args
->kernel_file_has_header_flag
) {
255 /* Build the kernel header */
256 khdr
.loadaddr
= htonl(load_address
);
257 khdr
.entry
= htonl(entry
);
258 khdr
.lzmalen
= htonl(kernellen
);
260 /* Write the kernel header */
261 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
263 /* Increase the kernel size by the header size */
264 kernellen
+= sizeof(khdr
);
267 /* Write the kernel */
268 while (kernelfile
&& !feof(kernelfile
) && !ferror(kernelfile
)) {
269 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), kernelfile
);
270 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
273 /* Write the RootFS */
274 fseek(binfile
, rootfsoff
- fwaddr
+ cfelen
, SEEK_SET
);
275 while (rootfsfile
&& !feof(rootfsfile
) && !ferror(rootfsfile
)) {
276 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), rootfsfile
);
277 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
280 /* Flush the binfile buffer so that when we read from file, it contains
281 * everything in the buffer
285 /* Compute the crc32 of the entire image (deadC0de included) */
286 imagecrc
= compute_crc32(imagecrc
, binfile
, sizeof(tag
), imagelen
);
287 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
288 kernelcrc
= compute_crc32(kernelcrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
);
289 kernelfscrc
= compute_crc32(kernelfscrc
, binfile
, rootfsoff
- fwaddr
+ cfelen
, kernellen
+ rootfslen
);
290 rootfscrc
= compute_crc32(rootfscrc
, binfile
, rootfsoff
- fwaddr
+ cfelen
, rootfslen
);
293 /* Close the files */
301 strncpy(tag
.tagVersion
, args
->tag_version_arg
, sizeof(tag
.tagVersion
) - 1);
302 strncpy(tag
.sig_1
, args
->signature_arg
, sizeof(tag
.sig_1
) - 1);
303 strncpy(tag
.sig_2
, args
->signature2_arg
, sizeof(tag
.sig_2
) - 1);
304 strncpy(tag
.chipid
, args
->chipid_arg
, sizeof(tag
.chipid
) - 1);
305 strncpy(tag
.boardid
, args
->boardid_arg
, sizeof(tag
.boardid
) - 1);
306 strcpy(tag
.big_endian
, "1");
307 sprintf(tag
.totalLength
, "%lu", imagelen
);
309 if (args
->cfe_given
) {
310 sprintf(tag
.cfeAddress
, "%" PRIu32
, flash_start
);
311 sprintf(tag
.cfeLength
, "%lu", cfelen
);
313 /* We don't include CFE */
314 strcpy(tag
.cfeAddress
, "0");
315 strcpy(tag
.cfeLength
, "0");
318 sprintf(tag
.kernelAddress
, "%lu", kerneloff
);
319 sprintf(tag
.kernelLength
, "%lu", kernellen
+ rootfsoffpadlen
);
321 if (args
->root_first_flag
) {
322 sprintf(tag
.flashImageStart
, "%lu", rootfsoff
);
323 sprintf(tag
.flashRootLength
, "%lu", rootfslen
);
325 sprintf(tag
.flashImageStart
, "%lu", kerneloff
);
326 sprintf(tag
.flashRootLength
, "%lu", rootfslen
+ sizeof(deadcode
));
328 int2tag(tag
.rootLength
, oldrootfslen
+ sizeof(deadcode
));
330 if (args
->rsa_signature_given
) {
331 strncpy(tag
.rsa_signature
, args
->rsa_signature_arg
, RSASIG_LEN
);
334 if (args
->layoutver_given
) {
335 strncpy(tag
.flashLayoutVer
, args
->layoutver_arg
, TAGLAYOUT_LEN
);
338 if (args
->info1_given
) {
339 strncpy(tag
.information1
, args
->info1_arg
, TAGINFO1_LEN
);
342 if (args
->info2_given
) {
343 strncpy(tag
.information2
, args
->info2_arg
, TAGINFO2_LEN
);
346 if (args
->reserved2_given
) {
347 strncpy(tag
.reserved2
, args
->reserved2_arg
, 16);
350 if (args
->altinfo_given
) {
351 strncpy(tag
.information1
, args
->altinfo_arg
, TAGINFO1_LEN
);
354 if (args
->second_image_flag_given
) {
355 if (strncmp(args
->second_image_flag_arg
, "2", DUALFLAG_LEN
) != 0) {
356 strncpy(tag
.dualImage
, args
->second_image_flag_arg
, DUALFLAG_LEN
);
360 if (args
->inactive_given
) {
361 if (strncmp(args
->inactive_arg
, "2", INACTIVEFLAG_LEN
) != 0) {
362 strncpy(tag
.inactiveFlag
, args
->second_image_flag_arg
, INACTIVEFLAG_LEN
);
366 for (i
= 0; i
< NUM_PIRELLI
; i
++) {
367 if (strncmp(args
->boardid_arg
, pirellitab
[i
], BOARDID_LEN
) == 0) {
374 int2tag(tag
.imageCRC
, kernelfscrc
);
376 int2tag(tag
.imageCRC
, kernelcrc
);
379 int2tag(&(tag
.rootfsCRC
[0]), rootfscrc
);
380 int2tag(tag
.kernelCRC
, kernelcrc
);
381 int2tag(tag
.fskernelCRC
, kernelfscrc
);
382 int2tag(tag
.headerCRC
, cyg_crc32_accumulate(IMAGETAG_CRC_START
, (uint8_t*)&tag
, sizeof(tag
) - 20));
384 fseek(binfile
, 0L, SEEK_SET
);
385 fwrite(&tag
, sizeof(uint8_t), sizeof(tag
), binfile
);
393 int main(int argc
, char **argv
)
396 char *kernel
, *rootfs
, *bin
;
397 uint32_t flash_start
, image_offset
, block_size
, load_address
, entry
;
398 flash_start
= image_offset
= block_size
= load_address
= entry
= 0;
399 struct gengetopt_args_info parsed_args
;
401 kernel
= rootfs
= bin
= NULL
;
403 if (imagetag_cmdline(argc
, argv
, &parsed_args
)) {
407 printf("Broadcom 63xx image tagger - v2.0.0\n");
408 printf("Copyright (C) 2008 Axel Gembe\n");
409 printf("Copyright (C) 2009-2010 Daniel Dickinson\n");
410 printf("Licensed under the terms of the Gnu General Public License\n");
412 kernel
= parsed_args
.kernel_arg
;
413 rootfs
= parsed_args
.rootfs_arg
;
414 bin
= parsed_args
.output_arg
;
415 if (strlen(parsed_args
.tag_version_arg
) >= TAGVER_LEN
) {
416 fprintf(stderr
, "Error: Tag Version (tag_version,v) too long.\n");
419 if (strlen(parsed_args
.boardid_arg
) >= BOARDID_LEN
) {
420 fprintf(stderr
, "Error: Board ID (boardid,b) too long.\n");
423 if (strlen(parsed_args
.chipid_arg
) >= CHIPID_LEN
) {
424 fprintf(stderr
, "Error: Chip ID (chipid,c) too long.\n");
427 if (strlen(parsed_args
.signature_arg
) >= SIG1_LEN
) {
428 fprintf(stderr
, "Error: Magic string (signature,a) too long.\n");
431 if (strlen(parsed_args
.signature2_arg
) >= SIG2_LEN
) {
432 fprintf(stderr
, "Error: Second magic string (signature2,m) too long.\n");
435 if (parsed_args
.layoutver_given
) {
436 if (strlen(parsed_args
.layoutver_arg
) > FLASHLAYOUTVER_LEN
) {
437 fprintf(stderr
, "Error: Flash layout version (layoutver,y) too long.\n");
441 if (parsed_args
.rsa_signature_given
) {
442 if (strlen(parsed_args
.rsa_signature_arg
) > RSASIG_LEN
) {
443 fprintf(stderr
, "Error: RSA Signature (rsa_signature,r) too long.\n");
448 if (parsed_args
.info1_given
) {
449 if (strlen(parsed_args
.info1_arg
) >= TAGINFO1_LEN
) {
450 fprintf(stderr
, "Error: Vendor Information 1 (info1) too long.\n");
455 if (parsed_args
.info2_given
) {
456 if (strlen(parsed_args
.info2_arg
) >= TAGINFO2_LEN
) {
457 fprintf(stderr
, "Error: Vendor Information 2 (info2) too long.\n");
462 if (parsed_args
.altinfo_given
) {
463 if (strlen(parsed_args
.altinfo_arg
) >= ALTTAGINFO_LEN
) {
464 fprintf(stderr
, "Error: Vendor Information 1 (info1) too long.\n");
469 if (parsed_args
.pad_given
) {
470 if (parsed_args
.pad_arg
< 0) {
471 fprintf(stderr
, "Error: pad size must be positive.\r");
476 flash_start
= strtoul(parsed_args
.flash_start_arg
, NULL
, 16);
477 image_offset
= strtoul(parsed_args
.image_offset_arg
, NULL
, 16);
478 block_size
= strtoul(parsed_args
.block_size_arg
, NULL
, 16);
480 if (!parsed_args
.kernel_file_has_header_flag
) {
481 load_address
= strtoul(parsed_args
.load_addr_arg
, NULL
, 16);
482 entry
= strtoul(parsed_args
.entry_arg
, NULL
, 16);
483 if (load_address
== 0) {
484 fprintf(stderr
, "Error: Invalid value for load address\n");
487 fprintf(stderr
, "Error: Invalid value for entry\n");
491 return tagfile(kernel
, rootfs
, bin
, &parsed_args
, flash_start
, image_offset
, block_size
, load_address
, entry
);