image.c 29 KB
Newer Older
drath's avatar
drath committed
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2007 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
6
7
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
8
9
10
 *   Copyright (C) 2008 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
11
12
13
 *   Copyright (C) 2009 by Franck Hereson                                  *
 *   franck.hereson@secad.fr                                               *
 *                                                                         *
drath's avatar
drath committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "image.h"
34
#include "target.h"
drath's avatar
drath committed
35
36
37
#include "log.h"


38
/* convert ELF header field to host endianness */
39
#define field16(elf,field)\
zwelch's avatar
zwelch committed
40
	((elf->endianness == ELFDATA2LSB)? \
41
		le_to_h_u16((uint8_t*)&field):be_to_h_u16((uint8_t*)&field))
42

43
#define field32(elf,field)\
zwelch's avatar
zwelch committed
44
	((elf->endianness == ELFDATA2LSB)? \
45
		le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field))
46

47
static int autodetect_image_type(image_t *image, const char *url)
48
49
50
{
	int retval;
	fileio_t fileio;
51
	uint32_t read_bytes;
52
	uint8_t buffer[9];
oharboe's avatar
oharboe committed
53

54
55
56
57
58
	/* read the first 4 bytes of image */
	if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
	{
		return retval;
	}
oharboe's avatar
oharboe committed
59
	retval = fileio_read(&fileio, 9, buffer, &read_bytes);
oharboe's avatar
oharboe committed
60

zwelch's avatar
zwelch committed
61
	if (retval == ERROR_OK)
62
	{
oharboe's avatar
oharboe committed
63
64
		if (read_bytes != 9)
		{
zwelch's avatar
zwelch committed
65
			retval = ERROR_FILEIO_OPERATION_FAILED;
oharboe's avatar
oharboe committed
66
		}
67
68
	}
	fileio_close(&fileio);
oharboe's avatar
oharboe committed
69

zwelch's avatar
zwelch committed
70
	if (retval != ERROR_OK)
oharboe's avatar
oharboe committed
71
		return retval;
72
73

	/* check header against known signatures */
zwelch's avatar
zwelch committed
74
	if (strncmp((char*)buffer,ELFMAG,SELFMAG) == 0)
75
	{
76
		LOG_DEBUG("ELF image detected.");
77
78
		image->type = IMAGE_ELF;
	}
79
80
81
82
83
84
85
86
	else if ((buffer[0]==':') /* record start byte */
		&&(isxdigit(buffer[1]))
		&&(isxdigit(buffer[2]))
		&&(isxdigit(buffer[3]))
		&&(isxdigit(buffer[4]))
		&&(isxdigit(buffer[5]))
		&&(isxdigit(buffer[6]))
		&&(buffer[7]=='0') /* record type : 00 -> 05 */
zwelch's avatar
zwelch committed
87
		&&(buffer[8]>='0') && (buffer[8]<'6'))
88
	{
89
		LOG_DEBUG("IHEX image detected.");
90
91
		image->type = IMAGE_IHEX;
	}
ntfreak's avatar
ntfreak committed
92
93
94
95
96
97
	else if ((buffer[0] == 'S') /* record start byte */
		&&(isxdigit(buffer[1]))
		&&(isxdigit(buffer[2]))
		&&(isxdigit(buffer[3]))
		&&(buffer[1] >= '0') && (buffer[1] < '9'))
	{
98
		LOG_DEBUG("S19 image detected.");
ntfreak's avatar
ntfreak committed
99
100
		image->type = IMAGE_SRECORD;
	}
101
102
103
104
105
106
107
108
	else
	{
		image->type = IMAGE_BINARY;
	}

	return ERROR_OK;
}

109
static int identify_image_type(image_t *image, const char *type_string, const char *url)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{
	if (type_string)
	{
		if (!strcmp(type_string, "bin"))
		{
			image->type = IMAGE_BINARY;
		}
		else if (!strcmp(type_string, "ihex"))
		{
			image->type = IMAGE_IHEX;
		}
		else if (!strcmp(type_string, "elf"))
		{
			image->type = IMAGE_ELF;
		}
		else if (!strcmp(type_string, "mem"))
		{
			image->type = IMAGE_MEMORY;
		}
ntfreak's avatar
ntfreak committed
129
130
131
132
		else if (!strcmp(type_string, "s19"))
		{
			image->type = IMAGE_SRECORD;
		}
133
134
135
136
		else if (!strcmp(type_string, "build"))
		{
			image->type = IMAGE_BUILDER;
		}
137
138
139
140
141
142
143
144
145
		else
		{
			return ERROR_IMAGE_TYPE_UNKNOWN;
		}
	}
	else
	{
		return autodetect_image_type(image, url);
	}
oharboe's avatar
oharboe committed
146

147
148
149
	return ERROR_OK;
}

