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 * Copyright (C) 2009 by Franck Hereson *
12 * franck.hereson@secad.fr *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
38 /* convert ELF header field to host endianness */
39 #define field16(elf,field)\
40 ((elf->endianness == ELFDATA2LSB)? \
41 le_to_h_u16((uint8_t*)&field):be_to_h_u16((uint8_t*)&field))
43 #define field32(elf,field)\
44 ((elf->endianness == ELFDATA2LSB)? \
45 le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field))
47 static int autodetect_image_type(struct image
*image
, const char *url
)
54 /* read the first 4 bytes of image */
55 if ((retval
= fileio_open(&fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
59 retval
= fileio_read(&fileio
, 9, buffer
, &read_bytes
);
61 if (retval
== ERROR_OK
)
65 retval
= ERROR_FILEIO_OPERATION_FAILED
;
68 fileio_close(&fileio
);
70 if (retval
!= ERROR_OK
)
73 /* check header against known signatures */
74 if (strncmp((char*)buffer
,ELFMAG
,SELFMAG
) == 0)
76 LOG_DEBUG("ELF image detected.");
77 image
->type
= IMAGE_ELF
;
79 else if ((buffer
[0]==':') /* record start byte */
80 &&(isxdigit(buffer
[1]))
81 &&(isxdigit(buffer
[2]))
82 &&(isxdigit(buffer
[3]))
83 &&(isxdigit(buffer
[4]))
84 &&(isxdigit(buffer
[5]))
85 &&(isxdigit(buffer
[6]))
86 &&(buffer
[7]=='0') /* record type : 00 -> 05 */
87 &&(buffer
[8]>='0') && (buffer
[8]<'6'))
89 LOG_DEBUG("IHEX image detected.");
90 image
->type
= IMAGE_IHEX
;
92 else if ((buffer
[0] == 'S') /* record start byte */
93 &&(isxdigit(buffer
[1]))
94 &&(isxdigit(buffer
[2]))
95 &&(isxdigit(buffer
[3]))
96 &&(buffer
[1] >= '0') && (buffer
[1] < '9'))
98 LOG_DEBUG("S19 image detected.");
99 image
->type
= IMAGE_SRECORD
;
103 image
->type
= IMAGE_BINARY
;
109 static int identify_image_type(struct image
*image
, const char *type_string
, const char *url
)
113 if (!strcmp(type_string
, "bin"))
115 image
->type
= IMAGE_BINARY
;
117 else if (!strcmp(type_string
, "ihex"))
119 image
->type
= IMAGE_IHEX
;
121 else if (!strcmp(type_string
, "elf"))
123 image
->type
= IMAGE_ELF
;
125 else if (!strcmp(type_string
, "mem"))
127 image
->type
= IMAGE_MEMORY
;
129 else if (!strcmp(type_string
, "s19"))
131 image
->type
= IMAGE_SRECORD
;
133 else if (!strcmp(type_string
, "build"))
135 image
->type
= IMAGE_BUILDER
;
139 return ERROR_IMAGE_TYPE_UNKNOWN
;
144 return autodetect_image_type(image
, url
);
150 static int image_ihex_buffer_complete(struct image
*image
)
152 struct image_ihex
*ihex
= image
->type_private
;
153 struct fileio
*fileio
= &ihex
->fileio
;
154 uint32_t full_address
= 0x0;
155 uint32_t cooked_bytes
;
159 /* we can't determine the number of sections that we'll have to create ahead of time,
160 * so we locally hold them until parsing is finished */
161 struct imageection section
[IMAGE_MAX_SECTIONS
];
163 ihex
->buffer
= malloc(fileio
->size
>> 1);
165 image
->num_sections
= 0;
166 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
167 section
[image
->num_sections
].base_address
= 0x0;
168 section
[image
->num_sections
].size
= 0x0;
169 section
[image
->num_sections
].flags
= 0;
171 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
175 uint32_t record_type
;
177 uint8_t cal_checksum
= 0;
178 uint32_t bytes_read
= 0;
180 if (sscanf(&lpszLine
[bytes_read
], ":%2" SCNx32
"%4" SCNx32
"%2" SCNx32
, &count
, &address
, &record_type
) != 3)
182 return ERROR_IMAGE_FORMAT_ERROR
;
186 cal_checksum
+= (uint8_t)count
;
187 cal_checksum
+= (uint8_t)(address
>> 8);
188 cal_checksum
+= (uint8_t)address
;
189 cal_checksum
+= (uint8_t)record_type
;
191 if (record_type
== 0) /* Data Record */
193 if ((full_address
& 0xffff) != address
)
195 /* we encountered a nonconsecutive location, create a new section,
196 * unless the current section has zero size, in which case this specifies
197 * the current section's base address
199 if (section
[image
->num_sections
].size
!= 0)
201 image
->num_sections
++;
202 if (image
->num_sections
>= IMAGE_MAX_SECTIONS
)
204 /* too many sections */
205 LOG_ERROR("Too many sections found in IHEX file");
206 return ERROR_IMAGE_FORMAT_ERROR
;
208 section
[image
->num_sections
].size
= 0x0;
209 section
[image
->num_sections
].flags
= 0;
210 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
212 section
[image
->num_sections
].base_address
=
213 (full_address
& 0xffff0000) | address
;
214 full_address
= (full_address
& 0xffff0000) | address
;
220 sscanf(&lpszLine
[bytes_read
], "%2x", &value
);
221 ihex
->buffer
[cooked_bytes
] = (uint8_t)value
;
222 cal_checksum
+= (uint8_t)ihex
->buffer
[cooked_bytes
];
225 section
[image
->num_sections
].size
+= 1;
229 else if (record_type
== 1) /* End of File Record */
231 /* finish the current section */
232 image
->num_sections
++;
234 /* copy section information */
235 image
->sections
= malloc(sizeof(struct imageection
) * image
->num_sections
);
236 for (i
= 0; i
< image
->num_sections
; i
++)
238 image
->sections
[i
].private = section
[i
].private;
239 image
->sections
[i
].base_address
= section
[i
].base_address
;
240 image
->sections
[i
].size
= section
[i
].size
;
241 image
->sections
[i
].flags
= section
[i
].flags
;
246 else if (record_type
== 2) /* Linear Address Record */
248 uint16_t upper_address
;
250 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
251 cal_checksum
+= (uint8_t)(upper_address
>> 8);
252 cal_checksum
+= (uint8_t)upper_address
;
255 if ((full_address
>> 4) != upper_address
)
257 /* we encountered a nonconsecutive location, create a new section,
258 * unless the current section has zero size, in which case this specifies
259 * the current section's base address
261 if (section
[image
->num_sections
].size
!= 0)
263 image
->num_sections
++;
264 if (image
->num_sections
>= IMAGE_MAX_SECTIONS
)
266 /* too many sections */
267 LOG_ERROR("Too many sections found in IHEX file");
268 return ERROR_IMAGE_FORMAT_ERROR
;
270 section
[image
->num_sections
].size
= 0x0;
271 section
[image
->num_sections
].flags
= 0;
272 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
274 section
[image
->num_sections
].base_address
=
275 (full_address
& 0xffff) | (upper_address
<< 4);
276 full_address
= (full_address
& 0xffff) | (upper_address
<< 4);
279 else if (record_type
== 3) /* Start Segment Address Record */
283 /* "Start Segment Address Record" will not be supported */
284 /* but we must consume it, and do not create an error. */
287 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &dummy
);
288 cal_checksum
+= (uint8_t)dummy
;
292 else if (record_type
== 4) /* Extended Linear Address Record */
294 uint16_t upper_address
;
296 sscanf(&lpszLine
[bytes_read
], "%4hx", &upper_address
);
297 cal_checksum
+= (uint8_t)(upper_address
>> 8);
298 cal_checksum
+= (uint8_t)upper_address
;
301 if ((full_address
>> 16) != upper_address
)
303 /* we encountered a nonconsecutive location, create a new section,
304 * unless the current section has zero size, in which case this specifies
305 * the current section's base address
307 if (section
[image
->num_sections
].size
!= 0)
309 image
->num_sections
++;
310 if (image
->num_sections
>= IMAGE_MAX_SECTIONS
)
312 /* too many sections */
313 LOG_ERROR("Too many sections found in IHEX file");
314 return ERROR_IMAGE_FORMAT_ERROR
;
316 section
[image
->num_sections
].size
= 0x0;
317 section
[image
->num_sections
].flags
= 0;
318 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
320 section
[image
->num_sections
].base_address
=
321 (full_address
& 0xffff) | (upper_address
<< 16);
322 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
325 else if (record_type
== 5) /* Start Linear Address Record */
327 uint32_t start_address
;
329 sscanf(&lpszLine
[bytes_read
], "%8" SCNx32
, &start_address
);
330 cal_checksum
+= (uint8_t)(start_address
>> 24);
331 cal_checksum
+= (uint8_t)(start_address
>> 16);
332 cal_checksum
+= (uint8_t)(start_address
>> 8);
333 cal_checksum
+= (uint8_t)start_address
;
336 image
->start_address_set
= 1;
337 image
->start_address
= be_to_h_u32((uint8_t*)&start_address
);
341 LOG_ERROR("unhandled IHEX record type: %i", (int)record_type
);
342 return ERROR_IMAGE_FORMAT_ERROR
;
345 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &checksum
);
348 if ((uint8_t)checksum
!= (uint8_t)(~cal_checksum
+ 1))
350 /* checksum failed */
351 LOG_ERROR("incorrect record checksum found in IHEX file");
352 return ERROR_IMAGE_CHECKSUM
;
356 LOG_ERROR("premature end of IHEX file, no end-of-file record found");
357 return ERROR_IMAGE_FORMAT_ERROR
;
360 static int image_elf_read_headers(struct image
*image
)
362 struct image_elf
*elf
= image
->type_private
;
367 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
369 if (elf
->header
== NULL
)
371 LOG_ERROR("insufficient memory to perform operation ");
372 return ERROR_FILEIO_OPERATION_FAILED
;
375 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (uint8_t*)elf
->header
, &read_bytes
)) != ERROR_OK
)
377 LOG_ERROR("cannot read ELF file header, read failed");
378 return ERROR_FILEIO_OPERATION_FAILED
;
380 if (read_bytes
!= sizeof(Elf32_Ehdr
))
382 LOG_ERROR("cannot read ELF file header, only partially read");
383 return ERROR_FILEIO_OPERATION_FAILED
;
386 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
) != 0)
388 LOG_ERROR("invalid ELF file, bad magic number");
389 return ERROR_IMAGE_FORMAT_ERROR
;
391 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
393 LOG_ERROR("invalid ELF file, only 32bits files are supported");
394 return ERROR_IMAGE_FORMAT_ERROR
;
397 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
398 if ((elf
->endianness
!= ELFDATA2LSB
)
399 &&(elf
->endianness
!= ELFDATA2MSB
))
401 LOG_ERROR("invalid ELF file, unknown endianess setting");
402 return ERROR_IMAGE_FORMAT_ERROR
;
405 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
406 if (elf
->segment_count
== 0)
408 LOG_ERROR("invalid ELF file, no program headers");
409 return ERROR_IMAGE_FORMAT_ERROR
;
412 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,elf
->header
->e_phoff
))) != ERROR_OK
)
414 LOG_ERROR("cannot seek to ELF program header table, read failed");
418 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
419 if (elf
->segments
== NULL
)
421 LOG_ERROR("insufficient memory to perform operation ");
422 return ERROR_FILEIO_OPERATION_FAILED
;
425 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (uint8_t*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
427 LOG_ERROR("cannot read ELF segment headers, read failed");
430 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
432 LOG_ERROR("cannot read ELF segment headers, only partially read");
433 return ERROR_FILEIO_OPERATION_FAILED
;
436 /* count useful segments (loadable), ignore BSS section */
437 image
->num_sections
= 0;
438 for (i
= 0;i
< elf
->segment_count
;i
++)
439 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
440 image
->num_sections
++;
441 /* alloc and fill sections array with loadable segments */
442 image
->sections
= malloc(image
->num_sections
* sizeof(struct imageection
));
443 for (i
= 0,j
= 0;i
< elf
->segment_count
;i
++)
445 if ((field32(elf
, elf
->segments
[i
].p_type
) == PT_LOAD
) && (field32(elf
, elf
->segments
[i
].p_filesz
) != 0))
447 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_filesz
);
448 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_paddr
);
449 image
->sections
[j
].private = &elf
->segments
[i
];
450 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
455 image
->start_address_set
= 1;
456 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
461 static int image_elf_read_section(struct image
*image
, int section
, uint32_t offset
, uint32_t size
, uint8_t *buffer
, uint32_t *size_read
)
463 struct image_elf
*elf
= image
->type_private
;
464 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
465 uint32_t read_size
,really_read
;
470 LOG_DEBUG("load segment %d at 0x%" PRIx32
" (sz = 0x%" PRIx32
")",section
,offset
,size
);
472 /* read initialized data in current segment if any */
473 if (offset
< field32(elf
,segment
->p_filesz
))
475 /* maximal size present in file for the current segment */
476 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
477 LOG_DEBUG("read elf: size = 0x%" PRIx32
" at 0x%" PRIx32
"",read_size
,
478 field32(elf
,segment
->p_offset
) + offset
);
479 /* read initialized area of the segment */
480 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
) + offset
)) != ERROR_OK
)
482 LOG_ERROR("cannot find ELF segment content, seek failed");
485 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
487 LOG_ERROR("cannot read ELF segment content, read failed");
493 *size_read
+= read_size
;
494 /* need more data ? */
502 static int image_mot_buffer_complete(struct image
*image
)
504 struct image_mot
*mot
= image
->type_private
;
505 struct fileio
*fileio
= &mot
->fileio
;
506 uint32_t full_address
= 0x0;
507 uint32_t cooked_bytes
;
511 /* we can't determine the number of sections that we'll have to create ahead of time,
512 * so we locally hold them until parsing is finished */
513 struct imageection section
[IMAGE_MAX_SECTIONS
];
515 mot
->buffer
= malloc(fileio
->size
>> 1);
517 image
->num_sections
= 0;
518 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
519 section
[image
->num_sections
].base_address
= 0x0;
520 section
[image
->num_sections
].size
= 0x0;
521 section
[image
->num_sections
].flags
= 0;
523 while (fileio_fgets(fileio
, 1023, lpszLine
) == ERROR_OK
)
527 uint32_t record_type
;
529 uint8_t cal_checksum
= 0;
530 uint32_t bytes_read
= 0;
532 /* get record type and record length */
533 if (sscanf(&lpszLine
[bytes_read
], "S%1" SCNx32
"%2" SCNx32
, &record_type
, &count
) != 2)
535 return ERROR_IMAGE_FORMAT_ERROR
;
539 cal_checksum
+= (uint8_t)count
;
541 /* skip checksum byte */
544 if (record_type
== 0)
546 /* S0 - starting record (optional) */
549 while (count
-- > 0) {
550 sscanf(&lpszLine
[bytes_read
], "%2x", &iValue
);
551 cal_checksum
+= (uint8_t)iValue
;
555 else if (record_type
>= 1 && record_type
<= 3)
560 /* S1 - 16 bit address data record */
561 sscanf(&lpszLine
[bytes_read
], "%4" SCNx32
, &address
);
562 cal_checksum
+= (uint8_t)(address
>> 8);
563 cal_checksum
+= (uint8_t)address
;
569 /* S2 - 24 bit address data record */
570 sscanf(&lpszLine
[bytes_read
], "%6" SCNx32
, &address
);
571 cal_checksum
+= (uint8_t)(address
>> 16);
572 cal_checksum
+= (uint8_t)(address
>> 8);
573 cal_checksum
+= (uint8_t)address
;
579 /* S3 - 32 bit address data record */
580 sscanf(&lpszLine
[bytes_read
], "%8" SCNx32
, &address
);
581 cal_checksum
+= (uint8_t)(address
>> 24);
582 cal_checksum
+= (uint8_t)(address
>> 16);
583 cal_checksum
+= (uint8_t)(address
>> 8);
584 cal_checksum
+= (uint8_t)address
;
591 if (full_address
!= address
)
593 /* we encountered a nonconsecutive location, create a new section,
594 * unless the current section has zero size, in which case this specifies
595 * the current section's base address
597 if (section
[image
->num_sections
].size
!= 0)
599 image
->num_sections
++;
600 section
[image
->num_sections
].size
= 0x0;
601 section
[image
->num_sections
].flags
= 0;
602 section
[image
->num_sections
].private = &mot
->buffer
[cooked_bytes
];
604 section
[image
->num_sections
].base_address
= address
;
605 full_address
= address
;
611 sscanf(&lpszLine
[bytes_read
], "%2x", &value
);
612 mot
->buffer
[cooked_bytes
] = (uint8_t)value
;
613 cal_checksum
+= (uint8_t)mot
->buffer
[cooked_bytes
];
616 section
[image
->num_sections
].size
+= 1;
620 else if (record_type
== 5)
622 /* S5 is the data count record, we ignore it */
627 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &dummy
);
628 cal_checksum
+= (uint8_t)dummy
;
632 else if (record_type
>= 7 && record_type
<= 9)
634 /* S7, S8, S9 - ending records for 32, 24 and 16bit */
635 image
->num_sections
++;
637 /* copy section information */
638 image
->sections
= malloc(sizeof(struct imageection
) * image
->num_sections
);
639 for (i
= 0; i
< image
->num_sections
; i
++)
641 image
->sections
[i
].private = section
[i
].private;
642 image
->sections
[i
].base_address
= section
[i
].base_address
;
643 image
->sections
[i
].size
= section
[i
].size
;
644 image
->sections
[i
].flags
= section
[i
].flags
;
651 LOG_ERROR("unhandled S19 record type: %i", (int)(record_type
));
652 return ERROR_IMAGE_FORMAT_ERROR
;
655 /* account for checksum, will always be 0xFF */
656 sscanf(&lpszLine
[bytes_read
], "%2" SCNx32
, &checksum
);
657 cal_checksum
+= (uint8_t)checksum
;
660 if (cal_checksum
!= 0xFF)
662 /* checksum failed */
663 LOG_ERROR("incorrect record checksum found in S19 file");
664 return ERROR_IMAGE_CHECKSUM
;
668 LOG_ERROR("premature end of S19 file, no end-of-file record found");
669 return ERROR_IMAGE_FORMAT_ERROR
;
672 int image_open(struct image
*image
, const char *url
, const char *type_string
)
674 int retval
= ERROR_OK
;
676 if ((retval
= identify_image_type(image
, type_string
, url
)) != ERROR_OK
)
681 if (image
->type
== IMAGE_BINARY
)
683 struct image_binary
*image_binary
;
685 image_binary
= image
->type_private
= malloc(sizeof(struct image_binary
));
687 if ((retval
= fileio_open(&image_binary
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
692 image
->num_sections
= 1;
693 image
->sections
= malloc(sizeof(struct imageection
));
694 image
->sections
[0].base_address
= 0x0;
695 image
->sections
[0].size
= image_binary
->fileio
.size
;
696 image
->sections
[0].flags
= 0;
698 else if (image
->type
== IMAGE_IHEX
)
700 struct image_ihex
*image_ihex
;
702 image_ihex
= image
->type_private
= malloc(sizeof(struct image_ihex
));
704 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
709 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
711 LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
712 fileio_close(&image_ihex
->fileio
);
716 else if (image
->type
== IMAGE_ELF
)
718 struct image_elf
*image_elf
;
720 image_elf
= image
->type_private
= malloc(sizeof(struct image_elf
));
722 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
727 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
729 fileio_close(&image_elf
->fileio
);
733 else if (image
->type
== IMAGE_MEMORY
)
735 struct target
*target
= get_target(url
);
739 LOG_ERROR("target '%s' not defined", url
);
743 struct image_memory
*image_memory
;
745 image
->num_sections
= 1;
746 image
->sections
= malloc(sizeof(struct imageection
));
747 image
->sections
[0].base_address
= 0x0;
748 image
->sections
[0].size
= 0xffffffff;
749 image
->sections
[0].flags
= 0;
751 image_memory
= image
->type_private
= malloc(sizeof(struct image_memory
));
753 image_memory
->target
= target
;
754 image_memory
->cache
= NULL
;
755 image_memory
->cache_address
= 0x0;
757 else if (image
->type
== IMAGE_SRECORD
)
759 struct image_mot
*image_mot
;
761 image_mot
= image
->type_private
= malloc(sizeof(struct image_mot
));
763 if ((retval
= fileio_open(&image_mot
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
768 if ((retval
= image_mot_buffer_complete(image
)) != ERROR_OK
)
770 LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
771 fileio_close(&image_mot
->fileio
);
775 else if (image
->type
== IMAGE_BUILDER
)
777 image
->num_sections
= 0;
778 image
->sections
= NULL
;
779 image
->type_private
= NULL
;
782 if (image
->base_address_set
)
786 for (section
= 0; section
< image
->num_sections
; section
++)
788 image
->sections
[section
].base_address
+= image
->base_address
;
790 /* we're done relocating. The two statements below are mainly
791 * for documenation purposes: stop anyone from empirically
792 * thinking they should use these values henceforth. */
793 image
->base_address
= 0;
794 image
->base_address_set
= 0;
800 int image_read_section(struct image
*image
, int section
, uint32_t offset
, uint32_t size
, uint8_t *buffer
, uint32_t *size_read
)
804 /* don't read past the end of a section */
805 if (offset
+ size
> image
->sections
[section
].size
)
807 LOG_DEBUG("read past end of section: 0x%8.8" PRIx32
" + 0x%8.8" PRIx32
" > 0x%8.8" PRIx32
"",
808 offset
, size
, image
->sections
[section
].size
);
809 return ERROR_INVALID_ARGUMENTS
;
812 if (image
->type
== IMAGE_BINARY
)
814 struct image_binary
*image_binary
= image
->type_private
;
816 /* only one section in a plain binary */
818 return ERROR_INVALID_ARGUMENTS
;
821 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
826 /* return requested bytes */
827 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
832 else if (image
->type
== IMAGE_IHEX
)
834 memcpy(buffer
, (uint8_t*)image
->sections
[section
].private + offset
, size
);
839 else if (image
->type
== IMAGE_ELF
)
841 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
843 else if (image
->type
== IMAGE_MEMORY
)
845 struct image_memory
*image_memory
= image
->type_private
;
846 uint32_t address
= image
->sections
[section
].base_address
+ offset
;
850 while ((size
- *size_read
) > 0)
852 uint32_t size_in_cache
;
854 if (!image_memory
->cache
855 || (address
< image_memory
->cache_address
)
856 || (address
>= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
)))
858 if (!image_memory
->cache
)
859 image_memory
->cache
= malloc(IMAGE_MEMORY_CACHE_SIZE
);
861 if (target_read_buffer(image_memory
->target
, address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1),
862 IMAGE_MEMORY_CACHE_SIZE
, image_memory
->cache
) != ERROR_OK
)
864 free(image_memory
->cache
);
865 image_memory
->cache
= NULL
;
866 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
;
868 image_memory
->cache_address
= address
& ~(IMAGE_MEMORY_CACHE_SIZE
- 1);
871 size_in_cache
= (image_memory
->cache_address
+ IMAGE_MEMORY_CACHE_SIZE
) - address
;
873 memcpy(buffer
+ *size_read
,
874 image_memory
->cache
+ (address
- image_memory
->cache_address
),
875 (size_in_cache
> size
) ? size
: size_in_cache
878 *size_read
+= (size_in_cache
> size
) ? size
: size_in_cache
;
879 address
+= (size_in_cache
> size
) ? size
: size_in_cache
;
882 else if (image
->type
== IMAGE_SRECORD
)
884 memcpy(buffer
, (uint8_t*)image
->sections
[section
].private + offset
, size
);
889 else if (image
->type
== IMAGE_BUILDER
)
891 memcpy(buffer
, (uint8_t*)image
->sections
[section
].private + offset
, size
);
900 int image_add_section(struct image
*image
, uint32_t base
, uint32_t size
, int flags
, uint8_t *data
)
902 struct imageection
*section
;
904 /* only image builder supports adding sections */
905 if (image
->type
!= IMAGE_BUILDER
)
906 return ERROR_INVALID_ARGUMENTS
;
908 /* see if there's a previous section */
909 if (image
->num_sections
)
911 section
= &image
->sections
[image
->num_sections
- 1];
913 /* see if it's enough to extend the last section,
914 * adding data to previous sections or merging is not supported */
915 if (((section
->base_address
+ section
->size
) == base
) && (section
->flags
== flags
))
917 section
->private = realloc(section
->private, section
->size
+ size
);
918 memcpy((uint8_t*)section
->private + section
->size
, data
, size
);
919 section
->size
+= size
;
924 /* allocate new section */
925 image
->num_sections
++;
926 image
->sections
= realloc(image
->sections
, sizeof(struct imageection
) * image
->num_sections
);
927 section
= &image
->sections
[image
->num_sections
- 1];
928 section
->base_address
= base
;
929 section
->size
= size
;
930 section
->flags
= flags
;
931 section
->private = malloc(sizeof(uint8_t) * size
);
932 memcpy((uint8_t*)section
->private, data
, size
);
937 void image_close(struct image
*image
)
939 if (image
->type
== IMAGE_BINARY
)
941 struct image_binary
*image_binary
= image
->type_private
;
943 fileio_close(&image_binary
->fileio
);
945 else if (image
->type
== IMAGE_IHEX
)
947 struct image_ihex
*image_ihex
= image
->type_private
;
949 fileio_close(&image_ihex
->fileio
);
951 if (image_ihex
->buffer
)
953 free(image_ihex
->buffer
);
954 image_ihex
->buffer
= NULL
;
957 else if (image
->type
== IMAGE_ELF
)
959 struct image_elf
*image_elf
= image
->type_private
;
961 fileio_close(&image_elf
->fileio
);
963 if (image_elf
->header
)
965 free(image_elf
->header
);
966 image_elf
->header
= NULL
;
969 if (image_elf
->segments
)
971 free(image_elf
->segments
);
972 image_elf
->segments
= NULL
;
975 else if (image
->type
== IMAGE_MEMORY
)
977 struct image_memory
*image_memory
= image
->type_private
;
979 if (image_memory
->cache
)
981 free(image_memory
->cache
);
982 image_memory
->cache
= NULL
;
985 else if (image
->type
== IMAGE_SRECORD
)
987 struct image_mot
*image_mot
= image
->type_private
;
989 fileio_close(&image_mot
->fileio
);
991 if (image_mot
->buffer
)
993 free(image_mot
->buffer
);
994 image_mot
->buffer
= NULL
;
997 else if (image
->type
== IMAGE_BUILDER
)
1001 for (i
= 0; i
< image
->num_sections
; i
++)
1003 free(image
->sections
[i
].private);
1004 image
->sections
[i
].private = NULL
;
1008 if (image
->type_private
)
1010 free(image
->type_private
);
1011 image
->type_private
= NULL
;
1014 if (image
->sections
)
1016 free(image
->sections
);
1017 image
->sections
= NULL
;
1021 int image_calculate_checksum(uint8_t* buffer
, uint32_t nbytes
, uint32_t* checksum
)
1023 uint32_t crc
= 0xffffffff;
1024 LOG_DEBUG("Calculating checksum");
1026 uint32_t crc32_table
[256];
1028 /* Initialize the CRC table and the decoding table. */
1031 for (i
= 0; i
< 256; i
++)
1034 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1035 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1050 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buffer
++) & 255];
1055 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)