image.c 28.1 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                                                  *
 *                                                                         *
drath's avatar
drath committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *   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"
31
#include "target.h"
drath's avatar
drath committed
32
33
34
#include "log.h"


35
/* convert ELF header field to host endianness */
36
37
#define field16(elf,field)\
	((elf->endianness==ELFDATA2LSB)? \
38
		le_to_h_u16((uint8_t*)&field):be_to_h_u16((uint8_t*)&field))
39

40
41
#define field32(elf,field)\
	((elf->endianness==ELFDATA2LSB)? \
42
		le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field))
43

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

51
52
53
54
55
	/* 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
56
	retval = fileio_read(&fileio, 9, buffer, &read_bytes);
oharboe's avatar
oharboe committed
57

oharboe's avatar
oharboe committed
58
	if (retval==ERROR_OK)
59
	{
oharboe's avatar
oharboe committed
60
61
62
63
		if (read_bytes != 9)
		{
			retval=ERROR_FILEIO_OPERATION_FAILED;
		}
64
65
	}
	fileio_close(&fileio);
oharboe's avatar
oharboe committed
66

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

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

	return ERROR_OK;
}

106
static int identify_image_type(image_t *image, char *type_string, char *url)
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{
	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
126
127
128
129
		else if (!strcmp(type_string, "s19"))
		{
			image->type = IMAGE_SRECORD;
		}
130
131
132
133
		else if (!strcmp(type_string, "build"))
		{
			image->type = IMAGE_BUILDER;
		}
134
135
136
137
138
139
140
141
142
		else
		{
			return ERROR_IMAGE_TYPE_UNKNOWN;
		}
	}
	else
	{
		return autodetect_image_type(image, url);
	}
oharboe's avatar
oharboe committed
143

144
145
146
	return ERROR_OK;
}

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

156
157
158
159
160
161
162
	/* 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;
163
	section[image->num_sections].private = &ihex->buffer[cooked_bytes];
164
165
166
	section[image->num_sections].base_address = 0x0;
	section[image->num_sections].size = 0x0;
	section[image->num_sections].flags = 0;
oharboe's avatar
oharboe committed
167

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

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

183
184
185
186
		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
187

188
		if (record_type == 0) /* Data Record */