150
static int image_ihex_buffer_complete(image_t *image)
drath's avatar
drath committed
151
152
153
{
	image_ihex_t *ihex = image->type_private;
	fileio_t *fileio = &ihex->fileio;
154
155
	uint32_t full_address = 0x0;
	uint32_t cooked_bytes;
156
	int i;
157
	char lpszLine[1023];
oharboe's avatar
oharboe committed
158

159
160
161
162
163
164
165
	/* we can't determine the number of sections that we'll have to create ahead of time,
	 * so we locally hold them until parsing is finished */
	image_section_t section[IMAGE_MAX_SECTIONS];

	ihex->buffer = malloc(fileio->size >> 1);
	cooked_bytes = 0x0;
	image->num_sections = 0;
166
	section[image->num_sections].private = &ihex->buffer[cooked_bytes];
167
168
169
	section[image->num_sections].base_address = 0x0;
	section[image->num_sections].size = 0x0;
	section[image->num_sections].flags = 0;
oharboe's avatar
oharboe committed
170

171
	while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
drath's avatar
drath committed
172
	{
173
174
175
176
		uint32_t count;
		uint32_t address;
		uint32_t record_type;
		uint32_t checksum;
177
		uint8_t cal_checksum = 0;
178
		uint32_t bytes_read = 0;
oharboe's avatar
oharboe committed
179

duane's avatar
duane committed
180
		if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32 , &count, &address, &record_type) != 3)
drath's avatar
drath committed
181
182
183
		{
			return ERROR_IMAGE_FORMAT_ERROR;
		}
184
		bytes_read += 9;
oharboe's avatar
oharboe committed
185

186
187
188
189
		cal_checksum += (uint8_t)count;
		cal_checksum += (uint8_t)(address >> 8);
		cal_checksum += (uint8_t)address;
		cal_checksum += (uint8_t)record_type;
oharboe's avatar
oharboe committed
190

191
		if (record_type == 0) /* Data Record */
drath's avatar
drath committed
192
193
194
		{
			if ((full_address & 0xffff) != address)
			{
195
196
197
198
199
200
201
				/* we encountered a nonconsecutive location, create a new section,
				 * unless the current section has zero size, in which case this specifies
				 * the current section's base address
				 */
				if (section[image->num_sections].size != 0)
				{
					image->num_sections++;
202
203
204
205
206
207
					if (image->num_sections >= IMAGE_MAX_SECTIONS)
					{
						/* too many sections */
						LOG_ERROR("Too many sections found in IHEX file");
						return ERROR_IMAGE_FORMAT_ERROR;
					}
208
209
					section[image->num_sections].size = 0x0;
					section[image->num_sections].flags = 0;
210
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
211
212
213
214
				}
				section[image->num_sections].base_address =
					(full_address & 0xffff0000) | address;
				full_address = (full_address & 0xffff0000) | address;
drath's avatar
drath committed
215
			}
oharboe's avatar
oharboe committed
216

drath's avatar
drath committed
217
218
			while (count-- > 0)
			{
219
220
				unsigned value;
				sscanf(&lpszLine[bytes_read], "%2x", &value);
221
222
				ihex->buffer[cooked_bytes] = (uint8_t)value;
				cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
223
				bytes_read += 2;
drath's avatar
drath committed
224
				cooked_bytes += 1;
225
				section[image->num_sections].size += 1;
drath's avatar
drath committed
226
227
228
				full_address++;
			}
		}
229
		else if (record_type == 1) /* End of File Record */
drath's avatar
drath committed
230
		{
231
232
			/* finish the current section */
			image->num_sections++;
oharboe's avatar
oharboe committed
233

234
235
236
237
			/* copy section information */
			image->sections = malloc(sizeof(image_section_t) * image->num_sections);
			for (i = 0; i < image->num_sections; i++)
			{
238
				image->sections[i].private = section[i].private;
239
				image->sections[i].base_address = section[i].base_address;
240
241
242
				image->sections[i].size = section[i].size;
				image->sections[i].flags = section[i].flags;
			}
oharboe's avatar
oharboe committed
243

drath's avatar
drath committed
244
245
			return ERROR_OK;
		}
