1 // SPDX-License-Identifier: GPL-2.0-only
3 * Parser/loader for IHEX formatted data.
5 * Copyright © 2008 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2005 Jan Harkes <jaharkes@cs.cmu.edu>
10 #include <arpa/inet.h>
13 #include <sys/types.h>
24 #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
25 #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
26 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
29 struct ihex_binrec
*next
; /* not part of the real data structure */
36 * nybble/hex are little helpers to parse hexadecimal numbers to a byte value
38 static uint8_t nybble(const uint8_t n
)
40 if (n
>= '0' && n
<= '9') return n
- '0';
41 else if (n
>= 'A' && n
<= 'F') return n
- ('A' - 10);
42 else if (n
>= 'a' && n
<= 'f') return n
- ('a' - 10);
46 static uint8_t hex(const uint8_t *data
, uint8_t *crc
)
48 uint8_t val
= (nybble(data
[0]) << 4) | nybble(data
[1]);
53 static int process_ihex(uint8_t *data
, ssize_t size
);
54 static void file_record(struct ihex_binrec
*record
);
55 static int output_records(int outfd
);
57 static int sort_records
= 0;
58 static int wide_records
= 0;
59 static int include_jump
= 0;
61 static int usage(void)
63 fprintf(stderr
, "ihex2fw: Convert ihex files into binary "
64 "representation for use by Linux kernel\n");
65 fprintf(stderr
, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>\n");
66 fprintf(stderr
, " -w: wide records (16-bit length)\n");
67 fprintf(stderr
, " -s: sort records by address\n");
68 fprintf(stderr
, " -j: include records for CS:IP/EIP address\n");
72 int main(int argc
, char **argv
)
79 while ((opt
= getopt(argc
, argv
, "wsj")) != -1) {
95 if (optind
+ 2 != argc
)
98 if (!strcmp(argv
[optind
], "-"))
101 infd
= open(argv
[optind
], O_RDONLY
);
103 fprintf(stderr
, "Failed to open source file: %s",
107 if (fstat(infd
, &st
)) {
111 data
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, infd
, 0);
112 if (data
== MAP_FAILED
) {
117 if (!strcmp(argv
[optind
+1], "-"))
120 outfd
= open(argv
[optind
+1], O_TRUNC
|O_CREAT
|O_WRONLY
, 0644);
122 fprintf(stderr
, "Failed to open destination file: %s",
126 if (process_ihex(data
, st
.st_size
))
129 return output_records(outfd
);
132 static int process_ihex(uint8_t *data
, ssize_t size
)
134 struct ihex_binrec
*record
;
138 uint8_t type
, crc
= 0, crcbyte
= 0;
145 /* search for the start of record character */
147 if (data
[i
] == '\n') line
++;
148 if (data
[i
++] == ':') break;
151 /* Minimum record length would be about 10 characters */
153 fprintf(stderr
, "Can't find valid record at line %d\n", line
);
157 len
= hex(data
+ i
, &crc
); i
+= 2;
160 len
+= hex(data
+ i
, &crc
); i
+= 2;
162 record_size
= ALIGN(sizeof(*record
) + len
, 4);
163 record
= malloc(record_size
);
165 fprintf(stderr
, "out of memory for records\n");
168 memset(record
, 0, record_size
);
171 /* now check if we have enough data to read everything */
172 if (i
+ 8 + (record
->len
* 2) > size
) {
173 fprintf(stderr
, "Not enough data to read complete record at line %d\n",
178 record
->addr
= hex(data
+ i
, &crc
) << 8; i
+= 2;
179 record
->addr
|= hex(data
+ i
, &crc
); i
+= 2;
180 type
= hex(data
+ i
, &crc
); i
+= 2;
182 for (j
= 0; j
< record
->len
; j
++, i
+= 2)
183 record
->data
[j
] = hex(data
+ i
, &crc
);
186 crcbyte
= hex(data
+ i
, &crc
); i
+= 2;
188 fprintf(stderr
, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
189 line
, crcbyte
, (unsigned char)(crcbyte
-crc
));
193 /* Done reading the record */
196 /* old style EOF record? */
200 record
->addr
+= offset
;
204 case 1: /* End-Of-File Record */
205 if (record
->addr
|| record
->len
) {
206 fprintf(stderr
, "Bad EOF record (type 01) format at line %d",
212 case 2: /* Extended Segment Address Record (HEX86) */
213 case 4: /* Extended Linear Address Record (HEX386) */
214 if (record
->addr
|| record
->len
!= 2) {
215 fprintf(stderr
, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
220 /* We shouldn't really be using the offset for HEX86 because
221 * the wraparound case is specified quite differently. */
222 offset
= record
->data
[0] << 8 | record
->data
[1];
223 offset
<<= (type
== 2 ? 4 : 16);
226 case 3: /* Start Segment Address Record */
227 case 5: /* Start Linear Address Record */
228 if (record
->addr
|| record
->len
!= 4) {
229 fprintf(stderr
, "Bad Start Address record (type %02X) at line %d\n",
234 memcpy(&data32
, &record
->data
[0], sizeof(data32
));
235 data32
= htonl(data32
);
236 memcpy(&record
->data
[0], &data32
, sizeof(data32
));
238 /* These records contain the CS/IP or EIP where execution
239 * starts. If requested output this as a record. */
245 fprintf(stderr
, "Unknown record (type %02X)\n", type
);
252 static struct ihex_binrec
*records
;
254 static void file_record(struct ihex_binrec
*record
)
256 struct ihex_binrec
**p
= &records
;
258 while ((*p
) && (!sort_records
|| (*p
)->addr
< record
->addr
))
265 static uint16_t ihex_binrec_size(struct ihex_binrec
*p
)
267 return p
->len
+ sizeof(p
->addr
) + sizeof(p
->len
);
270 static int output_records(int outfd
)
272 unsigned char zeroes
[6] = {0, 0, 0, 0, 0, 0};
273 struct ihex_binrec
*p
= records
;
276 uint16_t writelen
= ALIGN(ihex_binrec_size(p
), 4);
278 p
->addr
= htonl(p
->addr
);
279 p
->len
= htons(p
->len
);
280 if (write(outfd
, &p
->addr
, writelen
) != writelen
)
284 /* EOF record is zero length, since we don't bother to represent
285 the type field in the binary version */
286 if (write(outfd
, zeroes
, 6) != 6)