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
= 0x00640000,
62 .kern_start
= 0xbf030000,
63 .kern_entry
= 0x80060000,
64 .firmware_max_length
= 0x00640000,
70 typedef struct part_data
{
71 char partition_name
[64];
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
;
79 char filename
[PATH_MAX
];
83 #define MAX_SECTIONS 8
84 #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
85 #define DEFAULT_VERSION "UNKNOWN"
87 #define OPTIONS "B:hv:o:r:k:"
91 typedef struct image_info
{
93 char outputfile
[PATH_MAX
];
95 part_data_t parts
[MAX_SECTIONS
];
98 static void write_header(void* mem
, const char* version
)
100 header_t
* header
= mem
;
101 memset(header
, 0, sizeof(header_t
));
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
)));
111 static void write_signature(void* mem
, u_int32_t sig_offset
)
113 /* write signature */
114 signature_t
* sign
= (signature_t
*)(mem
+ sig_offset
);
115 memset(sign
, 0, sizeof(signature_t
));
117 memcpy(sign
->magic
, MAGIC_END
, MAGIC_LENGTH
);
118 sign
->crc
= htonl(crc32(0L,(unsigned char *)mem
, sig_offset
));
122 static int write_part(void* mem
, part_data_t
* d
)
127 part_crc_t
* crc
= mem
+ sizeof(part_t
) + d
->stats
.st_size
;
129 fd
= open(d
->filename
, O_RDONLY
);
132 ERROR("Failed opening file '%s'\n", d
->filename
);
136 if ((addr
=(char*)mmap(0, d
->stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
138 ERROR("Failed mmaping memory for file '%s'\n", d
->filename
);
143 memcpy(mem
+ sizeof(part_t
), addr
, d
->stats
.st_size
);
144 munmap(addr
, d
->stats
.st_size
);
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
);
156 crc
->crc
= htonl(crc32(0L, mem
, d
->stats
.st_size
+ sizeof(part_t
)));
162 static void usage(const char* progname
)
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
);
175 static void print_image_info(const image_info_t
* im
)
178 INFO("Firmware version: '%s'\n"
179 "Output file: '%s'\n"
181 im
->version
, im
->outputfile
,
184 for (i
= 0; i
< im
->part_count
; ++i
)
186 const part_data_t
* d
= &im
->parts
[i
];
187 INFO(" %10s: %8ld bytes (free: %8ld)\n",
190 d
->partition_length
- d
->stats
.st_size
);
196 static u_int32_t
filelength(const char* file
)
201 if ( (p
= fopen(file
, "rb") ) == NULL
) return (-1);
203 fseek(p
, 0, SEEK_END
);
211 static int create_image_layout(const char* kernelfile
, const char* rootfsfile
, char* board_name
, image_info_t
* im
)
213 part_data_t
* kernel
= &im
->parts
[0];
214 part_data_t
* rootfs
= &im
->parts
[1];
218 p
= &fw_layout_data
[0];
219 while ((strlen(p
->name
) != 0) && (strncmp(p
->name
, board_name
, sizeof(board_name
)) != 0))
221 if (p
->name
== NULL
) {
222 printf("BUG! Unable to find default fw layout!\n");
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
));
235 if (filelength(rootfsfile
) + kernel
->partition_length
> p
->firmware_max_length
)
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
));
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
);
254 * Checks the availability and validity of all image components.
255 * Fills in stats member of the part_data structure.
257 static int validate_image_layout(image_info_t
* im
)
261 if (im
->part_count
== 0 || im
->part_count
> MAX_SECTIONS
)
263 ERROR("Invalid part count '%d'\n", im
->part_count
);
267 for (i
= 0; i
< im
->part_count
; ++i
)
269 part_data_t
* d
= &im
->parts
[i
];
270 int len
= strlen(d
->partition_name
);
271 if (len
== 0 || len
> 16)
273 ERROR("Invalid partition name '%s' of the part %d\n",
274 d
->partition_name
, i
);
277 if (stat(d
->filename
, &d
->stats
) < 0)
279 ERROR("Couldn't stat file '%s' from part '%s'\n",
280 d
->filename
, d
->partition_name
);
283 if (d
->stats
.st_size
== 0)
285 ERROR("File '%s' from part '%s' is empty!\n",
286 d
->filename
, d
->partition_name
);
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
);
300 static int build_image(image_info_t
* im
)
308 // build in-memory buffer
309 mem_size
= sizeof(header_t
) + sizeof(signature_t
);
310 for (i
= 0; i
< im
->part_count
; ++i
)
312 part_data_t
* d
= &im
->parts
[i
];
313 mem_size
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
316 mem
= (char*)calloc(mem_size
, 1);
319 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size
);
324 write_header(mem
, im
->version
);
325 ptr
= mem
+ sizeof(header_t
);
327 for (i
= 0; i
< im
->part_count
; ++i
)
329 part_data_t
* d
= &im
->parts
[i
];
331 if ((rc
= write_part(ptr
, d
)) != 0)
333 ERROR("ERROR: failed writing part %u '%s'\n", i
, d
->partition_name
);
335 ptr
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
338 write_signature(mem
, mem_size
- sizeof(signature_t
));
340 // write in-memory buffer into file
341 if ((f
= fopen(im
->outputfile
, "w")) == NULL
)
343 ERROR("Can not create output file: '%s'\n", im
->outputfile
);
347 if (fwrite(mem
, mem_size
, 1, f
) != 1)
349 ERROR("Could not write %d bytes into file: '%s'\n",
350 mem_size
, im
->outputfile
);
360 int main(int argc
, char* argv
[])
362 char kernelfile
[PATH_MAX
];
363 char rootfsfile
[PATH_MAX
];
364 char board_name
[PATH_MAX
];
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
));
373 strcpy(im
.outputfile
, DEFAULT_OUTPUT_FILE
);
374 strcpy(im
.version
, DEFAULT_VERSION
);
376 while ((o
= getopt(argc
, argv
, OPTIONS
)) != -1)
381 strncpy(im
.version
, optarg
, sizeof(im
.version
));
385 strncpy(im
.outputfile
, optarg
, sizeof(im
.outputfile
));
392 strncpy(kernelfile
, optarg
, sizeof(kernelfile
));
396 strncpy(rootfsfile
, optarg
, sizeof(rootfsfile
));
400 strncpy(board_name
, optarg
, sizeof(board_name
));
404 if (strlen(board_name
) == 0)
405 strcpy(board_name
, "XS2"); /* default to XS2 */
407 if (strlen(kernelfile
) == 0)
409 ERROR("Kernel file is not specified, cannot continue\n");
414 if (strlen(rootfsfile
) == 0)
416 ERROR("Root FS file is not specified, cannot continue\n");
421 if ((rc
= create_image_layout(kernelfile
, rootfsfile
, board_name
, &im
)) != 0)
423 ERROR("Failed creating firmware layout description - error code: %d\n", rc
);
427 if ((rc
= validate_image_layout(&im
)) != 0)
429 ERROR("Failed validating firmware layout - error code: %d\n", rc
);
433 print_image_info(&im
);
435 if ((rc
= build_image(&im
)) != 0)
437 ERROR("Failed building image file '%s' - error code: %d\n", im
.outputfile
, rc
);