246
247
		else if (record_type == 2) /* Linear Address Record */
		{
zwelch's avatar
zwelch committed
248
			uint16_t upper_address;
oharboe's avatar
oharboe committed
249

250
			sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
251
252
			cal_checksum += (uint8_t)(upper_address >> 8);
			cal_checksum += (uint8_t)upper_address;
253
			bytes_read += 4;
oharboe's avatar
oharboe committed
254

255
256
257
258
259
260
261
262
263
			if ((full_address >> 4) != upper_address)
			{
				/* we encountered a nonconsecutive location, create a new section,
				 * unless the current section has zero size, in which case this specifies
				 * the current section's base address
				 */
				if (section[image->num_sections].size != 0)
				{
					image->num_sections++;
264
265
266
267
268
269
					if (image->num_sections >= IMAGE_MAX_SECTIONS)
					{
						/* too many sections */
						LOG_ERROR("Too many sections found in IHEX file");
						return ERROR_IMAGE_FORMAT_ERROR;
					}
270
271
272
273
					section[image->num_sections].size = 0x0;
					section[image->num_sections].flags = 0;
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
				}
oharboe's avatar
oharboe committed
274
				section[image->num_sections].base_address =
275
276
277
278
					(full_address & 0xffff) | (upper_address << 4);
				full_address = (full_address & 0xffff) | (upper_address << 4);
			}
		}
279
280
		else if (record_type == 3) /* Start Segment Address Record */
		{
281
			uint32_t dummy;
oharboe's avatar
oharboe committed
282

283
284
285
286
			/* "Start Segment Address Record" will not be supported */
			/* but we must consume it, and do not create an error.  */
			while (count-- > 0)
			{
duane's avatar
duane committed
287
				sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
288
				cal_checksum += (uint8_t)dummy;
289
290
291
				bytes_read += 2;
			}
		}
292
		else if (record_type == 4) /* Extended Linear Address Record */
drath's avatar
drath committed
293
		{
zwelch's avatar
zwelch committed
294
			uint16_t upper_address;
oharboe's avatar
oharboe committed
295

296
			sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
297
298
			cal_checksum += (uint8_t)(upper_address >> 8);
			cal_checksum += (uint8_t)upper_address;
299
			bytes_read += 4;
oharboe's avatar
oharboe committed
300

drath's avatar
drath committed
301
302
			if ((full_address >> 16) != upper_address)
			{
303
304
305
306
307
308
309
				/* we encountered a nonconsecutive location, create a new section,
				 * unless the current section has zero size, in which case this specifies
				 * the current section's base address
				 */
				if (section[image->num_sections].size != 0)
				{
					image->num_sections++;
310
311
312
313
314
315
					if (image->num_sections >= IMAGE_MAX_SECTIONS)
					{
						/* too many sections */
						LOG_ERROR("Too many sections found in IHEX file");
						return ERROR_IMAGE_FORMAT_ERROR;
					}
316
317
					section[image->num_sections].size = 0x0;
					section[image->num_sections].flags = 0;
318
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
319
				}
oharboe's avatar
oharboe committed
320
				section[image->num_sections].base_address =
321
322
					(full_address & 0xffff) | (upper_address << 16);
				full_address = (full_address & 0xffff) | (upper_address << 16);
drath's avatar
drath committed
323
324
			}
		}
325
		else if (record_type == 5) /* Start Linear Address Record */
drath's avatar
drath committed
326
		{
327
			uint32_t start_address;
oharboe's avatar
oharboe committed
328

duane's avatar
duane committed
329
			sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
330
331
332
333
			cal_checksum += (uint8_t)(start_address >> 24);
			cal_checksum += (uint8_t)(start_address >> 16);
			cal_checksum += (uint8_t)(start_address >> 8);
			cal_checksum += (uint8_t)start_address;
334
			bytes_read += 8;
oharboe's avatar
oharboe committed
335

drath's avatar
drath committed
336
			image->start_address_set = 1;
337
			image->start_address = be_to_h_u32((uint8_t*)&start_address);
drath's avatar
drath committed
338
339
340
		}
		else
		{
duane's avatar
duane committed
341
		  LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
drath's avatar
drath committed
342
343
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
344

duane's avatar
duane committed
345
		sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
346
		bytes_read += 2;
oharboe's avatar
oharboe committed
347

348
		if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1))
