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>
25 struct ihex_binrec
*next
; /* not part of the real data structure */
32 * nybble/hex are little helpers to parse hexadecimal numbers to a byte value
34 static uint8_t nybble(const uint8_t n
)
36 if (n
>= '0' && n
<= '9') return n
- '0';
37 else if (n
>= 'A' && n
<= 'F') return n
- ('A' - 10);
38 else if (n
>= 'a' && n
<= 'f') return n
- ('a' - 10);
42 static uint8_t hex(const uint8_t *data
, uint8_t *crc
)
44 uint8_t val
= (nybble(data
[0]) << 4) | nybble(data
[1]);
49 static int process_ihex(uint8_t *data
, ssize_t size
);
50 static void file_record(struct ihex_binrec
*record
);
51 static int output_records(int outfd
);
53 static int sort_records
= 0;
55 int main(int argc
, char **argv
)
61 if (argc
== 4 && !strcmp(argv
[1], "-s")) {
68 fprintf(stderr
, "ihex2fw: Convert ihex files into binary "
69 "representation for use by Linux kernel\n");
70 fprintf(stderr
, "usage: ihex2fw [-s] <src.HEX> <dst.fw>\n");
71 fprintf(stderr
, " -s: sort records by address\n");
74 if (!strcmp(argv
[1], "-"))
77 infd
= open(argv
[1], O_RDONLY
);
79 fprintf(stderr
, "Failed to open source file: %s",
83 if (fstat(infd
, &st
)) {
87 data
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, infd
, 0);
88 if (data
== MAP_FAILED
) {
93 if (!strcmp(argv
[2], "-"))
96 outfd
= open(argv
[2], O_TRUNC
|O_CREAT
|O_WRONLY
, 0644);
98 fprintf(stderr
, "Failed to open destination file: %s",
102 if (process_ihex(data
, st
.st_size
))
105 output_records(outfd
);
109 static int process_ihex(uint8_t *data
, ssize_t size
)
111 struct ihex_binrec
*record
;
113 uint8_t type
, crc
= 0, crcbyte
= 0;
120 /* search for the start of record character */
122 if (data
[i
] == '\n') line
++;
123 if (data
[i
++] == ':') break;
126 /* Minimum record length would be about 10 characters */
128 fprintf(stderr
, "Can't find valid record at line %d\n", line
);
132 len
= hex(data
+ i
, &crc
); i
+= 2;
134 record
= malloc((sizeof (*record
) + len
+ 3) & ~3);
136 fprintf(stderr
, "out of memory for records\n");
139 memset(record
, 0, (sizeof(*record
) + len
+ 3) & ~3);
142 /* now check if we have enough data to read everything */
143 if (i
+ 8 + (record
->len
* 2) > size
) {
144 fprintf(stderr
, "Not enough data to read complete record at line %d\n",
149 record
->addr
= hex(data
+ i
, &crc
) << 8; i
+= 2;
150 record
->addr
|= hex(data
+ i
, &crc
); i
+= 2;
151 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
);
157 crcbyte
= hex(data
+ i
, &crc
); i
+= 2;
159 fprintf(stderr
, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
160 line
, crcbyte
, (unsigned char)(crcbyte
-crc
));
164 /* Done reading the record */
167 /* old style EOF record? */
171 record
->addr
+= offset
;
175 case 1: /* End-Of-File Record */
176 if (record
->addr
|| record
->len
) {
177 fprintf(stderr
, "Bad EOF record (type 01) format at line %d",
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",
191 /* We shouldn't really be using the offset for HEX86 because
192 * the wraparound case is specified quite differently. */
193 offset
= record
->data
[0] << 8 | record
->data
[1];
194 offset
<<= (type
== 2 ? 4 : 16);
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",
205 /* These records contain the CS/IP or EIP where execution
206 * starts. Don't really know what to do with them. */
210 fprintf(stderr
, "Unknown record (type %02X)\n", type
);
217 static struct ihex_binrec
*records
;
219 static void file_record(struct ihex_binrec
*record
)
221 struct ihex_binrec
**p
= &records
;
223 while ((*p
) && (!sort_records
|| (*p
)->addr
< record
->addr
))
230 static int output_records(int outfd
)
232 unsigned char zeroes
[5] = {0, 0, 0, 0, 0};
233 struct ihex_binrec
*p
= records
;
236 uint16_t writelen
= (p
->len
+ 9) & ~3;
238 p
->addr
= htonl(p
->addr
);
239 p
->len
= htonl(p
->len
);
240 write(outfd
, &p
->addr
, writelen
);
243 /* EOF record is zero length, since we don't bother to represent
244 the type field in the binary version */
245 write(outfd
, zeroes
, 5);
This page took 0.038805 seconds and 6 git commands to generate.