Blame view

firmware/ihex2fw.c 6.57 KB
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * Parser/loader for IHEX formatted data.
   *
   * Copyright © 2008 David Woodhouse <dwmw2@infradead.org>
   * Copyright © 2005 Jan Harkes <jaharkes@cs.cmu.edu>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
  
  #include <stdint.h>
  #include <arpa/inet.h>
  #include <stdio.h>
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/mman.h>
  #include <fcntl.h>
  #include <string.h>
  #include <unistd.h>
  #include <stdlib.h>
59890f74e   David Woodhouse   ihex: Add support...
23
24
  #define _GNU_SOURCE
  #include <getopt.h>
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  
  struct ihex_binrec {
  	struct ihex_binrec *next; /* not part of the real data structure */
          uint32_t addr;
          uint16_t len;
          uint8_t data[];
  };
  
  /**
   * nybble/hex are little helpers to parse hexadecimal numbers to a byte value
   **/
  static uint8_t nybble(const uint8_t n)
  {
         if      (n >= '0' && n <= '9') return n - '0';
         else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
         else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
         return 0;
  }
  
  static uint8_t hex(const uint8_t *data, uint8_t *crc)
  {
         uint8_t val = (nybble(data[0]) << 4) | nybble(data[1]);
         *crc += val;
         return val;
  }
  
  static int process_ihex(uint8_t *data, ssize_t size);
  static void file_record(struct ihex_binrec *record);
  static int output_records(int outfd);
  
  static int sort_records = 0;
59890f74e   David Woodhouse   ihex: Add support...
56
  static int wide_records = 0;
2473238ea   Mark Brown   ihex: add support...
57
  static int include_jump = 0;
59890f74e   David Woodhouse   ihex: Add support...
58

b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
59
  static int usage(void)
