2 * Copyright (C) 2007 Ubiquiti Networks, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <sys/types.h>
27 #include <netinet/in.h>
34 typedef struct part_data
{
35 char partition_name
[64];
37 u_int32_t partition_baseaddr
;
38 u_int32_t partition_memaddr
;
39 u_int32_t partition_entryaddr
;
40 u_int32_t partition_length
;
42 char filename
[PATH_MAX
];
46 #define MAX_SECTIONS 8
47 #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
48 #define DEFAULT_VERSION "UNKNOWN"
50 #define OPTIONS "hv:o:i:"
54 typedef struct image_info
{
56 char outputfile
[PATH_MAX
];
58 part_data_t parts
[MAX_SECTIONS
];
61 static void write_header(void* mem
, const char* version
)
63 header_t
* header
= mem
;
64 memset(header
, 0, sizeof(header_t
));
66 memcpy(header
->magic
, MAGIC_HEADER
, MAGIC_LENGTH
);
67 strncpy(header
->version
, version
, sizeof(header
->version
));
68 header
->crc
= htonl(crc32(0L, (unsigned char *)header
,
69 sizeof(header_t
) - 2 * sizeof(u_int32_t
)));
74 static void write_signature(void* mem
, u_int32_t sig_offset
)
77 signature_t
* sign
= (signature_t
*)(mem
+ sig_offset
);
78 memset(sign
, 0, sizeof(signature_t
));
80 memcpy(sign
->magic
, MAGIC_END
, MAGIC_LENGTH
);
81 sign
->crc
= htonl(crc32(0L,(unsigned char *)mem
, sig_offset
));
85 static int write_part(void* mem
, part_data_t
* d
)
90 part_crc_t
* crc
= mem
+ sizeof(part_t
) + d
->stats
.st_size
;
92 fd
= open(d
->filename
, O_RDONLY
);
95 ERROR("Failed opening file '%s'\n", d
->filename
);
99 if ((addr
=(char*)mmap(0, d
->stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
101 ERROR("Failed mmaping memory for file '%s'\n", d
->filename
);
106 memcpy(mem
+ sizeof(part_t
), addr
, d
->stats
.st_size
);
107 munmap(addr
, d
->stats
.st_size
);
109 memset(p
->name
, 0, sizeof(p
->name
));
110 strncpy(p
->magic
, MAGIC_PART
, MAGIC_LENGTH
);
111 strncpy(p
->name
, d
->partition_name
, sizeof(p
->name
));
112 p
->index
= htonl(d
->partition_index
);
113 p
->data_size
= htonl(d
->stats
.st_size
);
114 p
->part_size
= htonl(d
->partition_length
);
115 p
->baseaddr
= htonl(d
->partition_baseaddr
);
116 p
->memaddr
= htonl(d
->partition_memaddr
);
117 p
->entryaddr
= htonl(d
->partition_entryaddr
);
119 crc
->crc
= htonl(crc32(0L, mem
, d
->stats
.st_size
+ sizeof(part_t
)));
125 static void usage(const char* progname
)
128 "Usage: %s [options]\n"
129 "\t-v <version string>\t - firmware version information, default: %s\n"
130 "\t-o <output file>\t - firmware output file, default: %s\n"
131 "\t-i <input file>\t\t - firmware layout file, default: none\n"
132 "\t-h\t\t\t - this help\n", VERSION
,
133 progname
, DEFAULT_VERSION
, DEFAULT_OUTPUT_FILE
);
136 static void print_image_info(const image_info_t
* im
)
139 INFO("Firmware version: '%s'\n"
140 "Output file: '%s'\n"
142 im
->version
, im
->outputfile
,
145 for (i
= 0; i
< im
->part_count
; ++i
)
147 const part_data_t
* d
= &im
->parts
[i
];
148 INFO(" %10s: %8ld bytes (free: %8ld)\n",
151 d
->partition_length
- d
->stats
.st_size
);
158 * Image layout file format:
160 * <partition name>\t<partition index>\t<partition size>\t<data file name>
163 static int parse_image_layout(const char* layoutfile
, image_info_t
* im
)
171 fd
= open(layoutfile
, O_RDONLY
);
173 ERROR("Could not open file '%s'\n", layoutfile
);
195 if (fgets(line
, sizeof(line
), f
) == NULL
)
198 // TODO: very inconvenient format, use smarter parsing someday
199 if ((c
= sscanf(line
, "%32[^\t]\t%X\t%X\t%X\t%X\t%X\t%128[^\t\n]", name
, &index
, &baseaddr
, &size
, &memaddr
, &entryaddr
, file
)) != 7)
202 DEBUG("%s\t\t0x%02X\t0x%08X\t0x%08X\t0x%08X\t0x%08X\t%s\n", name
, index
, baseaddr
, size
, memaddr
, entryaddr
, file
);
205 if (c
== MAX_SECTIONS
)
209 strncpy(d
->partition_name
, name
, sizeof(d
->partition_name
));
210 d
->partition_index
= index
;
211 d
->partition_baseaddr
= baseaddr
;
212 d
->partition_length
= size
;
213 d
->partition_memaddr
= memaddr
;
214 d
->partition_entryaddr
= entryaddr
;
215 strncpy(d
->filename
, file
, sizeof(d
->filename
));
226 * Checks the availability and validity of all image components.
227 * Fills in stats member of the part_data structure.
229 static int validate_image_layout(image_info_t
* im
)
233 if (im
->part_count
== 0 || im
->part_count
> MAX_SECTIONS
)
235 ERROR("Invalid part count '%d'\n", im
->part_count
);
239 for (i
= 0; i
< im
->part_count
; ++i
)
241 part_data_t
* d
= &im
->parts
[i
];
242 int len
= strlen(d
->partition_name
);
243 if (len
== 0 || len
> 16)
245 ERROR("Invalid partition name '%s' of the part %d\n",
246 d
->partition_name
, i
);
249 if (stat(d
->filename
, &d
->stats
) < 0)
251 ERROR("Couldn't stat file '%s' from part '%s'\n",
252 d
->filename
, d
->partition_name
);
255 if (d
->stats
.st_size
== 0)
257 ERROR("File '%s' from part '%s' is empty!\n",
258 d
->filename
, d
->partition_name
);
261 if (d
->stats
.st_size
> d
->partition_length
) {
262 ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
263 d
->filename
, i
, d
->partition_length
,
264 d
->stats
.st_size
- d
->partition_length
);
272 static int build_image(image_info_t
* im
)
280 // build in-memory buffer
281 mem_size
= sizeof(header_t
) + sizeof(signature_t
);
282 for (i
= 0; i
< im
->part_count
; ++i
)
284 part_data_t
* d
= &im
->parts
[i
];
285 mem_size
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
288 mem
= (char*)calloc(mem_size
, 1);
291 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size
);
296 write_header(mem
, im
->version
);
297 ptr
= mem
+ sizeof(header_t
);
299 for (i
= 0; i
< im
->part_count
; ++i
)
301 part_data_t
* d
= &im
->parts
[i
];
303 if ((rc
= write_part(ptr
, d
)) != 0)
305 ERROR("ERROR: failed writing part %u '%s'\n", i
, d
->partition_name
);
307 ptr
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
310 write_signature(mem
, mem_size
- sizeof(signature_t
));
312 // write in-memory buffer into file
313 if ((f
= fopen(im
->outputfile
, "w")) == NULL
)
315 ERROR("Can not create output file: '%s'\n", im
->outputfile
);
319 if (fwrite(mem
, mem_size
, 1, f
) != 1)
321 ERROR("Could not write %d bytes into file: '%s'\n",
322 mem_size
, im
->outputfile
);
332 int main(int argc
, char* argv
[])
334 char inputfile
[PATH_MAX
];
338 memset(&im
, 0, sizeof(im
));
339 memset(inputfile
, 0, sizeof(inputfile
));
341 strcpy(im
.outputfile
, DEFAULT_OUTPUT_FILE
);
342 strcpy(im
.version
, DEFAULT_VERSION
);
344 while ((o
= getopt(argc
, argv
, OPTIONS
)) != -1)
349 strncpy(im
.version
, optarg
, sizeof(im
.version
));
353 strncpy(im
.outputfile
, optarg
, sizeof(im
.outputfile
));
357 strncpy(inputfile
, optarg
, sizeof(inputfile
));
365 if (strlen(inputfile
) == 0)
367 ERROR("Input file is not specified, cannot continue\n");
372 if ((rc
= parse_image_layout(inputfile
, &im
)) != 0)
374 ERROR("Failed parsing firmware layout file '%s' - error code: %d\n",
379 if ((rc
= validate_image_layout(&im
)) != 0)
381 ERROR("Failed validating firmware layout - error code: %d\n", rc
);
385 print_image_info(&im
);
387 if ((rc
= build_image(&im
)) != 0)
389 ERROR("Failed building image file '%s' - error code: %d\n", im
.outputfile
, rc
);