2 * Copyright (C) 2007 Ubiquiti Networks, Inc.
3 * Copyright (C) 2008 Lukas Kuna <ValXdater@seznam.cz>
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.
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.
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.
20 #include <sys/types.h>
28 #include <netinet/in.h>
34 typedef struct fw_layout_data
{
38 u_int32_t firmware_max_length
;
41 fw_layout_t fw_layout_data
[] = {
44 .kern_start
= 0xbfc30000,
45 .kern_entry
= 0x80041000,
46 .firmware_max_length
= 0x00390000,
50 .kern_start
= 0xbe030000,
51 .kern_entry
= 0x80041000,
52 .firmware_max_length
= 0x00390000,
56 .kern_start
= 0xbf030000,
57 .kern_entry
= 0x80060000,
58 .firmware_max_length
= 0x00B00000,
62 .kern_start
= 0xbf030000,
63 .kern_entry
= 0x80060000,
64 .firmware_max_length
= 0x00B00000,
68 .kern_start
= 0xbf030000,
69 .kern_entry
= 0x80060000,
70 .firmware_max_length
= 0x00640000,
74 .kern_start
= 0xa8030000,
75 .kern_entry
= 0x80041000,
76 .firmware_max_length
= 0x006C0000,
80 .kern_start
= 0x9f050000,
81 .kern_entry
= 0x80002000,
82 .firmware_max_length
= 0x006A0000,
88 typedef struct part_data
{
89 char partition_name
[64];
91 u_int32_t partition_baseaddr
;
92 u_int32_t partition_startaddr
;
93 u_int32_t partition_memaddr
;
94 u_int32_t partition_entryaddr
;
95 u_int32_t partition_length
;
97 char filename
[PATH_MAX
];
101 #define MAX_SECTIONS 8
102 #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
103 #define DEFAULT_VERSION "UNKNOWN"
105 #define OPTIONS "B:hv:m:o:r:k:"
107 static int debug
= 0;
109 typedef struct image_info
{
112 char outputfile
[PATH_MAX
];
113 u_int32_t part_count
;
114 part_data_t parts
[MAX_SECTIONS
];
117 static void write_header(void* mem
, const char *magic
, const char* version
)
119 header_t
* header
= mem
;
120 memset(header
, 0, sizeof(header_t
));
122 memcpy(header
->magic
, magic
, 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
)));
130 static void write_signature(void* mem
, u_int32_t sig_offset
)
132 /* write signature */
133 signature_t
* sign
= (signature_t
*)(mem
+ sig_offset
);
134 memset(sign
, 0, sizeof(signature_t
));
136 memcpy(sign
->magic
, MAGIC_END
, MAGIC_LENGTH
);
137 sign
->crc
= htonl(crc32(0L,(unsigned char *)mem
, sig_offset
));
141 static int write_part(void* mem
, part_data_t
* d
)
146 part_crc_t
* crc
= mem
+ sizeof(part_t
) + d
->stats
.st_size
;
148 fd
= open(d
->filename
, O_RDONLY
);
151 ERROR("Failed opening file '%s'\n", d
->filename
);
155 if ((addr
=(char*)mmap(0, d
->stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
157 ERROR("Failed mmaping memory for file '%s'\n", d
->filename
);
162 memcpy(mem
+ sizeof(part_t
), addr
, d
->stats
.st_size
);
163 munmap(addr
, d
->stats
.st_size
);
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
);
175 crc
->crc
= htonl(crc32(0L, mem
, d
->stats
.st_size
+ sizeof(part_t
)));
181 static void usage(const char* progname
)
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-m <magic>\t - firmware magic, default: %s\n"
188 "\t-k <kernel file>\t\t - kernel file\n"
189 "\t-r <rootfs file>\t\t - rootfs file\n"
190 "\t-B <board name>\t\t - choose firmware layout for specified board (XS2, XS5, RS, XM)\n"
191 "\t-h\t\t\t - this help\n", VERSION
,
192 progname
, DEFAULT_VERSION
, DEFAULT_OUTPUT_FILE
, MAGIC_HEADER
);
195 static void print_image_info(const image_info_t
* im
)
198 INFO("Firmware version: '%s'\n"
199 "Output file: '%s'\n"
201 im
->version
, im
->outputfile
,
204 for (i
= 0; i
< im
->part_count
; ++i
)
206 const part_data_t
* d
= &im
->parts
[i
];
207 INFO(" %10s: %8ld bytes (free: %8ld)\n",
210 d
->partition_length
- d
->stats
.st_size
);
216 static u_int32_t
filelength(const char* file
)
221 if ( (p
= fopen(file
, "rb") ) == NULL
) return (-1);
223 fseek(p
, 0, SEEK_END
);
231 static int create_image_layout(const char* kernelfile
, const char* rootfsfile
, char* board_name
, image_info_t
* im
)
233 part_data_t
* kernel
= &im
->parts
[0];
234 part_data_t
* rootfs
= &im
->parts
[1];
238 p
= &fw_layout_data
[0];
239 while ((strlen(p
->name
) != 0) && (strncmp(p
->name
, board_name
, sizeof(board_name
)) != 0))
241 if (p
->name
== NULL
) {
242 printf("BUG! Unable to find default fw layout!\n");
246 printf("board = %s\n", p
->name
);
247 strcpy(kernel
->partition_name
, "kernel");
248 kernel
->partition_index
= 1;
249 kernel
->partition_baseaddr
= p
->kern_start
;
250 if ( (kernel
->partition_length
= filelength(kernelfile
)) < 0) return (-1);
251 kernel
->partition_memaddr
= p
->kern_entry
;
252 kernel
->partition_entryaddr
= p
->kern_entry
;
253 strncpy(kernel
->filename
, kernelfile
, sizeof(kernel
->filename
));
255 if (filelength(rootfsfile
) + kernel
->partition_length
> p
->firmware_max_length
)
258 strcpy(rootfs
->partition_name
, "rootfs");
259 rootfs
->partition_index
= 2;
260 rootfs
->partition_baseaddr
= kernel
->partition_baseaddr
+ kernel
->partition_length
;
261 rootfs
->partition_length
= p
->firmware_max_length
- kernel
->partition_length
;
262 rootfs
->partition_memaddr
= 0x00000000;
263 rootfs
->partition_entryaddr
= 0x00000000;
264 strncpy(rootfs
->filename
, rootfsfile
, sizeof(rootfs
->filename
));
266 printf("kernel: %d 0x%08x\n", kernel
->partition_length
, kernel
->partition_baseaddr
);
267 printf("root: %d 0x%08x\n", rootfs
->partition_length
, rootfs
->partition_baseaddr
);
274 * Checks the availability and validity of all image components.
275 * Fills in stats member of the part_data structure.
277 static int validate_image_layout(image_info_t
* im
)
281 if (im
->part_count
== 0 || im
->part_count
> MAX_SECTIONS
)
283 ERROR("Invalid part count '%d'\n", im
->part_count
);
287 for (i
= 0; i
< im
->part_count
; ++i
)
289 part_data_t
* d
= &im
->parts
[i
];
290 int len
= strlen(d
->partition_name
);
291 if (len
== 0 || len
> 16)
293 ERROR("Invalid partition name '%s' of the part %d\n",
294 d
->partition_name
, i
);
297 if (stat(d
->filename
, &d
->stats
) < 0)
299 ERROR("Couldn't stat file '%s' from part '%s'\n",
300 d
->filename
, d
->partition_name
);
303 if (d
->stats
.st_size
== 0)
305 ERROR("File '%s' from part '%s' is empty!\n",
306 d
->filename
, d
->partition_name
);
309 if (d
->stats
.st_size
> d
->partition_length
) {
310 ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
311 d
->filename
, i
, d
->partition_length
,
312 d
->stats
.st_size
- d
->partition_length
);
320 static int build_image(image_info_t
* im
)
328 // build in-memory buffer
329 mem_size
= sizeof(header_t
) + sizeof(signature_t
);
330 for (i
= 0; i
< im
->part_count
; ++i
)
332 part_data_t
* d
= &im
->parts
[i
];
333 mem_size
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
336 mem
= (char*)calloc(mem_size
, 1);
339 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size
);
344 write_header(mem
, im
->magic
, im
->version
);
345 ptr
= mem
+ sizeof(header_t
);
347 for (i
= 0; i
< im
->part_count
; ++i
)
349 part_data_t
* d
= &im
->parts
[i
];
351 if ((rc
= write_part(ptr
, d
)) != 0)
353 ERROR("ERROR: failed writing part %u '%s'\n", i
, d
->partition_name
);
355 ptr
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
358 write_signature(mem
, mem_size
- sizeof(signature_t
));
360 // write in-memory buffer into file
361 if ((f
= fopen(im
->outputfile
, "w")) == NULL
)
363 ERROR("Can not create output file: '%s'\n", im
->outputfile
);
367 if (fwrite(mem
, mem_size
, 1, f
) != 1)
369 ERROR("Could not write %d bytes into file: '%s'\n",
370 mem_size
, im
->outputfile
);
380 int main(int argc
, char* argv
[])
382 char kernelfile
[PATH_MAX
];
383 char rootfsfile
[PATH_MAX
];
384 char board_name
[PATH_MAX
];
388 memset(&im
, 0, sizeof(im
));
389 memset(kernelfile
, 0, sizeof(kernelfile
));
390 memset(rootfsfile
, 0, sizeof(rootfsfile
));
391 memset(board_name
, 0, sizeof(board_name
));
393 strcpy(im
.outputfile
, DEFAULT_OUTPUT_FILE
);
394 strcpy(im
.version
, DEFAULT_VERSION
);
395 strncpy(im
.magic
, MAGIC_HEADER
, sizeof(im
.magic
));
397 while ((o
= getopt(argc
, argv
, OPTIONS
)) != -1)
402 strncpy(im
.version
, optarg
, sizeof(im
.version
));
406 strncpy(im
.outputfile
, optarg
, sizeof(im
.outputfile
));
410 strncpy(im
.magic
, optarg
, sizeof(im
.magic
));
417 strncpy(kernelfile
, optarg
, sizeof(kernelfile
));
421 strncpy(rootfsfile
, optarg
, sizeof(rootfsfile
));
425 strncpy(board_name
, optarg
, sizeof(board_name
));
429 if (strlen(board_name
) == 0)
430 strcpy(board_name
, "XS2"); /* default to XS2 */
432 if (strlen(kernelfile
) == 0)
434 ERROR("Kernel file is not specified, cannot continue\n");
439 if (strlen(rootfsfile
) == 0)
441 ERROR("Root FS file is not specified, cannot continue\n");
446 if ((rc
= create_image_layout(kernelfile
, rootfsfile
, board_name
, &im
)) != 0)
448 ERROR("Failed creating firmware layout description - error code: %d\n", rc
);
452 if ((rc
= validate_image_layout(&im
)) != 0)
454 ERROR("Failed validating firmware layout - error code: %d\n", rc
);
458 print_image_info(&im
);
460 if ((rc
= build_image(&im
)) != 0)
462 ERROR("Failed building image file '%s' - error code: %d\n", im
.outputfile
, rc
);