fix mkfwimage to recognize the -s option
[openwrt.git] / tools / firmware-utils / src / mkfwimage.c
1 /*
2  * Copyright (C) 2007 Ubiquiti Networks, Inc.
3  * Copyright (C) 2008 Lukas Kuna <ValXdater@seznam.cz>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <zlib.h>
27 #include <sys/mman.h>
28 #include <netinet/in.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <limits.h>
32 #include "fw.h"
33
34 typedef struct part_data {
35         char    partition_name[64];
36         int     partition_index;
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;
42
43         char    filename[PATH_MAX];
44         struct stat stats;
45 } part_data_t;
46
47 #define MAX_SECTIONS    8
48 #define DEFAULT_OUTPUT_FILE     "firmware-image.bin"
49 #define DEFAULT_VERSION         "UNKNOWN"
50
51 #define OPTIONS "hv:o:r:k:s:"
52
53 #define FIRMWARE_MAX_LENGTH     (0x390000)
54 #define partition_startaddr     (0xBFC30000)
55 static int debug = 0;
56
57 typedef struct image_info {
58         char version[256];
59         char outputfile[PATH_MAX];
60         u_int32_t       part_count;
61         part_data_t parts[MAX_SECTIONS];
62 } image_info_t;
63
64 static void write_header(void* mem, const char* version)
65 {
66         header_t* header = mem;
67         memset(header, 0, sizeof(header_t));
68
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)));
73         header->pad = 0L;
74 }
75
76
77 static void write_signature(void* mem, u_int32_t sig_offset)
78 {
79         /* write signature */
80         signature_t* sign = (signature_t*)(mem + sig_offset);
81         memset(sign, 0, sizeof(signature_t));
82
83         memcpy(sign->magic, MAGIC_END, MAGIC_LENGTH);
84         sign->crc = htonl(crc32(0L,(unsigned char *)mem, sig_offset));
85         sign->pad = 0L;
86 }
87
88 static int write_part(void* mem, part_data_t* d)
89 {
90         char* addr;
91         int fd;
92         part_t* p = mem;
93         part_crc_t* crc = mem + sizeof(part_t) + d->stats.st_size;
94
95         fd = open(d->filename, O_RDONLY);
96         if (fd < 0)
97         {
98                 ERROR("Failed opening file '%s'\n", d->filename);
99                 return -1;
100         }
101
102         if ((addr=(char*)mmap(0, d->stats.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
103         {
104                 ERROR("Failed mmaping memory for file '%s'\n", d->filename);
105                 close(fd);
106                 return -2;
107         }
108
109         memcpy(mem + sizeof(part_t), addr, d->stats.st_size);
110         munmap(addr, d->stats.st_size);
111
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);
121
122         crc->crc = htonl(crc32(0L, mem, d->stats.st_size + sizeof(part_t)));
123         crc->pad = 0L;
124
125         return 0;
126 }
127
128 static void usage(const char* progname)
129 {
130         INFO("Version %s\n"
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);
138 }
139
140 static void print_image_info(const image_info_t* im)
141 {
142         int i = 0;
143         INFO("Firmware version: '%s'\n"
144              "Output file: '%s'\n"
145              "Part count: %u\n",
146              im->version, im->outputfile,
147              im->part_count);
148
149         for (i = 0; i < im->part_count; ++i)
150         {
151                 const part_data_t* d = &im->parts[i];
152                 INFO(" %10s: %8ld bytes (free: %8ld)\n",
153                      d->partition_name,
154                      d->stats.st_size,
155                      d->partition_length - d->stats.st_size);
156         }
157 }
158
159
160
161 static u_int32_t filelength(const char* file)
162 {
163         FILE *p;
164         int ret = -1;
165
166         if ( (p = fopen(file, "rb") ) == NULL) return (-1);
167
168         fseek(p, 0, SEEK_END);
169         ret = ftell(p);
170
171         fclose (p);
172
173         return (ret);
174 }
175
176 static int create_image_layout(const char* kernelfile, const char* rootfsfile, image_info_t* im)
177 {
178         part_data_t* kernel = &im->parts[0];
179         part_data_t* rootfs = &im->parts[1];
180
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));
188
189         if (filelength(rootfsfile) + kernel->partition_length > FIRMWARE_MAX_LENGTH)
190                 return (-2);
191
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));
199
200         im->part_count = 2;
201
202         return 0;
203 }
204
205 /**
206  * Checks the availability and validity of all image components.
207  * Fills in stats member of the part_data structure.
208  */
209 static int validate_image_layout(image_info_t* im)
210 {
211         int i;
212
213         if (im->part_count == 0 || im->part_count > MAX_SECTIONS)
214         {
215                 ERROR("Invalid part count '%d'\n", im->part_count);
216                 return -1;
217         }
218
219         for (i = 0; i < im->part_count; ++i)
220         {
221                 part_data_t* d = &im->parts[i];
222                 int len = strlen(d->partition_name);
223                 if (len == 0 || len > 16)
224                 {
225                         ERROR("Invalid partition name '%s' of the part %d\n",
226                                         d->partition_name, i);
227                         return -1;
228                 }
229                 if (stat(d->filename, &d->stats) < 0)
230                 {
231                         ERROR("Couldn't stat file '%s' from part '%s'\n",
232                                         d->filename, d->partition_name);
233                         return -2;
234                 }
235                 if (d->stats.st_size == 0)
236                 {
237                         ERROR("File '%s' from part '%s' is empty!\n",
238                                         d->filename, d->partition_name);
239                         return -3;
240                 }
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);
245                         return -4;
246                 }
247         }
248
249         return 0;
250 }
251
252 static int build_image(image_info_t* im)
253 {
254         char* mem;
255         char* ptr;
256         u_int32_t mem_size;
257         FILE* f;
258         int i;
259
260         // build in-memory buffer
261         mem_size = sizeof(header_t) + sizeof(signature_t);
262         for (i = 0; i < im->part_count; ++i)
263         {
264                 part_data_t* d = &im->parts[i];
265                 mem_size += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
266         }
267
268         mem = (char*)calloc(mem_size, 1);
269         if (mem == NULL)
270         {
271                 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size);
272                 return -1;
273         }
274
275         // write header
276         write_header(mem, im->version);
277         ptr = mem + sizeof(header_t);
278         // write all parts
279         for (i = 0; i < im->part_count; ++i)
280         {
281                 part_data_t* d = &im->parts[i];
282                 int rc;
283                 if ((rc = write_part(ptr, d)) != 0)
284                 {
285                         ERROR("ERROR: failed writing part %u '%s'\n", i, d->partition_name);
286                 }
287                 ptr += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
288         }
289         // write signature
290         write_signature(mem, mem_size - sizeof(signature_t));
291
292         // write in-memory buffer into file
293         if ((f = fopen(im->outputfile, "w")) == NULL)
294         {
295                 ERROR("Can not create output file: '%s'\n", im->outputfile);
296                 return -10;
297         }
298
299         if (fwrite(mem, mem_size, 1, f) != 1)
300         {
301                 ERROR("Could not write %d bytes into file: '%s'\n",
302                                 mem_size, im->outputfile);
303                 return -11;
304         }
305
306         free(mem);
307         fclose(f);
308         return 0;
309 }
310
311
312 int main(int argc, char* argv[])
313 {
314         char kernelfile[PATH_MAX];
315         char rootfsfile[PATH_MAX];
316         int o, rc;
317         image_info_t im;
318
319         memset(&im, 0, sizeof(im));
320         memset(kernelfile, 0, sizeof(kernelfile));
321         memset(rootfsfile, 0, sizeof(rootfsfile));
322
323         strcpy(im.outputfile, DEFAULT_OUTPUT_FILE);
324         strcpy(im.version, DEFAULT_VERSION);
325
326         while ((o = getopt(argc, argv, OPTIONS)) != -1)
327         {
328                 switch (o) {
329                 case 'v':
330                         if (optarg)
331                                 strncpy(im.version, optarg, sizeof(im.version));
332                         break;
333                 case 'o':
334                         if (optarg)
335                                 strncpy(im.outputfile, optarg, sizeof(im.outputfile));
336                         break;
337                 case 'h':
338                         usage(argv[0]);
339                         return -1;
340                 case 'k':
341                         if (optarg)
342                                 strncpy(kernelfile, optarg, sizeof(kernelfile));
343                         break;
344                 case 'r':
345                         if (optarg)
346                                 strncpy(rootfsfile, optarg, sizeof(rootfsfile));
347                         break;
348                 case 's':
349                         if (optarg)
350                                 #undef partition_startaddr
351                                 #define partition_startaddr     (optarg)
352                         break;
353                 }
354         }
355
356         if (strlen(kernelfile) == 0)
357         {
358                 ERROR("Kernel file is not specified, cannot continue\n");
359                 usage(argv[0]);
360                 return -2;
361         }
362
363         if (strlen(rootfsfile) == 0)
364         {
365                 ERROR("Root FS file is not specified, cannot continue\n");
366                 usage(argv[0]);
367                 return -2;
368         }
369
370         if ((rc = create_image_layout(kernelfile, rootfsfile, &im)) != 0)
371         {
372                 ERROR("Failed creating firmware layout description - error code: %d\n", rc);
373                 return -3;
374         }
375
376         if ((rc = validate_image_layout(&im)) != 0)
377         {
378                 ERROR("Failed validating firmware layout - error code: %d\n", rc);
379                 return -4;
380         }
381
382         print_image_info(&im);
383
384         if ((rc = build_image(&im)) != 0)
385         {
386                 ERROR("Failed building image file '%s' - error code: %d\n", im.outputfile, rc);
387                 return -5;
388         }
389
390         return 0;
391 }