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 if ((retval
= fileio_read(&fileio
, 9, buffer
, &read_bytes
)) != ERROR_OK
)
62 return ERROR_FILEIO_OPERATION_FAILED
;
66 return ERROR_FILEIO_OPERATION_FAILED
;
68 fileio_close(&fileio
);
70 /* check header against known signatures */
71 if (strncmp((char*)buffer
,ELFMAG
,SELFMAG
)==0)
73 LOG_DEBUG("ELF image detected.");
74 image
->type
= IMAGE_ELF
;
76 else if ((buffer
[0]==':') /* record start byte */
77 &&(isxdigit(buffer
[1]))
78 &&(isxdigit(buffer
[2]))
79 &&(isxdigit(buffer
[3]))
80 &&(isxdigit(buffer
[4]))
81 &&(isxdigit(buffer
[5]))
82 &&(isxdigit(buffer
[6]))
83 &&(buffer
[7]=='0') /* record type : 00 -> 05 */
84 &&(buffer
[8]>='0')&&(buffer
[8]<'6'))
86 LOG_DEBUG("IHEX image detected.");
87 image
->type
= IMAGE_IHEX
;
89 else if ((buffer
[0] == 'S') /* record start byte */
90 &&(isxdigit(buffer
[1]))
91 &&(isxdigit(buffer
[2]))
92 &&(isxdigit(buffer
[3]))
93 &&(buffer
[1] >= '0') && (buffer
[1] < '9'))
95 LOG_DEBUG("S19 image detected.");
96 image
->type
= IMAGE_SRECORD
;
100 image
->type
= IMAGE_BINARY
;
106 int identify_image_type(image_t
*image
, char *type_string
, char *url
)
110 if (!strcmp(type_string
, "bin"))
112 image
->type
= IMAGE_BINARY
;
114 else if (!strcmp(type_string
, "ihex"))
116 image
->type
= IMAGE_IHEX
;
118 else if (!strcmp(type_string
, "elf"))
120 image
->type
= IMAGE_ELF
;
122 else if (!strcmp(type_string
, "mem"))
124 image
->type
= IMAGE_MEMORY
;
126 else if (!strcmp(type_string
, "s19"))
128 image
->type
= IMAGE_SRECORD
;
130 else if (!strcmp(type_string
, "build"))
132 image
->type
= IMAGE_BUILDER
;
136 return ERROR_IMAGE_TYPE_UNKNOWN
;
141 return autodetect_image_type(image
, url
);
147 int image_ihex_buffer_complete(image_t
*image
)
149 image_ihex_t
*ihex
= image
->type_private
;
150 fileio_t
*fileio
= &ihex
->fileio
;
151 u32 full_address
= 0x0;
156 /* we can't determine the number of sections that we'll have to create ahead of time,
157 * so we locally hold them until parsing is finished */
158 image_section_t section
[IMAGE_MAX_SECTIONS
];
160 ihex
->buffer
= malloc(fileio
->size
>> 1);
162 image
->num_sections
= 0;
163 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
164 section
[image
->num_sections
].base_address
= 0x0;
165 section
[image
->num_sections
].size
= 0x0;
166 section
[image
->num_sections
].flags
= 0;
168 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
177 if (sscanf(&lpszLine
[bytes_read
], ":%2x%4x%2x", &count
, &address
, &record_type
) != 3)
179 return ERROR_IMAGE_FORMAT_ERROR
;
183 cal_checksum
+= (u8
)count
;
184 cal_checksum
+= (u8
)(address
>> 8);
185 cal_checksum
+= (u8
)address
;
186 cal_checksum
+= (u8
)record_type
;
188 if (record_type
== 0) /* Data Record */
190 if ((full_address
& 0xffff) != address
)
192 /* we encountered a nonconsecutive location, create a new section,
193 * unless the current section has zero size, in which case this specifies
194 * the current section's base address
196 if (section
[image
->num_sections
].size
!= 0)
198 image
->num_sections
++;
199 section
[image
->num_sections
].size
= 0x0;
200 section
[image
->num_sections
].flags
= 0;
201 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
203 section
[image
->num_sections
].base_address
=
204 (full_address
& 0xffff0000) | address
;
205 full_address
= (full_address
& 0xffff0000) | address
;
210 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&ihex
->buffer
[cooked_bytes
]);
211 cal_checksum
+= (u8
)ihex
->buffer
[cooked_bytes
];
214 section
[image
->num_sections
].size
+= 1;
218 else if (record_type
== 1) /* End of File Record */
220 /* finish the current section */
221 image
->num_sections
++;
223 /* copy section information */
224 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
225 for (i
= 0; i
< image
->num_sections
; i
++)
227 image
->sections
[i
].private = section
[i
].private;
228 image
->sections
[i
].base_address
= section
[i
].base_address
;
229 image
->sections
[i
].size
= section
[i
].size
;
230 image
->sections
[i
].flags
= section
[i
].flags
;
235 else if (record_type
== 2) /* Linear Address Record */
239 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
240 cal_checksum
+= (u8
)(upper_address
>> 8);
241 cal_checksum
+= (u8
)upper_address
;
244 if ((full_address
>> 4) != upper_address
)
246 /* we encountered a nonconsecutive location, create a new section,
247 * unless the current section has zero size, in which case this specifies
248 * the current section's base address
250 if (section
[image
->num_sections
].size
!= 0)
252 image
->num_sections
++;
253 section
[image
->num_sections
].size
= 0x0;
254 section
[image
->num_sections
].flags
= 0;
255 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
257 section
[image
->num_sections
].base_address
=
258 (full_address
& 0xffff) | (upper_address
<< 4);
259 full_address
= (full_address
& 0xffff) | (upper_address
<< 4);
262 else if (record_type
== 3) /* Start Segment Address Record */
266 /* "Start Segment Address Record" will not be supported */
267 /* but we must consume it, and do not create an error. */
270 sscanf(&lpszLine
[bytes_read
], "%2x", &dummy
);
271 cal_checksum
+= (u8
)dummy
;
275 else if (record_type
== 4) /* Extended Linear Address Record */
279 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
280 cal_checksum
+= (u8
)(upper_address
>> 8);
281 cal_checksum
+= (u8
)upper_address
;
284 if ((full_address
>> 16) != upper_address
)
286 /* we encountered a nonconsecutive location, create a new section,
287 * unless the current section has zero size, in which case this specifies
288 * the current section's base address
290 if (section
[image
->num_sections
].size
!= 0)
292 image
->num_sections
++;
293 section
[image
->num_sections
].size
= 0x0;
294 section
[image
->num_sections
].flags
= 0;
295 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
297 section
[image
->num_sections
].base_address
=
298 (full_address
& 0xffff) | (upper_address
<< 16);
299 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
302 else if (record_type
== 5) /* Start Linear Address Record */
306 sscanf(&lpszLine
[bytes_read
], "%8x", &start_address
);
307 cal_checksum
+= (u8
)(start_address
>> 24);
308 cal_checksum
+= (u8
)(start_address
>> 16);
309 cal_checksum
+= (u8
)(start_address
>> 8);
310 cal_checksum
+= (u8
)start_address
;
313 image
->start_address_set
= 1;
314 image
->start_address
= be_to_h_u32((u8
*)&start_address
);
318 LOG_ERROR("unhandled IHEX record type: %i", record_type
);
319 return ERROR_IMAGE_FORMAT_ERROR
;
322 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
325 if ((u8
)checksum
!= (u8
)(~cal_checksum
+ 1))
327 /* checksum failed */
328 LOG_ERROR("incorrect record checksum found in IHEX file");
329 return ERROR_IMAGE_CHECKSUM
;
333 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
334 return ERROR_IMAGE_FORMAT_ERROR
;
337 int image_elf_read_headers(image_t
*image
)
339 image_elf_t
*elf
= image
->type_private
;
344 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
346 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (u8
*)elf
->header
, &read_bytes
)) != ERROR_OK
)
348 LOG_ERROR("cannot read ELF file header, read failed");
349 return ERROR_FILEIO_OPERATION_FAILED
;
351 if (read_bytes
!= sizeof(Elf32_Ehdr
))
353 LOG_ERROR("cannot read ELF file header, only partially read");
354 return ERROR_FILEIO_OPERATION_FAILED
;
357 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
)!=0)
359 LOG_ERROR("invalid ELF file, bad magic number");
360 return ERROR_IMAGE_FORMAT_ERROR
;
362 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
364 LOG_ERROR("invalid ELF file, only 32bits files are supported");
365 return ERROR_IMAGE_FORMAT_ERROR
;
369 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
370 if ((elf
->endianness
!=ELFDATA2LSB
)
371 &&(elf
->endianness
!=ELFDATA2MSB
))
373 LOG_ERROR("invalid ELF file, unknown endianess setting");
374 return ERROR_IMAGE_FORMAT_ERROR
;
377 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
378 if (elf
->segment_count
==0)
380 LOG_ERROR("invalid ELF file, no program headers");
381 return ERROR_IMAGE_FORMAT_ERROR
;
384 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,elf
->header
->e_phoff
))) != ERROR_OK
)
386 LOG_ERROR("cannot seek to ELF program header table, read failed");
390 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
392 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (u8
*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
394 LOG_ERROR("cannot read ELF segment headers, read failed");
397 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
399 LOG_ERROR("cannot read ELF segment headers, only partially read");
400 return ERROR_FILEIO_OPERATION_FAILED
;
403 /* count useful segments (loadable), ignore BSS section */
404 image
->num_sections
= 0;
405 for (i
=0;i
<elf
->segment_count
;i
++)
406 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
407 image
->num_sections
++;
408 /* alloc and fill sections array with loadable segments */
409 image
->sections
= malloc(image
->num_sections
* sizeof(image_section_t
));
410 for (i
=0,j
=0;i
<elf
->segment_count
;i
++)
412 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
414 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_filesz
);
415 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_paddr
);
416 image
->sections
[j
].private = &elf
->segments
[i
];
417 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
422 image
->start_address_set
= 1;
423 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
428 int image_elf_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
430 image_elf_t
*elf
= image
->type_private
;
431 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
432 u32 read_size
,really_read
;
437 LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section
,offset
,size
);
439 /* read initialized data in current segment if any */
440 if (offset
<field32(elf
,segment
->p_filesz
))
442 /* maximal size present in file for the current segment */
443 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
444 LOG_DEBUG("read elf: size = 0x%x at 0x%x",read_size
,
445 field32(elf
,segment
->p_offset
)+offset
);
446 /* read initialized area of the segment */
447 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
)+offset
)) != ERROR_OK
)
449 LOG_ERROR("cannot find ELF segment content, seek failed");
452 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
454 LOG_ERROR("cannot read ELF segment content, read failed");
460 *size_read
+= read_size
;
461 /* need more data ? */
469 int image_mot_buffer_complete(image_t
*image
)
471 image_mot_t
*mot
= image
->type_private
;
472 fileio_t
*fileio
= &mot
->fileio
;
473 u32 full_address
= 0x0;
478 /* we can't determine the number of sections that we'll have to create ahead of time,
479 * so we locally hold them until parsing is finished */
480 image_section_t section
[IMAGE_MAX_SECTIONS
];
482 mot
->buffer
= malloc(fileio
->size
>> 1);
484 image
->num_sections
= 0;
485 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
486 section
[image
->num_sections
].base_address
= 0x0;
487 section
[image
->num_sections
].size
= 0x0;
488 section
[image
->num_sections
].flags
= 0;
490 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
499 /* get record type and record length */
500 if (sscanf(&lpszLine
[bytes_read
], "S%1x%2x", &record_type
, &count
) != 2)
502 return ERROR_IMAGE_FORMAT_ERROR
;
506 cal_checksum
+= (u8
)count
;
508 /* skip checksum byte */
511 if (record_type
== 0)
513 /* S0 - starting record (optional) */
516 while (count
-- > 0) {
517 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
518 cal_checksum
+= (u8
)iValue
;
522 else if (record_type
>= 1 && record_type
<= 3)
524 switch( record_type
)
527 /* S1 - 16 bit address data record */
528 sscanf(&lpszLine
[bytes_read
], "%4x", &address
);
529 cal_checksum
+= (u8
)(address
>> 8);
530 cal_checksum
+= (u8
)address
;
536 /* S2 - 24 bit address data record */
537 sscanf(&lpszLine
[bytes_read
], "%6x", &address
);
538 cal_checksum
+= (u8
)(address
>> 16);
539 cal_checksum
+= (u8
)(address
>> 8);
540 cal_checksum
+= (u8
)address
;
546 /* S3 - 32 bit address data record */
547 sscanf(&lpszLine
[bytes_read
], "%8x", &address
);
548 cal_checksum
+= (u8
)(address
>> 24);
549 cal_checksum
+= (u8
)(address
>> 16);
550 cal_checksum
+= (u8
)(address
>> 8);
551 cal_checksum
+= (u8
)address
;
558 if (full_address
!= address
)
560 /* we encountered a nonconsecutive location, create a new section,
561 * unless the current section has zero size, in which case this specifies
562 * the current section's base address
564 if (section
[image
->num_sections
].size
!= 0)
566 image
->num_sections
++;
567 section
[image
->num_sections
].size
= 0x0;
568 section
[image
->num_sections
].flags
= 0;
569 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
571 section
[image
->num_sections
].base_address
= address
;
572 full_address
= address
;
577 sscanf(&lpszLine
[bytes_read
], "%2x", (u32
*)&mot
->buffer
[cooked_bytes
]);
578 cal_checksum
+= (u8
)mot
->buffer
[cooked_bytes
];
581 section
[image
->num_sections
].size
+= 1;
585 else if (record_type
== 5)
587 /* S5 is the data count record, we ignore it */
592 sscanf(&lpszLine
[bytes_read
], "%2x", &dummy
);
593 cal_checksum
+= (u8
)dummy
;
597 else if (record_type
>= 7 && record_type
<= 9)
599 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
600 image
->num_sections
++;
602 /* copy section information */
603 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
604 for (i
= 0; i
< image
->num_sections
; i
++)
606 image
->sections
[i
].private = section
[i
].private;
607 image
->sections
[i
].base_address
= section
[i
].base_address
;
608 image
->sections
[i
].size
= section
[i
].size
;
609 image
->sections
[i
].flags
= section
[i
].flags
;
616 LOG_ERROR("unhandled S19 record type: %i", record_type
);
617 return ERROR_IMAGE_FORMAT_ERROR
;
620 /* account for checksum, will always be 0xFF */
621 sscanf(&lpszLine
[bytes_read
], "%2x", &checksum
);
622 cal_checksum
+= (u8
)checksum
;
625 if( cal_checksum
!= 0xFF )
627 /* checksum failed */
628 LOG_ERROR("incorrect record checksum found in S19 file");
629 return ERROR_IMAGE_CHECKSUM
;
633 LOG_ERROR("premature end of S19 file, no end-of-file record found");
634 return ERROR_IMAGE_FORMAT_ERROR
;
637 int image_open(image_t
*image
, char *url
, char *type_string
)
639 int retval
= ERROR_OK
;
641 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
646 if (image
->type
== IMAGE_BINARY
)
648 image_binary_t
*image_binary
;
650 image_binary
= image
->type_private
= malloc(sizeof(image_binary_t
));
652 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
657 image
->num_sections
= 1;
658 image
->sections
= malloc(sizeof(image_section_t
));
659 image
->sections
[0].base_address
= 0x0;
660 image
->sections
[0].size
= image_binary
->fileio
.size
;
661 image
->sections
[0].flags
= 0;
663 else if (image
->type
== IMAGE_IHEX
)
665 image_ihex_t
*image_ihex
;
667 image_ihex
= image
->type_private
= malloc(sizeof(image_ihex_t
));
669 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
674 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
676 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
677 fileio_close(&image_ihex
->fileio
);
681 else if (image
->type
== IMAGE_ELF
)
683 image_elf_t
*image_elf
;
685 image_elf
= image
->type_private
= malloc(sizeof(image_elf_t
));
687 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
692 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
694 fileio_close(&image_elf
->fileio
);
698 else if (image
->type
== IMAGE_MEMORY
)
700 image_memory_t
*image_memory
;
702 image
->num_sections
= 1;
703 image
->sections
= malloc(sizeof(image_section_t
));
704 image
->sections
[0].base_address
= 0x0;
705 image
->sections
[0].size
= 0xffffffff;
706 image
->sections
[0].flags
= 0;
708 image_memory
= image
->type_private
= malloc(sizeof(image_memory_t
));
710 image_memory
->target
= get_target_by_num(strtoul(url
, NULL
, 0));;
711 image_memory
->cache
= NULL
;
712 image_memory
->cache_address
= 0x0;
714 else if (image
->type
== IMAGE_SRECORD
)
716 image_mot_t
*image_mot
;
718 image_mot
= image
->type_private
= malloc(sizeof(image_mot_t
));
720 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
725 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
727 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
728 fileio_close(&image_mot
->fileio
);
732 else if (image
->type
== IMAGE_BUILDER
)
734 image
->num_sections
= 0;
735 image
->sections
= NULL
;
736 image
->type_private
= NULL
;
739 if (image
->base_address_set
)
743 for (section
=0; section
< image
->num_sections
; section
++)
745 image
->sections
[section
].base_address
+=image
->base_address
;
747 /* we're done relocating. The two statements below are mainly
748 * for documenation purposes: stop anyone from empirically
749 * thinking they should use these values henceforth. */
750 image
->base_address
=0;
751 image
->base_address_set
=0;
757 int image_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
761 /* don't read past the end of a section */
762 if (offset
+ size
> image
->sections
[section
].size
)
764 LOG_DEBUG("read past end of section: 0x%8.8x + 0x%8.8x > 0x%8.8x",
765 offset
, size
, image
->sections
[section
].size
);
766 return ERROR_INVALID_ARGUMENTS
;
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
;
778 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
783 /* return requested bytes */
784 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
789 else if (image
->type
== IMAGE_IHEX
)
791 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
796 else if (image
->type
== IMAGE_ELF
)
798 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
800 else if (image
->type
== IMAGE_MEMORY
)
802 image_memory_t
*image_memory
= image
->type_private
;
803 u32 address
= image
->sections
[section
].base_address
+ offset
;
807 while ((size
- *size_read
) > 0)
811 if (!image_memory
->cache
812 || (address
< image_memory
->cache_address
)
813 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
815 if (!image_memory
->cache
)
816 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
818 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
819 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
821 free(image_memory
->cache
);
822 image_memory
->cache
= NULL
;
823 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
825 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
828 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
830 memcpy(buffer
+ *size_read
,
831 image_memory
->cache
+ (address
- image_memory
->cache_address
),
832 (size_in_cache
> size
) ? size
: size_in_cache
835 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
836 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
839 else if (image
->type
== IMAGE_SRECORD
)
841 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
846 else if (image
->type
== IMAGE_BUILDER
)
848 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
857 int image_add_section(image_t
*image
, u32 base
, u32 size
, int flags
, u8
*data
)
859 image_section_t
*section
;
861 /* only image builder supports adding sections */
862 if (image
->type
!= IMAGE_BUILDER
)
863 return ERROR_INVALID_ARGUMENTS
;
865 /* see if there's a previous section */
866 if (image
->num_sections
)
868 section
= &image
->sections
[image
->num_sections
- 1];
870 /* see if it's enough to extend the last section,
871 * adding data to previous sections or merging is not supported */
872 if (((section
->base_address
+ section
->size
) == base
) && (section
->flags
== flags
))
874 section
->private = realloc(section
->private, section
->size
+ size
);
875 memcpy((u8
*)section
->private + section
->size
, data
, size
);
876 section
->size
+= size
;
881 /* allocate new section */
882 image
->num_sections
++;
883 image
->sections
= realloc(image
->sections
, sizeof(image_section_t
) * image
->num_sections
);
884 section
= &image
->sections
[image
->num_sections
- 1];
885 section
->base_address
= base
;
886 section
->size
= size
;
887 section
->flags
= flags
;
888 section
->private = malloc(sizeof(u8
) * size
);
889 memcpy((u8
*)section
->private, data
, size
);
894 int image_close(image_t
*image
)
896 if (image
->type
== IMAGE_BINARY
)
898 image_binary_t
*image_binary
= image
->type_private
;
900 fileio_close(&image_binary
->fileio
);
902 else if (image
->type
== IMAGE_IHEX
)
904 image_ihex_t
*image_ihex
= image
->type_private
;
906 fileio_close(&image_ihex
->fileio
);
908 if (image_ihex
->buffer
)
910 free(image_ihex
->buffer
);
911 image_ihex
->buffer
= NULL
;
914 else if (image
->type
== IMAGE_ELF
)
916 image_elf_t
*image_elf
= image
->type_private
;
918 fileio_close(&image_elf
->fileio
);
920 if (image_elf
->header
)
922 free(image_elf
->header
);
923 image_elf
->header
= NULL
;
926 if (image_elf
->segments
)
928 free(image_elf
->segments
);
929 image_elf
->segments
= NULL
;
932 else if (image
->type
== IMAGE_MEMORY
)
934 image_memory_t
*image_memory
= image
->type_private
;
936 if (image_memory
->cache
)
938 free(image_memory
->cache
);
939 image_memory
->cache
= NULL
;
942 else if (image
->type
== IMAGE_SRECORD
)
944 image_mot_t
*image_mot
= image
->type_private
;
946 fileio_close(&image_mot
->fileio
);
948 if (image_mot
->buffer
)
950 free(image_mot
->buffer
);
951 image_mot
->buffer
= NULL
;
954 else if (image
->type
== IMAGE_BUILDER
)
958 for (i
= 0; i
< image
->num_sections
; i
++)
960 free(image
->sections
[i
].private);
961 image
->sections
[i
].private = NULL
;
965 if (image
->type_private
)
967 free(image
->type_private
);
968 image
->type_private
= NULL
;
973 free(image
->sections
);
974 image
->sections
= NULL
;
980 static u32 crc32_table
[256] = {0, 0};
982 int image_calculate_checksum(u8
* buffer
, u32 nbytes
, u32
* checksum
)
984 u32 crc
= 0xffffffff;
988 /* Initialize the CRC table and the decoding table. */
991 for (i
= 0; i
< 256; i
++)
994 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
995 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1003 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)