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