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 ***************************************************************************/
31 #include "replacements.h"
37 /* convert ELF header field to host endianness */
38 #define field16(elf,field)\
39 ((elf->endianness==ELFDATA2LSB)? \
40 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
42 #define field32(elf,field)\
43 ((elf->endianness==ELFDATA2LSB)? \
44 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
46 int image_ihex_buffer_complete(image_t
*image
)
48 image_ihex_t
*ihex
= image
->type_private
;
49 fileio_t
*fileio
= &ihex
->fileio
;
50 u32 raw_bytes_read
, raw_bytes
;
52 u32 full_address
= 0x0;
53 char *buffer
= malloc(fileio
->size
);
57 /* we can't determine the number of sections that we'll have to create ahead of time,
58 * so we locally hold them until parsing is finished */
59 image_section_t section
[IMAGE_MAX_SECTIONS
];
61 if ((retval
= fileio_read(fileio
, fileio
->size
, (u8
*)buffer
, &raw_bytes_read
)) != ERROR_OK
)
64 ERROR("failed buffering IHEX file, read failed");
65 return ERROR_FILEIO_OPERATION_FAILED
;
68 if (raw_bytes_read
!= fileio
->size
)
71 ERROR("failed buffering complete IHEX file, only partially read");
72 return ERROR_FILEIO_OPERATION_FAILED
;
75 ihex
->buffer
= malloc(fileio
->size
>> 1);
78 image
->num_sections
= 0;
79 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
80 section
[image
->num_sections
].base_address
= 0x0;
81 section
[image
->num_sections
].size
= 0x0;
82 section
[image
->num_sections
].flags
= 0;
83 while (raw_bytes
< raw_bytes_read
)
90 if (sscanf(&buffer
[raw_bytes
], ":%2x%4x%2x", &count
, &address
, &record_type
) != 3)
92 return ERROR_IMAGE_FORMAT_ERROR
;
96 if (record_type
== 0) /* Data Record */
98 if ((full_address
& 0xffff) != address
)
100 /* we encountered a nonconsecutive location, create a new section,
101 * unless the current section has zero size, in which case this specifies
102 * the current section's base address
104 if (section
[image
->num_sections
].size
!= 0)
106 image
->num_sections
++;
107 section
[image
->num_sections
].size
= 0x0;
108 section
[image
->num_sections
].flags
= 0;
109 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
111 section
[image
->num_sections
].base_address
=
112 (full_address
& 0xffff0000) | address
;
113 full_address
= (full_address
& 0xffff0000) | address
;
118 sscanf(&buffer
[raw_bytes
], "%2hhx", &ihex
->buffer
[cooked_bytes
]);
121 section
[image
->num_sections
].size
+= 1;
125 else if (record_type
== 1) /* End of File Record */
127 /* finish the current section */
128 image
->num_sections
++;
130 /* copy section information */
131 image
->sections
= malloc(sizeof(image_section_t
) * image
->num_sections
);
132 for (i
= 0; i
< image
->num_sections
; i
++)
134 image
->sections
[i
].private = section
[i
].private;
135 image
->sections
[i
].base_address
= section
[i
].base_address
+
136 ((image
->base_address_set
) ? image
->base_address
: 0);
137 image
->sections
[i
].size
= section
[i
].size
;
138 image
->sections
[i
].flags
= section
[i
].flags
;
144 else if (record_type
== 4) /* Extended Linear Address Record */
148 sscanf(&buffer
[raw_bytes
], "%4hx", &upper_address
);
151 if ((full_address
>> 16) != upper_address
)
153 /* we encountered a nonconsecutive location, create a new section,
154 * unless the current section has zero size, in which case this specifies
155 * the current section's base address
157 if (section
[image
->num_sections
].size
!= 0)
159 image
->num_sections
++;
160 section
[image
->num_sections
].size
= 0x0;
161 section
[image
->num_sections
].flags
= 0;
162 section
[image
->num_sections
].private = &ihex
->buffer
[cooked_bytes
];
164 section
[image
->num_sections
].base_address
=
165 (full_address
& 0xffff) | (upper_address
<< 16);
166 full_address
= (full_address
& 0xffff) | (upper_address
<< 16);
169 else if (record_type
== 5) /* Start Linear Address Record */
173 sscanf(&buffer
[raw_bytes
], "%8x", &start_address
);
176 image
->start_address_set
= 1;
177 image
->start_address
= be_to_h_u32((u8
*)&start_address
);
182 ERROR("unhandled IHEX record type: %i", record_type
);
183 return ERROR_IMAGE_FORMAT_ERROR
;
186 sscanf(&buffer
[raw_bytes
], "%2x", &checksum
);
189 /* consume new-line character(s) */
190 if ((buffer
[raw_bytes
] == '\n') || (buffer
[raw_bytes
] == '\r'))
193 if ((buffer
[raw_bytes
] == '\n') || (buffer
[raw_bytes
] == '\r'))
198 ERROR("premature end of IHEX file, no end-of-file record found");
199 return ERROR_IMAGE_FORMAT_ERROR
;
202 int image_elf_read_headers(image_t
*image
)
204 image_elf_t
*elf
= image
->type_private
;
209 elf
->header
= malloc(sizeof(Elf32_Ehdr
));
211 if ((retval
= fileio_read(&elf
->fileio
, sizeof(Elf32_Ehdr
), (u8
*)elf
->header
, &read_bytes
)) != ERROR_OK
)
213 ERROR("cannot read ELF file header, read failed");
214 return ERROR_FILEIO_OPERATION_FAILED
;
216 if (read_bytes
!= sizeof(Elf32_Ehdr
))
218 ERROR("cannot read ELF file header, only partially read");
219 return ERROR_FILEIO_OPERATION_FAILED
;
222 if (strncmp((char*)elf
->header
->e_ident
,ELFMAG
,SELFMAG
)!=0)
224 ERROR("invalid ELF file, bad magic number");
225 return ERROR_IMAGE_FORMAT_ERROR
;
227 if (elf
->header
->e_ident
[EI_CLASS
]!=ELFCLASS32
)
229 ERROR("invalid ELF file, only 32bits files are supported");
230 return ERROR_IMAGE_FORMAT_ERROR
;
234 elf
->endianness
= elf
->header
->e_ident
[EI_DATA
];
235 if ((elf
->endianness
==ELFDATANONE
)
236 ||(elf
->endianness
>=ELFDATANUM
))
238 ERROR("invalid ELF file, unknown endianess setting");
239 return ERROR_IMAGE_FORMAT_ERROR
;
242 elf
->segment_count
= field16(elf
,elf
->header
->e_phnum
);
243 if (elf
->segment_count
==0)
245 ERROR("invalid ELF file, no program headers");
246 return ERROR_IMAGE_FORMAT_ERROR
;
249 elf
->segments
= malloc(elf
->segment_count
*sizeof(Elf32_Phdr
));
251 if ((retval
= fileio_read(&elf
->fileio
, elf
->segment_count
*sizeof(Elf32_Phdr
), (u8
*)elf
->segments
, &read_bytes
)) != ERROR_OK
)
253 ERROR("cannot read ELF segment headers, read failed");
256 if (read_bytes
!= elf
->segment_count
*sizeof(Elf32_Phdr
))
258 ERROR("cannot read ELF segment headers, only partially read");
259 return ERROR_FILEIO_OPERATION_FAILED
;
262 /* count useful segments (loadable) */
263 image
->num_sections
= 0;
264 for (i
=0;i
<elf
->segment_count
;i
++)
265 if (field32(elf
,elf
->segments
[i
].p_type
) == PT_LOAD
)
266 image
->num_sections
++;
267 /* alloc and fill sections array with loadable segments */
268 image
->sections
= malloc(image
->num_sections
* sizeof(image_section_t
));
269 for (i
=0,j
=0;i
<elf
->segment_count
;i
++)
271 if (field32(elf
,elf
->segments
[i
].p_type
) == PT_LOAD
)
273 image
->sections
[j
].size
= field32(elf
,elf
->segments
[i
].p_memsz
);
274 image
->sections
[j
].base_address
= field32(elf
,elf
->segments
[i
].p_vaddr
);
275 image
->sections
[j
].private = &elf
->segments
[i
];
276 image
->sections
[j
].flags
= field32(elf
,elf
->segments
[i
].p_flags
);
281 image
->start_address_set
= 1;
282 image
->start_address
= field32(elf
,elf
->header
->e_entry
);
287 int image_elf_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
289 image_elf_t
*elf
= image
->type_private
;
290 Elf32_Phdr
*segment
= (Elf32_Phdr
*)image
->sections
[section
].private;
291 u32 read_size
,really_read
;
296 DEBUG("load segment %d at 0x%x (sz=0x%x)",section
,offset
,size
);
298 /* read initialized data in current segment if any */
299 if (offset
<field32(elf
,segment
->p_filesz
))
301 /* maximal size present in file for the current segment */
302 read_size
= MIN(size
, field32(elf
,segment
->p_filesz
)-offset
);
303 DEBUG("read elf: size = 0x%x at 0x%x",read_size
,
304 field32(elf
,segment
->p_offset
)+offset
);
305 /* read initialized area of the segment */
306 if ((retval
= fileio_seek(&elf
->fileio
, field32(elf
,segment
->p_offset
)+offset
)) != ERROR_OK
)
308 ERROR("cannot find ELF segment content, seek failed");
311 if ((retval
= fileio_read(&elf
->fileio
, read_size
, buffer
, &really_read
)) != ERROR_OK
)
313 ERROR("cannot read ELF segment content, read failed");
319 *size_read
+= read_size
;
320 /* need more data ? */
324 /* if there is remaining zeroed area in current segment */
325 if (offset
<field32(elf
,segment
->p_memsz
))
327 /* fill zeroed part (BSS) of the segment */
328 read_size
= MIN(size
, field32(elf
,segment
->p_memsz
)-offset
);
329 DEBUG("zero fill: size = 0x%x",read_size
);
330 memset(buffer
,0,read_size
);
331 *size_read
+= read_size
;
337 int image_open(image_t
*image
, void *source
, enum fileio_access access
)
339 int retval
= ERROR_OK
;
341 if (image
->type
== IMAGE_BINARY
)
343 image_binary_t
*image_binary
;
346 image_binary
= image
->type_private
= malloc(sizeof(image_binary_t
));
348 if ((retval
= fileio_open(&image_binary
->fileio
, url
, access
, FILEIO_BINARY
)) != ERROR_OK
)
350 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
351 ERROR(image
->error_str
);
355 image
->num_sections
= 1;
356 image
->sections
= malloc(sizeof(image_section_t
));
357 image
->sections
[0].base_address
= 0x0;
358 image
->sections
[0].size
= image_binary
->fileio
.size
;
359 image
->sections
[0].flags
= 0;
361 if (image
->base_address_set
== 1)
362 image
->sections
[0].base_address
= image
->base_address
;
366 else if (image
->type
== IMAGE_IHEX
)
368 image_ihex_t
*image_ihex
;
371 if (access
!= FILEIO_READ
)
373 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
374 "can't open IHEX file for writing");
375 ERROR(image
->error_str
);
376 return ERROR_FILEIO_ACCESS_NOT_SUPPORTED
;
379 image_ihex
= image
->type_private
= malloc(sizeof(image_ihex_t
));
381 if ((retval
= fileio_open(&image_ihex
->fileio
, url
, FILEIO_READ
, FILEIO_TEXT
)) != ERROR_OK
)
383 strncpy(image
->error_str
, image_ihex
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
384 ERROR(image
->error_str
);
388 if ((retval
= image_ihex_buffer_complete(image
)) != ERROR_OK
)
390 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
391 "failed buffering IHEX image, check daemon output for additional information");
392 ERROR(image
->error_str
);
393 fileio_close(&image_ihex
->fileio
);
397 else if (image
->type
== IMAGE_ELF
)
399 image_elf_t
*image_elf
;
402 image_elf
= image
->type_private
= malloc(sizeof(image_elf_t
));
404 if ((retval
= fileio_open(&image_elf
->fileio
, url
, FILEIO_READ
, FILEIO_BINARY
)) != ERROR_OK
)
406 strncpy(image
->error_str
, image_elf
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
407 ERROR(image
->error_str
);
411 if ((retval
= image_elf_read_headers(image
)) != ERROR_OK
)
413 snprintf(image
->error_str
, IMAGE_MAX_ERROR_STRING
,
414 "failed to read ELF headers, check daemon output for additional information");
415 ERROR(image
->error_str
);
416 fileio_close(&image_elf
->fileio
);
420 else if (image
->type
== IMAGE_MEMORY
)
422 image_memory_t
*image_memory
;
423 target_t
*target
= source
;
425 image_memory
= image
->type_private
= malloc(sizeof(image_memory_t
));
427 image_memory
->target
= target
;
433 int image_read_section(image_t
*image
, int section
, u32 offset
, u32 size
, u8
*buffer
, u32
*size_read
)
437 if (image
->type
== IMAGE_BINARY
)
439 image_binary_t
*image_binary
= image
->type_private
;
441 /* only one section in a plain binary */
443 return ERROR_INVALID_ARGUMENTS
;
445 if ((offset
> image
->sections
[0].size
) || (offset
+ size
> image
->sections
[0].size
))
446 return ERROR_INVALID_ARGUMENTS
;
449 if ((retval
= fileio_seek(&image_binary
->fileio
, offset
)) != ERROR_OK
)
451 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
455 /* return requested bytes */
456 if ((retval
= fileio_read(&image_binary
->fileio
, size
, buffer
, size_read
)) != ERROR_OK
)
458 strncpy(image
->error_str
, image_binary
->fileio
.error_str
, IMAGE_MAX_ERROR_STRING
);
462 else if (image
->type
== IMAGE_IHEX
)
464 memcpy(buffer
, (u8
*)image
->sections
[section
].private + offset
, size
);
466 image
->error_str
[0] = '\0';
470 else if (image
->type
== IMAGE_ELF
)
472 return image_elf_read_section(image
, section
, offset
, size
, buffer
, size_read
);
474 else if (image
->type
== IMAGE_MEMORY
)
476 /* TODO: handle target memory pseudo image */
482 int image_close(image_t
*image
)
484 if (image
->type
== IMAGE_BINARY
)
486 image_binary_t
*image_binary
= image
->type_private
;
488 fileio_close(&image_binary
->fileio
);
490 else if (image
->type
== IMAGE_IHEX
)
492 image_ihex_t
*image_ihex
= image
->type_private
;
494 fileio_close(&image_ihex
->fileio
);
496 if (image_ihex
->section_pointer
)
497 free(image_ihex
->section_pointer
);
499 if (image_ihex
->buffer
)
500 free(image_ihex
->buffer
);
502 else if (image
->type
== IMAGE_ELF
)
504 image_elf_t
*image_elf
= image
->type_private
;
506 fileio_close(&image_elf
->fileio
);
508 if (image_elf
->header
)
509 free(image_elf
->header
);
511 if (image_elf
->segments
)
512 free(image_elf
->segments
);
514 else if (image
->type
== IMAGE_MEMORY
)
516 /* do nothing for now */
519 if (image
->type_private
)
520 free(image
->type_private
);
523 free(image
->sections
);
528 int identify_image_type(image_type_t
*type
, char *type_string
)
532 if (!strcmp(type_string
, "bin"))
534 *type
= IMAGE_BINARY
;
536 else if (!strcmp(type_string
, "ihex"))
540 else if (!strcmp(type_string
, "elf"))
546 return ERROR_IMAGE_TYPE_UNKNOWN
;
551 *type
= IMAGE_BINARY
;
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)