ptgen: add Chromium OS kernel partition support
[project/firmware-utils.git] / src / imagetag.c
1 /*
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
4 * for more details.
5 *
6 * Copyright (C) 2008 Axel Gembe <ago@bastart.eu.org>
7 * Copyright (C) 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <netinet/in.h>
17 #include <inttypes.h>
18
19 #include "bcm_tag.h"
20 #include "imagetag_cmdline.h"
21 #include "cyg_crc.h"
22
23 #define DEADCODE 0xDEADC0DE
24
25 /* Kernel header */
26 struct kernelhdr {
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 */
30 };
31
32 static char pirellitab[NUM_PIRELLI][BOARDID_LEN] = PIRELLI_BOARDS;
33
34 void int2tag(char *tag, uint32_t value) {
35 uint32_t network = htonl(value);
36 memcpy(tag, (char *)(&network), 4);
37 }
38
39 uint32_t compute_crc32(uint32_t crc, FILE *binfile, size_t compute_start, size_t compute_len)
40 {
41 uint8_t readbuf[1024];
42 size_t read;
43
44 fseek(binfile, compute_start, SEEK_SET);
45
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;
51 }
52
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);
57 }
58
59 return crc;
60 }
61
62 size_t getlen(FILE *fp)
63 {
64 size_t retval, curpos;
65
66 if (!fp)
67 return 0;
68
69 curpos = ftell(fp);
70 fseek(fp, 0, SEEK_END);
71 retval = ftell(fp);
72 fseek(fp, curpos, SEEK_SET);
73
74 return retval;
75 }
76
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)
81 {
82 struct bcm_tag tag;
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, \
87 rootfsend;
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;
93 uint32_t fwaddr = 0;
94 const uint32_t deadcode = htonl(DEADCODE);
95 int i;
96 int is_pirelli = 0;
97
98
99 memset(&tag, 0, sizeof(struct bcm_tag));
100
101 if (!kernel || !rootfs) {
102 fprintf(stderr, "imagetag can't create an image without both kernel and rootfs\n");
103 }
104
105 if (kernel && !(kernelfile = fopen(kernel, "rb"))) {
106 fprintf(stderr, "Unable to open kernel \"%s\"\n", kernel);
107 return 1;
108 }
109
110 if (rootfs && !(rootfsfile = fopen(rootfs, "rb"))) {
111 fprintf(stderr, "Unable to open rootfs \"%s\"\n", rootfs);
112 return 1;
113 }
114
115 if (!bin || !(binfile = fopen(bin, "wb+"))) {
116 fprintf(stderr, "Unable to open output file \"%s\"\n", bin);
117 return 1;
118 }
119
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);
123 }
124 }
125
126 fwaddr = flash_start + image_offset;
127 if (cfefile) {
128 cfelen = getlen(cfefile);
129 /* Seek to the start of the file after tag */
130 fseek(binfile, sizeof(tag), SEEK_SET);
131
132 /* Write the cfe */
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);
136 }
137
138 } else {
139 cfelen = 0;
140 }
141
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);
145
146 kernellen = getlen(kernelfile);
147
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);
153
154 /* Increase the kernel size by the header size */
155 kernellen += sizeof(khdr);
156 }
157
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;
163 else
164 rootfsoff = (rootfsoff % 4) > 0 ? (((rootfsoff / 4) + 1) * 4) : rootfsoff;
165
166 /* align the end */
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);
173
174 /* Seek to the start of the kernel */
175 fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
176
177 /* Write the kernel header */
178 fwrite(&khdr, sizeof(khdr), 1, binfile);
179
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);
184 }
185
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);
191 }
192
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);
197
198 oldrootfslen = rootfslen;
199 if (args->pad_given) {
200 uint32_t allfs = 0xffffffff;
201 uint32_t pad_size = args->pad_arg * 1024 * 1024;
202
203 printf("Padding image to %d bytes ...\n", pad_size);
204 while (imagelen < pad_size) {
205 fwrite(&allfs, sizeof(uint32_t), 1, binfile);
206 imagelen += 4;
207 rootfslen += 4;
208 }
209 }
210
211 /* Flush the binfile buffer so that when we read from file, it contains
212 * everything in the buffer
213 */
214 fflush(binfile);
215
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
230 */
231 rootfscrc = compute_crc32(rootfscrc, binfile, kerneloff - fwaddr + cfelen, rootfslen + sizeof(deadcode));
232
233 } else {
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;
240
241 kerneloff = rootfsoff + rootfslen;
242 kernellen = getlen(kernelfile);
243
244 imagelen = cfelen + rootfslen + kernellen;
245
246 /* Seek to the start of the kernel */
247 fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
248
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);
254
255 /* Write the kernel header */
256 fwrite(&khdr, sizeof(khdr), 1, binfile);
257
258 /* Increase the kernel size by the header size */
259 kernellen += sizeof(khdr);
260 }
261
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);
266 }
267
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);
273 }
274
275 /* Flush the binfile buffer so that when we read from file, it contains
276 * everything in the buffer
277 */
278 fflush(binfile);
279
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);
286 }
287
288 /* Close the files */
289 if (cfefile) {
290 fclose(cfefile);
291 }
292 fclose(kernelfile);
293 fclose(rootfsfile);
294
295 /* Build the tag */
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);
303
304 if (args->cfe_given) {
305 sprintf(tag.cfeAddress, "%" PRIu32, flash_start);
306 sprintf(tag.cfeLength, "%lu", cfelen);
307 } else {
308 /* We don't include CFE */
309 strcpy(tag.cfeAddress, "0");
310 strcpy(tag.cfeLength, "0");
311 }
312
313 sprintf(tag.kernelAddress, "%lu", kerneloff);
314 sprintf(tag.kernelLength, "%lu", kernellen + rootfsoffpadlen);
315
316 if (args->root_first_flag) {
317 sprintf(tag.flashImageStart, "%lu", rootfsoff);
318 sprintf(tag.flashRootLength, "%lu", rootfslen);
319 } else {
320 sprintf(tag.flashImageStart, "%lu", kerneloff);
321 sprintf(tag.flashRootLength, "%lu", rootfslen + sizeof(deadcode));
322 }
323 int2tag(tag.rootLength, oldrootfslen + sizeof(deadcode));
324
325 if (args->rsa_signature_given) {
326 strncpy(tag.rsa_signature, args->rsa_signature_arg, RSASIG_LEN);
327 }
328
329 if (args->layoutver_given) {
330 strncpy(tag.flashLayoutVer, args->layoutver_arg, TAGLAYOUT_LEN);
331 }
332
333 if (args->info1_given) {
334 strncpy(tag.information1, args->info1_arg, TAGINFO1_LEN);
335 }
336
337 if (args->info2_given) {
338 strncpy(tag.information2, args->info2_arg, TAGINFO2_LEN);
339 }
340
341 if (args->reserved2_given) {
342 strncpy(tag.reserved2, args->reserved2_arg, 16);
343 }
344
345 if (args->altinfo_given) {
346 strncpy(tag.information1, args->altinfo_arg, TAGINFO1_LEN);
347 }
348
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);
352 }
353 }
354
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);
358 }
359 }
360
361 for (i = 0; i < NUM_PIRELLI; i++) {
362 if (strncmp(args->boardid_arg, pirellitab[i], BOARDID_LEN) == 0) {
363 is_pirelli = 1;
364 break;
365 }
366 }
367
368 if ( !is_pirelli ) {
369 int2tag(tag.imageCRC, kernelfscrc);
370 } else {
371 int2tag(tag.imageCRC, kernelcrc);
372 }
373
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));
378
379 fseek(binfile, 0L, SEEK_SET);
380 fwrite(&tag, sizeof(uint8_t), sizeof(tag), binfile);
381
382 fflush(binfile);
383 fclose(binfile);
384
385 return 0;
386 }
387
388 int main(int argc, char **argv)
389 {
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;
394
395 kernel = rootfs = bin = NULL;
396
397 if (imagetag_cmdline(argc, argv, &parsed_args)) {
398 exit(1);
399 }
400
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");
405
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");
411 exit(1);
412 }
413 if (strlen(parsed_args.boardid_arg) >= BOARDID_LEN) {
414 fprintf(stderr, "Error: Board ID (boardid,b) too long.\n");
415 exit(1);
416 }
417 if (strlen(parsed_args.chipid_arg) >= CHIPID_LEN) {
418 fprintf(stderr, "Error: Chip ID (chipid,c) too long.\n");
419 exit(1);
420 }
421 if (strlen(parsed_args.signature_arg) >= SIG1_LEN) {
422 fprintf(stderr, "Error: Magic string (signature,a) too long.\n");
423 exit(1);
424 }
425 if (strlen(parsed_args.signature2_arg) >= SIG2_LEN) {
426 fprintf(stderr, "Error: Second magic string (signature2,m) too long.\n");
427 exit(1);
428 }
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");
432 exit(1);
433 }
434 }
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");
438 exit(1);
439 }
440 }
441
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");
445 exit(1);
446 }
447 }
448
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");
452 exit(1);
453 }
454 }
455
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");
459 exit(1);
460 }
461 }
462
463 if (parsed_args.pad_given) {
464 if (parsed_args.pad_arg < 0) {
465 fprintf(stderr, "Error: pad size must be positive.\r");
466 exit(1);
467 }
468 }
469
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);
473
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");
479 }
480 if (entry == 0) {
481 fprintf(stderr, "Error: Invalid value for entry\n");
482 }
483 }
484
485 return tagfile(kernel, rootfs, bin, &parsed_args, flash_start, image_offset, block_size, load_address, entry);
486 }