mkfwimage2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright (C) 2007 Ubiquiti Networks, Inc.
  3. * Copyright (C) 2008 Lukas Kuna <ValXdater@seznam.cz>
  4. * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  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. #undef VERSION
  34. #define VERSION "1.2-OpenWrt.1"
  35. #define MAX_SECTIONS 8
  36. #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
  37. #define DEFAULT_VERSION "UNKNOWN"
  38. #define DEFAULT_FLASH_BASE (0xbfc00000)
  39. #define FIRMWARE_MAX_LENGTH (0x390000)
  40. typedef struct part_data {
  41. char partition_name[64];
  42. int partition_index;
  43. u_int32_t partition_baseaddr;
  44. u_int32_t partition_offset;
  45. u_int32_t partition_memaddr;
  46. u_int32_t partition_entryaddr;
  47. u_int32_t partition_length;
  48. char filename[PATH_MAX];
  49. struct stat stats;
  50. } part_data_t;
  51. typedef struct image_info {
  52. char version[256];
  53. char outputfile[PATH_MAX];
  54. char magic[MAGIC_LENGTH];
  55. u_int32_t flash_baseaddr;
  56. u_int32_t part_count;
  57. part_data_t parts[MAX_SECTIONS];
  58. } image_info_t;
  59. static image_info_t im;
  60. static int debug = 0;
  61. static int zero_part_baseaddr = 0;
  62. static void write_header(void* mem, const char* version)
  63. {
  64. header_t* header = mem;
  65. memset(header, 0, sizeof(header_t));
  66. memcpy(header->magic, im.magic, 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)));
  70. header->pad = 0L;
  71. }
  72. static void write_signature(void* mem, u_int32_t sig_offset)
  73. {
  74. /* write signature */
  75. signature_t* sign = (signature_t*)(mem + sig_offset);
  76. memset(sign, 0, sizeof(signature_t));
  77. memcpy(sign->magic, MAGIC_END, MAGIC_LENGTH);
  78. sign->crc = htonl(crc32(0L,(unsigned char *)mem, sig_offset));
  79. sign->pad = 0L;
  80. }
  81. static int write_part(void* mem, part_data_t* d)
  82. {
  83. char* addr;
  84. int fd;
  85. part_t* p = mem;
  86. part_crc_t* crc = mem + sizeof(part_t) + d->stats.st_size;
  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. if ((addr=(char*)mmap(0, d->stats.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
  93. ERROR("Failed mmaping memory for file '%s'\n", d->filename);
  94. close(fd);
  95. return -2;
  96. }
  97. memcpy(mem + sizeof(part_t), addr, d->stats.st_size);
  98. munmap(addr, d->stats.st_size);
  99. memset(p->name, 0, sizeof(p->name));
  100. strncpy(p->magic, MAGIC_PART, MAGIC_LENGTH);
  101. strncpy(p->name, d->partition_name, sizeof(p->name));
  102. p->index = htonl(d->partition_index);
  103. p->data_size = htonl(d->stats.st_size);
  104. p->part_size = htonl(d->partition_length);
  105. p->baseaddr = htonl(d->partition_baseaddr);
  106. p->memaddr = htonl(d->partition_memaddr);
  107. p->entryaddr = htonl(d->partition_entryaddr);
  108. crc->crc = htonl(crc32(0L, mem, d->stats.st_size + sizeof(part_t)));
  109. crc->pad = 0L;
  110. return 0;
  111. }
  112. static void usage(const char* progname)
  113. {
  114. INFO("Version %s\n"
  115. "Usage: %s [options]\n"
  116. "\t-v <version string>\t - firmware version information, default: %s\n"
  117. "\t-m <magic>\t\t - firmware magic, default: %s\n"
  118. "\t-f <flash base>\t\t - flash base address, default: 0x%08x\n"
  119. "\t-o <output file>\t - firmware output file, default: %s\n"
  120. "\t-p <name>:<offset>:<len>:<memaddr>:<entry>:<file>\n "
  121. "\t\t\t\t - create a partition from <file>\n"
  122. "\t-z\t\t\t - set partition offsets to zero\n"
  123. "\t-h\t\t\t - this help\n",
  124. VERSION, progname, DEFAULT_VERSION, MAGIC_HEADER,
  125. DEFAULT_FLASH_BASE, DEFAULT_OUTPUT_FILE);
  126. }
  127. static void print_image_info(void)
  128. {
  129. int i;
  130. INFO("Firmware version : '%s'\n"
  131. "Output file : '%s'\n"
  132. "Part count : %u\n",
  133. im.version, im.outputfile, im.part_count);
  134. for (i = 0; i < im.part_count; ++i) {
  135. const part_data_t* d = &im.parts[i];
  136. INFO(" %10s: %08x %08x %08x %08x %8ld bytes (free: %8ld)\n",
  137. d->partition_name,
  138. d->partition_baseaddr,
  139. d->partition_length,
  140. d->partition_entryaddr,
  141. d->partition_memaddr,
  142. d->stats.st_size,
  143. d->partition_length - d->stats.st_size);
  144. }
  145. }
  146. static int filelength(const char* file)
  147. {
  148. FILE *p;
  149. int ret = -1;
  150. if ( (p = fopen(file, "rb") ) == NULL) return (-1);
  151. fseek(p, 0, SEEK_END);
  152. ret = ftell(p);
  153. fclose (p);
  154. return (ret);
  155. }
  156. int str2u32(char *arg, u_int32_t *val)
  157. {
  158. char *err = NULL;
  159. uint32_t t;
  160. errno = 0;
  161. t = strtoul(arg, &err, 0);
  162. if (errno || (err == arg) || ((err != NULL) && *err)) {
  163. return -1;
  164. }
  165. *val = t;
  166. return 0;
  167. }
  168. static int image_layout_add_partition(const char *part_desc)
  169. {
  170. part_data_t *d;
  171. char memaddr[16];
  172. char entryaddr[16];
  173. char offset[16];
  174. char length[16];
  175. int t;
  176. if (im.part_count >= MAX_SECTIONS) {
  177. ERROR("Too many partitions specified\n");
  178. return (-1);
  179. }
  180. d = &im.parts[im.part_count];
  181. 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]:%256s",
  182. d->partition_name,
  183. offset,
  184. length,
  185. memaddr,
  186. entryaddr,
  187. d->filename);
  188. if (t != 6) {
  189. ERROR("Bad partition parameter %d, '%s'\n", t, part_desc);
  190. return (-1);
  191. }
  192. if (strlen(d->partition_name) == 0) {
  193. ERROR("No partition name specified in '%s'\n", part_desc);
  194. return (-1);
  195. }
  196. if (str2u32(offset, &d->partition_offset)) {
  197. ERROR("Bad offset value '%s'\n", offset);
  198. return (-1);
  199. }
  200. if (str2u32(length, &d->partition_length)) {
  201. ERROR("Bad length value '%s'\n", length);
  202. return (-1);
  203. }
  204. if (d->partition_length == 0) {
  205. int flen;
  206. flen = filelength(d->filename);
  207. if (flen < 0) {
  208. ERROR("Unable to determine size of '%s'\n",
  209. d->filename);
  210. return (-1);
  211. }
  212. d->partition_length = flen;
  213. }
  214. if (str2u32(memaddr, &d->partition_memaddr)) {
  215. ERROR("Bad memaddr vaule '%s'\n", memaddr);
  216. return (-1);
  217. }
  218. if (str2u32(entryaddr, &d->partition_entryaddr)) {
  219. ERROR("Bad entry address value '%s'\n", entryaddr);
  220. return (-1);
  221. }
  222. im.part_count++;
  223. d->partition_index = im.part_count;
  224. return 0;
  225. }
  226. static int image_layout_verify(void)
  227. {
  228. u_int32_t offset;
  229. int i;
  230. if (im.part_count == 0) {
  231. ERROR("No partitions specified\n");
  232. return -1;
  233. }
  234. offset = im.parts[0].partition_offset;
  235. for (i = 0; i < im.part_count; i++)
  236. {
  237. part_data_t* d = &im.parts[i];
  238. if (stat(d->filename, &d->stats) < 0) {
  239. ERROR("Couldn't stat file '%s' from part '%s'\n",
  240. d->filename, d->partition_name);
  241. return -2;
  242. }
  243. if (d->stats.st_size == 0) {
  244. ERROR("File '%s' from part '%s' is empty!\n",
  245. d->filename, d->partition_name);
  246. return -3;
  247. }
  248. if (d->stats.st_size > d->partition_length) {
  249. ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
  250. d->filename, i, d->partition_length,
  251. d->stats.st_size - d->partition_length);
  252. return -4;
  253. }
  254. if (d->partition_offset < offset)
  255. d->partition_offset = offset;
  256. if (zero_part_baseaddr) {
  257. d->partition_baseaddr = 0;
  258. } else {
  259. d->partition_baseaddr =
  260. im.flash_baseaddr + d->partition_offset;
  261. }
  262. offset += d->partition_length;
  263. }
  264. return 0;
  265. }
  266. static int build_image(void)
  267. {
  268. char* mem;
  269. char* ptr;
  270. u_int32_t mem_size;
  271. FILE* f;
  272. int i;
  273. /* build in-memory buffer */
  274. mem_size = sizeof(header_t) + sizeof(signature_t);
  275. for (i = 0; i < im.part_count; ++i) {
  276. part_data_t* d = &im.parts[i];
  277. mem_size += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
  278. }
  279. mem = (char*)calloc(mem_size, 1);
  280. if (mem == NULL) {
  281. ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size);
  282. return -1;
  283. }
  284. /* write header */
  285. write_header(mem, im.version);
  286. ptr = mem + sizeof(header_t);
  287. /* write all parts */
  288. for (i = 0; i < im.part_count; ++i) {
  289. part_data_t* d = &im.parts[i];
  290. int rc;
  291. if ((rc = write_part(ptr, d)) != 0) {
  292. ERROR("ERROR: failed writing part %u '%s'\n", i, d->partition_name);
  293. return -1;
  294. }
  295. ptr += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
  296. }
  297. /* write signature */
  298. write_signature(mem, mem_size - sizeof(signature_t));
  299. /* write in-memory buffer into file */
  300. if ((f = fopen(im.outputfile, "w")) == NULL) {
  301. ERROR("Can not create output file: '%s'\n", im.outputfile);
  302. return -10;
  303. }
  304. if (fwrite(mem, mem_size, 1, f) != 1) {
  305. ERROR("Could not write %d bytes into file: '%s'\n",
  306. mem_size, im.outputfile);
  307. return -11;
  308. }
  309. free(mem);
  310. fclose(f);
  311. return 0;
  312. }
  313. int main(int argc, char* argv[])
  314. {
  315. int o, rc;
  316. memset(&im, 0, sizeof(im));
  317. strcpy(im.outputfile, DEFAULT_OUTPUT_FILE);
  318. strcpy(im.version, DEFAULT_VERSION);
  319. memcpy(im.magic, MAGIC_HEADER, MAGIC_LENGTH);
  320. im.flash_baseaddr = DEFAULT_FLASH_BASE;
  321. while ((o = getopt(argc, argv, "f:hm:o:p:v:z")) != -1)
  322. {
  323. switch (o) {
  324. case 'f':
  325. if (optarg)
  326. if (str2u32(optarg, &im.flash_baseaddr)) {
  327. ERROR("Invalid flash start address %s\n", optarg);
  328. return -1;
  329. }
  330. break;
  331. case 'h':
  332. usage(argv[0]);
  333. return -1;
  334. case 'm':
  335. if (optarg) {
  336. if (strlen(optarg) != MAGIC_LENGTH) {
  337. ERROR("Invalid magic %s\n", optarg);
  338. return -1;
  339. }
  340. memcpy(im.magic, optarg, MAGIC_LENGTH);
  341. }
  342. break;
  343. case 'o':
  344. if (optarg)
  345. strncpy(im.outputfile, optarg, sizeof(im.outputfile));
  346. break;
  347. case 'p':
  348. if (optarg) {
  349. if (image_layout_add_partition(optarg))
  350. return -1;
  351. }
  352. break;
  353. case 'v':
  354. if (optarg)
  355. strncpy(im.version, optarg, sizeof(im.version));
  356. break;
  357. case 'z':
  358. zero_part_baseaddr = 1;
  359. break;
  360. }
  361. }
  362. rc = image_layout_verify();
  363. if (rc) {
  364. ERROR("Failed validating firmware layout - error code: %d\n",
  365. rc);
  366. return -4;
  367. }
  368. print_image_info();
  369. rc = build_image();
  370. if (rc) {
  371. ERROR("Failed building image file '%s' - error code: %d\n",
  372. im.outputfile, rc);
  373. return -5;
  374. }
  375. return 0;
  376. }