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