mkfwimage.c 11 KB

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