ntfreak's avatar
ntfreak committed
349
350
		{
			/* checksum failed */
351
			LOG_ERROR("incorrect record checksum found in IHEX file");
ntfreak's avatar
ntfreak committed
352
353
			return ERROR_IMAGE_CHECKSUM;
		}
drath's avatar
drath committed
354
	}
oharboe's avatar
oharboe committed
355

356
	LOG_ERROR("premature end of IHEX file, no end-of-file record found");
drath's avatar
drath committed
357
358
359
	return ERROR_IMAGE_FORMAT_ERROR;
}

360
static int image_elf_read_headers(image_t *image)
361
362
{
	image_elf_t *elf = image->type_private;
363
364
	uint32_t read_bytes;
	uint32_t i,j;
365
366
367
368
	int retval;

	elf->header = malloc(sizeof(Elf32_Ehdr));

zwelch's avatar
zwelch committed
369
	if (elf->header == NULL)
370
371
372
373
374
	{
		LOG_ERROR("insufficient memory to perform operation ");
		return ERROR_FILEIO_OPERATION_FAILED;
	}

375
	if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
376
	{
377
		LOG_ERROR("cannot read ELF file header, read failed");
378
379
380
381
		return ERROR_FILEIO_OPERATION_FAILED;
	}
	if (read_bytes != sizeof(Elf32_Ehdr))
	{
382
		LOG_ERROR("cannot read ELF file header, only partially read");
383
384
385
		return ERROR_FILEIO_OPERATION_FAILED;
	}

zwelch's avatar
zwelch committed
386
	if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG) != 0)
387
	{
388
		LOG_ERROR("invalid ELF file, bad magic number");
389
390
		return ERROR_IMAGE_FORMAT_ERROR;
	}
391
	if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
392
	{
393
		LOG_ERROR("invalid ELF file, only 32bits files are supported");
394
395
396
397
		return ERROR_IMAGE_FORMAT_ERROR;
	}

	elf->endianness = elf->header->e_ident[EI_DATA];
zwelch's avatar
zwelch committed
398
399
	if ((elf->endianness != ELFDATA2LSB)
		 &&(elf->endianness != ELFDATA2MSB))
400
	{
401
		LOG_ERROR("invalid ELF file, unknown endianess setting");
402
403
404
		return ERROR_IMAGE_FORMAT_ERROR;
	}

405
	elf->segment_count = field16(elf,elf->header->e_phnum);
zwelch's avatar
zwelch committed
406
	if (elf->segment_count == 0)
407
	{
408
		LOG_ERROR("invalid ELF file, no program headers");
409
410
411
		return ERROR_IMAGE_FORMAT_ERROR;
	}

vpalatin's avatar
vpalatin committed
412
	if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
413
	{
414
		LOG_ERROR("cannot seek to ELF program header table, read failed");
415
416
417
		return retval;
	}

418
	elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
zwelch's avatar
zwelch committed
419
	if (elf->segments == NULL)
420
421
422
423
	{
		LOG_ERROR("insufficient memory to perform operation ");
		return ERROR_FILEIO_OPERATION_FAILED;
	}
424

425
	if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
426
	{
427
		LOG_ERROR("cannot read ELF segment headers, read failed");
428
429
		return retval;
	}
430
	if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
431
	{
432
		LOG_ERROR("cannot read ELF segment headers, only partially read");
433
434
435
		return ERROR_FILEIO_OPERATION_FAILED;
	}

ntfreak's avatar
ntfreak committed
436
	/* count useful segments (loadable), ignore BSS section */
437
	image->num_sections = 0;
zwelch's avatar
zwelch committed
438
	for (i = 0;i < elf->segment_count;i++)
ntfreak's avatar
ntfreak committed
439
		if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
440
441
442
			image->num_sections++;
	/* alloc and fill sections array with loadable segments */
	image->sections = malloc(image->num_sections * sizeof(image_section_t));
zwelch's avatar
zwelch committed
443
	for (i = 0,j = 0;i < elf->segment_count;i++)
