image.c 27.5 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
31
 *   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 <stdlib.h>
#include <string.h>
32
#ifdef HAVE_ELF_H
33
#include <elf.h>
34
#endif
drath's avatar
drath committed
35
36
37
38
39
40
41
42
43
44

#include "image.h"

#include "types.h"
#include "replacements.h"
#include "log.h"

#include "fileio.h"
#include "target.h"

45
/* convert ELF header field to host endianness */
46
47
#define field16(elf,field)\
	((elf->endianness==ELFDATA2LSB)? \
oharboe's avatar
oharboe committed
48
		le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
49

50
51
#define field32(elf,field)\
	((elf->endianness==ELFDATA2LSB)? \
oharboe's avatar
oharboe committed
52
		le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
53

54
55
56
57
58
59
static int autodetect_image_type(image_t *image, char *url)
{
	int retval;
	fileio_t fileio;
	u32 read_bytes;
	u8 buffer[9];
oharboe's avatar
oharboe committed
60

61
62
63
64
65
	/* 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
66
	retval = fileio_read(&fileio, 9, buffer, &read_bytes);
oharboe's avatar
oharboe committed
67

oharboe's avatar
oharboe committed
68
	if (retval==ERROR_OK)
69
	{
oharboe's avatar
oharboe committed
70
71
72
73
		if (read_bytes != 9)
		{
			retval=ERROR_FILEIO_OPERATION_FAILED;
		}
74
75
	}
	fileio_close(&fileio);
oharboe's avatar
oharboe committed
76

oharboe's avatar
oharboe committed
77
78
	if (retval!=ERROR_OK)
		return retval;
79
80
81
82

	/* check header against known signatures */
	if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
	{
83
		LOG_DEBUG("ELF image detected.");
84
85
		image->type = IMAGE_ELF;
	}
86
	else if  ((buffer[0]==':') /* record start byte */
87
88
89
90
91
92
	        &&(isxdigit(buffer[1]))
	        &&(isxdigit(buffer[2]))
	        &&(isxdigit(buffer[3]))
	        &&(isxdigit(buffer[4]))
	        &&(isxdigit(buffer[5]))
	        &&(isxdigit(buffer[6]))
93
94
	        &&(buffer[7]=='0') /* record type : 00 -> 05 */
	        &&(buffer[8]>='0')&&(buffer[8]<'6'))
95
	{
96
		LOG_DEBUG("IHEX image detected.");
97
98
		image->type = IMAGE_IHEX;
	}
ntfreak's avatar
ntfreak committed
99
100
101
102
103
104
	else if ((buffer[0] == 'S') /* record start byte */
		&&(isxdigit(buffer[1]))
		&&(isxdigit(buffer[2]))
		&&(isxdigit(buffer[3]))
		&&(buffer[1] >= '0') && (buffer[1] < '9'))
	{
105
		LOG_DEBUG("S19 image detected.");
ntfreak's avatar
ntfreak committed
106
107
		image->type = IMAGE_SRECORD;
	}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	else
	{
		image->type = IMAGE_BINARY;
	}

	return ERROR_OK;
}

int identify_image_type(image_t *image, char *type_string, char *url)
{
	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
136
137
138
139
		else if (!strcmp(type_string, "s19"))
		{
			image->type = IMAGE_SRECORD;
		}
140
141
142
143
		else if (!strcmp(type_string, "build"))
		{
			image->type = IMAGE_BUILDER;
		}
144
145
146
147
148
149
150
151
152
		else
		{
			return ERROR_IMAGE_TYPE_UNKNOWN;
		}
	}
	else
	{
		return autodetect_image_type(image, url);
	}
oharboe's avatar
oharboe committed
153

154
155
156
	return ERROR_OK;
}

drath's avatar
drath committed
157
158
159
160
int image_ihex_buffer_complete(image_t *image)
{
	image_ihex_t *ihex = image->type_private;
	fileio_t *fileio = &ihex->fileio;
161
162
163
	u32 full_address = 0x0;
	u32 cooked_bytes;
	int i;
164
	char lpszLine[1023];
oharboe's avatar
oharboe committed
165

166
167
168
169
170
171
172
	/* 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;
173
	section[image->num_sections].private = &ihex->buffer[cooked_bytes];
174
175
176
	section[image->num_sections].base_address = 0x0;
	section[image->num_sections].size = 0x0;
	section[image->num_sections].flags = 0;
oharboe's avatar
oharboe committed
177

178
	while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
drath's avatar
drath committed
179
180
181
182
183
	{
		u32 count;
		u32 address;
		u32 record_type;
		u32 checksum;
ntfreak's avatar
ntfreak committed
184
		u8 cal_checksum = 0;
185
		u32 bytes_read = 0;
oharboe's avatar
oharboe committed
186

187
		if (sscanf(&lpszLine[bytes_read], ":%2x%4x%2x", &count, &address, &record_type) != 3)
drath's avatar
drath committed
188
189
190
		{
			return ERROR_IMAGE_FORMAT_ERROR;
		}
191
		bytes_read += 9;
oharboe's avatar
oharboe committed
192

ntfreak's avatar
ntfreak committed
193
194
195
196
		cal_checksum += (u8)count;
		cal_checksum += (u8)(address >> 8);
		cal_checksum += (u8)address;
		cal_checksum += (u8)record_type;
oharboe's avatar
oharboe committed
197

198
		if (record_type == 0) /* Data Record */
drath's avatar
drath committed
199
200
201
		{
			if ((full_address & 0xffff) != address)
			{
202
203
204
205
206
207
208
209
210
				/* 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;
211
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
212
213
214
215
				}
				section[image->num_sections].base_address =
					(full_address & 0xffff0000) | address;
				full_address = (full_address & 0xffff0000) | address;
drath's avatar
drath committed
216
			}
oharboe's avatar
oharboe committed
217

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

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

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

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

254
255
256
257
258
259
260
261
262
263
264
265
266
			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
267
				section[image->num_sections].base_address =
268
269
270
271
					(full_address & 0xffff) | (upper_address << 4);
				full_address = (full_address & 0xffff) | (upper_address << 4);
			}
		}
272
273
274
		else if (record_type == 3) /* Start Segment Address Record */
		{
			u32 dummy;
oharboe's avatar
oharboe committed
275

276
277
278
279
280
281
282
283
284
			/* "Start Segment Address Record" will not be supported */
			/* but we must consume it, and do not create an error.  */
			while (count-- > 0)
			{
				sscanf(&lpszLine[bytes_read], "%2x", &dummy);
				cal_checksum += (u8)dummy;
				bytes_read += 2;
			}
		}
285
		else if (record_type == 4) /* Extended Linear Address Record */
drath's avatar
drath committed
286
287
		{
			u16 upper_address;
oharboe's avatar
oharboe committed
288

289
			sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
ntfreak's avatar
ntfreak committed
290
291
			cal_checksum += (u8)(upper_address >> 8);
			cal_checksum += (u8)upper_address;
292
			bytes_read += 4;
oharboe's avatar
oharboe committed
293

drath's avatar
drath committed
294
295
			if ((full_address >> 16) != upper_address)
			{
296
297
298
299
300
301
302
303
304
				/* 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;
305
					section[image->num_sections].private = &ihex->buffer[cooked_bytes];
306
				}
oharboe's avatar
oharboe committed
307
				section[image->num_sections].base_address =
308
309
					(full_address & 0xffff) | (upper_address << 16);
				full_address = (full_address & 0xffff) | (upper_address << 16);
drath's avatar
drath committed
310
311
			}
		}
312
		else if (record_type == 5) /* Start Linear Address Record */
drath's avatar
drath committed
313
314
		{
			u32 start_address;
oharboe's avatar
oharboe committed
315

316
			sscanf(&lpszLine[bytes_read], "%8x", &start_address);
ntfreak's avatar
ntfreak committed
317
318
319
320
			cal_checksum += (u8)(start_address >> 24);
			cal_checksum += (u8)(start_address >> 16);
			cal_checksum += (u8)(start_address >> 8);
			cal_checksum += (u8)start_address;
321
			bytes_read += 8;
oharboe's avatar
oharboe committed
322

drath's avatar
drath committed
323
324
325
326
327
			image->start_address_set = 1;
			image->start_address = be_to_h_u32((u8*)&start_address);
		}
		else
		{
328
			LOG_ERROR("unhandled IHEX record type: %i", record_type);
drath's avatar
drath committed
329
330
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
331

332
333
		sscanf(&lpszLine[bytes_read], "%2x", &checksum);
		bytes_read += 2;
oharboe's avatar
oharboe committed
334

ntfreak's avatar
ntfreak committed
335
336
337
		if ((u8)checksum != (u8)(~cal_checksum + 1))
		{
			/* checksum failed */
338
			LOG_ERROR("incorrect record checksum found in IHEX file");
ntfreak's avatar
ntfreak committed
339
340
			return ERROR_IMAGE_CHECKSUM;
		}
drath's avatar
drath committed
341
	}
oharboe's avatar
oharboe committed
342

343
	LOG_ERROR("premature end of IHEX file, no end-of-file record found");
drath's avatar
drath committed
344
345
346
	return ERROR_IMAGE_FORMAT_ERROR;
}

347
348
349
350
351
352
353
354
355
int image_elf_read_headers(image_t *image)
{
	image_elf_t *elf = image->type_private;
	u32 read_bytes;
	u32 i,j;
	int retval;

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

356
357
358
359
360
361
	if(elf->header == NULL)
	{
		LOG_ERROR("insufficient memory to perform operation ");
		return ERROR_FILEIO_OPERATION_FAILED;
	}

362
363
	if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
	{
364
		LOG_ERROR("cannot read ELF file header, read failed");
365
366
367
368
		return ERROR_FILEIO_OPERATION_FAILED;
	}
	if (read_bytes != sizeof(Elf32_Ehdr))
	{
369
		LOG_ERROR("cannot read ELF file header, only partially read");
370
371
372
		return ERROR_FILEIO_OPERATION_FAILED;
	}

373
	if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
374
	{
375
		LOG_ERROR("invalid ELF file, bad magic number");
376
377
		return ERROR_IMAGE_FORMAT_ERROR;
	}
378
	if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
379
	{
380
		LOG_ERROR("invalid ELF file, only 32bits files are supported");
381
382
383
384
385
		return ERROR_IMAGE_FORMAT_ERROR;
	}


	elf->endianness = elf->header->e_ident[EI_DATA];
386
387
	if ((elf->endianness!=ELFDATA2LSB)
		 &&(elf->endianness!=ELFDATA2MSB))
388
	{
389
		LOG_ERROR("invalid ELF file, unknown endianess setting");
390
391
392
		return ERROR_IMAGE_FORMAT_ERROR;
	}

393
394
	elf->segment_count = field16(elf,elf->header->e_phnum);
	if (elf->segment_count==0)
395
	{
396
		LOG_ERROR("invalid ELF file, no program headers");
397
398
399
		return ERROR_IMAGE_FORMAT_ERROR;
	}

vpalatin's avatar
vpalatin committed
400
	if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
401
	{
402
		LOG_ERROR("cannot seek to ELF program header table, read failed");
403
404
405
		return retval;
	}

406
	elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
407
408
409
410
411
	if(elf->segments == NULL)
	{
		LOG_ERROR("insufficient memory to perform operation ");
		return ERROR_FILEIO_OPERATION_FAILED;
	}
412

413
	if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
414
	{
415
		LOG_ERROR("cannot read ELF segment headers, read failed");
416
417
		return retval;
	}
418
	if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
419
	{
420
		LOG_ERROR("cannot read ELF segment headers, only partially read");
421
422
423
		return ERROR_FILEIO_OPERATION_FAILED;
	}

ntfreak's avatar
ntfreak committed
424
	/* count useful segments (loadable), ignore BSS section */
425
	image->num_sections = 0;
426
	for (i=0;i<elf->segment_count;i++)
ntfreak's avatar
ntfreak committed
427
		if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
428
429
430
			image->num_sections++;
	/* alloc and fill sections array with loadable segments */
	image->sections = malloc(image->num_sections * sizeof(image_section_t));
431
	for (i=0,j=0;i<elf->segment_count;i++)
432
	{
ntfreak's avatar
ntfreak committed
433
		if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
434
		{
435
436
			image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
			image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
437
			image->sections[j].private = &elf->segments[i];
438
			image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
439
440
441
			j++;
		}
	}
oharboe's avatar
oharboe committed
442

443
	image->start_address_set = 1;
444
	image->start_address = field32(elf,elf->header->e_entry);
445
446
447
448
449
450
451
452

	return ERROR_OK;
}

int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
{
	image_elf_t *elf = image->type_private;
	Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
453
	u32 read_size,really_read;
454
455
456
	int retval;

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

458
	LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
459
460

	/* read initialized data in current segment if any */
461
	if (offset<field32(elf,segment->p_filesz))
462
463
	{
		/* maximal size present in file for the current segment */
464
		read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
465
		LOG_DEBUG("read elf: size = 0x%x at 0x%x",read_size,
466
			field32(elf,segment->p_offset)+offset);
467
		/* read initialized area of the segment */
468
		if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
469
		{
470
			LOG_ERROR("cannot find ELF segment content, seek failed");
471
472
473
474
			return retval;
		}
		if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
		{
475
			LOG_ERROR("cannot read ELF segment content, read failed");
476
477
478
479
480
481
482
483
484
485
			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
486

487
488
489
	return ERROR_OK;
}

ntfreak's avatar
ntfreak committed
490
491
492
493
494
495
496
int image_mot_buffer_complete(image_t *image)
{
	image_mot_t *mot = image->type_private;
	fileio_t *fileio = &mot->fileio;
	u32 full_address = 0x0;
	u32 cooked_bytes;
	int i;
497
	char lpszLine[1023];
oharboe's avatar
oharboe committed
498

ntfreak's avatar
ntfreak committed
499
500
501
	/* 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
502

ntfreak's avatar
ntfreak committed
503
504
505
506
507
508
509
	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
510

511
	while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
ntfreak's avatar
ntfreak committed
512
513
514
515
516
517
	{
		u32 count;
		u32 address;
		u32 record_type;
		u32 checksum;
		u8 cal_checksum = 0;
518
		u32 bytes_read = 0;
oharboe's avatar
oharboe committed
519

ntfreak's avatar
ntfreak committed
520
		/* get record type and record length */
521
		if (sscanf(&lpszLine[bytes_read], "S%1x%2x", &record_type, &count) != 2)
ntfreak's avatar
ntfreak committed
522
523
524
		{
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
525

526
		bytes_read += 4;
ntfreak's avatar
ntfreak committed
527
		cal_checksum += (u8)count;
oharboe's avatar
oharboe committed
528

ntfreak's avatar
ntfreak committed
529
530
		/* skip checksum byte */
		count -=1;
oharboe's avatar
oharboe committed
531

ntfreak's avatar
ntfreak committed
532
533
534
535
		if (record_type == 0)
		{
			/* S0 - starting record (optional) */
			int iValue;
oharboe's avatar
oharboe committed
536

ntfreak's avatar
ntfreak committed
537
			while (count-- > 0) {
538
				sscanf(&lpszLine[bytes_read], "%2x", &iValue);
ntfreak's avatar
ntfreak committed
539
				cal_checksum += (u8)iValue;
540
				bytes_read += 2;
ntfreak's avatar
ntfreak committed
541
542
543
544
545
546
547
548
			}
		}
		else if (record_type >= 1 && record_type <= 3)
		{
			switch( record_type )
			{
				case 1:
					/* S1 - 16 bit address data record */
549
					sscanf(&lpszLine[bytes_read], "%4x", &address);
ntfreak's avatar
ntfreak committed
550
551
					cal_checksum += (u8)(address >> 8);
					cal_checksum += (u8)address;
552
					bytes_read += 4;
ntfreak's avatar
ntfreak committed
553
554
					count -=2;
					break;
oharboe's avatar
oharboe committed
555

ntfreak's avatar
ntfreak committed
556
557
				case 2:
					/* S2 - 24 bit address data record */
558
					sscanf(&lpszLine[bytes_read], "%6x", &address);
ntfreak's avatar
ntfreak committed
559
560
561
					cal_checksum += (u8)(address >> 16);
					cal_checksum += (u8)(address >> 8);
					cal_checksum += (u8)address;
562
					bytes_read += 6;
ntfreak's avatar
ntfreak committed
563
564
					count -=3;
					break;
oharboe's avatar
oharboe committed
565

ntfreak's avatar
ntfreak committed
566
567
				case 3:
					/* S3 - 32 bit address data record */
568
					sscanf(&lpszLine[bytes_read], "%8x", &address);
ntfreak's avatar
ntfreak committed
569
570
571
572
					cal_checksum += (u8)(address >> 24);
					cal_checksum += (u8)(address >> 16);
					cal_checksum += (u8)(address >> 8);
					cal_checksum += (u8)address;
573
					bytes_read += 8;
ntfreak's avatar
ntfreak committed
574
575
					count -=4;
					break;
oharboe's avatar
oharboe committed
576

ntfreak's avatar
ntfreak committed
577
			}
oharboe's avatar
oharboe committed
578

ntfreak's avatar
ntfreak committed
579
580
581
582
583
584
585
586
587
588
589
590
591
			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];
				}
592
593
				section[image->num_sections].base_address = address;
				full_address = address;
ntfreak's avatar
ntfreak committed
594
			}
oharboe's avatar
oharboe committed
595

ntfreak's avatar
ntfreak committed
596
597
			while (count-- > 0)
			{
598
				sscanf(&lpszLine[bytes_read], "%2x", (u32*)&mot->buffer[cooked_bytes]);
ntfreak's avatar
ntfreak committed
599
				cal_checksum += (u8)mot->buffer[cooked_bytes];
600
				bytes_read += 2;
ntfreak's avatar
ntfreak committed
601
602
603
604
605
				cooked_bytes += 1;
				section[image->num_sections].size += 1;
				full_address++;
			}
		}
606
607
608
609
		else if (record_type == 5)
		{
			/* S5 is the data count record, we ignore it */
			u32 dummy;
oharboe's avatar
oharboe committed
610

611
612
613
614
615
616
617
			while (count-- > 0)
			{
				sscanf(&lpszLine[bytes_read], "%2x", &dummy);
				cal_checksum += (u8)dummy;
				bytes_read += 2;
			}
		}
ntfreak's avatar
ntfreak committed
618
619
620
621
		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
622

ntfreak's avatar
ntfreak committed
623
624
625
626
627
			/* 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;
628
				image->sections[i].base_address = section[i].base_address;
ntfreak's avatar
ntfreak committed
629
630
631
				image->sections[i].size = section[i].size;
				image->sections[i].flags = section[i].flags;
			}
oharboe's avatar
oharboe committed
632

ntfreak's avatar
ntfreak committed
633
634
635
636
			return ERROR_OK;
		}
		else
		{
637
			LOG_ERROR("unhandled S19 record type: %i", record_type);
ntfreak's avatar
ntfreak committed
638
639
			return ERROR_IMAGE_FORMAT_ERROR;
		}
oharboe's avatar
oharboe committed
640

ntfreak's avatar
ntfreak committed
641
		/* account for checksum, will always be 0xFF */
642
		sscanf(&lpszLine[bytes_read], "%2x", &checksum);
ntfreak's avatar
ntfreak committed
643
		cal_checksum += (u8)checksum;
644
		bytes_read += 2;
oharboe's avatar
oharboe committed
645

ntfreak's avatar
ntfreak committed
646
647
648
		if( cal_checksum != 0xFF )
		{
			/* checksum failed */
649
			LOG_ERROR("incorrect record checksum found in S19 file");
ntfreak's avatar
ntfreak committed
650
651
652
			return ERROR_IMAGE_CHECKSUM;
		}
	}
oharboe's avatar
oharboe committed
653

654
	LOG_ERROR("premature end of S19 file, no end-of-file record found");
ntfreak's avatar
ntfreak committed
655
656
657
	return ERROR_IMAGE_FORMAT_ERROR;
}

658
int image_open(image_t *image, char *url, char *type_string)
drath's avatar
drath committed
659
660
{
	int retval = ERROR_OK;
oharboe's avatar
oharboe committed
661

662
	if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
663
664
	{
		return retval;
665
	}
oharboe's avatar
oharboe committed
666

drath's avatar
drath committed
667
668
669
	if (image->type == IMAGE_BINARY)
	{
		image_binary_t *image_binary;
oharboe's avatar
oharboe committed
670

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

673
		if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
drath's avatar
drath committed
674
675
676
		{
			return retval;
		}
oharboe's avatar
oharboe committed
677

678
679
680
681
682
		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
683
684
685
686
	}
	else if (image->type == IMAGE_IHEX)
	{
		image_ihex_t *image_ihex;
oharboe's avatar
oharboe committed
687

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

drath's avatar
drath committed
690
691
692
693
		if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
694

drath's avatar
drath committed
695
696
		if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
		{
697
			LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
drath's avatar
drath committed
698
699
700
701
			fileio_close(&image_ihex->fileio);
			return retval;
		}
	}
702
703
704
	else if (image->type == IMAGE_ELF)
	{
		image_elf_t *image_elf;
oharboe's avatar
oharboe committed
705

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

708
709
710
711
		if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
712

713
714
715
716
717
718
		if ((retval = image_elf_read_headers(image)) != ERROR_OK)
		{
			fileio_close(&image_elf->fileio);
			return retval;
		}
	}
drath's avatar
drath committed
719
720
721
	else if (image->type == IMAGE_MEMORY)
	{
		image_memory_t *image_memory;
oharboe's avatar
oharboe committed
722

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

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

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

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

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

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

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

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

778
int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
drath's avatar
drath committed
779
780
{
	int retval;
781
782
783
784

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

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

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

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

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

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

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

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

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

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

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

851
852
853
854
			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
855

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

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

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

875
876
877
878
879
	return ERROR_OK;
}

int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
{
880
	image_section_t *section;
oharboe's avatar
oharboe committed
881

882
883
884
	/* only image builder supports adding sections */
	if (image->type != IMAGE_BUILDER)
		return ERROR_INVALID_ARGUMENTS;
oharboe's avatar
oharboe committed
885

886
887
	/* see if there's a previous section */
	if (image->num_sections)
888
	{
889
		section = &image->sections[image->num_sections - 1];
oharboe's avatar
oharboe committed
890

891
892
893
894
895
896
897
898
899
		/* see if it's enough to extend the last section,
		 * adding data to previous sections or merging is not supported */
		if (((section->base_address + section->size) == base) && (section->flags == flags))
		{
			section->private = realloc(section->private, section->size + size);
			memcpy((u8*)section->private + section->size, data, size);
			section->size += size;
			return ERROR_OK;
		}
900
	}
oharboe's avatar
oharboe committed
901

902
903
904
	/* allocate new section */
	image->num_sections++;
	image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
905
906
907
908
909
910
	section = &image->sections[image->num_sections - 1];
	section->base_address = base;
	section->size = size;
	section->flags = flags;
	section->private = malloc(sizeof(u8) * size);
	memcpy((u8*)section->private, data, size);
oharboe's avatar
oharboe committed
911

drath's avatar
drath committed
912
913
914
	return ERROR_OK;
}

915
void image_close(image_t *image)
drath's avatar
drath committed
916
917
918
919
{
	if (image->type == IMAGE_BINARY)
	{
		image_binary_t *image_binary = image->type_private;
oharboe's avatar
oharboe committed
920

drath's avatar
drath committed
921
922
923
924
925
		fileio_close(&image_binary->fileio);
	}
	else if (image->type == IMAGE_IHEX)
	{
		image_ihex_t *image_ihex = image->type_private;
oharboe's avatar
oharboe committed
926

drath's avatar
drath committed
927
		fileio_close(&image_ihex->fileio);
oharboe's avatar
oharboe committed
928

drath's avatar
drath committed
929
		if (image_ihex->buffer)
930
		{
drath's avatar
drath committed
931
			free(image_ihex->buffer);
932
933
			image_ihex->buffer = NULL;
		}
drath's avatar
drath committed
934
	}
935
936
937
	else if (image->type == IMAGE_ELF)
	{
		image_elf_t *image_elf = image->type_private;
oharboe's avatar
oharboe committed
938

939
		fileio_close(&image_elf->fileio);
940

941
		if (image_elf->header)
942
		{
943
			free(image_elf->header);
944
945
			image_elf->header = NULL;
		}
946
947

		if (image_elf->segments)
948
		{
949
			free(image_elf->segments);
950
951
			image_elf->segments = NULL;
		}
952
	}
drath's avatar
drath committed
953
954
	else if (image->type == IMAGE_MEMORY)
	{
955
		image_memory_t *image_memory = image->type_private;