30 #define field16(elf, field) \
31 ((elf->endianness == ELFDATA2LSB) ? \
32 le_to_h_u16((uint8_t *)&field) : be_to_h_u16((uint8_t *)&field))
34 #define field32(elf, field) \
35 ((elf->endianness == ELFDATA2LSB) ? \
36 le_to_h_u32((uint8_t *)&field) : be_to_h_u32((uint8_t *)&field))
38 #define field64(elf, field) \
39 ((elf->endianness == ELFDATA2LSB) ? \
40 le_to_h_u64((uint8_t *)&field) : be_to_h_u64((uint8_t *)&field))
57 if (retval ==
ERROR_OK && read_bytes != 9) {
58 LOG_DEBUG(
"Less than 9 bytes in the image file found.");
71 }
else if ((
buffer[0] ==
':')
82 }
else if ((
buffer[0] ==
'S')
100 if (!strcmp(type_string,
"bin")) {
102 }
else if (!strcmp(type_string,
"ihex")) {
104 }
else if (!strcmp(type_string,
"elf")) {
106 }
else if (!strcmp(type_string,
"mem")) {
108 }
else if (!strcmp(type_string,
"s19")) {
110 }
else if (!strcmp(type_string,
"build")) {
113 LOG_ERROR(
"Unknown image type: %s, use one of: bin, ihex, elf, mem, s19, build", type_string);
128 uint32_t full_address;
129 uint32_t cooked_bytes;
130 bool end_rec =
false;
141 ihex->
buffer = malloc(filesize >> 1);
155 uint32_t record_type;
157 uint8_t cal_checksum = 0;
158 size_t bytes_read = 0;
161 if ((lpsz_line[0] ==
'#') || (strlen(lpsz_line + strspn(lpsz_line,
"\n\t\r ")) == 0))
164 if (sscanf(&lpsz_line[bytes_read],
":%2" SCNx32
"%4" SCNx32
"%2" SCNx32, &
count,
165 &address, &record_type) != 3)
169 cal_checksum += (uint8_t)
count;
170 cal_checksum += (uint8_t)(address >> 8);
171 cal_checksum += (uint8_t)address;
172 cal_checksum += (uint8_t)record_type;
174 if (record_type == 0) {
175 if ((full_address & 0xffff) != address) {
184 LOG_ERROR(
"Too many sections found in IHEX file");
190 &ihex->
buffer[cooked_bytes];
193 (full_address & 0xffff0000) | address;
194 full_address = (full_address & 0xffff0000) | address;
197 while (
count-- > 0) {
199 sscanf(&lpsz_line[bytes_read],
"%2x", &value);
200 ihex->
buffer[cooked_bytes] = (uint8_t)value;
201 cal_checksum += (uint8_t)ihex->
buffer[cooked_bytes];
207 }
else if (record_type == 1) {
222 }
else if (record_type == 2) {
223 uint16_t upper_address;
225 sscanf(&lpsz_line[bytes_read],
"%4hx", &upper_address);
226 cal_checksum += (uint8_t)(upper_address >> 8);
227 cal_checksum += (uint8_t)upper_address;
230 if ((full_address >> 4) != upper_address) {
239 LOG_ERROR(
"Too many sections found in IHEX file");
245 &ihex->
buffer[cooked_bytes];
248 (full_address & 0xffff) | (upper_address << 4);
249 full_address = (full_address & 0xffff) | (upper_address << 4);
251 }
else if (record_type == 3) {
256 while (
count-- > 0) {
257 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &
dummy);
258 cal_checksum += (uint8_t)
dummy;
261 }
else if (record_type == 4) {
262 uint16_t upper_address;
264 sscanf(&lpsz_line[bytes_read],
"%4hx", &upper_address);
265 cal_checksum += (uint8_t)(upper_address >> 8);
266 cal_checksum += (uint8_t)upper_address;
269 if ((full_address >> 16) != upper_address) {
278 LOG_ERROR(
"Too many sections found in IHEX file");
284 &ihex->
buffer[cooked_bytes];
287 (full_address & 0xffff) | (upper_address << 16);
288 full_address = (full_address & 0xffff) | (upper_address << 16);
290 }
else if (record_type == 5) {
291 uint32_t start_address;
293 sscanf(&lpsz_line[bytes_read],
"%8" SCNx32, &start_address);
294 cal_checksum += (uint8_t)(start_address >> 24);
295 cal_checksum += (uint8_t)(start_address >> 16);
296 cal_checksum += (uint8_t)(start_address >> 8);
297 cal_checksum += (uint8_t)start_address;
303 LOG_ERROR(
"unhandled IHEX record type: %i", (
int)record_type);
307 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &checksum);
309 if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1)) {
311 LOG_ERROR(
"incorrect record checksum found in IHEX file");
317 LOG_WARNING(
"continuing after end-of-file record: %.40s", lpsz_line);
325 LOG_ERROR(
"premature end of IHEX file, no matching end-of-file record found");
336 char *lpsz_line = malloc(1023);
364 bool load_to_vaddr =
false;
368 LOG_ERROR(
"cannot seek to ELF file header, read failed");
375 LOG_ERROR(
"insufficient memory to perform operation");
381 LOG_ERROR(
"cannot read ELF file header, read failed");
385 LOG_ERROR(
"cannot read ELF file header, only partially read");
391 LOG_ERROR(
"invalid ELF file, no program headers");
397 LOG_ERROR(
"cannot seek to ELF program header table, read failed");
403 LOG_ERROR(
"insufficient memory to perform operation");
410 LOG_ERROR(
"cannot read ELF segment headers, read failed");
414 LOG_ERROR(
"cannot read ELF segment headers, only partially read");
427 LOG_ERROR(
"invalid ELF file, no loadable segments");
450 load_to_vaddr =
true;
455 LOG_ERROR(
"insufficient memory to perform operation");
489 bool load_to_vaddr =
false;
493 LOG_ERROR(
"cannot seek to ELF file header, read failed");
500 LOG_ERROR(
"insufficient memory to perform operation");
506 LOG_ERROR(
"cannot read ELF file header, read failed");
510 LOG_ERROR(
"cannot read ELF file header, only partially read");
516 LOG_ERROR(
"invalid ELF file, no program headers");
522 LOG_ERROR(
"cannot seek to ELF program header table, read failed");
528 LOG_ERROR(
"insufficient memory to perform operation");
535 LOG_ERROR(
"cannot read ELF segment headers, read failed");
539 LOG_ERROR(
"cannot read ELF segment headers, only partially read");
552 LOG_ERROR(
"invalid ELF file, no loadable segments");
575 load_to_vaddr =
true;
580 LOG_ERROR(
"insufficient memory to perform operation");
616 LOG_ERROR(
"cannot read ELF file header, read failed");
620 LOG_ERROR(
"cannot read ELF file header, only partially read");
625 LOG_ERROR(
"invalid ELF file, bad magic number");
632 LOG_ERROR(
"invalid ELF file, unknown endianness setting");
648 LOG_ERROR(
"invalid ELF file, only 32/64 bit ELF files are supported");
662 size_t read_size, really_read;
678 LOG_ERROR(
"cannot find ELF segment content, seek failed");
683 LOG_ERROR(
"cannot read ELF segment content, read failed");
687 *size_read += read_size;
705 size_t read_size, really_read;
721 LOG_ERROR(
"cannot find ELF segment content, seek failed");
726 LOG_ERROR(
"cannot read ELF segment content, read failed");
730 *size_read += read_size;
760 uint32_t full_address;
761 uint32_t cooked_bytes;
762 bool end_rec =
false;
773 mot->
buffer = malloc(filesize >> 1);
787 uint32_t record_type;
789 uint8_t cal_checksum = 0;
790 uint32_t bytes_read = 0;
793 if ((lpsz_line[0] ==
'#') || (strlen(lpsz_line + strspn(lpsz_line,
"\n\t\r ")) == 0))
797 if (sscanf(&lpsz_line[bytes_read],
"S%1" SCNx32
"%2" SCNx32, &record_type,
802 cal_checksum += (uint8_t)
count;
807 if (record_type == 0) {
811 while (
count-- > 0) {
812 sscanf(&lpsz_line[bytes_read],
"%2x", &value);
813 cal_checksum += (uint8_t)value;
816 }
else if (record_type >= 1 && record_type <= 3) {
817 switch (record_type) {
820 sscanf(&lpsz_line[bytes_read],
"%4" SCNx32, &address);
821 cal_checksum += (uint8_t)(address >> 8);
822 cal_checksum += (uint8_t)address;
829 sscanf(&lpsz_line[bytes_read],
"%6" SCNx32, &address);
830 cal_checksum += (uint8_t)(address >> 16);
831 cal_checksum += (uint8_t)(address >> 8);
832 cal_checksum += (uint8_t)address;
839 sscanf(&lpsz_line[bytes_read],
"%8" SCNx32, &address);
840 cal_checksum += (uint8_t)(address >> 24);
841 cal_checksum += (uint8_t)(address >> 16);
842 cal_checksum += (uint8_t)(address >> 8);
843 cal_checksum += (uint8_t)address;
850 if (full_address != address) {
860 &mot->
buffer[cooked_bytes];
863 full_address = address;
866 while (
count-- > 0) {
868 sscanf(&lpsz_line[bytes_read],
"%2x", &value);
869 mot->
buffer[cooked_bytes] = (uint8_t)value;
870 cal_checksum += (uint8_t)mot->
buffer[cooked_bytes];
876 }
else if (record_type == 5 || record_type == 6) {
880 while (
count-- > 0) {
881 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &
dummy);
882 cal_checksum += (uint8_t)
dummy;
885 }
else if (record_type >= 7 && record_type <= 9) {
901 LOG_ERROR(
"unhandled S19 record type: %i", (
int)(record_type));
906 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &checksum);
907 cal_checksum += (uint8_t)checksum;
909 if (cal_checksum != 0xFF) {
911 LOG_ERROR(
"incorrect record checksum found in S19 file");
917 LOG_WARNING(
"continuing after end-of-file record: %.40s", lpsz_line);
925 LOG_ERROR(
"premature end of S19 file, no matching end-of-file record found");
936 char *lpsz_line = malloc(1023);
972 goto free_mem_on_error;
978 goto free_mem_on_error;
993 goto free_mem_on_error;
998 "failed buffering IHEX image, check server output for additional information");
1000 goto free_mem_on_error;
1009 goto free_mem_on_error;
1014 goto free_mem_on_error;
1020 LOG_ERROR(
"target '%s' not defined", url);
1044 goto free_mem_on_error;
1049 "failed buffering S19 image, check server output for additional information");
1051 goto free_mem_on_error;
1091 "read past end of section: 0x%8.8" TARGET_PRIxADDR " + 0x%8.8" PRIx32
" > 0x%8.8" PRIx32
"",
1127 while ((
size - *size_read) > 0) {
1128 uint32_t size_in_cache;
1151 memcpy(
buffer + *size_read,
1153 (size_in_cache >
size) ?
size : size_in_cache
1156 *size_read += (size_in_cache >
size) ?
size : size_in_cache;
1157 address += (size_in_cache >
size) ?
size : size_in_cache;
1205 section->
private = malloc(
sizeof(uint8_t) *
size);
1270 uint32_t crc = 0xffffffff;
1273 static uint32_t crc32_table[256];
1275 static bool first_init;
1278 unsigned int i, j, c;
1279 for (i = 0; i < 256; i++) {
1281 for (c = i << 24, j = 8; j > 0; --j)
1282 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1289 while (nbytes > 0) {
1296 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *
buffer++) & 255];
1303 LOG_DEBUG(
"Calculating checksum done; checksum=0x%" PRIx32, crc);
#define ERROR_COMMAND_SYNTAX_ERROR
int fileio_seek(struct fileio *fileio, size_t position)
int fileio_read(struct fileio *fileio, size_t size, void *buffer, size_t *size_read)
int fileio_close(struct fileio *fileio)
int fileio_size(struct fileio *fileio, size_t *size)
FIX!!!!
int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
int fileio_feof(struct fileio *fileio)
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
#define ERROR_FILEIO_OPERATION_FAILED
static int image_ihex_buffer_complete_inner(struct image *image, char *lpsz_line, struct imagesection *section)
void image_close(struct image *image)
static int image_elf32_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
static int image_elf64_read_headers(struct image *image)
static int image_elf64_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
static int image_mot_buffer_complete_inner(struct image *image, char *lpsz_line, struct imagesection *section)
#define field64(elf, field)
static int image_ihex_buffer_complete(struct image *image)
Allocate memory dynamically instead of on the stack.
static int image_mot_buffer_complete(struct image *image)
Allocate memory dynamically instead of on the stack.
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
#define field32(elf, field)
static int autodetect_image_type(struct image *image, const char *url)
static int identify_image_type(struct image *image, const char *type_string, const char *url)
static int image_elf_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
#define field16(elf, field)
static int image_elf_read_headers(struct image *image)
int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
static int image_elf32_read_headers(struct image *image)
int image_add_section(struct image *image, target_addr_t base, uint32_t size, uint64_t flags, uint8_t const *data)
int image_open(struct image *image, const char *url, const char *type_string)
#define IMAGE_MAX_SECTIONS
#define ERROR_IMAGE_TYPE_UNKNOWN
#define ERROR_IMAGE_CHECKSUM
#define IMAGE_MEMORY_CACHE_SIZE
#define ERROR_IMAGE_TEMPORARILY_UNAVAILABLE
#define ERROR_IMAGE_FORMAT_ERROR
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
size_t size
Size of the control block search area.
bool openocd_is_shutdown_pending(void)
#define ERROR_SERVER_INTERRUPTED
unsigned int num_sections
struct imagesection * sections
target_addr_t base_address
struct target * get_target(const char *id)
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
static uint32_t be_to_h_u32(const uint8_t *buf)