444
	{
ntfreak's avatar
ntfreak committed
445
		if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
446
		{
447
448
			image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
			image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
449
			image->sections[j].private = &elf->segments[i];
450
			image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
451
452
453
			j++;
		}
	}
oharboe's avatar
oharboe committed
454

455
	image->start_address_set = 1;
456
	image->start_address = field32(elf,elf->header->e_entry);
457
458
459
460

	return ERROR_OK;
}

461
static int image_elf_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
462
463
464
{
	image_elf_t *elf = image->type_private;
	Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
465
	uint32_t read_size,really_read;
466
467
468
	int retval;

	*size_read = 0;
oharboe's avatar
oharboe committed
469

zwelch's avatar
zwelch committed
470
	LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz = 0x%" PRIx32 ")",section,offset,size);
471
472

	/* read initialized data in current segment if any */
zwelch's avatar
zwelch committed
473
	if (offset < field32(elf,segment->p_filesz))
474
475
	{
		/* maximal size present in file for the current segment */
476
		read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
duane's avatar
duane committed
477
		LOG_DEBUG("read elf: size = 0x%" PRIx32 " at 0x%" PRIx32 "",read_size,
zwelch's avatar
zwelch committed
478
			field32(elf,segment->p_offset) + offset);
479
		/* read initialized area of the segment */
zwelch's avatar
zwelch committed
480
		if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset) + offset)) != ERROR_OK)
481
		{
482
			LOG_ERROR("cannot find ELF segment content, seek failed");
483
484
485
486
			return retval;
		}
		if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
		{
487
			LOG_ERROR("cannot read ELF segment content, read failed");
488
489
490
491
492
493
494
495
496
497
			return retval;
		}
		buffer += read_size;
		size -= read_size;
		offset += read_size;
		*size_read += read_size;
		/* need more data ? */
		if (!size)
			return ERROR_OK;
	}
oharboe's avatar
oharboe committed
498

499
500
501
	return ERROR_OK;
}

502
static int image_mot_buffer_complete(image_t *image)
ntfreak's avatar
ntfreak committed
503
504
505
{
	image_mot_t *mot = image->type_private;
	fileio_t *fileio = &mot->fileio;
506
507
	uint32_t full_address = 0x0;
	uint32_t cooked_bytes;
ntfreak's avatar
ntfreak committed
508
	int i;
509
	char lpszLine[1023];
oharboe's avatar
oharboe committed
510

ntfreak's avatar
ntfreak committed
511
512
513
	/* we can't determine the number of sections that we'll have to create ahead of time,
	 * so we locally hold them until parsing is finished */
	image_section_t section[IMAGE_MAX_SECTIONS];
oharboe's avatar
oharboe committed
514

ntfreak's avatar
ntfreak committed
515
516
517
518
519
520
521
	mot->buffer = malloc(fileio->size >> 1);
	cooked_bytes = 0x0;
	image->num_sections = 0;
	section[image->num_sections].private = &mot->buffer[cooked_bytes];
	section[image->num_sections].base_address = 0x0;
	section[image->num_sections].size = 0x0;
	section[image->num_sections].flags = 0;
oharboe's avatar
oharboe committed
522

523
	while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
ntfreak's avatar
ntfreak committed
524
	{
525
526
527
528
		uint32_t count;
		uint32_t address;
		uint32_t record_type;
		uint32_t checksum;
529
		uint8_t cal_checksum = 0;
530
		uint32_t bytes_read = 0;
oharboe's avatar
oharboe committed
531

ntfreak's avatar
ntfreak committed
532
		/* get record type and record length */
duane's avatar
duane committed
533
		if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32 , &record_type, &count) != 2)