drath's avatar
drath committed
189
190
191
		{
			if ((full_address & 0xffff) != address)
			{
192
193
194
195
196
197
198
199
200
				/* 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;
201
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
202
203
204
205
				}
				section[image->num_sections].base_address =
					(full_address & 0xffff0000) | address;
				full_address = (full_address & 0xffff0000) | address;
drath's avatar
drath committed
206
			}
oharboe's avatar
oharboe committed
207

drath's avatar
drath committed
208
209
			while (count-- > 0)
			{
210
211
				unsigned value;
				sscanf(&lpszLine[bytes_read], "%2x", &value);
212
213
				ihex->buffer[cooked_bytes] = (uint8_t)value;
				cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
214
				bytes_read += 2;
drath's avatar
drath committed
215
				cooked_bytes += 1;
216
				section[image->num_sections].size += 1;
drath's avatar
drath committed
217
218
219
				full_address++;
			}
		}
220
		else if (record_type == 1) /* End of File Record */
drath's avatar
drath committed
221
		{
222
223
			/* finish the current section */
			image->num_sections++;
oharboe's avatar
oharboe committed
224

225
226
227
228
			/* copy section information */
			image->sections = malloc(sizeof(image_section_t) * image->num_sections);
			for (i = 0; i < image->num_sections; i++)
			{
229
				image->sections[i].private = section[i].private;
230
				image->sections[i].base_address = section[i].base_address;
231
232
233
				image->sections[i].size = section[i].size;
				image->sections[i].flags = section[i].flags;
			}
oharboe's avatar
oharboe committed
234

drath's avatar
drath committed
235
236
			return ERROR_OK;
		}
237
238
		else if (record_type == 2) /* Linear Address Record */
		{
zwelch's avatar
zwelch committed
239
			uint16_t upper_address;
oharboe's avatar
oharboe committed
240

241
			sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
242
243
			cal_checksum += (uint8_t)(upper_address >> 8);
			cal_checksum += (uint8_t)upper_address;
244
			bytes_read += 4;
oharboe's avatar
oharboe committed
245

246
247
248
249
250
251
252
253
254
255
256
257
258
			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++;
					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
259
				section[image->num_sections].base_address =
260
261
262
263
					(full_address & 0xffff) | (upper_address << 4);
				full_address = (full_address & 0xffff) | (upper_address << 4);
			}
		}
264
265
		else if (record_type == 3) /* Start Segment Address Record */
		{
266
			uint32_t dummy;
oharboe's avatar
oharboe committed
267

268
269
270
271
			/* "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
272
				sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
273
				cal_checksum += (uint8_t)dummy;
274
275
276
				bytes_read += 2;
			}
		}
277
		else if (record_type == 4) /* Extended Linear Address Record */
drath's avatar
drath committed
278
		{
zwelch's avatar
zwelch committed
279
			uint16_t upper_address;
oharboe's avatar
oharboe committed
280

281
			sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
282
283
			cal_checksum += (uint8_t)(upper_address >> 8);
			cal_checksum += (uint8_t)upper_address;
284
			bytes_read += 4;
oharboe's avatar
oharboe committed
285

drath's avatar
drath committed
286
287
			if ((full_address >> 16) != upper_address)
			{
288
289
290
291
292
293
294
295
296
				/* 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;
297
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
298
				}
oharboe's avatar
oharboe committed
299
				section[image->num_sections].base_address =
300
301
					(full_address & 0xffff) | (upper_address << 16);
				full_address = (full_address & 0xffff) | (upper_address << 16);
drath's avatar
drath committed
302
303
			}
		}
304
		else if (record_type == 5) /* Start Linear Address Record */
drath's avatar
drath committed
305
		{
306
			uint32_t start_address;
oharboe's avatar
oharboe committed
307

duane's avatar
duane committed
308
			sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
309
310
311
312
			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;
313
			bytes_read += 8;
oharboe's avatar
oharboe committed
314

drath's avatar
drath committed
315
			image->start_address_set = 1;
316
			image->start_address = be_to_h_u32((uint8_t*)&start_address);
drath's avatar
drath committed
317
318
319
		}
		else
		{
duane's avatar
duane committed
320
		  LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
drath's avatar
drath committed
321
322
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
323

duane's avatar
duane committed
324
		sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
325
		bytes_read += 2;
oharboe's avatar
oharboe committed
326

327
		if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1))
ntfreak's avatar
ntfreak committed
328
329
		{
			/* checksum failed */
330
			LOG_ERROR("incorrect record checksum found in IHEX file");
ntfreak's avatar
ntfreak committed
331
332
			return ERROR_IMAGE_CHECKSUM;
		}
drath's avatar
drath committed
333
	}
oharboe's avatar
oharboe committed
334

335
	LOG_ERROR("premature end of IHEX file, no end-of-file record found");
drath's avatar
drath committed
336
337
338
	return ERROR_IMAGE_FORMAT_ERROR;
}

339
static int image_elf_read_headers(image_t *image)
340
341
{
	image_elf_t *elf = image->type_private;
342
343
	uint32_t read_bytes;
	uint32_t i,j;
344
345
346
347
	int retval;

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

zwelch's avatar
zwelch committed
348
	if (elf->header == NULL)
349
350
351
352
353
	{
		LOG_ERROR("insufficient memory to perform operation ");
		return ERROR_FILEIO_OPERATION_FAILED;
	}

354
	if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
355
	{
356
		LOG_ERROR("cannot read ELF file header, read failed");
357
358
359
360
		return ERROR_FILEIO_OPERATION_FAILED;
	}
	if (read_bytes != sizeof(Elf32_Ehdr))
	{
361
		LOG_ERROR("cannot read ELF file header, only partially read");
362
363
364
		return ERROR_FILEIO_OPERATION_FAILED;
	}

zwelch's avatar
zwelch committed
365
	if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG) != 0)
366
	{
367
		LOG_ERROR("invalid ELF file, bad magic number");
368
369
		return ERROR_IMAGE_FORMAT_ERROR;
	}
370
	if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
