mkcameofw.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <unistd.h> /* for unlink() */
  14. #include <libgen.h>
  15. #include <getopt.h> /* for getopt() */
  16. #include <stdarg.h>
  17. #include <errno.h>
  18. #include <sys/stat.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #define MAX_MODEL_LEN 20
  22. #define MAX_SIGNATURE_LEN 30
  23. #define MAX_REGION_LEN 4
  24. #define MAX_VERSION_LEN 12
  25. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  26. struct file_info {
  27. char *file_name; /* name of the file */
  28. uint32_t file_size; /* length of the file */
  29. uint32_t write_size;
  30. };
  31. struct img_header {
  32. uint32_t checksum;
  33. uint32_t image_size;
  34. uint32_t kernel_size;
  35. char model[MAX_MODEL_LEN];
  36. char signature[MAX_SIGNATURE_LEN];
  37. char region[MAX_REGION_LEN];
  38. char version[MAX_VERSION_LEN];
  39. unsigned char header_len;
  40. unsigned char is_tgz;
  41. unsigned char pad[4];
  42. } __attribute__ ((packed));
  43. /*
  44. * Globals
  45. */
  46. static char *ofname;
  47. static char *progname;
  48. static char *model;
  49. static char *signature;
  50. static char *region = "DEF";
  51. static char *version;
  52. static struct file_info kernel_info;
  53. static struct file_info rootfs_info;
  54. static uint32_t kernel_size;
  55. static uint32_t image_size;
  56. static int combined;
  57. /*
  58. * Message macros
  59. */
  60. #define ERR(fmt, ...) do { \
  61. fflush(0); \
  62. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  63. progname, ## __VA_ARGS__ ); \
  64. } while (0)
  65. #define ERRS(fmt, ...) do { \
  66. int save = errno; \
  67. fflush(0); \
  68. fprintf(stderr, "[%s] *** error: " fmt " (%s)\n", \
  69. progname, ## __VA_ARGS__, strerror(save)); \
  70. } while (0)
  71. #define DBG(fmt, ...) do { \
  72. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  73. } while (0)
  74. static void usage(int status)
  75. {
  76. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  77. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  78. fprintf(stream,
  79. "\n"
  80. "Options:\n"
  81. " -k <file> read kernel image from the file <file>\n"
  82. " -c use the kernel image as a combined image\n"
  83. " -M <model> set model to <model>\n"
  84. " -o <file> write output to the file <file>\n"
  85. " -r <file> read rootfs image from the file <file>\n"
  86. " -S <signature> set image signature to <signature>\n"
  87. " -R <region> set image region to <region>\n"
  88. " -V <version> set image version to <version>\n"
  89. " -I <size> set image size to <size>\n"
  90. " -K <size> set kernel size to <size>\n"
  91. " -h show this screen\n"
  92. );
  93. exit(status);
  94. }
  95. int
  96. str2u32(char *arg, uint32_t *val)
  97. {
  98. char *err = NULL;
  99. uint32_t t;
  100. errno=0;
  101. t = strtoul(arg, &err, 0);
  102. if (errno || (err==arg) || ((err != NULL) && *err)) {
  103. return -1;
  104. }
  105. *val = t;
  106. return 0;
  107. }
  108. static int get_file_stat(struct file_info *fdata)
  109. {
  110. struct stat st;
  111. int res;
  112. if (fdata->file_name == NULL)
  113. return 0;
  114. res = stat(fdata->file_name, &st);
  115. if (res){
  116. ERRS("stat failed on %s", fdata->file_name);
  117. return res;
  118. }
  119. fdata->file_size = st.st_size;
  120. fdata->write_size = fdata->file_size;
  121. return 0;
  122. }
  123. static int read_to_buf(struct file_info *fdata, char *buf)
  124. {
  125. FILE *f;
  126. int ret = EXIT_FAILURE;
  127. f = fopen(fdata->file_name, "r");
  128. if (f == NULL) {
  129. ERRS("could not open \"%s\" for reading", fdata->file_name);
  130. goto out;
  131. }
  132. errno = 0;
  133. fread(buf, fdata->file_size, 1, f);
  134. if (errno != 0) {
  135. ERRS("unable to read from file \"%s\"", fdata->file_name);
  136. goto out_close;
  137. }
  138. ret = EXIT_SUCCESS;
  139. out_close:
  140. fclose(f);
  141. out:
  142. return ret;
  143. }
  144. static int check_options(void)
  145. {
  146. int ret;
  147. #define CHKSTR(_name, _msg) \
  148. do { \
  149. if (_name == NULL) { \
  150. ERR("no %s specified", _msg); \
  151. return -1; \
  152. } \
  153. } while (0)
  154. #define CHKSTRLEN(_name, _msg) \
  155. do { \
  156. int field_len; \
  157. CHKSTR(_name, _msg); \
  158. field_len = FIELD_SIZEOF(struct img_header, _name) - 1; \
  159. if (strlen(_name) > field_len) { \
  160. ERR("%s is too long, max length is %d", \
  161. _msg, field_len); \
  162. return -1; \
  163. } \
  164. } while (0)
  165. CHKSTRLEN(model, "model");
  166. CHKSTRLEN(signature, "signature");
  167. CHKSTRLEN(region, "region");
  168. CHKSTRLEN(version, "version");
  169. CHKSTR(ofname, "output file");
  170. CHKSTR(kernel_info.file_name, "kernel image");
  171. ret = get_file_stat(&kernel_info);
  172. if (ret)
  173. return ret;
  174. if (combined) {
  175. if (!kernel_size) {
  176. ERR("kernel size must be specified for combined images");
  177. return -1; \
  178. }
  179. if (!image_size)
  180. image_size = kernel_info.file_size;
  181. if (kernel_info.file_size > image_size) {
  182. ERR("kernel image is too big");
  183. return -1;
  184. }
  185. kernel_info.write_size = image_size;
  186. } else {
  187. CHKSTR(rootfs_info.file_name, "rootfs image");
  188. ret = get_file_stat(&rootfs_info);
  189. if (ret)
  190. return ret;
  191. if (kernel_size) {
  192. /* override kernel size */
  193. kernel_info.write_size = kernel_size;
  194. }
  195. if (image_size) {
  196. if (image_size < kernel_info.write_size)
  197. kernel_info.write_size = image_size;
  198. /* override rootfs size */
  199. rootfs_info.write_size = image_size - kernel_info.write_size;
  200. }
  201. if (kernel_info.file_size > kernel_info.write_size) {
  202. ERR("kernel image is too big");
  203. return -1;
  204. }
  205. if (rootfs_info.file_size > rootfs_info.write_size) {
  206. ERR("rootfs image is too big");
  207. return -1;
  208. }
  209. }
  210. return 0;
  211. }
  212. static int write_fw(char *data, int len)
  213. {
  214. FILE *f;
  215. int ret = EXIT_FAILURE;
  216. f = fopen(ofname, "w");
  217. if (f == NULL) {
  218. ERRS("could not open \"%s\" for writing", ofname);
  219. goto out;
  220. }
  221. errno = 0;
  222. fwrite(data, len, 1, f);
  223. if (errno) {
  224. ERRS("unable to write output file");
  225. goto out_flush;
  226. }
  227. DBG("firmware file \"%s\" completed", ofname);
  228. ret = EXIT_SUCCESS;
  229. out_flush:
  230. fflush(f);
  231. fclose(f);
  232. if (ret != EXIT_SUCCESS) {
  233. unlink(ofname);
  234. }
  235. out:
  236. return ret;
  237. }
  238. static uint32_t get_csum(unsigned char *p, uint32_t len)
  239. {
  240. uint32_t csum = 0;
  241. while (len--)
  242. csum += *p++;
  243. return csum;
  244. }
  245. static int build_fw(void)
  246. {
  247. int buflen;
  248. char *buf;
  249. char *p;
  250. uint32_t csum;
  251. struct img_header *hdr;
  252. int ret = EXIT_FAILURE;
  253. buflen = sizeof(struct img_header) +
  254. kernel_info.write_size + rootfs_info.write_size;
  255. buf = malloc(buflen);
  256. if (!buf) {
  257. ERR("no memory for buffer\n");
  258. goto out;
  259. }
  260. memset(buf, 0, buflen);
  261. p = buf + sizeof(struct img_header);
  262. /* read kernel data */
  263. ret = read_to_buf(&kernel_info, p);
  264. if (ret)
  265. goto out_free_buf;
  266. if (!combined) {
  267. p += kernel_info.write_size;
  268. /* read rootfs data */
  269. ret = read_to_buf(&rootfs_info, p);
  270. if (ret)
  271. goto out_free_buf;
  272. }
  273. csum = get_csum((unsigned char *)(buf + sizeof(struct img_header)),
  274. buflen - sizeof(struct img_header));
  275. /* fill firmware header */
  276. hdr = (struct img_header *) buf;
  277. hdr->checksum = htonl(csum);
  278. hdr->image_size = htonl(buflen - sizeof(struct img_header));
  279. if (!combined)
  280. hdr->kernel_size = htonl(kernel_info.write_size);
  281. else
  282. hdr->kernel_size = htonl(kernel_size);
  283. hdr->header_len = sizeof(struct img_header);
  284. strncpy(hdr->model, model, sizeof(hdr->model));
  285. strncpy(hdr->signature, signature, sizeof(hdr->signature));
  286. strncpy(hdr->version, version, sizeof(hdr->version));
  287. strncpy(hdr->region, region, sizeof(hdr->region));
  288. ret = write_fw(buf, buflen);
  289. if (ret)
  290. goto out_free_buf;
  291. ret = EXIT_SUCCESS;
  292. out_free_buf:
  293. free(buf);
  294. out:
  295. return ret;
  296. }
  297. int main(int argc, char *argv[])
  298. {
  299. int ret = EXIT_FAILURE;
  300. progname = basename(argv[0]);
  301. while (1) {
  302. int c;
  303. c = getopt(argc, argv, "M:S:V:R:k:K:I:r:o:hc");
  304. if (c == -1)
  305. break;
  306. switch (c) {
  307. case 'M':
  308. model = optarg;
  309. break;
  310. case 'S':
  311. signature = optarg;
  312. break;
  313. case 'V':
  314. version = optarg;
  315. break;
  316. case 'R':
  317. region = optarg;
  318. break;
  319. case 'k':
  320. kernel_info.file_name = optarg;
  321. break;
  322. case 'K':
  323. if (str2u32(optarg, &kernel_size)) {
  324. ERR("%s is invalid '%s'",
  325. "kernel size", optarg);
  326. goto out;
  327. }
  328. break;
  329. case 'I':
  330. if (str2u32(optarg, &image_size)) {
  331. ERR("%s is invalid '%s'",
  332. "image size", optarg);
  333. goto out;
  334. }
  335. break;
  336. case 'r':
  337. rootfs_info.file_name = optarg;
  338. break;
  339. case 'c':
  340. combined = 1;
  341. break;
  342. case 'o':
  343. ofname = optarg;
  344. break;
  345. case 'h':
  346. usage(EXIT_SUCCESS);
  347. break;
  348. default:
  349. usage(EXIT_FAILURE);
  350. break;
  351. }
  352. }
  353. ret = check_options();
  354. if (ret)
  355. goto out;
  356. ret = build_fw();
  357. out:
  358. return ret;
  359. }