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