mkedimaximg.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  19. # define HOST_TO_LE16(x) (x)
  20. # define HOST_TO_LE32(x) (x)
  21. #else
  22. # define HOST_TO_LE16(x) bswap_16(x)
  23. # define HOST_TO_LE32(x) bswap_32(x)
  24. #endif
  25. struct header
  26. {
  27. unsigned char sign[4];
  28. unsigned int start;
  29. unsigned int flash;
  30. unsigned char model[4];
  31. unsigned int size;
  32. } __attribute__ ((packed));
  33. struct finfo
  34. {
  35. char *name;
  36. off_t size;
  37. };
  38. struct buf
  39. {
  40. char *start;
  41. size_t size;
  42. };
  43. static char *progname;
  44. static void usage(int status)
  45. {
  46. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  47. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  48. fprintf(stream,
  49. "\n"
  50. "Options:\n"
  51. " -s <sig> set image signature to <sig>\n"
  52. " -m <model> set model to <model>\n"
  53. " -i <file> read input from file <file>\n"
  54. " -o <file> write output to file <file>\n"
  55. " -f <flash> set flash address to <flash>\n"
  56. " -S <start> set start address to <start>\n");
  57. exit(status);
  58. }
  59. static int strtou32(char *arg, unsigned int *val)
  60. {
  61. char *endptr = NULL;
  62. errno = 0;
  63. *val = strtoul(arg, &endptr, 0);
  64. if (errno || (endptr == arg) || (*endptr && (endptr != NULL))) {
  65. return EXIT_SUCCESS;
  66. }
  67. return EXIT_FAILURE;
  68. }
  69. static unsigned short fwcsum (struct buf *buf) {
  70. int i;
  71. unsigned short ret = 0;
  72. for (i = 0; i < buf->size / 2; i++)
  73. ret -= ((unsigned short *) buf->start)[i];
  74. return ret;
  75. }
  76. static int fwread(struct finfo *finfo, struct buf *buf)
  77. {
  78. FILE *f;
  79. f = fopen(finfo->name, "r");
  80. if (!f) {
  81. fprintf(stderr, "could not open \"%s\" for reading\n", finfo->name);
  82. usage(EXIT_FAILURE);
  83. }
  84. buf->size = fread(buf->start, 1, finfo->size, f);
  85. if (buf->size != finfo->size) {
  86. fprintf(stderr, "unable to read from file \"%s\"\n", finfo->name);
  87. usage(EXIT_FAILURE);
  88. }
  89. fclose(f);
  90. return EXIT_SUCCESS;
  91. }
  92. static int fwwrite(struct finfo *finfo, struct buf *buf)
  93. {
  94. FILE *f;
  95. f = fopen(finfo->name, "w");
  96. if (!f) {
  97. fprintf(stderr, "could not open \"%s\" for writing\n", finfo->name);
  98. usage(EXIT_FAILURE);
  99. }
  100. buf->size = fwrite(buf->start, 1, finfo->size, f);
  101. if (buf->size != finfo->size) {
  102. fprintf(stderr, "unable to write to file \"%s\"\n", finfo->name);
  103. usage(EXIT_FAILURE);
  104. }
  105. fclose(f);
  106. return EXIT_SUCCESS;
  107. }
  108. int main(int argc, char **argv)
  109. {
  110. struct stat st;
  111. struct header header;
  112. struct buf ibuf, obuf;
  113. struct finfo ifinfo, ofinfo;
  114. unsigned short csum;
  115. int c;
  116. ifinfo.name = ofinfo.name = NULL;
  117. header.flash = header.size = header.start = 0;
  118. progname = basename(argv[0]);
  119. while((c = getopt(argc, argv, "i:o:m:s:f:S:h")) != -1) {
  120. switch (c) {
  121. case 'i':
  122. ifinfo.name = optarg;
  123. break;
  124. case 'o':
  125. ofinfo.name = optarg;
  126. break;
  127. case 'm':
  128. if (strlen(optarg) != 4) {
  129. fprintf(stderr, "model must be 4 characters long\n");
  130. usage(EXIT_FAILURE);
  131. }
  132. memcpy(header.model, optarg, 4);
  133. break;
  134. case 's':
  135. if (strlen(optarg) != 4) {
  136. fprintf(stderr, "signature must be 4 characters long\n");
  137. usage(EXIT_FAILURE);
  138. }
  139. memcpy(header.sign, optarg, 4);
  140. break;
  141. case 'h':
  142. usage(EXIT_SUCCESS);
  143. break;
  144. case 'f':
  145. if (!strtou32(optarg, &header.flash)) {
  146. fprintf(stderr, "invalid flash address specified\n");
  147. usage(EXIT_FAILURE);
  148. }
  149. break;
  150. case 'S':
  151. if (!strtou32(optarg, &header.start)) {
  152. fprintf(stderr, "invalid start address specified\n");
  153. usage(EXIT_FAILURE);
  154. }
  155. break;
  156. default:
  157. usage(EXIT_FAILURE);
  158. break;
  159. }
  160. }
  161. if (ifinfo.name == NULL) {
  162. fprintf(stderr, "no input file specified\n");
  163. usage(EXIT_FAILURE);
  164. }
  165. if (ofinfo.name == NULL) {
  166. fprintf(stderr, "no output file specified\n");
  167. usage(EXIT_FAILURE);
  168. }
  169. if (stat(ifinfo.name, &st)) {
  170. fprintf(stderr, "stat failed on %s\n", ifinfo.name);
  171. usage(EXIT_FAILURE);
  172. }
  173. if (header.sign == NULL) {
  174. fprintf(stderr, "no signature specified\n");
  175. usage(EXIT_FAILURE);
  176. }
  177. if (header.model == NULL) {
  178. fprintf(stderr, "no model specified\n");
  179. usage(EXIT_FAILURE);
  180. }
  181. if (!header.flash) {
  182. fprintf(stderr, "no flash address specified\n");
  183. usage(EXIT_FAILURE);
  184. }
  185. if (!header.start) {
  186. fprintf(stderr, "no start address specified\n");
  187. usage(EXIT_FAILURE);
  188. }
  189. ifinfo.size = st.st_size;
  190. obuf.size = ifinfo.size + sizeof(struct header) + sizeof(unsigned short);
  191. if (obuf.size % sizeof(unsigned short))
  192. obuf.size++;
  193. obuf.start = malloc(obuf.size);
  194. if (!obuf.start) {
  195. fprintf(stderr, "no memory for buffer\n");
  196. usage(EXIT_FAILURE);
  197. }
  198. memset(obuf.start, 0, obuf.size);
  199. ibuf.size = ifinfo.size;
  200. ibuf.start = obuf.start + sizeof(struct header);
  201. if (fwread(&ifinfo, &ibuf))
  202. usage(EXIT_FAILURE);
  203. header.flash = HOST_TO_LE32(header.flash);
  204. header.size = HOST_TO_LE32(obuf.size - sizeof(struct header));
  205. header.start = HOST_TO_LE32(header.start);
  206. memcpy (obuf.start, &header, sizeof(struct header));
  207. csum = HOST_TO_LE16(fwcsum(&ibuf));
  208. memcpy(obuf.start + obuf.size - sizeof(unsigned short),
  209. &csum, sizeof(unsigned short));
  210. ofinfo.size = obuf.size;
  211. if (fwwrite(&ofinfo, &obuf))
  212. usage(EXIT_FAILURE);
  213. return EXIT_SUCCESS;
  214. }