ihex2fw.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Parser/loader for IHEX formatted data.
  4. *
  5. * Copyright © 2008 David Woodhouse <dwmw2@infradead.org>
  6. * Copyright © 2005 Jan Harkes <jaharkes@cs.cmu.edu>
  7. */
  8. #include <stdint.h>
  9. #include <arpa/inet.h>
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <sys/mman.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #define _GNU_SOURCE
  20. #include <getopt.h>
  21. #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
  22. #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
  23. #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
  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. default:
  80. return usage();
  81. }
  82. }
  83. if (optind + 2 != argc)
  84. return usage();
  85. if (!strcmp(argv[optind], "-"))
  86. infd = 0;
  87. else
  88. infd = open(argv[optind], O_RDONLY);
  89. if (infd == -1) {
  90. fprintf(stderr, "Failed to open source file: %s",
  91. strerror(errno));
  92. return usage();
  93. }
  94. if (fstat(infd, &st)) {
  95. perror("stat");
  96. return 1;
  97. }
  98. data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, infd, 0);
  99. if (data == MAP_FAILED) {
  100. perror("mmap");
  101. return 1;
  102. }
  103. if (!strcmp(argv[optind+1], "-"))
  104. outfd = 1;
  105. else
  106. outfd = open(argv[optind+1], O_TRUNC|O_CREAT|O_WRONLY, 0644);
  107. if (outfd == -1) {
  108. fprintf(stderr, "Failed to open destination file: %s",
  109. strerror(errno));
  110. return usage();
  111. }
  112. if (process_ihex(data, st.st_size))
  113. return 1;
  114. return output_records(outfd);
  115. }
  116. static int process_ihex(uint8_t *data, ssize_t size)
  117. {
  118. struct ihex_binrec *record;
  119. size_t record_size;
  120. uint32_t offset = 0;
  121. uint32_t data32;
  122. uint8_t type, crc = 0, crcbyte = 0;
  123. int i, j;
  124. int line = 1;
  125. int len;
  126. i = 0;
  127. next_record:
  128. /* search for the start of record character */
  129. while (i < size) {
  130. if (data[i] == '\n') line++;
  131. if (data[i++] == ':') break;
  132. }
  133. /* Minimum record length would be about 10 characters */
  134. if (i + 10 > size) {
  135. fprintf(stderr, "Can't find valid record at line %d\n", line);
  136. return -EINVAL;
  137. }
  138. len = hex(data + i, &crc); i += 2;
  139. if (wide_records) {
  140. len <<= 8;
  141. len += hex(data + i, &crc); i += 2;
  142. }
  143. record_size = ALIGN(sizeof(*record) + len, 4);
  144. record = malloc(record_size);
  145. if (!record) {
  146. fprintf(stderr, "out of memory for records\n");
  147. return -ENOMEM;
  148. }
  149. memset(record, 0, record_size);
  150. record->len = len;
  151. /* now check if we have enough data to read everything */
  152. if (i + 8 + (record->len * 2) > size) {
  153. fprintf(stderr, "Not enough data to read complete record at line %d\n",
  154. line);
  155. return -EINVAL;
  156. }
  157. record->addr = hex(data + i, &crc) << 8; i += 2;
  158. record->addr |= hex(data + i, &crc); i += 2;
  159. type = hex(data + i, &crc); i += 2;
  160. for (j = 0; j < record->len; j++, i += 2)
  161. record->data[j] = hex(data + i, &crc);
  162. /* check CRC */
  163. crcbyte = hex(data + i, &crc); i += 2;
  164. if (crc != 0) {
  165. fprintf(stderr, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
  166. line, crcbyte, (unsigned char)(crcbyte-crc));
  167. return -EINVAL;
  168. }
  169. /* Done reading the record */
  170. switch (type) {
  171. case 0:
  172. /* old style EOF record? */
  173. if (!record->len)
  174. break;
  175. record->addr += offset;
  176. file_record(record);
  177. goto next_record;
  178. case 1: /* End-Of-File Record */
  179. if (record->addr || record->len) {
  180. fprintf(stderr, "Bad EOF record (type 01) format at line %d",
  181. line);
  182. return -EINVAL;
  183. }
  184. break;
  185. case 2: /* Extended Segment Address Record (HEX86) */
  186. case 4: /* Extended Linear Address Record (HEX386) */
  187. if (record->addr || record->len != 2) {
  188. fprintf(stderr, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
  189. type, line);
  190. return -EINVAL;
  191. }
  192. /* We shouldn't really be using the offset for HEX86 because
  193. * the wraparound case is specified quite differently. */
  194. offset = record->data[0] << 8 | record->data[1];
  195. offset <<= (type == 2 ? 4 : 16);
  196. goto next_record;
  197. case 3: /* Start Segment Address Record */
  198. case 5: /* Start Linear Address Record */
  199. if (record->addr || record->len != 4) {
  200. fprintf(stderr, "Bad Start Address record (type %02X) at line %d\n",
  201. type, line);
  202. return -EINVAL;
  203. }
  204. memcpy(&data32, &record->data[0], sizeof(data32));
  205. data32 = htonl(data32);
  206. memcpy(&record->data[0], &data32, sizeof(data32));
  207. /* These records contain the CS/IP or EIP where execution
  208. * starts. If requested output this as a record. */
  209. if (include_jump)
  210. file_record(record);
  211. goto next_record;
  212. default:
  213. fprintf(stderr, "Unknown record (type %02X)\n", type);
  214. return -EINVAL;
  215. }
  216. return 0;
  217. }
  218. static struct ihex_binrec *records;
  219. static void file_record(struct ihex_binrec *record)
  220. {
  221. struct ihex_binrec **p = &records;
  222. while ((*p) && (!sort_records || (*p)->addr < record->addr))
  223. p = &((*p)->next);
  224. record->next = *p;
  225. *p = record;
  226. }
  227. static uint16_t ihex_binrec_size(struct ihex_binrec *p)
  228. {
  229. return p->len + sizeof(p->addr) + sizeof(p->len);
  230. }
  231. static int output_records(int outfd)
  232. {
  233. unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0};
  234. struct ihex_binrec *p = records;
  235. while (p) {
  236. uint16_t writelen = ALIGN(ihex_binrec_size(p), 4);
  237. p->addr = htonl(p->addr);
  238. p->len = htons(p->len);
  239. if (write(outfd, &p->addr, writelen) != writelen)
  240. return 1;
  241. p = p->next;
  242. }
  243. /* EOF record is zero length, since we don't bother to represent
  244. the type field in the binary version */
  245. if (write(outfd, zeroes, 6) != 6)
  246. return 1;
  247. return 0;
  248. }