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