1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2007 Ubiquiti Networks, Inc.
4 * Copyright (C) 2008 Lukas Kuna <ValXdater@seznam.cz>
5 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
16 #include <netinet/in.h>
23 #define VERSION "1.2-OpenWrt.1"
25 #define MAX_SECTIONS 8
26 #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
27 #define DEFAULT_VERSION "UNKNOWN"
28 #define DEFAULT_FLASH_BASE (0xbfc00000)
30 #define FIRMWARE_MAX_LENGTH (0x390000)
32 typedef struct part_data
{
33 char partition_name
[64];
35 u_int32_t partition_baseaddr
;
36 u_int32_t partition_offset
;
37 u_int32_t partition_memaddr
;
38 u_int32_t partition_entryaddr
;
39 u_int32_t partition_length
;
41 char filename
[PATH_MAX
];
45 typedef struct image_info
{
47 char outputfile
[PATH_MAX
];
48 char magic
[MAGIC_LENGTH
];
49 u_int32_t flash_baseaddr
;
51 part_data_t parts
[MAX_SECTIONS
];
54 static image_info_t im
;
55 static int zero_part_baseaddr
= 0;
57 static void write_header(void* mem
, const char* version
)
59 header_t
* header
= mem
;
60 memset(header
, 0, sizeof(header_t
));
62 memcpy(header
->magic
, im
.magic
, MAGIC_LENGTH
);
63 strncpy(header
->version
, version
, sizeof(header
->version
));
64 header
->crc
= htonl(crc32(0L, (unsigned char *)header
,
65 sizeof(header_t
) - 2 * sizeof(u_int32_t
)));
69 static void write_signature(void* mem
, u_int32_t sig_offset
)
72 signature_t
* sign
= (signature_t
*)(mem
+ sig_offset
);
73 memset(sign
, 0, sizeof(signature_t
));
75 memcpy(sign
->magic
, MAGIC_END
, MAGIC_LENGTH
);
76 sign
->crc
= htonl(crc32(0L,(unsigned char *)mem
, sig_offset
));
80 static int write_part(void* mem
, part_data_t
* d
)
85 part_crc_t
* crc
= mem
+ sizeof(part_t
) + d
->stats
.st_size
;
87 fd
= open(d
->filename
, O_RDONLY
);
89 ERROR("Failed opening file '%s'\n", d
->filename
);
93 if ((addr
=(char*)mmap(0, d
->stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
94 ERROR("Failed mmaping memory for file '%s'\n", d
->filename
);
99 memcpy(mem
+ sizeof(part_t
), addr
, d
->stats
.st_size
);
100 munmap(addr
, d
->stats
.st_size
);
102 memset(p
->name
, 0, sizeof(p
->name
));
103 strncpy(p
->magic
, MAGIC_PART
, MAGIC_LENGTH
);
104 strncpy(p
->name
, d
->partition_name
, sizeof(p
->name
));
105 p
->index
= htonl(d
->partition_index
);
106 p
->data_size
= htonl(d
->stats
.st_size
);
107 p
->part_size
= htonl(d
->partition_length
);
108 p
->baseaddr
= htonl(d
->partition_baseaddr
);
109 p
->memaddr
= htonl(d
->partition_memaddr
);
110 p
->entryaddr
= htonl(d
->partition_entryaddr
);
112 crc
->crc
= htonl(crc32(0L, mem
, d
->stats
.st_size
+ sizeof(part_t
)));
118 static void usage(const char* progname
)
121 "Usage: %s [options]\n"
122 "\t-v <version string>\t - firmware version information, default: %s\n"
123 "\t-m <magic>\t\t - firmware magic, default: %s\n"
124 "\t-f <flash base>\t\t - flash base address, default: 0x%08x\n"
125 "\t-o <output file>\t - firmware output file, default: %s\n"
126 "\t-p <name>:<offset>:<len>:<memaddr>:<entry>:<file>\n "
127 "\t\t\t\t - create a partition from <file>\n"
128 "\t-z\t\t\t - set partition offsets to zero\n"
129 "\t-h\t\t\t - this help\n",
130 VERSION
, progname
, DEFAULT_VERSION
, MAGIC_HEADER
,
131 DEFAULT_FLASH_BASE
, DEFAULT_OUTPUT_FILE
);
134 static void print_image_info(void)
138 INFO("Firmware version : '%s'\n"
139 "Output file : '%s'\n"
141 im
.version
, im
.outputfile
, im
.part_count
);
143 for (i
= 0; i
< im
.part_count
; ++i
) {
144 const part_data_t
* d
= &im
.parts
[i
];
145 INFO(" %10s: %08x %08x %08x %08x %8ld bytes (free: %8ld)\n",
147 d
->partition_baseaddr
,
149 d
->partition_entryaddr
,
150 d
->partition_memaddr
,
152 d
->partition_length
- d
->stats
.st_size
);
156 static int filelength(const char* file
)
161 if ( (p
= fopen(file
, "rb") ) == NULL
) return (-1);
163 fseek(p
, 0, SEEK_END
);
171 int str2u32(char *arg
, u_int32_t
*val
)
177 t
= strtoul(arg
, &err
, 0);
178 if (errno
|| (err
== arg
) || ((err
!= NULL
) && *err
)) {
187 #define STRINGIFY2(X) #X
188 #define STRINGIFY(X) STRINGIFY2(X)
190 static int image_layout_add_partition(const char *part_desc
)
199 if (im
.part_count
>= MAX_SECTIONS
) {
200 ERROR("Too many partitions specified\n");
204 d
= &im
.parts
[im
.part_count
];
205 t
= sscanf(part_desc
, "%15[-0-9a-zA-Z]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%"STRINGIFY(PATH_MAX
)"s",
214 ERROR("Bad partition parameter %d, '%s'\n", t
, part_desc
);
218 if (strlen(d
->partition_name
) == 0) {
219 ERROR("No partition name specified in '%s'\n", part_desc
);
223 if (str2u32(offset
, &d
->partition_offset
)) {
224 ERROR("Bad offset value '%s'\n", offset
);
228 if (str2u32(length
, &d
->partition_length
)) {
229 ERROR("Bad length value '%s'\n", length
);
233 if (d
->partition_length
== 0) {
235 flen
= filelength(d
->filename
);
237 ERROR("Unable to determine size of '%s'\n",
241 d
->partition_length
= flen
;
244 if (str2u32(memaddr
, &d
->partition_memaddr
)) {
245 ERROR("Bad memaddr vaule '%s'\n", memaddr
);
249 if (str2u32(entryaddr
, &d
->partition_entryaddr
)) {
250 ERROR("Bad entry address value '%s'\n", entryaddr
);
255 d
->partition_index
= im
.part_count
;
260 static int image_layout_verify(void)
265 if (im
.part_count
== 0) {
266 ERROR("No partitions specified\n");
270 offset
= im
.parts
[0].partition_offset
;
271 for (i
= 0; i
< im
.part_count
; i
++)
273 part_data_t
* d
= &im
.parts
[i
];
275 if (stat(d
->filename
, &d
->stats
) < 0) {
276 ERROR("Couldn't stat file '%s' from part '%s'\n",
277 d
->filename
, d
->partition_name
);
281 if (d
->stats
.st_size
== 0) {
282 ERROR("File '%s' from part '%s' is empty!\n",
283 d
->filename
, d
->partition_name
);
287 if (d
->stats
.st_size
> d
->partition_length
) {
288 ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
289 d
->filename
, i
, d
->partition_length
,
290 d
->stats
.st_size
- d
->partition_length
);
294 if (d
->partition_offset
< offset
)
295 d
->partition_offset
= offset
;
297 if (zero_part_baseaddr
) {
298 d
->partition_baseaddr
= 0;
300 d
->partition_baseaddr
=
301 im
.flash_baseaddr
+ d
->partition_offset
;
303 offset
+= d
->partition_length
;
309 static int build_image(void)
317 /* build in-memory buffer */
318 mem_size
= sizeof(header_t
) + sizeof(signature_t
);
319 for (i
= 0; i
< im
.part_count
; ++i
) {
320 part_data_t
* d
= &im
.parts
[i
];
321 mem_size
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
324 mem
= (char*)calloc(mem_size
, 1);
326 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size
);
331 write_header(mem
, im
.version
);
332 ptr
= mem
+ sizeof(header_t
);
334 /* write all parts */
335 for (i
= 0; i
< im
.part_count
; ++i
) {
336 part_data_t
* d
= &im
.parts
[i
];
338 if ((rc
= write_part(ptr
, d
)) != 0) {
339 ERROR("ERROR: failed writing part %u '%s'\n", i
, d
->partition_name
);
342 ptr
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
346 /* write signature */
347 write_signature(mem
, mem_size
- sizeof(signature_t
));
349 /* write in-memory buffer into file */
350 if ((f
= fopen(im
.outputfile
, "w")) == NULL
) {
351 ERROR("Can not create output file: '%s'\n", im
.outputfile
);
356 if (fwrite(mem
, mem_size
, 1, f
) != 1) {
357 ERROR("Could not write %d bytes into file: '%s'\n",
358 mem_size
, im
.outputfile
);
369 int main(int argc
, char* argv
[])
373 memset(&im
, 0, sizeof(im
));
375 strcpy(im
.outputfile
, DEFAULT_OUTPUT_FILE
);
376 strcpy(im
.version
, DEFAULT_VERSION
);
377 memcpy(im
.magic
, MAGIC_HEADER
, MAGIC_LENGTH
);
378 im
.flash_baseaddr
= DEFAULT_FLASH_BASE
;
380 while ((o
= getopt(argc
, argv
, "f:hm:o:p:v:z")) != -1)
385 if (str2u32(optarg
, &im
.flash_baseaddr
)) {
386 ERROR("Invalid flash start address %s\n", optarg
);
395 if (strlen(optarg
) != MAGIC_LENGTH
) {
396 ERROR("Invalid magic %s\n", optarg
);
400 memcpy(im
.magic
, optarg
, MAGIC_LENGTH
);
405 strncpy(im
.outputfile
, optarg
, sizeof(im
.outputfile
));
409 if (image_layout_add_partition(optarg
))
415 strncpy(im
.version
, optarg
, sizeof(im
.version
));
418 zero_part_baseaddr
= 1;
423 rc
= image_layout_verify();
425 ERROR("Failed validating firmware layout - error code: %d\n",
434 ERROR("Failed building image file '%s' - error code: %d\n",