X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Ftarget%2Fimage.c;h=3fac057182f266fd803df2974d88fa62e2d12828;hb=53883c27849b6c09e30ff986f87aab2b4be2bb1e;hp=dbb1c2ab3a41791de683a42bdf760bb209888b05;hpb=995326b6000773efd454e308d487dec0b9f564b5;p=openocd.git diff --git a/src/target/image.c b/src/target/image.c index dbb1c2ab3a..3fac057182 100644 --- a/src/target/image.c +++ b/src/target/image.c @@ -55,20 +55,14 @@ static int autodetect_image_type(image_t *image, char *url) /* read the first 4 bytes of image */ if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK) { - snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot open image: %s", fileio.error_str); - ERROR(image->error_str); return retval; } if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != ERROR_OK) { - snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image header: %s", fileio.error_str); - ERROR(image->error_str); return ERROR_FILEIO_OPERATION_FAILED; } if (read_bytes != 9) { - snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image, only partially read"); - ERROR(image->error_str); return ERROR_FILEIO_OPERATION_FAILED; } fileio_close(&fileio); @@ -231,8 +225,7 @@ int image_ihex_buffer_complete(image_t *image) for (i = 0; i < image->num_sections; i++) { image->sections[i].private = section[i].private; - image->sections[i].base_address = section[i].base_address + - ((image->base_address_set) ? image->base_address : 0); + image->sections[i].base_address = section[i].base_address; image->sections[i].size = section[i].size; image->sections[i].flags = section[i].flags; } @@ -266,6 +259,19 @@ int image_ihex_buffer_complete(image_t *image) full_address = (full_address & 0xffff) | (upper_address << 4); } } + else if (record_type == 3) /* Start Segment Address Record */ + { + u32 dummy; + + /* "Start Segment Address Record" will not be supported */ + /* but we must consume it, and do not create an error. */ + while (count-- > 0) + { + sscanf(&lpszLine[bytes_read], "%2x", &dummy); + cal_checksum += (u8)dummy; + bytes_read += 2; + } + } else if (record_type == 4) /* Extended Linear Address Record */ { u16 upper_address; @@ -399,7 +405,7 @@ int image_elf_read_headers(image_t *image) { if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0)) { - image->sections[j].size = field32(elf,elf->segments[i].p_memsz); + image->sections[j].size = field32(elf,elf->segments[i].p_filesz); image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr); image->sections[j].private = &elf->segments[i]; image->sections[j].flags = field32(elf,elf->segments[i].p_flags); @@ -450,15 +456,6 @@ int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 if (!size) return ERROR_OK; } - /* if there is remaining zeroed area in current segment */ - if (offsetp_memsz)) - { - /* fill zeroed part (BSS) of the segment */ - read_size = MIN(size, field32(elf,segment->p_memsz)-offset); - DEBUG("zero fill: size = 0x%x",read_size); - memset(buffer,0,read_size); - *size_read += read_size; - } return ERROR_OK; } @@ -579,6 +576,18 @@ int image_mot_buffer_complete(image_t *image) full_address++; } } + else if (record_type == 5) + { + /* S5 is the data count record, we ignore it */ + u32 dummy; + + while (count-- > 0) + { + sscanf(&lpszLine[bytes_read], "%2x", &dummy); + cal_checksum += (u8)dummy; + bytes_read += 2; + } + } else if (record_type >= 7 && record_type <= 9) { /* S7, S8, S9 - ending records for 32, 24 and 16bit */ @@ -589,8 +598,7 @@ int image_mot_buffer_complete(image_t *image) for (i = 0; i < image->num_sections; i++) { image->sections[i].private = section[i].private; - image->sections[i].base_address = section[i].base_address + - ((image->base_address_set) ? image->base_address : 0); + image->sections[i].base_address = section[i].base_address; image->sections[i].size = section[i].size; image->sections[i].flags = section[i].flags; } @@ -637,8 +645,6 @@ int image_open(image_t *image, char *url, char *type_string) if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK) { - strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING); - ERROR(image->error_str); return retval; } @@ -647,11 +653,6 @@ int image_open(image_t *image, char *url, char *type_string) image->sections[0].base_address = 0x0; image->sections[0].size = image_binary->fileio.size; image->sections[0].flags = 0; - - if (image->base_address_set == 1) - image->sections[0].base_address = image->base_address; - - return ERROR_OK; } else if (image->type == IMAGE_IHEX) { @@ -661,16 +662,12 @@ int image_open(image_t *image, char *url, char *type_string) if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK) { - strncpy(image->error_str, image_ihex->fileio.error_str, IMAGE_MAX_ERROR_STRING); - ERROR(image->error_str); return retval; } if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK) { - snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, - "failed buffering IHEX image, check daemon output for additional information"); - ERROR(image->error_str); + ERROR("failed buffering IHEX image, check daemon output for additional information"); fileio_close(&image_ihex->fileio); return retval; } @@ -683,16 +680,11 @@ int image_open(image_t *image, char *url, char *type_string) if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK) { - strncpy(image->error_str, image_elf->fileio.error_str, IMAGE_MAX_ERROR_STRING); - ERROR(image->error_str); return retval; } if ((retval = image_elf_read_headers(image)) != ERROR_OK) { - snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, - "failed to read ELF headers, check daemon output for additional information"); - ERROR(image->error_str); fileio_close(&image_elf->fileio); return retval; } @@ -721,16 +713,12 @@ int image_open(image_t *image, char *url, char *type_string) if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK) { - strncpy(image->error_str, image_mot->fileio.error_str, IMAGE_MAX_ERROR_STRING); - ERROR(image->error_str); return retval; } if ((retval = image_mot_buffer_complete(image)) != ERROR_OK) { - snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, - "failed buffering S19 image, check daemon output for additional information"); - ERROR(image->error_str); + ERROR("failed buffering S19 image, check daemon output for additional information"); fileio_close(&image_mot->fileio); return retval; } @@ -741,6 +729,21 @@ int image_open(image_t *image, char *url, char *type_string) image->sections = NULL; image->type_private = NULL; } + + if (image->base_address_set) + { + /* relocate */ + int section; + for (section=0; section < image->num_sections; section++) + { + image->sections[section].base_address+=image->base_address; + } + /* we're done relocating. The two statements below are mainly + * for documenation purposes: stop anyone from empirically + * thinking they should use these values henceforth. */ + image->base_address=0; + image->base_address_set=0; + } return retval; }; @@ -768,14 +771,12 @@ int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *bu /* seek to offset */ if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK) { - strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING); return retval; } /* return requested bytes */ if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK) { - strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING); return retval; } } @@ -783,7 +784,6 @@ int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *bu { memcpy(buffer, (u8*)image->sections[section].private + offset, size); *size_read = size; - image->error_str[0] = '\0'; return ERROR_OK; } @@ -813,6 +813,7 @@ int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *bu IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK) { free(image_memory->cache); + image_memory->cache = NULL; return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE; } image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1); @@ -833,7 +834,6 @@ int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *bu { memcpy(buffer, (u8*)image->sections[section].private + offset, size); *size_read = size; - image->error_str[0] = '\0'; return ERROR_OK; } @@ -841,7 +841,6 @@ int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *bu { memcpy(buffer, (u8*)image->sections[section].private + offset, size); *size_read = size; - image->error_str[0] = '\0'; return ERROR_OK; } @@ -901,7 +900,10 @@ int image_close(image_t *image) fileio_close(&image_ihex->fileio); if (image_ihex->buffer) + { free(image_ihex->buffer); + image_ihex->buffer = NULL; + } } else if (image->type == IMAGE_ELF) { @@ -910,17 +912,26 @@ int image_close(image_t *image) fileio_close(&image_elf->fileio); if (image_elf->header) + { free(image_elf->header); + image_elf->header = NULL; + } if (image_elf->segments) + { free(image_elf->segments); + image_elf->segments = NULL; + } } else if (image->type == IMAGE_MEMORY) { image_memory_t *image_memory = image->type_private; if (image_memory->cache) + { free(image_memory->cache); + image_memory->cache = NULL; + } } else if (image->type == IMAGE_SRECORD) { @@ -929,7 +940,10 @@ int image_close(image_t *image) fileio_close(&image_mot->fileio); if (image_mot->buffer) + { free(image_mot->buffer); + image_mot->buffer = NULL; + } } else if (image->type == IMAGE_BUILDER) { @@ -938,14 +952,52 @@ int image_close(image_t *image) for (i = 0; i < image->num_sections; i++) { free(image->sections[i].private); + image->sections[i].private = NULL; } } if (image->type_private) + { free(image->type_private); + image->type_private = NULL; + } if (image->sections) + { free(image->sections); + image->sections = NULL; + } + + return ERROR_OK; +} + +static u32 crc32_table[256] = {0, 0}; + +int image_calculate_checksum(u8* buffer, u32 nbytes, u32* checksum) +{ + u32 crc = 0xffffffff; + + if (!crc32_table[1]) + { + /* Initialize the CRC table and the decoding table. */ + int i, j; + unsigned int c; + for (i = 0; i < 256; i++) + { + /* as per gdb */ + for (c = i << 24, j = 8; j > 0; --j) + c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); + crc32_table[i] = c; + } + } + while (nbytes--) + { + /* as per gdb */ + crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255]; + } + + *checksum = crc; return ERROR_OK; } +