ihex2fw.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. output_records(outfd);
  114. return 0;
  115. }
  116. static int process_ihex(uint8_t *data, ssize_t size)
  117. {
  118. struct ihex_binrec *record;
  119. uint32_t offset = 0;
  120. uint32_t data32;
  121. uint8_t type, crc = 0, crcbyte = 0;
  122. int i, j;
  123. int line = 1;
  124. int len;
  125. i = 0;
  126. next_record:
  127. /* search for the start of record character */
  128. while (i < size) {
  129. if (data[i] == '\n') line++;
  130. if (data[i++] == ':') break;
  131. }
  132. /* Minimum record length would be about 10 characters */
  133. if (i + 10 > size) {
  134. fprintf(stderr, "Can't find valid record at line %d\n", line);
  135. return -EINVAL;
  136. }
  137. len = hex(data + i, &crc); i += 2;
  138. if (wide_records) {
  139. len <<= 8;
  140. len += hex(data + i, &crc); i += 2;
  141. }
  142. record = malloc((sizeof (*record) + len + 3) & ~3);
  143. if (!record) {
  144. fprintf(stderr, "out of memory for records\n");
  145. return -ENOMEM;
  146. }
  147. memset(record, 0, (sizeof(*record) + len + 3) & ~3);
  148. record->len = len;
  149. /* now check if we have enough data to read everything */
  150. if (i + 8 + (record->len * 2) > size) {
  151. fprintf(stderr, "Not enough data to read complete record at line %d\n",
  152. line);
  153. return -EINVAL;
  154. }
  155. record->addr = hex(data + i, &crc) << 8; i += 2;
  156. record->addr |= hex(data + i, &crc); i += 2;
  157. type = hex(data + i, &crc); i += 2;
  158. for (j = 0; j < record->len; j++, i += 2)
  159. record->data[j] = hex(data + i, &crc);
  160. /* check CRC */
  161. crcbyte = hex(data + i, &crc); i += 2;
  162. if (crc != 0) {
  163. fprintf(stderr, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
  164. line, crcbyte, (unsigned char)(crcbyte-crc));
  165. return -EINVAL;
  166. }
  167. /* Done reading the record */
  168. switch (type) {
  169. case 0:
  170. /* old style EOF record? */
  171. if (!record->len)
  172. break;
  173. record->addr += offset;
  174. file_record(record);
  175. goto next_record;
  176. case 1: /* End-Of-File Record */
  177. if (record->addr || record->len) {
  178. fprintf(stderr, "Bad EOF record (type 01) format at line %d",
  179. line);
  180. return -EINVAL;
  181. }
  182. break;
  183. case 2: /* Extended Segment Address Record (HEX86) */
  184. case 4: /* Extended Linear Address Record (HEX386) */
  185. if (record->addr || record->len != 2) {
  186. fprintf(stderr, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
  187. type, line);
  188. return -EINVAL;
  189. }
  190. /* We shouldn't really be using the offset for HEX86 because
  191. * the wraparound case is specified quite differently. */
  192. offset = record->data[0] << 8 | record->data[1];
  193. offset <<= (type == 2 ? 4 : 16);
  194. goto next_record;
  195. case 3: /* Start Segment Address Record */
  196. case 5: /* Start Linear Address Record */
  197. if (record->addr || record->len != 4) {
  198. fprintf(stderr, "Bad Start Address record (type %02X) at line %d\n",
  199. type, line);
  200. return -EINVAL;
  201. }
  202. memcpy(&data32, &record->data[0], sizeof(data32));
  203. data32 = htonl(data32);
  204. memcpy(&record->data[0], &data32, sizeof(data32));
  205. /* These records contain the CS/IP or EIP where execution
  206. * starts. If requested output this as a record. */
  207. if (include_jump)
  208. file_record(record);
  209. goto next_record;
  210. default:
  211. fprintf(stderr, "Unknown record (type %02X)\n", type);
  212. return -EINVAL;
  213. }
  214. return 0;
  215. }
  216. static struct ihex_binrec *records;
  217. static void file_record(struct ihex_binrec *record)
  218. {
  219. struct ihex_binrec **p = &records;
  220. while ((*p) && (!sort_records || (*p)->addr < record->addr))
  221. p = &((*p)->next);
  222. record->next = *p;
  223. *p = record;
  224. }
  225. static int output_records(int outfd)
  226. {
  227. unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0};
  228. struct ihex_binrec *p = records;
  229. while (p) {
  230. uint16_t writelen = (p->len + 9) & ~3;
  231. p->addr = htonl(p->addr);
  232. p->len = htons(p->len);
  233. write(outfd, &p->addr, writelen);
  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. write(outfd, zeroes, 6);
  239. return 0;
  240. }