1
0

mkedimaximg.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2011 Vasilis Tsiligiannis <b_tsiligiannis@silverton.gr>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <libgen.h>
  14. #include <getopt.h>
  15. #include <errno.h>
  16. #include <sys/stat.h>
  17. #include <endian.h> /* for __BYTE_ORDER */
  18. #define FALSE 0
  19. #define TRUE 1
  20. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  21. # define HOST_TO_LE16(x) (x)
  22. # define HOST_TO_LE32(x) (x)
  23. # define HOST_TO_BE16(x) bswap_16(x)
  24. # define HOST_TO_BE32(x) bswap_32(x)
  25. #else
  26. # define HOST_TO_LE16(x) bswap_16(x)
  27. # define HOST_TO_LE32(x) bswap_32(x)
  28. # define HOST_TO_BE16(x) (x)
  29. # define HOST_TO_BE32(x) (x)
  30. #endif
  31. struct header
  32. {
  33. unsigned char sign[4];
  34. unsigned int start;
  35. unsigned int flash;
  36. unsigned char model[4];
  37. unsigned int size;
  38. } __attribute__ ((packed));
  39. struct finfo
  40. {
  41. char *name;
  42. off_t size;
  43. };
  44. struct buf
  45. {
  46. char *start;
  47. size_t size;
  48. };
  49. static char *progname;
  50. static int force_be = FALSE;
  51. static void usage(int status)
  52. {
  53. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  54. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  55. fprintf(stream,
  56. "\n"
  57. "Options:\n"
  58. " -s <sig> set image signature to <sig>\n"
  59. " -m <model> set model to <model>\n"
  60. " -i <file> read input from file <file>\n"
  61. " -o <file> write output to file <file>\n"
  62. " -f <flash> set flash address to <flash>\n"
  63. " -S <start> set start address to <start>\n"
  64. " -b big-endianness mode\n");
  65. exit(status);
  66. }
  67. static int strtou32(char *arg, unsigned int *val)
  68. {
  69. char *endptr = NULL;
  70. errno = 0;
  71. *val = strtoul(arg, &endptr, 0);
  72. if (errno || (endptr == arg) || (*endptr && (endptr != NULL))) {
  73. return EXIT_SUCCESS;
  74. }
  75. return EXIT_FAILURE;
  76. }
  77. static unsigned short fwcsum (struct buf *buf) {
  78. int i;
  79. unsigned short ret = 0;
  80. for (i = 0; i < buf->size / 2; i++) {
  81. if (force_be == FALSE)
  82. ret -= ((unsigned short *) buf->start)[i];
  83. else
  84. ret -= HOST_TO_BE16(((unsigned short *) buf->start)[i]);
  85. }
  86. return ret;
  87. }
  88. static int fwread(struct finfo *finfo, struct buf *buf)
  89. {
  90. FILE *f;
  91. f = fopen(finfo->name, "r");
  92. if (!f) {
  93. fprintf(stderr, "could not open \"%s\" for reading\n", finfo->name);
  94. usage(EXIT_FAILURE);
  95. }
  96. buf->size = fread(buf->start, 1, finfo->size, f);
  97. if (buf->size != finfo->size) {
  98. fprintf(stderr, "unable to read from file \"%s\"\n", finfo->name);
  99. usage(EXIT_FAILURE);
  100. }
  101. fclose(f);
  102. return EXIT_SUCCESS;
  103. }
  104. static int fwwrite(struct finfo *finfo, struct buf *buf)
  105. {
  106. FILE *f;
  107. f = fopen(finfo->name, "w");
  108. if (!f) {
  109. fprintf(stderr, "could not open \"%s\" for writing\n", finfo->name);
  110. usage(EXIT_FAILURE);
  111. }
  112. buf->size = fwrite(buf->start, 1, finfo->size, f);
  113. if (buf->size != finfo->size) {
  114. fprintf(stderr, "unable to write to file \"%s\"\n", finfo->name);
  115. usage(EXIT_FAILURE);
  116. }
  117. fclose(f);
  118. return EXIT_SUCCESS;
  119. }
  120. int main(int argc, char **argv)
  121. {
  122. struct stat st;
  123. struct header header;
  124. struct buf ibuf, obuf;
  125. struct finfo ifinfo, ofinfo;
  126. unsigned short csum;
  127. int c;
  128. ifinfo.name = ofinfo.name = NULL;
  129. header.flash = header.size = header.start = 0;
  130. progname = basename(argv[0]);
  131. while((c = getopt(argc, argv, "i:o:m:s:f:S:h:b")) != -1) {
  132. switch (c) {
  133. case 'i':
  134. ifinfo.name = optarg;
  135. break;
  136. case 'o':
  137. ofinfo.name = optarg;
  138. break;
  139. case 'm':
  140. if (strlen(optarg) != 4) {
  141. fprintf(stderr, "model must be 4 characters long\n");
  142. usage(EXIT_FAILURE);
  143. }
  144. memcpy(header.model, optarg, 4);
  145. break;
  146. case 's':
  147. if (strlen(optarg) != 4) {
  148. fprintf(stderr, "signature must be 4 characters long\n");
  149. usage(EXIT_FAILURE);
  150. }
  151. memcpy(header.sign, optarg, 4);
  152. break;
  153. case 'h':
  154. usage(EXIT_SUCCESS);
  155. break;
  156. case 'f':
  157. if (!strtou32(optarg, &header.flash)) {
  158. fprintf(stderr, "invalid flash address specified\n");
  159. usage(EXIT_FAILURE);
  160. }
  161. break;
  162. case 'S':
  163. if (!strtou32(optarg, &header.start)) {
  164. fprintf(stderr, "invalid start address specified\n");
  165. usage(EXIT_FAILURE);
  166. }
  167. break;
  168. case 'b':
  169. force_be = TRUE;
  170. break;
  171. default:
  172. usage(EXIT_FAILURE);
  173. break;
  174. }
  175. }
  176. if (ifinfo.name == NULL) {
  177. fprintf(stderr, "no input file specified\n");
  178. usage(EXIT_FAILURE);
  179. }
  180. if (ofinfo.name == NULL) {
  181. fprintf(stderr, "no output file specified\n");
  182. usage(EXIT_FAILURE);
  183. }
  184. if (stat(ifinfo.name, &st)) {
  185. fprintf(stderr, "stat failed on %s\n", ifinfo.name);
  186. usage(EXIT_FAILURE);
  187. }
  188. if (header.sign == NULL) {
  189. fprintf(stderr, "no signature specified\n");
  190. usage(EXIT_FAILURE);
  191. }
  192. if (header.model == NULL) {
  193. fprintf(stderr, "no model specified\n");
  194. usage(EXIT_FAILURE);
  195. }
  196. if (!header.flash) {
  197. fprintf(stderr, "no flash address specified\n");
  198. usage(EXIT_FAILURE);
  199. }
  200. if (!header.start) {
  201. fprintf(stderr, "no start address specified\n");
  202. usage(EXIT_FAILURE);
  203. }
  204. ifinfo.size = st.st_size;
  205. obuf.size = ifinfo.size + sizeof(struct header) + sizeof(unsigned short);
  206. if (obuf.size % sizeof(unsigned short))
  207. obuf.size++;
  208. obuf.start = malloc(obuf.size);
  209. if (!obuf.start) {
  210. fprintf(stderr, "no memory for buffer\n");
  211. usage(EXIT_FAILURE);
  212. }
  213. memset(obuf.start, 0, obuf.size);
  214. ibuf.size = ifinfo.size;
  215. ibuf.start = obuf.start + sizeof(struct header);
  216. if (fwread(&ifinfo, &ibuf))
  217. usage(EXIT_FAILURE);
  218. if (force_be == FALSE) {
  219. header.flash = HOST_TO_LE32(header.flash);
  220. header.size = HOST_TO_LE32(obuf.size - sizeof(struct header));
  221. header.start = HOST_TO_LE32(header.start);
  222. } else {
  223. header.flash = HOST_TO_BE32(header.flash);
  224. header.size = HOST_TO_BE32(obuf.size - sizeof(struct header));
  225. header.start = HOST_TO_BE32(header.start);
  226. }
  227. memcpy (obuf.start, &header, sizeof(struct header));
  228. if (force_be == FALSE)
  229. csum = HOST_TO_LE16(fwcsum(&ibuf));
  230. else
  231. csum = HOST_TO_BE16(fwcsum(&ibuf));
  232. memcpy(obuf.start + obuf.size - sizeof(unsigned short),
  233. &csum, sizeof(unsigned short));
  234. ofinfo.size = obuf.size;
  235. if (fwwrite(&ofinfo, &obuf))
  236. usage(EXIT_FAILURE);
  237. return EXIT_SUCCESS;
  238. }