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
;
136 else if (!strcmp(type_string
, "build"))
138 image
->type
= IMAGE_BUILDER
;
142 return ERROR_IMAGE_TYPE_UNKNOWN
;
147 return autodetect_image_type(image
, url
);
153 int image_ihex_buffer_complete(image_t
*image
)
155 image_ihex_t
*ihex
= image
->type_private
;
156 fileio_t
*fileio
= &ihex
->fileio
;
157 u32 full_address
= 0x0;
162 /* we can't determine the number of sections that we'll have to create ahead of time,
163 * so we locally hold them until parsing is finished */
164 image_section_t section
[IMAGE_MAX_SECTIONS
];
166 ihex
->buffer
= malloc(fileio
->size
>> 1);
168 image
->num_sections
= 0;
169 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
170 section
[image
->num_sections
].base_address
= 0x0;
171 section
[image
->num_sections
].size
= 0x0;
172 section
[image
->num_sections
].flags
= 0;
174 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
183 if (sscanf(&lpszLine
[bytes_read
], ":%2x%4x%2x", &count
, &address
, &record_type
) != 3)
185 return ERROR_IMAGE_FORMAT_ERROR
;
189 cal_checksum
+= (u8
)count
;
190 cal_checksum
+= (u8
)(address
>> 8);
191 cal_checksum
+= (u8
)address
;
192 cal_checksum
+= (u8
)record_type
;
194 if (record_type
== 0) /* Data Record */
196 if ((full_address
& 0xffff) != address
)
198 /* we encountered a nonconsecutive location, create a new section,
199 * unless the current section has zero size, in which case this specifies
200 * the current section's base address
202 if (section
[image
->num_sections
].size
!= 0)
204 image
->num_sections
++;
205 section
[image
->num_sections
].size
= 0x0;
206 section
[image
->num_sections
].flags
= 0;
207 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
209 section
[image
->num_sections
].base_address
=
210 (full_address
& 0xffff0000) | address
;
211 full_address
= (full_address
& 0xffff0000) | address
;
216 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&ihex
->buffer
[cooked_bytes
]);
217 cal_checksum
+= (u8
)ihex
->buffer
[cooked_bytes
];
220 section
[image
->num_sections
].size
+= 1;
224 else if (record_type
== 1) /* End of File Record */
226 /* finish the current section */
227 image
->num_sections
++;
229 /* copy section information */
230 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
231 for (i
= 0; i
< image
->num_sections
; i
++)
233 image
->sections
[i
].private = section
[i
].private;
234 image
->sections
[i
].base_address
= section
[i
].base_address
+
235 ((image
->base_address_set
) ? image
->base_address
: 0);
236 image
->sections
[i
].size
= section
[i
].size
;
237 image
->sections
[i
].flags
= section
[i
].flags
;
242 else if (record_type
== 2) /* Linear Address Record */
246 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
247 cal_checksum
+= (u8
)(upper_address
>> 8);
248 cal_checksum
+= (u8
)upper_address
;
251 if ((full_address
>> 4) != upper_address
)
253 /* we encountered a nonconsecutive location, create a new section,
254 * unless the current section has zero size, in which case this specifies
255 * the current section's base address
257 if (section
[image
->num_sections
].size
!= 0)
259 image
->num_sections
++;
260 section
[image
->num_sections
].size
= 0x0;
261 section
[image
->num_sections
].flags
= 0;
262 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
264 section
[image
->num_sections
].base_address
=
265 (full_address
& 0xffff) | (upper_address
<< 4);
266 full_address
= (full_address
& 0xffff) | (upper_address
<< 4);
269 else if (record_type
== 4) /* Extended Linear Address Record */
273 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
274 cal_checksum
+= (u8
)(upper_address
>> 8);
275 cal_checksum
+= (u8
)upper_address
;
278 if ((full_address
>> 16) != upper_address
)
280 /* we encountered a nonconsecutive location, create a new section,
281 * unless the current section has zero size, in which case this specifies
282 * the current section's base address
284 if (section
[image
->num_sections
].size
!= 0)
286 image
->num_sections
++;
287 section
[image
->num_sections
].size
= 0x0;
288 section
[image
->num_sections
].flags
= 0;
289 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
291 section
[image
->num_sections
].base_address
=
292 (full_address
& 0xffff) | (upper_address
<< 16);
293 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
296 else if (record_type
== 5) /* Start Linear Address Record */
300 sscanf(&lpszLine
[bytes_read
], "%8x", &start_address
);
301 cal_checksum
+= (u8
)(start_address
>> 24);
302 cal_checksum
+= (u8
)(start_address
>> 16);
303 cal_checksum
+= (u8
)(start_address
>> 8);
304 cal_checksum
+= (u8
)start_address
;
307 image
->start_address_set
= 1;
308 image
->start_address
= be_to_h_u32((u8
*)&start_address
);
312 ERROR("unhandled IHEX record type: %i", record_type
);
313 return ERROR_IMAGE_FORMAT_ERROR
;
316 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
319 if ((u8
)checksum
!= (u8
)(~cal_checksum
+ 1))
321 /* checksum failed */
322 ERROR("incorrect record checksum found in IHEX file");
323 return ERROR_IMAGE_CHECKSUM
;
327 ERROR("premature end of IHEX file, no end-of-file record found");
328 return ERROR_IMAGE_FORMAT_ERROR
;
331 int image_elf_read_headers(image_t
*image
)
333 image_elf_t
*elf
= image
->type_private
;
338 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
340 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (u8
*)elf
->header
, &read_bytes
)) != ERROR_OK
)
342 ERROR("cannot read ELF file header, read failed");
343 return ERROR_FILEIO_OPERATION_FAILED
;
345 if (read_bytes
!= sizeof(Elf32_Ehdr
))
347 ERROR("cannot read ELF file header, only partially read");
348 return ERROR_FILEIO_OPERATION_FAILED
;
351 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
)!=0)
353 ERROR("invalid ELF file, bad magic number");
354 return ERROR_IMAGE_FORMAT_ERROR
;
356 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
358 ERROR("invalid ELF file, only 32bits files are supported");
359 return ERROR_IMAGE_FORMAT_ERROR
;
363 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
364 if ((elf
->endianness
!=ELFDATA2LSB
)
365 &&(elf
->endianness
!=ELFDATA2MSB
))
367 ERROR("invalid ELF file, unknown endianess setting");
368 return ERROR_IMAGE_FORMAT_ERROR
;
371 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
372 if (elf
->segment_count
==0)
374 ERROR("invalid ELF file, no program headers");
375 return ERROR_IMAGE_FORMAT_ERROR
;
378 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
380 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (u8
*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
382 ERROR("cannot read ELF segment headers, read failed");
385 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
387 ERROR("cannot read ELF segment headers, only partially read");
388 return ERROR_FILEIO_OPERATION_FAILED
;
391 /* count useful segments (loadable), ignore BSS section */
392 image
->num_sections
= 0;
393 for (i
=0;i
<elf
->segment_count
;i
++)
394 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
395 image
->num_sections
++;
396 /* alloc and fill sections array with loadable segments */
397 image
->sections
= malloc(image
->num_sections
* sizeof(image_section_t
));
398 for (i
=0,j
=0;i
<elf
->segment_count
;i
++)
400 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
402 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_memsz
);
403 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_paddr
);
404 image
->sections
[j
].private = &elf
->segments
[i
];
405 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
410 image
->start_address_set
= 1;
411 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
416 int image_elf_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
418 image_elf_t
*elf
= image
->type_private
;
419 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
420 u32 read_size
,really_read
;
425 DEBUG("load segment %d at 0x%x (sz=0x%x)",section
,offset
,size
);
427 /* read initialized data in current segment if any */
428 if (offset
<field32(elf
,segment
->p_filesz
))
430 /* maximal size present in file for the current segment */
431 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
432 DEBUG("read elf: size = 0x%x at 0x%x",read_size
,
433 field32(elf
,segment
->p_offset
)+offset
);
434 /* read initialized area of the segment */
435 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
)+offset
)) != ERROR_OK
)
437 ERROR("cannot find ELF segment content, seek failed");
440 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
442 ERROR("cannot read ELF segment content, read failed");
448 *size_read
+= read_size
;
449 /* need more data ? */
453 /* if there is remaining zeroed area in current segment */
454 if (offset
<field32(elf
,segment
->p_memsz
))
456 /* fill zeroed part (BSS) of the segment */
457 read_size
= MIN(size
, field32(elf
,segment
->p_memsz
)-offset
);
458 DEBUG("zero fill: size = 0x%x",read_size
);
459 memset(buffer
,0,read_size
);
460 *size_read
+= read_size
;
466 int image_mot_buffer_complete(image_t
*image
)
468 image_mot_t
*mot
= image
->type_private
;
469 fileio_t
*fileio
= &mot
->fileio
;
470 u32 full_address
= 0x0;
475 /* we can't determine the number of sections that we'll have to create ahead of time,
476 * so we locally hold them until parsing is finished */
477 image_section_t section
[IMAGE_MAX_SECTIONS
];
479 mot
->buffer
= malloc(fileio
->size
>> 1);
481 image
->num_sections
= 0;
482 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
483 section
[image
->num_sections
].base_address
= 0x0;
484 section
[image
->num_sections
].size
= 0x0;
485 section
[image
->num_sections
].flags
= 0;
487 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
496 /* get record type and record length */
497 if (sscanf(&lpszLine
[bytes_read
], "S%1x%2x", &record_type
, &count
) != 2)
499 return ERROR_IMAGE_FORMAT_ERROR
;
503 cal_checksum
+= (u8
)count
;
505 /* skip checksum byte */
508 if (record_type
== 0)
510 /* S0 - starting record (optional) */
513 while (count
-- > 0) {
514 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
515 cal_checksum
+= (u8
)iValue
;
519 else if (record_type
>= 1 && record_type
<= 3)
521 switch( record_type
)
524 /* S1 - 16 bit address data record */
525 sscanf(&lpszLine
[bytes_read
], "%4x", &address
);
526 cal_checksum
+= (u8
)(address
>> 8);
527 cal_checksum
+= (u8
)address
;
533 /* S2 - 24 bit address data record */
534 sscanf(&lpszLine
[bytes_read
], "%6x", &address
);
535 cal_checksum
+= (u8
)(address
>> 16);
536 cal_checksum
+= (u8
)(address
>> 8);
537 cal_checksum
+= (u8
)address
;
543 /* S3 - 32 bit address data record */
544 sscanf(&lpszLine
[bytes_read
], "%8x", &address
);
545 cal_checksum
+= (u8
)(address
>> 24);
546 cal_checksum
+= (u8
)(address
>> 16);
547 cal_checksum
+= (u8
)(address
>> 8);
548 cal_checksum
+= (u8
)address
;
555 if (full_address
!= address
)
557 /* we encountered a nonconsecutive location, create a new section,
558 * unless the current section has zero size, in which case this specifies
559 * the current section's base address
561 if (section
[image
->num_sections
].size
!= 0)
563 image
->num_sections
++;
564 section
[image
->num_sections
].size
= 0x0;
565 section
[image
->num_sections
].flags
= 0;
566 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
568 section
[image
->num_sections
].base_address
= address
;
569 full_address
= address
;
574 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&mot
->buffer
[cooked_bytes
]);
575 cal_checksum
+= (u8
)mot
->buffer
[cooked_bytes
];
578 section
[image
->num_sections
].size
+= 1;
582 else if (record_type
>= 7 && record_type
<= 9)
584 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
585 image
->num_sections
++;
587 /* copy section information */
588 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
589 for (i
= 0; i
< image
->num_sections
; i
++)
591 image
->sections
[i
].private = section
[i
].private;
592 image
->sections
[i
].base_address
= section
[i
].base_address
+
593 ((image
->base_address_set
) ? image
->base_address
: 0);
594 image
->sections
[i
].size
= section
[i
].size
;
595 image
->sections
[i
].flags
= section
[i
].flags
;
602 ERROR("unhandled S19 record type: %i", record_type
);
603 return ERROR_IMAGE_FORMAT_ERROR
;
606 /* account for checksum, will always be 0xFF */
607 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
608 cal_checksum
+= (u8
)checksum
;
611 if( cal_checksum
!= 0xFF )
613 /* checksum failed */
614 ERROR("incorrect record checksum found in S19 file");
615 return ERROR_IMAGE_CHECKSUM
;
619 ERROR("premature end of S19 file, no end-of-file record found");
620 return ERROR_IMAGE_FORMAT_ERROR
;
623 int image_open(image_t
*image
, char *url
, char *type_string
)
625 int retval
= ERROR_OK
;
627 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
632 if (image
->type
== IMAGE_BINARY
)
634 image_binary_t
*image_binary
;
636 image_binary
= image
->type_private
= malloc(sizeof(image_binary_t
));
638 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
640 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
641 ERROR(image
->error_str
);
645 image
->num_sections
= 1;
646 image
->sections
= malloc(sizeof(image_section_t
));
647 image
->sections
[0].base_address
= 0x0;
648 image
->sections
[0].size
= image_binary
->fileio
.size
;
649 image
->sections
[0].flags
= 0;
651 if (image
->base_address_set
== 1)
652 image
->sections
[0].base_address
= image
->base_address
;
656 else if (image
->type
== IMAGE_IHEX
)
658 image_ihex_t
*image_ihex
;
660 image_ihex
= image
->type_private
= malloc(sizeof(image_ihex_t
));
662 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
664 strncpy(image
->error_str
, image_ihex
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
665 ERROR(image
->error_str
);
669 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
671 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
672 "failed buffering IHEX image, check daemon output for additional information");
673 ERROR(image
->error_str
);
674 fileio_close(&image_ihex
->fileio
);
678 else if (image
->type
== IMAGE_ELF
)
680 image_elf_t
*image_elf
;
682 image_elf
= image
->type_private
= malloc(sizeof(image_elf_t
));
684 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
686 strncpy(image
->error_str
, image_elf
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
687 ERROR(image
->error_str
);
691 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
693 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
694 "failed to read ELF headers, check daemon output for additional information");
695 ERROR(image
->error_str
);
696 fileio_close(&image_elf
->fileio
);
700 else if (image
->type
== IMAGE_MEMORY
)
702 image_memory_t
*image_memory
;
704 image
->num_sections
= 1;
705 image
->sections
= malloc(sizeof(image_section_t
));
706 image
->sections
[0].base_address
= 0x0;
707 image
->sections
[0].size
= 0xffffffff;
708 image
->sections
[0].flags
= 0;
710 image_memory
= image
->type_private
= malloc(sizeof(image_memory_t
));
712 image_memory
->target
= get_target_by_num(strtoul(url
, NULL
, 0));;
713 image_memory
->cache
= NULL
;
714 image_memory
->cache_address
= 0x0;
716 else if (image
->type
== IMAGE_SRECORD
)
718 image_mot_t
*image_mot
;
720 image_mot
= image
->type_private
= malloc(sizeof(image_mot_t
));
722 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
724 strncpy(image
->error_str
, image_mot
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
725 ERROR(image
->error_str
);
729 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
731 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
732 "failed buffering S19 image, check daemon output for additional information");
733 ERROR(image
->error_str
);
734 fileio_close(&image_mot
->fileio
);
738 else if (image
->type
== IMAGE_BUILDER
)
740 image
->num_sections
= 0;
741 image
->sections
= NULL
;
742 image
->type_private
= NULL
;
748 int image_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
752 /* don't read past the end of a section */
753 if (offset
+ size
> image
->sections
[section
].size
)
755 DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
756 offset
, size
, image
->sections
[section
].size
);
757 return ERROR_INVALID_ARGUMENTS
;
760 if (image
->type
== IMAGE_BINARY
)
762 image_binary_t
*image_binary
= image
->type_private
;
764 /* only one section in a plain binary */
766 return ERROR_INVALID_ARGUMENTS
;
769 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
771 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
775 /* return requested bytes */
776 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
778 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
782 else if (image
->type
== IMAGE_IHEX
)
784 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
786 image
->error_str
[0] = '\0';
790 else if (image
->type
== IMAGE_ELF
)
792 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
794 else if (image
->type
== IMAGE_MEMORY
)
796 image_memory_t
*image_memory
= image
->type_private
;
797 u32 address
= image
->sections
[section
].base_address
+ offset
;
801 while ((size
- *size_read
) > 0)
805 if (!image_memory
->cache
806 || (address
< image_memory
->cache_address
)
807 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
809 if (!image_memory
->cache
)
810 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
812 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
813 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
815 free(image_memory
->cache
);
816 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
818 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
821 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
823 memcpy(buffer
+ *size_read
,
824 image_memory
->cache
+ (address
- image_memory
->cache_address
),
825 (size_in_cache
> size
) ? size
: size_in_cache
828 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
829 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
832 else if (image
->type
== IMAGE_SRECORD
)
834 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
836 image
->error_str
[0] = '\0';
840 else if (image
->type
== IMAGE_BUILDER
)
842 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
844 image
->error_str
[0] = '\0';
852 int image_add_section(image_t
*image
, u32 base
, u32 size
, int flags
, u8
*data
)
854 image_section_t
*section
;
856 /* only image builder supports adding sections */
857 if (image
->type
!= IMAGE_BUILDER
)
858 return ERROR_INVALID_ARGUMENTS
;
860 /* see if there's a previous section */
861 if (image
->num_sections
)
863 section
= &image
->sections
[image
->num_sections
- 1];
865 /* see if it's enough to extend the last section,
866 * adding data to previous sections or merging is not supported */
867 if (((section
->base_address
+ section
->size
) == base
) && (section
->flags
== flags
))
869 section
->private = realloc(section
->private, section
->size
+ size
);
870 memcpy((u8
*)section
->private + section
->size
, data
, size
);
871 section
->size
+= size
;
876 /* allocate new section */
877 image
->num_sections
++;
878 image
->sections
= realloc(image
->sections
, sizeof(image_section_t
) * image
->num_sections
);
879 section
= &image
->sections
[image
->num_sections
- 1];
880 section
->base_address
= base
;
881 section
->size
= size
;
882 section
->flags
= flags
;
883 section
->private = malloc(sizeof(u8
) * size
);
884 memcpy((u8
*)section
->private, data
, size
);
889 int image_close(image_t
*image
)
891 if (image
->type
== IMAGE_BINARY
)
893 image_binary_t
*image_binary
= image
->type_private
;
895 fileio_close(&image_binary
->fileio
);
897 else if (image
->type
== IMAGE_IHEX
)
899 image_ihex_t
*image_ihex
= image
->type_private
;
901 fileio_close(&image_ihex
->fileio
);
903 if (image_ihex
->buffer
)
904 free(image_ihex
->buffer
);
906 else if (image
->type
== IMAGE_ELF
)
908 image_elf_t
*image_elf
= image
->type_private
;
910 fileio_close(&image_elf
->fileio
);
912 if (image_elf
->header
)
913 free(image_elf
->header
);
915 if (image_elf
->segments
)
916 free(image_elf
->segments
);
918 else if (image
->type
== IMAGE_MEMORY
)
920 image_memory_t
*image_memory
= image
->type_private
;
922 if (image_memory
->cache
)
923 free(image_memory
->cache
);
925 else if (image
->type
== IMAGE_SRECORD
)
927 image_mot_t
*image_mot
= image
->type_private
;
929 fileio_close(&image_mot
->fileio
);
931 if (image_mot
->buffer
)
932 free(image_mot
->buffer
);
934 else if (image
->type
== IMAGE_BUILDER
)
938 for (i
= 0; i
< image
->num_sections
; i
++)
940 free(image
->sections
[i
].private);
944 if (image
->type_private
)
945 free(image
->type_private
);
948 free(image
->sections
);
953 static u32 crc32_table
[256] = {0, 0};
955 int image_calculate_checksum(u8
* buffer
, u32 nbytes
, u32
* checksum
)
957 u32 crc
= 0xffffffff;
961 /* Initialize the CRC table and the decoding table. */
964 for (i
= 0; i
< 256; i
++)
967 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
968 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
976 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buffer
++) & 255];
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)