1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
33 #include "replacements.h"
39 /* convert ELF header field to host endianness */
40 #define field16(elf,field)\
41 ((elf->endianness==ELFDATA2LSB)? \
42 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
44 #define field32(elf,field)\
45 ((elf->endianness==ELFDATA2LSB)? \
46 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
48 static int autodetect_image_type(image_t *image, char *url)
55 /* read the first 4 bytes of image */
56 if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
60 retval = fileio_read(&fileio, 9, buffer, &read_bytes);
66 retval=ERROR_FILEIO_OPERATION_FAILED;
69 fileio_close(&fileio);
74 /* check header against known signatures */
75 if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
77 LOG_DEBUG("ELF image detected.");
78 image->type = IMAGE_ELF;
80 else if ((buffer[0]==':') /* record start byte */
81 &&(isxdigit(buffer[1]))
82 &&(isxdigit(buffer[2]))
83 &&(isxdigit(buffer[3]))
84 &&(isxdigit(buffer[4]))
85 &&(isxdigit(buffer[5]))
86 &&(isxdigit(buffer[6]))
87 &&(buffer[7]=='0') /* record type : 00 -> 05 */
88 &&(buffer[8]>='0')&&(buffer[8]<'6'))
90 LOG_DEBUG("IHEX image detected.");
91 image->type = IMAGE_IHEX;
93 else if ((buffer[0] == 'S') /* record start byte */
94 &&(isxdigit(buffer[1]))
95 &&(isxdigit(buffer[2]))
96 &&(isxdigit(buffer[3]))
97 &&(buffer[1] >= '0') && (buffer[1] < '9'))
99 LOG_DEBUG("S19 image detected.");
100 image->type = IMAGE_SRECORD;
104 image->type = IMAGE_BINARY;
110 int identify_image_type(image_t *image, char *type_string, char *url)
114 if (!strcmp(type_string, "bin"))
116 image->type = IMAGE_BINARY;
118 else if (!strcmp(type_string, "ihex"))
120 image->type = IMAGE_IHEX;
122 else if (!strcmp(type_string, "elf"))
124 image->type = IMAGE_ELF;
126 else if (!strcmp(type_string, "mem"))
128 image->type = IMAGE_MEMORY;
130 else if (!strcmp(type_string, "s19"))
132 image->type = IMAGE_SRECORD;
134 else if (!strcmp(type_string, "build"))
136 image->type = IMAGE_BUILDER;
140 return ERROR_IMAGE_TYPE_UNKNOWN;
145 return autodetect_image_type(image, url);
151 int image_ihex_buffer_complete(image_t *image)
153 image_ihex_t *ihex = image->type_private;
154 fileio_t *fileio = &ihex->fileio;
155 u32 full_address = 0x0;
160 /* we can't determine the number of sections that we'll have to create ahead of time,
161 * so we locally hold them until parsing is finished */
162 image_section_t section[IMAGE_MAX_SECTIONS];
164 ihex->buffer = malloc(fileio->size >> 1);
166 image->num_sections = 0;
167 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
168 section[image->num_sections].base_address = 0x0;
169 section[image->num_sections].size = 0x0;
170 section[image->num_sections].flags = 0;
172 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
181 if (sscanf(&lpszLine[bytes_read], ":%2x%4x%2x", &count, &address, &record_type) != 3)
183 return ERROR_IMAGE_FORMAT_ERROR;
187 cal_checksum += (u8)count;
188 cal_checksum += (u8)(address >> 8);
189 cal_checksum += (u8)address;
190 cal_checksum += (u8)record_type;
192 if (record_type == 0) /* Data Record */
194 if ((full_address & 0xffff) != address)
196 /* we encountered a nonconsecutive location, create a new section,
197 * unless the current section has zero size, in which case this specifies
198 * the current section's base address
200 if (section[image->num_sections].size != 0)
202 image->num_sections++;
203 section[image->num_sections].size = 0x0;
204 section[image->num_sections].flags = 0;
205 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
207 section[image->num_sections].base_address =
208 (full_address & 0xffff0000) | address;
209 full_address = (full_address & 0xffff0000) | address;
214 sscanf(&lpszLine[bytes_read], "%2x", (u32*)&ihex->buffer[cooked_bytes]);
215 cal_checksum += (u8)ihex->buffer[cooked_bytes];
218 section[image->num_sections].size += 1;
222 else if (record_type == 1) /* End of File Record */
224 /* finish the current section */
225 image->num_sections++;
227 /* copy section information */
228 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
229 for (i = 0; i < image->num_sections; i++)
231 image->sections[i].private = section[i].private;
232 image->sections[i].base_address = section[i].base_address;
233 image->sections[i].size = section[i].size;
234 image->sections[i].flags = section[i].flags;
239 else if (record_type == 2) /* Linear Address Record */
243 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
244 cal_checksum += (u8)(upper_address >> 8);
245 cal_checksum += (u8)upper_address;
248 if ((full_address >> 4) != upper_address)
250 /* we encountered a nonconsecutive location, create a new section,
251 * unless the current section has zero size, in which case this specifies
252 * the current section's base address
254 if (section[image->num_sections].size != 0)
256 image->num_sections++;
257 section[image->num_sections].size = 0x0;
258 section[image->num_sections].flags = 0;
259 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
261 section[image->num_sections].base_address =
262 (full_address & 0xffff) | (upper_address << 4);
263 full_address = (full_address & 0xffff) | (upper_address << 4);
266 else if (record_type == 3) /* Start Segment Address Record */
270 /* "Start Segment Address Record" will not be supported */
271 /* but we must consume it, and do not create an error. */
274 sscanf(&lpszLine[bytes_read], "%2x", &dummy);
275 cal_checksum += (u8)dummy;
279 else if (record_type == 4) /* Extended Linear Address Record */
283 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
284 cal_checksum += (u8)(upper_address >> 8);
285 cal_checksum += (u8)upper_address;
288 if ((full_address >> 16) != upper_address)
290 /* we encountered a nonconsecutive location, create a new section,
291 * unless the current section has zero size, in which case this specifies
292 * the current section's base address
294 if (section[image->num_sections].size != 0)
296 image->num_sections++;
297 section[image->num_sections].size = 0x0;
298 section[image->num_sections].flags = 0;
299 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
301 section[image->num_sections].base_address =
302 (full_address & 0xffff) | (upper_address << 16);
303 full_address = (full_address & 0xffff) | (upper_address << 16);
306 else if (record_type == 5) /* Start Linear Address Record */
310 sscanf(&lpszLine[bytes_read], "%8x", &start_address);
311 cal_checksum += (u8)(start_address >> 24);
312 cal_checksum += (u8)(start_address >> 16);
313 cal_checksum += (u8)(start_address >> 8);
314 cal_checksum += (u8)start_address;
317 image->start_address_set = 1;
318 image->start_address = be_to_h_u32((u8*)&start_address);
322 LOG_ERROR("unhandled IHEX record type: %i", record_type);
323 return ERROR_IMAGE_FORMAT_ERROR;
326 sscanf(&lpszLine[bytes_read], "%2x", &checksum);
329 if ((u8)checksum != (u8)(~cal_checksum + 1))
331 /* checksum failed */
332 LOG_ERROR("incorrect record checksum found in IHEX file");
333 return ERROR_IMAGE_CHECKSUM;
337 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
338 return ERROR_IMAGE_FORMAT_ERROR;
341 int image_elf_read_headers(image_t *image)
343 image_elf_t *elf = image->type_private;
348 elf->header = malloc(sizeof(Elf32_Ehdr));
350 if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
352 LOG_ERROR("cannot read ELF file header, read failed");
353 return ERROR_FILEIO_OPERATION_FAILED;
355 if (read_bytes != sizeof(Elf32_Ehdr))
357 LOG_ERROR("cannot read ELF file header, only partially read");
358 return ERROR_FILEIO_OPERATION_FAILED;
361 if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
363 LOG_ERROR("invalid ELF file, bad magic number");
364 return ERROR_IMAGE_FORMAT_ERROR;
366 if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
368 LOG_ERROR("invalid ELF file, only 32bits files are supported");
369 return ERROR_IMAGE_FORMAT_ERROR;
373 elf->endianness = elf->header->e_ident[EI_DATA];
374 if ((elf->endianness!=ELFDATA2LSB)
375 &&(elf->endianness!=ELFDATA2MSB))
377 LOG_ERROR("invalid ELF file, unknown endianess setting");
378 return ERROR_IMAGE_FORMAT_ERROR;
381 elf->segment_count = field16(elf,elf->header->e_phnum);
382 if (elf->segment_count==0)
384 LOG_ERROR("invalid ELF file, no program headers");
385 return ERROR_IMAGE_FORMAT_ERROR;
388 if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
390 LOG_ERROR("cannot seek to ELF program header table, read failed");
394 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
396 if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
398 LOG_ERROR("cannot read ELF segment headers, read failed");
401 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
403 LOG_ERROR("cannot read ELF segment headers, only partially read");
404 return ERROR_FILEIO_OPERATION_FAILED;
407 /* count useful segments (loadable), ignore BSS section */
408 image->num_sections = 0;
409 for (i=0;i<elf->segment_count;i++)
410 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
411 image->num_sections++;
412 /* alloc and fill sections array with loadable segments */
413 image->sections = malloc(image->num_sections * sizeof(image_section_t));
414 for (i=0,j=0;i<elf->segment_count;i++)
416 if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
418 image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
419 image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
420 image->sections[j].private = &elf->segments[i];
421 image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
426 image->start_address_set = 1;
427 image->start_address = field32(elf,elf->header->e_entry);
432 int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
434 image_elf_t *elf = image->type_private;
435 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
436 u32 read_size,really_read;
441 LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
443 /* read initialized data in current segment if any */
444 if (offset<field32(elf,segment->p_filesz))
446 /* maximal size present in file for the current segment */
447 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
448 LOG_DEBUG("read elf: size = 0x%x at 0x%x",read_size,
449 field32(elf,segment->p_offset)+offset);
450 /* read initialized area of the segment */
451 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
453 LOG_ERROR("cannot find ELF segment content, seek failed");
456 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
458 LOG_ERROR("cannot read ELF segment content, read failed");
464 *size_read += read_size;
465 /* need more data ? */
473 int image_mot_buffer_complete(image_t *image)
475 image_mot_t *mot = image->type_private;
476 fileio_t *fileio = &mot->fileio;
477 u32 full_address = 0x0;
482 /* we can't determine the number of sections that we'll have to create ahead of time,
483 * so we locally hold them until parsing is finished */
484 image_section_t section[IMAGE_MAX_SECTIONS];
486 mot->buffer = malloc(fileio->size >> 1);
488 image->num_sections = 0;
489 section[image->num_sections].private = &mot->buffer[cooked_bytes];
490 section[image->num_sections].base_address = 0x0;
491 section[image->num_sections].size = 0x0;
492 section[image->num_sections].flags = 0;
494 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
503 /* get record type and record length */
504 if (sscanf(&lpszLine[bytes_read], "S%1x%2x", &record_type, &count) != 2)
506 return ERROR_IMAGE_FORMAT_ERROR;
510 cal_checksum += (u8)count;
512 /* skip checksum byte */
515 if (record_type == 0)
517 /* S0 - starting record (optional) */
520 while (count-- > 0) {
521 sscanf(&lpszLine[bytes_read], "%2x", &iValue);
522 cal_checksum += (u8)iValue;
526 else if (record_type >= 1 && record_type <= 3)
528 switch( record_type )
531 /* S1 - 16 bit address data record */
532 sscanf(&lpszLine[bytes_read], "%4x", &address);
533 cal_checksum += (u8)(address >> 8);
534 cal_checksum += (u8)address;
540 /* S2 - 24 bit address data record */
541 sscanf(&lpszLine[bytes_read], "%6x", &address);
542 cal_checksum += (u8)(address >> 16);
543 cal_checksum += (u8)(address >> 8);
544 cal_checksum += (u8)address;
550 /* S3 - 32 bit address data record */
551 sscanf(&lpszLine[bytes_read], "%8x", &address);
552 cal_checksum += (u8)(address >> 24);
553 cal_checksum += (u8)(address >> 16);
554 cal_checksum += (u8)(address >> 8);
555 cal_checksum += (u8)address;
562 if (full_address != address)
564 /* we encountered a nonconsecutive location, create a new section,
565 * unless the current section has zero size, in which case this specifies
566 * the current section's base address
568 if (section[image->num_sections].size != 0)
570 image->num_sections++;
571 section[image->num_sections].size = 0x0;
572 section[image->num_sections].flags = 0;
573 section[image->num_sections].private = &mot->buffer[cooked_bytes];
575 section[image->num_sections].base_address = address;
576 full_address = address;
581 sscanf(&lpszLine[bytes_read], "%2x", (u32*)&mot->buffer[cooked_bytes]);
582 cal_checksum += (u8)mot->buffer[cooked_bytes];
585 section[image->num_sections].size += 1;
589 else if (record_type == 5)
591 /* S5 is the data count record, we ignore it */
596 sscanf(&lpszLine[bytes_read], "%2x", &dummy);
597 cal_checksum += (u8)dummy;
601 else if (record_type >= 7 && record_type <= 9)
603 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
604 image->num_sections++;
606 /* copy section information */
607 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
608 for (i = 0; i < image->num_sections; i++)
610 image->sections[i].private = section[i].private;
611 image->sections[i].base_address = section[i].base_address;
612 image->sections[i].size = section[i].size;
613 image->sections[i].flags = section[i].flags;
620 LOG_ERROR("unhandled S19 record type: %i", record_type);
621 return ERROR_IMAGE_FORMAT_ERROR;
624 /* account for checksum, will always be 0xFF */
625 sscanf(&lpszLine[bytes_read], "%2x", &checksum);
626 cal_checksum += (u8)checksum;
629 if( cal_checksum != 0xFF )
631 /* checksum failed */
632 LOG_ERROR("incorrect record checksum found in S19 file");
633 return ERROR_IMAGE_CHECKSUM;
637 LOG_ERROR("premature end of S19 file, no end-of-file record found");
638 return ERROR_IMAGE_FORMAT_ERROR;
641 int image_open(image_t *image, char *url, char *type_string)
643 int retval = ERROR_OK;
645 if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
650 if (image->type == IMAGE_BINARY)
652 image_binary_t *image_binary;
654 image_binary = image->type_private = malloc(sizeof(image_binary_t));
656 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
661 image->num_sections = 1;
662 image->sections = malloc(sizeof(image_section_t));
663 image->sections[0].base_address = 0x0;
664 image->sections[0].size = image_binary->fileio.size;
665 image->sections[0].flags = 0;
667 else if (image->type == IMAGE_IHEX)
669 image_ihex_t *image_ihex;
671 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
673 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
678 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
680 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
681 fileio_close(&image_ihex->fileio);
685 else if (image->type == IMAGE_ELF)
687 image_elf_t *image_elf;
689 image_elf = image->type_private = malloc(sizeof(image_elf_t));
691 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
696 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
698 fileio_close(&image_elf->fileio);
702 else if (image->type == IMAGE_MEMORY)
704 image_memory_t *image_memory;
706 image->num_sections = 1;
707 image->sections = malloc(sizeof(image_section_t));
708 image->sections[0].base_address = 0x0;
709 image->sections[0].size = 0xffffffff;
710 image->sections[0].flags = 0;
712 image_memory = image->type_private = malloc(sizeof(image_memory_t));
714 image_memory->target = get_target_by_num(strtoul(url, NULL, 0));;
715 image_memory->cache = NULL;
716 image_memory->cache_address = 0x0;
718 else if (image->type == IMAGE_SRECORD)
720 image_mot_t *image_mot;
722 image_mot = image->type_private = malloc(sizeof(image_mot_t));
724 if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
729 if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
731 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
732 fileio_close(&image_mot->fileio);
736 else if (image->type == IMAGE_BUILDER)
738 image->num_sections = 0;
739 image->sections = NULL;
740 image->type_private = NULL;
743 if (image->base_address_set)
747 for (section=0; section < image->num_sections; section++)
749 image->sections[section].base_address+=image->base_address;
751 /* we're done relocating. The two statements below are mainly
752 * for documenation purposes: stop anyone from empirically
753 * thinking they should use these values henceforth. */
754 image->base_address=0;
755 image->base_address_set=0;
761 int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
765 /* don't read past the end of a section */
766 if (offset + size > image->sections[section].size)
768 LOG_DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
769 offset, size, image->sections[section].size);
770 return ERROR_INVALID_ARGUMENTS;
773 if (image->type == IMAGE_BINARY)
775 image_binary_t *image_binary = image->type_private;
777 /* only one section in a plain binary */
779 return ERROR_INVALID_ARGUMENTS;
782 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
787 /* return requested bytes */
788 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
793 else if (image->type == IMAGE_IHEX)
795 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
800 else if (image->type == IMAGE_ELF)
802 return image_elf_read_section(image, section, offset, size, buffer, size_read);
804 else if (image->type == IMAGE_MEMORY)
806 image_memory_t *image_memory = image->type_private;
807 u32 address = image->sections[section].base_address + offset;
811 while ((size - *size_read) > 0)
815 if (!image_memory->cache
816 || (address < image_memory->cache_address)
817 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
819 if (!image_memory->cache)
820 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
822 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
823 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
825 free(image_memory->cache);
826 image_memory->cache = NULL;
827 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
829 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
832 size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
834 memcpy(buffer + *size_read,
835 image_memory->cache + (address - image_memory->cache_address),
836 (size_in_cache > size) ? size : size_in_cache
839 *size_read += (size_in_cache > size) ? size : size_in_cache;
840 address += (size_in_cache > size) ? size : size_in_cache;
843 else if (image->type == IMAGE_SRECORD)
845 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
850 else if (image->type == IMAGE_BUILDER)
852 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
861 int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
863 image_section_t *section;
865 /* only image builder supports adding sections */
866 if (image->type != IMAGE_BUILDER)
867 return ERROR_INVALID_ARGUMENTS;
869 /* see if there's a previous section */
870 if (image->num_sections)
872 section = &image->sections[image->num_sections - 1];
874 /* see if it's enough to extend the last section,
875 * adding data to previous sections or merging is not supported */
876 if (((section->base_address + section->size) == base) && (section->flags == flags))
878 section->private = realloc(section->private, section->size + size);
879 memcpy((u8*)section->private + section->size, data, size);
880 section->size += size;
885 /* allocate new section */
886 image->num_sections++;
887 image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
888 section = &image->sections[image->num_sections - 1];
889 section->base_address = base;
890 section->size = size;
891 section->flags = flags;
892 section->private = malloc(sizeof(u8) * size);
893 memcpy((u8*)section->private, data, size);
898 int image_close(image_t *image)
900 if (image->type == IMAGE_BINARY)
902 image_binary_t *image_binary = image->type_private;
904 fileio_close(&image_binary->fileio);
906 else if (image->type == IMAGE_IHEX)
908 image_ihex_t *image_ihex = image->type_private;
910 fileio_close(&image_ihex->fileio);
912 if (image_ihex->buffer)
914 free(image_ihex->buffer);
915 image_ihex->buffer = NULL;
918 else if (image->type == IMAGE_ELF)
920 image_elf_t *image_elf = image->type_private;
922 fileio_close(&image_elf->fileio);
924 if (image_elf->header)
926 free(image_elf->header);
927 image_elf->header = NULL;
930 if (image_elf->segments)
932 free(image_elf->segments);
933 image_elf->segments = NULL;
936 else if (image->type == IMAGE_MEMORY)
938 image_memory_t *image_memory = image->type_private;
940 if (image_memory->cache)
942 free(image_memory->cache);
943 image_memory->cache = NULL;
946 else if (image->type == IMAGE_SRECORD)
948 image_mot_t *image_mot = image->type_private;
950 fileio_close(&image_mot->fileio);
952 if (image_mot->buffer)
954 free(image_mot->buffer);
955 image_mot->buffer = NULL;
958 else if (image->type == IMAGE_BUILDER)
962 for (i = 0; i < image->num_sections; i++)
964 free(image->sections[i].private);
965 image->sections[i].private = NULL;
969 if (image->type_private)
971 free(image->type_private);
972 image->type_private = NULL;
977 free(image->sections);
978 image->sections = NULL;
984 static u32 crc32_table[256] = {0, 0};
986 int image_calculate_checksum(u8* buffer, u32 nbytes, u32* checksum)
988 u32 crc = 0xffffffff;
992 /* Initialize the CRC table and the decoding table. */
995 for (i = 0; i < 256; i++)
998 for (c = i << 24, j = 8; j > 0; --j)
999 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1007 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];