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, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
34 #include <helper/log.h>
36 /* convert ELF header field to host endianness */
37 #define field16(elf, field) \
38 ((elf->endianness == ELFDATA2LSB) ? \
39 le_to_h_u16((uint8_t *)&field) : be_to_h_u16((uint8_t *)&field))
41 #define field32(elf, field) \
42 ((elf->endianness == ELFDATA2LSB) ? \
43 le_to_h_u32((uint8_t *)&field) : be_to_h_u32((uint8_t *)&field))
45 static int autodetect_image_type(struct image *image, const char *url)
48 struct fileio *fileio;
52 /* read the first 4 bytes of image */
53 retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY);
54 if (retval != ERROR_OK)
56 retval = fileio_read(fileio, 9, buffer, &read_bytes);
58 if (retval == ERROR_OK) {
60 retval = ERROR_FILEIO_OPERATION_FAILED;
64 if (retval != ERROR_OK)
67 /* check header against known signatures */
68 if (strncmp((char *)buffer, ELFMAG, SELFMAG) == 0) {
69 LOG_DEBUG("ELF image detected.");
70 image->type = IMAGE_ELF;
71 } else if ((buffer[0] == ':') /* record start byte */
72 && (isxdigit(buffer[1]))
73 && (isxdigit(buffer[2]))
74 && (isxdigit(buffer[3]))
75 && (isxdigit(buffer[4]))
76 && (isxdigit(buffer[5]))
77 && (isxdigit(buffer[6]))
78 && (buffer[7] == '0') /* record type : 00 -> 05 */
79 && (buffer[8] >= '0') && (buffer[8] < '6')) {
80 LOG_DEBUG("IHEX image detected.");
81 image->type = IMAGE_IHEX;
82 } else if ((buffer[0] == 'S') /* record start byte */
83 && (isxdigit(buffer[1]))
84 && (isxdigit(buffer[2]))
85 && (isxdigit(buffer[3]))
86 && (buffer[1] >= '0') && (buffer[1] < '9')) {
87 LOG_DEBUG("S19 image detected.");
88 image->type = IMAGE_SRECORD;
90 image->type = IMAGE_BINARY;
95 static int identify_image_type(struct image *image, const char *type_string, const char *url)
98 if (!strcmp(type_string, "bin"))
99 image->type = IMAGE_BINARY;
100 else if (!strcmp(type_string, "ihex"))
101 image->type = IMAGE_IHEX;
102 else if (!strcmp(type_string, "elf"))
103 image->type = IMAGE_ELF;
104 else if (!strcmp(type_string, "mem"))
105 image->type = IMAGE_MEMORY;
106 else if (!strcmp(type_string, "s19"))
107 image->type = IMAGE_SRECORD;
108 else if (!strcmp(type_string, "build"))
109 image->type = IMAGE_BUILDER;
111 return ERROR_IMAGE_TYPE_UNKNOWN;
113 return autodetect_image_type(image, url);
118 static int image_ihex_buffer_complete_inner(struct image *image,
120 struct imagesection *section)
122 struct image_ihex *ihex = image->type_private;
123 struct fileio *fileio = ihex->fileio;
124 uint32_t full_address;
125 uint32_t cooked_bytes;
126 bool end_rec = false;
128 /* we can't determine the number of sections that we'll have to create ahead of time,
129 * so we locally hold them until parsing is finished */
133 retval = fileio_size(fileio, &filesize);
134 if (retval != ERROR_OK)
137 ihex->buffer = malloc(filesize >> 1);
139 image->num_sections = 0;
141 while (!fileio_feof(fileio)) {
143 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
144 section[image->num_sections].base_address = 0x0;
145 section[image->num_sections].size = 0x0;
146 section[image->num_sections].flags = 0;
148 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK) {
151 uint32_t record_type;
153 uint8_t cal_checksum = 0;
154 size_t bytes_read = 0;
156 /* skip comments and blank lines */
157 if ((lpszLine[0] == '#') || (strlen(lpszLine + strspn(lpszLine, "\n\t\r ")) == 0))
160 if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32, &count,
161 &address, &record_type) != 3)
162 return ERROR_IMAGE_FORMAT_ERROR;
165 cal_checksum += (uint8_t)count;
166 cal_checksum += (uint8_t)(address >> 8);
167 cal_checksum += (uint8_t)address;
168 cal_checksum += (uint8_t)record_type;
170 if (record_type == 0) { /* Data Record */
171 if ((full_address & 0xffff) != address) {
172 /* we encountered a nonconsecutive location, create a new section,
173 * unless the current section has zero size, in which case this specifies
174 * the current section's base address
176 if (section[image->num_sections].size != 0) {
177 image->num_sections++;
178 if (image->num_sections >= IMAGE_MAX_SECTIONS) {
179 /* too many sections */
180 LOG_ERROR("Too many sections found in IHEX file");
181 return ERROR_IMAGE_FORMAT_ERROR;
183 section[image->num_sections].size = 0x0;
184 section[image->num_sections].flags = 0;
185 section[image->num_sections].private =
186 &ihex->buffer[cooked_bytes];
188 section[image->num_sections].base_address =
189 (full_address & 0xffff0000) | address;
190 full_address = (full_address & 0xffff0000) | address;
193 while (count-- > 0) {
195 sscanf(&lpszLine[bytes_read], "%2x", &value);
196 ihex->buffer[cooked_bytes] = (uint8_t)value;
197 cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
200 section[image->num_sections].size += 1;
203 } else if (record_type == 1) { /* End of File Record */
204 /* finish the current section */
205 image->num_sections++;
207 /* copy section information */
208 image->sections = malloc(sizeof(struct imagesection) * image->num_sections);
209 for (unsigned int i = 0; i < image->num_sections; i++) {
210 image->sections[i].private = section[i].private;
211 image->sections[i].base_address = section[i].base_address;
212 image->sections[i].size = section[i].size;
213 image->sections[i].flags = section[i].flags;
218 } else if (record_type == 2) { /* Linear Address Record */
219 uint16_t upper_address;
221 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
222 cal_checksum += (uint8_t)(upper_address >> 8);
223 cal_checksum += (uint8_t)upper_address;
226 if ((full_address >> 4) != upper_address) {
227 /* we encountered a nonconsecutive location, create a new section,
228 * unless the current section has zero size, in which case this specifies
229 * the current section's base address
231 if (section[image->num_sections].size != 0) {
232 image->num_sections++;
233 if (image->num_sections >= IMAGE_MAX_SECTIONS) {
234 /* too many sections */
235 LOG_ERROR("Too many sections found in IHEX file");
236 return ERROR_IMAGE_FORMAT_ERROR;
238 section[image->num_sections].size = 0x0;
239 section[image->num_sections].flags = 0;
240 section[image->num_sections].private =
241 &ihex->buffer[cooked_bytes];
243 section[image->num_sections].base_address =
244 (full_address & 0xffff) | (upper_address << 4);
245 full_address = (full_address & 0xffff) | (upper_address << 4);
247 } else if (record_type == 3) { /* Start Segment Address Record */
250 /* "Start Segment Address Record" will not be supported
251 * but we must consume it, and do not create an error. */
252 while (count-- > 0) {
253 sscanf(&lpszLine[bytes_read], "%2" SCNx32, &dummy);
254 cal_checksum += (uint8_t)dummy;
257 } else if (record_type == 4) { /* Extended Linear Address Record */
258 uint16_t upper_address;
260 sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
261 cal_checksum += (uint8_t)(upper_address >> 8);
262 cal_checksum += (uint8_t)upper_address;
265 if ((full_address >> 16) != upper_address) {
266 /* we encountered a nonconsecutive location, create a new section,
267 * unless the current section has zero size, in which case this specifies
268 * the current section's base address
270 if (section[image->num_sections].size != 0) {
271 image->num_sections++;
272 if (image->num_sections >= IMAGE_MAX_SECTIONS) {
273 /* too many sections */
274 LOG_ERROR("Too many sections found in IHEX file");
275 return ERROR_IMAGE_FORMAT_ERROR;
277 section[image->num_sections].size = 0x0;
278 section[image->num_sections].flags = 0;
279 section[image->num_sections].private =
280 &ihex->buffer[cooked_bytes];
282 section[image->num_sections].base_address =
283 (full_address & 0xffff) | (upper_address << 16);
284 full_address = (full_address & 0xffff) | (upper_address << 16);
286 } else if (record_type == 5) { /* Start Linear Address Record */
287 uint32_t start_address;
289 sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
290 cal_checksum += (uint8_t)(start_address >> 24);
291 cal_checksum += (uint8_t)(start_address >> 16);
292 cal_checksum += (uint8_t)(start_address >> 8);
293 cal_checksum += (uint8_t)start_address;
296 image->start_address_set = true;
297 image->start_address = be_to_h_u32((uint8_t *)&start_address);
299 LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
300 return ERROR_IMAGE_FORMAT_ERROR;
303 sscanf(&lpszLine[bytes_read], "%2" SCNx32, &checksum);
305 if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1)) {
306 /* checksum failed */
307 LOG_ERROR("incorrect record checksum found in IHEX file");
308 return ERROR_IMAGE_CHECKSUM;
313 LOG_WARNING("continuing after end-of-file record: %.40s", lpszLine);
321 LOG_ERROR("premature end of IHEX file, no matching end-of-file record found");
322 return ERROR_IMAGE_FORMAT_ERROR;
327 * Allocate memory dynamically instead of on the stack. This
328 * is important w/embedded hosts.
330 static int image_ihex_buffer_complete(struct image *image)
332 char *lpszLine = malloc(1023);
333 if (lpszLine == NULL) {
334 LOG_ERROR("Out of memory");
337 struct imagesection *section = malloc(sizeof(struct imagesection) * IMAGE_MAX_SECTIONS);
338 if (section == NULL) {
340 LOG_ERROR("Out of memory");
345 retval = image_ihex_buffer_complete_inner(image, lpszLine, section);
353 static int image_elf_read_headers(struct image *image)
355 struct image_elf *elf = image->type_private;
359 uint32_t nload, load_to_vaddr = 0;
361 elf->header = malloc(sizeof(Elf32_Ehdr));
363 if (elf->header == NULL) {
364 LOG_ERROR("insufficient memory to perform operation ");
365 return ERROR_FILEIO_OPERATION_FAILED;
368 retval = fileio_read(elf->fileio, sizeof(Elf32_Ehdr), (uint8_t *)elf->header, &read_bytes);
369 if (retval != ERROR_OK) {
370 LOG_ERROR("cannot read ELF file header, read failed");
371 return ERROR_FILEIO_OPERATION_FAILED;
373 if (read_bytes != sizeof(Elf32_Ehdr)) {
374 LOG_ERROR("cannot read ELF file header, only partially read");
375 return ERROR_FILEIO_OPERATION_FAILED;
378 if (strncmp((char *)elf->header->e_ident, ELFMAG, SELFMAG) != 0) {
379 LOG_ERROR("invalid ELF file, bad magic number");
380 return ERROR_IMAGE_FORMAT_ERROR;
382 if (elf->header->e_ident[EI_CLASS] != ELFCLASS32) {
383 LOG_ERROR("invalid ELF file, only 32bits files are supported");
384 return ERROR_IMAGE_FORMAT_ERROR;
387 elf->endianness = elf->header->e_ident[EI_DATA];
388 if ((elf->endianness != ELFDATA2LSB)
389 && (elf->endianness != ELFDATA2MSB)) {
390 LOG_ERROR("invalid ELF file, unknown endianness setting");
391 return ERROR_IMAGE_FORMAT_ERROR;
394 elf->segment_count = field16(elf, elf->header->e_phnum);
395 if (elf->segment_count == 0) {
396 LOG_ERROR("invalid ELF file, no program headers");
397 return ERROR_IMAGE_FORMAT_ERROR;
400 retval = fileio_seek(elf->fileio, field32(elf, elf->header->e_phoff));
401 if (retval != ERROR_OK) {
402 LOG_ERROR("cannot seek to ELF program header table, read failed");
406 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
407 if (elf->segments == NULL) {
408 LOG_ERROR("insufficient memory to perform operation ");
409 return ERROR_FILEIO_OPERATION_FAILED;
412 retval = fileio_read(elf->fileio, elf->segment_count*sizeof(Elf32_Phdr),
413 (uint8_t *)elf->segments, &read_bytes);
414 if (retval != ERROR_OK) {
415 LOG_ERROR("cannot read ELF segment headers, read failed");
418 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr)) {
419 LOG_ERROR("cannot read ELF segment headers, only partially read");
420 return ERROR_FILEIO_OPERATION_FAILED;
423 /* count useful segments (loadable), ignore BSS section */
424 image->num_sections = 0;
425 for (i = 0; i < elf->segment_count; i++)
427 elf->segments[i].p_type) == PT_LOAD) &&
428 (field32(elf, elf->segments[i].p_filesz) != 0))
429 image->num_sections++;
431 assert(image->num_sections > 0);
434 * some ELF linkers produce binaries with *all* the program header
435 * p_paddr fields zero (there can be however one loadable segment
436 * that has valid physical address 0x0).
437 * If we have such a binary with more than
438 * one PT_LOAD header, then use p_vaddr instead of p_paddr
439 * (ARM ELF standard demands p_paddr = 0 anyway, and BFD
440 * library uses this approach to workaround zero-initialized p_paddrs
441 * when obtaining lma - look at elf.c of BDF)
443 for (nload = 0, i = 0; i < elf->segment_count; i++)
444 if (elf->segments[i].p_paddr != 0)
446 else if ((field32(elf,
447 elf->segments[i].p_type) == PT_LOAD) &&
448 (field32(elf, elf->segments[i].p_memsz) != 0))
451 if (i >= elf->segment_count && nload > 1)
454 /* alloc and fill sections array with loadable segments */
455 image->sections = malloc(image->num_sections * sizeof(struct imagesection));
456 for (i = 0, j = 0; i < elf->segment_count; i++) {
458 elf->segments[i].p_type) == PT_LOAD) &&
459 (field32(elf, elf->segments[i].p_filesz) != 0)) {
460 image->sections[j].size = field32(elf, elf->segments[i].p_filesz);
462 image->sections[j].base_address = field32(elf,
463 elf->segments[i].p_vaddr);
465 image->sections[j].base_address = field32(elf,
466 elf->segments[i].p_paddr);
467 image->sections[j].private = &elf->segments[i];
468 image->sections[j].flags = field32(elf, elf->segments[i].p_flags);
473 image->start_address_set = true;
474 image->start_address = field32(elf, elf->header->e_entry);
479 static int image_elf_read_section(struct image *image,
486 struct image_elf *elf = image->type_private;
487 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
488 size_t read_size, really_read;
493 LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz = 0x%" PRIx32 ")", section, offset, size);
495 /* read initialized data in current segment if any */
496 if (offset < field32(elf, segment->p_filesz)) {
497 /* maximal size present in file for the current segment */
498 read_size = MIN(size, field32(elf, segment->p_filesz) - offset);
499 LOG_DEBUG("read elf: size = 0x%zx at 0x%" PRIx32 "", read_size,
500 field32(elf, segment->p_offset) + offset);
501 /* read initialized area of the segment */
502 retval = fileio_seek(elf->fileio, field32(elf, segment->p_offset) + offset);
503 if (retval != ERROR_OK) {
504 LOG_ERROR("cannot find ELF segment content, seek failed");
507 retval = fileio_read(elf->fileio, read_size, buffer, &really_read);
508 if (retval != ERROR_OK) {
509 LOG_ERROR("cannot read ELF segment content, read failed");
513 *size_read += read_size;
514 /* need more data ? */
522 static int image_mot_buffer_complete_inner(struct image *image,
524 struct imagesection *section)
526 struct image_mot *mot = image->type_private;
527 struct fileio *fileio = mot->fileio;
528 uint32_t full_address;
529 uint32_t cooked_bytes;
530 bool end_rec = false;
532 /* we can't determine the number of sections that we'll have to create ahead of time,
533 * so we locally hold them until parsing is finished */
537 retval = fileio_size(fileio, &filesize);
538 if (retval != ERROR_OK)
541 mot->buffer = malloc(filesize >> 1);
543 image->num_sections = 0;
545 while (!fileio_feof(fileio)) {
547 section[image->num_sections].private = &mot->buffer[cooked_bytes];
548 section[image->num_sections].base_address = 0x0;
549 section[image->num_sections].size = 0x0;
550 section[image->num_sections].flags = 0;
552 while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK) {
555 uint32_t record_type;
557 uint8_t cal_checksum = 0;
558 uint32_t bytes_read = 0;
560 /* skip comments and blank lines */
561 if ((lpszLine[0] == '#') || (strlen(lpszLine + strspn(lpszLine, "\n\t\r ")) == 0))
564 /* get record type and record length */
565 if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32, &record_type,
567 return ERROR_IMAGE_FORMAT_ERROR;
570 cal_checksum += (uint8_t)count;
572 /* skip checksum byte */
575 if (record_type == 0) {
576 /* S0 - starting record (optional) */
579 while (count-- > 0) {
580 sscanf(&lpszLine[bytes_read], "%2x", &iValue);
581 cal_checksum += (uint8_t)iValue;
584 } else if (record_type >= 1 && record_type <= 3) {
585 switch (record_type) {
587 /* S1 - 16 bit address data record */
588 sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
589 cal_checksum += (uint8_t)(address >> 8);
590 cal_checksum += (uint8_t)address;
596 /* S2 - 24 bit address data record */
597 sscanf(&lpszLine[bytes_read], "%6" SCNx32, &address);
598 cal_checksum += (uint8_t)(address >> 16);
599 cal_checksum += (uint8_t)(address >> 8);
600 cal_checksum += (uint8_t)address;
606 /* S3 - 32 bit address data record */
607 sscanf(&lpszLine[bytes_read], "%8" SCNx32, &address);
608 cal_checksum += (uint8_t)(address >> 24);
609 cal_checksum += (uint8_t)(address >> 16);
610 cal_checksum += (uint8_t)(address >> 8);
611 cal_checksum += (uint8_t)address;
618 if (full_address != address) {
619 /* we encountered a nonconsecutive location, create a new section,
620 * unless the current section has zero size, in which case this specifies
621 * the current section's base address
623 if (section[image->num_sections].size != 0) {
624 image->num_sections++;
625 section[image->num_sections].size = 0x0;
626 section[image->num_sections].flags = 0;
627 section[image->num_sections].private =
628 &mot->buffer[cooked_bytes];
630 section[image->num_sections].base_address = address;
631 full_address = address;
634 while (count-- > 0) {
636 sscanf(&lpszLine[bytes_read], "%2x", &value);
637 mot->buffer[cooked_bytes] = (uint8_t)value;
638 cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
641 section[image->num_sections].size += 1;
644 } else if (record_type == 5 || record_type == 6) {
645 /* S5 and S6 are the data count records, we ignore them */
648 while (count-- > 0) {
649 sscanf(&lpszLine[bytes_read], "%2" SCNx32, &dummy);
650 cal_checksum += (uint8_t)dummy;
653 } else if (record_type >= 7 && record_type <= 9) {
654 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
655 image->num_sections++;
657 /* copy section information */
658 image->sections = malloc(sizeof(struct imagesection) * image->num_sections);
659 for (unsigned int i = 0; i < image->num_sections; i++) {
660 image->sections[i].private = section[i].private;
661 image->sections[i].base_address = section[i].base_address;
662 image->sections[i].size = section[i].size;
663 image->sections[i].flags = section[i].flags;
669 LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
670 return ERROR_IMAGE_FORMAT_ERROR;
673 /* account for checksum, will always be 0xFF */
674 sscanf(&lpszLine[bytes_read], "%2" SCNx32, &checksum);
675 cal_checksum += (uint8_t)checksum;
677 if (cal_checksum != 0xFF) {
678 /* checksum failed */
679 LOG_ERROR("incorrect record checksum found in S19 file");
680 return ERROR_IMAGE_CHECKSUM;
685 LOG_WARNING("continuing after end-of-file record: %.40s", lpszLine);
693 LOG_ERROR("premature end of S19 file, no matching end-of-file record found");
694 return ERROR_IMAGE_FORMAT_ERROR;
699 * Allocate memory dynamically instead of on the stack. This
700 * is important w/embedded hosts.
702 static int image_mot_buffer_complete(struct image *image)
704 char *lpszLine = malloc(1023);
705 if (lpszLine == NULL) {
706 LOG_ERROR("Out of memory");
709 struct imagesection *section = malloc(sizeof(struct imagesection) * IMAGE_MAX_SECTIONS);
710 if (section == NULL) {
712 LOG_ERROR("Out of memory");
717 retval = image_mot_buffer_complete_inner(image, lpszLine, section);
725 int image_open(struct image *image, const char *url, const char *type_string)
727 int retval = ERROR_OK;
729 retval = identify_image_type(image, type_string, url);
730 if (retval != ERROR_OK)
733 if (image->type == IMAGE_BINARY) {
734 struct image_binary *image_binary;
736 image_binary = image->type_private = malloc(sizeof(struct image_binary));
738 retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY);
739 if (retval != ERROR_OK)
742 retval = fileio_size(image_binary->fileio, &filesize);
743 if (retval != ERROR_OK) {
744 fileio_close(image_binary->fileio);
748 image->num_sections = 1;
749 image->sections = malloc(sizeof(struct imagesection));
750 image->sections[0].base_address = 0x0;
751 image->sections[0].size = filesize;
752 image->sections[0].flags = 0;
753 } else if (image->type == IMAGE_IHEX) {
754 struct image_ihex *image_ihex;
756 image_ihex = image->type_private = malloc(sizeof(struct image_ihex));
758 retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT);
759 if (retval != ERROR_OK)
762 retval = image_ihex_buffer_complete(image);
763 if (retval != ERROR_OK) {
765 "failed buffering IHEX image, check server output for additional information");
766 fileio_close(image_ihex->fileio);
769 } else if (image->type == IMAGE_ELF) {
770 struct image_elf *image_elf;
772 image_elf = image->type_private = malloc(sizeof(struct image_elf));
774 retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY);
775 if (retval != ERROR_OK)
778 retval = image_elf_read_headers(image);
779 if (retval != ERROR_OK) {
780 fileio_close(image_elf->fileio);
783 } else if (image->type == IMAGE_MEMORY) {
784 struct target *target = get_target(url);
786 if (target == NULL) {
787 LOG_ERROR("target '%s' not defined", url);
791 struct image_memory *image_memory;
793 image->num_sections = 1;
794 image->sections = malloc(sizeof(struct imagesection));
795 image->sections[0].base_address = 0x0;
796 image->sections[0].size = 0xffffffff;
797 image->sections[0].flags = 0;
799 image_memory = image->type_private = malloc(sizeof(struct image_memory));
801 image_memory->target = target;
802 image_memory->cache = NULL;
803 image_memory->cache_address = 0x0;
804 } else if (image->type == IMAGE_SRECORD) {
805 struct image_mot *image_mot;
807 image_mot = image->type_private = malloc(sizeof(struct image_mot));
809 retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT);
810 if (retval != ERROR_OK)
813 retval = image_mot_buffer_complete(image);
814 if (retval != ERROR_OK) {
816 "failed buffering S19 image, check server output for additional information");
817 fileio_close(image_mot->fileio);
820 } else if (image->type == IMAGE_BUILDER) {
821 image->num_sections = 0;
822 image->base_address_set = false;
823 image->sections = NULL;
824 image->type_private = NULL;
827 if (image->base_address_set) {
829 for (unsigned int section = 0; section < image->num_sections; section++)
830 image->sections[section].base_address += image->base_address;
831 /* we're done relocating. The two statements below are mainly
832 * for documentation purposes: stop anyone from empirically
833 * thinking they should use these values henceforth. */
834 image->base_address = 0;
835 image->base_address_set = false;
841 int image_read_section(struct image *image,
850 /* don't read past the end of a section */
851 if (offset + size > image->sections[section].size) {
853 "read past end of section: 0x%8.8" PRIx32 " + 0x%8.8" PRIx32 " > 0x%8.8" PRIx32 "",
856 image->sections[section].size);
857 return ERROR_COMMAND_SYNTAX_ERROR;
860 if (image->type == IMAGE_BINARY) {
861 struct image_binary *image_binary = image->type_private;
863 /* only one section in a plain binary */
865 return ERROR_COMMAND_SYNTAX_ERROR;
868 retval = fileio_seek(image_binary->fileio, offset);
869 if (retval != ERROR_OK)
872 /* return requested bytes */
873 retval = fileio_read(image_binary->fileio, size, buffer, size_read);
874 if (retval != ERROR_OK)
876 } else if (image->type == IMAGE_IHEX) {
877 memcpy(buffer, (uint8_t *)image->sections[section].private + offset, size);
881 } else if (image->type == IMAGE_ELF)
882 return image_elf_read_section(image, section, offset, size, buffer, size_read);
883 else if (image->type == IMAGE_MEMORY) {
884 struct image_memory *image_memory = image->type_private;
885 uint32_t address = image->sections[section].base_address + offset;
889 while ((size - *size_read) > 0) {
890 uint32_t size_in_cache;
892 if (!image_memory->cache
893 || (address < image_memory->cache_address)
895 (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE))) {
896 if (!image_memory->cache)
897 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
899 if (target_read_buffer(image_memory->target, address &
900 ~(IMAGE_MEMORY_CACHE_SIZE - 1),
901 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK) {
902 free(image_memory->cache);
903 image_memory->cache = NULL;
904 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
906 image_memory->cache_address = address &
907 ~(IMAGE_MEMORY_CACHE_SIZE - 1);
911 (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
913 memcpy(buffer + *size_read,
914 image_memory->cache + (address - image_memory->cache_address),
915 (size_in_cache > size) ? size : size_in_cache
918 *size_read += (size_in_cache > size) ? size : size_in_cache;
919 address += (size_in_cache > size) ? size : size_in_cache;
921 } else if (image->type == IMAGE_SRECORD) {
922 memcpy(buffer, (uint8_t *)image->sections[section].private + offset, size);
926 } else if (image->type == IMAGE_BUILDER) {
927 memcpy(buffer, (uint8_t *)image->sections[section].private + offset, size);
936 int image_add_section(struct image *image, uint32_t base, uint32_t size, int flags, uint8_t const *data)
938 struct imagesection *section;
940 /* only image builder supports adding sections */
941 if (image->type != IMAGE_BUILDER)
942 return ERROR_COMMAND_SYNTAX_ERROR;
944 /* see if there's a previous section */
945 if (image->num_sections) {
946 section = &image->sections[image->num_sections - 1];
948 /* see if it's enough to extend the last section,
949 * adding data to previous sections or merging is not supported */
950 if (((section->base_address + section->size) == base) &&
951 (section->flags == flags)) {
952 section->private = realloc(section->private, section->size + size);
953 memcpy((uint8_t *)section->private + section->size, data, size);
954 section->size += size;
959 /* allocate new section */
960 image->num_sections++;
962 realloc(image->sections, sizeof(struct imagesection) * image->num_sections);
963 section = &image->sections[image->num_sections - 1];
964 section->base_address = base;
965 section->size = size;
966 section->flags = flags;
967 section->private = malloc(sizeof(uint8_t) * size);
968 memcpy((uint8_t *)section->private, data, size);
973 void image_close(struct image *image)
975 if (image->type == IMAGE_BINARY) {
976 struct image_binary *image_binary = image->type_private;
978 fileio_close(image_binary->fileio);
979 } else if (image->type == IMAGE_IHEX) {
980 struct image_ihex *image_ihex = image->type_private;
982 fileio_close(image_ihex->fileio);
984 free(image_ihex->buffer);
985 image_ihex->buffer = NULL;
986 } else if (image->type == IMAGE_ELF) {
987 struct image_elf *image_elf = image->type_private;
989 fileio_close(image_elf->fileio);
991 free(image_elf->header);
992 image_elf->header = NULL;
994 free(image_elf->segments);
995 image_elf->segments = NULL;
996 } else if (image->type == IMAGE_MEMORY) {
997 struct image_memory *image_memory = image->type_private;
999 free(image_memory->cache);
1000 image_memory->cache = NULL;
1001 } else if (image->type == IMAGE_SRECORD) {
1002 struct image_mot *image_mot = image->type_private;
1004 fileio_close(image_mot->fileio);
1006 free(image_mot->buffer);
1007 image_mot->buffer = NULL;
1008 } else if (image->type == IMAGE_BUILDER) {
1009 for (unsigned int i = 0; i < image->num_sections; i++) {
1010 free(image->sections[i].private);
1011 image->sections[i].private = NULL;
1015 free(image->type_private);
1016 image->type_private = NULL;
1018 free(image->sections);
1019 image->sections = NULL;
1022 int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
1024 uint32_t crc = 0xffffffff;
1025 LOG_DEBUG("Calculating checksum");
1027 static uint32_t crc32_table[256];
1029 static bool first_init;
1031 /* Initialize the CRC table and the decoding table. */
1032 unsigned int i, j, c;
1033 for (i = 0; i < 256; i++) {
1035 for (c = i << 24, j = 8; j > 0; --j)
1036 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1043 while (nbytes > 0) {
1050 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
1055 LOG_DEBUG("Calculating checksum done");