1/*
2 * mkbrnimg.c - partially based on OpenWrt's wndr3700.c
3 *
4 * Copyright (C) 2011 Tobias Diedrich <ranma+openwrt@tdiedrich.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License,
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stddef.h>
23#include <unistd.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <sys/mman.h>
27#include <string.h>
28#include <netinet/in.h>
29#include <inttypes.h>
30
31#define BPB 8 /* bits/byte */
32
33static uint32_t crc32[1<<BPB];
34
35static void init_crc32()
36{
37	const uint32_t poly = ntohl(0x2083b8ed);
38	int n;
39
40	for (n = 0; n < 1<<BPB; n++) {
41		uint32_t crc = n;
42		int bit;
43
44		for (bit = 0; bit < BPB; bit++)
45			crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
46		crc32[n] = crc;
47	}
48}
49
50static uint32_t crc32buf(unsigned char *buf, size_t len)
51{
52	uint32_t crc = 0xFFFFFFFF;
53
54	for (; len; len--, buf++)
55		crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
56	return ~crc;
57}
58
59static void usage(const char *) __attribute__ (( __noreturn__ ));
60
61static void usage(const char *mess)
62{
63	fprintf(stderr, "Error: %s\n", mess);
64	fprintf(stderr, "Usage: mkbrnimg [-o output_file] [-m magic] [-s signature] kernel_file [additional files]\n");
65	fprintf(stderr, "\n");
66	exit(1);
67}
68
69static char *output_file = "default-brnImage";
70static uint32_t magic = 0x12345678;
71static char *signature = "BRNDTW502";
72
73static void parseopts(int *argc, char ***argv)
74{
75	char *endptr;
76	int res;
77
78	while ((res = getopt(*argc, *argv, "o:m:s:")) != -1) {
79		switch (res) {
80		default:
81			usage("Unknown option");
82			break;
83		case 'o':
84			output_file = optarg;
85			break;
86		case 'm':
87			magic = strtoul(optarg, &endptr, 0);
88			if (endptr == optarg || *endptr != 0)
89				usage("magic must be a decimal or hexadecimal 32-bit value");
90			break;
91		case 's':
92			signature = optarg;
93			break;
94		}
95	}
96	*argc -= optind;
97	*argv += optind;
98}
99
100static void appendfile(int outfd, char *path, int kernel) {
101	int fd;
102	size_t len, padded_len;
103	char *input_file;
104	uint32_t crc;
105	char padding[0x400];
106	char footer[12];
107
108	memset(padding, 0xff, sizeof(padding));
109
110	// mmap input_file
111	if ((fd = open(path, O_RDONLY))  < 0
112	|| (len = lseek(fd, 0, SEEK_END)) < 0
113	|| (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
114	|| close(fd) < 0)
115	{
116		fprintf(stderr, "Error mapping file '%s': %s\n", path, strerror(errno));
117		exit(1);
118	}
119
120	// kernel should be lzma compressed image, not uImage
121	if (kernel &&
122	    (input_file[0] != (char)0x5d ||
123	     input_file[1] != (char)0x00 ||
124	     input_file[2] != (char)0x00 ||
125	     input_file[3] != (char)0x80)) {
126		fprintf(stderr, "lzma signature not found on kernel image.\n");
127		exit(1);
128	}
129
130	init_crc32();
131	crc = crc32buf(input_file, len);
132	fprintf(stderr, "crc32 for '%s' is %08x.\n", path, crc);
133
134	// write the file
135	write(outfd, input_file, len);
136
137	// write padding
138	padded_len = ((len + sizeof(footer) + sizeof(padding) - 1) & ~(sizeof(padding) - 1)) - sizeof(footer);
139	fprintf(stderr, "len=%08x padded_len=%08x\n", len, padded_len);
140	write(outfd, padding, padded_len - len);
141
142	// write footer
143	footer[0]  = (len   >>  0) & 0xff;
144	footer[1]  = (len   >>  8) & 0xff;
145	footer[2]  = (len   >> 16) & 0xff;
146	footer[3]  = (len   >> 24) & 0xff;
147	footer[4]  = (magic >>  0) & 0xff;
148	footer[5]  = (magic >>  8) & 0xff;
149	footer[6]  = (magic >> 16) & 0xff;
150	footer[7]  = (magic >> 24) & 0xff;
151	footer[8]  = (crc   >>  0) & 0xff;
152	footer[9]  = (crc   >>  8) & 0xff;
153	footer[10] = (crc   >> 16) & 0xff;
154	footer[11] = (crc   >> 24) & 0xff;
155	write(outfd, footer, sizeof(footer));
156
157	munmap(input_file, len);
158}
159
160int main(int argc, char **argv)
161{
162	int outfd;
163	int i;
164
165	parseopts(&argc, &argv);
166
167	if (argc < 1)
168		usage("wrong number of arguments");
169
170	if ((outfd = open(output_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
171	{
172		fprintf(stderr, "Error opening '%s' for writing: %s\n", output_file, strerror(errno));
173		exit(1);
174	}
175
176	for (i=0; i<argc; i++) {
177		appendfile(outfd, argv[i], i == 0);
178	}
179	write(outfd, signature, strlen(signature)+1);
180	close(outfd);
181
182	return 0;
183}
184