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 <time.h>
15#include <unistd.h>
16#include <sys/stat.h>
17#include <netinet/in.h>
18#include <inttypes.h>
19
20#include "bcm_tag.h"
21#include "imagetag_cmdline.h"
22#include "cyg_crc.h"
23
24#define DEADCODE			0xDEADC0DE
25
26/* Kernel header */
27struct kernelhdr {
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 */
31};
32
33static char pirellitab[NUM_PIRELLI][BOARDID_LEN] = PIRELLI_BOARDS;
34
35void int2tag(char *tag, uint32_t value) {
36  uint32_t network = htonl(value);
37  memcpy(tag, (char *)(&network), 4);
38}
39
40uint32_t compute_crc32(uint32_t crc, FILE *binfile, size_t compute_start, size_t compute_len)
41{
42	uint8_t readbuf[1024];
43	size_t read;
44
45	fseek(binfile, compute_start, SEEK_SET);
46
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;
52	}
53
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);
58	}
59
60	return crc;
61}
62
63size_t getlen(FILE *fp)
64{
65	size_t retval, curpos;
66
67	if (!fp)
68		return 0;
69
70	curpos = ftell(fp);
71	fseek(fp, 0, SEEK_END);
72	retval = ftell(fp);
73	fseek(fp, curpos, SEEK_SET);
74
75	return retval;
76}
77
78int 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)
82{
83	struct bcm_tag tag;
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, \
88	  rootfsend;
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;
94	uint32_t fwaddr = 0;
95	uint8_t crc_val;
96	const uint32_t deadcode = htonl(DEADCODE);
97	int i;
98	int is_pirelli = 0;
99
100
101	memset(&tag, 0, sizeof(struct bcm_tag));
102
103	if (!kernel || !rootfs) {
104		fprintf(stderr, "imagetag can't create an image without both kernel and rootfs\n");
105	}
106
107	if (kernel && !(kernelfile = fopen(kernel, "rb"))) {
108		fprintf(stderr, "Unable to open kernel \"%s\"\n", kernel);
109		return 1;
110	}
111
112	if (rootfs && !(rootfsfile = fopen(rootfs, "rb"))) {
113		fprintf(stderr, "Unable to open rootfs \"%s\"\n", rootfs);
114		return 1;
115	}
116
117	if (!bin || !(binfile = fopen(bin, "wb+"))) {
118		fprintf(stderr, "Unable to open output file \"%s\"\n", bin);
119		return 1;
120	}
121
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);
125	  }
126	}
127
128	fwaddr = flash_start + image_offset;
129	if (cfefile) {
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);
134
135	  /* Write the cfe */
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);
139	  }
140
141	} else {
142	  cfeoff = 0;
143	  cfelen = 0;
144	}
145
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);
149
150	  kernellen = getlen(kernelfile);
151
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);
157
158		/* Increase the kernel size by the header size */
159		kernellen += sizeof(khdr);
160	  }
161
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;
167          else
168		rootfsoff = (rootfsoff % 4) > 0 ? (((rootfsoff / 4) + 1) * 4) : rootfsoff;
169
170	  /* align the end */
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);
177
178	  /* Seek to the start of the kernel */
179	  fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
180
181	  /* Write the kernel header */
182	  fwrite(&khdr, sizeof(khdr), 1, binfile);
183
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);
188	  }
189
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);
195	  }
196
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);
201
202	  oldrootfslen = rootfslen;
203	  if (args->pad_given) {
204		uint32_t allfs = 0xffffffff;
205		uint32_t pad_size = args->pad_arg * 1024 * 1024;
206
207		printf("Padding image to %d bytes ...\n", pad_size);
208		while (imagelen < pad_size) {
209			fwrite(&allfs, sizeof(uint32_t), 1, binfile);
210			imagelen += 4;
211			rootfslen += 4;
212		}
213	  }
214
215	  /* Flush the binfile buffer so that when we read from file, it contains
216	   * everything in the buffer
217	   */
218	  fflush(binfile);
219
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
234	   */
235	  rootfscrc = compute_crc32(rootfscrc, binfile, kerneloff - fwaddr + cfelen, rootfslen + sizeof(deadcode));
236
237	} else {
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;
245
246	  kerneloff = rootfsoff + rootfslen;
247	  kernellen = getlen(kernelfile);
248
249	  imagelen = cfelen + rootfslen + kernellen;
250
251	  /* Seek to the start of the kernel */
252	  fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
253
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);
259
260		/* Write the kernel header */
261		fwrite(&khdr, sizeof(khdr), 1, binfile);
262
263		/* Increase the kernel size by the header size */
264		kernellen += sizeof(khdr);
265	  }
266
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);
271	  }
272
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);
278	  }
279
280	  /* Flush the binfile buffer so that when we read from file, it contains
281	   * everything in the buffer
282	   */
283	  fflush(binfile);
284
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);
291	}
292
293	/* Close the files */
294	if (cfefile) {
295	  fclose(cfefile);
296	}
297	fclose(kernelfile);
298	fclose(rootfsfile);
299
300	/* Build the tag */
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);
308
309	if (args->cfe_given) {
310	  sprintf(tag.cfeAddress, "%" PRIu32, flash_start);
311	  sprintf(tag.cfeLength, "%lu", cfelen);
312	} else {
313	  /* We don't include CFE */
314	  strcpy(tag.cfeAddress, "0");
315	  strcpy(tag.cfeLength, "0");
316	}
317
318	sprintf(tag.kernelAddress, "%lu", kerneloff);
319	sprintf(tag.kernelLength, "%lu", kernellen + rootfsoffpadlen);
320
321	if (args->root_first_flag) {
322	  sprintf(tag.flashImageStart, "%lu", rootfsoff);
323	  sprintf(tag.flashRootLength, "%lu", rootfslen);
324	} else {
325	  sprintf(tag.flashImageStart, "%lu", kerneloff);
326	  sprintf(tag.flashRootLength, "%lu", rootfslen + sizeof(deadcode));
327	}
328	int2tag(tag.rootLength, oldrootfslen + sizeof(deadcode));
329
330	if (args->rsa_signature_given) {
331	    strncpy(tag.rsa_signature, args->rsa_signature_arg, RSASIG_LEN);
332	}
333
334	if (args->layoutver_given) {
335	    strncpy(tag.flashLayoutVer, args->layoutver_arg, TAGLAYOUT_LEN);
336	}
337
338	if (args->info1_given) {
339	  strncpy(tag.information1, args->info1_arg, TAGINFO1_LEN);
340	}
341
342	if (args->info2_given) {
343	  strncpy(tag.information2, args->info2_arg, TAGINFO2_LEN);
344	}
345
346	if (args->reserved2_given) {
347	  strncpy(tag.reserved2, args->reserved2_arg, 16);
348	}
349
350	if (args->altinfo_given) {
351	  strncpy(tag.information1, args->altinfo_arg, TAGINFO1_LEN);
352	}
353
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);
357	  }
358	}
359
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);
363	  }
364	}
365
366	for (i = 0; i < NUM_PIRELLI; i++) {
367		if (strncmp(args->boardid_arg, pirellitab[i], BOARDID_LEN) == 0) {
368			is_pirelli = 1;
369			break;
370		}
371	}
372
373	if ( !is_pirelli ) {
374	  int2tag(tag.imageCRC, kernelfscrc);
375	} else {
376	  int2tag(tag.imageCRC, kernelcrc);
377	}
378
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));
383
384	fseek(binfile, 0L, SEEK_SET);
385	fwrite(&tag, sizeof(uint8_t), sizeof(tag), binfile);
386
387    fflush(binfile);
388	fclose(binfile);
389
390	return 0;
391}
392
393int main(int argc, char **argv)
394{
395    int c, i;
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;
400
401	kernel = rootfs = bin = NULL;
402
403	if (imagetag_cmdline(argc, argv, &parsed_args)) {
404	  exit(1);
405	}
406
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");
411
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");
417	  exit(1);
418	}
419	if (strlen(parsed_args.boardid_arg) >= BOARDID_LEN) {
420	  fprintf(stderr, "Error: Board ID (boardid,b) too long.\n");
421	  exit(1);
422	}
423	if (strlen(parsed_args.chipid_arg) >= CHIPID_LEN) {
424	  fprintf(stderr, "Error: Chip ID (chipid,c) too long.\n");
425	  exit(1);
426	}
427	if (strlen(parsed_args.signature_arg) >= SIG1_LEN) {
428	  fprintf(stderr, "Error: Magic string (signature,a) too long.\n");
429	  exit(1);
430	}
431	if (strlen(parsed_args.signature2_arg) >= SIG2_LEN) {
432	  fprintf(stderr, "Error: Second magic string (signature2,m) too long.\n");
433	  exit(1);
434	}
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");
438		exit(1);
439	  }
440	}
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");
444		exit(1);
445	  }
446	}
447
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");
451		exit(1);
452	  }
453	}
454
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");
458		exit(1);
459	  }
460	}
461
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");
465		exit(1);
466	  }
467	}
468
469	if (parsed_args.pad_given) {
470	  if (parsed_args.pad_arg < 0) {
471		fprintf(stderr, "Error: pad size must be positive.\r");
472		exit(1);
473	  }
474	}
475
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);
479
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");
485	  }
486	  if (entry == 0) {
487		fprintf(stderr, "Error: Invalid value for entry\n");
488	  }
489	}
490
491	return tagfile(kernel, rootfs, bin, &parsed_args, flash_start, image_offset, block_size, load_address, entry);
492}
493