ntfreak's avatar
ntfreak committed
534
535
536
		{
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
537

538
		bytes_read += 4;
539
		cal_checksum += (uint8_t)count;
oharboe's avatar
oharboe committed
540

ntfreak's avatar
ntfreak committed
541
542
		/* skip checksum byte */
		count -=1;
oharboe's avatar
oharboe committed
543

ntfreak's avatar
ntfreak committed
544
545
546
547
		if (record_type == 0)
		{
			/* S0 - starting record (optional) */
			int iValue;
oharboe's avatar
oharboe committed
548

ntfreak's avatar
ntfreak committed
549
			while (count-- > 0) {
550
				sscanf(&lpszLine[bytes_read], "%2x", &iValue);
551
				cal_checksum += (uint8_t)iValue;
552
				bytes_read += 2;
ntfreak's avatar
ntfreak committed
553
554
555
556
			}
		}
		else if (record_type >= 1 && record_type <= 3)
		{
557
			switch (record_type)
ntfreak's avatar
ntfreak committed
558
559
560
			{
				case 1:
					/* S1 - 16 bit address data record */
duane's avatar
duane committed
561
					sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
562
563
					cal_checksum += (uint8_t)(address >> 8);
					cal_checksum += (uint8_t)address;
564
					bytes_read += 4;
ntfreak's avatar
ntfreak committed
565
566
					count -=2;
					break;
oharboe's avatar
oharboe committed
567

ntfreak's avatar
ntfreak committed
568
569
				case 2:
					/* S2 - 24 bit address data record */
duane's avatar
duane committed
570
					sscanf(&lpszLine[bytes_read], "%6" SCNx32 , &address);
571
572
573
					cal_checksum += (uint8_t)(address >> 16);
					cal_checksum += (uint8_t)(address >> 8);
					cal_checksum += (uint8_t)address;
574
					bytes_read += 6;
ntfreak's avatar
ntfreak committed
575
576
					count -=3;
					break;
oharboe's avatar
oharboe committed
577

ntfreak's avatar
ntfreak committed
578
579
				case 3:
					/* S3 - 32 bit address data record */
duane's avatar
duane committed
580
					sscanf(&lpszLine[bytes_read], "%8" SCNx32 , &address);
581
582
583
584
					cal_checksum += (uint8_t)(address >> 24);
					cal_checksum += (uint8_t)(address >> 16);
					cal_checksum += (uint8_t)(address >> 8);
					cal_checksum += (uint8_t)address;
585
					bytes_read += 8;
ntfreak's avatar
ntfreak committed
586
587
					count -=4;
					break;
oharboe's avatar
oharboe committed
588

ntfreak's avatar
ntfreak committed
589
			}
oharboe's avatar
oharboe committed
590

ntfreak's avatar
ntfreak committed
591
592
593
594
595
596
597
598
599
600
601
602
603
			if (full_address != address)
			{
				/* we encountered a nonconsecutive location, create a new section,
				 * unless the current section has zero size, in which case this specifies
				 * the current section's base address
				 */
				if (section[image->num_sections].size != 0)
				{
					image->num_sections++;
					section[image->num_sections].size = 0x0;
					section[image->num_sections].flags = 0;
					section[image->num_sections].private = &mot->buffer[cooked_bytes];
				}
604
605
				section[image->num_sections].base_address = address;
				full_address = address;
ntfreak's avatar
ntfreak committed
606
			}
oharboe's avatar
oharboe committed
607

ntfreak's avatar
ntfreak committed
608
609
			while (count-- > 0)
			{
610
611
				unsigned value;
				sscanf(&lpszLine[bytes_read], "%2x", &value);
612
613
				mot->buffer[cooked_bytes] = (uint8_t)value;
				cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
614
				bytes_read += 2;
ntfreak's avatar
ntfreak committed
615
616
617
618
619
				cooked_bytes += 1;
				section[image->num_sections].size += 1;
				full_address++;
			}
		}
620
621
622
		else if (record_type == 5)
		{
			/* S5 is the data count record, we ignore it */
623
			uint32_t dummy;
oharboe's avatar
oharboe committed
624

625
626
			while (count-- > 0)
			{
duane's avatar
duane committed
627
				sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
628
				cal_checksum += (uint8_t)dummy;
629
630
631
				bytes_read += 2;
			}
		}
ntfreak's avatar
ntfreak committed
632
633
634
635
		else if (record_type >= 7 && record_type <= 9)
		{
			/* S7, S8, S9 - ending records for 32, 24 and 16bit */
			image->num_sections++;
oharboe's avatar
oharboe committed
636

ntfreak's avatar
ntfreak committed
637
638
639
640
641
			/* copy section information */
			image->sections = malloc(sizeof(image_section_t) * image->num_sections);
			for (i = 0; i < image->num_sections; i++)
			{
				image->sections[i].private = section[i].private;
642
				image->sections[i].base_address = section[i].base_address;
ntfreak's avatar
ntfreak committed
643
644
645
				image->sections[i].size = section[i].size;
				image->sections[i].flags = section[i].flags;
			}
oharboe's avatar
oharboe committed
646

ntfreak's avatar
ntfreak committed
647
648
649
650
			return ERROR_OK;
		}
		else
		{
duane's avatar
duane committed
651
		  LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
ntfreak's avatar
ntfreak committed
652
653
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
654

ntfreak's avatar
ntfreak committed
655
		/* account for checksum, will always be 0xFF */
duane's avatar
duane committed
656
		sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
657
		cal_checksum += (uint8_t)checksum;
658
		bytes_read += 2;
oharboe's avatar
oharboe committed
659

660
		if (cal_checksum != 0xFF)
ntfreak's avatar
ntfreak committed
661
662
		{
			/* checksum failed */
663
			LOG_ERROR("incorrect record checksum found in S19 file");
ntfreak's avatar
ntfreak committed
664
665
666
			return ERROR_IMAGE_CHECKSUM;
		}
	}
oharboe's avatar
oharboe committed
667

668
	LOG_ERROR("premature end of S19 file, no end-of-file record found");
ntfreak's avatar
ntfreak committed
669
670
671
	return ERROR_IMAGE_FORMAT_ERROR;
}

