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
)
60 retval
= fileio_read(&fileio
, 9, buffer
, &read_bytes
);
66 retval
=ERROR_FILEIO_OPERATION_FAILED
;
69 fileio_close(&fileio
);
74 /* check header against known signatures */
75 if (strncmp((char*)buffer
,ELFMAG
,SELFMAG
)==0)
77 LOG_DEBUG("ELF image detected.");
78 image
->type
= IMAGE_ELF
;
80 else if ((buffer
[0]==':') /* record start byte */
81 &&(isxdigit(buffer
[1]))
82 &&(isxdigit(buffer
[2]))
83 &&(isxdigit(buffer
[3]))
84 &&(isxdigit(buffer
[4]))
85 &&(isxdigit(buffer
[5]))
86 &&(isxdigit(buffer
[6]))
87 &&(buffer
[7]=='0') /* record type : 00 -> 05 */
88 &&(buffer
[8]>='0')&&(buffer
[8]<'6'))
90 LOG_DEBUG("IHEX image detected.");
91 image
->type
= IMAGE_IHEX
;
93 else if ((buffer
[0] == 'S') /* record start byte */
94 &&(isxdigit(buffer
[1]))
95 &&(isxdigit(buffer
[2]))
96 &&(isxdigit(buffer
[3]))
97 &&(buffer
[1] >= '0') && (buffer
[1] < '9'))
99 LOG_DEBUG("S19 image detected.");
100 image
->type
= IMAGE_SRECORD
;
104 image
->type
= IMAGE_BINARY
;
110 int identify_image_type(image_t
*image
, char *type_string
, char *url
)
114 if (!strcmp(type_string
, "bin"))
116 image
->type
= IMAGE_BINARY
;
118 else if (!strcmp(type_string
, "ihex"))
120 image
->type
= IMAGE_IHEX
;
122 else if (!strcmp(type_string
, "elf"))
124 image
->type
= IMAGE_ELF
;
126 else if (!strcmp(type_string
, "mem"))
128 image
->type
= IMAGE_MEMORY
;
130 else if (!strcmp(type_string
, "s19"))
132 image
->type
= IMAGE_SRECORD
;
134 else if (!strcmp(type_string
, "build"))
136 image
->type
= IMAGE_BUILDER
;
140 return ERROR_IMAGE_TYPE_UNKNOWN
;
145 return autodetect_image_type(image
, url
);
151 int image_ihex_buffer_complete(image_t
*image
)
153 image_ihex_t
*ihex
= image
->type_private
;
154 fileio_t
*fileio
= &ihex
->fileio
;
155 u32 full_address
= 0x0;
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 ihex
->buffer
= malloc(fileio
->size
>> 1);
166 image
->num_sections
= 0;
167 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
168 section
[image
->num_sections
].base_address
= 0x0;
169 section
[image
->num_sections
].size
= 0x0;
170 section
[image
->num_sections
].flags
= 0;
172 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
181 if (sscanf(&lpszLine
[bytes_read
], ":%2x%4x%2x", &count
, &address
, &record_type
) != 3)
183 return ERROR_IMAGE_FORMAT_ERROR
;
187 cal_checksum
+= (u8
)count
;
188 cal_checksum
+= (u8
)(address
>> 8);
189 cal_checksum
+= (u8
)address
;
190 cal_checksum
+= (u8
)record_type
;
192 if (record_type
== 0) /* Data Record */
194 if ((full_address
& 0xffff) != address
)
196 /* we encountered a nonconsecutive location, create a new section,
197 * unless the current section has zero size, in which case this specifies
198 * the current section's base address
200 if (section
[image
->num_sections
].size
!= 0)
202 image
->num_sections
++;
203 section
[image
->num_sections
].size
= 0x0;
204 section
[image
->num_sections
].flags
= 0;
205 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
207 section
[image
->num_sections
].base_address
=
208 (full_address
& 0xffff0000) | address
;
209 full_address
= (full_address
& 0xffff0000) | address
;
214 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&ihex
->buffer
[cooked_bytes
]);
215 cal_checksum
+= (u8
)ihex
->buffer
[cooked_bytes
];
218 section
[image
->num_sections
].size
+= 1;
222 else if (record_type
== 1) /* End of File Record */
224 /* finish the current section */
225 image
->num_sections
++;
227 /* copy section information */
228 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
229 for (i
= 0; i
< image
->num_sections
; i
++)
231 image
->sections
[i
].private = section
[i
].private;
232 image
->sections
[i
].base_address
= section
[i
].base_address
;
233 image
->sections
[i
].size
= section
[i
].size
;
234 image
->sections
[i
].flags
= section
[i
].flags
;
239 else if (record_type
== 2) /* Linear Address Record */
243 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
244 cal_checksum
+= (u8
)(upper_address
>> 8);
245 cal_checksum
+= (u8
)upper_address
;
248 if ((full_address
>> 4) != upper_address
)
250 /* we encountered a nonconsecutive location, create a new section,
251 * unless the current section has zero size, in which case this specifies
252 * the current section's base address
254 if (section
[image
->num_sections
].size
!= 0)
256 image
->num_sections
++;
257 section
[image
->num_sections
].size
= 0x0;
258 section
[image
->num_sections
].flags
= 0;
259 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
261 section
[image
->num_sections
].base_address
=
262 (full_address
& 0xffff) | (upper_address
<< 4);
263 full_address
= (full_address
& 0xffff) | (upper_address
<< 4);
266 else if (record_type
== 3) /* Start Segment Address Record */
270 /* "Start Segment Address Record" will not be supported */
271 /* but we must consume it, and do not create an error. */
274 sscanf(&lpszLine
[bytes_read
], "%2x", &dummy
);
275 cal_checksum
+= (u8
)dummy
;
279 else if (record_type
== 4) /* Extended Linear Address Record */
283 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
284 cal_checksum
+= (u8
)(upper_address
>> 8);
285 cal_checksum
+= (u8
)upper_address
;
288 if ((full_address
>> 16) != upper_address
)
290 /* we encountered a nonconsecutive location, create a new section,
291 * unless the current section has zero size, in which case this specifies
292 * the current section's base address
294 if (section
[image
->num_sections
].size
!= 0)
296 image
->num_sections
++;
297 section
[image
->num_sections
].size
= 0x0;
298 section
[image
->num_sections
].flags
= 0;
299 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
301 section
[image
->num_sections
].base_address
=
302 (full_address
& 0xffff) | (upper_address
<< 16);
303 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
306 else if (record_type
== 5) /* Start Linear Address Record */
310 sscanf(&lpszLine
[bytes_read
], "%8x", &start_address
);
311 cal_checksum
+= (u8
)(start_address
>> 24);
312 cal_checksum
+= (u8
)(start_address
>> 16);
313 cal_checksum
+= (u8
)(start_address
>> 8);
314 cal_checksum
+= (u8
)start_address
;
317 image
->start_address_set
= 1;
318 image
->start_address
= be_to_h_u32((u8
*)&start_address
);
322 LOG_ERROR("unhandled IHEX record type: %i", record_type
);
323 return ERROR_IMAGE_FORMAT_ERROR
;
326 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
329 if ((u8
)checksum
!= (u8
)(~cal_checksum
+ 1))
331 /* checksum failed */
332 LOG_ERROR("incorrect record checksum found in IHEX file");
333 return ERROR_IMAGE_CHECKSUM
;
337 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
338 return ERROR_IMAGE_FORMAT_ERROR
;
341 int image_elf_read_headers(image_t
*image
)
343 image_elf_t
*elf
= image
->type_private
;
348 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
350 if(elf
->header
== NULL
)
352 LOG_ERROR("insufficient memory to perform operation ");
353 return ERROR_FILEIO_OPERATION_FAILED
;
356 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (u8
*)elf
->header
, &read_bytes
)) != ERROR_OK
)
358 LOG_ERROR("cannot read ELF file header, read failed");
359 return ERROR_FILEIO_OPERATION_FAILED
;
361 if (read_bytes
!= sizeof(Elf32_Ehdr
))
363 LOG_ERROR("cannot read ELF file header, only partially read");
364 return ERROR_FILEIO_OPERATION_FAILED
;
367 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
)!=0)
369 LOG_ERROR("invalid ELF file, bad magic number");
370 return ERROR_IMAGE_FORMAT_ERROR
;
372 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
374 LOG_ERROR("invalid ELF file, only 32bits files are supported");
375 return ERROR_IMAGE_FORMAT_ERROR
;
379 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
380 if ((elf
->endianness
!=ELFDATA2LSB
)
381 &&(elf
->endianness
!=ELFDATA2MSB
))
383 LOG_ERROR("invalid ELF file, unknown endianess setting");
384 return ERROR_IMAGE_FORMAT_ERROR
;
387 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
388 if (elf
->segment_count
==0)
390 LOG_ERROR("invalid ELF file, no program headers");
391 return ERROR_IMAGE_FORMAT_ERROR
;
394 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,elf
->header
->e_phoff
))) != ERROR_OK
)
396 LOG_ERROR("cannot seek to ELF program header table, read failed");
400 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
401 if(elf
->segments
== NULL
)
403 LOG_ERROR("insufficient memory to perform operation ");
404 return ERROR_FILEIO_OPERATION_FAILED
;
407 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (u8
*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
409 LOG_ERROR("cannot read ELF segment headers, read failed");
412 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
414 LOG_ERROR("cannot read ELF segment headers, only partially read");
415 return ERROR_FILEIO_OPERATION_FAILED
;
418 /* count useful segments (loadable), ignore BSS section */
419 image
->num_sections
= 0;
420 for (i
=0;i
<elf
->segment_count
;i
++)
421 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
422 image
->num_sections
++;
423 /* alloc and fill sections array with loadable segments */
424 image
->sections
= malloc(image
->num_sections
* sizeof(image_section_t
));
425 for (i
=0,j
=0;i
<elf
->segment_count
;i
++)
427 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
429 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_filesz
);
430 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_paddr
);
431 image
->sections
[j
].private = &elf
->segments
[i
];
432 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
437 image
->start_address_set
= 1;
438 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
443 int image_elf_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
445 image_elf_t
*elf
= image
->type_private
;
446 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
447 u32 read_size
,really_read
;
452 LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section
,offset
,size
);
454 /* read initialized data in current segment if any */
455 if (offset
<field32(elf
,segment
->p_filesz
))
457 /* maximal size present in file for the current segment */
458 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
459 LOG_DEBUG("read elf: size = 0x%x at 0x%x",read_size
,
460 field32(elf
,segment
->p_offset
)+offset
);
461 /* read initialized area of the segment */
462 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
)+offset
)) != ERROR_OK
)
464 LOG_ERROR("cannot find ELF segment content, seek failed");
467 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
469 LOG_ERROR("cannot read ELF segment content, read failed");
475 *size_read
+= read_size
;
476 /* need more data ? */
484 int image_mot_buffer_complete(image_t
*image
)
486 image_mot_t
*mot
= image
->type_private
;
487 fileio_t
*fileio
= &mot
->fileio
;
488 u32 full_address
= 0x0;
493 /* we can't determine the number of sections that we'll have to create ahead of time,
494 * so we locally hold them until parsing is finished */
495 image_section_t section
[IMAGE_MAX_SECTIONS
];
497 mot
->buffer
= malloc(fileio
->size
>> 1);
499 image
->num_sections
= 0;
500 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
501 section
[image
->num_sections
].base_address
= 0x0;
502 section
[image
->num_sections
].size
= 0x0;
503 section
[image
->num_sections
].flags
= 0;
505 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
514 /* get record type and record length */
515 if (sscanf(&lpszLine
[bytes_read
], "S%1x%2x", &record_type
, &count
) != 2)
517 return ERROR_IMAGE_FORMAT_ERROR
;
521 cal_checksum
+= (u8
)count
;
523 /* skip checksum byte */
526 if (record_type
== 0)
528 /* S0 - starting record (optional) */
531 while (count
-- > 0) {
532 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
533 cal_checksum
+= (u8
)iValue
;
537 else if (record_type
>= 1 && record_type
<= 3)
539 switch( record_type
)
542 /* S1 - 16 bit address data record */
543 sscanf(&lpszLine
[bytes_read
], "%4x", &address
);
544 cal_checksum
+= (u8
)(address
>> 8);
545 cal_checksum
+= (u8
)address
;
551 /* S2 - 24 bit address data record */
552 sscanf(&lpszLine
[bytes_read
], "%6x", &address
);
553 cal_checksum
+= (u8
)(address
>> 16);
554 cal_checksum
+= (u8
)(address
>> 8);
555 cal_checksum
+= (u8
)address
;
561 /* S3 - 32 bit address data record */
562 sscanf(&lpszLine
[bytes_read
], "%8x", &address
);
563 cal_checksum
+= (u8
)(address
>> 24);
564 cal_checksum
+= (u8
)(address
>> 16);
565 cal_checksum
+= (u8
)(address
>> 8);
566 cal_checksum
+= (u8
)address
;
573 if (full_address
!= address
)
575 /* we encountered a nonconsecutive location, create a new section,
576 * unless the current section has zero size, in which case this specifies
577 * the current section's base address
579 if (section
[image
->num_sections
].size
!= 0)
581 image
->num_sections
++;
582 section
[image
->num_sections
].size
= 0x0;
583 section
[image
->num_sections
].flags
= 0;
584 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
586 section
[image
->num_sections
].base_address
= address
;
587 full_address
= address
;
592 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&mot
->buffer
[cooked_bytes
]);
593 cal_checksum
+= (u8
)mot
->buffer
[cooked_bytes
];
596 section
[image
->num_sections
].size
+= 1;
600 else if (record_type
== 5)
602 /* S5 is the data count record, we ignore it */
607 sscanf(&lpszLine
[bytes_read
], "%2x", &dummy
);
608 cal_checksum
+= (u8
)dummy
;
612 else if (record_type
>= 7 && record_type
<= 9)
614 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
615 image
->num_sections
++;
617 /* copy section information */
618 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
619 for (i
= 0; i
< image
->num_sections
; i
++)
621 image
->sections
[i
].private = section
[i
].private;
622 image
->sections
[i
].base_address
= section
[i
].base_address
;
623 image
->sections
[i
].size
= section
[i
].size
;
624 image
->sections
[i
].flags
= section
[i
].flags
;
631 LOG_ERROR("unhandled S19 record type: %i", record_type
);
632 return ERROR_IMAGE_FORMAT_ERROR
;
635 /* account for checksum, will always be 0xFF */
636 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
637 cal_checksum
+= (u8
)checksum
;
640 if( cal_checksum
!= 0xFF )
642 /* checksum failed */
643 LOG_ERROR("incorrect record checksum found in S19 file");
644 return ERROR_IMAGE_CHECKSUM
;
648 LOG_ERROR("premature end of S19 file, no end-of-file record found");
649 return ERROR_IMAGE_FORMAT_ERROR
;
652 int image_open(image_t
*image
, char *url
, char *type_string
)
654 int retval
= ERROR_OK
;
656 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
661 if (image
->type
== IMAGE_BINARY
)
663 image_binary_t
*image_binary
;
665 image_binary
= image
->type_private
= malloc(sizeof(image_binary_t
));
667 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
672 image
->num_sections
= 1;
673 image
->sections
= malloc(sizeof(image_section_t
));
674 image
->sections
[0].base_address
= 0x0;
675 image
->sections
[0].size
= image_binary
->fileio
.size
;
676 image
->sections
[0].flags
= 0;
678 else if (image
->type
== IMAGE_IHEX
)
680 image_ihex_t
*image_ihex
;
682 image_ihex
= image
->type_private
= malloc(sizeof(image_ihex_t
));
684 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
689 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
691 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
692 fileio_close(&image_ihex
->fileio
);
696 else if (image
->type
== IMAGE_ELF
)
698 image_elf_t
*image_elf
;
700 image_elf
= image
->type_private
= malloc(sizeof(image_elf_t
));
702 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
707 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
709 fileio_close(&image_elf
->fileio
);
713 else if (image
->type
== IMAGE_MEMORY
)
715 image_memory_t
*image_memory
;
717 image
->num_sections
= 1;
718 image
->sections
= malloc(sizeof(image_section_t
));
719 image
->sections
[0].base_address
= 0x0;
720 image
->sections
[0].size
= 0xffffffff;
721 image
->sections
[0].flags
= 0;
723 image_memory
= image
->type_private
= malloc(sizeof(image_memory_t
));
725 image_memory
->target
= get_target_by_num(strtoul(url
, NULL
, 0));;
726 image_memory
->cache
= NULL
;
727 image_memory
->cache_address
= 0x0;
729 else if (image
->type
== IMAGE_SRECORD
)
731 image_mot_t
*image_mot
;
733 image_mot
= image
->type_private
= malloc(sizeof(image_mot_t
));
735 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
740 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
742 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
743 fileio_close(&image_mot
->fileio
);
747 else if (image
->type
== IMAGE_BUILDER
)
749 image
->num_sections
= 0;
750 image
->sections
= NULL
;
751 image
->type_private
= NULL
;
754 if (image
->base_address_set
)
758 for (section
=0; section
< image
->num_sections
; section
++)
760 image
->sections
[section
].base_address
+=image
->base_address
;
762 /* we're done relocating. The two statements below are mainly
763 * for documenation purposes: stop anyone from empirically
764 * thinking they should use these values henceforth. */
765 image
->base_address
=0;
766 image
->base_address_set
=0;
772 int image_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
776 /* don't read past the end of a section */
777 if (offset
+ size
> image
->sections
[section
].size
)
779 LOG_DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
780 offset
, size
, image
->sections
[section
].size
);
781 return ERROR_INVALID_ARGUMENTS
;
784 if (image
->type
== IMAGE_BINARY
)
786 image_binary_t
*image_binary
= image
->type_private
;
788 /* only one section in a plain binary */
790 return ERROR_INVALID_ARGUMENTS
;
793 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
798 /* return requested bytes */
799 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
804 else if (image
->type
== IMAGE_IHEX
)
806 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
811 else if (image
->type
== IMAGE_ELF
)
813 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
815 else if (image
->type
== IMAGE_MEMORY
)
817 image_memory_t
*image_memory
= image
->type_private
;
818 u32 address
= image
->sections
[section
].base_address
+ offset
;
822 while ((size
- *size_read
) > 0)
826 if (!image_memory
->cache
827 || (address
< image_memory
->cache_address
)
828 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
830 if (!image_memory
->cache
)
831 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
833 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
834 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
836 free(image_memory
->cache
);
837 image_memory
->cache
= NULL
;
838 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
840 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
843 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
845 memcpy(buffer
+ *size_read
,
846 image_memory
->cache
+ (address
- image_memory
->cache_address
),
847 (size_in_cache
> size
) ? size
: size_in_cache
850 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
851 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
854 else if (image
->type
== IMAGE_SRECORD
)
856 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
861 else if (image
->type
== IMAGE_BUILDER
)
863 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
872 int image_add_section(image_t
*image
, u32 base
, u32 size
, int flags
, u8
*data
)
874 image_section_t
*section
;
876 /* only image builder supports adding sections */
877 if (image
->type
!= IMAGE_BUILDER
)
878 return ERROR_INVALID_ARGUMENTS
;
880 /* see if there's a previous section */
881 if (image
->num_sections
)
883 section
= &image
->sections
[image
->num_sections
- 1];
885 /* see if it's enough to extend the last section,
886 * adding data to previous sections or merging is not supported */
887 if (((section
->base_address
+ section
->size
) == base
) && (section
->flags
== flags
))
889 section
->private = realloc(section
->private, section
->size
+ size
);
890 memcpy((u8
*)section
->private + section
->size
, data
, size
);
891 section
->size
+= size
;
896 /* allocate new section */
897 image
->num_sections
++;
898 image
->sections
= realloc(image
->sections
, sizeof(image_section_t
) * image
->num_sections
);
899 section
= &image
->sections
[image
->num_sections
- 1];
900 section
->base_address
= base
;
901 section
->size
= size
;
902 section
->flags
= flags
;
903 section
->private = malloc(sizeof(u8
) * size
);
904 memcpy((u8
*)section
->private, data
, size
);
909 int image_close(image_t
*image
)
911 if (image
->type
== IMAGE_BINARY
)
913 image_binary_t
*image_binary
= image
->type_private
;
915 fileio_close(&image_binary
->fileio
);
917 else if (image
->type
== IMAGE_IHEX
)
919 image_ihex_t
*image_ihex
= image
->type_private
;
921 fileio_close(&image_ihex
->fileio
);
923 if (image_ihex
->buffer
)
925 free(image_ihex
->buffer
);
926 image_ihex
->buffer
= NULL
;
929 else if (image
->type
== IMAGE_ELF
)
931 image_elf_t
*image_elf
= image
->type_private
;
933 fileio_close(&image_elf
->fileio
);
935 if (image_elf
->header
)
937 free(image_elf
->header
);
938 image_elf
->header
= NULL
;
941 if (image_elf
->segments
)
943 free(image_elf
->segments
);
944 image_elf
->segments
= NULL
;
947 else if (image
->type
== IMAGE_MEMORY
)
949 image_memory_t
*image_memory
= image
->type_private
;
951 if (image_memory
->cache
)
953 free(image_memory
->cache
);
954 image_memory
->cache
= NULL
;
957 else if (image
->type
== IMAGE_SRECORD
)
959 image_mot_t
*image_mot
= image
->type_private
;
961 fileio_close(&image_mot
->fileio
);
963 if (image_mot
->buffer
)
965 free(image_mot
->buffer
);
966 image_mot
->buffer
= NULL
;
969 else if (image
->type
== IMAGE_BUILDER
)
973 for (i
= 0; i
< image
->num_sections
; i
++)
975 free(image
->sections
[i
].private);
976 image
->sections
[i
].private = NULL
;
980 if (image
->type_private
)
982 free(image
->type_private
);
983 image
->type_private
= NULL
;
988 free(image
->sections
);
989 image
->sections
= NULL
;
995 static u32 crc32_table
[256] = {0, 0};
997 int image_calculate_checksum(u8
* buffer
, u32 nbytes
, u32
* checksum
)
999 u32 crc
= 0xffffffff;
1001 if (!crc32_table
[1])
1003 /* Initialize the CRC table and the decoding table. */
1006 for (i
= 0; i
< 256; i
++)
1009 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1010 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1018 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)