371
	{
372
		LOG_ERROR("invalid ELF file, only 32bits files are supported");
373
374
375
376
		return ERROR_IMAGE_FORMAT_ERROR;
	}

	elf->endianness = elf->header->e_ident[EI_DATA];
zwelch's avatar
zwelch committed
377
378
	if ((elf->endianness != ELFDATA2LSB)
		 &&(elf->endianness != ELFDATA2MSB))
379
	{
380
		LOG_ERROR("invalid ELF file, unknown endianess setting");
381
382
383
		return ERROR_IMAGE_FORMAT_ERROR;
	}

384
385
	elf->segment_count = field16(elf,elf->header->e_phnum);
	if (elf->segment_count==0)
386
	{
387
		LOG_ERROR("invalid ELF file, no program headers");
388
389
390
		return ERROR_IMAGE_FORMAT_ERROR;
	}

vpalatin's avatar
vpalatin committed
391
	if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
392
	{
393
		LOG_ERROR("cannot seek to ELF program header table, read failed");
394
395
396
		return retval;
	}

397
	elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
zwelch's avatar
zwelch committed
398
	if (elf->segments == NULL)
399
400
401
402
	{
		LOG_ERROR("insufficient memory to perform operation ");
		return ERROR_FILEIO_OPERATION_FAILED;
	}
403

404
	if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
405
	{
406
		LOG_ERROR("cannot read ELF segment headers, read failed");
407
408
		return retval;
	}
409
	if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
410
	{
411
		LOG_ERROR("cannot read ELF segment headers, only partially read");
412
413
414
		return ERROR_FILEIO_OPERATION_FAILED;
	}

ntfreak's avatar
ntfreak committed
415
	/* count useful segments (loadable), ignore BSS section */
416
	image->num_sections = 0;
417
	for (i=0;i<elf->segment_count;i++)
ntfreak's avatar
ntfreak committed
418
		if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
419
420
421
			image->num_sections++;
	/* alloc and fill sections array with loadable segments */
	image->sections = malloc(image->num_sections * sizeof(image_section_t));
422
	for (i=0,j=0;i<elf->segment_count;i++)
423
	{
ntfreak's avatar
ntfreak committed
424
		if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
425
		{
426
427
			image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
			image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
428
			image->sections[j].private = &elf->segments[i];
429
			image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
430
431
432
			j++;
		}
	}
oharboe's avatar
oharboe committed
433

434
	image->start_address_set = 1;
435
	image->start_address = field32(elf,elf->header->e_entry);
436
437
438
439

	return ERROR_OK;
}

440
static int image_elf_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
441
442
443
{
	image_elf_t *elf = image->type_private;
	Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
444
	uint32_t read_size,really_read;
445
446
447
	int retval;

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

duane's avatar
duane committed
449
	LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz=0x%" PRIx32 ")",section,offset,size);
450
451

	/* read initialized data in current segment if any */
452
	if (offset<field32(elf,segment->p_filesz))
453
454
	{
		/* maximal size present in file for the current segment */
455
		read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
duane's avatar
duane committed
456
		LOG_DEBUG("read elf: size = 0x%" PRIx32 " at 0x%" PRIx32 "",read_size,
457
			field32(elf,segment->p_offset)+offset);
458
		/* read initialized area of the segment */
459
		if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
460
		{
461
			LOG_ERROR("cannot find ELF segment content, seek failed");
462
463
464
465
			return retval;
		}
		if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
		{
466
			LOG_ERROR("cannot read ELF segment content, read failed");
467
468
469
470
471
472
473
474
475
476
			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
477

478
479
480
	return ERROR_OK;
}

481
static int image_mot_buffer_complete(image_t *image)
ntfreak's avatar
ntfreak committed
482
483
484
{
	image_mot_t *mot = image->type_private;
	fileio_t *fileio = &mot->fileio;
485
486
	uint32_t full_address = 0x0;
	uint32_t cooked_bytes;
ntfreak's avatar
ntfreak committed
487
	int i;
488
	char lpszLine[1023];
oharboe's avatar
oharboe committed
489

ntfreak's avatar
ntfreak committed
490
491
492
	/* 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
493

ntfreak's avatar
ntfreak committed
494
495
496
497
498
499
500
	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
501

502
	while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
ntfreak's avatar
ntfreak committed
503
	{
504
505
506
507
		uint32_t count;
		uint32_t address;
		uint32_t record_type;
		uint32_t checksum;
508
		uint8_t cal_checksum = 0;
509
		uint32_t bytes_read = 0;
oharboe's avatar
oharboe committed
510

ntfreak's avatar
ntfreak committed
511
		/* get record type and record length */
duane's avatar
duane committed
512
		if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32 , &record_type, &count) != 2)