672
int image_open(image_t *image, const char *url, const char *type_string)
drath's avatar
drath committed
673
674
{
	int retval = ERROR_OK;
oharboe's avatar
oharboe committed
675

676
	if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
677
678
	{
		return retval;
679
	}
oharboe's avatar
oharboe committed
680

drath's avatar
drath committed
681
682
683
	if (image->type == IMAGE_BINARY)
	{
		image_binary_t *image_binary;
oharboe's avatar
oharboe committed
684

drath's avatar
drath committed
685
		image_binary = image->type_private = malloc(sizeof(image_binary_t));
oharboe's avatar
oharboe committed
686

687
		if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
drath's avatar
drath committed
688
689
690
		{
			return retval;
		}
oharboe's avatar
oharboe committed
691

692
693
694
695
696
		image->num_sections = 1;
		image->sections = malloc(sizeof(image_section_t));
		image->sections[0].base_address = 0x0;
		image->sections[0].size = image_binary->fileio.size;
		image->sections[0].flags = 0;
drath's avatar
drath committed
697
698
699
700
	}
	else if (image->type == IMAGE_IHEX)
	{
		image_ihex_t *image_ihex;
oharboe's avatar
oharboe committed
701

drath's avatar
drath committed
702
		image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
oharboe's avatar
oharboe committed
703

drath's avatar
drath committed
704
705
706
707
		if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
708

drath's avatar
drath committed
709
710
		if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
		{
711
			LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
drath's avatar
drath committed
712
713
714
715
			fileio_close(&image_ihex->fileio);
			return retval;
		}
	}
716
717
718
	else if (image->type == IMAGE_ELF)
	{
		image_elf_t *image_elf;
oharboe's avatar
oharboe committed
719

720
		image_elf = image->type_private = malloc(sizeof(image_elf_t));
oharboe's avatar
oharboe committed
721

722
723
724
725
		if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
726

727
728
729
730
731
732
		if ((retval = image_elf_read_headers(image)) != ERROR_OK)
		{
			fileio_close(&image_elf->fileio);
			return retval;
		}
	}
drath's avatar
drath committed
733
734
	else if (image->type == IMAGE_MEMORY)
	{
735
736
737
		target_t *target = get_target(url);

		if (target == NULL)
738
		{
739
			LOG_ERROR("target '%s' not defined", url);
740
741
742
			return ERROR_FAIL;
		}

drath's avatar
drath committed
743
		image_memory_t *image_memory;
oharboe's avatar
oharboe committed
744

745
746
747
748
749
		image->num_sections = 1;
		image->sections = malloc(sizeof(image_section_t));
		image->sections[0].base_address = 0x0;
		image->sections[0].size = 0xffffffff;
		image->sections[0].flags = 0;
oharboe's avatar
oharboe committed
750

drath's avatar
drath committed
751
		image_memory = image->type_private = malloc(sizeof(image_memory_t));
oharboe's avatar
oharboe committed
752

753
		image_memory->target = target;
754
755
		image_memory->cache = NULL;
		image_memory->cache_address = 0x0;
drath's avatar
drath committed
756
	}
ntfreak's avatar
ntfreak committed
757
758
759
	else if (image->type == IMAGE_SRECORD)
	{
		image_mot_t *image_mot;
oharboe's avatar
oharboe committed
760

ntfreak's avatar
ntfreak committed
761
		image_mot = image->type_private = malloc(sizeof(image_mot_t));
oharboe's avatar
oharboe committed
762

ntfreak's avatar
ntfreak committed
763
764
765
766
		if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
767

ntfreak's avatar
ntfreak committed
768
769
		if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
		{
770
			LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
ntfreak's avatar
ntfreak committed
771
772
773
774
			fileio_close(&image_mot->fileio);
			return retval;
		}
	}
775
776
777
778
779
780
	else if (image->type == IMAGE_BUILDER)
	{
		image->num_sections = 0;
		image->sections = NULL;
		image->type_private = NULL;
	}
781
782
783

	if (image->base_address_set)
	{
784
		/* relocate */
785
		int section;
zwelch's avatar
zwelch committed
786
		for (section = 0; section < image->num_sections; section++)
787
		{
zwelch's avatar
zwelch committed
788
			image->sections[section].base_address += image->base_address;
789
		}
790
791
792
		/* we're done relocating. The two statements below are mainly
		 * for documenation purposes: stop anyone from empirically
		 * thinking they should use these values henceforth. */
zwelch's avatar
zwelch committed
793
794
		image->base_address = 0;
		image->base_address_set = 0;
795
	}
oharboe's avatar
oharboe committed
796

drath's avatar
drath committed
797
798
799
	return retval;
};

