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;
129 /* we can't determine the number of sections that we'll have to create ahead of time,
130 * so we locally hold them until parsing is finished */
134 retval
= fileio_size(fileio
, &filesize
);
135 if (retval
!= ERROR_OK
)
138 ihex
->buffer
= malloc(filesize
>> 1);
140 image
->num_sections
= 0;
142 while (!fileio_feof(fileio
)) {
144 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
145 section
[image
->num_sections
].base_address
= 0x0;
146 section
[image
->num_sections
].size
= 0x0;
147 section
[image
->num_sections
].flags
= 0;
149 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
) {
152 uint32_t record_type
;
154 uint8_t cal_checksum
= 0;
155 size_t bytes_read
= 0;
157 /* skip comments and blank lines */
158 if ((lpszLine
[0] == '#') || (strlen(lpszLine
+ strspn(lpszLine
, "\n\t\r ")) == 0))
161 if (sscanf(&lpszLine
[bytes_read
], ":%2" SCNx32
"%4" SCNx32
"%2" SCNx32
, &count
,
162 &address
, &record_type
) != 3)
163 return ERROR_IMAGE_FORMAT_ERROR
;
166 cal_checksum
+= (uint8_t)count
;
167 cal_checksum
+= (uint8_t)(address
>> 8);
168 cal_checksum
+= (uint8_t)address
;
169 cal_checksum
+= (uint8_t)record_type
;
171 if (record_type
== 0) { /* Data Record */
172 if ((full_address
& 0xffff) != address
) {
173 /* we encountered a nonconsecutive location, create a new section,
174 * unless the current section has zero size, in which case this specifies
175 * the current section's base address
177 if (section
[image
->num_sections
].size
!= 0) {
178 image
->num_sections
++;
179 if (image
->num_sections
>= IMAGE_MAX_SECTIONS
) {
180 /* too many sections */
181 LOG_ERROR("Too many sections found in IHEX file");
182 return ERROR_IMAGE_FORMAT_ERROR
;
184 section
[image
->num_sections
].size
= 0x0;
185 section
[image
->num_sections
].flags
= 0;
186 section
[image
->num_sections
].private =
187 &ihex
->buffer
[cooked_bytes
];
189 section
[image
->num_sections
].base_address
=
190 (full_address
& 0xffff0000) | address
;
191 full_address
= (full_address
& 0xffff0000) | address
;
194 while (count
-- > 0) {
196 sscanf(&lpszLine
[bytes_read
], "%2x", &value
);
197 ihex
->buffer
[cooked_bytes
] = (uint8_t)value
;
198 cal_checksum
+= (uint8_t)ihex
->buffer
[cooked_bytes
];
201 section
[image
->num_sections
].size
+= 1;
204 } else if (record_type
== 1) { /* End of File Record */
205 /* finish the current section */
206 image
->num_sections
++;
208 /* copy section information */
209 image
->sections
= malloc(sizeof(struct imagesection
) * image
->num_sections
);
210 for (i
= 0; i
< image
->num_sections
; i
++) {
211 image
->sections
[i
].private = section
[i
].private;
212 image
->sections
[i
].base_address
= section
[i
].base_address
;
213 image
->sections
[i
].size
= section
[i
].size
;
214 image
->sections
[i
].flags
= section
[i
].flags
;
219 } else if (record_type
== 2) { /* Linear Address Record */
220 uint16_t upper_address
;
222 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
223 cal_checksum
+= (uint8_t)(upper_address
>> 8);
224 cal_checksum
+= (uint8_t)upper_address
;
227 if ((full_address
>> 4) != upper_address
) {
228 /* we encountered a nonconsecutive location, create a new section,
229 * unless the current section has zero size, in which case this specifies
230 * the current section's base address
232 if (section
[image
->num_sections
].size
!= 0) {
233 image
->num_sections
++;
234 if (image
->num_sections
>= IMAGE_MAX_SECTIONS
) {
235 /* too many sections */
236 LOG_ERROR("Too many sections found in IHEX file");
237 return ERROR_IMAGE_FORMAT_ERROR
;
239 section
[image
->num_sections
].size
= 0x0;
240 section
[image
->num_sections
].flags
= 0;
241 section
[image
->num_sections
].private =
242 &ihex
->buffer
[cooked_bytes
];
244 section
[image
->num_sections
].base_address
=
245 (full_address
& 0xffff) | (upper_address
<< 4);
246 full_address
= (full_address
& 0xffff) | (upper_address
<< 4);
248 } else if (record_type
== 3) { /* Start Segment Address Record */
251 /* "Start Segment Address Record" will not be supported
252 * but we must consume it, and do not create an error. */
253 while (count
-- > 0) {
254 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &dummy
);
255 cal_checksum
+= (uint8_t)dummy
;
258 } else if (record_type
== 4) { /* Extended Linear Address Record */
259 uint16_t upper_address
;
261 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
262 cal_checksum
+= (uint8_t)(upper_address
>> 8);
263 cal_checksum
+= (uint8_t)upper_address
;
266 if ((full_address
>> 16) != upper_address
) {
267 /* we encountered a nonconsecutive location, create a new section,
268 * unless the current section has zero size, in which case this specifies
269 * the current section's base address
271 if (section
[image
->num_sections
].size
!= 0) {
272 image
->num_sections
++;
273 if (image
->num_sections
>= IMAGE_MAX_SECTIONS
) {
274 /* too many sections */
275 LOG_ERROR("Too many sections found in IHEX file");
276 return ERROR_IMAGE_FORMAT_ERROR
;
278 section
[image
->num_sections
].size
= 0x0;
279 section
[image
->num_sections
].flags
= 0;
280 section
[image
->num_sections
].private =
281 &ihex
->buffer
[cooked_bytes
];
283 section
[image
->num_sections
].base_address
=
284 (full_address
& 0xffff) | (upper_address
<< 16);
285 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
287 } else if (record_type
== 5) { /* Start Linear Address Record */
288 uint32_t start_address
;
290 sscanf(&lpszLine
[bytes_read
], "%8" SCNx32
, &start_address
);
291 cal_checksum
+= (uint8_t)(start_address
>> 24);
292 cal_checksum
+= (uint8_t)(start_address
>> 16);
293 cal_checksum
+= (uint8_t)(start_address
>> 8);
294 cal_checksum
+= (uint8_t)start_address
;
297 image
->start_address_set
= 1;
298 image
->start_address
= be_to_h_u32((uint8_t *)&start_address
);
300 LOG_ERROR("unhandled IHEX record type: %i", (int)record_type
);
301 return ERROR_IMAGE_FORMAT_ERROR
;
304 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &checksum
);
306 if ((uint8_t)checksum
!= (uint8_t)(~cal_checksum
+ 1)) {
307 /* checksum failed */
308 LOG_ERROR("incorrect record checksum found in IHEX file");
309 return ERROR_IMAGE_CHECKSUM
;
314 LOG_WARNING("continuing after end-of-file record: %.40s", lpszLine
);
322 LOG_ERROR("premature end of IHEX file, no matching end-of-file record found");
323 return ERROR_IMAGE_FORMAT_ERROR
;
328 * Allocate memory dynamically instead of on the stack. This
329 * is important w/embedded hosts.
331 static int image_ihex_buffer_complete(struct image
*image
)
333 char *lpszLine
= malloc(1023);
334 if (lpszLine
== NULL
) {
335 LOG_ERROR("Out of memory");
338 struct imagesection
*section
= malloc(sizeof(struct imagesection
) * IMAGE_MAX_SECTIONS
);
339 if (section
== NULL
) {
341 LOG_ERROR("Out of memory");
346 retval
= image_ihex_buffer_complete_inner(image
, lpszLine
, section
);
354 static int image_elf_read_headers(struct image
*image
)
356 struct image_elf
*elf
= image
->type_private
;
360 uint32_t nload
, load_to_vaddr
= 0;
362 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
364 if (elf
->header
== NULL
) {
365 LOG_ERROR("insufficient memory to perform operation ");
366 return ERROR_FILEIO_OPERATION_FAILED
;
369 retval
= fileio_read(elf
->fileio
, sizeof(Elf32_Ehdr
), (uint8_t *)elf
->header
, &read_bytes
);
370 if (retval
!= ERROR_OK
) {
371 LOG_ERROR("cannot read ELF file header, read failed");
372 return ERROR_FILEIO_OPERATION_FAILED
;
374 if (read_bytes
!= sizeof(Elf32_Ehdr
)) {
375 LOG_ERROR("cannot read ELF file header, only partially read");
376 return ERROR_FILEIO_OPERATION_FAILED
;
379 if (strncmp((char *)elf
->header
->e_ident
, ELFMAG
, SELFMAG
) != 0) {
380 LOG_ERROR("invalid ELF file, bad magic number");
381 return ERROR_IMAGE_FORMAT_ERROR
;
383 if (elf
->header
->e_ident
[EI_CLASS
] != ELFCLASS32
) {
384 LOG_ERROR("invalid ELF file, only 32bits files are supported");
385 return ERROR_IMAGE_FORMAT_ERROR
;
388 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
389 if ((elf
->endianness
!= ELFDATA2LSB
)
390 && (elf
->endianness
!= ELFDATA2MSB
)) {
391 LOG_ERROR("invalid ELF file, unknown endianness setting");
392 return ERROR_IMAGE_FORMAT_ERROR
;
395 elf
->segment_count
= field16(elf
, elf
->header
->e_phnum
);
396 if (elf
->segment_count
== 0) {
397 LOG_ERROR("invalid ELF file, no program headers");
398 return ERROR_IMAGE_FORMAT_ERROR
;
401 retval
= fileio_seek(elf
->fileio
, field32(elf
, elf
->header
->e_phoff
));
402 if (retval
!= 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
) {
409 LOG_ERROR("insufficient memory to perform operation ");
410 return ERROR_FILEIO_OPERATION_FAILED
;
413 retval
= fileio_read(elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
),
414 (uint8_t *)elf
->segments
, &read_bytes
);
415 if (retval
!= ERROR_OK
) {
416 LOG_ERROR("cannot read ELF segment headers, read failed");
419 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
)) {
420 LOG_ERROR("cannot read ELF segment headers, only partially read");
421 return ERROR_FILEIO_OPERATION_FAILED
;
424 /* count useful segments (loadable), ignore BSS section */
425 image
->num_sections
= 0;
426 for (i
= 0; i
< elf
->segment_count
; i
++)
428 elf
->segments
[i
].p_type
) == PT_LOAD
) &&
429 (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
430 image
->num_sections
++;
432 assert(image
->num_sections
> 0);
435 * some ELF linkers produce binaries with *all* the program header
436 * p_paddr fields zero (there can be however one loadable segment
437 * that has valid physical address 0x0).
438 * If we have such a binary with more than
439 * one PT_LOAD header, then use p_vaddr instead of p_paddr
440 * (ARM ELF standard demands p_paddr = 0 anyway, and BFD
441 * library uses this approach to workaround zero-initialized p_paddrs
442 * when obtaining lma - look at elf.c of BDF)
444 for (nload
= 0, i
= 0; i
< elf
->segment_count
; i
++)
445 if (elf
->segments
[i
].p_paddr
!= 0)
447 else if ((field32(elf
,
448 elf
->segments
[i
].p_type
) == PT_LOAD
) &&
449 (field32(elf
, elf
->segments
[i
].p_memsz
) != 0))
452 if (i
>= elf
->segment_count
&& nload
> 1)
455 /* alloc and fill sections array with loadable segments */
456 image
->sections
= malloc(image
->num_sections
* sizeof(struct imagesection
));
457 for (i
= 0, j
= 0; i
< elf
->segment_count
; i
++) {
459 elf
->segments
[i
].p_type
) == PT_LOAD
) &&
460 (field32(elf
, elf
->segments
[i
].p_filesz
) != 0)) {
461 image
->sections
[j
].size
= field32(elf
, elf
->segments
[i
].p_filesz
);
463 image
->sections
[j
].base_address
= field32(elf
,
464 elf
->segments
[i
].p_vaddr
);
466 image
->sections
[j
].base_address
= field32(elf
,
467 elf
->segments
[i
].p_paddr
);
468 image
->sections
[j
].private = &elf
->segments
[i
];
469 image
->sections
[j
].flags
= field32(elf
, elf
->segments
[i
].p_flags
);
474 image
->start_address_set
= 1;
475 image
->start_address
= field32(elf
, elf
->header
->e_entry
);
480 static int image_elf_read_section(struct image
*image
,
487 struct image_elf
*elf
= image
->type_private
;
488 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
489 size_t read_size
, really_read
;
494 LOG_DEBUG("load segment %d at 0x%" PRIx32
" (sz = 0x%" PRIx32
")", section
, offset
, size
);
496 /* read initialized data in current segment if any */
497 if (offset
< field32(elf
, segment
->p_filesz
)) {
498 /* maximal size present in file for the current segment */
499 read_size
= MIN(size
, field32(elf
, segment
->p_filesz
) - offset
);
500 LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32
"", read_size
,
501 field32(elf
, segment
->p_offset
) + offset
);
502 /* read initialized area of the segment */
503 retval
= fileio_seek(elf
->fileio
, field32(elf
, segment
->p_offset
) + offset
);
504 if (retval
!= ERROR_OK
) {
505 LOG_ERROR("cannot find ELF segment content, seek failed");
508 retval
= fileio_read(elf
->fileio
, read_size
, buffer
, &really_read
);
509 if (retval
!= ERROR_OK
) {
510 LOG_ERROR("cannot read ELF segment content, read failed");
514 *size_read
+= read_size
;
515 /* need more data ? */
523 static int image_mot_buffer_complete_inner(struct image
*image
,
525 struct imagesection
*section
)
527 struct image_mot
*mot
= image
->type_private
;
528 struct fileio
*fileio
= mot
->fileio
;
529 uint32_t full_address
;
530 uint32_t cooked_bytes
;
531 bool end_rec
= false;
534 /* we can't determine the number of sections that we'll have to create ahead of time,
535 * so we locally hold them until parsing is finished */
539 retval
= fileio_size(fileio
, &filesize
);
540 if (retval
!= ERROR_OK
)
543 mot
->buffer
= malloc(filesize
>> 1);
545 image
->num_sections
= 0;
547 while (!fileio_feof(fileio
)) {
549 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
550 section
[image
->num_sections
].base_address
= 0x0;
551 section
[image
->num_sections
].size
= 0x0;
552 section
[image
->num_sections
].flags
= 0;
554 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
) {
557 uint32_t record_type
;
559 uint8_t cal_checksum
= 0;
560 uint32_t bytes_read
= 0;
562 /* skip comments and blank lines */
563 if ((lpszLine
[0] == '#') || (strlen(lpszLine
+ strspn(lpszLine
, "\n\t\r ")) == 0))
566 /* get record type and record length */
567 if (sscanf(&lpszLine
[bytes_read
], "S%1" SCNx32
"%2" SCNx32
, &record_type
,
569 return ERROR_IMAGE_FORMAT_ERROR
;
572 cal_checksum
+= (uint8_t)count
;
574 /* skip checksum byte */
577 if (record_type
== 0) {
578 /* S0 - starting record (optional) */
581 while (count
-- > 0) {
582 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
583 cal_checksum
+= (uint8_t)iValue
;
586 } else if (record_type
>= 1 && record_type
<= 3) {
587 switch (record_type
) {
589 /* S1 - 16 bit address data record */
590 sscanf(&lpszLine
[bytes_read
], "%4" SCNx32
, &address
);
591 cal_checksum
+= (uint8_t)(address
>> 8);
592 cal_checksum
+= (uint8_t)address
;
598 /* S2 - 24 bit address data record */
599 sscanf(&lpszLine
[bytes_read
], "%6" SCNx32
, &address
);
600 cal_checksum
+= (uint8_t)(address
>> 16);
601 cal_checksum
+= (uint8_t)(address
>> 8);
602 cal_checksum
+= (uint8_t)address
;
608 /* S3 - 32 bit address data record */
609 sscanf(&lpszLine
[bytes_read
], "%8" SCNx32
, &address
);
610 cal_checksum
+= (uint8_t)(address
>> 24);
611 cal_checksum
+= (uint8_t)(address
>> 16);
612 cal_checksum
+= (uint8_t)(address
>> 8);
613 cal_checksum
+= (uint8_t)address
;
620 if (full_address
!= address
) {
621 /* we encountered a nonconsecutive location, create a new section,
622 * unless the current section has zero size, in which case this specifies
623 * the current section's base address
625 if (section
[image
->num_sections
].size
!= 0) {
626 image
->num_sections
++;
627 section
[image
->num_sections
].size
= 0x0;
628 section
[image
->num_sections
].flags
= 0;
629 section
[image
->num_sections
].private =
630 &mot
->buffer
[cooked_bytes
];
632 section
[image
->num_sections
].base_address
= address
;
633 full_address
= address
;
636 while (count
-- > 0) {
638 sscanf(&lpszLine
[bytes_read
], "%2x", &value
);
639 mot
->buffer
[cooked_bytes
] = (uint8_t)value
;
640 cal_checksum
+= (uint8_t)mot
->buffer
[cooked_bytes
];
643 section
[image
->num_sections
].size
+= 1;
646 } else if (record_type
== 5) {
647 /* S5 is the data count record, we ignore it */
650 while (count
-- > 0) {
651 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &dummy
);
652 cal_checksum
+= (uint8_t)dummy
;
655 } else if (record_type
>= 7 && record_type
<= 9) {
656 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
657 image
->num_sections
++;
659 /* copy section information */
660 image
->sections
= malloc(sizeof(struct imagesection
) * image
->num_sections
);
661 for (i
= 0; i
< image
->num_sections
; i
++) {
662 image
->sections
[i
].private = section
[i
].private;
663 image
->sections
[i
].base_address
= section
[i
].base_address
;
664 image
->sections
[i
].size
= section
[i
].size
;
665 image
->sections
[i
].flags
= section
[i
].flags
;
671 LOG_ERROR("unhandled S19 record type: %i", (int)(record_type
));
672 return ERROR_IMAGE_FORMAT_ERROR
;
675 /* account for checksum, will always be 0xFF */
676 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &checksum
);
677 cal_checksum
+= (uint8_t)checksum
;
679 if (cal_checksum
!= 0xFF) {
680 /* checksum failed */
681 LOG_ERROR("incorrect record checksum found in S19 file");
682 return ERROR_IMAGE_CHECKSUM
;
687 LOG_WARNING("continuing after end-of-file record: %.40s", lpszLine
);
695 LOG_ERROR("premature end of S19 file, no matching end-of-file record found");
696 return ERROR_IMAGE_FORMAT_ERROR
;
701 * Allocate memory dynamically instead of on the stack. This
702 * is important w/embedded hosts.
704 static int image_mot_buffer_complete(struct image
*image
)
706 char *lpszLine
= malloc(1023);
707 if (lpszLine
== NULL
) {
708 LOG_ERROR("Out of memory");
711 struct imagesection
*section
= malloc(sizeof(struct imagesection
) * IMAGE_MAX_SECTIONS
);
712 if (section
== NULL
) {
714 LOG_ERROR("Out of memory");
719 retval
= image_mot_buffer_complete_inner(image
, lpszLine
, section
);
727 int image_open(struct image
*image
, const char *url
, const char *type_string
)
729 int retval
= ERROR_OK
;
731 retval
= identify_image_type(image
, type_string
, url
);
732 if (retval
!= ERROR_OK
)
735 if (image
->type
== IMAGE_BINARY
) {
736 struct image_binary
*image_binary
;
738 image_binary
= image
->type_private
= malloc(sizeof(struct image_binary
));
740 retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
);
741 if (retval
!= ERROR_OK
)
744 retval
= fileio_size(image_binary
->fileio
, &filesize
);
745 if (retval
!= ERROR_OK
) {
746 fileio_close(image_binary
->fileio
);
750 image
->num_sections
= 1;
751 image
->sections
= malloc(sizeof(struct imagesection
));
752 image
->sections
[0].base_address
= 0x0;
753 image
->sections
[0].size
= filesize
;
754 image
->sections
[0].flags
= 0;
755 } else if (image
->type
== IMAGE_IHEX
) {
756 struct image_ihex
*image_ihex
;
758 image_ihex
= image
->type_private
= malloc(sizeof(struct image_ihex
));
760 retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
);
761 if (retval
!= ERROR_OK
)
764 retval
= image_ihex_buffer_complete(image
);
765 if (retval
!= ERROR_OK
) {
767 "failed buffering IHEX image, check server output for additional information");
768 fileio_close(image_ihex
->fileio
);
771 } else if (image
->type
== IMAGE_ELF
) {
772 struct image_elf
*image_elf
;
774 image_elf
= image
->type_private
= malloc(sizeof(struct image_elf
));
776 retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
);
777 if (retval
!= ERROR_OK
)
780 retval
= image_elf_read_headers(image
);
781 if (retval
!= ERROR_OK
) {
782 fileio_close(image_elf
->fileio
);
785 } else if (image
->type
== IMAGE_MEMORY
) {
786 struct target
*target
= get_target(url
);
788 if (target
== NULL
) {
789 LOG_ERROR("target '%s' not defined", url
);
793 struct image_memory
*image_memory
;
795 image
->num_sections
= 1;
796 image
->sections
= malloc(sizeof(struct imagesection
));
797 image
->sections
[0].base_address
= 0x0;
798 image
->sections
[0].size
= 0xffffffff;
799 image
->sections
[0].flags
= 0;
801 image_memory
= image
->type_private
= malloc(sizeof(struct image_memory
));
803 image_memory
->target
= target
;
804 image_memory
->cache
= NULL
;
805 image_memory
->cache_address
= 0x0;
806 } else if (image
->type
== IMAGE_SRECORD
) {
807 struct image_mot
*image_mot
;
809 image_mot
= image
->type_private
= malloc(sizeof(struct image_mot
));
811 retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
);
812 if (retval
!= ERROR_OK
)
815 retval
= image_mot_buffer_complete(image
);
816 if (retval
!= ERROR_OK
) {
818 "failed buffering S19 image, check server output for additional information");
819 fileio_close(image_mot
->fileio
);
822 } else if (image
->type
== IMAGE_BUILDER
) {
823 image
->num_sections
= 0;
824 image
->base_address_set
= 0;
825 image
->sections
= NULL
;
826 image
->type_private
= NULL
;
829 if (image
->base_address_set
) {
832 for (section
= 0; section
< image
->num_sections
; section
++)
833 image
->sections
[section
].base_address
+= image
->base_address
;
834 /* we're done relocating. The two statements below are mainly
835 * for documenation purposes: stop anyone from empirically
836 * thinking they should use these values henceforth. */
837 image
->base_address
= 0;
838 image
->base_address_set
= 0;
844 int image_read_section(struct image
*image
,
853 /* don't read past the end of a section */
854 if (offset
+ size
> image
->sections
[section
].size
) {
856 "read past end of section: 0x%8.8" PRIx32
" + 0x%8.8" PRIx32
" > 0x%8.8" PRIx32
"",
859 image
->sections
[section
].size
);
860 return ERROR_COMMAND_SYNTAX_ERROR
;
863 if (image
->type
== IMAGE_BINARY
) {
864 struct image_binary
*image_binary
= image
->type_private
;
866 /* only one section in a plain binary */
868 return ERROR_COMMAND_SYNTAX_ERROR
;
871 retval
= fileio_seek(image_binary
->fileio
, offset
);
872 if (retval
!= ERROR_OK
)
875 /* return requested bytes */
876 retval
= fileio_read(image_binary
->fileio
, size
, buffer
, size_read
);
877 if (retval
!= ERROR_OK
)
879 } else if (image
->type
== IMAGE_IHEX
) {
880 memcpy(buffer
, (uint8_t *)image
->sections
[section
].private + offset
, size
);
884 } else if (image
->type
== IMAGE_ELF
)
885 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
886 else if (image
->type
== IMAGE_MEMORY
) {
887 struct image_memory
*image_memory
= image
->type_private
;
888 uint32_t address
= image
->sections
[section
].base_address
+ offset
;
892 while ((size
- *size_read
) > 0) {
893 uint32_t size_in_cache
;
895 if (!image_memory
->cache
896 || (address
< image_memory
->cache_address
)
898 (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
))) {
899 if (!image_memory
->cache
)
900 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
902 if (target_read_buffer(image_memory
->target
, address
&
903 ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
904 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
) {
905 free(image_memory
->cache
);
906 image_memory
->cache
= NULL
;
907 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
909 image_memory
->cache_address
= address
&
910 ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
914 (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
916 memcpy(buffer
+ *size_read
,
917 image_memory
->cache
+ (address
- image_memory
->cache_address
),
918 (size_in_cache
> size
) ? size
: size_in_cache
921 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
922 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
924 } else if (image
->type
== IMAGE_SRECORD
) {
925 memcpy(buffer
, (uint8_t *)image
->sections
[section
].private + offset
, size
);
929 } else if (image
->type
== IMAGE_BUILDER
) {
930 memcpy(buffer
, (uint8_t *)image
->sections
[section
].private + offset
, size
);
939 int image_add_section(struct image
*image
, uint32_t base
, uint32_t size
, int flags
, uint8_t const *data
)
941 struct imagesection
*section
;
943 /* only image builder supports adding sections */
944 if (image
->type
!= IMAGE_BUILDER
)
945 return ERROR_COMMAND_SYNTAX_ERROR
;
947 /* see if there's a previous section */
948 if (image
->num_sections
) {
949 section
= &image
->sections
[image
->num_sections
- 1];
951 /* see if it's enough to extend the last section,
952 * adding data to previous sections or merging is not supported */
953 if (((section
->base_address
+ section
->size
) == base
) &&
954 (section
->flags
== flags
)) {
955 section
->private = realloc(section
->private, section
->size
+ size
);
956 memcpy((uint8_t *)section
->private + section
->size
, data
, size
);
957 section
->size
+= size
;
962 /* allocate new section */
963 image
->num_sections
++;
965 realloc(image
->sections
, sizeof(struct imagesection
) * image
->num_sections
);
966 section
= &image
->sections
[image
->num_sections
- 1];
967 section
->base_address
= base
;
968 section
->size
= size
;
969 section
->flags
= flags
;
970 section
->private = malloc(sizeof(uint8_t) * size
);
971 memcpy((uint8_t *)section
->private, data
, size
);
976 void image_close(struct image
*image
)
978 if (image
->type
== IMAGE_BINARY
) {
979 struct image_binary
*image_binary
= image
->type_private
;
981 fileio_close(image_binary
->fileio
);
982 } else if (image
->type
== IMAGE_IHEX
) {
983 struct image_ihex
*image_ihex
= image
->type_private
;
985 fileio_close(image_ihex
->fileio
);
987 if (image_ihex
->buffer
) {
988 free(image_ihex
->buffer
);
989 image_ihex
->buffer
= NULL
;
991 } else if (image
->type
== IMAGE_ELF
) {
992 struct image_elf
*image_elf
= image
->type_private
;
994 fileio_close(image_elf
->fileio
);
996 if (image_elf
->header
) {
997 free(image_elf
->header
);
998 image_elf
->header
= NULL
;
1001 if (image_elf
->segments
) {
1002 free(image_elf
->segments
);
1003 image_elf
->segments
= NULL
;
1005 } else if (image
->type
== IMAGE_MEMORY
) {
1006 struct image_memory
*image_memory
= image
->type_private
;
1008 if (image_memory
->cache
) {
1009 free(image_memory
->cache
);
1010 image_memory
->cache
= NULL
;
1012 } else if (image
->type
== IMAGE_SRECORD
) {
1013 struct image_mot
*image_mot
= image
->type_private
;
1015 fileio_close(image_mot
->fileio
);
1017 if (image_mot
->buffer
) {
1018 free(image_mot
->buffer
);
1019 image_mot
->buffer
= NULL
;
1021 } else if (image
->type
== IMAGE_BUILDER
) {
1024 for (i
= 0; i
< image
->num_sections
; i
++) {
1025 free(image
->sections
[i
].private);
1026 image
->sections
[i
].private = NULL
;
1030 if (image
->type_private
) {
1031 free(image
->type_private
);
1032 image
->type_private
= NULL
;
1035 if (image
->sections
) {
1036 free(image
->sections
);
1037 image
->sections
= NULL
;
1041 int image_calculate_checksum(uint8_t *buffer
, uint32_t nbytes
, uint32_t *checksum
)
1043 uint32_t crc
= 0xffffffff;
1044 LOG_DEBUG("Calculating checksum");
1046 static uint32_t crc32_table
[256];
1048 static bool first_init
;
1050 /* Initialize the CRC table and the decoding table. */
1051 unsigned int i
, j
, c
;
1052 for (i
= 0; i
< 256; i
++) {
1054 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1055 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1062 while (nbytes
> 0) {
1069 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buffer
++) & 255];
1074 LOG_DEBUG("Calculating checksum done");