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 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
39 #include "replacements.h"
45 /* convert ELF header field to host endianness */
46 #define field16(elf,field)\
47 ((elf->endianness==ELFDATA2LSB)? \
48 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
50 #define field32(elf,field)\
51 ((elf->endianness==ELFDATA2LSB)? \
52 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
54 static int autodetect_image_type(image_t
*image
, char *url
)
61 /* read the first 4 bytes of image */
62 if ((retval
= fileio_open(&fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
66 retval
= fileio_read(&fileio
, 9, buffer
, &read_bytes
);
72 retval
=ERROR_FILEIO_OPERATION_FAILED
;
75 fileio_close(&fileio
);
80 /* check header against known signatures */
81 if (strncmp((char*)buffer
,ELFMAG
,SELFMAG
)==0)
83 LOG_DEBUG("ELF image detected.");
84 image
->type
= IMAGE_ELF
;
86 else if ((buffer
[0]==':') /* record start byte */
87 &&(isxdigit(buffer
[1]))
88 &&(isxdigit(buffer
[2]))
89 &&(isxdigit(buffer
[3]))
90 &&(isxdigit(buffer
[4]))
91 &&(isxdigit(buffer
[5]))
92 &&(isxdigit(buffer
[6]))
93 &&(buffer
[7]=='0') /* record type : 00 -> 05 */
94 &&(buffer
[8]>='0')&&(buffer
[8]<'6'))
96 LOG_DEBUG("IHEX image detected.");
97 image
->type
= IMAGE_IHEX
;
99 else if ((buffer
[0] == 'S') /* record start byte */
100 &&(isxdigit(buffer
[1]))
101 &&(isxdigit(buffer
[2]))
102 &&(isxdigit(buffer
[3]))
103 &&(buffer
[1] >= '0') && (buffer
[1] < '9'))
105 LOG_DEBUG("S19 image detected.");
106 image
->type
= IMAGE_SRECORD
;
110 image
->type
= IMAGE_BINARY
;
116 int identify_image_type(image_t
*image
, char *type_string
, char *url
)
120 if (!strcmp(type_string
, "bin"))
122 image
->type
= IMAGE_BINARY
;
124 else if (!strcmp(type_string
, "ihex"))
126 image
->type
= IMAGE_IHEX
;
128 else if (!strcmp(type_string
, "elf"))
130 image
->type
= IMAGE_ELF
;
132 else if (!strcmp(type_string
, "mem"))
134 image
->type
= IMAGE_MEMORY
;
136 else if (!strcmp(type_string
, "s19"))
138 image
->type
= IMAGE_SRECORD
;
140 else if (!strcmp(type_string
, "build"))
142 image
->type
= IMAGE_BUILDER
;
146 return ERROR_IMAGE_TYPE_UNKNOWN
;
151 return autodetect_image_type(image
, url
);
157 int image_ihex_buffer_complete(image_t
*image
)
159 image_ihex_t
*ihex
= image
->type_private
;
160 fileio_t
*fileio
= &ihex
->fileio
;
161 u32 full_address
= 0x0;
166 /* we can't determine the number of sections that we'll have to create ahead of time,
167 * so we locally hold them until parsing is finished */
168 image_section_t section
[IMAGE_MAX_SECTIONS
];
170 ihex
->buffer
= malloc(fileio
->size
>> 1);
172 image
->num_sections
= 0;
173 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
174 section
[image
->num_sections
].base_address
= 0x0;
175 section
[image
->num_sections
].size
= 0x0;
176 section
[image
->num_sections
].flags
= 0;
178 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
187 if (sscanf(&lpszLine
[bytes_read
], ":%2x%4x%2x", &count
, &address
, &record_type
) != 3)
189 return ERROR_IMAGE_FORMAT_ERROR
;
193 cal_checksum
+= (u8
)count
;
194 cal_checksum
+= (u8
)(address
>> 8);
195 cal_checksum
+= (u8
)address
;
196 cal_checksum
+= (u8
)record_type
;
198 if (record_type
== 0) /* Data Record */
200 if ((full_address
& 0xffff) != address
)
202 /* we encountered a nonconsecutive location, create a new section,
203 * unless the current section has zero size, in which case this specifies
204 * the current section's base address
206 if (section
[image
->num_sections
].size
!= 0)
208 image
->num_sections
++;
209 section
[image
->num_sections
].size
= 0x0;
210 section
[image
->num_sections
].flags
= 0;
211 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
213 section
[image
->num_sections
].base_address
=
214 (full_address
& 0xffff0000) | address
;
215 full_address
= (full_address
& 0xffff0000) | address
;
220 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&ihex
->buffer
[cooked_bytes
]);
221 cal_checksum
+= (u8
)ihex
->buffer
[cooked_bytes
];
224 section
[image
->num_sections
].size
+= 1;
228 else if (record_type
== 1) /* End of File Record */
230 /* finish the current section */
231 image
->num_sections
++;
233 /* copy section information */
234 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
235 for (i
= 0; i
< image
->num_sections
; i
++)
237 image
->sections
[i
].private = section
[i
].private;
238 image
->sections
[i
].base_address
= section
[i
].base_address
;
239 image
->sections
[i
].size
= section
[i
].size
;
240 image
->sections
[i
].flags
= section
[i
].flags
;
245 else if (record_type
== 2) /* Linear Address Record */
249 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
250 cal_checksum
+= (u8
)(upper_address
>> 8);
251 cal_checksum
+= (u8
)upper_address
;
254 if ((full_address
>> 4) != upper_address
)
256 /* we encountered a nonconsecutive location, create a new section,
257 * unless the current section has zero size, in which case this specifies
258 * the current section's base address
260 if (section
[image
->num_sections
].size
!= 0)
262 image
->num_sections
++;
263 section
[image
->num_sections
].size
= 0x0;
264 section
[image
->num_sections
].flags
= 0;
265 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
267 section
[image
->num_sections
].base_address
=
268 (full_address
& 0xffff) | (upper_address
<< 4);
269 full_address
= (full_address
& 0xffff) | (upper_address
<< 4);
272 else if (record_type
== 3) /* Start Segment Address Record */
276 /* "Start Segment Address Record" will not be supported */
277 /* but we must consume it, and do not create an error. */
280 sscanf(&lpszLine
[bytes_read
], "%2x", &dummy
);
281 cal_checksum
+= (u8
)dummy
;
285 else if (record_type
== 4) /* Extended Linear Address Record */
289 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
290 cal_checksum
+= (u8
)(upper_address
>> 8);
291 cal_checksum
+= (u8
)upper_address
;
294 if ((full_address
>> 16) != upper_address
)
296 /* we encountered a nonconsecutive location, create a new section,
297 * unless the current section has zero size, in which case this specifies
298 * the current section's base address
300 if (section
[image
->num_sections
].size
!= 0)
302 image
->num_sections
++;
303 section
[image
->num_sections
].size
= 0x0;
304 section
[image
->num_sections
].flags
= 0;
305 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
307 section
[image
->num_sections
].base_address
=
308 (full_address
& 0xffff) | (upper_address
<< 16);
309 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
312 else if (record_type
== 5) /* Start Linear Address Record */
316 sscanf(&lpszLine
[bytes_read
], "%8x", &start_address
);
317 cal_checksum
+= (u8
)(start_address
>> 24);
318 cal_checksum
+= (u8
)(start_address
>> 16);
319 cal_checksum
+= (u8
)(start_address
>> 8);
320 cal_checksum
+= (u8
)start_address
;
323 image
->start_address_set
= 1;
324 image
->start_address
= be_to_h_u32((u8
*)&start_address
);
328 LOG_ERROR("unhandled IHEX record type: %i", record_type
);
329 return ERROR_IMAGE_FORMAT_ERROR
;
332 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
335 if ((u8
)checksum
!= (u8
)(~cal_checksum
+ 1))
337 /* checksum failed */
338 LOG_ERROR("incorrect record checksum found in IHEX file");
339 return ERROR_IMAGE_CHECKSUM
;
343 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
344 return ERROR_IMAGE_FORMAT_ERROR
;
347 int image_elf_read_headers(image_t
*image
)
349 image_elf_t
*elf
= image
->type_private
;
354 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
356 if(elf
->header
== NULL
)
358 LOG_ERROR("insufficient memory to perform operation ");
359 return ERROR_FILEIO_OPERATION_FAILED
;
362 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (u8
*)elf
->header
, &read_bytes
)) != ERROR_OK
)
364 LOG_ERROR("cannot read ELF file header, read failed");
365 return ERROR_FILEIO_OPERATION_FAILED
;
367 if (read_bytes
!= sizeof(Elf32_Ehdr
))
369 LOG_ERROR("cannot read ELF file header, only partially read");
370 return ERROR_FILEIO_OPERATION_FAILED
;
373 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
)!=0)
375 LOG_ERROR("invalid ELF file, bad magic number");
376 return ERROR_IMAGE_FORMAT_ERROR
;
378 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
380 LOG_ERROR("invalid ELF file, only 32bits files are supported");
381 return ERROR_IMAGE_FORMAT_ERROR
;
384 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
385 if ((elf
->endianness
!=ELFDATA2LSB
)
386 &&(elf
->endianness
!=ELFDATA2MSB
))
388 LOG_ERROR("invalid ELF file, unknown endianess setting");
389 return ERROR_IMAGE_FORMAT_ERROR
;
392 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
393 if (elf
->segment_count
==0)
395 LOG_ERROR("invalid ELF file, no program headers");
396 return ERROR_IMAGE_FORMAT_ERROR
;
399 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,elf
->header
->e_phoff
))) != ERROR_OK
)
401 LOG_ERROR("cannot seek to ELF program header table, read failed");
405 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
406 if(elf
->segments
== NULL
)
408 LOG_ERROR("insufficient memory to perform operation ");
409 return ERROR_FILEIO_OPERATION_FAILED
;
412 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (u8
*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
414 LOG_ERROR("cannot read ELF segment headers, read failed");
417 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
419 LOG_ERROR("cannot read ELF segment headers, only partially read");
420 return ERROR_FILEIO_OPERATION_FAILED
;
423 /* count useful segments (loadable), ignore BSS section */
424 image
->num_sections
= 0;
425 for (i
=0;i
<elf
->segment_count
;i
++)
426 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
427 image
->num_sections
++;
428 /* alloc and fill sections array with loadable segments */
429 image
->sections
= malloc(image
->num_sections
* sizeof(image_section_t
));
430 for (i
=0,j
=0;i
<elf
->segment_count
;i
++)
432 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
434 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_filesz
);
435 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_paddr
);
436 image
->sections
[j
].private = &elf
->segments
[i
];
437 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
442 image
->start_address_set
= 1;
443 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
448 int image_elf_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
450 image_elf_t
*elf
= image
->type_private
;
451 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
452 u32 read_size
,really_read
;
457 LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section
,offset
,size
);
459 /* read initialized data in current segment if any */
460 if (offset
<field32(elf
,segment
->p_filesz
))
462 /* maximal size present in file for the current segment */
463 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
464 LOG_DEBUG("read elf: size = 0x%x at 0x%x",read_size
,
465 field32(elf
,segment
->p_offset
)+offset
);
466 /* read initialized area of the segment */
467 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
)+offset
)) != ERROR_OK
)
469 LOG_ERROR("cannot find ELF segment content, seek failed");
472 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
474 LOG_ERROR("cannot read ELF segment content, read failed");
480 *size_read
+= read_size
;
481 /* need more data ? */
489 int image_mot_buffer_complete(image_t
*image
)
491 image_mot_t
*mot
= image
->type_private
;
492 fileio_t
*fileio
= &mot
->fileio
;
493 u32 full_address
= 0x0;
498 /* we can't determine the number of sections that we'll have to create ahead of time,
499 * so we locally hold them until parsing is finished */
500 image_section_t section
[IMAGE_MAX_SECTIONS
];
502 mot
->buffer
= malloc(fileio
->size
>> 1);
504 image
->num_sections
= 0;
505 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
506 section
[image
->num_sections
].base_address
= 0x0;
507 section
[image
->num_sections
].size
= 0x0;
508 section
[image
->num_sections
].flags
= 0;
510 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
519 /* get record type and record length */
520 if (sscanf(&lpszLine
[bytes_read
], "S%1x%2x", &record_type
, &count
) != 2)
522 return ERROR_IMAGE_FORMAT_ERROR
;
526 cal_checksum
+= (u8
)count
;
528 /* skip checksum byte */
531 if (record_type
== 0)
533 /* S0 - starting record (optional) */
536 while (count
-- > 0) {
537 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
538 cal_checksum
+= (u8
)iValue
;
542 else if (record_type
>= 1 && record_type
<= 3)
544 switch( record_type
)
547 /* S1 - 16 bit address data record */
548 sscanf(&lpszLine
[bytes_read
], "%4x", &address
);
549 cal_checksum
+= (u8
)(address
>> 8);
550 cal_checksum
+= (u8
)address
;
556 /* S2 - 24 bit address data record */
557 sscanf(&lpszLine
[bytes_read
], "%6x", &address
);
558 cal_checksum
+= (u8
)(address
>> 16);
559 cal_checksum
+= (u8
)(address
>> 8);
560 cal_checksum
+= (u8
)address
;
566 /* S3 - 32 bit address data record */
567 sscanf(&lpszLine
[bytes_read
], "%8x", &address
);
568 cal_checksum
+= (u8
)(address
>> 24);
569 cal_checksum
+= (u8
)(address
>> 16);
570 cal_checksum
+= (u8
)(address
>> 8);
571 cal_checksum
+= (u8
)address
;
578 if (full_address
!= address
)
580 /* we encountered a nonconsecutive location, create a new section,
581 * unless the current section has zero size, in which case this specifies
582 * the current section's base address
584 if (section
[image
->num_sections
].size
!= 0)
586 image
->num_sections
++;
587 section
[image
->num_sections
].size
= 0x0;
588 section
[image
->num_sections
].flags
= 0;
589 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
591 section
[image
->num_sections
].base_address
= address
;
592 full_address
= address
;
597 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&mot
->buffer
[cooked_bytes
]);
598 cal_checksum
+= (u8
)mot
->buffer
[cooked_bytes
];
601 section
[image
->num_sections
].size
+= 1;
605 else if (record_type
== 5)
607 /* S5 is the data count record, we ignore it */
612 sscanf(&lpszLine
[bytes_read
], "%2x", &dummy
);
613 cal_checksum
+= (u8
)dummy
;
617 else if (record_type
>= 7 && record_type
<= 9)
619 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
620 image
->num_sections
++;
622 /* copy section information */
623 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
624 for (i
= 0; i
< image
->num_sections
; i
++)
626 image
->sections
[i
].private = section
[i
].private;
627 image
->sections
[i
].base_address
= section
[i
].base_address
;
628 image
->sections
[i
].size
= section
[i
].size
;
629 image
->sections
[i
].flags
= section
[i
].flags
;
636 LOG_ERROR("unhandled S19 record type: %i", record_type
);
637 return ERROR_IMAGE_FORMAT_ERROR
;
640 /* account for checksum, will always be 0xFF */
641 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
642 cal_checksum
+= (u8
)checksum
;
645 if( cal_checksum
!= 0xFF )
647 /* checksum failed */
648 LOG_ERROR("incorrect record checksum found in S19 file");
649 return ERROR_IMAGE_CHECKSUM
;
653 LOG_ERROR("premature end of S19 file, no end-of-file record found");
654 return ERROR_IMAGE_FORMAT_ERROR
;
657 int image_open(image_t
*image
, char *url
, char *type_string
)
659 int retval
= ERROR_OK
;
661 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
666 if (image
->type
== IMAGE_BINARY
)
668 image_binary_t
*image_binary
;
670 image_binary
= image
->type_private
= malloc(sizeof(image_binary_t
));
672 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
677 image
->num_sections
= 1;
678 image
->sections
= malloc(sizeof(image_section_t
));
679 image
->sections
[0].base_address
= 0x0;
680 image
->sections
[0].size
= image_binary
->fileio
.size
;
681 image
->sections
[0].flags
= 0;
683 else if (image
->type
== IMAGE_IHEX
)
685 image_ihex_t
*image_ihex
;
687 image_ihex
= image
->type_private
= malloc(sizeof(image_ihex_t
));
689 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
694 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
696 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
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
)
712 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
714 fileio_close(&image_elf
->fileio
);
718 else if (image
->type
== IMAGE_MEMORY
)
720 target_t
*target
= get_target_by_num(strtoul(url
, NULL
, 0));
723 LOG_ERROR("Target '%s' does not exist", url
);
727 image_memory_t
*image_memory
;
729 image
->num_sections
= 1;
730 image
->sections
= malloc(sizeof(image_section_t
));
731 image
->sections
[0].base_address
= 0x0;
732 image
->sections
[0].size
= 0xffffffff;
733 image
->sections
[0].flags
= 0;
735 image_memory
= image
->type_private
= malloc(sizeof(image_memory_t
));
737 image_memory
->target
= target
;
738 image_memory
->cache
= NULL
;
739 image_memory
->cache_address
= 0x0;
741 else if (image
->type
== IMAGE_SRECORD
)
743 image_mot_t
*image_mot
;
745 image_mot
= image
->type_private
= malloc(sizeof(image_mot_t
));
747 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
752 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
754 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
755 fileio_close(&image_mot
->fileio
);
759 else if (image
->type
== IMAGE_BUILDER
)
761 image
->num_sections
= 0;
762 image
->sections
= NULL
;
763 image
->type_private
= NULL
;
766 if (image
->base_address_set
)
770 for (section
=0; section
< image
->num_sections
; section
++)
772 image
->sections
[section
].base_address
+=image
->base_address
;
774 /* we're done relocating. The two statements below are mainly
775 * for documenation purposes: stop anyone from empirically
776 * thinking they should use these values henceforth. */
777 image
->base_address
=0;
778 image
->base_address_set
=0;
784 int image_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
788 /* don't read past the end of a section */
789 if (offset
+ size
> image
->sections
[section
].size
)
791 LOG_DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
792 offset
, size
, image
->sections
[section
].size
);
793 return ERROR_INVALID_ARGUMENTS
;
796 if (image
->type
== IMAGE_BINARY
)
798 image_binary_t
*image_binary
= image
->type_private
;
800 /* only one section in a plain binary */
802 return ERROR_INVALID_ARGUMENTS
;
805 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
810 /* return requested bytes */
811 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
816 else if (image
->type
== IMAGE_IHEX
)
818 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
823 else if (image
->type
== IMAGE_ELF
)
825 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
827 else if (image
->type
== IMAGE_MEMORY
)
829 image_memory_t
*image_memory
= image
->type_private
;
830 u32 address
= image
->sections
[section
].base_address
+ offset
;
834 while ((size
- *size_read
) > 0)
838 if (!image_memory
->cache
839 || (address
< image_memory
->cache_address
)
840 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
842 if (!image_memory
->cache
)
843 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
845 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
846 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
848 free(image_memory
->cache
);
849 image_memory
->cache
= NULL
;
850 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
852 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
855 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
857 memcpy(buffer
+ *size_read
,
858 image_memory
->cache
+ (address
- image_memory
->cache_address
),
859 (size_in_cache
> size
) ? size
: size_in_cache
862 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
863 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
866 else if (image
->type
== IMAGE_SRECORD
)
868 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
873 else if (image
->type
== IMAGE_BUILDER
)
875 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
884 int image_add_section(image_t
*image
, u32 base
, u32 size
, int flags
, u8
*data
)
886 image_section_t
*section
;
888 /* only image builder supports adding sections */
889 if (image
->type
!= IMAGE_BUILDER
)
890 return ERROR_INVALID_ARGUMENTS
;
892 /* see if there's a previous section */
893 if (image
->num_sections
)
895 section
= &image
->sections
[image
->num_sections
- 1];
897 /* see if it's enough to extend the last section,
898 * adding data to previous sections or merging is not supported */
899 if (((section
->base_address
+ section
->size
) == base
) && (section
->flags
== flags
))
901 section
->private = realloc(section
->private, section
->size
+ size
);
902 memcpy((u8
*)section
->private + section
->size
, data
, size
);
903 section
->size
+= size
;
908 /* allocate new section */
909 image
->num_sections
++;
910 image
->sections
= realloc(image
->sections
, sizeof(image_section_t
) * image
->num_sections
);
911 section
= &image
->sections
[image
->num_sections
- 1];
912 section
->base_address
= base
;
913 section
->size
= size
;
914 section
->flags
= flags
;
915 section
->private = malloc(sizeof(u8
) * size
);
916 memcpy((u8
*)section
->private, data
, size
);
921 void image_close(image_t
*image
)
923 if (image
->type
== IMAGE_BINARY
)
925 image_binary_t
*image_binary
= image
->type_private
;
927 fileio_close(&image_binary
->fileio
);
929 else if (image
->type
== IMAGE_IHEX
)
931 image_ihex_t
*image_ihex
= image
->type_private
;
933 fileio_close(&image_ihex
->fileio
);
935 if (image_ihex
->buffer
)
937 free(image_ihex
->buffer
);
938 image_ihex
->buffer
= NULL
;
941 else if (image
->type
== IMAGE_ELF
)
943 image_elf_t
*image_elf
= image
->type_private
;
945 fileio_close(&image_elf
->fileio
);
947 if (image_elf
->header
)
949 free(image_elf
->header
);
950 image_elf
->header
= NULL
;
953 if (image_elf
->segments
)
955 free(image_elf
->segments
);
956 image_elf
->segments
= NULL
;
959 else if (image
->type
== IMAGE_MEMORY
)
961 image_memory_t
*image_memory
= image
->type_private
;
963 if (image_memory
->cache
)
965 free(image_memory
->cache
);
966 image_memory
->cache
= NULL
;
969 else if (image
->type
== IMAGE_SRECORD
)
971 image_mot_t
*image_mot
= image
->type_private
;
973 fileio_close(&image_mot
->fileio
);
975 if (image_mot
->buffer
)
977 free(image_mot
->buffer
);
978 image_mot
->buffer
= NULL
;
981 else if (image
->type
== IMAGE_BUILDER
)
985 for (i
= 0; i
< image
->num_sections
; i
++)
987 free(image
->sections
[i
].private);
988 image
->sections
[i
].private = NULL
;
992 if (image
->type_private
)
994 free(image
->type_private
);
995 image
->type_private
= NULL
;
1000 free(image
->sections
);
1001 image
->sections
= NULL
;
1005 int image_calculate_checksum(u8
* buffer
, u32 nbytes
, u32
* checksum
)
1007 u32 crc
= 0xffffffff;
1008 LOG_DEBUG("Calculating checksum");
1010 u32 crc32_table
[256];
1012 /* Initialize the CRC table and the decoding table. */
1015 for (i
= 0; i
< 256; i
++)
1018 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1019 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1034 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buffer
++) & 255];
1039 LOG_DEBUG("Calculating checksum done");
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)