800
int image_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
drath's avatar
drath committed
801
802
{
	int retval;
803
804
805
806

	/* don't read past the end of a section */
	if (offset + size > image->sections[section].size)
	{
duane's avatar
duane committed
807
		LOG_DEBUG("read past end of section: 0x%8.8" PRIx32 " + 0x%8.8" PRIx32 " > 0x%8.8" PRIx32 "",
808
809
810
811
				offset, size, image->sections[section].size);
		return ERROR_INVALID_ARGUMENTS;
	}

drath's avatar
drath committed
812
813
814
	if (image->type == IMAGE_BINARY)
	{
		image_binary_t *image_binary = image->type_private;
oharboe's avatar
oharboe committed
815

816
817
818
		/* only one section in a plain binary */
		if (section != 0)
			return ERROR_INVALID_ARGUMENTS;
oharboe's avatar
oharboe committed
819

820
821
822
823
824
		/* seek to offset */
		if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
825

826
		/* return requested bytes */
drath's avatar
drath committed
827
828
829
830
831
832
833
		if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
		{
			return retval;
		}
	}
	else if (image->type == IMAGE_IHEX)
	{
834
		memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
drath's avatar
drath committed
835
		*size_read = size;
oharboe's avatar
oharboe committed
836

drath's avatar
drath committed
837
838
		return ERROR_OK;
	}
839
840
841
842
	else if (image->type == IMAGE_ELF)
	{
		return image_elf_read_section(image, section, offset, size, buffer, size_read);
	}
drath's avatar
drath committed
843
844
	else if (image->type == IMAGE_MEMORY)
	{
845
		image_memory_t *image_memory = image->type_private;
846
		uint32_t address = image->sections[section].base_address + offset;
oharboe's avatar
oharboe committed
847

848
		*size_read = 0;
oharboe's avatar
oharboe committed
849

850
851
		while ((size - *size_read) > 0)
		{
852
			uint32_t size_in_cache;
oharboe's avatar
oharboe committed
853

854
855
856
857
858
859
			if (!image_memory->cache
				|| (address < image_memory->cache_address)
				|| (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
			{
				if (!image_memory->cache)
					image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
oharboe's avatar
oharboe committed
860

861
862
863
864
				if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
					IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
				{
					free(image_memory->cache);
865
					image_memory->cache = NULL;
866
867
868
869
					return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
				}
				image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
			}
oharboe's avatar
oharboe committed
870

871
			size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
oharboe's avatar
oharboe committed
872

873
874
875
			memcpy(buffer + *size_read,
				image_memory->cache + (address - image_memory->cache_address),
				(size_in_cache > size) ? size : size_in_cache
876
);
oharboe's avatar
oharboe committed
877

878
879
880
			*size_read += (size_in_cache > size) ? size : size_in_cache;
			address += (size_in_cache > size) ? size : size_in_cache;
		}
drath's avatar
drath committed
881
	}
ntfreak's avatar
ntfreak committed
882
883