ntfreak's avatar
ntfreak committed
513
514
515
		{
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
516

517
		bytes_read += 4;
518
		cal_checksum += (uint8_t)count;
oharboe's avatar
oharboe committed
519

ntfreak's avatar
ntfreak committed
520
521
		/* skip checksum byte */
		count -=1;
oharboe's avatar
oharboe committed
522

ntfreak's avatar
ntfreak committed
523
524
525
526
		if (record_type == 0)
		{
			/* S0 - starting record (optional) */
			int iValue;
oharboe's avatar
oharboe committed
527

ntfreak's avatar
ntfreak committed
528
			while (count-- > 0) {
529
				sscanf(&lpszLine[bytes_read], "%2x", &iValue);
530
				cal_checksum += (uint8_t)iValue;
531
				bytes_read += 2;
ntfreak's avatar
ntfreak committed
532
533
534
535
			}
		}
		else if (record_type >= 1 && record_type <= 3)
		{
zwelch's avatar
zwelch committed
536
			switch ( record_type )
ntfreak's avatar
ntfreak committed
537
538
539
			{
				case 1:
					/* S1 - 16 bit address data record */
duane's avatar
duane committed
540
					sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
541
542
					cal_checksum += (uint8_t)(address >> 8);
					cal_checksum += (uint8_t)address;
543
					bytes_read += 4;
ntfreak's avatar
ntfreak committed
544
545
					count -=2;
					break;
oharboe's avatar
oharboe committed
546

ntfreak's avatar
ntfreak committed
547
548
				case 2:
					/* S2 - 24 bit address data record */
duane's avatar
duane committed
549
					sscanf(&lpszLine[bytes_read], "%6" SCNx32 , &address);
550
551
552
					cal_checksum += (uint8_t)(address >> 16);
					cal_checksum += (uint8_t)(address >> 8);
					cal_checksum += (uint8_t)address;
553
					bytes_read += 6;
ntfreak's avatar
ntfreak committed
554
555
					count -=3;
					break;
oharboe's avatar
oharboe committed
556

ntfreak's avatar
ntfreak committed
557
558
				case 3:
					/* S3 - 32 bit address data record */
duane's avatar
duane committed
559
					sscanf(&lpszLine[bytes_read], "%8" SCNx32 , &address);
560
561
562
563
					cal_checksum += (uint8_t)(address >> 24);
					cal_checksum += (uint8_t)(address >> 16);
					cal_checksum += (uint8_t)(address >> 8);
					cal_checksum += (uint8_t)address;
564
					bytes_read += 8;
ntfreak's avatar
ntfreak committed
565
566
					count -=4;
					break;
oharboe's avatar
oharboe committed
567

ntfreak's avatar
ntfreak committed
568
			}
oharboe's avatar
oharboe committed
569

ntfreak's avatar
ntfreak committed
570
571
572
573
574
575
576
577
578
579
580
581
582
			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];
				}
583
584
				section[image->num_sections].base_address = address;
				full_address = address;
ntfreak's avatar
ntfreak committed
585
			}
oharboe's avatar
oharboe committed
586

ntfreak's avatar
ntfreak committed
587
588
			while (count-- > 0)
			{
589
590
				unsigned value;
				sscanf(&lpszLine[bytes_read], "%2x", &value);
591
592
				mot->buffer[cooked_bytes] = (uint8_t)value;
				cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
593
				bytes_read += 2;
ntfreak's avatar
ntfreak committed
594
595
596
597
598
				cooked_bytes += 1;
				section[image->num_sections].size += 1;
				full_address++;
			}
		}
