fwtool.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/types.h>
  14. #include <stdio.h>
  15. #include <getopt.h>
  16. #include <stdbool.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "fwimage.h"
  21. #include "utils.h"
  22. #include "crc32.h"
  23. #define METADATA_MAXLEN 30 * 1024
  24. #define SIGNATURE_MAXLEN 1 * 1024
  25. #define BUFLEN (METADATA_MAXLEN + SIGNATURE_MAXLEN + 1024)
  26. enum {
  27. MODE_DEFAULT = -1,
  28. MODE_EXTRACT = 0,
  29. MODE_APPEND = 1,
  30. };
  31. struct data_buf {
  32. char *cur;
  33. char *prev;
  34. int cur_len;
  35. int file_len;
  36. };
  37. static FILE *signature_file, *metadata_file, *firmware_file;
  38. static int file_mode = MODE_DEFAULT;
  39. static bool truncate_file;
  40. static bool write_truncated;
  41. static bool quiet = false;
  42. static uint32_t crc_table[256];
  43. #define msg(...) \
  44. do { \
  45. if (!quiet) \
  46. fprintf(stderr, __VA_ARGS__); \
  47. } while (0)
  48. static int
  49. usage(const char *progname)
  50. {
  51. fprintf(stderr, "Usage: %s <options> <firmware>\n"
  52. "\n"
  53. "Options:\n"
  54. " -S <file>: Append signature file to firmware image\n"
  55. " -I <file>: Append metadata file to firmware image\n"
  56. " -s <file>: Extract signature file from firmware image\n"
  57. " -i <file>: Extract metadata file from firmware image\n"
  58. " -t: Remove extracted chunks from firmare image (using -s, -i)\n"
  59. " -T: Output firmware image without extracted chunks to stdout (using -s, -i)\n"
  60. " -q: Quiet (suppress error messages)\n"
  61. "\n", progname);
  62. return 1;
  63. }
  64. static FILE *
  65. open_file(const char *name, bool write)
  66. {
  67. FILE *ret;
  68. if (!strcmp(name, "-"))
  69. return write ? stdout : stdin;
  70. ret = fopen(name, write ? "w" : "r+");
  71. if (!ret && !write)
  72. ret = fopen(name, "r");
  73. return ret;
  74. }
  75. static int
  76. set_file(FILE **file, const char *name, int mode)
  77. {
  78. if (file_mode < 0)
  79. file_mode = mode;
  80. else if (file_mode != mode) {
  81. msg("Error: mixing appending and extracting data is not supported\n");
  82. return 1;
  83. }
  84. if (*file) {
  85. msg("Error: the same append/extract option cannot be used multiple times\n");
  86. return 1;
  87. }
  88. *file = open_file(name, mode == MODE_EXTRACT);
  89. return !*file;
  90. }
  91. static void
  92. trailer_update_crc(struct fwimage_trailer *tr, void *buf, int len)
  93. {
  94. tr->crc32 = cpu_to_be32(crc32_block(be32_to_cpu(tr->crc32), buf, len, crc_table));
  95. }
  96. static int
  97. append_data(FILE *in, FILE *out, struct fwimage_trailer *tr, int maxlen)
  98. {
  99. while (1) {
  100. char buf[512];
  101. int len;
  102. len = fread(buf, 1, sizeof(buf), in);
  103. if (!len)
  104. break;
  105. maxlen -= len;
  106. if (maxlen < 0)
  107. return 1;
  108. tr->size += len;
  109. trailer_update_crc(tr, buf, len);
  110. fwrite(buf, len, 1, out);
  111. }
  112. return 0;
  113. }
  114. static void
  115. append_trailer(FILE *out, struct fwimage_trailer *tr)
  116. {
  117. tr->size = cpu_to_be32(tr->size);
  118. fwrite(tr, sizeof(*tr), 1, out);
  119. trailer_update_crc(tr, tr, sizeof(*tr));
  120. }
  121. static int
  122. add_metadata(struct fwimage_trailer *tr)
  123. {
  124. struct fwimage_header hdr = {};
  125. tr->type = FWIMAGE_INFO;
  126. tr->size = sizeof(hdr) + sizeof(*tr);
  127. trailer_update_crc(tr, &hdr, sizeof(hdr));
  128. fwrite(&hdr, sizeof(hdr), 1, firmware_file);
  129. if (append_data(metadata_file, firmware_file, tr, METADATA_MAXLEN))
  130. return 1;
  131. append_trailer(firmware_file, tr);
  132. return 0;
  133. }
  134. static int
  135. add_signature(struct fwimage_trailer *tr)
  136. {
  137. if (!signature_file)
  138. return 0;
  139. tr->type = FWIMAGE_SIGNATURE;
  140. tr->size = sizeof(*tr);
  141. if (append_data(signature_file, firmware_file, tr, SIGNATURE_MAXLEN))
  142. return 1;
  143. append_trailer(firmware_file, tr);
  144. return 0;
  145. }
  146. static int
  147. add_data(const char *name)
  148. {
  149. struct fwimage_trailer tr = {
  150. .magic = cpu_to_be32(FWIMAGE_MAGIC),
  151. .crc32 = ~0,
  152. };
  153. int file_len = 0;
  154. int ret = 0;
  155. firmware_file = fopen(name, "r+");
  156. if (!firmware_file) {
  157. msg("Failed to open firmware file\n");
  158. return 1;
  159. }
  160. while (1) {
  161. char buf[512];
  162. int len;
  163. len = fread(buf, 1, sizeof(buf), firmware_file);
  164. if (!len)
  165. break;
  166. file_len += len;
  167. trailer_update_crc(&tr, buf, len);
  168. }
  169. if (metadata_file)
  170. ret = add_metadata(&tr);
  171. else if (signature_file)
  172. ret = add_signature(&tr);
  173. if (ret) {
  174. fflush(firmware_file);
  175. ftruncate(fileno(firmware_file), file_len);
  176. }
  177. return ret;
  178. }
  179. static void
  180. remove_tail(struct data_buf *dbuf, int len)
  181. {
  182. dbuf->cur_len -= len;
  183. dbuf->file_len -= len;
  184. if (dbuf->cur_len)
  185. return;
  186. free(dbuf->cur);
  187. dbuf->cur = dbuf->prev;
  188. dbuf->prev = NULL;
  189. dbuf->cur_len = BUFLEN;
  190. }
  191. static int
  192. extract_tail(struct data_buf *dbuf, void *dest, int len)
  193. {
  194. int cur_len = dbuf->cur_len;
  195. if (!dbuf->cur)
  196. return 1;
  197. if (cur_len >= len)
  198. cur_len = len;
  199. memcpy(dest + (len - cur_len), dbuf->cur + dbuf->cur_len - cur_len, cur_len);
  200. remove_tail(dbuf, cur_len);
  201. cur_len = len - cur_len;
  202. if (cur_len && !dbuf->cur)
  203. return 1;
  204. memcpy(dest, dbuf->cur + dbuf->cur_len - cur_len, cur_len);
  205. remove_tail(dbuf, cur_len);
  206. return 0;
  207. }
  208. static uint32_t
  209. tail_crc32(struct data_buf *dbuf, uint32_t crc32)
  210. {
  211. if (dbuf->prev)
  212. crc32 = crc32_block(crc32, dbuf->prev, BUFLEN, crc_table);
  213. return crc32_block(crc32, dbuf->cur, dbuf->cur_len, crc_table);
  214. }
  215. static int
  216. validate_metadata(struct fwimage_header *hdr, int data_len)
  217. {
  218. if (hdr->version != 0)
  219. return 1;
  220. return 0;
  221. }
  222. static int
  223. extract_data(const char *name)
  224. {
  225. struct fwimage_header *hdr;
  226. struct fwimage_trailer tr;
  227. struct data_buf dbuf = {};
  228. uint32_t crc32 = ~0;
  229. int data_len = 0;
  230. int ret = 1;
  231. void *buf;
  232. bool metadata_keep = false;
  233. firmware_file = open_file(name, false);
  234. if (!firmware_file) {
  235. msg("Failed to open firmware file\n");
  236. return 1;
  237. }
  238. if (truncate_file && firmware_file == stdin) {
  239. msg("Cannot truncate file when reading from stdin\n");
  240. return 1;
  241. }
  242. buf = malloc(BUFLEN);
  243. if (!buf)
  244. return 1;
  245. do {
  246. char *tmp = dbuf.cur;
  247. if (write_truncated && dbuf.prev)
  248. fwrite(dbuf.prev, 1, BUFLEN, stdout);
  249. dbuf.cur = dbuf.prev;
  250. dbuf.prev = tmp;
  251. if (dbuf.cur)
  252. crc32 = crc32_block(crc32, dbuf.cur, BUFLEN, crc_table);
  253. else
  254. dbuf.cur = malloc(BUFLEN);
  255. if (!dbuf.cur)
  256. goto out;
  257. dbuf.cur_len = fread(dbuf.cur, 1, BUFLEN, firmware_file);
  258. dbuf.file_len += dbuf.cur_len;
  259. } while (dbuf.cur_len == BUFLEN);
  260. while (1) {
  261. if (extract_tail(&dbuf, &tr, sizeof(tr)))
  262. break;
  263. if (tr.magic != cpu_to_be32(FWIMAGE_MAGIC)) {
  264. msg("Data not found\n");
  265. metadata_keep = true;
  266. break;
  267. }
  268. data_len = be32_to_cpu(tr.size) - sizeof(tr);
  269. if (be32_to_cpu(tr.crc32) != tail_crc32(&dbuf, crc32)) {
  270. msg("CRC error\n");
  271. break;
  272. }
  273. if (data_len > BUFLEN) {
  274. msg("Size error\n");
  275. break;
  276. }
  277. extract_tail(&dbuf, buf, data_len);
  278. if (tr.type == FWIMAGE_SIGNATURE) {
  279. if (!signature_file)
  280. continue;
  281. fwrite(buf, data_len, 1, signature_file);
  282. ret = 0;
  283. break;
  284. } else if (tr.type == FWIMAGE_INFO) {
  285. if (!metadata_file) {
  286. dbuf.file_len += data_len + sizeof(tr);
  287. metadata_keep = true;
  288. break;
  289. }
  290. hdr = buf;
  291. data_len -= sizeof(*hdr);
  292. if (validate_metadata(hdr, data_len))
  293. continue;
  294. fwrite(hdr + 1, data_len, 1, metadata_file);
  295. ret = 0;
  296. break;
  297. } else {
  298. continue;
  299. }
  300. }
  301. if (!ret && truncate_file)
  302. ftruncate(fileno(firmware_file), dbuf.file_len);
  303. if (write_truncated) {
  304. if (dbuf.prev)
  305. fwrite(dbuf.prev, 1, BUFLEN, stdout);
  306. if (dbuf.cur)
  307. fwrite(dbuf.cur, 1, dbuf.cur_len, stdout);
  308. if (metadata_keep) {
  309. fwrite(buf, data_len, 1, stdout);
  310. fwrite(&tr, sizeof(tr), 1, stdout);
  311. }
  312. }
  313. out:
  314. free(buf);
  315. free(dbuf.cur);
  316. free(dbuf.prev);
  317. return ret;
  318. }
  319. static void cleanup(void)
  320. {
  321. if (signature_file)
  322. fclose(signature_file);
  323. if (metadata_file)
  324. fclose(metadata_file);
  325. if (firmware_file)
  326. fclose(firmware_file);
  327. }
  328. int main(int argc, char **argv)
  329. {
  330. const char *progname = argv[0];
  331. int ret, ch;
  332. crc32_filltable(crc_table);
  333. while ((ch = getopt(argc, argv, "i:I:qs:S:tT")) != -1) {
  334. ret = 0;
  335. switch(ch) {
  336. case 'S':
  337. ret = set_file(&signature_file, optarg, MODE_APPEND);
  338. break;
  339. case 'I':
  340. ret = set_file(&metadata_file, optarg, MODE_APPEND);
  341. break;
  342. case 's':
  343. ret = set_file(&signature_file, optarg, MODE_EXTRACT);
  344. break;
  345. case 'i':
  346. ret = set_file(&metadata_file, optarg, MODE_EXTRACT);
  347. break;
  348. case 't':
  349. truncate_file = true;
  350. break;
  351. case 'T':
  352. write_truncated = true;
  353. break;
  354. case 'q':
  355. quiet = true;
  356. break;
  357. }
  358. if (ret)
  359. goto out;
  360. }
  361. if (optind >= argc) {
  362. ret = usage(progname);
  363. goto out;
  364. }
  365. if (file_mode == MODE_DEFAULT) {
  366. ret = usage(progname);
  367. goto out;
  368. }
  369. if (signature_file && metadata_file) {
  370. msg("Cannot append/extract metadata and signature in one run\n");
  371. return 1;
  372. }
  373. if (file_mode)
  374. ret = add_data(argv[optind]);
  375. else
  376. ret = extract_data(argv[optind]);
  377. out:
  378. cleanup();
  379. return ret;
  380. }