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