59890f74e   David Woodhouse   ihex: Add support...
60
61
62
63
64
65
66
67
68
69
  {
  	fprintf(stderr, "ihex2fw: Convert ihex files into binary "
  		"representation for use by Linux kernel
  ");
  	fprintf(stderr, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>
  ");
  	fprintf(stderr, "       -w: wide records (16-bit length)
  ");
  	fprintf(stderr, "       -s: sort records by address
  ");
2473238ea   Mark Brown   ihex: add support...
70
71
  	fprintf(stderr, "       -j: include records for CS:IP/EIP address
  ");
59890f74e   David Woodhouse   ihex: Add support...
72
73
  	return 1;
  }
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
74
75
76
77
78
79
  
  int main(int argc, char **argv)
  {
  	int infd, outfd;
  	struct stat st;
  	uint8_t *data;
59890f74e   David Woodhouse   ihex: Add support...
80
  	int opt;
2473238ea   Mark Brown   ihex: add support...
81
  	while ((opt = getopt(argc, argv, "wsj")) != -1) {
59890f74e   David Woodhouse   ihex: Add support...
82
83
84
85
86
87
88
  		switch (opt) {
  		case 'w':
  			wide_records = 1;
  			break;
  		case 's':
  			sort_records = 1;
  			break;
2473238ea   Mark Brown   ihex: add support...
89
90
91
  		case 'j':
  			include_jump = 1;
  			break;
59890f74e   David Woodhouse   ihex: Add support...
92
93
  			return usage();
  		}
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
94
  	}
59890f74e   David Woodhouse   ihex: Add support...
95
96
97
98
99
  
  	if (optind + 2 != argc)
  		return usage();
  
  	if (!strcmp(argv[optind], "-"))
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
100
101
  	    infd = 0;
  	else
59890f74e   David Woodhouse   ihex: Add support...
102
  		infd = open(argv[optind], O_RDONLY);
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
103
104
105
  	if (infd == -1) {
  		fprintf(stderr, "Failed to open source file: %s",
  			strerror(errno));
59890f74e   David Woodhouse   ihex: Add support...
106
  		return usage();
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
107
108
109
110
111
112
113
114
115
116
  	}
  	if (fstat(infd, &st)) {
  		perror("stat");
  		return 1;
  	}
  	data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, infd, 0);
  	if (data == MAP_FAILED) {
  		perror("mmap");
  		return 1;
  	}
59890f74e   David Woodhouse   ihex: Add support...
117
  	if (!strcmp(argv[optind+1], "-"))
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
118
119
  	    outfd = 1;
  	else
59890f74e   David Woodhouse   ihex: Add support...
120
  		outfd = open(argv[optind+1], O_TRUNC|O_CREAT|O_WRONLY, 0644);
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
121
122
123
  	if (outfd == -1) {
  		fprintf(stderr, "Failed to open destination file: %s",
  			strerror(errno));
59890f74e   David Woodhouse   ihex: Add support...
124
  		return usage();
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
125
126
127
  	}
  	if (process_ihex(data, st.st_size))
  		return 1;
b8cb464e4   Chris Ruffin   ihex: fix unused ...
128
  	return output_records(outfd);
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
129
130
131
132
133
134
  }
  
  static int process_ihex(uint8_t *data, ssize_t size)
  {
  	struct ihex_binrec *record;
  	uint32_t offset = 0;
2473238ea   Mark Brown   ihex: add support...
135
  	uint32_t data32;
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  	uint8_t type, crc = 0, crcbyte = 0;
  	int i, j;
  	int line = 1;
  	int len;
  
  	i = 0;
  next_record:
  	/* search for the start of record character */
  	while (i < size) {
  		if (data[i] == '
  ') line++;
  		if (data[i++] == ':') break;
  	}
  
  	/* Minimum record length would be about 10 characters */
  	if (i + 10 > size) {
  		fprintf(stderr, "Can't find valid record at line %d
  ", line);
  		return -EINVAL;
  	}
  
  	len = hex(data + i, &crc); i += 2;
59890f74e   David Woodhouse   ihex: Add support...
158
159
160
161
  	if (wide_records) {
  		len <<= 8;
  		len += hex(data + i, &crc); i += 2;
  	}
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  	record = malloc((sizeof (*record) + len + 3) & ~3);
  	if (!record) {
  		fprintf(stderr, "out of memory for records
  ");
  		return -ENOMEM;
  	}
  	memset(record, 0, (sizeof(*record) + len + 3) & ~3);
  	record->len = len;
  
  	/* now check if we have enough data to read everything */
  	if (i + 8 + (record->len * 2) > size) {
  		fprintf(stderr, "Not enough data to read complete record at line %d
  ",
  			line);
  		return -EINVAL;
  	}
  
  	record->addr  = hex(data + i, &crc) << 8; i += 2;
  	record->addr |= hex(data + i, &crc); i += 2;
  	type = hex(data + i, &crc); i += 2;
  
  	for (j = 0; j < record->len; j++, i += 2)
  		record->data[j] = hex(data + i, &crc);
  
  	/* check CRC */
  	crcbyte = hex(data + i, &crc); i += 2;
  	if (crc != 0) {
  		fprintf(stderr, "CRC failure at line %d: got 0x%X, expected 0x%X
  ",
  			line, crcbyte, (unsigned char)(crcbyte-crc));
  		return -EINVAL;
  	}
  
  	/* Done reading the record */
  	switch (type) {
  	case 0:
  		/* old style EOF record? */
  		if (!record->len)
  			break;
  
  		record->addr += offset;
  		file_record(record);
  		goto next_record;
  
  	case 1: /* End-Of-File Record */
  		if (record->addr || record->len) {
  			fprintf(stderr, "Bad EOF record (type 01) format at line %d",
  				line);
  			return -EINVAL;
  		}
  		break;
  
  	case 2: /* Extended Segment Address Record (HEX86) */
  	case 4: /* Extended Linear Address Record (HEX386) */
  		if (record->addr || record->len != 2) {
  			fprintf(stderr, "Bad HEX86/HEX386 record (type %02X) at line %d
  ",
  				type, line);
  			return -EINVAL;
  		}
  
  		/* We shouldn't really be using the offset for HEX86 because
  		 * the wraparound case is specified quite differently. */
  		offset = record->data[0] << 8 | record->data[1];
  		offset <<= (type == 2 ? 4 : 16);
  		goto next_record;
  
  	case 3: /* Start Segment Address Record */
  	case 5: /* Start Linear Address Record */
  		if (record->addr || record->len != 4) {
  			fprintf(stderr, "Bad Start Address record (type %02X) at line %d
  ",
  				type, line);
  			return -EINVAL;
  		}
2473238ea   Mark Brown   ihex: add support...
237
238
239
  		memcpy(&data32, &record->data[0], sizeof(data32));
  		data32 = htonl(data32);
  		memcpy(&record->data[0], &data32, sizeof(data32));
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
240
  		/* These records contain the CS/IP or EIP where execution
2473238ea   Mark Brown   ihex: add support...
241
242
243
  		 * starts. If requested output this as a record. */
  		if (include_jump)
  			file_record(record);
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  		goto next_record;
  
  	default:
  		fprintf(stderr, "Unknown record (type %02X)
  ", type);
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static struct ihex_binrec *records;
  
  static void file_record(struct ihex_binrec *record)
  {
  	struct ihex_binrec **p = &records;
  
  	while ((*p) && (!sort_records || (*p)->addr < record->addr))
  		p = &((*p)->next);
  
  	record->next = *p;
  	*p = record;
  }
  
  static int output_records(int outfd)
  {
85ebd0033   Marc Zyngier   Fix IHEX firmware...
270
  	unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0};
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
271
272
273
274
275
276
  	struct ihex_binrec *p = records;
  
  	while (p) {
  		uint16_t writelen = (p->len + 9) & ~3;
  
  		p->addr = htonl(p->addr);
85ebd0033   Marc Zyngier   Fix IHEX firmware...
277
  		p->len = htons(p->len);
b8cb464e4   Chris Ruffin   ihex: fix unused ...
278
279
  		if (write(outfd, &p->addr, writelen) != writelen)
  			return 1;
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
280
281
282
283
  		p = p->next;
  	}
  	/* EOF record is zero length, since we don't bother to represent
  	   the type field in the binary version */
b8cb464e4   Chris Ruffin   ihex: fix unused ...
284
285
  	if (write(outfd, zeroes, 6) != 6)
  		return 1;
8bd6b2229   David Woodhouse   ihex: add ihex2fw...
286
287
  	return 0;
  }