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 * Copyright (C) 2009 by Franck Hereson *
12 * franck.hereson@secad.fr *
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. *
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. *
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 ***************************************************************************/
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
)
54 /* read the first 4 bytes of image */
55 if ((retval
= fileio_open(&fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
59 retval
= fileio_read(&fileio
, 9, buffer
, &read_bytes
);
61 if (retval
== ERROR_OK
)
65 retval
= ERROR_FILEIO_OPERATION_FAILED
;
68 fileio_close(&fileio
);
70 if (retval
!= ERROR_OK
)
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
;
103 image
->type
= IMAGE_BINARY
;
109 static int identify_image_type(struct image
*image
, const char *type_string
, const char *url
)
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
;
139 return ERROR_IMAGE_TYPE_UNKNOWN
;
144 return autodetect_image_type(image
, url
);
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
;
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 */
163 retval
= fileio_size(fileio
, &filesize
);
164 if (retval
!= ERROR_OK
)
167 ihex
->buffer
= malloc(filesize
>> 1);
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
)
179 uint32_t record_type
;
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
;
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
;
224 sscanf(&lpszLine
[bytes_read
], "%2x", &value
);
225 ihex
->buffer
[cooked_bytes
] = (uint8_t)value
;
226 cal_checksum
+= (uint8_t)ihex
->buffer
[cooked_bytes
];
229 section
[image
->num_sections
].size
+= 1;
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
;
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
;
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 */
287 /* "Start Segment Address Record" will not be supported */
288 /* but we must consume it, and do not create an error. */
291 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &dummy
);
292 cal_checksum
+= (uint8_t)dummy
;
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
;
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
;
340 image
->start_address_set
= 1;
341 image
->start_address
= be_to_h_u32((uint8_t*)&start_address
);
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
);
351 if ((uint8_t)checksum
!= (uint8_t)(~cal_checksum
+ 1))
353 /* checksum failed */
354 LOG_ERROR("incorrect record checksum found in IHEX file");
355 return ERROR_IMAGE_CHECKSUM
;
359 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
360 return ERROR_IMAGE_FORMAT_ERROR
;
364 * Allocate memory dynamically instead of on the stack. This
365 * is important w/embedded hosts.
367 static int image_ihex_buffer_complete(struct image
*image
)
369 char *lpszLine
= malloc(1023);
370 if (lpszLine
== NULL
)
372 LOG_ERROR("Out of memory");
375 struct imagesection
*section
= malloc(sizeof(struct imagesection
) * IMAGE_MAX_SECTIONS
);
379 LOG_ERROR("Out of memory");
384 retval
= image_ihex_buffer_complete_inner(image
, lpszLine
, section
);
392 static int image_elf_read_headers(struct image
*image
)
394 struct image_elf
*elf
= image
->type_private
;
398 uint32_t nload
,load_to_vaddr
=0;
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 endianness 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");
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");
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
++;
475 assert(image
->num_sections
> 0);
478 * some ELF linkers produce binaries with *all* the program header
479 * p_paddr fields zero (there can be however one loadable segment
480 * that has valid physical address 0x0).
481 * If we have such a binary with more than
482 * one PT_LOAD header, then use p_vaddr instead of p_paddr
483 * (ARM ELF standard demands p_paddr = 0 anyway, and BFD
484 * library uses this approach to workaround zero-initialized p_paddrs
485 * when obtaining lma - look at elf.c of BDF)
487 for (nload
= 0, i
= 0; i
< elf
->segment_count
; i
++)
488 if (elf
->segments
[i
].p_paddr
!= 0)
490 else if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_memsz
) != 0))
493 if (i
>= elf
->segment_count
&& nload
> 1)
496 /* alloc and fill sections array with loadable segments */
497 image
->sections
= malloc(image
->num_sections
* sizeof(struct imagesection
));
498 for (i
= 0,j
= 0;i
< elf
->segment_count
;i
++)
500 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
502 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_filesz
);
504 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_vaddr
);
506 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_paddr
);
507 image
->sections
[j
].private = &elf
->segments
[i
];
508 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
513 image
->start_address_set
= 1;
514 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
519 static int image_elf_read_section(struct image
*image
, int section
, uint32_t offset
, uint32_t size
, uint8_t *buffer
, size_t *size_read
)
521 struct image_elf
*elf
= image
->type_private
;
522 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
523 size_t read_size
,really_read
;
528 LOG_DEBUG("load segment %d at 0x%" PRIx32
" (sz = 0x%" PRIx32
")",section
,offset
,size
);
530 /* read initialized data in current segment if any */
531 if (offset
< field32(elf
,segment
->p_filesz
))
533 /* maximal size present in file for the current segment */
534 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
535 LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32
"", read_size
,
536 field32(elf
,segment
->p_offset
) + offset
);
537 /* read initialized area of the segment */
538 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
) + offset
)) != ERROR_OK
)
540 LOG_ERROR("cannot find ELF segment content, seek failed");
543 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
545 LOG_ERROR("cannot read ELF segment content, read failed");
549 *size_read
+= read_size
;
550 /* need more data ? */
558 static int image_mot_buffer_complete_inner(struct image
*image
, char *lpszLine
, struct imagesection
*section
)
560 struct image_mot
*mot
= image
->type_private
;
561 struct fileio
*fileio
= &mot
->fileio
;
562 uint32_t full_address
= 0x0;
563 uint32_t cooked_bytes
;
566 /* we can't determine the number of sections that we'll have to create ahead of time,
567 * so we locally hold them until parsing is finished */
571 retval
= fileio_size(fileio
, &filesize
);
572 if (retval
!= ERROR_OK
)
575 mot
->buffer
= malloc(filesize
>> 1);
577 image
->num_sections
= 0;
578 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
579 section
[image
->num_sections
].base_address
= 0x0;
580 section
[image
->num_sections
].size
= 0x0;
581 section
[image
->num_sections
].flags
= 0;
583 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
587 uint32_t record_type
;
589 uint8_t cal_checksum
= 0;
590 uint32_t bytes_read
= 0;
592 /* get record type and record length */
593 if (sscanf(&lpszLine
[bytes_read
], "S%1" SCNx32
"%2" SCNx32
, &record_type
, &count
) != 2)
595 return ERROR_IMAGE_FORMAT_ERROR
;
599 cal_checksum
+= (uint8_t)count
;
601 /* skip checksum byte */
604 if (record_type
== 0)
606 /* S0 - starting record (optional) */
609 while (count
-- > 0) {
610 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
611 cal_checksum
+= (uint8_t)iValue
;
615 else if (record_type
>= 1 && record_type
<= 3)
620 /* S1 - 16 bit address data record */
621 sscanf(&lpszLine
[bytes_read
], "%4" SCNx32
, &address
);
622 cal_checksum
+= (uint8_t)(address
>> 8);
623 cal_checksum
+= (uint8_t)address
;
629 /* S2 - 24 bit address data record */
630 sscanf(&lpszLine
[bytes_read
], "%6" SCNx32
, &address
);
631 cal_checksum
+= (uint8_t)(address
>> 16);
632 cal_checksum
+= (uint8_t)(address
>> 8);
633 cal_checksum
+= (uint8_t)address
;
639 /* S3 - 32 bit address data record */
640 sscanf(&lpszLine
[bytes_read
], "%8" SCNx32
, &address
);
641 cal_checksum
+= (uint8_t)(address
>> 24);
642 cal_checksum
+= (uint8_t)(address
>> 16);
643 cal_checksum
+= (uint8_t)(address
>> 8);
644 cal_checksum
+= (uint8_t)address
;
651 if (full_address
!= address
)
653 /* we encountered a nonconsecutive location, create a new section,
654 * unless the current section has zero size, in which case this specifies
655 * the current section's base address
657 if (section
[image
->num_sections
].size
!= 0)
659 image
->num_sections
++;
660 section
[image
->num_sections
].size
= 0x0;
661 section
[image
->num_sections
].flags
= 0;
662 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
664 section
[image
->num_sections
].base_address
= address
;
665 full_address
= address
;
671 sscanf(&lpszLine
[bytes_read
], "%2x", &value
);
672 mot
->buffer
[cooked_bytes
] = (uint8_t)value
;
673 cal_checksum
+= (uint8_t)mot
->buffer
[cooked_bytes
];
676 section
[image
->num_sections
].size
+= 1;
680 else if (record_type
== 5)
682 /* S5 is the data count record, we ignore it */
687 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &dummy
);
688 cal_checksum
+= (uint8_t)dummy
;
692 else if (record_type
>= 7 && record_type
<= 9)
694 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
695 image
->num_sections
++;
697 /* copy section information */
698 image
->sections
= malloc(sizeof(struct imagesection
) * image
->num_sections
);
699 for (i
= 0; i
< image
->num_sections
; i
++)
701 image
->sections
[i
].private = section
[i
].private;
702 image
->sections
[i
].base_address
= section
[i
].base_address
;
703 image
->sections
[i
].size
= section
[i
].size
;
704 image
->sections
[i
].flags
= section
[i
].flags
;
711 LOG_ERROR("unhandled S19 record type: %i", (int)(record_type
));
712 return ERROR_IMAGE_FORMAT_ERROR
;
715 /* account for checksum, will always be 0xFF */
716 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &checksum
);
717 cal_checksum
+= (uint8_t)checksum
;
719 if (cal_checksum
!= 0xFF)
721 /* checksum failed */
722 LOG_ERROR("incorrect record checksum found in S19 file");
723 return ERROR_IMAGE_CHECKSUM
;
727 LOG_ERROR("premature end of S19 file, no end-of-file record found");
728 return ERROR_IMAGE_FORMAT_ERROR
;
732 * Allocate memory dynamically instead of on the stack. This
733 * is important w/embedded hosts.
735 static int image_mot_buffer_complete(struct image
*image
)
737 char *lpszLine
= malloc(1023);
738 if (lpszLine
== NULL
)
740 LOG_ERROR("Out of memory");
743 struct imagesection
*section
= malloc(sizeof(struct imagesection
) * IMAGE_MAX_SECTIONS
);
747 LOG_ERROR("Out of memory");
752 retval
= image_mot_buffer_complete_inner(image
, lpszLine
, section
);
761 int image_open(struct image
*image
, const char *url
, const char *type_string
)
763 int retval
= ERROR_OK
;
765 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
770 if (image
->type
== IMAGE_BINARY
)
772 struct image_binary
*image_binary
;
774 image_binary
= image
->type_private
= malloc(sizeof(struct image_binary
));
776 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
781 retval
= fileio_size(&image_binary
->fileio
, &filesize
);
782 if (retval
!= ERROR_OK
)
784 fileio_close(&image_binary
->fileio
);
788 image
->num_sections
= 1;
789 image
->sections
= malloc(sizeof(struct imagesection
));
790 image
->sections
[0].base_address
= 0x0;
791 image
->sections
[0].size
= filesize
;
792 image
->sections
[0].flags
= 0;
794 else if (image
->type
== IMAGE_IHEX
)
796 struct image_ihex
*image_ihex
;
798 image_ihex
= image
->type_private
= malloc(sizeof(struct image_ihex
));
800 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
805 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
807 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
808 fileio_close(&image_ihex
->fileio
);
812 else if (image
->type
== IMAGE_ELF
)
814 struct image_elf
*image_elf
;
816 image_elf
= image
->type_private
= malloc(sizeof(struct image_elf
));
818 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
823 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
825 fileio_close(&image_elf
->fileio
);
829 else if (image
->type
== IMAGE_MEMORY
)
831 struct target
*target
= get_target(url
);
835 LOG_ERROR("target '%s' not defined", url
);
839 struct image_memory
*image_memory
;
841 image
->num_sections
= 1;
842 image
->sections
= malloc(sizeof(struct imagesection
));
843 image
->sections
[0].base_address
= 0x0;
844 image
->sections
[0].size
= 0xffffffff;
845 image
->sections
[0].flags
= 0;
847 image_memory
= image
->type_private
= malloc(sizeof(struct image_memory
));
849 image_memory
->target
= target
;
850 image_memory
->cache
= NULL
;
851 image_memory
->cache_address
= 0x0;
853 else if (image
->type
== IMAGE_SRECORD
)
855 struct image_mot
*image_mot
;
857 image_mot
= image
->type_private
= malloc(sizeof(struct image_mot
));
859 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
864 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
866 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
867 fileio_close(&image_mot
->fileio
);
871 else if (image
->type
== IMAGE_BUILDER
)
873 image
->num_sections
= 0;
874 image
->sections
= NULL
;
875 image
->type_private
= NULL
;
878 if (image
->base_address_set
)
882 for (section
= 0; section
< image
->num_sections
; section
++)
884 image
->sections
[section
].base_address
+= image
->base_address
;
886 /* we're done relocating. The two statements below are mainly
887 * for documenation purposes: stop anyone from empirically
888 * thinking they should use these values henceforth. */
889 image
->base_address
= 0;
890 image
->base_address_set
= 0;
896 int image_read_section(struct image
*image
, int section
, uint32_t offset
, uint32_t size
, uint8_t *buffer
, size_t *size_read
)
900 /* don't read past the end of a section */
901 if (offset
+ size
> image
->sections
[section
].size
)
903 LOG_DEBUG("read past end of section: 0x%8.8" PRIx32
" + 0x%8.8" PRIx32
" > 0x%8.8" PRIx32
"",
904 offset
, size
, image
->sections
[section
].size
);
905 return ERROR_INVALID_ARGUMENTS
;
908 if (image
->type
== IMAGE_BINARY
)
910 struct image_binary
*image_binary
= image
->type_private
;
912 /* only one section in a plain binary */
914 return ERROR_INVALID_ARGUMENTS
;
917 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
922 /* return requested bytes */
923 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
928 else if (image
->type
== IMAGE_IHEX
)
930 memcpy(buffer
, (uint8_t*)image
->sections
[section
].private + offset
, size
);
935 else if (image
->type
== IMAGE_ELF
)
937 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
939 else if (image
->type
== IMAGE_MEMORY
)
941 struct image_memory
*image_memory
= image
->type_private
;
942 uint32_t address
= image
->sections
[section
].base_address
+ offset
;
946 while ((size
- *size_read
) > 0)
948 uint32_t size_in_cache
;
950 if (!image_memory
->cache
951 || (address
< image_memory
->cache_address
)
952 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
954 if (!image_memory
->cache
)
955 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
957 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
958 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
960 free(image_memory
->cache
);
961 image_memory
->cache
= NULL
;
962 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
964 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
967 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
969 memcpy(buffer
+ *size_read
,
970 image_memory
->cache
+ (address
- image_memory
->cache_address
),
971 (size_in_cache
> size
) ? size
: size_in_cache
974 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
975 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
978 else if (image
->type
== IMAGE_SRECORD
)
980 memcpy(buffer
, (uint8_t*)image
->sections
[section
].private + offset
, size
);
985 else if (image
->type
== IMAGE_BUILDER
)
987 memcpy(buffer
, (uint8_t*)image
->sections
[section
].private + offset
, size
);
996 int image_add_section(struct image
*image
, uint32_t base
, uint32_t size
, int flags
, uint8_t *data
)
998 struct imagesection
*section
;
1000 /* only image builder supports adding sections */
1001 if (image
->type
!= IMAGE_BUILDER
)
1002 return ERROR_INVALID_ARGUMENTS
;
1004 /* see if there's a previous section */
1005 if (image
->num_sections
)
1007 section
= &image
->sections
[image
->num_sections
- 1];
1009 /* see if it's enough to extend the last section,
1010 * adding data to previous sections or merging is not supported */
1011 if (((section
->base_address
+ section
->size
) == base
) && (section
->flags
== flags
))
1013 section
->private = realloc(section
->private, section
->size
+ size
);
1014 memcpy((uint8_t*)section
->private + section
->size
, data
, size
);
1015 section
->size
+= size
;
1020 /* allocate new section */
1021 image
->num_sections
++;
1022 image
->sections
= realloc(image
->sections
, sizeof(struct imagesection
) * image
->num_sections
);
1023 section
= &image
->sections
[image
->num_sections
- 1];
1024 section
->base_address
= base
;
1025 section
->size
= size
;
1026 section
->flags
= flags
;
1027 section
->private = malloc(sizeof(uint8_t) * size
);
1028 memcpy((uint8_t*)section
->private, data
, size
);
1033 void image_close(struct image
*image
)
1035 if (image
->type
== IMAGE_BINARY
)
1037 struct image_binary
*image_binary
= image
->type_private
;
1039 fileio_close(&image_binary
->fileio
);
1041 else if (image
->type
== IMAGE_IHEX
)
1043 struct image_ihex
*image_ihex
= image
->type_private
;
1045 fileio_close(&image_ihex
->fileio
);
1047 if (image_ihex
->buffer
)
1049 free(image_ihex
->buffer
);
1050 image_ihex
->buffer
= NULL
;
1053 else if (image
->type
== IMAGE_ELF
)
1055 struct image_elf
*image_elf
= image
->type_private
;
1057 fileio_close(&image_elf
->fileio
);
1059 if (image_elf
->header
)
1061 free(image_elf
->header
);
1062 image_elf
->header
= NULL
;
1065 if (image_elf
->segments
)
1067 free(image_elf
->segments
);
1068 image_elf
->segments
= NULL
;
1071 else if (image
->type
== IMAGE_MEMORY
)
1073 struct image_memory
*image_memory
= image
->type_private
;
1075 if (image_memory
->cache
)
1077 free(image_memory
->cache
);
1078 image_memory
->cache
= NULL
;
1081 else if (image
->type
== IMAGE_SRECORD
)
1083 struct image_mot
*image_mot
= image
->type_private
;
1085 fileio_close(&image_mot
->fileio
);
1087 if (image_mot
->buffer
)
1089 free(image_mot
->buffer
);
1090 image_mot
->buffer
= NULL
;
1093 else if (image
->type
== IMAGE_BUILDER
)
1097 for (i
= 0; i
< image
->num_sections
; i
++)
1099 free(image
->sections
[i
].private);
1100 image
->sections
[i
].private = NULL
;
1104 if (image
->type_private
)
1106 free(image
->type_private
);
1107 image
->type_private
= NULL
;
1110 if (image
->sections
)
1112 free(image
->sections
);
1113 image
->sections
= NULL
;
1117 int image_calculate_checksum(uint8_t* buffer
, uint32_t nbytes
, uint32_t* checksum
)
1119 uint32_t crc
= 0xffffffff;
1120 LOG_DEBUG("Calculating checksum");
1122 static uint32_t crc32_table
[256];
1124 static bool first_init
= false;
1127 /* Initialize the CRC table and the decoding table. */
1130 for (i
= 0; i
< 256; i
++)
1133 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1134 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1152 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buffer
++) & 255];
1157 LOG_DEBUG("Calculating checksum done");