mkfwimage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <zlib.h>
  26. #include <sys/mman.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <limits.h>
  31. #include "fw.h"
  32. typedef struct fw_layout_data {
  33. char name[PATH_MAX];
  34. u_int32_t kern_start;
  35. u_int32_t kern_entry;
  36. u_int32_t firmware_max_length;
  37. } fw_layout_t;
  38. fw_layout_t fw_layout_data[] = {
  39. {
  40. .name = "XS2",
  41. .kern_start = 0xbfc30000,
  42. .kern_entry = 0x80041000,
  43. .firmware_max_length= 0x00390000,
  44. },
  45. {
  46. .name = "XS5",
  47. .kern_start = 0xbe030000,
  48. .kern_entry = 0x80041000,
  49. .firmware_max_length= 0x00390000,
  50. },
  51. {
  52. .name = "RS",
  53. .kern_start = 0xbf030000,
  54. .kern_entry = 0x80060000,
  55. .firmware_max_length= 0x00B00000,
  56. },
  57. {
  58. .name = "RSPRO",
  59. .kern_start = 0xbf030000,
  60. .kern_entry = 0x80060000,
  61. .firmware_max_length= 0x00F00000,
  62. },
  63. {
  64. .name = "LS-SR71",
  65. .kern_start = 0xbf030000,
  66. .kern_entry = 0x80060000,
  67. .firmware_max_length= 0x00640000,
  68. },
  69. {
  70. .name = "XS2-8",
  71. .kern_start = 0xa8030000,
  72. .kern_entry = 0x80041000,
  73. .firmware_max_length= 0x006C0000,
  74. },
  75. {
  76. .name = "XM",
  77. .kern_start = 0x9f050000,
  78. .kern_entry = 0x80002000,
  79. .firmware_max_length= 0x006A0000,
  80. },
  81. { .name = "",
  82. },
  83. };
  84. typedef struct part_data {
  85. char partition_name[64];
  86. int partition_index;
  87. u_int32_t partition_baseaddr;
  88. u_int32_t partition_startaddr;
  89. u_int32_t partition_memaddr;
  90. u_int32_t partition_entryaddr;
  91. u_int32_t partition_length;
  92. char filename[PATH_MAX];
  93. struct stat stats;
  94. } part_data_t;
  95. #define MAX_SECTIONS 8
  96. #define DEFAULT_OUTPUT_FILE "firmware-image.bin"
  97. #define DEFAULT_VERSION "UNKNOWN"
  98. #define OPTIONS "B:hv:m:o:r:k:"
  99. static int debug = 0;
  100. typedef struct image_info {
  101. char magic[16];
  102. char version[256];
  103. char outputfile[PATH_MAX];
  104. u_int32_t part_count;
  105. part_data_t parts[MAX_SECTIONS];
  106. } image_info_t;
  107. static void write_header(void* mem, const char *magic, const char* version)
  108. {
  109. header_t* header = mem;
  110. memset(header, 0, sizeof(header_t));
  111. memcpy(header->magic, magic, MAGIC_LENGTH);
  112. strncpy(header->version, version, sizeof(header->version));
  113. header->crc = htonl(crc32(0L, (unsigned char *)header,
  114. sizeof(header_t) - 2 * sizeof(u_int32_t)));
  115. header->pad = 0L;
  116. }
  117. static void write_signature(void* mem, u_int32_t sig_offset)
  118. {
  119. /* write signature */
  120. signature_t* sign = (signature_t*)(mem + sig_offset);
  121. memset(sign, 0, sizeof(signature_t));
  122. memcpy(sign->magic, MAGIC_END, MAGIC_LENGTH);
  123. sign->crc = htonl(crc32(0L,(unsigned char *)mem, sig_offset));
  124. sign->pad = 0L;
  125. }
  126. static int write_part(void* mem, part_data_t* d)
  127. {
  128. char* addr;
  129. int fd;
  130. part_t* p = mem;
  131. part_crc_t* crc = mem + sizeof(part_t) + d->stats.st_size;
  132. fd = open(d->filename, O_RDONLY);
  133. if (fd < 0)
  134. {
  135. ERROR("Failed opening file '%s'\n", d->filename);
  136. return -1;
  137. }
  138. if ((addr=(char*)mmap(0, d->stats.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
  139. {
  140. ERROR("Failed mmaping memory for file '%s'\n", d->filename);
  141. close(fd);
  142. return -2;
  143. }
  144. memcpy(mem + sizeof(part_t), addr, d->stats.st_size);
  145. munmap(addr, d->stats.st_size);
  146. memset(p->name, 0, sizeof(p->name));
  147. strncpy(p->magic, MAGIC_PART, MAGIC_LENGTH);
  148. strncpy(p->name, d->partition_name, sizeof(p->name));
  149. p->index = htonl(d->partition_index);
  150. p->data_size = htonl(d->stats.st_size);
  151. p->part_size = htonl(d->partition_length);
  152. p->baseaddr = htonl(d->partition_baseaddr);
  153. p->memaddr = htonl(d->partition_memaddr);
  154. p->entryaddr = htonl(d->partition_entryaddr);
  155. crc->crc = htonl(crc32(0L, mem, d->stats.st_size + sizeof(part_t)));
  156. crc->pad = 0L;
  157. return 0;
  158. }
  159. static void usage(const char* progname)
  160. {
  161. INFO("Version %s\n"
  162. "Usage: %s [options]\n"
  163. "\t-v <version string>\t - firmware version information, default: %s\n"
  164. "\t-o <output file>\t - firmware output file, default: %s\n"
  165. "\t-m <magic>\t - firmware magic, default: %s\n"
  166. "\t-k <kernel file>\t\t - kernel file\n"
  167. "\t-r <rootfs file>\t\t - rootfs file\n"
  168. "\t-B <board name>\t\t - choose firmware layout for specified board (XS2, XS5, RS, XM)\n"
  169. "\t-h\t\t\t - this help\n", VERSION,
  170. progname, DEFAULT_VERSION, DEFAULT_OUTPUT_FILE, MAGIC_HEADER);
  171. }
  172. static void print_image_info(const image_info_t* im)
  173. {
  174. int i = 0;
  175. INFO("Firmware version: '%s'\n"
  176. "Output file: '%s'\n"
  177. "Part count: %u\n",
  178. im->version, im->outputfile,
  179. im->part_count);
  180. for (i = 0; i < im->part_count; ++i)
  181. {
  182. const part_data_t* d = &im->parts[i];
  183. INFO(" %10s: %8ld bytes (free: %8ld)\n",
  184. d->partition_name,
  185. d->stats.st_size,
  186. d->partition_length - d->stats.st_size);
  187. }
  188. }
  189. static u_int32_t filelength(const char* file)
  190. {
  191. FILE *p;
  192. int ret = -1;
  193. if ( (p = fopen(file, "rb") ) == NULL) return (-1);
  194. fseek(p, 0, SEEK_END);
  195. ret = ftell(p);
  196. fclose (p);
  197. return (ret);
  198. }
  199. static int create_image_layout(const char* kernelfile, const char* rootfsfile, char* board_name, image_info_t* im)
  200. {
  201. part_data_t* kernel = &im->parts[0];
  202. part_data_t* rootfs = &im->parts[1];
  203. fw_layout_t* p;
  204. p = &fw_layout_data[0];
  205. while ((strlen(p->name) != 0) && (strncmp(p->name, board_name, sizeof(board_name)) != 0))
  206. p++;
  207. if (p->name == NULL) {
  208. printf("BUG! Unable to find default fw layout!\n");
  209. exit(-1);
  210. }
  211. printf("board = %s\n", p->name);
  212. strcpy(kernel->partition_name, "kernel");
  213. kernel->partition_index = 1;
  214. kernel->partition_baseaddr = p->kern_start;
  215. if ( (kernel->partition_length = filelength(kernelfile)) < 0) return (-1);
  216. kernel->partition_memaddr = p->kern_entry;
  217. kernel->partition_entryaddr = p->kern_entry;
  218. strncpy(kernel->filename, kernelfile, sizeof(kernel->filename));
  219. if (filelength(rootfsfile) + kernel->partition_length > p->firmware_max_length)
  220. return (-2);
  221. strcpy(rootfs->partition_name, "rootfs");
  222. rootfs->partition_index = 2;
  223. rootfs->partition_baseaddr = kernel->partition_baseaddr + kernel->partition_length;
  224. rootfs->partition_length = p->firmware_max_length - kernel->partition_length;
  225. rootfs->partition_memaddr = 0x00000000;
  226. rootfs->partition_entryaddr = 0x00000000;
  227. strncpy(rootfs->filename, rootfsfile, sizeof(rootfs->filename));
  228. printf("kernel: %d 0x%08x\n", kernel->partition_length, kernel->partition_baseaddr);
  229. printf("root: %d 0x%08x\n", rootfs->partition_length, rootfs->partition_baseaddr);
  230. im->part_count = 2;
  231. return 0;
  232. }
  233. /**
  234. * Checks the availability and validity of all image components.
  235. * Fills in stats member of the part_data structure.
  236. */
  237. static int validate_image_layout(image_info_t* im)
  238. {
  239. int i;
  240. if (im->part_count == 0 || im->part_count > MAX_SECTIONS)
  241. {
  242. ERROR("Invalid part count '%d'\n", im->part_count);
  243. return -1;
  244. }
  245. for (i = 0; i < im->part_count; ++i)
  246. {
  247. part_data_t* d = &im->parts[i];
  248. int len = strlen(d->partition_name);
  249. if (len == 0 || len > 16)
  250. {
  251. ERROR("Invalid partition name '%s' of the part %d\n",
  252. d->partition_name, i);
  253. return -1;
  254. }
  255. if (stat(d->filename, &d->stats) < 0)
  256. {
  257. ERROR("Couldn't stat file '%s' from part '%s'\n",
  258. d->filename, d->partition_name);
  259. return -2;
  260. }
  261. if (d->stats.st_size == 0)
  262. {
  263. ERROR("File '%s' from part '%s' is empty!\n",
  264. d->filename, d->partition_name);
  265. return -3;
  266. }
  267. if (d->stats.st_size > d->partition_length) {
  268. ERROR("File '%s' too big (%d) - max size: 0x%08X (exceeds %lu bytes)\n",
  269. d->filename, i, d->partition_length,
  270. d->stats.st_size - d->partition_length);
  271. return -4;
  272. }
  273. }
  274. return 0;
  275. }
  276. static int build_image(image_info_t* im)
  277. {
  278. char* mem;
  279. char* ptr;
  280. u_int32_t mem_size;
  281. FILE* f;
  282. int i;
  283. // build in-memory buffer
  284. mem_size = sizeof(header_t) + sizeof(signature_t);
  285. for (i = 0; i < im->part_count; ++i)
  286. {
  287. part_data_t* d = &im->parts[i];
  288. mem_size += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
  289. }
  290. mem = (char*)calloc(mem_size, 1);
  291. if (mem == NULL)
  292. {
  293. ERROR("Cannot allocate memory chunk of size '%u'\n", mem_size);
  294. return -1;
  295. }
  296. // write header
  297. write_header(mem, im->magic, im->version);
  298. ptr = mem + sizeof(header_t);
  299. // write all parts
  300. for (i = 0; i < im->part_count; ++i)
  301. {
  302. part_data_t* d = &im->parts[i];
  303. int rc;
  304. if ((rc = write_part(ptr, d)) != 0)
  305. {
  306. ERROR("ERROR: failed writing part %u '%s'\n", i, d->partition_name);
  307. }
  308. ptr += sizeof(part_t) + d->stats.st_size + sizeof(part_crc_t);
  309. }
  310. // write signature
  311. write_signature(mem, mem_size - sizeof(signature_t));
  312. // write in-memory buffer into file
  313. if ((f = fopen(im->outputfile, "w")) == NULL)
  314. {
  315. ERROR("Can not create output file: '%s'\n", im->outputfile);
  316. return -10;
  317. }
  318. if (fwrite(mem, mem_size, 1, f) != 1)
  319. {
  320. ERROR("Could not write %d bytes into file: '%s'\n",
  321. mem_size, im->outputfile);
  322. return -11;
  323. }
  324. free(mem);
  325. fclose(f);
  326. return 0;
  327. }
  328. int main(int argc, char* argv[])
  329. {
  330. char kernelfile[PATH_MAX];
  331. char rootfsfile[PATH_MAX];
  332. char board_name[PATH_MAX];
  333. int o, rc;
  334. image_info_t im;
  335. memset(&im, 0, sizeof(im));
  336. memset(kernelfile, 0, sizeof(kernelfile));
  337. memset(rootfsfile, 0, sizeof(rootfsfile));
  338. memset(board_name, 0, sizeof(board_name));
  339. strcpy(im.outputfile, DEFAULT_OUTPUT_FILE);
  340. strcpy(im.version, DEFAULT_VERSION);
  341. strncpy(im.magic, MAGIC_HEADER, sizeof(im.magic));
  342. while ((o = getopt(argc, argv, OPTIONS)) != -1)
  343. {
  344. switch (o) {
  345. case 'v':
  346. if (optarg)
  347. strncpy(im.version, optarg, sizeof(im.version));
  348. break;
  349. case 'o':
  350. if (optarg)
  351. strncpy(im.outputfile, optarg, sizeof(im.outputfile));
  352. break;
  353. case 'm':
  354. if (optarg)
  355. strncpy(im.magic, optarg, sizeof(im.magic));
  356. break;
  357. case 'h':
  358. usage(argv[0]);
  359. return -1;
  360. case 'k':
  361. if (optarg)
  362. strncpy(kernelfile, optarg, sizeof(kernelfile));
  363. break;
  364. case 'r':
  365. if (optarg)
  366. strncpy(rootfsfile, optarg, sizeof(rootfsfile));
  367. break;
  368. case 'B':
  369. if (optarg)
  370. strncpy(board_name, optarg, sizeof(board_name));
  371. break;
  372. }
  373. }
  374. if (strlen(board_name) == 0)
  375. strcpy(board_name, "XS2"); /* default to XS2 */
  376. if (strlen(kernelfile) == 0)
  377. {
  378. ERROR("Kernel file is not specified, cannot continue\n");
  379. usage(argv[0]);
  380. return -2;
  381. }
  382. if (strlen(rootfsfile) == 0)
  383. {
  384. ERROR("Root FS file is not specified, cannot continue\n");
  385. usage(argv[0]);
  386. return -2;
  387. }
  388. if ((rc = create_image_layout(kernelfile, rootfsfile, board_name, &im)) != 0)
  389. {
  390. ERROR("Failed creating firmware layout description - error code: %d\n", rc);
  391. return -3;
  392. }
  393. if ((rc = validate_image_layout(&im)) != 0)
  394. {
  395. ERROR("Failed validating firmware layout - error code: %d\n", rc);
  396. return -4;
  397. }
  398. print_image_info(&im);
  399. if ((rc = build_image(&im)) != 0)
  400. {
  401. ERROR("Failed building image file '%s' - error code: %d\n", im.outputfile, rc);
  402. return -5;
  403. }
  404. return 0;
  405. }