1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 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> 6 */ 7 8 #include <sys/types.h> 9 #include <sys/stat.h> 10 #include <fcntl.h> 11 #include <unistd.h> 12 #include <string.h> 13 #include <errno.h> 14 #include <zlib.h> 15 #include <sys/mman.h> 16 #include <netinet/in.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <limits.h> 20 #include "fw.h" 21 22 #undef VERSION 23 #define VERSION "1.2-OpenWrt.1" 24 25 #define MAX_SECTIONS 8 26 #define DEFAULT_OUTPUT_FILE "firmware-image.bin" 27 #define DEFAULT_VERSION "UNKNOWN" 28 #define DEFAULT_FLASH_BASE (0xbfc00000) 29 30 #define FIRMWARE_MAX_LENGTH (0x390000) 31 32 typedef struct part_data { 33 char partition_name[64]; 34 int partition_index; 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; 40 41 char filename[PATH_MAX]; 42 struct stat stats; 43 } part_data_t; 44 45 typedef struct image_info { 46 char version[256]; 47 char outputfile[PATH_MAX]; 48 char magic[MAGIC_LENGTH]; 49 u_int32_t flash_baseaddr; 50 u_int32_t part_count; 51 part_data_t parts[MAX_SECTIONS]; 52 } image_info_t; 53 54 static image_info_t im; 55 static int zero_part_baseaddr = 0; 56 57 static void write_header(void* mem, const char* version) 58 { 59 header_t* header = mem; 60 memset(header, 0, sizeof(header_t)); 61 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))); 66 header->pad = 0L; 67 } 68 69 static void write_signature(void* mem, u_int32_t sig_offset) 70 { 71 /* write signature */ 72 signature_t* sign = (signature_t*)(mem + sig_offset); 73 memset(sign, 0, sizeof(signature_t)); 74 75 memcpy(sign->magic, MAGIC_END, MAGIC_LENGTH); 76 sign->crc = htonl(crc32(0L,(unsigned char *)mem, sig_offset)); 77 sign->pad = 0L; 78 } 79 80 static int write_part(void* mem, part_data_t* d) 81 { 82 char* addr; 83 int fd; 84 part_t* p = mem; 85 part_crc_t* crc = mem + sizeof(part_t) + d->stats.st_size; 86 87 fd = open(d->filename, O_RDONLY); 88 if (fd < 0) { 89 ERROR("Failed opening file '%s'\n", d->filename); 90 return -1; 91 } 92 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); 95 close(fd); 96 return -2; 97 } 98 99 memcpy(mem + sizeof(part_t), addr, d->stats.st_size); 100 munmap(addr, d->stats.st_size); 101 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); 111 112 crc->crc = htonl(crc32(0L, mem, d->stats.st_size + sizeof(part_t))); 113 crc->pad = 0L; 114 115 return 0; 116 } 117 118 static void usage(const char* progname) 119 { 120 INFO("Version %s\n" 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); 132 } 133 134 static void print_image_info(void) 135 { 136 int i; 137 138 INFO("Firmware version : '%s'\n" 139 "Output file : '%s'\n" 140 "Part count : %u\n", 141 im.version, im.outputfile, im.part_count); 142 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", 146 d->partition_name, 147 d->partition_baseaddr, 148 d->partition_length, 149 d->partition_entryaddr, 150 d->partition_memaddr, 151 d->stats.st_size, 152 d->partition_length - d->stats.st_size); 153 } 154 } 155 156 static int filelength(const char* file) 157 { 158 FILE *p; 159 int ret = -1; 160 161 if ( (p = fopen(file, "rb") ) == NULL) return (-1); 162 163 fseek(p, 0, SEEK_END); 164 ret = ftell(p); 165 166 fclose (p); 167 168 return (ret); 169 } 170 171 int str2u32(char *arg, u_int32_t *val) 172 { 173 char *err = NULL; 174 uint32_t t; 175 176 errno = 0; 177 t = strtoul(arg, &err, 0); 178 if (errno || (err == arg) || ((err != NULL) && *err)) { 179 return -1; 180 } 181 182 *val = t; 183 return 0; 184 } 185 186 #ifndef STRINGIFY 187 #define STRINGIFY2(X) #X 188 #define STRINGIFY(X) STRINGIFY2(X) 189 #endif 190 static int image_layout_add_partition(const char *part_desc) 191 { 192 part_data_t *d; 193 char memaddr[16]; 194 char entryaddr[16]; 195 char offset[16]; 196 char length[16]; 197 int t; 198 199 if (im.part_count >= MAX_SECTIONS) { 200 ERROR("Too many partitions specified\n"); 201 return (-1); 202 } 203 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", 206 d->partition_name, 207 offset, 208 length, 209 memaddr, 210 entryaddr, 211 d->filename); 212 213 if (t != 6) { 214 ERROR("Bad partition parameter %d, '%s'\n", t, part_desc); 215 return (-1); 216 } 217 218 if (strlen(d->partition_name) == 0) { 219 ERROR("No partition name specified in '%s'\n", part_desc); 220 return (-1); 221 } 222 223 if (str2u32(offset, &d->partition_offset)) { 224 ERROR("Bad offset value '%s'\n", offset); 225 return (-1); 226 } 227 228 if (str2u32(length, &d->partition_length)) { 229 ERROR("Bad length value '%s'\n", length); 230 return (-1); 231 } 232 233 if (d->partition_length == 0) { 234 int flen; 235 flen = filelength(d->filename); 236 if (flen < 0) { 237 ERROR("Unable to determine size of '%s'\n", 238 d->filename); 239 return (-1); 240 } 241 d->partition_length = flen; 242 } 243 244 if (str2u32(memaddr, &d->partition_memaddr)) { 245 ERROR("Bad memaddr vaule '%s'\n", memaddr); 246 return (-1); 247 } 248 249 if (str2u32(entryaddr, &d->partition_entryaddr)) { 250 ERROR("Bad entry address value '%s'\n", entryaddr); 251 return (-1); 252 } 253 254 im.part_count++; 255 d->partition_index = im.part_count; 256 257 return 0; 258 } 259 260 static int image_layout_verify(void) 261 { 262 u_int32_t offset; 263 int i; 264 265 if (im.part_count == 0) { 266 ERROR("No partitions specified\n"); 267 return -1; 268 } 269 270 offset = im.parts[0].partition_offset; 271 for (i = 0; i < im.part_count; i++) 272 { 273 part_data_t* d = &im.parts[i]; 274 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); 278 return -2; 279 } 280 281 if (d->stats.st_size == 0) { 282 ERROR("File '%s' from part '%s' is empty!\n", 283 d->filename, d->partition_name); 284 return -3; 285 } 286 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); 291 return -4; 292 } 293 294 if (d->partition_offset < offset) 295 d->partition_offset = offset; 296 297 if (zero_part_baseaddr) { 298 d->partition_baseaddr = 0; 299 } else { 300 d->partition_baseaddr = 301 im.flash_baseaddr + d->partition_offset; 302 } 303 offset += d->partition_length; 304 } 305 306 return 0; 307 } 308 309 static int build_image(void) 310 { 311 char* mem; 312 char* ptr; 313 u_int32_t mem_size; 314 FILE* f; 315 int i; 316 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); 322 } 323 324 mem = (char*)calloc(mem_size, 1); 325 if (mem == NULL) { 326 ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size); 327 return -1; 328 } 329 330 /* write header */ 331 write_header(mem, im.version); 332 ptr = mem + sizeof(header_t); 333 334 /* write all parts */ 335 for (i = 0; i < im.part_count; ++i) { 336 part_data_t* d = &im.parts[i]; 337 int rc; 338 if ((rc = write_part(ptr, d)) != 0) { 339 ERROR("ERROR: failed writing part %u '%s'\n", i, d->partition_name); 340 return -1; 341 } 342 ptr += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t); 343 } 344 345 346 /* write signature */ 347 write_signature(mem, mem_size - sizeof(signature_t)); 348 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); 352 free(mem); 353 return -10; 354 } 355 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); 359 free(mem); 360 fclose(f); 361 return -11; 362 } 363 364 free(mem); 365 fclose(f); 366 return 0; 367 } 368 369 int main(int argc, char* argv[]) 370 { 371 int o, rc; 372 373 memset(&im, 0, sizeof(im)); 374 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; 379 380 while ((o = getopt(argc, argv, "f:hm:o:p:v:z")) != -1) 381 { 382 switch (o) { 383 case 'f': 384 if (optarg) 385 if (str2u32(optarg, &im.flash_baseaddr)) { 386 ERROR("Invalid flash start address %s\n", optarg); 387 return -1; 388 } 389 break; 390 case 'h': 391 usage(argv[0]); 392 return -1; 393 case 'm': 394 if (optarg) { 395 if (strlen(optarg) != MAGIC_LENGTH) { 396 ERROR("Invalid magic %s\n", optarg); 397 return -1; 398 } 399 400 memcpy(im.magic, optarg, MAGIC_LENGTH); 401 } 402 break; 403 case 'o': 404 if (optarg) 405 strncpy(im.outputfile, optarg, sizeof(im.outputfile)); 406 break; 407 case 'p': 408 if (optarg) { 409 if (image_layout_add_partition(optarg)) 410 return -1; 411 } 412 break; 413 case 'v': 414 if (optarg) 415 strncpy(im.version, optarg, sizeof(im.version)); 416 break; 417 case 'z': 418 zero_part_baseaddr = 1; 419 break; 420 } 421 } 422 423 rc = image_layout_verify(); 424 if (rc) { 425 ERROR("Failed validating firmware layout - error code: %d\n", 426 rc); 427 return -4; 428 } 429 430 print_image_info(); 431 432 rc = build_image(); 433 if (rc) { 434 ERROR("Failed building image file '%s' - error code: %d\n", 435 im.outputfile, rc); 436 return -5; 437 } 438 439 return 0; 440 } 441
This page was automatically generated by LXR 0.3.1. • OpenWrt