]>
git.ipfire.org Git - thirdparty/kernel/stable.git/blob - tools/firmware/ihex2fw.c
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>
27 #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
28 #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
29 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
32 struct ihex_binrec
*next
; /* not part of the real data structure */
39 * nybble/hex are little helpers to parse hexadecimal numbers to a byte value
41 static uint8_t nybble(const uint8_t n
)
43 if (n
>= '0' && n
<= '9') return n
- '0';
44 else if (n
>= 'A' && n
<= 'F') return n
- ('A' - 10);
45 else if (n
>= 'a' && n
<= 'f') return n
- ('a' - 10);
49 static uint8_t hex(const uint8_t *data
, uint8_t *crc
)
51 uint8_t val
= (nybble(data
[0]) << 4) | nybble(data
[1]);
56 static int process_ihex(uint8_t *data
, ssize_t size
);
57 static void file_record(struct ihex_binrec
*record
);
58 static int output_records(int outfd
);
60 static int sort_records
= 0;
61 static int wide_records
= 0;
62 static int include_jump
= 0;
64 static int usage(void)
66 fprintf(stderr
, "ihex2fw: Convert ihex files into binary "
67 "representation for use by Linux kernel\n");
68 fprintf(stderr
, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>\n");
69 fprintf(stderr
, " -w: wide records (16-bit length)\n");
70 fprintf(stderr
, " -s: sort records by address\n");
71 fprintf(stderr
, " -j: include records for CS:IP/EIP address\n");
75 int main(int argc
, char **argv
)
82 while ((opt
= getopt(argc
, argv
, "wsj")) != -1) {
98 if (optind
+ 2 != argc
)
101 if (!strcmp(argv
[optind
], "-"))
104 infd
= open(argv
[optind
], O_RDONLY
);
106 fprintf(stderr
, "Failed to open source file: %s",
110 if (fstat(infd
, &st
)) {
114 data
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, infd
, 0);
115 if (data
== MAP_FAILED
) {
120 if (!strcmp(argv
[optind
+1], "-"))
123 outfd
= open(argv
[optind
+1], O_TRUNC
|O_CREAT
|O_WRONLY
, 0644);
125 fprintf(stderr
, "Failed to open destination file: %s",
129 if (process_ihex(data
, st
.st_size
))
132 return output_records(outfd
);
135 static int process_ihex(uint8_t *data
, ssize_t size
)
137 struct ihex_binrec
*record
;
141 uint8_t type
, crc
= 0, crcbyte
= 0;
148 /* search for the start of record character */
150 if (data
[i
] == '\n') line
++;
151 if (data
[i
++] == ':') break;
154 /* Minimum record length would be about 10 characters */
156 fprintf(stderr
, "Can't find valid record at line %d\n", line
);
160 len
= hex(data
+ i
, &crc
); i
+= 2;
163 len
+= hex(data
+ i
, &crc
); i
+= 2;
165 record_size
= ALIGN(sizeof(*record
) + len
, 4);
166 record
= malloc(record_size
);
168 fprintf(stderr
, "out of memory for records\n");
171 memset(record
, 0, record_size
);
174 /* now check if we have enough data to read everything */
175 if (i
+ 8 + (record
->len
* 2) > size
) {
176 fprintf(stderr
, "Not enough data to read complete record at line %d\n",
181 record
->addr
= hex(data
+ i
, &crc
) << 8; i
+= 2;
182 record
->addr
|= hex(data
+ i
, &crc
); i
+= 2;
183 type
= hex(data
+ i
, &crc
); i
+= 2;
185 for (j
= 0; j
< record
->len
; j
++, i
+= 2)
186 record
->data
[j
] = hex(data
+ i
, &crc
);
189 crcbyte
= hex(data
+ i
, &crc
); i
+= 2;
191 fprintf(stderr
, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
192 line
, crcbyte
, (unsigned char)(crcbyte
-crc
));
196 /* Done reading the record */
199 /* old style EOF record? */
203 record
->addr
+= offset
;
207 case 1: /* End-Of-File Record */
208 if (record
->addr
|| record
->len
) {
209 fprintf(stderr
, "Bad EOF record (type 01) format at line %d",
215 case 2: /* Extended Segment Address Record (HEX86) */
216 case 4: /* Extended Linear Address Record (HEX386) */
217 if (record
->addr
|| record
->len
!= 2) {
218 fprintf(stderr
, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
223 /* We shouldn't really be using the offset for HEX86 because
224 * the wraparound case is specified quite differently. */
225 offset
= record
->data
[0] << 8 | record
->data
[1];
226 offset
<<= (type
== 2 ? 4 : 16);
229 case 3: /* Start Segment Address Record */
230 case 5: /* Start Linear Address Record */
231 if (record
->addr
|| record
->len
!= 4) {
232 fprintf(stderr
, "Bad Start Address record (type %02X) at line %d\n",
237 memcpy(&data32
, &record
->data
[0], sizeof(data32
));
238 data32
= htonl(data32
);
239 memcpy(&record
->data
[0], &data32
, sizeof(data32
));
241 /* These records contain the CS/IP or EIP where execution
242 * starts. If requested output this as a record. */
248 fprintf(stderr
, "Unknown record (type %02X)\n", type
);
255 static struct ihex_binrec
*records
;
257 static void file_record(struct ihex_binrec
*record
)
259 struct ihex_binrec
**p
= &records
;
261 while ((*p
) && (!sort_records
|| (*p
)->addr
< record
->addr
))
268 static uint16_t ihex_binrec_size(struct ihex_binrec
*p
)
270 return p
->len
+ sizeof(p
->addr
) + sizeof(p
->len
);
273 static int output_records(int outfd
)
275 unsigned char zeroes
[6] = {0, 0, 0, 0, 0, 0};
276 struct ihex_binrec
*p
= records
;
279 uint16_t writelen
= ALIGN(ihex_binrec_size(p
), 4);
281 p
->addr
= htonl(p
->addr
);
282 p
->len
= htons(p
->len
);
283 if (write(outfd
, &p
->addr
, writelen
) != writelen
)
287 /* EOF record is zero length, since we don't bother to represent
288 the type field in the binary version */
289 if (write(outfd
, zeroes
, 6) != 6)