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