ihex2fw.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. int usage(void)
  52. {
  53. fprintf(stderr, "ihex2fw: Convert ihex files into binary "
  54. "representation for use by Linux kernel\n");
  55. fprintf(stderr, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>\n");
  56. fprintf(stderr, " -w: wide records (16-bit length)\n");
  57. fprintf(stderr, " -s: sort records by address\n");
  58. return 1;
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. int infd, outfd;
  63. struct stat st;
  64. uint8_t *data;
  65. int opt;
  66. while ((opt = getopt(argc, argv, "ws")) != -1) {
  67. switch (opt) {
  68. case 'w':
  69. wide_records = 1;
  70. break;
  71. case 's':
  72. sort_records = 1;
  73. break;
  74. default:
  75. return usage();
  76. }
  77. }
  78. if (optind + 2 != argc)
  79. return usage();
  80. if (!strcmp(argv[optind], "-"))
  81. infd = 0;
  82. else
  83. infd = open(argv[optind], O_RDONLY);
  84. if (infd == -1) {
  85. fprintf(stderr, "Failed to open source file: %s",
  86. strerror(errno));
  87. return usage();
  88. }
  89. if (fstat(infd, &st)) {
  90. perror("stat");
  91. return 1;
  92. }
  93. data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, infd, 0);
  94. if (data == MAP_FAILED) {
  95. perror("mmap");
  96. return 1;
  97. }
  98. if (!strcmp(argv[optind+1], "-"))
  99. outfd = 1;
  100. else
  101. outfd = open(argv[optind+1], O_TRUNC|O_CREAT|O_WRONLY, 0644);
  102. if (outfd == -1) {
  103. fprintf(stderr, "Failed to open destination file: %s",
  104. strerror(errno));
  105. return usage();
  106. }
  107. if (process_ihex(data, st.st_size))
  108. return 1;
  109. output_records(outfd);
  110. return 0;
  111. }
  112. static int process_ihex(uint8_t *data, ssize_t size)
  113. {
  114. struct ihex_binrec *record;
  115. uint32_t offset = 0;
  116. uint8_t type, crc = 0, crcbyte = 0;
  117. int i, j;
  118. int line = 1;
  119. int len;
  120. i = 0;
  121. next_record:
  122. /* search for the start of record character */
  123. while (i < size) {
  124. if (data[i] == '\n') line++;
  125. if (data[i++] == ':') break;
  126. }
  127. /* Minimum record length would be about 10 characters */
  128. if (i + 10 > size) {
  129. fprintf(stderr, "Can't find valid record at line %d\n", line);
  130. return -EINVAL;
  131. }
  132. len = hex(data + i, &crc); i += 2;
  133. if (wide_records) {
  134. len <<= 8;
  135. len += hex(data + i, &crc); i += 2;
  136. }
  137. record = malloc((sizeof (*record) + len + 3) & ~3);
  138. if (!record) {
  139. fprintf(stderr, "out of memory for records\n");
  140. return -ENOMEM;
  141. }
  142. memset(record, 0, (sizeof(*record) + len + 3) & ~3);
  143. record->len = len;
  144. /* now check if we have enough data to read everything */
  145. if (i + 8 + (record->len * 2) > size) {
  146. fprintf(stderr, "Not enough data to read complete record at line %d\n",
  147. line);
  148. return -EINVAL;
  149. }
  150. record->addr = hex(data + i, &crc) << 8; i += 2;
  151. record->addr |= hex(data + i, &crc); i += 2;
  152. type = hex(data + i, &crc); i += 2;
  153. for (j = 0; j < record->len; j++, i += 2)
  154. record->data[j] = hex(data + i, &crc);
  155. /* check CRC */
  156. crcbyte = hex(data + i, &crc); i += 2;
  157. if (crc != 0) {
  158. fprintf(stderr, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
  159. line, crcbyte, (unsigned char)(crcbyte-crc));
  160. return -EINVAL;
  161. }
  162. /* Done reading the record */
  163. switch (type) {
  164. case 0:
  165. /* old style EOF record? */
  166. if (!record->len)
  167. break;
  168. record->addr += offset;
  169. file_record(record);
  170. goto next_record;
  171. case 1: /* End-Of-File Record */
  172. if (record->addr || record->len) {
  173. fprintf(stderr, "Bad EOF record (type 01) format at line %d",
  174. line);
  175. return -EINVAL;
  176. }
  177. break;
  178. case 2: /* Extended Segment Address Record (HEX86) */
  179. case 4: /* Extended Linear Address Record (HEX386) */
  180. if (record->addr || record->len != 2) {
  181. fprintf(stderr, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
  182. type, line);
  183. return -EINVAL;
  184. }
  185. /* We shouldn't really be using the offset for HEX86 because
  186. * the wraparound case is specified quite differently. */
  187. offset = record->data[0] << 8 | record->data[1];
  188. offset <<= (type == 2 ? 4 : 16);
  189. goto next_record;
  190. case 3: /* Start Segment Address Record */
  191. case 5: /* Start Linear Address Record */
  192. if (record->addr || record->len != 4) {
  193. fprintf(stderr, "Bad Start Address record (type %02X) at line %d\n",
  194. type, line);
  195. return -EINVAL;
  196. }
  197. /* These records contain the CS/IP or EIP where execution
  198. * starts. Don't really know what to do with them. */
  199. goto next_record;
  200. default:
  201. fprintf(stderr, "Unknown record (type %02X)\n", type);
  202. return -EINVAL;
  203. }
  204. return 0;
  205. }
  206. static struct ihex_binrec *records;
  207. static void file_record(struct ihex_binrec *record)
  208. {
  209. struct ihex_binrec **p = &records;
  210. while ((*p) && (!sort_records || (*p)->addr < record->addr))
  211. p = &((*p)->next);
  212. record->next = *p;
  213. *p = record;
  214. }
  215. static int output_records(int outfd)
  216. {
  217. unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0};
  218. struct ihex_binrec *p = records;
  219. while (p) {
  220. uint16_t writelen = (p->len + 9) & ~3;
  221. p->addr = htonl(p->addr);
  222. p->len = htons(p->len);
  223. write(outfd, &p->addr, writelen);
  224. p = p->next;
  225. }
  226. /* EOF record is zero length, since we don't bother to represent
  227. the type field in the binary version */
  228. write(outfd, zeroes, 6);
  229. return 0;
  230. }