bugfix: stack corruption loading IHex images
[openocd/simonqian.git] / src / target / image.c
blobb9e641b331217bbc41a4d35f5af1f0c9d319bd58
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 "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(image_t *image, char *url)
49 int retval;
50 fileio_t fileio;
51 uint32_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(image_t *image, char *type_string, 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(image_t *image)
152 image_ihex_t *ihex = image->type_private;
153 fileio_t *fileio = &ihex->fileio;
154 uint32_t full_address = 0x0;
155 uint32_t cooked_bytes;
156 int i;
157 char lpszLine[1023];
159 /* we can't determine the number of sections that we'll have to create ahead of time,
160 * so we locally hold them until parsing is finished */
161 image_section_t section[IMAGE_MAX_SECTIONS];
163 ihex->buffer = malloc(fileio->size >> 1);
164 cooked_bytes = 0x0;
165 image->num_sections = 0;
166 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
167 section[image->num_sections].base_address = 0x0;
168 section[image->num_sections].size = 0x0;
169 section[image->num_sections].flags = 0;
171 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
173 uint32_t count;
174 uint32_t address;
175 uint32_t record_type;
176 uint32_t checksum;
177 uint8_t cal_checksum = 0;
178 uint32_t bytes_read = 0;
180 if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32 , &count, &address, &record_type) != 3)
182 return ERROR_IMAGE_FORMAT_ERROR;
184 bytes_read += 9;
186 cal_checksum += (uint8_t)count;
187 cal_checksum += (uint8_t)(address >> 8);
188 cal_checksum += (uint8_t)address;
189 cal_checksum += (uint8_t)record_type;
191 if (record_type == 0) /* Data Record */
193 if ((full_address & 0xffff) != address)
195 /* we encountered a nonconsecutive location, create a new section,
196 * unless the current section has zero size, in which case this specifies
197 * the current section's base address
199 if (section[image->num_sections].size != 0)
201 image->num_sections++;
202 if (image->num_sections >= IMAGE_MAX_SECTIONS)
204 /* too many sections */
205 LOG_ERROR("Too many sections found in IHEX file");
206 return ERROR_IMAGE_FORMAT_ERROR;
208 section[image->num_sections].size = 0x0;
209 section[image->num_sections].flags = 0;
210 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
212 section[image->num_sections].base_address =
213 (full_address & 0xffff0000) | address;
214 full_address = (full_address & 0xffff0000) | address;
217 while (count-- > 0)
219 unsigned value;
220 sscanf(&lpszLine[bytes_read], "%2x", &value);
221 ihex->buffer[cooked_bytes] = (uint8_t)value;
222 cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
223 bytes_read += 2;
224 cooked_bytes += 1;
225 section[image->num_sections].size += 1;
226 full_address++;
229 else if (record_type == 1) /* End of File Record */
231 /* finish the current section */
232 image->num_sections++;
234 /* copy section information */
235 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
236 for (i = 0; i < image->num_sections; i++)
238 image->sections[i].private = section[i].private;
239 image->sections[i].base_address = section[i].base_address;
240 image->sections[i].size = section[i].size;
241 image->sections[i].flags = section[i].flags;
244 return ERROR_OK;
246 else if (record_type == 2) /* Linear Address Record */
248 uint16_t upper_address;
250 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
251 cal_checksum += (uint8_t)(upper_address >> 8);
252 cal_checksum += (uint8_t)upper_address;
253 bytes_read += 4;
255 if ((full_address >> 4) != upper_address)
257 /* we encountered a nonconsecutive location, create a new section,
258 * unless the current section has zero size, in which case this specifies
259 * the current section's base address
261 if (section[image->num_sections].size != 0)
263 image->num_sections++;
264 if (image->num_sections >= IMAGE_MAX_SECTIONS)
266 /* too many sections */
267 LOG_ERROR("Too many sections found in IHEX file");
268 return ERROR_IMAGE_FORMAT_ERROR;
270 section[image->num_sections].size = 0x0;
271 section[image->num_sections].flags = 0;
272 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
274 section[image->num_sections].base_address =
275 (full_address & 0xffff) | (upper_address << 4);
276 full_address = (full_address & 0xffff) | (upper_address << 4);
279 else if (record_type == 3) /* Start Segment Address Record */
281 uint32_t dummy;
283 /* "Start Segment Address Record" will not be supported */
284 /* but we must consume it, and do not create an error. */
285 while (count-- > 0)
287 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
288 cal_checksum += (uint8_t)dummy;
289 bytes_read += 2;
292 else if (record_type == 4) /* Extended Linear Address Record */
294 uint16_t upper_address;
296 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
297 cal_checksum += (uint8_t)(upper_address >> 8);
298 cal_checksum += (uint8_t)upper_address;
299 bytes_read += 4;
301 if ((full_address >> 16) != upper_address)
303 /* we encountered a nonconsecutive location, create a new section,
304 * unless the current section has zero size, in which case this specifies
305 * the current section's base address
307 if (section[image->num_sections].size != 0)
309 image->num_sections++;
310 if (image->num_sections >= IMAGE_MAX_SECTIONS)
312 /* too many sections */
313 LOG_ERROR("Too many sections found in IHEX file");
314 return ERROR_IMAGE_FORMAT_ERROR;
316 section[image->num_sections].size = 0x0;
317 section[image->num_sections].flags = 0;
318 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
320 section[image->num_sections].base_address =
321 (full_address & 0xffff) | (upper_address << 16);
322 full_address = (full_address & 0xffff) | (upper_address << 16);
325 else if (record_type == 5) /* Start Linear Address Record */
327 uint32_t start_address;
329 sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
330 cal_checksum += (uint8_t)(start_address >> 24);
331 cal_checksum += (uint8_t)(start_address >> 16);
332 cal_checksum += (uint8_t)(start_address >> 8);
333 cal_checksum += (uint8_t)start_address;
334 bytes_read += 8;
336 image->start_address_set = 1;
337 image->start_address = be_to_h_u32((uint8_t*)&start_address);
339 else
341 LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
342 return ERROR_IMAGE_FORMAT_ERROR;
345 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
346 bytes_read += 2;
348 if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1))
350 /* checksum failed */
351 LOG_ERROR("incorrect record checksum found in IHEX file");
352 return ERROR_IMAGE_CHECKSUM;
356 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
357 return ERROR_IMAGE_FORMAT_ERROR;
360 static int image_elf_read_headers(image_t *image)
362 image_elf_t *elf = image->type_private;
363 uint32_t read_bytes;
364 uint32_t i,j;
365 int retval;
367 elf->header = malloc(sizeof(Elf32_Ehdr));
369 if (elf->header == NULL)
371 LOG_ERROR("insufficient memory to perform operation ");
372 return ERROR_FILEIO_OPERATION_FAILED;
375 if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
377 LOG_ERROR("cannot read ELF file header, read failed");
378 return ERROR_FILEIO_OPERATION_FAILED;
380 if (read_bytes != sizeof(Elf32_Ehdr))
382 LOG_ERROR("cannot read ELF file header, only partially read");
383 return ERROR_FILEIO_OPERATION_FAILED;
386 if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG) != 0)
388 LOG_ERROR("invalid ELF file, bad magic number");
389 return ERROR_IMAGE_FORMAT_ERROR;
391 if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
393 LOG_ERROR("invalid ELF file, only 32bits files are supported");
394 return ERROR_IMAGE_FORMAT_ERROR;
397 elf->endianness = elf->header->e_ident[EI_DATA];
398 if ((elf->endianness != ELFDATA2LSB)
399 &&(elf->endianness != ELFDATA2MSB))
401 LOG_ERROR("invalid ELF file, unknown endianess setting");
402 return ERROR_IMAGE_FORMAT_ERROR;
405 elf->segment_count = field16(elf,elf->header->e_phnum);
406 if (elf->segment_count == 0)
408 LOG_ERROR("invalid ELF file, no program headers");
409 return ERROR_IMAGE_FORMAT_ERROR;
412 if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
414 LOG_ERROR("cannot seek to ELF program header table, read failed");
415 return retval;
418 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
419 if (elf->segments == NULL)
421 LOG_ERROR("insufficient memory to perform operation ");
422 return ERROR_FILEIO_OPERATION_FAILED;
425 if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
427 LOG_ERROR("cannot read ELF segment headers, read failed");
428 return retval;
430 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
432 LOG_ERROR("cannot read ELF segment headers, only partially read");
433 return ERROR_FILEIO_OPERATION_FAILED;
436 /* count useful segments (loadable), ignore BSS section */
437 image->num_sections = 0;
438 for (i = 0;i < elf->segment_count;i++)
439 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
440 image->num_sections++;
441 /* alloc and fill sections array with loadable segments */
442 image->sections = malloc(image->num_sections * sizeof(image_section_t));
443 for (i = 0,j = 0;i < elf->segment_count;i++)
445 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
447 image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
448 image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
449 image->sections[j].private = &elf->segments[i];
450 image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
451 j++;
455 image->start_address_set = 1;
456 image->start_address = field32(elf,elf->header->e_entry);
458 return ERROR_OK;
461 static int image_elf_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
463 image_elf_t *elf = image->type_private;
464 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
465 uint32_t read_size,really_read;
466 int retval;
468 *size_read = 0;
470 LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz = 0x%" PRIx32 ")",section,offset,size);
472 /* read initialized data in current segment if any */
473 if (offset < field32(elf,segment->p_filesz))
475 /* maximal size present in file for the current segment */
476 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
477 LOG_DEBUG("read elf: size = 0x%" PRIx32 " at 0x%" PRIx32 "",read_size,
478 field32(elf,segment->p_offset) + offset);
479 /* read initialized area of the segment */
480 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset) + offset)) != ERROR_OK)
482 LOG_ERROR("cannot find ELF segment content, seek failed");
483 return retval;
485 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
487 LOG_ERROR("cannot read ELF segment content, read failed");
488 return retval;
490 buffer += read_size;
491 size -= read_size;
492 offset += read_size;
493 *size_read += read_size;
494 /* need more data ? */
495 if (!size)
496 return ERROR_OK;
499 return ERROR_OK;
502 static int image_mot_buffer_complete(image_t *image)
504 image_mot_t *mot = image->type_private;
505 fileio_t *fileio = &mot->fileio;
506 uint32_t full_address = 0x0;
507 uint32_t cooked_bytes;
508 int i;
509 char lpszLine[1023];
511 /* we can't determine the number of sections that we'll have to create ahead of time,
512 * so we locally hold them until parsing is finished */
513 image_section_t section[IMAGE_MAX_SECTIONS];
515 mot->buffer = malloc(fileio->size >> 1);
516 cooked_bytes = 0x0;
517 image->num_sections = 0;
518 section[image->num_sections].private = &mot->buffer[cooked_bytes];
519 section[image->num_sections].base_address = 0x0;
520 section[image->num_sections].size = 0x0;
521 section[image->num_sections].flags = 0;
523 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
525 uint32_t count;
526 uint32_t address;
527 uint32_t record_type;
528 uint32_t checksum;
529 uint8_t cal_checksum = 0;
530 uint32_t bytes_read = 0;
532 /* get record type and record length */
533 if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32 , &record_type, &count) != 2)
535 return ERROR_IMAGE_FORMAT_ERROR;
538 bytes_read += 4;
539 cal_checksum += (uint8_t)count;
541 /* skip checksum byte */
542 count -=1;
544 if (record_type == 0)
546 /* S0 - starting record (optional) */
547 int iValue;
549 while (count-- > 0) {
550 sscanf(&lpszLine[bytes_read], "%2x", &iValue);
551 cal_checksum += (uint8_t)iValue;
552 bytes_read += 2;
555 else if (record_type >= 1 && record_type <= 3)
557 switch (record_type)
559 case 1:
560 /* S1 - 16 bit address data record */
561 sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
562 cal_checksum += (uint8_t)(address >> 8);
563 cal_checksum += (uint8_t)address;
564 bytes_read += 4;
565 count -=2;
566 break;
568 case 2:
569 /* S2 - 24 bit address data record */
570 sscanf(&lpszLine[bytes_read], "%6" SCNx32 , &address);
571 cal_checksum += (uint8_t)(address >> 16);
572 cal_checksum += (uint8_t)(address >> 8);
573 cal_checksum += (uint8_t)address;
574 bytes_read += 6;
575 count -=3;
576 break;
578 case 3:
579 /* S3 - 32 bit address data record */
580 sscanf(&lpszLine[bytes_read], "%8" SCNx32 , &address);
581 cal_checksum += (uint8_t)(address >> 24);
582 cal_checksum += (uint8_t)(address >> 16);
583 cal_checksum += (uint8_t)(address >> 8);
584 cal_checksum += (uint8_t)address;
585 bytes_read += 8;
586 count -=4;
587 break;
591 if (full_address != address)
593 /* we encountered a nonconsecutive location, create a new section,
594 * unless the current section has zero size, in which case this specifies
595 * the current section's base address
597 if (section[image->num_sections].size != 0)
599 image->num_sections++;
600 section[image->num_sections].size = 0x0;
601 section[image->num_sections].flags = 0;
602 section[image->num_sections].private = &mot->buffer[cooked_bytes];
604 section[image->num_sections].base_address = address;
605 full_address = address;
608 while (count-- > 0)
610 unsigned value;
611 sscanf(&lpszLine[bytes_read], "%2x", &value);
612 mot->buffer[cooked_bytes] = (uint8_t)value;
613 cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
614 bytes_read += 2;
615 cooked_bytes += 1;
616 section[image->num_sections].size += 1;
617 full_address++;
620 else if (record_type == 5)
622 /* S5 is the data count record, we ignore it */
623 uint32_t dummy;
625 while (count-- > 0)
627 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
628 cal_checksum += (uint8_t)dummy;
629 bytes_read += 2;
632 else if (record_type >= 7 && record_type <= 9)
634 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
635 image->num_sections++;
637 /* copy section information */
638 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
639 for (i = 0; i < image->num_sections; i++)
641 image->sections[i].private = section[i].private;
642 image->sections[i].base_address = section[i].base_address;
643 image->sections[i].size = section[i].size;
644 image->sections[i].flags = section[i].flags;
647 return ERROR_OK;
649 else
651 LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
652 return ERROR_IMAGE_FORMAT_ERROR;
655 /* account for checksum, will always be 0xFF */
656 sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
657 cal_checksum += (uint8_t)checksum;
658 bytes_read += 2;
660 if (cal_checksum != 0xFF)
662 /* checksum failed */
663 LOG_ERROR("incorrect record checksum found in S19 file");
664 return ERROR_IMAGE_CHECKSUM;
668 LOG_ERROR("premature end of S19 file, no end-of-file record found");
669 return ERROR_IMAGE_FORMAT_ERROR;
672 int image_open(image_t *image, char *url, char *type_string)
674 int retval = ERROR_OK;
676 if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
678 return retval;
681 if (image->type == IMAGE_BINARY)
683 image_binary_t *image_binary;
685 image_binary = image->type_private = malloc(sizeof(image_binary_t));
687 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
689 return retval;
692 image->num_sections = 1;
693 image->sections = malloc(sizeof(image_section_t));
694 image->sections[0].base_address = 0x0;
695 image->sections[0].size = image_binary->fileio.size;
696 image->sections[0].flags = 0;
698 else if (image->type == IMAGE_IHEX)
700 image_ihex_t *image_ihex;
702 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
704 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
706 return retval;
709 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
711 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
712 fileio_close(&image_ihex->fileio);
713 return retval;
716 else if (image->type == IMAGE_ELF)
718 image_elf_t *image_elf;
720 image_elf = image->type_private = malloc(sizeof(image_elf_t));
722 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
724 return retval;
727 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
729 fileio_close(&image_elf->fileio);
730 return retval;
733 else if (image->type == IMAGE_MEMORY)
735 target_t *target = get_target(url);
737 if (target == NULL)
739 LOG_ERROR("target '%s' not defined", url);
740 return ERROR_FAIL;
743 image_memory_t *image_memory;
745 image->num_sections = 1;
746 image->sections = malloc(sizeof(image_section_t));
747 image->sections[0].base_address = 0x0;
748 image->sections[0].size = 0xffffffff;
749 image->sections[0].flags = 0;
751 image_memory = image->type_private = malloc(sizeof(image_memory_t));
753 image_memory->target = target;
754 image_memory->cache = NULL;
755 image_memory->cache_address = 0x0;
757 else if (image->type == IMAGE_SRECORD)
759 image_mot_t *image_mot;
761 image_mot = image->type_private = malloc(sizeof(image_mot_t));
763 if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
765 return retval;
768 if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
770 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
771 fileio_close(&image_mot->fileio);
772 return retval;
775 else if (image->type == IMAGE_BUILDER)
777 image->num_sections = 0;
778 image->sections = NULL;
779 image->type_private = NULL;
782 if (image->base_address_set)
784 /* relocate */
785 int section;
786 for (section = 0; section < image->num_sections; section++)
788 image->sections[section].base_address += image->base_address;
790 /* we're done relocating. The two statements below are mainly
791 * for documenation purposes: stop anyone from empirically
792 * thinking they should use these values henceforth. */
793 image->base_address = 0;
794 image->base_address_set = 0;
797 return retval;
800 int image_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
802 int retval;
804 /* don't read past the end of a section */
805 if (offset + size > image->sections[section].size)
807 LOG_DEBUG("read past end of section: 0x%8.8" PRIx32 " + 0x%8.8" PRIx32 " > 0x%8.8" PRIx32 "",
808 offset, size, image->sections[section].size);
809 return ERROR_INVALID_ARGUMENTS;
812 if (image->type == IMAGE_BINARY)
814 image_binary_t *image_binary = image->type_private;
816 /* only one section in a plain binary */
817 if (section != 0)
818 return ERROR_INVALID_ARGUMENTS;
820 /* seek to offset */
821 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
823 return retval;
826 /* return requested bytes */
827 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
829 return retval;
832 else if (image->type == IMAGE_IHEX)
834 memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
835 *size_read = size;
837 return ERROR_OK;
839 else if (image->type == IMAGE_ELF)
841 return image_elf_read_section(image, section, offset, size, buffer, size_read);
843 else if (image->type == IMAGE_MEMORY)
845 image_memory_t *image_memory = image->type_private;
846 uint32_t address = image->sections[section].base_address + offset;
848 *size_read = 0;
850 while ((size - *size_read) > 0)
852 uint32_t size_in_cache;
854 if (!image_memory->cache
855 || (address < image_memory->cache_address)
856 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
858 if (!image_memory->cache)
859 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
861 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
862 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
864 free(image_memory->cache);
865 image_memory->cache = NULL;
866 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
868 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
871 size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
873 memcpy(buffer + *size_read,
874 image_memory->cache + (address - image_memory->cache_address),
875 (size_in_cache > size) ? size : size_in_cache
878 *size_read += (size_in_cache > size) ? size : size_in_cache;
879 address += (size_in_cache > size) ? size : size_in_cache;
882 else if (image->type == IMAGE_SRECORD)
884 memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
885 *size_read = size;
887 return ERROR_OK;
889 else if (image->type == IMAGE_BUILDER)
891 memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
892 *size_read = size;
894 return ERROR_OK;
897 return ERROR_OK;
900 int image_add_section(image_t *image, uint32_t base, uint32_t size, int flags, uint8_t *data)
902 image_section_t *section;
904 /* only image builder supports adding sections */
905 if (image->type != IMAGE_BUILDER)
906 return ERROR_INVALID_ARGUMENTS;
908 /* see if there's a previous section */
909 if (image->num_sections)
911 section = &image->sections[image->num_sections - 1];
913 /* see if it's enough to extend the last section,
914 * adding data to previous sections or merging is not supported */
915 if (((section->base_address + section->size) == base) && (section->flags == flags))
917 section->private = realloc(section->private, section->size + size);
918 memcpy((uint8_t*)section->private + section->size, data, size);
919 section->size += size;
920 return ERROR_OK;
924 /* allocate new section */
925 image->num_sections++;
926 image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
927 section = &image->sections[image->num_sections - 1];
928 section->base_address = base;
929 section->size = size;
930 section->flags = flags;
931 section->private = malloc(sizeof(uint8_t) * size);
932 memcpy((uint8_t*)section->private, data, size);
934 return ERROR_OK;
937 void image_close(image_t *image)
939 if (image->type == IMAGE_BINARY)
941 image_binary_t *image_binary = image->type_private;
943 fileio_close(&image_binary->fileio);
945 else if (image->type == IMAGE_IHEX)
947 image_ihex_t *image_ihex = image->type_private;
949 fileio_close(&image_ihex->fileio);
951 if (image_ihex->buffer)
953 free(image_ihex->buffer);
954 image_ihex->buffer = NULL;
957 else if (image->type == IMAGE_ELF)
959 image_elf_t *image_elf = image->type_private;
961 fileio_close(&image_elf->fileio);
963 if (image_elf->header)
965 free(image_elf->header);
966 image_elf->header = NULL;
969 if (image_elf->segments)
971 free(image_elf->segments);
972 image_elf->segments = NULL;
975 else if (image->type == IMAGE_MEMORY)
977 image_memory_t *image_memory = image->type_private;
979 if (image_memory->cache)
981 free(image_memory->cache);
982 image_memory->cache = NULL;
985 else if (image->type == IMAGE_SRECORD)
987 image_mot_t *image_mot = image->type_private;
989 fileio_close(&image_mot->fileio);
991 if (image_mot->buffer)
993 free(image_mot->buffer);
994 image_mot->buffer = NULL;
997 else if (image->type == IMAGE_BUILDER)
999 int i;
1001 for (i = 0; i < image->num_sections; i++)
1003 free(image->sections[i].private);
1004 image->sections[i].private = NULL;
1008 if (image->type_private)
1010 free(image->type_private);
1011 image->type_private = NULL;
1014 if (image->sections)
1016 free(image->sections);
1017 image->sections = NULL;
1021 int image_calculate_checksum(uint8_t* buffer, uint32_t nbytes, uint32_t* checksum)
1023 uint32_t crc = 0xffffffff;
1024 LOG_DEBUG("Calculating checksum");
1026 uint32_t crc32_table[256];
1028 /* Initialize the CRC table and the decoding table. */
1029 int i, j;
1030 unsigned int c;
1031 for (i = 0; i < 256; i++)
1033 /* as per gdb */
1034 for (c = i << 24, j = 8; j > 0; --j)
1035 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1036 crc32_table[i] = c;
1039 while (nbytes > 0)
1041 int run = nbytes;
1042 if (run > 32768)
1044 run = 32768;
1046 nbytes -= run;
1047 while (run--)
1049 /* as per gdb */
1050 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
1052 keep_alive();
1055 LOG_DEBUG("Calculating checksum done");
1057 *checksum = crc;
1058 return ERROR_OK;