fix-u-media-header.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "cyg_crc.h"
  20. #include <arpa/inet.h>
  21. #include <netinet/in.h>
  22. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  23. #define IH_NMLEN 32 /* Image Name Length */
  24. #define UM_MAGIC 0x55525F46
  25. #define UM_HEADER_LEN 12
  26. /*
  27. * all data in network byte order (aka natural aka bigendian)
  28. */
  29. struct u_media_header {
  30. uint32_t ih_magic; /* Image Header Magic Number */
  31. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  32. uint32_t ih_time; /* Image Creation Timestamp */
  33. uint32_t ih_size; /* Image Data Size */
  34. uint32_t ih_load; /* Data Load Address */
  35. uint32_t ih_ep; /* Entry Point Address */
  36. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  37. uint8_t ih_os; /* Operating System */
  38. uint8_t ih_arch; /* CPU architecture */
  39. uint8_t ih_type; /* Image Type */
  40. uint8_t ih_comp; /* Compression Type */
  41. uint8_t ih_name[IH_NMLEN - UM_HEADER_LEN]; /* Image Name */
  42. uint32_t ih_UMedia_magic; /* U-Media magic number */
  43. uint32_t ih_UMedia_boardID; /* U-Media board ID */
  44. uint8_t ih_UMedia_imageType; /* U-Media image type */
  45. uint8_t ih_UMedia_LoadDefault; /* U-Media load to factory default setting */
  46. uint8_t ih_UMedia_temp1; /* U-Media didn't use this tag */
  47. uint8_t ih_UMedia_temp2; /* U-Media didn't use this tag */
  48. } __attribute__ ((packed));
  49. struct if_info {
  50. char *file_name; /* name of the file */
  51. uint32_t file_size; /* length of the file */
  52. };
  53. static char *progname;
  54. static char *ofname;
  55. static struct if_info if_info;
  56. static int factory_defaults;
  57. static uint32_t board_id;
  58. static uint8_t image_type;
  59. /*
  60. * Message macros
  61. */
  62. #define ERR(fmt, ...) do { \
  63. fflush(0); \
  64. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  65. progname, ## __VA_ARGS__ ); \
  66. } while (0)
  67. #define ERRS(fmt, ...) do { \
  68. int save = errno; \
  69. fflush(0); \
  70. fprintf(stderr, "[%s] *** error: " fmt " (%s)\n", \
  71. progname, ## __VA_ARGS__, strerror(save)); \
  72. } while (0)
  73. #define DBG(fmt, ...) do { \
  74. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  75. } while (0)
  76. static void usage(int status)
  77. {
  78. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  79. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  80. fprintf(stream,
  81. "\n"
  82. "Options:\n"
  83. " -B <board_id> set board ID to <board_id>\n"
  84. " -i <file> read input from the file <file>\n"
  85. " -F load factory defaults\n"
  86. " -o <file> write output to the file <file>\n"
  87. " -T <type> set image type to <type>\n"
  88. " -h show this screen\n"
  89. );
  90. exit(status);
  91. }
  92. static int str2u32(char *arg, uint32_t *val)
  93. {
  94. char *err = NULL;
  95. uint32_t t;
  96. errno=0;
  97. t = strtoul(arg, &err, 0);
  98. if (errno || (err==arg) || ((err != NULL) && *err)) {
  99. return -1;
  100. }
  101. *val = t;
  102. return 0;
  103. }
  104. static int str2u8(char *arg, uint8_t *val)
  105. {
  106. char *err = NULL;
  107. uint32_t t;
  108. errno=0;
  109. t = strtoul(arg, &err, 0);
  110. if (errno || (err==arg) || ((err != NULL) && *err)) {
  111. return -1;
  112. }
  113. if (t > 255)
  114. return -1;
  115. *val = t;
  116. return 0;
  117. }
  118. static int get_file_stat(struct if_info *fdata)
  119. {
  120. struct stat st;
  121. int res;
  122. if (fdata->file_name == NULL)
  123. return 0;
  124. res = stat(fdata->file_name, &st);
  125. if (res){
  126. ERRS("stat failed on %s", fdata->file_name);
  127. return res;
  128. }
  129. fdata->file_size = st.st_size;
  130. return 0;
  131. }
  132. static int read_to_buf(struct if_info *fdata, char *buf)
  133. {
  134. FILE *f;
  135. int ret = EXIT_FAILURE;
  136. f = fopen(fdata->file_name, "r");
  137. if (f == NULL) {
  138. ERRS("could not open \"%s\" for reading", fdata->file_name);
  139. goto out;
  140. }
  141. errno = 0;
  142. fread(buf, fdata->file_size, 1, f);
  143. if (errno != 0) {
  144. ERRS("unable to read from file \"%s\"", fdata->file_name);
  145. goto out_close;
  146. }
  147. ret = EXIT_SUCCESS;
  148. out_close:
  149. fclose(f);
  150. out:
  151. return ret;
  152. }
  153. static int check_options(void)
  154. {
  155. int ret;
  156. if (ofname == NULL) {
  157. ERR("no %s specified", "output file");
  158. return -1;
  159. }
  160. if (if_info.file_name == NULL) {
  161. ERR("no %s specified", "input file");
  162. return -1;
  163. }
  164. ret = get_file_stat(&if_info);
  165. if (ret)
  166. return ret;
  167. return 0;
  168. }
  169. static int write_fw(char *data, int len)
  170. {
  171. FILE *f;
  172. int ret = EXIT_FAILURE;
  173. f = fopen(ofname, "w");
  174. if (f == NULL) {
  175. ERRS("could not open \"%s\" for writing", ofname);
  176. goto out;
  177. }
  178. errno = 0;
  179. fwrite(data, len, 1, f);
  180. if (errno) {
  181. ERRS("unable to write output file");
  182. goto out_flush;
  183. }
  184. ret = EXIT_SUCCESS;
  185. out_flush:
  186. fflush(f);
  187. fclose(f);
  188. if (ret != EXIT_SUCCESS) {
  189. unlink(ofname);
  190. }
  191. out:
  192. return ret;
  193. }
  194. static int fix_header(void)
  195. {
  196. int buflen;
  197. char *buf;
  198. uint32_t crc, crc_orig;
  199. struct u_media_header *hdr;
  200. int ret = EXIT_FAILURE;
  201. buflen = if_info.file_size;
  202. if (buflen < sizeof(*hdr)) {
  203. ERR("invalid input file\n");
  204. return ret;
  205. }
  206. buf = malloc(buflen);
  207. if (!buf) {
  208. ERR("no memory for buffer\n");
  209. goto out;
  210. }
  211. ret = read_to_buf(&if_info, buf);
  212. if (ret)
  213. goto out_free_buf;
  214. hdr = (struct u_media_header *) buf;
  215. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  216. ERR("invalid input file, bad magic\n");
  217. goto out_free_buf;
  218. }
  219. /* verify header CRC */
  220. crc_orig = ntohl(hdr->ih_hcrc);
  221. hdr->ih_hcrc = 0;
  222. crc = cyg_ether_crc32((unsigned char *)hdr, sizeof(*hdr));
  223. if (crc != crc_orig) {
  224. ERR("invalid input file, bad header CRC\n");
  225. goto out_free_buf;
  226. }
  227. hdr->ih_name[IH_NMLEN - UM_HEADER_LEN - 1] = '\0';
  228. /* set U-Media specific fields */
  229. hdr->ih_UMedia_magic = htonl(UM_MAGIC);
  230. hdr->ih_UMedia_boardID = htonl(board_id);
  231. hdr->ih_UMedia_imageType = image_type;
  232. hdr->ih_UMedia_LoadDefault = (factory_defaults) ? 1 : 0;
  233. /* update header CRC */
  234. crc = cyg_ether_crc32((unsigned char *)hdr, sizeof(*hdr));
  235. hdr->ih_hcrc = htonl(crc);
  236. ret = write_fw(buf, buflen);
  237. if (ret)
  238. goto out_free_buf;
  239. DBG("U-Media header fixed in \"%s\"", ofname);
  240. ret = EXIT_SUCCESS;
  241. out_free_buf:
  242. free(buf);
  243. out:
  244. return ret;
  245. }
  246. int main(int argc, char *argv[])
  247. {
  248. int ret = EXIT_FAILURE;
  249. progname = basename(argv[0]);
  250. while (1) {
  251. int c;
  252. c = getopt(argc, argv, "B:Fi:o:T:h");
  253. if (c == -1)
  254. break;
  255. switch (c) {
  256. case 'B':
  257. if (str2u32(optarg, &board_id)) {
  258. ERR("%s is invalid '%s'",
  259. "board ID", optarg);
  260. goto out;
  261. }
  262. break;
  263. case 'T':
  264. if (str2u8(optarg, &image_type)) {
  265. ERR("%s is invalid '%s'",
  266. "image type", optarg);
  267. goto out;
  268. }
  269. break;
  270. case 'F':
  271. factory_defaults = 1;
  272. break;
  273. case 'i':
  274. if_info.file_name = optarg;
  275. break;
  276. case 'o':
  277. ofname = optarg;
  278. break;
  279. case 'h':
  280. usage(EXIT_SUCCESS);
  281. break;
  282. default:
  283. usage(EXIT_FAILURE);
  284. break;
  285. }
  286. }
  287. ret = check_options();
  288. if (ret)
  289. goto out;
  290. ret = fix_header();
  291. out:
  292. return ret;
  293. }