omap4430: add Blaze config
[openocd/ellerodev.git] / src / target / image.c
blobb0d957f8ab62164f7d9a1d63be442655ef2393f0
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 * Copyright (C) 2009 by Franck Hereson *
12 * franck.hereson@secad.fr *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include "image.h"
34 #include "target.h"
35 #include <helper/log.h>
38 /* convert ELF header field to host endianness */
39 #define field16(elf,field)\
40 ((elf->endianness == ELFDATA2LSB)? \
41 le_to_h_u16((uint8_t*)&field):be_to_h_u16((uint8_t*)&field))
43 #define field32(elf,field)\
44 ((elf->endianness == ELFDATA2LSB)? \
45 le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field))
47 static int autodetect_image_type(struct image *image, const char *url)
49 int retval;
50 struct fileio fileio;
51 size_t read_bytes;
52 uint8_t buffer[9];
54 /* read the first 4 bytes of image */
55 if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
57 return retval;
59 retval = fileio_read(&fileio, 9, buffer, &read_bytes);
61 if (retval == ERROR_OK)
63 if (read_bytes != 9)
65 retval = ERROR_FILEIO_OPERATION_FAILED;
68 fileio_close(&fileio);
70 if (retval != ERROR_OK)
71 return retval;
73 /* check header against known signatures */
74 if (strncmp((char*)buffer,ELFMAG,SELFMAG) == 0)
76 LOG_DEBUG("ELF image detected.");
77 image->type = IMAGE_ELF;
79 else if ((buffer[0]==':') /* record start byte */
80 &&(isxdigit(buffer[1]))
81 &&(isxdigit(buffer[2]))
82 &&(isxdigit(buffer[3]))
83 &&(isxdigit(buffer[4]))
84 &&(isxdigit(buffer[5]))
85 &&(isxdigit(buffer[6]))
86 &&(buffer[7]=='0') /* record type : 00 -> 05 */
87 &&(buffer[8]>='0') && (buffer[8]<'6'))
89 LOG_DEBUG("IHEX image detected.");
90 image->type = IMAGE_IHEX;
92 else if ((buffer[0] == 'S') /* record start byte */
93 &&(isxdigit(buffer[1]))
94 &&(isxdigit(buffer[2]))
95 &&(isxdigit(buffer[3]))
96 &&(buffer[1] >= '0') && (buffer[1] < '9'))
98 LOG_DEBUG("S19 image detected.");
99 image->type = IMAGE_SRECORD;
101 else
103 image->type = IMAGE_BINARY;
106 return ERROR_OK;
109 static int identify_image_type(struct image *image, const char *type_string, const char *url)
111 if (type_string)
113 if (!strcmp(type_string, "bin"))
115 image->type = IMAGE_BINARY;
117 else if (!strcmp(type_string, "ihex"))
119 image->type = IMAGE_IHEX;
121 else if (!strcmp(type_string, "elf"))
123 image->type = IMAGE_ELF;
125 else if (!strcmp(type_string, "mem"))
127 image->type = IMAGE_MEMORY;
129 else if (!strcmp(type_string, "s19"))
131 image->type = IMAGE_SRECORD;
133 else if (!strcmp(type_string, "build"))
135 image->type = IMAGE_BUILDER;
137 else
139 return ERROR_IMAGE_TYPE_UNKNOWN;
142 else
144 return autodetect_image_type(image, url);
147 return ERROR_OK;
150 static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine, struct imagesection *section)
152 struct image_ihex *ihex = image->type_private;
153 struct fileio *fileio = &ihex->fileio;
154 uint32_t full_address = 0x0;
155 uint32_t cooked_bytes;
156 int i;
158 /* we can't determine the number of sections that we'll have to create ahead of time,
159 * so we locally hold them until parsing is finished */
161 int filesize;
162 int retval;
163 retval = fileio_size(fileio, &filesize);
164 if (retval != ERROR_OK)
165 return retval;
167 ihex->buffer = malloc(filesize >> 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;
175 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
177 uint32_t count;
178 uint32_t address;
179 uint32_t record_type;
180 uint32_t checksum;
181 uint8_t cal_checksum = 0;
182 size_t bytes_read = 0;
184 if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32 , &count, &address, &record_type) != 3)
186 return ERROR_IMAGE_FORMAT_ERROR;
188 bytes_read += 9;
190 cal_checksum += (uint8_t)count;
191 cal_checksum += (uint8_t)(address >> 8);
192 cal_checksum += (uint8_t)address;
193 cal_checksum += (uint8_t)record_type;
195 if (record_type == 0) /* Data Record */
197 if ((full_address & 0xffff) != address)
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
203 if (section[image->num_sections].size != 0)
205 image->num_sections++;
206 if (image->num_sections >= IMAGE_MAX_SECTIONS)
208 /* too many sections */
209 LOG_ERROR("Too many sections found in IHEX file");
210 return ERROR_IMAGE_FORMAT_ERROR;
212 section[image->num_sections].size = 0x0;
213 section[image->num_sections].flags = 0;
214 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
216 section[image->num_sections].base_address =
217 (full_address & 0xffff0000) | address;
218 full_address = (full_address & 0xffff0000) | address;
221 while (count-- > 0)
223 unsigned value;
224 sscanf(&lpszLine[bytes_read], "%2x", &value);
225 ihex->buffer[cooked_bytes] = (uint8_t)value;
226 cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
227 bytes_read += 2;
228 cooked_bytes += 1;
229 section[image->num_sections].size += 1;
230 full_address++;
233 else if (record_type == 1) /* End of File Record */
235 /* finish the current section */
236 image->num_sections++;
238 /* copy section information */
239 image->sections = malloc(sizeof(struct imagesection) * image->num_sections);
240 for (i = 0; i < image->num_sections; i++)
242 image->sections[i].private = section[i].private;
243 image->sections[i].base_address = section[i].base_address;
244 image->sections[i].size = section[i].size;
245 image->sections[i].flags = section[i].flags;
248 return ERROR_OK;
250 else if (record_type == 2) /* Linear Address Record */
252 uint16_t upper_address;
254 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
255 cal_checksum += (uint8_t)(upper_address >> 8);
256 cal_checksum += (uint8_t)upper_address;
257 bytes_read += 4;
259 if ((full_address >> 4) != upper_address)
261 /* we encountered a nonconsecutive location, create a new section,
262 * unless the current section has zero size, in which case this specifies
263 * the current section's base address
265 if (section[image->num_sections].size != 0)
267 image->num_sections++;
268 if (image->num_sections >= IMAGE_MAX_SECTIONS)
270 /* too many sections */
271 LOG_ERROR("Too many sections found in IHEX file");
272 return ERROR_IMAGE_FORMAT_ERROR;
274 section[image->num_sections].size = 0x0;
275 section[image->num_sections].flags = 0;
276 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
278 section[image->num_sections].base_address =
279 (full_address & 0xffff) | (upper_address << 4);
280 full_address = (full_address & 0xffff) | (upper_address << 4);
283 else if (record_type == 3) /* Start Segment Address Record */
285 uint32_t dummy;
287 /* "Start Segment Address Record" will not be supported */
288 /* but we must consume it, and do not create an error. */
289 while (count-- > 0)
291 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
292 cal_checksum += (uint8_t)dummy;
293 bytes_read += 2;
296 else if (record_type == 4) /* Extended Linear Address Record */
298 uint16_t upper_address;
300 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
301 cal_checksum += (uint8_t)(upper_address >> 8);
302 cal_checksum += (uint8_t)upper_address;
303 bytes_read += 4;
305 if ((full_address >> 16) != upper_address)
307 /* we encountered a nonconsecutive location, create a new section,
308 * unless the current section has zero size, in which case this specifies
309 * the current section's base address
311 if (section[image->num_sections].size != 0)
313 image->num_sections++;
314 if (image->num_sections >= IMAGE_MAX_SECTIONS)
316 /* too many sections */
317 LOG_ERROR("Too many sections found in IHEX file");
318 return ERROR_IMAGE_FORMAT_ERROR;
320 section[image->num_sections].size = 0x0;
321 section[image->num_sections].flags = 0;
322 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
324 section[image->num_sections].base_address =
325 (full_address & 0xffff) | (upper_address << 16);
326 full_address = (full_address & 0xffff) | (upper_address << 16);
329 else if (record_type == 5) /* Start Linear Address Record */
331 uint32_t start_address;
333 sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
334 cal_checksum += (uint8_t)(start_address >> 24);
335 cal_checksum += (uint8_t)(start_address >> 16);
336 cal_checksum += (uint8_t)(start_address >> 8);
337 cal_checksum += (uint8_t)start_address;
338 bytes_read += 8;
340 image->start_address_set = 1;
341 image->start_address = be_to_h_u32((uint8_t*)&start_address);
343 else
345 LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
346 return ERROR_IMAGE_FORMAT_ERROR;
349 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
350 bytes_read += 2;
352 if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1))
354 /* checksum failed */
355 LOG_ERROR("incorrect record checksum found in IHEX file");
356 return ERROR_IMAGE_CHECKSUM;
360 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
361 return ERROR_IMAGE_FORMAT_ERROR;
365 * Allocate memory dynamically instead of on the stack. This
366 * is important w/embedded hosts.
368 static int image_ihex_buffer_complete(struct image *image)
370 char *lpszLine = malloc(1023);
371 if (lpszLine == NULL)
373 LOG_ERROR("Out of memory");
374 return ERROR_FAIL;
376 struct imagesection *section = malloc(sizeof(struct imagesection) * IMAGE_MAX_SECTIONS);
377 if (section == NULL)
379 free(lpszLine);
380 LOG_ERROR("Out of memory");
381 return ERROR_FAIL;
383 int retval;
385 retval = image_ihex_buffer_complete_inner(image, lpszLine, section);
387 free(section);
388 free(lpszLine);
390 return retval;
393 static int image_elf_read_headers(struct image *image)
395 struct image_elf *elf = image->type_private;
396 size_t read_bytes;
397 uint32_t i,j;
398 int retval;
400 elf->header = malloc(sizeof(Elf32_Ehdr));
402 if (elf->header == NULL)
404 LOG_ERROR("insufficient memory to perform operation ");
405 return ERROR_FILEIO_OPERATION_FAILED;
408 if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
410 LOG_ERROR("cannot read ELF file header, read failed");
411 return ERROR_FILEIO_OPERATION_FAILED;
413 if (read_bytes != sizeof(Elf32_Ehdr))
415 LOG_ERROR("cannot read ELF file header, only partially read");
416 return ERROR_FILEIO_OPERATION_FAILED;
419 if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG) != 0)
421 LOG_ERROR("invalid ELF file, bad magic number");
422 return ERROR_IMAGE_FORMAT_ERROR;
424 if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
426 LOG_ERROR("invalid ELF file, only 32bits files are supported");
427 return ERROR_IMAGE_FORMAT_ERROR;
430 elf->endianness = elf->header->e_ident[EI_DATA];
431 if ((elf->endianness != ELFDATA2LSB)
432 &&(elf->endianness != ELFDATA2MSB))
434 LOG_ERROR("invalid ELF file, unknown endianess setting");
435 return ERROR_IMAGE_FORMAT_ERROR;
438 elf->segment_count = field16(elf,elf->header->e_phnum);
439 if (elf->segment_count == 0)
441 LOG_ERROR("invalid ELF file, no program headers");
442 return ERROR_IMAGE_FORMAT_ERROR;
445 if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
447 LOG_ERROR("cannot seek to ELF program header table, read failed");
448 return retval;
451 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
452 if (elf->segments == NULL)
454 LOG_ERROR("insufficient memory to perform operation ");
455 return ERROR_FILEIO_OPERATION_FAILED;
458 if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
460 LOG_ERROR("cannot read ELF segment headers, read failed");
461 return retval;
463 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
465 LOG_ERROR("cannot read ELF segment headers, only partially read");
466 return ERROR_FILEIO_OPERATION_FAILED;
469 /* count useful segments (loadable), ignore BSS section */
470 image->num_sections = 0;
471 for (i = 0;i < elf->segment_count;i++)
472 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
473 image->num_sections++;
474 /* alloc and fill sections array with loadable segments */
475 image->sections = malloc(image->num_sections * sizeof(struct imagesection));
476 for (i = 0,j = 0;i < elf->segment_count;i++)
478 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
480 image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
481 image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
482 image->sections[j].private = &elf->segments[i];
483 image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
484 j++;
488 image->start_address_set = 1;
489 image->start_address = field32(elf,elf->header->e_entry);
491 return ERROR_OK;
494 static int image_elf_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
496 struct image_elf *elf = image->type_private;
497 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
498 size_t read_size,really_read;
499 int retval;
501 *size_read = 0;
503 LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz = 0x%" PRIx32 ")",section,offset,size);
505 /* read initialized data in current segment if any */
506 if (offset < field32(elf,segment->p_filesz))
508 /* maximal size present in file for the current segment */
509 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
510 LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32 "", read_size,
511 field32(elf,segment->p_offset) + offset);
512 /* read initialized area of the segment */
513 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset) + offset)) != ERROR_OK)
515 LOG_ERROR("cannot find ELF segment content, seek failed");
516 return retval;
518 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
520 LOG_ERROR("cannot read ELF segment content, read failed");
521 return retval;
523 buffer += read_size;
524 size -= read_size;
525 offset += read_size;
526 *size_read += read_size;
527 /* need more data ? */
528 if (!size)
529 return ERROR_OK;
532 return ERROR_OK;
535 static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine, struct imagesection *section)
537 struct image_mot *mot = image->type_private;
538 struct fileio *fileio = &mot->fileio;
539 uint32_t full_address = 0x0;
540 uint32_t cooked_bytes;
541 int i;
543 /* we can't determine the number of sections that we'll have to create ahead of time,
544 * so we locally hold them until parsing is finished */
546 int retval;
547 int filesize;
548 retval = fileio_size(fileio, &filesize);
549 if (retval != ERROR_OK)
550 return retval;
552 mot->buffer = malloc(filesize >> 1);
553 cooked_bytes = 0x0;
554 image->num_sections = 0;
555 section[image->num_sections].private = &mot->buffer[cooked_bytes];
556 section[image->num_sections].base_address = 0x0;
557 section[image->num_sections].size = 0x0;
558 section[image->num_sections].flags = 0;
560 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
562 uint32_t count;
563 uint32_t address;
564 uint32_t record_type;
565 uint32_t checksum;
566 uint8_t cal_checksum = 0;
567 uint32_t bytes_read = 0;
569 /* get record type and record length */
570 if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32 , &record_type, &count) != 2)
572 return ERROR_IMAGE_FORMAT_ERROR;
575 bytes_read += 4;
576 cal_checksum += (uint8_t)count;
578 /* skip checksum byte */
579 count -=1;
581 if (record_type == 0)
583 /* S0 - starting record (optional) */
584 int iValue;
586 while (count-- > 0) {
587 sscanf(&lpszLine[bytes_read], "%2x", &iValue);
588 cal_checksum += (uint8_t)iValue;
589 bytes_read += 2;
592 else if (record_type >= 1 && record_type <= 3)
594 switch (record_type)
596 case 1:
597 /* S1 - 16 bit address data record */
598 sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
599 cal_checksum += (uint8_t)(address >> 8);
600 cal_checksum += (uint8_t)address;
601 bytes_read += 4;
602 count -=2;
603 break;
605 case 2:
606 /* S2 - 24 bit address data record */
607 sscanf(&lpszLine[bytes_read], "%6" SCNx32 , &address);
608 cal_checksum += (uint8_t)(address >> 16);
609 cal_checksum += (uint8_t)(address >> 8);
610 cal_checksum += (uint8_t)address;
611 bytes_read += 6;
612 count -=3;
613 break;
615 case 3:
616 /* S3 - 32 bit address data record */
617 sscanf(&lpszLine[bytes_read], "%8" SCNx32 , &address);
618 cal_checksum += (uint8_t)(address >> 24);
619 cal_checksum += (uint8_t)(address >> 16);
620 cal_checksum += (uint8_t)(address >> 8);
621 cal_checksum += (uint8_t)address;
622 bytes_read += 8;
623 count -=4;
624 break;
628 if (full_address != address)
630 /* we encountered a nonconsecutive location, create a new section,
631 * unless the current section has zero size, in which case this specifies
632 * the current section's base address
634 if (section[image->num_sections].size != 0)
636 image->num_sections++;
637 section[image->num_sections].size = 0x0;
638 section[image->num_sections].flags = 0;
639 section[image->num_sections].private = &mot->buffer[cooked_bytes];
641 section[image->num_sections].base_address = address;
642 full_address = address;
645 while (count-- > 0)
647 unsigned value;
648 sscanf(&lpszLine[bytes_read], "%2x", &value);
649 mot->buffer[cooked_bytes] = (uint8_t)value;
650 cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
651 bytes_read += 2;
652 cooked_bytes += 1;
653 section[image->num_sections].size += 1;
654 full_address++;
657 else if (record_type == 5)
659 /* S5 is the data count record, we ignore it */
660 uint32_t dummy;
662 while (count-- > 0)
664 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
665 cal_checksum += (uint8_t)dummy;
666 bytes_read += 2;
669 else if (record_type >= 7 && record_type <= 9)
671 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
672 image->num_sections++;
674 /* copy section information */
675 image->sections = malloc(sizeof(struct imagesection) * image->num_sections);
676 for (i = 0; i < image->num_sections; i++)
678 image->sections[i].private = section[i].private;
679 image->sections[i].base_address = section[i].base_address;
680 image->sections[i].size = section[i].size;
681 image->sections[i].flags = section[i].flags;
684 return ERROR_OK;
686 else
688 LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
689 return ERROR_IMAGE_FORMAT_ERROR;
692 /* account for checksum, will always be 0xFF */
693 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
694 cal_checksum += (uint8_t)checksum;
695 bytes_read += 2;
697 if (cal_checksum != 0xFF)
699 /* checksum failed */
700 LOG_ERROR("incorrect record checksum found in S19 file");
701 return ERROR_IMAGE_CHECKSUM;
705 LOG_ERROR("premature end of S19 file, no end-of-file record found");
706 return ERROR_IMAGE_FORMAT_ERROR;
710 * Allocate memory dynamically instead of on the stack. This
711 * is important w/embedded hosts.
713 static int image_mot_buffer_complete(struct image *image)
715 char *lpszLine = malloc(1023);
716 if (lpszLine == NULL)
718 LOG_ERROR("Out of memory");
719 return ERROR_FAIL;
721 struct imagesection *section = malloc(sizeof(struct imagesection) * IMAGE_MAX_SECTIONS);
722 if (section == NULL)
724 free(lpszLine);
725 LOG_ERROR("Out of memory");
726 return ERROR_FAIL;
728 int retval;
730 retval = image_mot_buffer_complete_inner(image, lpszLine, section);
732 free(section);
733 free(lpszLine);
735 return retval;
739 int image_open(struct image *image, const char *url, const char *type_string)
741 int retval = ERROR_OK;
743 if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
745 return retval;
748 if (image->type == IMAGE_BINARY)
750 struct image_binary *image_binary;
752 image_binary = image->type_private = malloc(sizeof(struct image_binary));
754 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
756 return retval;
758 int filesize;
759 retval = fileio_size(&image_binary->fileio, &filesize);
760 if (retval != ERROR_OK)
762 fileio_close(&image_binary->fileio);
763 return retval;
766 image->num_sections = 1;
767 image->sections = malloc(sizeof(struct imagesection));
768 image->sections[0].base_address = 0x0;
769 image->sections[0].size = filesize;
770 image->sections[0].flags = 0;
772 else if (image->type == IMAGE_IHEX)
774 struct image_ihex *image_ihex;
776 image_ihex = image->type_private = malloc(sizeof(struct image_ihex));
778 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
780 return retval;
783 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
785 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
786 fileio_close(&image_ihex->fileio);
787 return retval;
790 else if (image->type == IMAGE_ELF)
792 struct image_elf *image_elf;
794 image_elf = image->type_private = malloc(sizeof(struct image_elf));
796 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
798 return retval;
801 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
803 fileio_close(&image_elf->fileio);
804 return retval;
807 else if (image->type == IMAGE_MEMORY)
809 struct target *target = get_target(url);
811 if (target == NULL)
813 LOG_ERROR("target '%s' not defined", url);
814 return ERROR_FAIL;
817 struct image_memory *image_memory;
819 image->num_sections = 1;
820 image->sections = malloc(sizeof(struct imagesection));
821 image->sections[0].base_address = 0x0;
822 image->sections[0].size = 0xffffffff;
823 image->sections[0].flags = 0;
825 image_memory = image->type_private = malloc(sizeof(struct image_memory));
827 image_memory->target = target;
828 image_memory->cache = NULL;
829 image_memory->cache_address = 0x0;
831 else if (image->type == IMAGE_SRECORD)
833 struct image_mot *image_mot;
835 image_mot = image->type_private = malloc(sizeof(struct image_mot));
837 if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
839 return retval;
842 if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
844 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
845 fileio_close(&image_mot->fileio);
846 return retval;
849 else if (image->type == IMAGE_BUILDER)
851 image->num_sections = 0;
852 image->sections = NULL;
853 image->type_private = NULL;
856 if (image->base_address_set)
858 /* relocate */
859 int section;
860 for (section = 0; section < image->num_sections; section++)
862 image->sections[section].base_address += image->base_address;
864 /* we're done relocating. The two statements below are mainly
865 * for documenation purposes: stop anyone from empirically
866 * thinking they should use these values henceforth. */
867 image->base_address = 0;
868 image->base_address_set = 0;
871 return retval;
874 int image_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
876 int retval;
878 /* don't read past the end of a section */
879 if (offset + size > image->sections[section].size)
881 LOG_DEBUG("read past end of section: 0x%8.8" PRIx32 " + 0x%8.8" PRIx32 " > 0x%8.8" PRIx32 "",
882 offset, size, image->sections[section].size);
883 return ERROR_INVALID_ARGUMENTS;
886 if (image->type == IMAGE_BINARY)
888 struct image_binary *image_binary = image->type_private;
890 /* only one section in a plain binary */
891 if (section != 0)
892 return ERROR_INVALID_ARGUMENTS;
894 /* seek to offset */
895 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
897 return retval;
900 /* return requested bytes */
901 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
903 return retval;
906 else if (image->type == IMAGE_IHEX)
908 memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
909 *size_read = size;
911 return ERROR_OK;
913 else if (image->type == IMAGE_ELF)
915 return image_elf_read_section(image, section, offset, size, buffer, size_read);
917 else if (image->type == IMAGE_MEMORY)
919 struct image_memory *image_memory = image->type_private;
920 uint32_t address = image->sections[section].base_address + offset;
922 *size_read = 0;
924 while ((size - *size_read) > 0)
926 uint32_t size_in_cache;
928 if (!image_memory->cache
929 || (address < image_memory->cache_address)
930 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
932 if (!image_memory->cache)
933 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
935 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
936 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
938 free(image_memory->cache);
939 image_memory->cache = NULL;
940 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
942 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
945 size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
947 memcpy(buffer + *size_read,
948 image_memory->cache + (address - image_memory->cache_address),
949 (size_in_cache > size) ? size : size_in_cache
952 *size_read += (size_in_cache > size) ? size : size_in_cache;
953 address += (size_in_cache > size) ? size : size_in_cache;
956 else if (image->type == IMAGE_SRECORD)
958 memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
959 *size_read = size;
961 return ERROR_OK;
963 else if (image->type == IMAGE_BUILDER)
965 memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
966 *size_read = size;
968 return ERROR_OK;
971 return ERROR_OK;
974 int image_add_section(struct image *image, uint32_t base, uint32_t size, int flags, uint8_t *data)
976 struct imagesection *section;
978 /* only image builder supports adding sections */
979 if (image->type != IMAGE_BUILDER)
980 return ERROR_INVALID_ARGUMENTS;
982 /* see if there's a previous section */
983 if (image->num_sections)
985 section = &image->sections[image->num_sections - 1];
987 /* see if it's enough to extend the last section,
988 * adding data to previous sections or merging is not supported */
989 if (((section->base_address + section->size) == base) && (section->flags == flags))
991 section->private = realloc(section->private, section->size + size);
992 memcpy((uint8_t*)section->private + section->size, data, size);
993 section->size += size;
994 return ERROR_OK;
998 /* allocate new section */
999 image->num_sections++;
1000 image->sections = realloc(image->sections, sizeof(struct imagesection) * image->num_sections);
1001 section = &image->sections[image->num_sections - 1];
1002 section->base_address = base;
1003 section->size = size;
1004 section->flags = flags;
1005 section->private = malloc(sizeof(uint8_t) * size);
1006 memcpy((uint8_t*)section->private, data, size);
1008 return ERROR_OK;
1011 void image_close(struct image *image)
1013 if (image->type == IMAGE_BINARY)
1015 struct image_binary *image_binary = image->type_private;
1017 fileio_close(&image_binary->fileio);
1019 else if (image->type == IMAGE_IHEX)
1021 struct image_ihex *image_ihex = image->type_private;
1023 fileio_close(&image_ihex->fileio);
1025 if (image_ihex->buffer)
1027 free(image_ihex->buffer);
1028 image_ihex->buffer = NULL;
1031 else if (image->type == IMAGE_ELF)
1033 struct image_elf *image_elf = image->type_private;
1035 fileio_close(&image_elf->fileio);
1037 if (image_elf->header)
1039 free(image_elf->header);
1040 image_elf->header = NULL;
1043 if (image_elf->segments)
1045 free(image_elf->segments);
1046 image_elf->segments = NULL;
1049 else if (image->type == IMAGE_MEMORY)
1051 struct image_memory *image_memory = image->type_private;
1053 if (image_memory->cache)
1055 free(image_memory->cache);
1056 image_memory->cache = NULL;
1059 else if (image->type == IMAGE_SRECORD)
1061 struct image_mot *image_mot = image->type_private;
1063 fileio_close(&image_mot->fileio);
1065 if (image_mot->buffer)
1067 free(image_mot->buffer);
1068 image_mot->buffer = NULL;
1071 else if (image->type == IMAGE_BUILDER)
1073 int i;
1075 for (i = 0; i < image->num_sections; i++)
1077 free(image->sections[i].private);
1078 image->sections[i].private = NULL;
1082 if (image->type_private)
1084 free(image->type_private);
1085 image->type_private = NULL;
1088 if (image->sections)
1090 free(image->sections);
1091 image->sections = NULL;
1095 int image_calculate_checksum(uint8_t* buffer, uint32_t nbytes, uint32_t* checksum)
1097 uint32_t crc = 0xffffffff;
1098 LOG_DEBUG("Calculating checksum");
1100 static uint32_t crc32_table[256];
1102 static bool first_init = false;
1103 if (!first_init)
1105 /* Initialize the CRC table and the decoding table. */
1106 int i, j;
1107 unsigned int c;
1108 for (i = 0; i < 256; i++)
1110 /* as per gdb */
1111 for (c = i << 24, j = 8; j > 0; --j)
1112 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1113 crc32_table[i] = c;
1116 first_init = true;
1119 while (nbytes > 0)
1121 int run = nbytes;
1122 if (run > 32768)
1124 run = 32768;
1126 nbytes -= run;
1127 while (run--)
1129 /* as per gdb */
1130 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
1132 keep_alive();
1135 LOG_DEBUG("Calculating checksum done");
1137 *checksum = crc;
1138 return ERROR_OK;