1 /*
2  * Copyright (C) 2007 Ubiquiti Networks, Inc.
3  * Copyright (C) 2008 Lukas Kuna <ValXdater@seznam.cz>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <zlib.h>
27 #include <sys/mman.h>
28 #include <netinet/in.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <limits.h>
32 #include <zlib.h>
33 #include "fw.h"
34 
35 typedef struct fw_layout_data {
36 	char		name[PATH_MAX];
37 	u_int32_t	kern_start;
38 	u_int32_t	kern_entry;
39 	u_int32_t	firmware_max_length;
40 } fw_layout_t;
41 
42 fw_layout_t fw_layout_data[] = {
43 	{
44 		.name		=	"XS2",
45 		.kern_start	=	0xbfc30000,
46 		.kern_entry	=	0x80041000,
47 		.firmware_max_length=	0x00390000,
48 	},
49 	{
50 		.name		=	"XS5",
51 		.kern_start	=	0xbe030000,
52 		.kern_entry	=	0x80041000,
53 		.firmware_max_length=	0x00390000,
54 	},
55 	{
56 		.name		=	"RS",
57 		.kern_start	=	0xbf030000,
58 		.kern_entry	=	0x80060000,
59 		.firmware_max_length=	0x00B00000,
60 	},
61 	{
62 		.name		=	"RSPRO",
63 		.kern_start	=	0xbf030000,
64 		.kern_entry	=	0x80050100,
65 		.firmware_max_length=	0x00B00000,
66 	},
67 	{
68 		.name		=	"LS-SR71",
69 		.kern_start	=	0xbf030000,
70 		.kern_entry	=	0x80060000,
71 		.firmware_max_length=	0x00640000,
72 	},
73 	{
74 		.name		=	"XS2-8",
75 		.kern_start	=	0xa8030000,
76 		.kern_entry	=	0x80041000,
77 		.firmware_max_length=	0x006C0000,
78 	},
79 	{
80 		.name		=	"XM",
81 		.kern_start	=	0x9f050000,
82 		.kern_entry	=	0x80002000,
83 		.firmware_max_length=	0x006A0000,
84 	},
85 	{	.name		=	"",
86 	},
87 };
88 
89 typedef struct part_data {
90 	char 	partition_name[64];
91 	int  	partition_index;
92 	u_int32_t	partition_baseaddr;
93 	u_int32_t	partition_startaddr;
94 	u_int32_t	partition_memaddr;
95 	u_int32_t	partition_entryaddr;
96 	u_int32_t  partition_length;
97 
98 	char	filename[PATH_MAX];
99 	struct stat stats;
100 } part_data_t;
101 
102 #define MAX_SECTIONS	8
103 #define DEFAULT_OUTPUT_FILE 	"firmware-image.bin"
104 #define DEFAULT_VERSION		"UNKNOWN"
105 
106 #define OPTIONS "B:hv:o:r:k:"
107 
108 static int debug = 1;
109 
110 typedef struct image_info {
111 	char version[256];
112 	char outputfile[PATH_MAX];
113 	u_int32_t	part_count;
114 	part_data_t parts[MAX_SECTIONS];
115 } image_info_t;
116 
write_header(void * mem,const char * version)117 static void write_header(void* mem, const char* version)
118 {
119 	header_t* header = mem;
120 	memset(header, 0, sizeof(header_t));
121 
122 	memcpy(header->magic, MAGIC_HEADER, MAGIC_LENGTH);
123 	strncpy(header->version, version, sizeof(header->version));
124 	header->crc = htonl(crc32(0L, (unsigned char *)header,
125 				sizeof(header_t) - 2 * sizeof(u_int32_t)));
126 	header->pad = 0L;
127 }
128 
129 
write_signature(void * mem,u_int32_t sig_offset)130 static void write_signature(void* mem, u_int32_t sig_offset)
131 {
132 	/* write signature */
133 	signature_t* sign = (signature_t*)(mem + sig_offset);
134 	memset(sign, 0, sizeof(signature_t));
135 
136 	memcpy(sign->magic, MAGIC_END, MAGIC_LENGTH);
137 	sign->crc = htonl(crc32(0L,(unsigned char *)mem, sig_offset));
138 	sign->pad = 0L;
139 }
140 
write_part(void * mem,part_data_t * d)141 static int write_part(void* mem, part_data_t* d)
142 {
143 	char* addr;
144 	int fd;
145 	part_t* p = mem;
146 	part_crc_t* crc = mem + sizeof(part_t) + d->stats.st_size;
147 
148 	fd = open(d->filename, O_RDONLY);
149 	if (fd < 0)
150 	{
151 		ERROR("Failed opening file '%s'\n", d->filename);
152 		return -1;
153 	}
154 
155 	if ((addr=(char*)mmap(0, d->stats.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
156 	{
157 		ERROR("Failed mmaping memory for file '%s'\n", d->filename);
158 		close(fd);
159 		return -2;
160 	}
161 
162 	memcpy(mem + sizeof(part_t), addr, d->stats.st_size);
163 	munmap(addr, d->stats.st_size);
164 
165 	memset(p->name, 0, sizeof(p->name));
166 	strncpy(p->magic, MAGIC_PART, MAGIC_LENGTH);
167 	strncpy(p->name, d->partition_name, sizeof(p->name));
168 	p->index = htonl(d->partition_index);
169 	p->data_size = htonl(d->stats.st_size);
170 	p->part_size = htonl(d->partition_length);
171 	p->baseaddr = htonl(d->partition_baseaddr);
172 	p->memaddr = htonl(d->partition_memaddr);
173 	p->entryaddr = htonl(d->partition_entryaddr);
174 
175 	crc->crc = htonl(crc32(0L, mem, d->stats.st_size + sizeof(part_t)));
176 	crc->pad = 0L;
177 
178 	return 0;
179 }
180 
usage(const char * progname)181 static void usage(const char* progname)
182 {
183 	INFO("Version %s\n"
184              "Usage: %s [options]\n"
185 	     "\t-v <version string>\t - firmware version information, default: %s\n"
186 	     "\t-o <output file>\t - firmware output file, default: %s\n"
187 	     "\t-k <kernel file>\t\t - kernel file\n"
188 	     "\t-r <rootfs file>\t\t - rootfs file\n"
189 	     "\t-B <board name>\t\t - choose firmware layout for specified board (XS2, XS5, RS, XM)\n"
190 	     "\t-h\t\t\t - this help\n", VERSION,
191 	     progname, DEFAULT_VERSION, DEFAULT_OUTPUT_FILE);
192 }
193 
print_image_info(const image_info_t * im)194 static void print_image_info(const image_info_t* im)
195 {
196 	int i = 0;
197 	INFO("Firmware version: '%s'\n"
198 	     "Output file: '%s'\n"
199 	     "Part count: %u\n",
200 	     im->version, im->outputfile,
201 	     im->part_count);
202 
203 	for (i = 0; i < im->part_count; ++i)
204 	{
205 		const part_data_t* d = &im->parts[i];
206 		INFO(" %10s: %8ld bytes (free: %8ld)\n",
207 		     d->partition_name,
208 		     d->stats.st_size,
209 		     d->partition_length - d->stats.st_size);
210 	}
211 }
212 
213 
214 
filelength(const char * file)215 static u_int32_t filelength(const char* file)
216 {
217 	FILE *p;
218 	int ret = -1;
219 
220 	if ( (p = fopen(file, "rb") ) == NULL) return (-1);
221 
222 	fseek(p, 0, SEEK_END);
223 	ret = ftell(p);
224 
225 	fclose (p);
226 
227 	return (ret);
228 }
229 
create_image_layout(const char * kernelfile,const char * rootfsfile,char * board_name,image_info_t * im)230 static int create_image_layout(const char* kernelfile, const char* rootfsfile, char* board_name, image_info_t* im)
231 {
232 	part_data_t* kernel = &im->parts[0];
233 	part_data_t* rootfs = &im->parts[1];
234 
235 	fw_layout_t* p;
236 
237 	p = &fw_layout_data[0];
238 	while ((strlen(p->name) != 0) && (strncmp(p->name, board_name, sizeof(board_name)) != 0))
239 		p++;
240 	if (p->name == NULL) {
241 		printf("BUG! Unable to find default fw layout!\n");
242 		exit(-1);
243 	}
244 
245 	printf("board = %s\n", p->name);
246 	strcpy(kernel->partition_name, "kernel");
247 	kernel->partition_index = 1;
248 	kernel->partition_baseaddr = p->kern_start;
249 	if ( (kernel->partition_length = filelength(kernelfile)) < 0) return (-1);
250 	kernel->partition_memaddr = p->kern_entry;
251 	kernel->partition_entryaddr = p->kern_entry;
252 	strncpy(kernel->filename, kernelfile, sizeof(kernel->filename));
253 
254 	if (filelength(rootfsfile) + kernel->partition_length > p->firmware_max_length)
255 		return (-2);
256 
257 	strcpy(rootfs->partition_name, "rootfs");
258 	rootfs->partition_index = 2;
259 	rootfs->partition_baseaddr = kernel->partition_baseaddr + kernel->partition_length;
260 	rootfs->partition_length = p->firmware_max_length - kernel->partition_length;
261 	rootfs->partition_memaddr = 0x00000000;
262 	rootfs->partition_entryaddr = 0x00000000;
263 	strncpy(rootfs->filename, rootfsfile, sizeof(rootfs->filename));
264 
265 printf("kernel: %d 0x%08x\n", kernel->partition_length, kernel->partition_baseaddr);
266 printf("root: %d 0x%08x\n", rootfs->partition_length, rootfs->partition_baseaddr);
267 	im->part_count = 2;
268 
269 	return 0;
270 }
271 
272 /**
273  * Checks the availability and validity of all image components.
274  * Fills in stats member of the part_data structure.
275  */
validate_image_layout(image_info_t * im)276 static int validate_image_layout(image_info_t* im)
277 {
278 	int i;
279 
280 	if (im->part_count == 0 || im->part_count > MAX_SECTIONS)
281 	{
282 		ERROR("Invalid part count '%d'\n", im->part_count);
283 		return -1;
284 	}
285 
286 	for (i = 0; i < im->part_count; ++i)
287 	{
288 		part_data_t* d = &im->parts[i];
289 		int len = strlen(d->partition_name);
290 		if (len == 0 || len > 16)
291 		{
292 			ERROR("Invalid partition name '%s' of the part %d\n",
293 					d->partition_name, i);
294 			return -1;
295 		}
296 		if (stat(d->filename, &d->stats) < 0)
297 		{
298 			ERROR("Couldn't stat file '%s' from part '%s'\n",
299 				       	d->filename, d->partition_name);
300 			return -2;
301 		}
302 		if (d->stats.st_size == 0)
303 		{
304 			ERROR("File '%s' from part '%s' is empty!\n",
305 				       	d->filename, d->partition_name);
306 			return -3;
307 		}
308 		if (d->stats.st_size > d->partition_length) {
309 			ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
310 				       	d->filename, i, d->partition_length,
311 					d->stats.st_size - d->partition_length);
312 			return -4;
313 		}
314 	}
315 
316 	return 0;
317 }
318 
build_image(image_info_t * im)319 static int build_image(image_info_t* im)
320 {
321 	char* mem;
322 	char* ptr;
323 	u_int32_t mem_size;
324 	FILE* f;
325 	int i;
326 
327 	// build in-memory buffer
328 	mem_size = sizeof(header_t) + sizeof(signature_t);
329 	for (i = 0; i < im->part_count; ++i)
330 	{
331 		part_data_t* d = &im->parts[i];
332 		mem_size += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
333 	}
334 
335 	mem = (char*)calloc(mem_size, 1);
336 	if (mem == NULL)
337 	{
338 		ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size);
339 		return -1;
340 	}
341 
342 	// write header
343 	write_header(mem, im->version);
344 	ptr = mem + sizeof(header_t);
345 	// write all parts
346 	for (i = 0; i < im->part_count; ++i)
347 	{
348 		part_data_t* d = &im->parts[i];
349 		int rc;
350 		if ((rc = write_part(ptr, d)) != 0)
351 		{
352 			ERROR("ERROR: failed writing part %u '%s'\n", i, d->partition_name);
353 		}
354 		ptr += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
355 	}
356 	// write signature
357 	write_signature(mem, mem_size - sizeof(signature_t));
358 
359 	// write in-memory buffer into file
360 	if ((f = fopen(im->outputfile, "w")) == NULL)
361 	{
362 		ERROR("Can not create output file: '%s'\n", im->outputfile);
363 		return -10;
364 	}
365 
366 	if (fwrite(mem, mem_size, 1, f) != 1)
367 	{
368 		ERROR("Could not write %d bytes into file: '%s'\n",
369 				mem_size, im->outputfile);
370 		return -11;
371 	}
372 
373 	free(mem);
374 	fclose(f);
375 	return 0;
376 }
377 
378 
main(int argc,char * argv[])379 int main(int argc, char* argv[])
380 {
381 	char kernelfile[PATH_MAX];
382 	char rootfsfile[PATH_MAX];
383 	char board_name[PATH_MAX];
384 	int o, rc;
385 	image_info_t im;
386 
387 	memset(&im, 0, sizeof(im));
388 	memset(kernelfile, 0, sizeof(kernelfile));
389 	memset(rootfsfile, 0, sizeof(rootfsfile));
390 	memset(board_name, 0, sizeof(board_name));
391 
392 	strcpy(im.outputfile, DEFAULT_OUTPUT_FILE);
393 	strcpy(im.version, DEFAULT_VERSION);
394 
395 	while ((o = getopt(argc, argv, OPTIONS)) != -1)
396 	{
397 		switch (o) {
398 		case 'v':
399 			if (optarg)
400 				strncpy(im.version, optarg, sizeof(im.version));
401 			break;
402 		case 'o':
403 			if (optarg)
404 				strncpy(im.outputfile, optarg, sizeof(im.outputfile));
405 			break;
406 		case 'h':
407 			usage(argv[0]);
408 			return -1;
409 		case 'k':
410 			if (optarg)
411 				strncpy(kernelfile, optarg, sizeof(kernelfile));
412 			break;
413 		case 'r':
414 			if (optarg)
415 				strncpy(rootfsfile, optarg, sizeof(rootfsfile));
416 			break;
417 		case 'B':
418 			if (optarg)
419 				strncpy(board_name, optarg, sizeof(board_name));
420 			break;
421 		}
422 	}
423 	if (strlen(board_name) == 0)
424 		strcpy(board_name, "XS2"); /* default to XS2 */
425 
426 	if (strlen(kernelfile) == 0)
427 	{
428 		ERROR("Kernel file is not specified, cannot continue\n");
429 		usage(argv[0]);
430 		return -2;
431 	}
432 
433 	if (strlen(rootfsfile) == 0)
434 	{
435 		ERROR("Root FS file is not specified, cannot continue\n");
436 		usage(argv[0]);
437 		return -2;
438 	}
439 
440 	if ((rc = create_image_layout(kernelfile, rootfsfile, board_name, &im)) != 0)
441 	{
442 		ERROR("Failed creating firmware layout description - error code: %d\n", rc);
443 		return -3;
444 	}
445 
446 	if ((rc = validate_image_layout(&im)) != 0)
447 	{
448 		ERROR("Failed validating firmware layout - error code: %d\n", rc);
449 		return -4;
450 	}
451 
452 	print_image_info(&im);
453 
454 	if ((rc = build_image(&im)) != 0)
455 	{
456 		ERROR("Failed building image file '%s' - error code: %d\n", im.outputfile, rc);
457 		return -5;
458 	}
459 
460 	return 0;
461 }
462