1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
39 #include "replacements.h"
45 /* convert ELF header field to host endianness */
46 #define field16(elf,field)\
47 ((elf->endianness==ELFDATA2LSB)? \
48 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
50 #define field32(elf,field)\
51 ((elf->endianness==ELFDATA2LSB)? \
52 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
54 static int autodetect_image_type(image_t *image, char *url)
61 /* read the first 4 bytes of image */
62 if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
66 retval = fileio_read(&fileio, 9, buffer, &read_bytes);
72 retval=ERROR_FILEIO_OPERATION_FAILED;
75 fileio_close(&fileio);
80 /* check header against known signatures */
81 if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
83 LOG_DEBUG("ELF image detected.");
84 image->type = IMAGE_ELF;
86 else if ((buffer[0]==':') /* record start byte */
87 &&(isxdigit(buffer[1]))
88 &&(isxdigit(buffer[2]))
89 &&(isxdigit(buffer[3]))
90 &&(isxdigit(buffer[4]))
91 &&(isxdigit(buffer[5]))
92 &&(isxdigit(buffer[6]))
93 &&(buffer[7]=='0') /* record type : 00 -> 05 */
94 &&(buffer[8]>='0')&&(buffer[8]<'6'))
96 LOG_DEBUG("IHEX image detected.");
97 image->type = IMAGE_IHEX;
99 else if ((buffer[0] == 'S') /* record start byte */
100 &&(isxdigit(buffer[1]))
101 &&(isxdigit(buffer[2]))
102 &&(isxdigit(buffer[3]))
103 &&(buffer[1] >= '0') && (buffer[1] < '9'))
105 LOG_DEBUG("S19 image detected.");
106 image->type = IMAGE_SRECORD;
110 image->type = IMAGE_BINARY;
116 static int identify_image_type(image_t *image, char *type_string, char *url)
120 if (!strcmp(type_string, "bin"))
122 image->type = IMAGE_BINARY;
124 else if (!strcmp(type_string, "ihex"))
126 image->type = IMAGE_IHEX;
128 else if (!strcmp(type_string, "elf"))
130 image->type = IMAGE_ELF;
132 else if (!strcmp(type_string, "mem"))
134 image->type = IMAGE_MEMORY;
136 else if (!strcmp(type_string, "s19"))
138 image->type = IMAGE_SRECORD;
140 else if (!strcmp(type_string, "build"))
142 image->type = IMAGE_BUILDER;
146 return ERROR_IMAGE_TYPE_UNKNOWN;
151 return autodetect_image_type(image, url);
157 static int image_ihex_buffer_complete(image_t *image)
159 image_ihex_t *ihex = image->type_private;
160 fileio_t *fileio = &ihex->fileio;
161 u32 full_address = 0x0;
166 /* we can't determine the number of sections that we'll have to create ahead of time,
167 * so we locally hold them until parsing is finished */
168 image_section_t section[IMAGE_MAX_SECTIONS];
170 ihex->buffer = malloc(fileio->size >> 1);
172 image->num_sections = 0;
173 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
174 section[image->num_sections].base_address = 0x0;
175 section[image->num_sections].size = 0x0;
176 section[image->num_sections].flags = 0;
178 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
187 if (sscanf(&lpszLine[bytes_read], ":%2x%4x%2x", &count, &address, &record_type) != 3)
189 return ERROR_IMAGE_FORMAT_ERROR;
193 cal_checksum += (u8)count;
194 cal_checksum += (u8)(address >> 8);
195 cal_checksum += (u8)address;
196 cal_checksum += (u8)record_type;
198 if (record_type == 0) /* Data Record */
200 if ((full_address & 0xffff) != address)
202 /* we encountered a nonconsecutive location, create a new section,
203 * unless the current section has zero size, in which case this specifies
204 * the current section's base address
206 if (section[image->num_sections].size != 0)
208 image->num_sections++;
209 section[image->num_sections].size = 0x0;
210 section[image->num_sections].flags = 0;
211 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
213 section[image->num_sections].base_address =
214 (full_address & 0xffff0000) | address;
215 full_address = (full_address & 0xffff0000) | address;
221 sscanf(&lpszLine[bytes_read], "%2x", &value);
222 ihex->buffer[cooked_bytes] = (u8)value;
223 cal_checksum += (u8)ihex->buffer[cooked_bytes];
226 section[image->num_sections].size += 1;
230 else if (record_type == 1) /* End of File Record */
232 /* finish the current section */
233 image->num_sections++;
235 /* copy section information */
236 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
237 for (i = 0; i < image->num_sections; i++)
239 image->sections[i].private = section[i].private;
240 image->sections[i].base_address = section[i].base_address;
241 image->sections[i].size = section[i].size;
242 image->sections[i].flags = section[i].flags;
247 else if (record_type == 2) /* Linear Address Record */
251 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
252 cal_checksum += (u8)(upper_address >> 8);
253 cal_checksum += (u8)upper_address;
256 if ((full_address >> 4) != upper_address)
258 /* we encountered a nonconsecutive location, create a new section,
259 * unless the current section has zero size, in which case this specifies
260 * the current section's base address
262 if (section[image->num_sections].size != 0)
264 image->num_sections++;
265 section[image->num_sections].size = 0x0;
266 section[image->num_sections].flags = 0;
267 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
269 section[image->num_sections].base_address =
270 (full_address & 0xffff) | (upper_address << 4);
271 full_address = (full_address & 0xffff) | (upper_address << 4);
274 else if (record_type == 3) /* Start Segment Address Record */
278 /* "Start Segment Address Record" will not be supported */
279 /* but we must consume it, and do not create an error. */
282 sscanf(&lpszLine[bytes_read], "%2x", &dummy);
283 cal_checksum += (u8)dummy;
287 else if (record_type == 4) /* Extended Linear Address Record */
291 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
292 cal_checksum += (u8)(upper_address >> 8);
293 cal_checksum += (u8)upper_address;
296 if ((full_address >> 16) != upper_address)
298 /* we encountered a nonconsecutive location, create a new section,
299 * unless the current section has zero size, in which case this specifies
300 * the current section's base address
302 if (section[image->num_sections].size != 0)
304 image->num_sections++;
305 section[image->num_sections].size = 0x0;
306 section[image->num_sections].flags = 0;
307 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
309 section[image->num_sections].base_address =
310 (full_address & 0xffff) | (upper_address << 16);
311 full_address = (full_address & 0xffff) | (upper_address << 16);
314 else if (record_type == 5) /* Start Linear Address Record */
318 sscanf(&lpszLine[bytes_read], "%8x", &start_address);
319 cal_checksum += (u8)(start_address >> 24);
320 cal_checksum += (u8)(start_address >> 16);
321 cal_checksum += (u8)(start_address >> 8);
322 cal_checksum += (u8)start_address;
325 image->start_address_set = 1;
326 image->start_address = be_to_h_u32((u8*)&start_address);
330 LOG_ERROR("unhandled IHEX record type: %i", record_type);
331 return ERROR_IMAGE_FORMAT_ERROR;
334 sscanf(&lpszLine[bytes_read], "%2x", &checksum);
337 if ((u8)checksum != (u8)(~cal_checksum + 1))
339 /* checksum failed */
340 LOG_ERROR("incorrect record checksum found in IHEX file");
341 return ERROR_IMAGE_CHECKSUM;
345 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
346 return ERROR_IMAGE_FORMAT_ERROR;
349 static int image_elf_read_headers(image_t *image)
351 image_elf_t *elf = image->type_private;
356 elf->header = malloc(sizeof(Elf32_Ehdr));
358 if(elf->header == NULL)
360 LOG_ERROR("insufficient memory to perform operation ");
361 return ERROR_FILEIO_OPERATION_FAILED;
364 if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
366 LOG_ERROR("cannot read ELF file header, read failed");
367 return ERROR_FILEIO_OPERATION_FAILED;
369 if (read_bytes != sizeof(Elf32_Ehdr))
371 LOG_ERROR("cannot read ELF file header, only partially read");
372 return ERROR_FILEIO_OPERATION_FAILED;
375 if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
377 LOG_ERROR("invalid ELF file, bad magic number");
378 return ERROR_IMAGE_FORMAT_ERROR;
380 if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
382 LOG_ERROR("invalid ELF file, only 32bits files are supported");
383 return ERROR_IMAGE_FORMAT_ERROR;
386 elf->endianness = elf->header->e_ident[EI_DATA];
387 if ((elf->endianness!=ELFDATA2LSB)
388 &&(elf->endianness!=ELFDATA2MSB))
390 LOG_ERROR("invalid ELF file, unknown endianess setting");
391 return ERROR_IMAGE_FORMAT_ERROR;
394 elf->segment_count = field16(elf,elf->header->e_phnum);
395 if (elf->segment_count==0)
397 LOG_ERROR("invalid ELF file, no program headers");
398 return ERROR_IMAGE_FORMAT_ERROR;
401 if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
403 LOG_ERROR("cannot seek to ELF program header table, read failed");
407 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
408 if(elf->segments == NULL)
410 LOG_ERROR("insufficient memory to perform operation ");
411 return ERROR_FILEIO_OPERATION_FAILED;
414 if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
416 LOG_ERROR("cannot read ELF segment headers, read failed");
419 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
421 LOG_ERROR("cannot read ELF segment headers, only partially read");
422 return ERROR_FILEIO_OPERATION_FAILED;
425 /* count useful segments (loadable), ignore BSS section */
426 image->num_sections = 0;
427 for (i=0;i<elf->segment_count;i++)
428 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
429 image->num_sections++;
430 /* alloc and fill sections array with loadable segments */
431 image->sections = malloc(image->num_sections * sizeof(image_section_t));
432 for (i=0,j=0;i<elf->segment_count;i++)
434 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
436 image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
437 image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
438 image->sections[j].private = &elf->segments[i];
439 image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
444 image->start_address_set = 1;
445 image->start_address = field32(elf,elf->header->e_entry);
450 static int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
452 image_elf_t *elf = image->type_private;
453 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
454 u32 read_size,really_read;
459 LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
461 /* read initialized data in current segment if any */
462 if (offset<field32(elf,segment->p_filesz))
464 /* maximal size present in file for the current segment */
465 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
466 LOG_DEBUG("read elf: size = 0x%x at 0x%x",read_size,
467 field32(elf,segment->p_offset)+offset);
468 /* read initialized area of the segment */
469 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
471 LOG_ERROR("cannot find ELF segment content, seek failed");
474 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
476 LOG_ERROR("cannot read ELF segment content, read failed");
482 *size_read += read_size;
483 /* need more data ? */
491 static int image_mot_buffer_complete(image_t *image)
493 image_mot_t *mot = image->type_private;
494 fileio_t *fileio = &mot->fileio;
495 u32 full_address = 0x0;
500 /* we can't determine the number of sections that we'll have to create ahead of time,
501 * so we locally hold them until parsing is finished */
502 image_section_t section[IMAGE_MAX_SECTIONS];
504 mot->buffer = malloc(fileio->size >> 1);
506 image->num_sections = 0;
507 section[image->num_sections].private = &mot->buffer[cooked_bytes];
508 section[image->num_sections].base_address = 0x0;
509 section[image->num_sections].size = 0x0;
510 section[image->num_sections].flags = 0;
512 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
521 /* get record type and record length */
522 if (sscanf(&lpszLine[bytes_read], "S%1x%2x", &record_type, &count) != 2)
524 return ERROR_IMAGE_FORMAT_ERROR;
528 cal_checksum += (u8)count;
530 /* skip checksum byte */
533 if (record_type == 0)
535 /* S0 - starting record (optional) */
538 while (count-- > 0) {
539 sscanf(&lpszLine[bytes_read], "%2x", &iValue);
540 cal_checksum += (u8)iValue;
544 else if (record_type >= 1 && record_type <= 3)
546 switch( record_type )
549 /* S1 - 16 bit address data record */
550 sscanf(&lpszLine[bytes_read], "%4x", &address);
551 cal_checksum += (u8)(address >> 8);
552 cal_checksum += (u8)address;
558 /* S2 - 24 bit address data record */
559 sscanf(&lpszLine[bytes_read], "%6x", &address);
560 cal_checksum += (u8)(address >> 16);
561 cal_checksum += (u8)(address >> 8);
562 cal_checksum += (u8)address;
568 /* S3 - 32 bit address data record */
569 sscanf(&lpszLine[bytes_read], "%8x", &address);
570 cal_checksum += (u8)(address >> 24);
571 cal_checksum += (u8)(address >> 16);
572 cal_checksum += (u8)(address >> 8);
573 cal_checksum += (u8)address;
580 if (full_address != address)
582 /* we encountered a nonconsecutive location, create a new section,
583 * unless the current section has zero size, in which case this specifies
584 * the current section's base address
586 if (section[image->num_sections].size != 0)
588 image->num_sections++;
589 section[image->num_sections].size = 0x0;
590 section[image->num_sections].flags = 0;
591 section[image->num_sections].private = &mot->buffer[cooked_bytes];
593 section[image->num_sections].base_address = address;
594 full_address = address;
600 sscanf(&lpszLine[bytes_read], "%2x", &value);
601 mot->buffer[cooked_bytes] = (u8)value;
602 cal_checksum += (u8)mot->buffer[cooked_bytes];
605 section[image->num_sections].size += 1;
609 else if (record_type == 5)
611 /* S5 is the data count record, we ignore it */
616 sscanf(&lpszLine[bytes_read], "%2x", &dummy);
617 cal_checksum += (u8)dummy;
621 else if (record_type >= 7 && record_type <= 9)
623 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
624 image->num_sections++;
626 /* copy section information */
627 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
628 for (i = 0; i < image->num_sections; i++)
630 image->sections[i].private = section[i].private;
631 image->sections[i].base_address = section[i].base_address;
632 image->sections[i].size = section[i].size;
633 image->sections[i].flags = section[i].flags;
640 LOG_ERROR("unhandled S19 record type: %i", record_type);
641 return ERROR_IMAGE_FORMAT_ERROR;
644 /* account for checksum, will always be 0xFF */
645 sscanf(&lpszLine[bytes_read], "%2x", &checksum);
646 cal_checksum += (u8)checksum;
649 if( cal_checksum != 0xFF )
651 /* checksum failed */
652 LOG_ERROR("incorrect record checksum found in S19 file");
653 return ERROR_IMAGE_CHECKSUM;
657 LOG_ERROR("premature end of S19 file, no end-of-file record found");
658 return ERROR_IMAGE_FORMAT_ERROR;
661 int image_open(image_t *image, char *url, char *type_string)
663 int retval = ERROR_OK;
665 if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
670 if (image->type == IMAGE_BINARY)
672 image_binary_t *image_binary;
674 image_binary = image->type_private = malloc(sizeof(image_binary_t));
676 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
681 image->num_sections = 1;
682 image->sections = malloc(sizeof(image_section_t));
683 image->sections[0].base_address = 0x0;
684 image->sections[0].size = image_binary->fileio.size;
685 image->sections[0].flags = 0;
687 else if (image->type == IMAGE_IHEX)
689 image_ihex_t *image_ihex;
691 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
693 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
698 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
700 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
701 fileio_close(&image_ihex->fileio);
705 else if (image->type == IMAGE_ELF)
707 image_elf_t *image_elf;
709 image_elf = image->type_private = malloc(sizeof(image_elf_t));
711 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
716 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
718 fileio_close(&image_elf->fileio);
722 else if (image->type == IMAGE_MEMORY)
724 target_t *target = get_target_by_num(strtoul(url, NULL, 0));
727 LOG_ERROR("Target '%s' does not exist", url);
731 image_memory_t *image_memory;
733 image->num_sections = 1;
734 image->sections = malloc(sizeof(image_section_t));
735 image->sections[0].base_address = 0x0;
736 image->sections[0].size = 0xffffffff;
737 image->sections[0].flags = 0;
739 image_memory = image->type_private = malloc(sizeof(image_memory_t));
741 image_memory->target = target;
742 image_memory->cache = NULL;
743 image_memory->cache_address = 0x0;
745 else if (image->type == IMAGE_SRECORD)
747 image_mot_t *image_mot;
749 image_mot = image->type_private = malloc(sizeof(image_mot_t));
751 if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
756 if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
758 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
759 fileio_close(&image_mot->fileio);
763 else if (image->type == IMAGE_BUILDER)
765 image->num_sections = 0;
766 image->sections = NULL;
767 image->type_private = NULL;
770 if (image->base_address_set)
774 for (section=0; section < image->num_sections; section++)
776 image->sections[section].base_address+=image->base_address;
778 /* we're done relocating. The two statements below are mainly
779 * for documenation purposes: stop anyone from empirically
780 * thinking they should use these values henceforth. */
781 image->base_address=0;
782 image->base_address_set=0;
788 int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
792 /* don't read past the end of a section */
793 if (offset + size > image->sections[section].size)
795 LOG_DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
796 offset, size, image->sections[section].size);
797 return ERROR_INVALID_ARGUMENTS;
800 if (image->type == IMAGE_BINARY)
802 image_binary_t *image_binary = image->type_private;
804 /* only one section in a plain binary */
806 return ERROR_INVALID_ARGUMENTS;
809 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
814 /* return requested bytes */
815 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
820 else if (image->type == IMAGE_IHEX)
822 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
827 else if (image->type == IMAGE_ELF)
829 return image_elf_read_section(image, section, offset, size, buffer, size_read);
831 else if (image->type == IMAGE_MEMORY)
833 image_memory_t *image_memory = image->type_private;
834 u32 address = image->sections[section].base_address + offset;
838 while ((size - *size_read) > 0)
842 if (!image_memory->cache
843 || (address < image_memory->cache_address)
844 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
846 if (!image_memory->cache)
847 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
849 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
850 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
852 free(image_memory->cache);
853 image_memory->cache = NULL;
854 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
856 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
859 size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
861 memcpy(buffer + *size_read,
862 image_memory->cache + (address - image_memory->cache_address),
863 (size_in_cache > size) ? size : size_in_cache
866 *size_read += (size_in_cache > size) ? size : size_in_cache;
867 address += (size_in_cache > size) ? size : size_in_cache;
870 else if (image->type == IMAGE_SRECORD)
872 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
877 else if (image->type == IMAGE_BUILDER)
879 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
888 int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
890 image_section_t *section;
892 /* only image builder supports adding sections */
893 if (image->type != IMAGE_BUILDER)
894 return ERROR_INVALID_ARGUMENTS;
896 /* see if there's a previous section */
897 if (image->num_sections)
899 section = &image->sections[image->num_sections - 1];
901 /* see if it's enough to extend the last section,
902 * adding data to previous sections or merging is not supported */
903 if (((section->base_address + section->size) == base) && (section->flags == flags))
905 section->private = realloc(section->private, section->size + size);
906 memcpy((u8*)section->private + section->size, data, size);
907 section->size += size;
912 /* allocate new section */
913 image->num_sections++;
914 image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
915 section = &image->sections[image->num_sections - 1];
916 section->base_address = base;
917 section->size = size;
918 section->flags = flags;
919 section->private = malloc(sizeof(u8) * size);
920 memcpy((u8*)section->private, data, size);
925 void image_close(image_t *image)
927 if (image->type == IMAGE_BINARY)
929 image_binary_t *image_binary = image->type_private;
931 fileio_close(&image_binary->fileio);
933 else if (image->type == IMAGE_IHEX)
935 image_ihex_t *image_ihex = image->type_private;
937 fileio_close(&image_ihex->fileio);
939 if (image_ihex->buffer)
941 free(image_ihex->buffer);
942 image_ihex->buffer = NULL;
945 else if (image->type == IMAGE_ELF)
947 image_elf_t *image_elf = image->type_private;
949 fileio_close(&image_elf->fileio);
951 if (image_elf->header)
953 free(image_elf->header);
954 image_elf->header = NULL;
957 if (image_elf->segments)
959 free(image_elf->segments);
960 image_elf->segments = NULL;
963 else if (image->type == IMAGE_MEMORY)
965 image_memory_t *image_memory = image->type_private;
967 if (image_memory->cache)
969 free(image_memory->cache);
970 image_memory->cache = NULL;
973 else if (image->type == IMAGE_SRECORD)
975 image_mot_t *image_mot = image->type_private;
977 fileio_close(&image_mot->fileio);
979 if (image_mot->buffer)
981 free(image_mot->buffer);
982 image_mot->buffer = NULL;
985 else if (image->type == IMAGE_BUILDER)
989 for (i = 0; i < image->num_sections; i++)
991 free(image->sections[i].private);
992 image->sections[i].private = NULL;
996 if (image->type_private)
998 free(image->type_private);
999 image->type_private = NULL;
1002 if (image->sections)
1004 free(image->sections);
1005 image->sections = NULL;
1009 int image_calculate_checksum(u8* buffer, u32 nbytes, u32* checksum)
1011 u32 crc = 0xffffffff;
1012 LOG_DEBUG("Calculating checksum");
1014 u32 crc32_table[256];
1016 /* Initialize the CRC table and the decoding table. */
1019 for (i = 0; i < 256; i++)
1022 for (c = i << 24, j = 8; j > 0; --j)
1023 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1038 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
1043 LOG_DEBUG("Calculating checksum done");