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
)
58 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
, "cannot open image: %s", fileio
.error_str
);
59 ERROR(image
->error_str
);
62 if ((retval
= fileio_read(&fileio
, 9, buffer
, &read_bytes
)) != ERROR_OK
)
64 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
, "cannot read image header: %s", fileio
.error_str
);
65 ERROR(image
->error_str
);
66 return ERROR_FILEIO_OPERATION_FAILED
;
70 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
, "cannot read image, only partially read");
71 ERROR(image
->error_str
);
72 return ERROR_FILEIO_OPERATION_FAILED
;
74 fileio_close(&fileio
);
76 /* check header against known signatures */
77 if (strncmp((char*)buffer
,ELFMAG
,SELFMAG
)==0)
79 DEBUG("ELF image detected.");
80 image
->type
= IMAGE_ELF
;
82 else if ((buffer
[0]==':') /* record start byte */
83 &&(isxdigit(buffer
[1]))
84 &&(isxdigit(buffer
[2]))
85 &&(isxdigit(buffer
[3]))
86 &&(isxdigit(buffer
[4]))
87 &&(isxdigit(buffer
[5]))
88 &&(isxdigit(buffer
[6]))
89 &&(buffer
[7]=='0') /* record type : 00 -> 05 */
90 &&(buffer
[8]>='0')&&(buffer
[8]<'6'))
92 DEBUG("IHEX image detected.");
93 image
->type
= IMAGE_IHEX
;
95 else if ((buffer
[0] == 'S') /* record start byte */
96 &&(isxdigit(buffer
[1]))
97 &&(isxdigit(buffer
[2]))
98 &&(isxdigit(buffer
[3]))
99 &&(buffer
[1] >= '0') && (buffer
[1] < '9'))
101 DEBUG("S19 image detected.");
102 image
->type
= IMAGE_SRECORD
;
106 image
->type
= IMAGE_BINARY
;
112 int identify_image_type(image_t
*image
, char *type_string
, char *url
)
116 if (!strcmp(type_string
, "bin"))
118 image
->type
= IMAGE_BINARY
;
120 else if (!strcmp(type_string
, "ihex"))
122 image
->type
= IMAGE_IHEX
;
124 else if (!strcmp(type_string
, "elf"))
126 image
->type
= IMAGE_ELF
;
128 else if (!strcmp(type_string
, "mem"))
130 image
->type
= IMAGE_MEMORY
;
132 else if (!strcmp(type_string
, "s19"))
134 image
->type
= IMAGE_SRECORD
;
138 return ERROR_IMAGE_TYPE_UNKNOWN
;
143 return autodetect_image_type(image
, url
);
149 int image_ihex_buffer_complete(image_t
*image
)
151 image_ihex_t
*ihex
= image
->type_private
;
152 fileio_t
*fileio
= &ihex
->fileio
;
153 u32 raw_bytes_read
, raw_bytes
;
155 u32 full_address
= 0x0;
156 char *buffer
= malloc(fileio
->size
);
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 if ((retval
= fileio_read(fileio
, fileio
->size
, (u8
*)buffer
, &raw_bytes_read
)) != ERROR_OK
)
167 ERROR("failed buffering IHEX file, read failed");
168 return ERROR_FILEIO_OPERATION_FAILED
;
171 if (raw_bytes_read
!= fileio
->size
)
174 ERROR("failed buffering complete IHEX file, only partially read");
175 return ERROR_FILEIO_OPERATION_FAILED
;
178 ihex
->buffer
= malloc(fileio
->size
>> 1);
181 image
->num_sections
= 0;
182 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
183 section
[image
->num_sections
].base_address
= 0x0;
184 section
[image
->num_sections
].size
= 0x0;
185 section
[image
->num_sections
].flags
= 0;
186 while (raw_bytes
< raw_bytes_read
)
194 if (sscanf(&buffer
[raw_bytes
], ":%2x%4x%2x", &count
, &address
, &record_type
) != 3)
196 return ERROR_IMAGE_FORMAT_ERROR
;
200 cal_checksum
+= (u8
)count
;
201 cal_checksum
+= (u8
)(address
>> 8);
202 cal_checksum
+= (u8
)address
;
203 cal_checksum
+= (u8
)record_type
;
205 if (record_type
== 0) /* Data Record */
207 if ((full_address
& 0xffff) != address
)
209 /* we encountered a nonconsecutive location, create a new section,
210 * unless the current section has zero size, in which case this specifies
211 * the current section's base address
213 if (section
[image
->num_sections
].size
!= 0)
215 image
->num_sections
++;
216 section
[image
->num_sections
].size
= 0x0;
217 section
[image
->num_sections
].flags
= 0;
218 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
220 section
[image
->num_sections
].base_address
=
221 (full_address
& 0xffff0000) | address
;
222 full_address
= (full_address
& 0xffff0000) | address
;
227 sscanf(&buffer
[raw_bytes
], "%2hhx", &ihex
->buffer
[cooked_bytes
]);
228 cal_checksum
+= (u8
)ihex
->buffer
[cooked_bytes
];
231 section
[image
->num_sections
].size
+= 1;
235 else if (record_type
== 1) /* End of File Record */
237 /* finish the current section */
238 image
->num_sections
++;
240 /* copy section information */
241 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
242 for (i
= 0; i
< image
->num_sections
; i
++)
244 image
->sections
[i
].private = section
[i
].private;
245 image
->sections
[i
].base_address
= section
[i
].base_address
+
246 ((image
->base_address_set
) ? image
->base_address
: 0);
247 image
->sections
[i
].size
= section
[i
].size
;
248 image
->sections
[i
].flags
= section
[i
].flags
;
254 else if (record_type
== 4) /* Extended Linear Address Record */
258 sscanf(&buffer
[raw_bytes
], "%4hx", &upper_address
);
259 cal_checksum
+= (u8
)(upper_address
>> 8);
260 cal_checksum
+= (u8
)upper_address
;
263 if ((full_address
>> 16) != upper_address
)
265 /* we encountered a nonconsecutive location, create a new section,
266 * unless the current section has zero size, in which case this specifies
267 * the current section's base address
269 if (section
[image
->num_sections
].size
!= 0)
271 image
->num_sections
++;
272 section
[image
->num_sections
].size
= 0x0;
273 section
[image
->num_sections
].flags
= 0;
274 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
276 section
[image
->num_sections
].base_address
=
277 (full_address
& 0xffff) | (upper_address
<< 16);
278 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
281 else if (record_type
== 5) /* Start Linear Address Record */
285 sscanf(&buffer
[raw_bytes
], "%8x", &start_address
);
286 cal_checksum
+= (u8
)(start_address
>> 24);
287 cal_checksum
+= (u8
)(start_address
>> 16);
288 cal_checksum
+= (u8
)(start_address
>> 8);
289 cal_checksum
+= (u8
)start_address
;
292 image
->start_address_set
= 1;
293 image
->start_address
= be_to_h_u32((u8
*)&start_address
);
298 ERROR("unhandled IHEX record type: %i", record_type
);
299 return ERROR_IMAGE_FORMAT_ERROR
;
302 sscanf(&buffer
[raw_bytes
], "%2x", &checksum
);
305 if ((u8
)checksum
!= (u8
)(~cal_checksum
+ 1))
307 /* checksum failed */
309 ERROR("incorrect record checksum found in IHEX file");
310 return ERROR_IMAGE_CHECKSUM
;
313 /* consume new-line character(s) */
314 if ((buffer
[raw_bytes
] == '\n') || (buffer
[raw_bytes
] == '\r'))
317 if ((buffer
[raw_bytes
] == '\n') || (buffer
[raw_bytes
] == '\r'))
322 ERROR("premature end of IHEX file, no end-of-file record found");
323 return ERROR_IMAGE_FORMAT_ERROR
;
326 int image_elf_read_headers(image_t
*image
)
328 image_elf_t
*elf
= image
->type_private
;
333 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
335 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (u8
*)elf
->header
, &read_bytes
)) != ERROR_OK
)
337 ERROR("cannot read ELF file header, read failed");
338 return ERROR_FILEIO_OPERATION_FAILED
;
340 if (read_bytes
!= sizeof(Elf32_Ehdr
))
342 ERROR("cannot read ELF file header, only partially read");
343 return ERROR_FILEIO_OPERATION_FAILED
;
346 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
)!=0)
348 ERROR("invalid ELF file, bad magic number");
349 return ERROR_IMAGE_FORMAT_ERROR
;
351 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
353 ERROR("invalid ELF file, only 32bits files are supported");
354 return ERROR_IMAGE_FORMAT_ERROR
;
358 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
359 if ((elf
->endianness
!=ELFDATA2LSB
)
360 &&(elf
->endianness
!=ELFDATA2MSB
))
362 ERROR("invalid ELF file, unknown endianess setting");
363 return ERROR_IMAGE_FORMAT_ERROR
;
366 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
367 if (elf
->segment_count
==0)
369 ERROR("invalid ELF file, no program headers");
370 return ERROR_IMAGE_FORMAT_ERROR
;
373 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
375 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (u8
*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
377 ERROR("cannot read ELF segment headers, read failed");
380 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
382 ERROR("cannot read ELF segment headers, only partially read");
383 return ERROR_FILEIO_OPERATION_FAILED
;
386 /* count useful segments (loadable), ignore BSS section */
387 image
->num_sections
= 0;
388 for (i
=0;i
<elf
->segment_count
;i
++)
389 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
390 image
->num_sections
++;
391 /* alloc and fill sections array with loadable segments */
392 image
->sections
= malloc(image
->num_sections
* sizeof(image_section_t
));
393 for (i
=0,j
=0;i
<elf
->segment_count
;i
++)
395 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
397 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_memsz
);
398 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_vaddr
);
399 image
->sections
[j
].private = &elf
->segments
[i
];
400 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
405 image
->start_address_set
= 1;
406 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
411 int image_elf_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
413 image_elf_t
*elf
= image
->type_private
;
414 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
415 u32 read_size
,really_read
;
420 DEBUG("load segment %d at 0x%x (sz=0x%x)",section
,offset
,size
);
422 /* read initialized data in current segment if any */
423 if (offset
<field32(elf
,segment
->p_filesz
))
425 /* maximal size present in file for the current segment */
426 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
427 DEBUG("read elf: size = 0x%x at 0x%x",read_size
,
428 field32(elf
,segment
->p_offset
)+offset
);
429 /* read initialized area of the segment */
430 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
)+offset
)) != ERROR_OK
)
432 ERROR("cannot find ELF segment content, seek failed");
435 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
437 ERROR("cannot read ELF segment content, read failed");
443 *size_read
+= read_size
;
444 /* need more data ? */
448 /* if there is remaining zeroed area in current segment */
449 if (offset
<field32(elf
,segment
->p_memsz
))
451 /* fill zeroed part (BSS) of the segment */
452 read_size
= MIN(size
, field32(elf
,segment
->p_memsz
)-offset
);
453 DEBUG("zero fill: size = 0x%x",read_size
);
454 memset(buffer
,0,read_size
);
455 *size_read
+= read_size
;
461 int image_mot_buffer_complete(image_t
*image
)
463 image_mot_t
*mot
= image
->type_private
;
464 fileio_t
*fileio
= &mot
->fileio
;
465 u32 raw_bytes_read
, raw_bytes
;
467 u32 full_address
= 0x0;
468 char *buffer
= malloc(fileio
->size
);
472 /* we can't determine the number of sections that we'll have to create ahead of time,
473 * so we locally hold them until parsing is finished */
474 image_section_t section
[IMAGE_MAX_SECTIONS
];
476 if ((retval
= fileio_read(fileio
, fileio
->size
, (u8
*)buffer
, &raw_bytes_read
)) != ERROR_OK
)
479 ERROR("failed buffering S19 file, read failed");
480 return ERROR_FILEIO_OPERATION_FAILED
;
483 if (raw_bytes_read
!= fileio
->size
)
486 ERROR("failed buffering complete IHEX file, only partially read");
487 return ERROR_FILEIO_OPERATION_FAILED
;
490 mot
->buffer
= malloc(fileio
->size
>> 1);
493 image
->num_sections
= 0;
494 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
495 section
[image
->num_sections
].base_address
= 0x0;
496 section
[image
->num_sections
].size
= 0x0;
497 section
[image
->num_sections
].flags
= 0;
499 while (raw_bytes
< raw_bytes_read
)
507 /* get record type and record length */
508 if (sscanf(&buffer
[raw_bytes
], "S%1x%2x", &record_type
, &count
) != 2)
510 return ERROR_IMAGE_FORMAT_ERROR
;
514 cal_checksum
+= (u8
)count
;
516 /* skip checksum byte */
519 if (record_type
== 0)
521 /* S0 - starting record (optional) */
524 while (count
-- > 0) {
525 sscanf(&buffer
[raw_bytes
], "%2x", &iValue
);
526 cal_checksum
+= (u8
)iValue
;
530 else if (record_type
>= 1 && record_type
<= 3)
532 switch( record_type
)
535 /* S1 - 16 bit address data record */
536 sscanf(&buffer
[raw_bytes
], "%4x", &address
);
537 cal_checksum
+= (u8
)(address
>> 8);
538 cal_checksum
+= (u8
)address
;
544 /* S2 - 24 bit address data record */
545 sscanf(&buffer
[raw_bytes
], "%6x", &address
);
546 cal_checksum
+= (u8
)(address
>> 16);
547 cal_checksum
+= (u8
)(address
>> 8);
548 cal_checksum
+= (u8
)address
;
554 /* S3 - 32 bit address data record */
555 sscanf(&buffer
[raw_bytes
], "%8x", &address
);
556 cal_checksum
+= (u8
)(address
>> 24);
557 cal_checksum
+= (u8
)(address
>> 16);
558 cal_checksum
+= (u8
)(address
>> 8);
559 cal_checksum
+= (u8
)address
;
566 if (full_address
!= address
)
568 /* we encountered a nonconsecutive location, create a new section,
569 * unless the current section has zero size, in which case this specifies
570 * the current section's base address
572 if (section
[image
->num_sections
].size
!= 0)
574 image
->num_sections
++;
575 section
[image
->num_sections
].size
= 0x0;
576 section
[image
->num_sections
].flags
= 0;
577 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
579 section
[image
->num_sections
].base_address
=
580 full_address
| address
;
581 full_address
= full_address
| address
;
586 sscanf(&buffer
[raw_bytes
], "%2hhx", &mot
->buffer
[cooked_bytes
]);
587 cal_checksum
+= (u8
)mot
->buffer
[cooked_bytes
];
590 section
[image
->num_sections
].size
+= 1;
594 else if (record_type
>= 7 && record_type
<= 9)
596 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
597 image
->num_sections
++;
599 /* copy section information */
600 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
601 for (i
= 0; i
< image
->num_sections
; i
++)
603 image
->sections
[i
].private = section
[i
].private;
604 image
->sections
[i
].base_address
= section
[i
].base_address
+
605 ((image
->base_address_set
) ? image
->base_address
: 0);
606 image
->sections
[i
].size
= section
[i
].size
;
607 image
->sections
[i
].flags
= section
[i
].flags
;
616 ERROR("unhandled S19 record type: %i", record_type
);
617 return ERROR_IMAGE_FORMAT_ERROR
;
620 /* account for checksum, will always be 0xFF */
621 sscanf(&buffer
[raw_bytes
], "%2x", &checksum
);
622 cal_checksum
+= (u8
)checksum
;
625 if( cal_checksum
!= 0xFF )
627 /* checksum failed */
629 ERROR("incorrect record checksum found in S19 file");
630 return ERROR_IMAGE_CHECKSUM
;
633 /* consume new-line character(s) */
634 if ((buffer
[raw_bytes
] == '\n') || (buffer
[raw_bytes
] == '\r'))
637 if ((buffer
[raw_bytes
] == '\n') || (buffer
[raw_bytes
] == '\r'))
642 ERROR("premature end of S19 file, no end-of-file record found");
643 return ERROR_IMAGE_FORMAT_ERROR
;
646 int image_open(image_t
*image
, char *url
, char *type_string
)
648 int retval
= ERROR_OK
;
650 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
655 if (image
->type
== IMAGE_BINARY
)
657 image_binary_t
*image_binary
;
659 image_binary
= image
->type_private
= malloc(sizeof(image_binary_t
));
661 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
663 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
664 ERROR(image
->error_str
);
668 image
->num_sections
= 1;
669 image
->sections
= malloc(sizeof(image_section_t
));
670 image
->sections
[0].base_address
= 0x0;
671 image
->sections
[0].size
= image_binary
->fileio
.size
;
672 image
->sections
[0].flags
= 0;
674 if (image
->base_address_set
== 1)
675 image
->sections
[0].base_address
= image
->base_address
;
679 else if (image
->type
== IMAGE_IHEX
)
681 image_ihex_t
*image_ihex
;
683 image_ihex
= image
->type_private
= malloc(sizeof(image_ihex_t
));
685 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
687 strncpy(image
->error_str
, image_ihex
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
688 ERROR(image
->error_str
);
692 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
694 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
695 "failed buffering IHEX image, check daemon output for additional information");
696 ERROR(image
->error_str
);
697 fileio_close(&image_ihex
->fileio
);
701 else if (image
->type
== IMAGE_ELF
)
703 image_elf_t
*image_elf
;
705 image_elf
= image
->type_private
= malloc(sizeof(image_elf_t
));
707 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
709 strncpy(image
->error_str
, image_elf
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
710 ERROR(image
->error_str
);
714 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
716 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
717 "failed to read ELF headers, check daemon output for additional information");
718 ERROR(image
->error_str
);
719 fileio_close(&image_elf
->fileio
);
723 else if (image
->type
== IMAGE_MEMORY
)
725 image_memory_t
*image_memory
;
727 image
->num_sections
= 1;
728 image
->sections
= malloc(sizeof(image_section_t
));
729 image
->sections
[0].base_address
= 0x0;
730 image
->sections
[0].size
= 0xffffffff;
731 image
->sections
[0].flags
= 0;
733 image_memory
= image
->type_private
= malloc(sizeof(image_memory_t
));
735 image_memory
->target
= get_target_by_num(strtoul(url
, NULL
, 0));;
736 image_memory
->cache
= NULL
;
737 image_memory
->cache_address
= 0x0;
739 else if (image
->type
== IMAGE_SRECORD
)
741 image_mot_t
*image_mot
;
743 image_mot
= image
->type_private
= malloc(sizeof(image_mot_t
));
745 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
747 strncpy(image
->error_str
, image_mot
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
748 ERROR(image
->error_str
);
752 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
754 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
755 "failed buffering S19 image, check daemon output for additional information");
756 ERROR(image
->error_str
);
757 fileio_close(&image_mot
->fileio
);
765 int image_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
769 if (image
->type
== IMAGE_BINARY
)
771 image_binary_t
*image_binary
= image
->type_private
;
773 /* only one section in a plain binary */
775 return ERROR_INVALID_ARGUMENTS
;
777 if ((offset
> image
->sections
[0].size
) || (offset
+ size
> image
->sections
[0].size
))
778 return ERROR_INVALID_ARGUMENTS
;
781 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
783 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
787 /* return requested bytes */
788 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
790 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
794 else if (image
->type
== IMAGE_IHEX
)
796 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
798 image
->error_str
[0] = '\0';
802 else if (image
->type
== IMAGE_ELF
)
804 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
806 else if (image
->type
== IMAGE_MEMORY
)
808 image_memory_t
*image_memory
= image
->type_private
;
809 u32 address
= image
->sections
[section
].base_address
+ offset
;
813 while ((size
- *size_read
) > 0)
817 if (!image_memory
->cache
818 || (address
< image_memory
->cache_address
)
819 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
821 if (!image_memory
->cache
)
822 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
824 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
825 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
827 free(image_memory
->cache
);
828 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
830 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
833 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
835 memcpy(buffer
+ *size_read
,
836 image_memory
->cache
+ (address
- image_memory
->cache_address
),
837 (size_in_cache
> size
) ? size
: size_in_cache
840 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
841 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
844 else if (image
->type
== IMAGE_SRECORD
)
846 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
848 image
->error_str
[0] = '\0';
856 int image_close(image_t
*image
)
858 if (image
->type
== IMAGE_BINARY
)
860 image_binary_t
*image_binary
= image
->type_private
;
862 fileio_close(&image_binary
->fileio
);
864 else if (image
->type
== IMAGE_IHEX
)
866 image_ihex_t
*image_ihex
= image
->type_private
;
868 fileio_close(&image_ihex
->fileio
);
870 if (image_ihex
->buffer
)
871 free(image_ihex
->buffer
);
873 else if (image
->type
== IMAGE_ELF
)
875 image_elf_t
*image_elf
= image
->type_private
;
877 fileio_close(&image_elf
->fileio
);
879 if (image_elf
->header
)
880 free(image_elf
->header
);
882 if (image_elf
->segments
)
883 free(image_elf
->segments
);
885 else if (image
->type
== IMAGE_MEMORY
)
887 image_memory_t
*image_memory
= image
->type_private
;
889 if (image_memory
->cache
)
890 free(image_memory
->cache
);
892 else if (image
->type
== IMAGE_SRECORD
)
894 image_mot_t
*image_mot
= image
->type_private
;
896 fileio_close(&image_mot
->fileio
);
898 if (image_mot
->buffer
)
899 free(image_mot
->buffer
);
902 if (image
->type_private
)
903 free(image
->type_private
);
906 free(image
->sections
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)