]> git.sur5r.net Git - openocd/blob - src/target/image.c
- reworked presto.c to allow use of either FTD2XX or libftdi (libftdi not functional...
[openocd] / src / target / image.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef HAVE_ELF_H
27 #include <elf.h>
28 #endif
29
30 #include "image.h"
31
32 #include "types.h"
33 #include "replacements.h"
34 #include "log.h"
35
36 #include "fileio.h"
37 #include "target.h"
38
39 /* convert ELF header field to host endianness */
40 #define field16(elf,field)\
41         ((elf->endianness==ELFDATA2LSB)? \
42                 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field)) 
43
44 #define field32(elf,field)\
45         ((elf->endianness==ELFDATA2LSB)? \
46                 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field)) 
47
48 static int autodetect_image_type(image_t *image, char *url)
49 {
50         int retval;
51         fileio_t fileio;
52         u32 read_bytes;
53         u8 buffer[9];
54         
55         /* read the first 4 bytes of image */
56         if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
57         {
58                 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot open image: %s", fileio.error_str); 
59                 ERROR(image->error_str);
60                 return retval;
61         }
62         if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != ERROR_OK)
63         {
64                 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image header: %s", fileio.error_str);
65                 ERROR(image->error_str);
66                 return ERROR_FILEIO_OPERATION_FAILED;
67         }
68         if (read_bytes != 9)
69         {
70                 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image, only partially read");
71                 ERROR(image->error_str);
72                 return ERROR_FILEIO_OPERATION_FAILED;
73         }
74         fileio_close(&fileio);
75
76         /* check header against known signatures */
77         if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
78         {
79                 DEBUG("ELF image detected.");
80                 image->type = IMAGE_ELF;
81         }
82         else if  ((buffer[0]==':') /* record start byte */
83                 &&(isxdigit(buffer[1]))
84                 &&(isxdigit(buffer[2]))
85                 &&(isxdigit(buffer[3]))
86                 &&(isxdigit(buffer[4]))
87                 &&(isxdigit(buffer[5]))
88                 &&(isxdigit(buffer[6]))
89                 &&(buffer[7]=='0') /* record type : 00 -> 05 */
90                 &&(buffer[8]>='0')&&(buffer[8]<'6'))
91         {
92                 DEBUG("IHEX image detected.");
93                 image->type = IMAGE_IHEX;
94         }
95         else if ((buffer[0] == 'S') /* record start byte */
96                 &&(isxdigit(buffer[1]))
97                 &&(isxdigit(buffer[2]))
98                 &&(isxdigit(buffer[3]))
99                 &&(buffer[1] >= '0') && (buffer[1] < '9'))
100         {
101                 DEBUG("S19 image detected.");
102                 image->type = IMAGE_SRECORD;
103         }
104         else
105         {
106                 image->type = IMAGE_BINARY;
107         }
108
109         return ERROR_OK;
110 }
111
112 int identify_image_type(image_t *image, char *type_string, char *url)
113 {
114         if (type_string)
115         {
116                 if (!strcmp(type_string, "bin"))
117                 {
118                         image->type = IMAGE_BINARY;
119                 }
120                 else if (!strcmp(type_string, "ihex"))
121                 {
122                         image->type = IMAGE_IHEX;
123                 }
124                 else if (!strcmp(type_string, "elf"))
125                 {
126                         image->type = IMAGE_ELF;
127                 }
128                 else if (!strcmp(type_string, "mem"))
129                 {
130                         image->type = IMAGE_MEMORY;
131                 }
132                 else if (!strcmp(type_string, "s19"))
133                 {
134                         image->type = IMAGE_SRECORD;
135                 }
136                 else if (!strcmp(type_string, "build"))
137                 {
138                         image->type = IMAGE_BUILDER;
139                 }
140                 else
141                 {
142                         return ERROR_IMAGE_TYPE_UNKNOWN;
143                 }
144         }
145         else
146         {
147                 return autodetect_image_type(image, url);
148         }
149         
150         return ERROR_OK;
151 }
152
153 int image_ihex_buffer_complete(image_t *image)
154 {
155         image_ihex_t *ihex = image->type_private;
156         fileio_t *fileio = &ihex->fileio;
157         u32 raw_bytes_read, raw_bytes;
158         int retval;
159         u32 full_address = 0x0;
160         char *buffer = malloc(fileio->size);
161         u32 cooked_bytes;
162         int i;
163         
164         /* we can't determine the number of sections that we'll have to create ahead of time,
165          * so we locally hold them until parsing is finished */
166         image_section_t section[IMAGE_MAX_SECTIONS];
167         
168         if ((retval = fileio_read(fileio, fileio->size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
169         {
170                 free(buffer);
171                 ERROR("failed buffering IHEX file, read failed");
172                 return ERROR_FILEIO_OPERATION_FAILED;
173         }
174         
175         if (raw_bytes_read != fileio->size)
176         {
177                 free(buffer);
178                 ERROR("failed buffering complete IHEX file, only partially read");
179                 return ERROR_FILEIO_OPERATION_FAILED;
180         }
181
182         ihex->buffer = malloc(fileio->size >> 1);
183         raw_bytes = 0x0;
184         cooked_bytes = 0x0;
185         image->num_sections = 0;
186         section[image->num_sections].private = &ihex->buffer[cooked_bytes];
187         section[image->num_sections].base_address = 0x0;
188         section[image->num_sections].size = 0x0;
189         section[image->num_sections].flags = 0;
190         while (raw_bytes < raw_bytes_read)
191         {
192                 u32 count;
193                 u32 address;
194                 u32 record_type;
195                 u32 checksum;
196                 u8 cal_checksum = 0;
197                 
198                 if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
199                 {
200                         return ERROR_IMAGE_FORMAT_ERROR;
201                 }
202                 raw_bytes += 9;
203                 
204                 cal_checksum += (u8)count;
205                 cal_checksum += (u8)(address >> 8);
206                 cal_checksum += (u8)address;
207                 cal_checksum += (u8)record_type;
208                 
209                 if (record_type == 0) /* Data Record */
210                 {
211                         if ((full_address & 0xffff) != address)
212                         {
213                                 /* we encountered a nonconsecutive location, create a new section,
214                                  * unless the current section has zero size, in which case this specifies
215                                  * the current section's base address
216                                  */
217                                 if (section[image->num_sections].size != 0)
218                                 {
219                                         image->num_sections++;
220                                         section[image->num_sections].size = 0x0;
221                                         section[image->num_sections].flags = 0;
222                                         section[image->num_sections].private = &ihex->buffer[cooked_bytes];
223                                 }
224                                 section[image->num_sections].base_address =
225                                         (full_address & 0xffff0000) | address;
226                                 full_address = (full_address & 0xffff0000) | address;
227                         }
228                         
229                         while (count-- > 0)
230                         {
231                                 sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
232                                 cal_checksum += (u8)ihex->buffer[cooked_bytes];
233                                 raw_bytes += 2;
234                                 cooked_bytes += 1;
235                                 section[image->num_sections].size += 1;
236                                 full_address++;
237                         }
238                 }
239                 else if (record_type == 1) /* End of File Record */
240                 {
241                         /* finish the current section */
242                         image->num_sections++;
243                         
244                         /* copy section information */
245                         image->sections = malloc(sizeof(image_section_t) * image->num_sections);
246                         for (i = 0; i < image->num_sections; i++)
247                         {
248                                 image->sections[i].private = section[i].private;
249                                 image->sections[i].base_address = section[i].base_address +
250                                         ((image->base_address_set) ? image->base_address : 0);
251                                 image->sections[i].size = section[i].size;
252                                 image->sections[i].flags = section[i].flags;
253                         }
254                         
255                         free(buffer);
256                         return ERROR_OK;
257                 }
258                 else if (record_type == 4) /* Extended Linear Address Record */
259                 {
260                         u16 upper_address;
261                         
262                         sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
263                         cal_checksum += (u8)(upper_address >> 8);
264                         cal_checksum += (u8)upper_address;
265                         raw_bytes += 4;
266                         
267                         if ((full_address >> 16) != upper_address)
268                         {
269                                 /* we encountered a nonconsecutive location, create a new section,
270                                  * unless the current section has zero size, in which case this specifies
271                                  * the current section's base address
272                                  */
273                                 if (section[image->num_sections].size != 0)
274                                 {
275                                         image->num_sections++;
276                                         section[image->num_sections].size = 0x0;
277                                         section[image->num_sections].flags = 0;
278                                         section[image->num_sections].private = &ihex->buffer[cooked_bytes];
279                                 }
280                                 section[image->num_sections].base_address = 
281                                         (full_address & 0xffff) | (upper_address << 16);
282                                 full_address = (full_address & 0xffff) | (upper_address << 16);
283                         }
284                 }
285                 else if (record_type == 5) /* Start Linear Address Record */
286                 {
287                         u32 start_address;
288                         
289                         sscanf(&buffer[raw_bytes], "%8x", &start_address);
290                         cal_checksum += (u8)(start_address >> 24);
291                         cal_checksum += (u8)(start_address >> 16);
292                         cal_checksum += (u8)(start_address >> 8);
293                         cal_checksum += (u8)start_address;
294                         raw_bytes += 8;
295                         
296                         image->start_address_set = 1;
297                         image->start_address = be_to_h_u32((u8*)&start_address);
298                 }
299                 else
300                 {
301                         free(buffer);
302                         ERROR("unhandled IHEX record type: %i", record_type);
303                         return ERROR_IMAGE_FORMAT_ERROR;
304                 }
305                 
306                 sscanf(&buffer[raw_bytes], "%2x", &checksum);
307                 raw_bytes += 2;
308                 
309                 if ((u8)checksum != (u8)(~cal_checksum + 1))
310                 {
311                         /* checksum failed */
312                         free(buffer);
313                         ERROR("incorrect record checksum found in IHEX file");
314                         return ERROR_IMAGE_CHECKSUM;
315                 }
316                 
317                 /* consume new-line character(s) */
318                 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
319                         raw_bytes++;
320
321                 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
322                         raw_bytes++;
323         }
324
325         free(buffer);
326         ERROR("premature end of IHEX file, no end-of-file record found");
327         return ERROR_IMAGE_FORMAT_ERROR;
328 }
329
330 int image_elf_read_headers(image_t *image)
331 {
332         image_elf_t *elf = image->type_private;
333         u32 read_bytes;
334         u32 i,j;
335         int retval;
336
337         elf->header = malloc(sizeof(Elf32_Ehdr));
338
339         if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
340         {
341                 ERROR("cannot read ELF file header, read failed");
342                 return ERROR_FILEIO_OPERATION_FAILED;
343         }
344         if (read_bytes != sizeof(Elf32_Ehdr))
345         {
346                 ERROR("cannot read ELF file header, only partially read");
347                 return ERROR_FILEIO_OPERATION_FAILED;
348         }
349
350         if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
351         {
352                 ERROR("invalid ELF file, bad magic number");
353                 return ERROR_IMAGE_FORMAT_ERROR;
354         }
355         if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
356         {
357                 ERROR("invalid ELF file, only 32bits files are supported");
358                 return ERROR_IMAGE_FORMAT_ERROR;
359         }
360
361
362         elf->endianness = elf->header->e_ident[EI_DATA];
363         if ((elf->endianness!=ELFDATA2LSB)
364                  &&(elf->endianness!=ELFDATA2MSB))
365         {
366                 ERROR("invalid ELF file, unknown endianess setting");
367                 return ERROR_IMAGE_FORMAT_ERROR;
368         }
369
370         elf->segment_count = field16(elf,elf->header->e_phnum);
371         if (elf->segment_count==0)
372         {
373                 ERROR("invalid ELF file, no program headers");
374                 return ERROR_IMAGE_FORMAT_ERROR;
375         }
376
377         elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
378
379         if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
380         {
381                 ERROR("cannot read ELF segment headers, read failed");
382                 return retval;
383         }
384         if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
385         {
386                 ERROR("cannot read ELF segment headers, only partially read");
387                 return ERROR_FILEIO_OPERATION_FAILED;
388         }
389
390         /* count useful segments (loadable), ignore BSS section */
391         image->num_sections = 0;
392         for (i=0;i<elf->segment_count;i++)
393                 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
394                         image->num_sections++;
395         /* alloc and fill sections array with loadable segments */
396         image->sections = malloc(image->num_sections * sizeof(image_section_t));
397         for (i=0,j=0;i<elf->segment_count;i++)
398         {
399                 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
400                 {
401                         image->sections[j].size = field32(elf,elf->segments[i].p_memsz);
402                         image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
403                         image->sections[j].private = &elf->segments[i];
404                         image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
405                         j++;
406                 }
407         }
408                 
409         image->start_address_set = 1;
410         image->start_address = field32(elf,elf->header->e_entry);
411
412         return ERROR_OK;
413 }
414
415 int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
416 {
417         image_elf_t *elf = image->type_private;
418         Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
419         u32 read_size,really_read;
420         int retval;
421
422         *size_read = 0;
423         
424         DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
425
426         /* read initialized data in current segment if any */
427         if (offset<field32(elf,segment->p_filesz))
428         {
429                 /* maximal size present in file for the current segment */
430                 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
431                 DEBUG("read elf: size = 0x%x at 0x%x",read_size,
432                         field32(elf,segment->p_offset)+offset);
433                 /* read initialized area of the segment */
434                 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
435                 {
436                         ERROR("cannot find ELF segment content, seek failed");
437                         return retval;
438                 }
439                 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
440                 {
441                         ERROR("cannot read ELF segment content, read failed");
442                         return retval;
443                 }
444                 buffer += read_size;
445                 size -= read_size;
446                 offset += read_size;
447                 *size_read += read_size;
448                 /* need more data ? */
449                 if (!size)
450                         return ERROR_OK;
451         }
452         /* if there is remaining zeroed area in current segment */
453         if (offset<field32(elf,segment->p_memsz))
454         {
455                 /* fill zeroed part (BSS) of the segment */
456                 read_size = MIN(size, field32(elf,segment->p_memsz)-offset);
457                 DEBUG("zero fill: size = 0x%x",read_size);
458                 memset(buffer,0,read_size);
459                 *size_read += read_size;
460         }
461         
462         return ERROR_OK;
463 }
464
465 int image_mot_buffer_complete(image_t *image)
466 {
467         image_mot_t *mot = image->type_private;
468         fileio_t *fileio = &mot->fileio;
469         u32 raw_bytes_read, raw_bytes;
470         int retval;
471         u32 full_address = 0x0;
472         char *buffer = malloc(fileio->size);
473         u32 cooked_bytes;
474         int i;
475         
476         /* we can't determine the number of sections that we'll have to create ahead of time,
477          * so we locally hold them until parsing is finished */
478         image_section_t section[IMAGE_MAX_SECTIONS];
479         
480         if ((retval = fileio_read(fileio, fileio->size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
481         {
482                 free(buffer);
483                 ERROR("failed buffering S19 file, read failed");
484                 return ERROR_FILEIO_OPERATION_FAILED;
485         }
486         
487         if (raw_bytes_read != fileio->size)
488         {
489                 free(buffer);
490                 ERROR("failed buffering complete IHEX file, only partially read");
491                 return ERROR_FILEIO_OPERATION_FAILED;
492         }
493
494         mot->buffer = malloc(fileio->size >> 1);
495         raw_bytes = 0x0;
496         cooked_bytes = 0x0;
497         image->num_sections = 0;
498         section[image->num_sections].private = &mot->buffer[cooked_bytes];
499         section[image->num_sections].base_address = 0x0;
500         section[image->num_sections].size = 0x0;
501         section[image->num_sections].flags = 0;
502         
503         while (raw_bytes < raw_bytes_read)
504         {
505                 u32 count;
506                 u32 address;
507                 u32 record_type;
508                 u32 checksum;
509                 u8 cal_checksum = 0;
510                 
511                 /* get record type and record length */
512                 if (sscanf(&buffer[raw_bytes], "S%1x%2x", &record_type, &count) != 2)
513                 {
514                         return ERROR_IMAGE_FORMAT_ERROR;
515                 }
516                 
517                 raw_bytes += 4;
518                 cal_checksum += (u8)count;
519                 
520                 /* skip checksum byte */
521                 count -=1;
522                 
523                 if (record_type == 0)
524                 {
525                         /* S0 - starting record (optional) */
526                         int iValue;
527                         
528                         while (count-- > 0) {
529                                 sscanf(&buffer[raw_bytes], "%2x", &iValue);
530                                 cal_checksum += (u8)iValue;
531                                 raw_bytes += 2;
532                         }
533                 }
534                 else if (record_type >= 1 && record_type <= 3)
535                 {
536                         switch( record_type )
537                         {
538                                 case 1:
539                                         /* S1 - 16 bit address data record */
540                                         sscanf(&buffer[raw_bytes], "%4x", &address);
541                                         cal_checksum += (u8)(address >> 8);
542                                         cal_checksum += (u8)address;
543                                         raw_bytes += 4;
544                                         count -=2;
545                                         break;
546                         
547                                 case 2:
548                                         /* S2 - 24 bit address data record */
549                                         sscanf(&buffer[raw_bytes], "%6x", &address);
550                                         cal_checksum += (u8)(address >> 16);
551                                         cal_checksum += (u8)(address >> 8);
552                                         cal_checksum += (u8)address;
553                                         raw_bytes += 6;
554                                         count -=3;
555                                         break;
556                                         
557                                 case 3:
558                                         /* S3 - 32 bit address data record */
559                                         sscanf(&buffer[raw_bytes], "%8x", &address);
560                                         cal_checksum += (u8)(address >> 24);
561                                         cal_checksum += (u8)(address >> 16);
562                                         cal_checksum += (u8)(address >> 8);
563                                         cal_checksum += (u8)address;
564                                         raw_bytes += 8;
565                                         count -=4;
566                                         break;
567                         
568                         }
569                         
570                         if (full_address != address)
571                         {
572                                 /* we encountered a nonconsecutive location, create a new section,
573                                  * unless the current section has zero size, in which case this specifies
574                                  * the current section's base address
575                                  */
576                                 if (section[image->num_sections].size != 0)
577                                 {
578                                         image->num_sections++;
579                                         section[image->num_sections].size = 0x0;
580                                         section[image->num_sections].flags = 0;
581                                         section[image->num_sections].private = &mot->buffer[cooked_bytes];
582                                 }
583                                 section[image->num_sections].base_address =
584                                         full_address | address;
585                                 full_address = full_address | address;
586                         }
587                         
588                         while (count-- > 0)
589                         {
590                                 sscanf(&buffer[raw_bytes], "%2hhx", &mot->buffer[cooked_bytes]);
591                                 cal_checksum += (u8)mot->buffer[cooked_bytes];
592                                 raw_bytes += 2;
593                                 cooked_bytes += 1;
594                                 section[image->num_sections].size += 1;
595                                 full_address++;
596                         }
597                 }
598                 else if (record_type >= 7 && record_type <= 9)
599                 {
600                         /* S7, S8, S9 - ending records for 32, 24 and 16bit */
601                         image->num_sections++;
602                         
603                         /* copy section information */
604                         image->sections = malloc(sizeof(image_section_t) * image->num_sections);
605                         for (i = 0; i < image->num_sections; i++)
606                         {
607                                 image->sections[i].private = section[i].private;
608                                 image->sections[i].base_address = section[i].base_address +
609                                         ((image->base_address_set) ? image->base_address : 0);
610                                 image->sections[i].size = section[i].size;
611                                 image->sections[i].flags = section[i].flags;
612                         }
613                         
614                         free(buffer);
615                         return ERROR_OK;
616                 }
617                 else
618                 {
619                         free(buffer);
620                         ERROR("unhandled S19 record type: %i", record_type);
621                         return ERROR_IMAGE_FORMAT_ERROR;
622                 }
623                 
624                 /* account for checksum, will always be 0xFF */
625                 sscanf(&buffer[raw_bytes], "%2x", &checksum);
626                 cal_checksum += (u8)checksum;
627                 raw_bytes += 2;
628                 
629                 if( cal_checksum != 0xFF )
630                 {
631                         /* checksum failed */
632                         free(buffer);
633                         ERROR("incorrect record checksum found in S19 file");
634                         return ERROR_IMAGE_CHECKSUM;
635                 }
636                 
637                 /* consume new-line character(s) */
638                 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
639                         raw_bytes++;
640
641                 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
642                         raw_bytes++;
643         }
644
645         free(buffer);
646         ERROR("premature end of S19 file, no end-of-file record found");
647         return ERROR_IMAGE_FORMAT_ERROR;
648 }
649
650 int image_open(image_t *image, char *url, char *type_string)
651 {
652         int retval = ERROR_OK;
653         
654         if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
655         {
656                 return retval;
657         }
658         
659         if (image->type == IMAGE_BINARY)
660         {
661                 image_binary_t *image_binary;
662                 
663                 image_binary = image->type_private = malloc(sizeof(image_binary_t));
664                 
665                 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
666                 {
667                         strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING); 
668                         ERROR(image->error_str);
669                         return retval;
670                 }
671                 
672                 image->num_sections = 1;
673                 image->sections = malloc(sizeof(image_section_t));
674                 image->sections[0].base_address = 0x0;
675                 image->sections[0].size = image_binary->fileio.size;
676                 image->sections[0].flags = 0;
677                 
678                 if (image->base_address_set == 1)
679                         image->sections[0].base_address = image->base_address;
680                 
681                 return ERROR_OK;
682         }
683         else if (image->type == IMAGE_IHEX)
684         {
685                 image_ihex_t *image_ihex;
686                 
687                 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
688                 
689                 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
690                 {
691                         strncpy(image->error_str, image_ihex->fileio.error_str, IMAGE_MAX_ERROR_STRING); 
692                         ERROR(image->error_str);
693                         return retval;
694                 }
695                 
696                 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
697                 {
698                         snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
699                                 "failed buffering IHEX image, check daemon output for additional information");
700                         ERROR(image->error_str);
701                         fileio_close(&image_ihex->fileio);
702                         return retval;
703                 }
704         }
705         else if (image->type == IMAGE_ELF)
706         {
707                 image_elf_t *image_elf;
708                 
709                 image_elf = image->type_private = malloc(sizeof(image_elf_t));
710                 
711                 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
712                 {
713                         strncpy(image->error_str, image_elf->fileio.error_str, IMAGE_MAX_ERROR_STRING); 
714                         ERROR(image->error_str);
715                         return retval;
716                 }
717                 
718                 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
719                 {
720                         snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
721                                 "failed to read ELF headers, check daemon output for additional information");
722                         ERROR(image->error_str);
723                         fileio_close(&image_elf->fileio);
724                         return retval;
725                 }
726         }
727         else if (image->type == IMAGE_MEMORY)
728         {
729                 image_memory_t *image_memory;
730                 
731                 image->num_sections = 1;
732                 image->sections = malloc(sizeof(image_section_t));
733                 image->sections[0].base_address = 0x0;
734                 image->sections[0].size = 0xffffffff;
735                 image->sections[0].flags = 0;
736                 
737                 image_memory = image->type_private = malloc(sizeof(image_memory_t));
738                 
739                 image_memory->target = get_target_by_num(strtoul(url, NULL, 0));;
740                 image_memory->cache = NULL;
741                 image_memory->cache_address = 0x0;
742         }
743         else if (image->type == IMAGE_SRECORD)
744         {
745                 image_mot_t *image_mot;
746                 
747                 image_mot = image->type_private = malloc(sizeof(image_mot_t));
748                 
749                 if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
750                 {
751                         strncpy(image->error_str, image_mot->fileio.error_str, IMAGE_MAX_ERROR_STRING); 
752                         ERROR(image->error_str);
753                         return retval;
754                 }
755                 
756                 if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
757                 {
758                         snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
759                                 "failed buffering S19 image, check daemon output for additional information");
760                         ERROR(image->error_str);
761                         fileio_close(&image_mot->fileio);
762                         return retval;
763                 }
764         }
765         else if (image->type == IMAGE_BUILDER)
766         {
767                 image->num_sections = 0;
768                 image->sections = NULL;
769                 image->type_private = NULL;
770         }
771         
772         return retval;
773 };
774
775 int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
776 {
777         int retval;
778
779         /* don't read past the end of a section */
780         if (offset + size > image->sections[section].size)
781         {
782                 DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
783                                 offset, size, image->sections[section].size);
784                 return ERROR_INVALID_ARGUMENTS;
785         }
786
787         if (image->type == IMAGE_BINARY)
788         {
789                 image_binary_t *image_binary = image->type_private;
790                 
791                 /* only one section in a plain binary */
792                 if (section != 0)
793                         return ERROR_INVALID_ARGUMENTS;
794                         
795                 /* seek to offset */
796                 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
797                 {
798                         strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
799                         return retval;
800                 }
801                 
802                 /* return requested bytes */
803                 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
804                 {
805                         strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
806                         return retval;
807                 }
808         }
809         else if (image->type == IMAGE_IHEX)
810         {
811                 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
812                 *size_read = size;
813                 image->error_str[0] = '\0';
814                 
815                 return ERROR_OK;
816         }
817         else if (image->type == IMAGE_ELF)
818         {
819                 return image_elf_read_section(image, section, offset, size, buffer, size_read);
820         }
821         else if (image->type == IMAGE_MEMORY)
822         {
823                 image_memory_t *image_memory = image->type_private;
824                 u32 address = image->sections[section].base_address + offset;
825                 
826                 *size_read = 0;
827                 
828                 while ((size - *size_read) > 0)
829                 {
830                         u32 size_in_cache;
831                         
832                         if (!image_memory->cache
833                                 || (address < image_memory->cache_address)
834                                 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
835                         {
836                                 if (!image_memory->cache)
837                                         image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
838                                 
839                                 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
840                                         IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
841                                 {
842                                         free(image_memory->cache);
843                                         return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
844                                 }
845                                 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
846                         }
847                         
848                         size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
849                         
850                         memcpy(buffer + *size_read,
851                                 image_memory->cache + (address - image_memory->cache_address),
852                                 (size_in_cache > size) ? size : size_in_cache
853                                 );
854                                 
855                         *size_read += (size_in_cache > size) ? size : size_in_cache;
856                         address += (size_in_cache > size) ? size : size_in_cache;
857                 }
858         }
859         else if (image->type == IMAGE_SRECORD)
860         {
861                 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
862                 *size_read = size;
863                 image->error_str[0] = '\0';
864                 
865                 return ERROR_OK;
866         }
867         else if (image->type == IMAGE_BUILDER)
868         {
869                 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
870                 *size_read = size;
871                 image->error_str[0] = '\0';
872                 
873                 return ERROR_OK;
874         }
875         
876         return ERROR_OK;
877 }
878
879 int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
880 {
881         image_section_t *section;
882         
883         /* only image builder supports adding sections */
884         if (image->type != IMAGE_BUILDER)
885                 return ERROR_INVALID_ARGUMENTS;
886         
887         /* see if there's a previous section */
888         if (image->num_sections)
889         {
890                 section = &image->sections[image->num_sections - 1];
891                 
892                 /* see if it's enough to extend the last section,
893                  * adding data to previous sections or merging is not supported */
894                 if (((section->base_address + section->size) == base) && (section->flags == flags))
895                 {
896                         section->private = realloc(section->private, section->size + size);
897                         memcpy((u8*)section->private + section->size, data, size);
898                         section->size += size;
899                         return ERROR_OK;
900                 }
901         }
902                 
903         /* allocate new section */
904         image->num_sections++;
905         image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
906         section = &image->sections[image->num_sections - 1];
907         section->base_address = base;
908         section->size = size;
909         section->flags = flags;
910         section->private = malloc(sizeof(u8) * size);
911         memcpy((u8*)section->private, data, size);
912         
913         return ERROR_OK;
914 }
915
916 int image_close(image_t *image)
917 {
918         if (image->type == IMAGE_BINARY)
919         {
920                 image_binary_t *image_binary = image->type_private;
921                 
922                 fileio_close(&image_binary->fileio);
923         }
924         else if (image->type == IMAGE_IHEX)
925         {
926                 image_ihex_t *image_ihex = image->type_private;
927                 
928                 fileio_close(&image_ihex->fileio);
929                 
930                 if (image_ihex->buffer)
931                         free(image_ihex->buffer);
932         }
933         else if (image->type == IMAGE_ELF)
934         {
935                 image_elf_t *image_elf = image->type_private;
936                 
937                 fileio_close(&image_elf->fileio);
938
939                 if (image_elf->header)
940                         free(image_elf->header);
941
942                 if (image_elf->segments)
943                         free(image_elf->segments);
944         }
945         else if (image->type == IMAGE_MEMORY)
946         {
947                 image_memory_t *image_memory = image->type_private;
948                 
949                 if (image_memory->cache)
950                         free(image_memory->cache);
951         }
952         else if (image->type == IMAGE_SRECORD)
953         {
954                 image_mot_t *image_mot = image->type_private;
955                 
956                 fileio_close(&image_mot->fileio);
957                 
958                 if (image_mot->buffer)
959                         free(image_mot->buffer);
960         }
961         else if (image->type == IMAGE_BUILDER)
962         {
963                 int i;
964                 
965                 for (i = 0; i < image->num_sections; i++)
966                 {
967                         free(image->sections[i].private);
968                 }
969         }
970
971         if (image->type_private)
972                 free(image->type_private);
973         
974         if (image->sections)
975                 free(image->sections);
976         
977         return ERROR_OK;
978 }