2 * Parser/loader for IHEX formatted data.
4 * Copyright © 2008 David Woodhouse <dwmw2@infradead.org>
5 * Copyright © 2005 Jan Harkes <jaharkes@cs.cmu.edu>
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.
13 #include <arpa/inet.h>
16 #include <sys/types.h>
28 struct ihex_binrec
*next
; /* not part of the real data structure */
35 * nybble/hex are little helpers to parse hexadecimal numbers to a byte value
37 static uint8_t nybble(const uint8_t n
)
39 if (n
>= '0' && n
<= '9') return n
- '0';
40 else if (n
>= 'A' && n
<= 'F') return n
- ('A' - 10);
41 else if (n
>= 'a' && n
<= 'f') return n
- ('a' - 10);
45 static uint8_t hex(const uint8_t *data
, uint8_t *crc
)
47 uint8_t val
= (nybble(data
[0]) << 4) | nybble(data
[1]);
52 static int process_ihex(uint8_t *data
, ssize_t size
);
53 static void file_record(struct ihex_binrec
*record
);
54 static int output_records(int outfd
);
56 static int sort_records
= 0;
57 static int wide_records
= 0;
61 fprintf(stderr
, "ihex2fw: Convert ihex files into binary "
62 "representation for use by Linux kernel\n");
63 fprintf(stderr
, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>\n");
64 fprintf(stderr
, " -w: wide records (16-bit length)\n");
65 fprintf(stderr
, " -s: sort records by address\n");
69 int main(int argc
, char **argv
)
76 while ((opt
= getopt(argc
, argv
, "ws")) != -1) {
89 if (optind
+ 2 != argc
)
92 if (!strcmp(argv
[optind
], "-"))
95 infd
= open(argv
[optind
], O_RDONLY
);
97 fprintf(stderr
, "Failed to open source file: %s",
101 if (fstat(infd
, &st
)) {
105 data
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, infd
, 0);
106 if (data
== MAP_FAILED
) {
111 if (!strcmp(argv
[optind
+1], "-"))
114 outfd
= open(argv
[optind
+1], O_TRUNC
|O_CREAT
|O_WRONLY
, 0644);
116 fprintf(stderr
, "Failed to open destination file: %s",
120 if (process_ihex(data
, st
.st_size
))
123 output_records(outfd
);
127 static int process_ihex(uint8_t *data
, ssize_t size
)
129 struct ihex_binrec
*record
;
131 uint8_t type
, crc
= 0, crcbyte
= 0;
138 /* search for the start of record character */
140 if (data
[i
] == '\n') line
++;
141 if (data
[i
++] == ':') break;
144 /* Minimum record length would be about 10 characters */
146 fprintf(stderr
, "Can't find valid record at line %d\n", line
);
150 len
= hex(data
+ i
, &crc
); i
+= 2;
153 len
+= hex(data
+ i
, &crc
); i
+= 2;
155 record
= malloc((sizeof (*record
) + len
+ 3) & ~3);
157 fprintf(stderr
, "out of memory for records\n");
160 memset(record
, 0, (sizeof(*record
) + len
+ 3) & ~3);
163 /* now check if we have enough data to read everything */
164 if (i
+ 8 + (record
->len
* 2) > size
) {
165 fprintf(stderr
, "Not enough data to read complete record at line %d\n",
170 record
->addr
= hex(data
+ i
, &crc
) << 8; i
+= 2;
171 record
->addr
|= hex(data
+ i
, &crc
); i
+= 2;
172 type
= hex(data
+ i
, &crc
); i
+= 2;
174 for (j
= 0; j
< record
->len
; j
++, i
+= 2)
175 record
->data
[j
] = hex(data
+ i
, &crc
);
178 crcbyte
= hex(data
+ i
, &crc
); i
+= 2;
180 fprintf(stderr
, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
181 line
, crcbyte
, (unsigned char)(crcbyte
-crc
));
185 /* Done reading the record */
188 /* old style EOF record? */
192 record
->addr
+= offset
;
196 case 1: /* End-Of-File Record */
197 if (record
->addr
|| record
->len
) {
198 fprintf(stderr
, "Bad EOF record (type 01) format at line %d",
204 case 2: /* Extended Segment Address Record (HEX86) */
205 case 4: /* Extended Linear Address Record (HEX386) */
206 if (record
->addr
|| record
->len
!= 2) {
207 fprintf(stderr
, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
212 /* We shouldn't really be using the offset for HEX86 because
213 * the wraparound case is specified quite differently. */
214 offset
= record
->data
[0] << 8 | record
->data
[1];
215 offset
<<= (type
== 2 ? 4 : 16);
218 case 3: /* Start Segment Address Record */
219 case 5: /* Start Linear Address Record */
220 if (record
->addr
|| record
->len
!= 4) {
221 fprintf(stderr
, "Bad Start Address record (type %02X) at line %d\n",
226 /* These records contain the CS/IP or EIP where execution
227 * starts. Don't really know what to do with them. */
231 fprintf(stderr
, "Unknown record (type %02X)\n", type
);
238 static struct ihex_binrec
*records
;
240 static void file_record(struct ihex_binrec
*record
)
242 struct ihex_binrec
**p
= &records
;
244 while ((*p
) && (!sort_records
|| (*p
)->addr
< record
->addr
))
251 static int output_records(int outfd
)
253 unsigned char zeroes
[6] = {0, 0, 0, 0, 0, 0};
254 struct ihex_binrec
*p
= records
;
257 uint16_t writelen
= (p
->len
+ 9) & ~3;
259 p
->addr
= htonl(p
->addr
);
260 p
->len
= htons(p
->len
);
261 write(outfd
, &p
->addr
, writelen
);
264 /* EOF record is zero length, since we don't bother to represent
265 the type field in the binary version */
266 write(outfd
, zeroes
, 6);
This page took 0.051359 seconds and 5 git commands to generate.