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>
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.
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.
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.
21 #include <sys/types.h>
29 #include <netinet/in.h>
36 #define VERSION "1.2-OpenWrt.1"
38 #define MAX_SECTIONS 8
39 #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
40 #define DEFAULT_VERSION "UNKNOWN"
41 #define DEFAULT_FLASH_BASE (0xbfc00000)
43 #define FIRMWARE_MAX_LENGTH (0x390000)
45 typedef struct part_data
{
46 char partition_name
[64];
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
;
54 char filename
[PATH_MAX
];
58 typedef struct image_info
{
60 char outputfile
[PATH_MAX
];
61 char magic
[MAGIC_LENGTH
];
62 u_int32_t flash_baseaddr
;
64 part_data_t parts
[MAX_SECTIONS
];
67 static image_info_t im
;
69 static int zero_part_baseaddr
= 0;
71 static void write_header(void* mem
, const char* version
)
73 header_t
* header
= mem
;
74 memset(header
, 0, sizeof(header_t
));
76 memcpy(header
->magic
, im
.magic
, MAGIC_LENGTH
);
77 strncpy(header
->version
, version
, sizeof(header
->version
));
78 header
->crc
= htonl(crc32(0L, (unsigned char *)header
,
79 sizeof(header_t
) - 2 * sizeof(u_int32_t
)));
83 static void write_signature(void* mem
, u_int32_t sig_offset
)
86 signature_t
* sign
= (signature_t
*)(mem
+ sig_offset
);
87 memset(sign
, 0, sizeof(signature_t
));
89 memcpy(sign
->magic
, MAGIC_END
, MAGIC_LENGTH
);
90 sign
->crc
= htonl(crc32(0L,(unsigned char *)mem
, sig_offset
));
94 static int write_part(void* mem
, part_data_t
* d
)
99 part_crc_t
* crc
= mem
+ sizeof(part_t
) + d
->stats
.st_size
;
101 fd
= open(d
->filename
, O_RDONLY
);
103 ERROR("Failed opening file '%s'\n", d
->filename
);
107 if ((addr
=(char*)mmap(0, d
->stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
108 ERROR("Failed mmaping memory for file '%s'\n", d
->filename
);
113 memcpy(mem
+ sizeof(part_t
), addr
, d
->stats
.st_size
);
114 munmap(addr
, d
->stats
.st_size
);
116 memset(p
->name
, 0, sizeof(p
->name
));
117 strncpy(p
->magic
, MAGIC_PART
, MAGIC_LENGTH
);
118 strncpy(p
->name
, d
->partition_name
, sizeof(p
->name
));
119 p
->index
= htonl(d
->partition_index
);
120 p
->data_size
= htonl(d
->stats
.st_size
);
121 p
->part_size
= htonl(d
->partition_length
);
122 p
->baseaddr
= htonl(d
->partition_baseaddr
);
123 p
->memaddr
= htonl(d
->partition_memaddr
);
124 p
->entryaddr
= htonl(d
->partition_entryaddr
);
126 crc
->crc
= htonl(crc32(0L, mem
, d
->stats
.st_size
+ sizeof(part_t
)));
132 static void usage(const char* progname
)
135 "Usage: %s [options]\n"
136 "\t-v <version string>\t - firmware version information, default: %s\n"
137 "\t-m <magic>\t\t - firmware magic, default: %s\n"
138 "\t-f <flash base>\t\t - flash base address, default: 0x%08x\n"
139 "\t-o <output file>\t - firmware output file, default: %s\n"
140 "\t-p <name>:<offset>:<len>:<memaddr>:<entry>:<file>\n "
141 "\t\t\t\t - create a partition from <file>\n"
142 "\t-z\t\t\t - set partition offsets to zero\n"
143 "\t-h\t\t\t - this help\n",
144 VERSION
, progname
, DEFAULT_VERSION
, MAGIC_HEADER
,
145 DEFAULT_FLASH_BASE
, DEFAULT_OUTPUT_FILE
);
148 static void print_image_info(void)
152 INFO("Firmware version : '%s'\n"
153 "Output file : '%s'\n"
155 im
.version
, im
.outputfile
, im
.part_count
);
157 for (i
= 0; i
< im
.part_count
; ++i
) {
158 const part_data_t
* d
= &im
.parts
[i
];
159 INFO(" %10s: %08x %08x %08x %08x %8ld bytes (free: %8ld)\n",
161 d
->partition_baseaddr
,
163 d
->partition_entryaddr
,
164 d
->partition_memaddr
,
166 d
->partition_length
- d
->stats
.st_size
);
170 static int filelength(const char* file
)
175 if ( (p
= fopen(file
, "rb") ) == NULL
) return (-1);
177 fseek(p
, 0, SEEK_END
);
185 int str2u32(char *arg
, u_int32_t
*val
)
191 t
= strtoul(arg
, &err
, 0);
192 if (errno
|| (err
== arg
) || ((err
!= NULL
) && *err
)) {
200 static int image_layout_add_partition(const char *part_desc
)
209 if (im
.part_count
>= MAX_SECTIONS
) {
210 ERROR("Too many partitions specified\n");
214 d
= &im
.parts
[im
.part_count
];
215 t
= sscanf(part_desc
, "%15[a-zA-Z]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%256s",
224 ERROR("Bad partition parameter %d, '%s'\n", t
, part_desc
);
228 if (strlen(d
->partition_name
) == 0) {
229 ERROR("No partition name specified in '%s'\n", part_desc
);
233 if (str2u32(offset
, &d
->partition_offset
)) {
234 ERROR("Bad offset value '%s'\n", offset
);
238 if (str2u32(length
, &d
->partition_length
)) {
239 ERROR("Bad length value '%s'\n", length
);
243 if (d
->partition_length
== 0) {
245 flen
= filelength(d
->filename
);
247 ERROR("Unable to determine size of '%s'\n",
251 d
->partition_length
= flen
;
254 if (str2u32(memaddr
, &d
->partition_memaddr
)) {
255 ERROR("Bad memaddr vaule '%s'\n", memaddr
);
259 if (str2u32(entryaddr
, &d
->partition_entryaddr
)) {
260 ERROR("Bad entry address value '%s'\n", entryaddr
);
265 d
->partition_index
= im
.part_count
;
270 static int image_layout_verify(void)
275 if (im
.part_count
== 0) {
276 ERROR("No partitions specified\n");
280 offset
= im
.parts
[0].partition_offset
;
281 for (i
= 0; i
< im
.part_count
; i
++)
283 part_data_t
* d
= &im
.parts
[i
];
285 if (stat(d
->filename
, &d
->stats
) < 0) {
286 ERROR("Couldn't stat file '%s' from part '%s'\n",
287 d
->filename
, d
->partition_name
);
291 if (d
->stats
.st_size
== 0) {
292 ERROR("File '%s' from part '%s' is empty!\n",
293 d
->filename
, d
->partition_name
);
297 if (d
->stats
.st_size
> d
->partition_length
) {
298 ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
299 d
->filename
, i
, d
->partition_length
,
300 d
->stats
.st_size
- d
->partition_length
);
304 if (d
->partition_offset
< offset
)
305 d
->partition_offset
= offset
;
307 if (zero_part_baseaddr
) {
308 d
->partition_baseaddr
= 0;
310 d
->partition_baseaddr
=
311 im
.flash_baseaddr
+ d
->partition_offset
;
313 offset
+= d
->partition_length
;
319 static int build_image(void)
327 /* build in-memory buffer */
328 mem_size
= sizeof(header_t
) + sizeof(signature_t
);
329 for (i
= 0; i
< im
.part_count
; ++i
) {
330 part_data_t
* d
= &im
.parts
[i
];
331 mem_size
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
334 mem
= (char*)calloc(mem_size
, 1);
336 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size
);
341 write_header(mem
, im
.version
);
342 ptr
= mem
+ sizeof(header_t
);
344 /* write all parts */
345 for (i
= 0; i
< im
.part_count
; ++i
) {
346 part_data_t
* d
= &im
.parts
[i
];
348 if ((rc
= write_part(ptr
, d
)) != 0) {
349 ERROR("ERROR: failed writing part %u '%s'\n", i
, d
->partition_name
);
352 ptr
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
356 /* write signature */
357 write_signature(mem
, mem_size
- sizeof(signature_t
));
359 /* write in-memory buffer into file */
360 if ((f
= fopen(im
.outputfile
, "w")) == NULL
) {
361 ERROR("Can not create output file: '%s'\n", im
.outputfile
);
365 if (fwrite(mem
, mem_size
, 1, f
) != 1) {
366 ERROR("Could not write %d bytes into file: '%s'\n",
367 mem_size
, im
.outputfile
);
376 int main(int argc
, char* argv
[])
380 memset(&im
, 0, sizeof(im
));
382 strcpy(im
.outputfile
, DEFAULT_OUTPUT_FILE
);
383 strcpy(im
.version
, DEFAULT_VERSION
);
384 memcpy(im
.magic
, MAGIC_HEADER
, MAGIC_LENGTH
);
385 im
.flash_baseaddr
= DEFAULT_FLASH_BASE
;
387 while ((o
= getopt(argc
, argv
, "f:hm:o:p:v:z")) != -1)
392 if (str2u32(optarg
, &im
.flash_baseaddr
)) {
393 ERROR("Invalid flash start address %s\n", optarg
);
402 if (strlen(optarg
) != MAGIC_LENGTH
) {
403 ERROR("Invalid magic %s\n", optarg
);
407 memcpy(im
.magic
, optarg
, MAGIC_LENGTH
);
412 strncpy(im
.outputfile
, optarg
, sizeof(im
.outputfile
));
416 if (image_layout_add_partition(optarg
))
422 strncpy(im
.version
, optarg
, sizeof(im
.version
));
425 zero_part_baseaddr
= 1;
430 rc
= image_layout_verify();
432 ERROR("Failed validating firmware layout - error code: %d\n",
441 ERROR("Failed building image file '%s' - error code: %d\n",