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