29 #define field16(elf, field) \
30 ((elf->endianness == ELFDATA2LSB) ? \
31 le_to_h_u16((uint8_t *)&field) : be_to_h_u16((uint8_t *)&field))
33 #define field32(elf, field) \
34 ((elf->endianness == ELFDATA2LSB) ? \
35 le_to_h_u32((uint8_t *)&field) : be_to_h_u32((uint8_t *)&field))
37 #define field64(elf, field) \
38 ((elf->endianness == ELFDATA2LSB) ? \
39 le_to_h_u64((uint8_t *)&field) : be_to_h_u64((uint8_t *)&field))
67 }
else if ((
buffer[0] ==
':')
78 }
else if ((
buffer[0] ==
'S')
94 if (!strcmp(type_string,
"bin"))
96 else if (!strcmp(type_string,
"ihex"))
98 else if (!strcmp(type_string,
"elf"))
100 else if (!strcmp(type_string,
"mem"))
102 else if (!strcmp(type_string,
"s19"))
104 else if (!strcmp(type_string,
"build"))
120 uint32_t full_address;
121 uint32_t cooked_bytes;
122 bool end_rec =
false;
133 ihex->
buffer = malloc(filesize >> 1);
147 uint32_t record_type;
149 uint8_t cal_checksum = 0;
150 size_t bytes_read = 0;
153 if ((lpsz_line[0] ==
'#') || (strlen(lpsz_line + strspn(lpsz_line,
"\n\t\r ")) == 0))
156 if (sscanf(&lpsz_line[bytes_read],
":%2" SCNx32
"%4" SCNx32
"%2" SCNx32, &
count,
157 &address, &record_type) != 3)
161 cal_checksum += (uint8_t)
count;
162 cal_checksum += (uint8_t)(address >> 8);
163 cal_checksum += (uint8_t)address;
164 cal_checksum += (uint8_t)record_type;
166 if (record_type == 0) {
167 if ((full_address & 0xffff) != address) {
176 LOG_ERROR(
"Too many sections found in IHEX file");
182 &ihex->
buffer[cooked_bytes];
185 (full_address & 0xffff0000) | address;
186 full_address = (full_address & 0xffff0000) | address;
189 while (
count-- > 0) {
191 sscanf(&lpsz_line[bytes_read],
"%2x", &value);
192 ihex->
buffer[cooked_bytes] = (uint8_t)value;
193 cal_checksum += (uint8_t)ihex->
buffer[cooked_bytes];
199 }
else if (record_type == 1) {
214 }
else if (record_type == 2) {
215 uint16_t upper_address;
217 sscanf(&lpsz_line[bytes_read],
"%4hx", &upper_address);
218 cal_checksum += (uint8_t)(upper_address >> 8);
219 cal_checksum += (uint8_t)upper_address;
222 if ((full_address >> 4) != upper_address) {
231 LOG_ERROR(
"Too many sections found in IHEX file");
237 &ihex->
buffer[cooked_bytes];
240 (full_address & 0xffff) | (upper_address << 4);
241 full_address = (full_address & 0xffff) | (upper_address << 4);
243 }
else if (record_type == 3) {
248 while (
count-- > 0) {
249 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &
dummy);
250 cal_checksum += (uint8_t)
dummy;
253 }
else if (record_type == 4) {
254 uint16_t upper_address;
256 sscanf(&lpsz_line[bytes_read],
"%4hx", &upper_address);
257 cal_checksum += (uint8_t)(upper_address >> 8);
258 cal_checksum += (uint8_t)upper_address;
261 if ((full_address >> 16) != upper_address) {
270 LOG_ERROR(
"Too many sections found in IHEX file");
276 &ihex->
buffer[cooked_bytes];
279 (full_address & 0xffff) | (upper_address << 16);
280 full_address = (full_address & 0xffff) | (upper_address << 16);
282 }
else if (record_type == 5) {
283 uint32_t start_address;
285 sscanf(&lpsz_line[bytes_read],
"%8" SCNx32, &start_address);
286 cal_checksum += (uint8_t)(start_address >> 24);
287 cal_checksum += (uint8_t)(start_address >> 16);
288 cal_checksum += (uint8_t)(start_address >> 8);
289 cal_checksum += (uint8_t)start_address;
295 LOG_ERROR(
"unhandled IHEX record type: %i", (
int)record_type);
299 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &checksum);
301 if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1)) {
303 LOG_ERROR(
"incorrect record checksum found in IHEX file");
309 LOG_WARNING(
"continuing after end-of-file record: %.40s", lpsz_line);
317 LOG_ERROR(
"premature end of IHEX file, no matching end-of-file record found");
328 char *lpsz_line = malloc(1023);
356 bool load_to_vaddr =
false;
360 LOG_ERROR(
"cannot seek to ELF file header, read failed");
367 LOG_ERROR(
"insufficient memory to perform operation");
373 LOG_ERROR(
"cannot read ELF file header, read failed");
377 LOG_ERROR(
"cannot read ELF file header, only partially read");
383 LOG_ERROR(
"invalid ELF file, no program headers");
389 LOG_ERROR(
"cannot seek to ELF program header table, read failed");
395 LOG_ERROR(
"insufficient memory to perform operation");
402 LOG_ERROR(
"cannot read ELF segment headers, read failed");
406 LOG_ERROR(
"cannot read ELF segment headers, only partially read");
419 LOG_ERROR(
"invalid ELF file, no loadable segments");
442 load_to_vaddr =
true;
447 LOG_ERROR(
"insufficient memory to perform operation");
481 bool load_to_vaddr =
false;
485 LOG_ERROR(
"cannot seek to ELF file header, read failed");
492 LOG_ERROR(
"insufficient memory to perform operation");
498 LOG_ERROR(
"cannot read ELF file header, read failed");
502 LOG_ERROR(
"cannot read ELF file header, only partially read");
508 LOG_ERROR(
"invalid ELF file, no program headers");
514 LOG_ERROR(
"cannot seek to ELF program header table, read failed");
520 LOG_ERROR(
"insufficient memory to perform operation");
527 LOG_ERROR(
"cannot read ELF segment headers, read failed");
531 LOG_ERROR(
"cannot read ELF segment headers, only partially read");
544 LOG_ERROR(
"invalid ELF file, no loadable segments");
567 load_to_vaddr =
true;
572 LOG_ERROR(
"insufficient memory to perform operation");
608 LOG_ERROR(
"cannot read ELF file header, read failed");
612 LOG_ERROR(
"cannot read ELF file header, only partially read");
617 LOG_ERROR(
"invalid ELF file, bad magic number");
624 LOG_ERROR(
"invalid ELF file, unknown endianness setting");
640 LOG_ERROR(
"invalid ELF file, only 32/64 bit ELF files are supported");
654 size_t read_size, really_read;
670 LOG_ERROR(
"cannot find ELF segment content, seek failed");
675 LOG_ERROR(
"cannot read ELF segment content, read failed");
679 *size_read += read_size;
697 size_t read_size, really_read;
713 LOG_ERROR(
"cannot find ELF segment content, seek failed");
718 LOG_ERROR(
"cannot read ELF segment content, read failed");
722 *size_read += read_size;
752 uint32_t full_address;
753 uint32_t cooked_bytes;
754 bool end_rec =
false;
765 mot->
buffer = malloc(filesize >> 1);
779 uint32_t record_type;
781 uint8_t cal_checksum = 0;
782 uint32_t bytes_read = 0;
785 if ((lpsz_line[0] ==
'#') || (strlen(lpsz_line + strspn(lpsz_line,
"\n\t\r ")) == 0))
789 if (sscanf(&lpsz_line[bytes_read],
"S%1" SCNx32
"%2" SCNx32, &record_type,
794 cal_checksum += (uint8_t)
count;
799 if (record_type == 0) {
803 while (
count-- > 0) {
804 sscanf(&lpsz_line[bytes_read],
"%2x", &value);
805 cal_checksum += (uint8_t)value;
808 }
else if (record_type >= 1 && record_type <= 3) {
809 switch (record_type) {
812 sscanf(&lpsz_line[bytes_read],
"%4" SCNx32, &address);
813 cal_checksum += (uint8_t)(address >> 8);
814 cal_checksum += (uint8_t)address;
821 sscanf(&lpsz_line[bytes_read],
"%6" SCNx32, &address);
822 cal_checksum += (uint8_t)(address >> 16);
823 cal_checksum += (uint8_t)(address >> 8);
824 cal_checksum += (uint8_t)address;
831 sscanf(&lpsz_line[bytes_read],
"%8" SCNx32, &address);
832 cal_checksum += (uint8_t)(address >> 24);
833 cal_checksum += (uint8_t)(address >> 16);
834 cal_checksum += (uint8_t)(address >> 8);
835 cal_checksum += (uint8_t)address;
842 if (full_address != address) {
852 &mot->
buffer[cooked_bytes];
855 full_address = address;
858 while (
count-- > 0) {
860 sscanf(&lpsz_line[bytes_read],
"%2x", &value);
861 mot->
buffer[cooked_bytes] = (uint8_t)value;
862 cal_checksum += (uint8_t)mot->
buffer[cooked_bytes];
868 }
else if (record_type == 5 || record_type == 6) {
872 while (
count-- > 0) {
873 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &
dummy);
874 cal_checksum += (uint8_t)
dummy;
877 }
else if (record_type >= 7 && record_type <= 9) {
893 LOG_ERROR(
"unhandled S19 record type: %i", (
int)(record_type));
898 sscanf(&lpsz_line[bytes_read],
"%2" SCNx32, &checksum);
899 cal_checksum += (uint8_t)checksum;
901 if (cal_checksum != 0xFF) {
903 LOG_ERROR(
"incorrect record checksum found in S19 file");
909 LOG_WARNING(
"continuing after end-of-file record: %.40s", lpsz_line);
917 LOG_ERROR(
"premature end of S19 file, no matching end-of-file record found");
928 char *lpsz_line = malloc(1023);
989 "failed buffering IHEX image, check server output for additional information");
1011 LOG_ERROR(
"target '%s' not defined", url);
1040 "failed buffering S19 image, check server output for additional information");
1077 "read past end of section: 0x%8.8" TARGET_PRIxADDR " + 0x%8.8" PRIx32
" > 0x%8.8" PRIx32
"",
1113 while ((
size - *size_read) > 0) {
1114 uint32_t size_in_cache;
1137 memcpy(
buffer + *size_read,
1139 (size_in_cache >
size) ?
size : size_in_cache
1142 *size_read += (size_in_cache >
size) ?
size : size_in_cache;
1143 address += (size_in_cache >
size) ?
size : size_in_cache;
1191 section->
private = malloc(
sizeof(uint8_t) *
size);
1256 uint32_t crc = 0xffffffff;
1259 static uint32_t crc32_table[256];
1261 static bool first_init;
1264 unsigned int i, j, c;
1265 for (i = 0; i < 256; i++) {
1267 for (c = i << 24, j = 8; j > 0; --j)
1268 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1275 while (nbytes > 0) {
1282 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *
buffer++) & 255];
1287 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.
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)