From 237e894805dd757cc24029af1b4b1e824c51712b Mon Sep 17 00:00:00 2001 From: drath Date: Tue, 29 May 2007 11:23:42 +0000 Subject: - split fileio handling into fileio part and image handling - reworked etm/etb into a generic etm part with trace capture drivers (currently only etb supported) - added XScale debug handler binary to repository - added Thumb disassembling (thanks to Vincent Palatin for this patch) - added support for non-CFI compatible flashes to cfi driver (currently only SST39VFxxx devices supported) This checkin is experimental, not suitable for general use git-svn-id: svn://svn.berlios.de/openocd/trunk@155 b42882b7-edfa-0310-969c-e2dbd0fdcd60 --- src/helper/fileio.c | 274 +--------------------------------------------------- src/helper/fileio.h | 57 +++-------- 2 files changed, 17 insertions(+), 314 deletions(-) (limited to 'src/helper') diff --git a/src/helper/fileio.c b/src/helper/fileio.c index 648f1879..3a760cd8 100644 --- a/src/helper/fileio.c +++ b/src/helper/fileio.c @@ -98,7 +98,7 @@ int fileio_open_local(fileio_t *fileio) } } - if (fileio->pri_type == FILEIO_IMAGE) + if (fileio->type == FILEIO_BINARY) strcat(access, "b"); if (!(fileio_local->file = fopen(fileio->url, access))) @@ -120,126 +120,7 @@ int fileio_open_local(fileio_t *fileio) return ERROR_OK; } -//#ifdef FILEIO_BUFFER_COMPLETE_IHEX -int fileio_ihex_buffer_complete(fileio_t *fileio) -{ - fileio_image_t *image = fileio->pri_type_private; - fileio_ihex_t *ihex = fileio->sec_type_private; - u32 raw_bytes_read, raw_bytes; - int retval; - u32 full_address = image->base_address; - char *buffer = malloc(ihex->raw_size); - u32 cooked_bytes = 0x0; - - ihex->raw_size = fileio->size; - ihex->buffer = malloc(ihex->raw_size >> 1); - - if ((retval = fileio_dispatch_read(fileio, ihex->raw_size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK) - { - free(buffer); - ERROR("failed buffering IHEX file, read failed"); - return ERROR_FILEIO_OPERATION_FAILED; - } - - if (raw_bytes_read != ihex->raw_size) - { - free(buffer); - ERROR("failed buffering complete IHEX file, only partially read"); - return ERROR_FILEIO_OPERATION_FAILED; - } - - raw_bytes = 0x0; - while (raw_bytes < raw_bytes_read) - { - u32 count; - u32 address; - u32 record_type; - u32 checksum; - - if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3) - { - snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "invalid IHEX record"); - return ERROR_FILEIO_OPERATION_FAILED; - } - raw_bytes += 9; - - if (record_type == 0) - { - if ((full_address & 0xffff) != address) - { - free(buffer); - ERROR("can't handle non-linear IHEX file"); - snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't handle non-linear IHEX file"); - return ERROR_FILEIO_OPERATION_FAILED; - } - - while (count-- > 0) - { - sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]); - raw_bytes += 2; - cooked_bytes += 1; - full_address++; - } - } - else if (record_type == 1) - { - free(buffer); - fileio->size = cooked_bytes; - return ERROR_OK; - } - else if (record_type == 4) - { - u16 upper_address; - - sscanf(&buffer[raw_bytes], "%4hx", &upper_address); - raw_bytes += 4; - - if ((full_address >> 16) != upper_address) - { - free(buffer); - ERROR("can't handle non-linear IHEX file"); - snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't handle non-linear IHEX file"); - return ERROR_FILEIO_OPERATION_FAILED; - } - } - else if (record_type == 5) - { - u32 start_address; - - sscanf(&buffer[raw_bytes], "%8x", &start_address); - raw_bytes += 8; - - image->has_start_address = 1; - image->start_address = be_to_h_u32((u8*)&start_address); - } - else - { - free(buffer); - ERROR("unhandled IHEX record type: %i", record_type); - snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "unhandled IHEX record type: %i", record_type); - return ERROR_FILEIO_OPERATION_FAILED; - } - - sscanf(&buffer[raw_bytes], "%2x", &checksum); - raw_bytes += 2; - - /* consume new-line character(s) */ - if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r')) - raw_bytes++; - - if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r')) - raw_bytes++; - } - - free(buffer); - ERROR("premature end of IHEX file, no end-of-file record found"); - snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "premature end of IHEX file, no end-of-file record found"); - return ERROR_FILEIO_OPERATION_FAILED; -} -//#endif - -int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, - enum fileio_pri_type pri_type, void *pri_info, enum fileio_sec_type sec_type) +int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type) { int retval = ERROR_OK; char *resource_identifier = NULL; @@ -261,9 +142,8 @@ int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, fileio->location = FILEIO_LOCAL; } + fileio->type = type; fileio->access = access; - fileio->pri_type = pri_type; - fileio->sec_type = sec_type; fileio->url = strdup(url); switch (fileio->location) @@ -279,50 +159,6 @@ int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, if (retval != ERROR_OK) return retval; - if (fileio->pri_type == FILEIO_TEXT) - { - /* do nothing for now */ - return ERROR_OK; - } - else if (fileio->pri_type == FILEIO_IMAGE) - { - fileio_image_t *image = malloc(sizeof(fileio_image_t)); - fileio_image_t *image_info = pri_info; - - fileio->pri_type_private = image; - *image = *image_info; - - if (fileio->sec_type == FILEIO_PLAIN) - { - fileio->sec_type_private = NULL; - } - else if (fileio->sec_type == FILEIO_IHEX) - { - fileio_ihex_t *fileio_ihex; - - if (fileio->access != FILEIO_READ) - { - ERROR("can't write/append to a IHEX file"); - snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't write/append to a IHEX file"); - fileio_close(fileio); - return ERROR_FILEIO_OPERATION_FAILED; - } - - fileio_ihex = malloc(sizeof(fileio_ihex_t)); - fileio->sec_type_private = fileio_ihex; - - fileio_ihex->position = 0; - fileio_ihex->raw_size = fileio->size; -#ifdef FILEIO_BUFFER_COMPLETE_IHEX - if (fileio_ihex_buffer_complete(fileio) != ERROR_OK) - { - fileio_close(fileio); - return ERROR_FILEIO_OPERATION_FAILED; - } -#endif - } - } - return ERROR_OK; } @@ -369,29 +205,6 @@ int fileio_close(fileio_t *fileio) free(fileio->url); - if (fileio->pri_type == FILEIO_TEXT) - { - /* do nothing for now */ - } - else if (fileio->pri_type == FILEIO_IMAGE) - { - if (fileio->sec_type == FILEIO_PLAIN) - { - /* nothing special to do for plain binary */ - } - else if (fileio->sec_type == FILEIO_IHEX) - { - fileio_ihex_t *fileio_ihex = fileio->sec_type_private; - - if (fileio_ihex->buffer) - free(fileio_ihex->buffer); - - free(fileio->sec_type_private); - } - - free(fileio->pri_type_private); - } - return ERROR_OK; } @@ -432,7 +245,7 @@ int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read) return ERROR_OK; } -int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read) +int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read) { switch (fileio->location) { @@ -445,38 +258,6 @@ int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read) } } -int fileio_read_ihex(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read) -{ - fileio_ihex_t *fileio_ihex = fileio->sec_type_private; - - if ((fileio_ihex->position + size) > fileio->size) - { - /* don't read past the end of the file */ - size = (fileio->size - fileio_ihex->position); - } - -#ifdef FILEIO_BUFFER_COMPLETE_IHEX - memcpy(buffer, fileio_ihex->buffer + fileio_ihex->position, size); - *size_read = size; -#endif - - return ERROR_OK; -} - -int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read) -{ - if (fileio->sec_type == FILEIO_PLAIN) - { - return fileio_dispatch_read(fileio, size, buffer, size_read); - } - else if (fileio->sec_type == FILEIO_IHEX) - { - return fileio_read_ihex(fileio, size, buffer, size_read); - } - - return ERROR_OK; -} - int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written) { fileio_local_t *fileio_local = fileio->location_private; @@ -486,7 +267,7 @@ int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written return ERROR_OK; } -int fileio_dispatch_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written) +int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written) { switch (fileio->location) { @@ -499,48 +280,3 @@ int fileio_dispatch_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_writ return ERROR_OK; } - -int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written) -{ - int retval = ERROR_FILEIO_OPERATION_NOT_SUPPORTED; - if (fileio->sec_type == FILEIO_PLAIN) - { - retval = fileio_dispatch_write(fileio, size, buffer, size_written); - } - else if (fileio->sec_type == FILEIO_IHEX) - { - return ERROR_FILEIO_OPERATION_NOT_SUPPORTED; - } - - if (retval != ERROR_OK) - return retval; - - fileio->size += size; - - return ERROR_OK; -} - -int fileio_identify_image_type(enum fileio_sec_type *sec_type, char *type_string) -{ - if (type_string) - { - if (!strcmp(type_string, "bin")) - { - *sec_type = FILEIO_PLAIN; - } - else if (!strcmp(type_string, "ihex")) - { - *sec_type = FILEIO_IHEX; - } - else - { - return ERROR_FILEIO_RESOURCE_TYPE_UNKNOWN; - } - } - else - { - *sec_type = FILEIO_PLAIN; - } - - return ERROR_OK; -} diff --git a/src/helper/fileio.h b/src/helper/fileio.h index c047cb87..55e6f323 100644 --- a/src/helper/fileio.h +++ b/src/helper/fileio.h @@ -22,28 +22,20 @@ #define FILEIO_MAX_ERROR_STRING (128) -/* make buffering of complete intel-hex format files optional - * to account for resource-limited hosts - */ -#define FILEIO_BUFFER_COMPLETE_IHEX +#include "types.h" +#include +#include +#include +#include #include +#include +#include -enum fileio_pri_type -{ - FILEIO_TEXT = 0x1, - FILEIO_IMAGE = 0x2, -}; - -enum fileio_sec_type +enum fileio_type { - FILEIO_PLAIN = 0x10, - FILEIO_IHEX = 0x20, -/* - * Possible future enhancements: - * FILEIO_ELF, - * FILEIO_SRECORD, - */ + FILEIO_TEXT, + FILEIO_BINARY, }; enum fileio_location @@ -73,48 +65,23 @@ typedef struct fileio_s char *url; char error_str[FILEIO_MAX_ERROR_STRING]; long long size; - enum fileio_pri_type pri_type; - enum fileio_sec_type sec_type; + enum fileio_type type; enum fileio_location location; enum fileio_access access; void *location_private; - void *pri_type_private; - void *sec_type_private; } fileio_t; -typedef struct fileio_text_s -{ -} fileio_text_t; - -typedef struct fileio_image_s -{ - u32 base_address; - int has_start_address; - u32 start_address; -} fileio_image_t; - typedef struct fileio_local_s { FILE *file; struct stat file_stat; } fileio_local_t; -typedef struct fileio_ihex_s -{ - u32 position; - u32 raw_size; -#ifdef FILEIO_BUFFER_COMPLETE_IHEX - u8 *buffer; -#endif -} fileio_ihex_t; - -extern int fileio_identify_image_type(enum fileio_sec_type *sec_type, char *type_string); extern int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written); extern int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read); extern int fileio_seek(fileio_t *fileio, u32 position); extern int fileio_close(fileio_t *fileio); -extern int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, - enum fileio_pri_type pri_type, void *pri_info, enum fileio_sec_type sec_type); +extern int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type); #define ERROR_FILEIO_LOCATION_UNKNOWN (-1200) #define ERROR_FILEIO_NOT_FOUND (-1201) -- cgit v1.2.3