ihex2fw.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Parser/loader for IHEX formatted data.
  3. *
  4. * Copyright © 2008 David Woodhouse <dwmw2@infradead.org>
  5. * Copyright © 2005 Jan Harkes <jaharkes@cs.cmu.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <stdint.h>
  12. #include <arpa/inet.h>
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/mman.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #define _GNU_SOURCE
  23. #include <getopt.h>
  24. struct ihex_binrec {
  25. struct ihex_binrec *next; /* not part of the real data structure */
  26. uint32_t addr;
  27. uint16_t len;
  28. uint8_t data[];
  29. };
  30. /**
  31. * nybble/hex are little helpers to parse hexadecimal numbers to a byte value
  32. **/
  33. static uint8_t nybble(const uint8_t n)
  34. {
  35. if (n >= '0' && n <= '9') return n - '0';
  36. else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
  37. else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
  38. return 0;
  39. }
  40. static uint8_t hex(const uint8_t *data, uint8_t *crc)
  41. {
  42. uint8_t val = (nybble(data[0]) << 4) | nybble(data[1]);
  43. *crc += val;
  44. return val;
  45. }
  46. static int process_ihex(uint8_t *data, ssize_t size);
  47. static void file_record(struct ihex_binrec *record);
  48. static int output_records(int outfd);
  49. static int sort_records = 0;
  50. static int wide_records = 0;
  51. static int include_jump = 0;
  52. static int usage(void)
  53. {
  54. fprintf(stderr, "ihex2fw: Convert ihex files into binary "
  55. "representation for use by Linux kernel\n");
  56. fprintf(stderr, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>\n");
  57. fprintf(stderr, " -w: wide records (16-bit length)\n");
  58. fprintf(stderr, " -s: sort records by address\n");
  59. fprintf(stderr, " -j: include records for CS:IP/EIP address\n");
  60. return 1;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. int infd, outfd;
  65. struct stat st;
  66. uint8_t *data;
  67. int opt;
  68. while ((opt = getopt(argc, argv, "wsj")) != -1) {
  69. switch (opt) {
  70. case 'w':
  71. wide_records = 1;
  72. break;
  73. case 's':
  74. sort_records = 1;
  75. break;
  76. case 'j':
  77. include_jump = 1;
  78. break;
  79. return usage();
  80. }
  81. }
  82. if (optind + 2 != argc)
  83. return usage();
  84. if (!strcmp(argv[optind], "-"))
  85. infd = 0;
  86. else
  87. infd = open(argv[optind], O_RDONLY);
  88. if (infd == -1) {
  89. fprintf(stderr, "Failed to open source file: %s",
  90. strerror(errno));
  91. return usage();
  92. }
  93. if (fstat(infd, &st)) {
  94. perror("stat");
  95. return 1;
  96. }
  97. data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, infd, 0);
  98. if (data == MAP_FAILED) {
  99. perror("mmap");
  100. return 1;
  101. }
  102. if (!strcmp(argv[optind+1], "-"))
  103. outfd = 1;
  104. else
  105. outfd = open(argv[optind+1], O_TRUNC|O_CREAT|O_WRONLY, 0644);
  106. if (outfd == -1) {
  107. fprintf(stderr, "Failed to open destination file: %s",
  108. strerror(errno));
  109. return usage();
  110. }
  111. if (process_ihex(data, st.st_size))
  112. return 1;
  113. return output_records(outfd);
  114. }
  115. static int process_ihex(uint8_t *data, ssize_t size)
  116. {
  117. struct ihex_binrec *record;
  118. uint32_t offset = 0;
  119. uint32_t data32;
  120. uint8_t type, crc = 0, crcbyte = 0;
  121. int i, j;
  122. int line = 1;
  123. int len;
  124. i = 0;
  125. next_record:
  126. /* search for the start of record character */
  127. while (i < size) {
  128. if (data[i] == '\n') line++;
  129. if (data[i++] == ':') break;
  130. }
  131. /* Minimum record length would be about 10 characters */
  132. if (i + 10 > size) {
  133. fprintf(stderr, "Can't find valid record at line %d\n", line);
  134. return -EINVAL;
  135. }
  136. len = hex(data + i, &crc); i += 2;
  137. if (wide_records) {
  138. len <<= 8;
  139. len += hex(data + i, &crc); i += 2;
  140. }
  141. record = malloc((sizeof (*record) + len + 3) & ~3);
  142. if (!record) {
  143. fprintf(stderr, "out of memory for records\n");
  144. return -ENOMEM;
  145. }
  146. memset(record, 0, (sizeof(*record) + len + 3) & ~3);
  147. record->len = len;
  148. /* now check if we have enough data to read everything */
  149. if (i + 8 + (record->len * 2) > size) {
  150. fprintf(stderr, "Not enough data to read complete record at line %d\n",
  151. line);
  152. return -EINVAL;
  153. }
  154. record->addr = hex(data + i, &crc) << 8; i += 2;
  155. record->addr |= hex(data + i, &crc); i += 2;
  156. type = hex(data + i, &crc); i += 2;
  157. for (j = 0; j < record->len; j++, i += 2)
  158. record->data[j] = hex(data + i, &crc);
  159. /* check CRC */
  160. crcbyte = hex(data + i, &crc); i += 2;
  161. if (crc != 0) {
  162. fprintf(stderr, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
  163. line, crcbyte, (unsigned char)(crcbyte-crc));
  164. return -EINVAL;
  165. }
  166. /* Done reading the record */
  167. switch (type) {
  168. case 0:
  169. /* old style EOF record? */
  170. if (!record->len)
  171. break;
  172. record->addr += offset;
  173. file_record(record);
  174. goto next_record;
  175. case 1: /* End-Of-File Record */
  176. if (record->addr || record->len) {
  177. fprintf(stderr, "Bad EOF record (type 01) format at line %d",
  178. line);
  179. return -EINVAL;
  180. }
  181. break;
  182. case 2: /* Extended Segment Address Record (HEX86) */
  183. case 4: /* Extended Linear Address Record (HEX386) */
  184. if (record->addr || record->len != 2) {
  185. fprintf(stderr, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
  186. type, line);
  187. return -EINVAL;
  188. }
  189. /* We shouldn't really be using the offset for HEX86 because
  190. * the wraparound case is specified quite differently. */
  191. offset = record->data[0] << 8 | record->data[1];
  192. offset <<= (type == 2 ? 4 : 16);
  193. goto next_record;
  194. case 3: /* Start Segment Address Record */
  195. case 5: /* Start Linear Address Record */
  196. if (record->addr || record->len != 4) {
  197. fprintf(stderr, "Bad Start Address record (type %02X) at line %d\n",
  198. type, line);
  199. return -EINVAL;
  200. }
  201. memcpy(&data32, &record->data[0], sizeof(data32));
  202. data32 = htonl(data32);
  203. memcpy(&record->data[0], &data32, sizeof(data32));
  204. /* These records contain the CS/IP or EIP where execution
  205. * starts. If requested output this as a record. */
  206. if (include_jump)
  207. file_record(record);
  208. goto next_record;
  209. default:
  210. fprintf(stderr, "Unknown record (type %02X)\n", type);
  211. return -EINVAL;
  212. }
  213. return 0;
  214. }
  215. static struct ihex_binrec *records;
  216. static void file_record(struct ihex_binrec *record)
  217. {
  218. struct ihex_binrec **p = &records;
  219. while ((*p) && (!sort_records || (*p)->addr < record->addr))
  220. p = &((*p)->next);
  221. record->next = *p;
  222. *p = record;
  223. }
  224. static int output_records(int outfd)
  225. {
  226. unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0};
  227. struct ihex_binrec *p = records;
  228. while (p) {
  229. uint16_t writelen = (p->len + 9) & ~3;
  230. p->addr = htonl(p->addr);
  231. p->len = htons(p->len);
  232. if (write(outfd, &p->addr, writelen) != writelen)
  233. return 1;
  234. p = p->next;
  235. }
  236. /* EOF record is zero length, since we don't bother to represent
  237. the type field in the binary version */
  238. if (write(outfd, zeroes, 6) != 6)
  239. return 1;
  240. return 0;
  241. }