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
;
169 rootfsend
= rootfsoff
+ getlen(rootfsfile
);
170 if ((rootfsend
% block_size
) > 0)
171 rootfsend
= (((rootfsend
/ block_size
) + 1) * block_size
);
172 rootfslen
= rootfsend
- rootfsoff
;
173 imagelen
= rootfsoff
+ rootfslen
- kerneloff
+ sizeof(deadcode
);
174 rootfsoffpadlen
= rootfsoff
- (kerneloff
+ kernellen
);
176 /* Seek to the start of the kernel */
177 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
179 /* Write the kernel header */
180 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
182 /* Write the kernel */
183 while (kernelfile
&& !feof(kernelfile
) && !ferror(kernelfile
)) {
184 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), kernelfile
);
185 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
188 /* Write the RootFS */
189 fseek(binfile
, rootfsoff
- fwaddr
+ cfelen
, SEEK_SET
);
190 while (rootfsfile
&& !feof(rootfsfile
) && !ferror(rootfsfile
)) {
191 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), rootfsfile
);
192 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
195 /* Align image to specified erase block size and append deadc0de */
196 printf("Data alignment to %dk with 'deadc0de' appended\n", block_size
/1024);
197 fseek(binfile
, rootfsoff
+ rootfslen
- fwaddr
+ cfelen
, SEEK_SET
);
198 fwrite(&deadcode
, sizeof(uint32_t), 1, binfile
);
200 oldrootfslen
= rootfslen
;
201 if (args
->pad_given
) {
202 uint32_t allfs
= 0xffffffff;
203 uint32_t pad_size
= args
->pad_arg
* 1024 * 1024;
205 printf("Padding image to %d bytes ...\n", pad_size
);
206 while (imagelen
< pad_size
) {
207 fwrite(&allfs
, sizeof(uint32_t), 1, binfile
);
213 /* Flush the binfile buffer so that when we read from file, it contains
214 * everything in the buffer
218 /* Compute the crc32 of the entire image (deadC0de included) */
219 imagecrc
= compute_crc32(imagecrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, imagelen
);
220 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
221 kernelcrc
= compute_crc32(kernelcrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
);
222 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
223 kernelfscrc
= compute_crc32(kernelfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
+ rootfslen
+ sizeof(deadcode
));
224 /* Compute the crc32 of the flashImageStart to rootLength.
225 * The broadcom firmware assumes the rootfs starts the image,
226 * therefore uses the rootfs start to determine where to flash
227 * the image. Since we have the kernel first we have to give
228 * it the kernel address, but the crc uses the length
229 * associated with this address, which is added to the kernel
230 * length to determine the length of image to flash and thus
231 * needs to be rootfs + deadcode
233 rootfscrc
= compute_crc32(rootfscrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, rootfslen
+ sizeof(deadcode
));
236 /* Build the kernel address and length (doesn't need to be aligned, read only) */
237 rootfsoff
= fwaddr
+ sizeof(tag
);
238 oldrootfslen
= getlen(rootfsfile
);
239 rootfslen
= oldrootfslen
;
240 rootfslen
= ( (rootfslen
% block_size
) > 0 ? (((rootfslen
/ block_size
) + 1) * block_size
) : rootfslen
);
241 kerneloffpadlen
= rootfslen
- oldrootfslen
;
242 oldrootfslen
= rootfslen
;
244 kerneloff
= rootfsoff
+ rootfslen
;
245 kernellen
= getlen(kernelfile
);
247 imagelen
= cfelen
+ rootfslen
+ kernellen
;
249 /* Seek to the start of the kernel */
250 fseek(binfile
, kerneloff
- fwaddr
+ cfelen
, SEEK_SET
);
252 if (!args
->kernel_file_has_header_flag
) {
253 /* Build the kernel header */
254 khdr
.loadaddr
= htonl(load_address
);
255 khdr
.entry
= htonl(entry
);
256 khdr
.lzmalen
= htonl(kernellen
);
258 /* Write the kernel header */
259 fwrite(&khdr
, sizeof(khdr
), 1, binfile
);
261 /* Increase the kernel size by the header size */
262 kernellen
+= sizeof(khdr
);
265 /* Write the kernel */
266 while (kernelfile
&& !feof(kernelfile
) && !ferror(kernelfile
)) {
267 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), kernelfile
);
268 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
271 /* Write the RootFS */
272 fseek(binfile
, rootfsoff
- fwaddr
+ cfelen
, SEEK_SET
);
273 while (rootfsfile
&& !feof(rootfsfile
) && !ferror(rootfsfile
)) {
274 read
= fread(readbuf
, sizeof(uint8_t), sizeof(readbuf
), rootfsfile
);
275 fwrite(readbuf
, sizeof(uint8_t), read
, binfile
);
278 /* Flush the binfile buffer so that when we read from file, it contains
279 * everything in the buffer
283 /* Compute the crc32 of the entire image (deadC0de included) */
284 imagecrc
= compute_crc32(imagecrc
, binfile
, sizeof(tag
), imagelen
);
285 /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
286 kernelcrc
= compute_crc32(kernelcrc
, binfile
, kerneloff
- fwaddr
+ cfelen
, kernellen
+ rootfsoffpadlen
);
287 kernelfscrc
= compute_crc32(kernelfscrc
, binfile
, rootfsoff
- fwaddr
+ cfelen
, kernellen
+ rootfslen
);
288 rootfscrc
= compute_crc32(rootfscrc
, binfile
, rootfsoff
- fwaddr
+ cfelen
, rootfslen
);
291 /* Close the files */
299 strncpy(tag
.tagVersion
, args
->tag_version_arg
, sizeof(tag
.tagVersion
) - 1);
300 strncpy(tag
.sig_1
, args
->signature_arg
, sizeof(tag
.sig_1
) - 1);
301 strncpy(tag
.sig_2
, args
->signature2_arg
, sizeof(tag
.sig_2
) - 1);
302 strncpy(tag
.chipid
, args
->chipid_arg
, sizeof(tag
.chipid
) - 1);
303 strncpy(tag
.boardid
, args
->boardid_arg
, sizeof(tag
.boardid
) - 1);
304 strcpy(tag
.big_endian
, "1");
305 sprintf(tag
.totalLength
, "%lu", imagelen
);
307 if (args
->cfe_given
) {
308 sprintf(tag
.cfeAddress
, "%" PRIu32
, flash_start
);
309 sprintf(tag
.cfeLength
, "%lu", cfelen
);
311 /* We don't include CFE */
312 strcpy(tag
.cfeAddress
, "0");
313 strcpy(tag
.cfeLength
, "0");
316 sprintf(tag
.kernelAddress
, "%lu", kerneloff
);
317 sprintf(tag
.kernelLength
, "%lu", kernellen
+ rootfsoffpadlen
);
319 if (args
->root_first_flag
) {
320 sprintf(tag
.flashImageStart
, "%lu", rootfsoff
);
321 sprintf(tag
.flashRootLength
, "%lu", rootfslen
);
323 sprintf(tag
.flashImageStart
, "%lu", kerneloff
);
324 sprintf(tag
.flashRootLength
, "%lu", rootfslen
+ sizeof(deadcode
));
326 int2tag(tag
.rootLength
, oldrootfslen
+ sizeof(deadcode
));
328 if (args
->rsa_signature_given
) {
329 strncpy(tag
.rsa_signature
, args
->rsa_signature_arg
, RSASIG_LEN
);
332 if (args
->layoutver_given
) {
333 strncpy(tag
.flashLayoutVer
, args
->layoutver_arg
, TAGLAYOUT_LEN
);
336 if (args
->info1_given
) {
337 strncpy(tag
.information1
, args
->info1_arg
, TAGINFO1_LEN
);
340 if (args
->info2_given
) {
341 strncpy(tag
.information2
, args
->info2_arg
, TAGINFO2_LEN
);
344 if (args
->reserved2_given
) {
345 strncpy(tag
.reserved2
, args
->reserved2_arg
, 16);
348 if (args
->altinfo_given
) {
349 strncpy(tag
.information1
, args
->altinfo_arg
, TAGINFO1_LEN
);
352 if (args
->second_image_flag_given
) {
353 if (strncmp(args
->second_image_flag_arg
, "2", DUALFLAG_LEN
) != 0) {
354 strncpy(tag
.dualImage
, args
->second_image_flag_arg
, DUALFLAG_LEN
);
358 if (args
->inactive_given
) {
359 if (strncmp(args
->inactive_arg
, "2", INACTIVEFLAG_LEN
) != 0) {
360 strncpy(tag
.inactiveFlag
, args
->second_image_flag_arg
, INACTIVEFLAG_LEN
);
364 for (i
= 0; i
< NUM_PIRELLI
; i
++) {
365 if (strncmp(args
->boardid_arg
, pirellitab
[i
], BOARDID_LEN
) == 0) {
372 int2tag(tag
.imageCRC
, kernelfscrc
);
374 int2tag(tag
.imageCRC
, kernelcrc
);
377 int2tag(&(tag
.rootfsCRC
[0]), rootfscrc
);
378 int2tag(tag
.kernelCRC
, kernelcrc
);
379 int2tag(tag
.fskernelCRC
, kernelfscrc
);
380 int2tag(tag
.headerCRC
, cyg_crc32_accumulate(IMAGETAG_CRC_START
, (uint8_t*)&tag
, sizeof(tag
) - 20));
382 fseek(binfile
, 0L, SEEK_SET
);
383 fwrite(&tag
, sizeof(uint8_t), sizeof(tag
), binfile
);
391 int main(int argc
, char **argv
)
394 char *kernel
, *rootfs
, *bin
;
395 uint32_t flash_start
, image_offset
, block_size
, load_address
, entry
;
396 flash_start
= image_offset
= block_size
= load_address
= entry
= 0;
397 struct gengetopt_args_info parsed_args
;
399 kernel
= rootfs
= bin
= NULL
;
401 if (imagetag_cmdline(argc
, argv
, &parsed_args
)) {
405 printf("Broadcom 63xx image tagger - v2.0.0\n");
406 printf("Copyright (C) 2008 Axel Gembe\n");
407 printf("Copyright (C) 2009-2010 Daniel Dickinson\n");
408 printf("Licensed under the terms of the Gnu General Public License\n");
410 kernel
= parsed_args
.kernel_arg
;
411 rootfs
= parsed_args
.rootfs_arg
;
412 bin
= parsed_args
.output_arg
;
413 if (strlen(parsed_args
.tag_version_arg
) >= TAGVER_LEN
) {
414 fprintf(stderr
, "Error: Tag Version (tag_version,v) too long.\n");
417 if (strlen(parsed_args
.boardid_arg
) >= BOARDID_LEN
) {
418 fprintf(stderr
, "Error: Board ID (boardid,b) too long.\n");
421 if (strlen(parsed_args
.chipid_arg
) >= CHIPID_LEN
) {
422 fprintf(stderr
, "Error: Chip ID (chipid,c) too long.\n");
425 if (strlen(parsed_args
.signature_arg
) >= SIG1_LEN
) {
426 fprintf(stderr
, "Error: Magic string (signature,a) too long.\n");
429 if (strlen(parsed_args
.signature2_arg
) >= SIG2_LEN
) {
430 fprintf(stderr
, "Error: Second magic string (signature2,m) too long.\n");
433 if (parsed_args
.layoutver_given
) {
434 if (strlen(parsed_args
.layoutver_arg
) > FLASHLAYOUTVER_LEN
) {
435 fprintf(stderr
, "Error: Flash layout version (layoutver,y) too long.\n");
439 if (parsed_args
.rsa_signature_given
) {
440 if (strlen(parsed_args
.rsa_signature_arg
) > RSASIG_LEN
) {
441 fprintf(stderr
, "Error: RSA Signature (rsa_signature,r) too long.\n");
446 if (parsed_args
.info1_given
) {
447 if (strlen(parsed_args
.info1_arg
) >= TAGINFO1_LEN
) {
448 fprintf(stderr
, "Error: Vendor Information 1 (info1) too long.\n");
453 if (parsed_args
.info2_given
) {
454 if (strlen(parsed_args
.info2_arg
) >= TAGINFO2_LEN
) {
455 fprintf(stderr
, "Error: Vendor Information 2 (info2) too long.\n");
460 if (parsed_args
.altinfo_given
) {
461 if (strlen(parsed_args
.altinfo_arg
) >= ALTTAGINFO_LEN
) {
462 fprintf(stderr
, "Error: Vendor Information 1 (info1) too long.\n");
467 if (parsed_args
.pad_given
) {
468 if (parsed_args
.pad_arg
< 0) {
469 fprintf(stderr
, "Error: pad size must be positive.\r");
474 flash_start
= strtoul(parsed_args
.flash_start_arg
, NULL
, 16);
475 image_offset
= strtoul(parsed_args
.image_offset_arg
, NULL
, 16);
476 block_size
= strtoul(parsed_args
.block_size_arg
, NULL
, 16);
478 if (!parsed_args
.kernel_file_has_header_flag
) {
479 load_address
= strtoul(parsed_args
.load_addr_arg
, NULL
, 16);
480 entry
= strtoul(parsed_args
.entry_arg
, NULL
, 16);
481 if (load_address
== 0) {
482 fprintf(stderr
, "Error: Invalid value for load address\n");
485 fprintf(stderr
, "Error: Invalid value for entry\n");
489 return tagfile(kernel
, rootfs
, bin
, &parsed_args
, flash_start
, image_offset
, block_size
, load_address
, entry
);