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 part_data
{
35 char partition_name
[64];
37 u_int32_t partition_baseaddr
;
38 u_int32_t partition_startaddr
;
39 u_int32_t partition_memaddr
;
40 u_int32_t partition_entryaddr
;
41 u_int32_t partition_length
;
43 char filename
[PATH_MAX
];
47 #define MAX_SECTIONS 8
48 #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
49 #define DEFAULT_VERSION "UNKNOWN"
51 #define OPTIONS "hv:o:r:k:s:"
53 #define FIRMWARE_MAX_LENGTH (0x390000)
54 #define partition_startaddr (0xBFC30000)
57 typedef struct image_info
{
59 char outputfile
[PATH_MAX
];
61 part_data_t parts
[MAX_SECTIONS
];
64 static void write_header(void* mem
, const char* version
)
66 header_t
* header
= mem
;
67 memset(header
, 0, sizeof(header_t
));
69 memcpy(header
->magic
, MAGIC_HEADER
, MAGIC_LENGTH
);
70 strncpy(header
->version
, version
, sizeof(header
->version
));
71 header
->crc
= htonl(crc32(0L, (unsigned char *)header
,
72 sizeof(header_t
) - 2 * sizeof(u_int32_t
)));
77 static void write_signature(void* mem
, u_int32_t sig_offset
)
80 signature_t
* sign
= (signature_t
*)(mem
+ sig_offset
);
81 memset(sign
, 0, sizeof(signature_t
));
83 memcpy(sign
->magic
, MAGIC_END
, MAGIC_LENGTH
);
84 sign
->crc
= htonl(crc32(0L,(unsigned char *)mem
, sig_offset
));
88 static int write_part(void* mem
, part_data_t
* d
)
93 part_crc_t
* crc
= mem
+ sizeof(part_t
) + d
->stats
.st_size
;
95 fd
= open(d
->filename
, O_RDONLY
);
98 ERROR("Failed opening file '%s'\n", d
->filename
);
102 if ((addr
=(char*)mmap(0, d
->stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
104 ERROR("Failed mmaping memory for file '%s'\n", d
->filename
);
109 memcpy(mem
+ sizeof(part_t
), addr
, d
->stats
.st_size
);
110 munmap(addr
, d
->stats
.st_size
);
112 memset(p
->name
, 0, sizeof(p
->name
));
113 strncpy(p
->magic
, MAGIC_PART
, MAGIC_LENGTH
);
114 strncpy(p
->name
, d
->partition_name
, sizeof(p
->name
));
115 p
->index
= htonl(d
->partition_index
);
116 p
->data_size
= htonl(d
->stats
.st_size
);
117 p
->part_size
= htonl(d
->partition_length
);
118 p
->baseaddr
= htonl(d
->partition_baseaddr
);
119 p
->memaddr
= htonl(d
->partition_memaddr
);
120 p
->entryaddr
= htonl(d
->partition_entryaddr
);
122 crc
->crc
= htonl(crc32(0L, mem
, d
->stats
.st_size
+ sizeof(part_t
)));
128 static void usage(const char* progname
)
131 "Usage: %s [options]\n"
132 "\t-v <version string>\t - firmware version information, default: %s\n"
133 "\t-o <output file>\t - firmware output file, default: %s\n"
134 "\t-k <kernel file>\t\t - kernel file\n"
135 "\t-r <rootfs file>\t\t - rootfs file\n"
136 "\t-h\t\t\t - this help\n", VERSION
,
137 progname
, DEFAULT_VERSION
, DEFAULT_OUTPUT_FILE
);
140 static void print_image_info(const image_info_t
* im
)
143 INFO("Firmware version: '%s'\n"
144 "Output file: '%s'\n"
146 im
->version
, im
->outputfile
,
149 for (i
= 0; i
< im
->part_count
; ++i
)
151 const part_data_t
* d
= &im
->parts
[i
];
152 INFO(" %10s: %8ld bytes (free: %8ld)\n",
155 d
->partition_length
- d
->stats
.st_size
);
161 static u_int32_t
filelength(const char* file
)
166 if ( (p
= fopen(file
, "rb") ) == NULL
) return (-1);
168 fseek(p
, 0, SEEK_END
);
176 static int create_image_layout(const char* kernelfile
, const char* rootfsfile
, image_info_t
* im
)
178 part_data_t
* kernel
= &im
->parts
[0];
179 part_data_t
* rootfs
= &im
->parts
[1];
181 strcpy(kernel
->partition_name
, "kernel");
182 kernel
->partition_index
= 1;
183 kernel
->partition_baseaddr
= partition_startaddr
;
184 if ( (kernel
->partition_length
= filelength(kernelfile
)) < 0) return (-1);
185 kernel
->partition_memaddr
= 0x80041000;
186 kernel
->partition_entryaddr
= 0x80041000;
187 strncpy(kernel
->filename
, kernelfile
, sizeof(kernel
->filename
));
189 if (filelength(rootfsfile
) + kernel
->partition_length
> FIRMWARE_MAX_LENGTH
)
192 strcpy(rootfs
->partition_name
, "rootfs");
193 rootfs
->partition_index
= 2;
194 rootfs
->partition_baseaddr
= partition_startaddr
+ kernel
->partition_length
;
195 rootfs
->partition_length
= FIRMWARE_MAX_LENGTH
- kernel
->partition_length
;
196 rootfs
->partition_memaddr
= 0x00000000;
197 rootfs
->partition_entryaddr
= 0x00000000;
198 strncpy(rootfs
->filename
, rootfsfile
, sizeof(rootfs
->filename
));
206 * Checks the availability and validity of all image components.
207 * Fills in stats member of the part_data structure.
209 static int validate_image_layout(image_info_t
* im
)
213 if (im
->part_count
== 0 || im
->part_count
> MAX_SECTIONS
)
215 ERROR("Invalid part count '%d'\n", im
->part_count
);
219 for (i
= 0; i
< im
->part_count
; ++i
)
221 part_data_t
* d
= &im
->parts
[i
];
222 int len
= strlen(d
->partition_name
);
223 if (len
== 0 || len
> 16)
225 ERROR("Invalid partition name '%s' of the part %d\n",
226 d
->partition_name
, i
);
229 if (stat(d
->filename
, &d
->stats
) < 0)
231 ERROR("Couldn't stat file '%s' from part '%s'\n",
232 d
->filename
, d
->partition_name
);
235 if (d
->stats
.st_size
== 0)
237 ERROR("File '%s' from part '%s' is empty!\n",
238 d
->filename
, d
->partition_name
);
241 if (d
->stats
.st_size
> d
->partition_length
) {
242 ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
243 d
->filename
, i
, d
->partition_length
,
244 d
->stats
.st_size
- d
->partition_length
);
252 static int build_image(image_info_t
* im
)
260 // build in-memory buffer
261 mem_size
= sizeof(header_t
) + sizeof(signature_t
);
262 for (i
= 0; i
< im
->part_count
; ++i
)
264 part_data_t
* d
= &im
->parts
[i
];
265 mem_size
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
268 mem
= (char*)calloc(mem_size
, 1);
271 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size
);
276 write_header(mem
, im
->version
);
277 ptr
= mem
+ sizeof(header_t
);
279 for (i
= 0; i
< im
->part_count
; ++i
)
281 part_data_t
* d
= &im
->parts
[i
];
283 if ((rc
= write_part(ptr
, d
)) != 0)
285 ERROR("ERROR: failed writing part %u '%s'\n", i
, d
->partition_name
);
287 ptr
+= sizeof(part_t
) + d
->stats
.st_size
+ sizeof(part_crc_t
);
290 write_signature(mem
, mem_size
- sizeof(signature_t
));
292 // write in-memory buffer into file
293 if ((f
= fopen(im
->outputfile
, "w")) == NULL
)
295 ERROR("Can not create output file: '%s'\n", im
->outputfile
);
299 if (fwrite(mem
, mem_size
, 1, f
) != 1)
301 ERROR("Could not write %d bytes into file: '%s'\n",
302 mem_size
, im
->outputfile
);
312 int main(int argc
, char* argv
[])
314 char kernelfile
[PATH_MAX
];
315 char rootfsfile
[PATH_MAX
];
319 memset(&im
, 0, sizeof(im
));
320 memset(kernelfile
, 0, sizeof(kernelfile
));
321 memset(rootfsfile
, 0, sizeof(rootfsfile
));
323 strcpy(im
.outputfile
, DEFAULT_OUTPUT_FILE
);
324 strcpy(im
.version
, DEFAULT_VERSION
);
326 while ((o
= getopt(argc
, argv
, OPTIONS
)) != -1)
331 strncpy(im
.version
, optarg
, sizeof(im
.version
));
335 strncpy(im
.outputfile
, optarg
, sizeof(im
.outputfile
));
342 strncpy(kernelfile
, optarg
, sizeof(kernelfile
));
346 strncpy(rootfsfile
, optarg
, sizeof(rootfsfile
));
350 #undef partition_startaddr
351 #define partition_startaddr (optarg)
356 if (strlen(kernelfile
) == 0)
358 ERROR("Kernel file is not specified, cannot continue\n");
363 if (strlen(rootfsfile
) == 0)
365 ERROR("Root FS file is not specified, cannot continue\n");
370 if ((rc
= create_image_layout(kernelfile
, rootfsfile
, &im
)) != 0)
372 ERROR("Failed creating firmware layout description - error code: %d\n", rc
);
376 if ((rc
= validate_image_layout(&im
)) != 0)
378 ERROR("Failed validating firmware layout - error code: %d\n", rc
);
382 print_image_info(&im
);
384 if ((rc
= build_image(&im
)) != 0)
386 ERROR("Failed building image file '%s' - error code: %d\n", im
.outputfile
, rc
);