599
600
601
		else if (record_type == 5)
		{
			/* S5 is the data count record, we ignore it */
602
			uint32_t dummy;
oharboe's avatar
oharboe committed
603

604
605
			while (count-- > 0)
			{
duane's avatar
duane committed
606
				sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
607
				cal_checksum += (uint8_t)dummy;
608
609
610
				bytes_read += 2;
			}
		}
ntfreak's avatar
ntfreak committed
611
612
613
614
		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
615

ntfreak's avatar
ntfreak committed
616
617
618
619
620
			/* 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;
621
				image->sections[i].base_address = section[i].base_address;
ntfreak's avatar
ntfreak committed
622
623
624
				image->sections[i].size = section[i].size;
				image->sections[i].flags = section[i].flags;
			}
oharboe's avatar
oharboe committed
625

ntfreak's avatar
ntfreak committed
626
627
628
629
			return ERROR_OK;
		}
		else
		{
duane's avatar
duane committed
630
		  LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
ntfreak's avatar
ntfreak committed
631
632
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
633

ntfreak's avatar
ntfreak committed
634
		/* account for checksum, will always be 0xFF */
duane's avatar
duane committed
635
		sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
636
		cal_checksum += (uint8_t)checksum;
637
		bytes_read += 2;
oharboe's avatar
oharboe committed
638

zwelch's avatar
zwelch committed
639
		if ( cal_checksum != 0xFF )
ntfreak's avatar
ntfreak committed
640
641
		{
			/* checksum failed */
642
			LOG_ERROR("incorrect record checksum found in S19 file");
ntfreak's avatar
ntfreak committed
643
644
645
			return ERROR_IMAGE_CHECKSUM;
		}
	}
oharboe's avatar
oharboe committed
646

647
	LOG_ERROR("premature end of S19 file, no end-of-file record found");
ntfreak's avatar
ntfreak committed
648
649
650
	return ERROR_IMAGE_FORMAT_ERROR;
}

651
int image_open(image_t *image, char *url, char *type_string)
drath's avatar
drath committed
652
653
{
	int retval = ERROR_OK;
oharboe's avatar
oharboe committed
654

655
	if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
656
657
	{
		return retval;
658
	}
oharboe's avatar
oharboe committed
659

drath's avatar
drath committed
660
661
662
	if (image->type == IMAGE_BINARY)
	{
		image_binary_t *image_binary;
oharboe's avatar
oharboe committed
663

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

666
		if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
drath's avatar
drath committed
667
668
669
		{
			return retval;
		}
oharboe's avatar
oharboe committed
670

671
672
673
674
675
		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
676
677
678
679
	}
	else if (image->type == IMAGE_IHEX)
	{
		image_ihex_t *image_ihex;
oharboe's avatar
oharboe committed
680

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

drath's avatar
drath committed
683
684
685
686
		if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
687

drath's avatar
drath committed
688
689
		if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
		{
690
			LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
drath's avatar
drath committed
691
692
693
694
			fileio_close(&image_ihex->fileio);
			return retval;
		}
	}
695
696
697
	else if (image->type == IMAGE_ELF)
	{
		image_elf_t *image_elf;
oharboe's avatar
oharboe committed
698

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

701
702
703
704
		if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
705

706
707
708
709
710
711
		if ((retval = image_elf_read_headers(image)) != ERROR_OK)
		{
			fileio_close(&image_elf->fileio);
			return retval;
		}
	}
drath's avatar
drath committed
712
713
	else if (image->type == IMAGE_MEMORY)
	{
714
715
716
		target_t *target = get_target(url);

		if (target == NULL)
717
		{
718
			LOG_ERROR("target '%s' not defined", url);
719
720
721
			return ERROR_FAIL;
		}

drath's avatar
drath committed
722
		image_memory_t *image_memory;
oharboe's avatar
oharboe committed
723

724
725
726
727
728
		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
729

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

732
		image_memory->target = target;
733
734
		image_memory->cache = NULL;
		image_memory->cache_address = 0x0;
drath's avatar
drath committed
735
	}
ntfreak's avatar
ntfreak committed
736
737
738
	else if (image->type == IMAGE_SRECORD)
	{
		image_mot_t *image_mot;
oharboe's avatar
oharboe committed
739

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

ntfreak's avatar
ntfreak committed
742
743
744
745
		if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
746

ntfreak's avatar
ntfreak committed
747
748
		if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
		{
749
			LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
ntfreak's avatar
ntfreak committed
750
751
752
753
			fileio_close(&image_mot->fileio);
			return retval;
		}
	}
754
755
756
757
758
759
	else if (image->type == IMAGE_BUILDER)
	{
		image->num_sections = 0;
		image->sections = NULL;
		image->type_private = NULL;
	}
760
761
762

	if (image->base_address_set)
	{
763
		/* relocate */
764
		int section;
765
		for (section=0; section < image->num_sections; section++)
766
		{
zwelch's avatar
zwelch committed
767
			image->sections[section].base_address += image->base_address;
768
		}
769
770
771
		/* we're done relocating. The two statements below are mainly
		 * for documenation purposes: stop anyone from empirically
		 * thinking they should use these values henceforth. */
772
773
		image->base_address=0;
		image->base_address_set=0;
774
	}
oharboe's avatar
oharboe committed
775

drath's avatar
drath committed
776
777
778
	return retval;
};

