mkfwimage.c 11 KB

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