779
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
780
781
{
	int retval;
782
783
784
785

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

drath's avatar
drath committed
791
792
793
	if (image->type == IMAGE_BINARY)
	{
		image_binary_t *image_binary = image->type_private;
oharboe's avatar
oharboe committed
794

795
796
797
		/* only one section in a plain binary */
		if (section != 0)
			return ERROR_INVALID_ARGUMENTS;
oharboe's avatar
oharboe committed
798

799
800
801
802
803
		/* seek to offset */
		if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
804

805
		/* return requested bytes */
drath's avatar
drath committed
806
807
808
809
810
811
812
		if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
		{
			return retval;
		}
	}
	else if (image->type == IMAGE_IHEX)
	{
813
		memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
drath's avatar
drath committed
814
		*size_read = size;
oharboe's avatar
oharboe committed
815

drath's avatar
drath committed
816
817
		return ERROR_OK;
	}
818
819
820
821
	else if (image->type == IMAGE_ELF)
	{
		return image_elf_read_section(image, section, offset, size, buffer, size_read);
	}
drath's avatar
drath committed
822
823
	else if (image->type == IMAGE_MEMORY)
	{
824
		image_memory_t *image_memory = image->type_private;
825
		uint32_t address = image->sections[section].base_address + offset;
oharboe's avatar
oharboe committed
826

827
		*size_read = 0;
oharboe's avatar
oharboe committed
828

829
830
		while ((size - *size_read) > 0)
		{
831
			uint32_t size_in_cache;
oharboe's avatar
oharboe committed
832

833
834
835
836
837
838
			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
839

840
841
842
843
				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);
844
					image_memory->cache = NULL;
845
846
847
848
					return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
				}
				image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
			}
oharboe's avatar
oharboe committed
849

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

852
853
854
855
			memcpy(buffer + *size_read,
				image_memory->cache + (address - image_memory->cache_address),
				(size_in_cache > size) ? size : size_in_cache
				);
oharboe's avatar
oharboe committed
856

857
858
859
			*size_read += (size_in_cache > size) ? size : size_in_cache;
			address += (size_in_cache > size) ? size : size_in_cache;
		}
drath's avatar
drath committed
860
	}
ntfreak's avatar
ntfreak committed
861
862
	else if (image->type == IMAGE_SRECORD)
	{
863
		memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
ntfreak's avatar
ntfreak committed
864
		*size_read = size;
oharboe's avatar
oharboe committed
865

ntfreak's avatar
ntfreak committed
866
867
		return ERROR_OK;
	}
868
869
	else if (image->type == IMAGE_BUILDER)
	{
870
		memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
871
		*size_read = size;
oharboe's avatar
oharboe committed
872

873
874
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
875

876
877
878
	return ERROR_OK;
}

879
int image_add_section(image_t *image, uint32_t base, uint32_t size, int flags, uint8_t *data)
880
{
881
	image_section_t *section;
oharboe's avatar
oharboe committed
882

883
884