helper/fileio: Remove nested struct
Change-Id: I1a3afbddcf950689da58e0df8850a05f558d7879 Signed-off-by: Marc Schink <openocd-dev@marcschink.de> Reviewed-on: http://openocd.zylin.com/3222 Tested-by: jenkins Reviewed-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
This commit is contained in:
parent
7c957b601f
commit
73b676c2fd
|
@ -749,7 +749,7 @@ COMMAND_HANDLER(mg_write_cmd)
|
|||
{
|
||||
uint32_t address, cnt, res, i;
|
||||
uint8_t *buffer;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
int ret;
|
||||
|
||||
if (CMD_ARGC != 3)
|
||||
|
@ -764,12 +764,12 @@ COMMAND_HANDLER(mg_write_cmd)
|
|||
size_t filesize;
|
||||
buffer = malloc(MG_FILEIO_CHUNK);
|
||||
if (!buffer) {
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return ERROR_FAIL;
|
||||
}
|
||||
int retval = fileio_size(&fileio, &filesize);
|
||||
int retval = fileio_size(fileio, &filesize);
|
||||
if (retval != ERROR_OK) {
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
free(buffer);
|
||||
return retval;
|
||||
}
|
||||
|
@ -782,7 +782,7 @@ COMMAND_HANDLER(mg_write_cmd)
|
|||
|
||||
size_t buf_cnt;
|
||||
for (i = 0; i < cnt; i++) {
|
||||
ret = fileio_read(&fileio, MG_FILEIO_CHUNK, buffer, &buf_cnt);
|
||||
ret = fileio_read(fileio, MG_FILEIO_CHUNK, buffer, &buf_cnt);
|
||||
if (ret != ERROR_OK)
|
||||
goto mg_write_cmd_err;
|
||||
ret = mg_mflash_write(address, buffer, MG_FILEIO_CHUNK);
|
||||
|
@ -792,7 +792,7 @@ COMMAND_HANDLER(mg_write_cmd)
|
|||
}
|
||||
|
||||
if (res) {
|
||||
ret = fileio_read(&fileio, res, buffer, &buf_cnt);
|
||||
ret = fileio_read(fileio, res, buffer, &buf_cnt);
|
||||
if (ret != ERROR_OK)
|
||||
goto mg_write_cmd_err;
|
||||
ret = mg_mflash_write(address, buffer, res);
|
||||
|
@ -807,13 +807,13 @@ COMMAND_HANDLER(mg_write_cmd)
|
|||
}
|
||||
|
||||
free(buffer);
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
return ERROR_OK;
|
||||
|
||||
mg_write_cmd_err:
|
||||
free(buffer);
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -822,7 +822,7 @@ COMMAND_HANDLER(mg_dump_cmd)
|
|||
{
|
||||
uint32_t address, size, cnt, res, i;
|
||||
uint8_t *buffer;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
int ret;
|
||||
|
||||
if (CMD_ARGC != 4)
|
||||
|
@ -837,7 +837,7 @@ COMMAND_HANDLER(mg_dump_cmd)
|
|||
|
||||
buffer = malloc(MG_FILEIO_CHUNK);
|
||||
if (!buffer) {
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return ERROR_FAIL;
|
||||
}
|
||||
|
||||
|
@ -852,7 +852,7 @@ COMMAND_HANDLER(mg_dump_cmd)
|
|||
ret = mg_mflash_read(address, buffer, MG_FILEIO_CHUNK);
|
||||
if (ret != ERROR_OK)
|
||||
goto mg_dump_cmd_err;
|
||||
ret = fileio_write(&fileio, MG_FILEIO_CHUNK, buffer, &size_written);
|
||||
ret = fileio_write(fileio, MG_FILEIO_CHUNK, buffer, &size_written);
|
||||
if (ret != ERROR_OK)
|
||||
goto mg_dump_cmd_err;
|
||||
address += MG_FILEIO_CHUNK;
|
||||
|
@ -862,7 +862,7 @@ COMMAND_HANDLER(mg_dump_cmd)
|
|||
ret = mg_mflash_read(address, buffer, res);
|
||||
if (ret != ERROR_OK)
|
||||
goto mg_dump_cmd_err;
|
||||
ret = fileio_write(&fileio, res, buffer, &size_written);
|
||||
ret = fileio_write(fileio, res, buffer, &size_written);
|
||||
if (ret != ERROR_OK)
|
||||
goto mg_dump_cmd_err;
|
||||
}
|
||||
|
@ -875,13 +875,13 @@ COMMAND_HANDLER(mg_dump_cmd)
|
|||
}
|
||||
|
||||
free(buffer);
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
return ERROR_OK;
|
||||
|
||||
mg_dump_cmd_err:
|
||||
free(buffer);
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ int nand_fileio_start(struct command_context *cmd_ctx,
|
|||
int nand_fileio_cleanup(struct nand_fileio_state *state)
|
||||
{
|
||||
if (state->file_opened)
|
||||
fileio_close(&state->fileio);
|
||||
fileio_close(state->fileio);
|
||||
|
||||
if (state->oob) {
|
||||
free(state->oob);
|
||||
|
@ -169,7 +169,7 @@ COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
|
|||
|
||||
if (!need_size) {
|
||||
size_t filesize;
|
||||
retval = fileio_size(&state->fileio, &filesize);
|
||||
retval = fileio_size(state->fileio, &filesize);
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
state->size = filesize;
|
||||
|
@ -190,7 +190,7 @@ int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
|
|||
size_t one_read;
|
||||
|
||||
if (NULL != s->page) {
|
||||
fileio_read(&s->fileio, s->page_size, s->page, &one_read);
|
||||
fileio_read(s->fileio, s->page_size, s->page, &one_read);
|
||||
if (one_read < s->page_size)
|
||||
memset(s->page + one_read, 0xff, s->page_size - one_read);
|
||||
total_read += one_read;
|
||||
|
@ -219,7 +219,7 @@ int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
|
|||
ecc += 10;
|
||||
}
|
||||
} else if (NULL != s->oob) {
|
||||
fileio_read(&s->fileio, s->oob_size, s->oob, &one_read);
|
||||
fileio_read(s->fileio, s->oob_size, s->oob, &one_read);
|
||||
if (one_read < s->oob_size)
|
||||
memset(s->oob + one_read, 0xff, s->oob_size - one_read);
|
||||
total_read += one_read;
|
||||
|
|
|
@ -37,7 +37,7 @@ struct nand_fileio_state {
|
|||
const int *eccpos;
|
||||
|
||||
bool file_opened;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
|
||||
struct duration bench;
|
||||
};
|
||||
|
|
|
@ -360,16 +360,16 @@ COMMAND_HANDLER(handle_nand_dump_command)
|
|||
}
|
||||
|
||||
if (NULL != s.page)
|
||||
fileio_write(&s.fileio, s.page_size, s.page, &size_written);
|
||||
fileio_write(s.fileio, s.page_size, s.page, &size_written);
|
||||
|
||||
if (NULL != s.oob)
|
||||
fileio_write(&s.fileio, s.oob_size, s.oob, &size_written);
|
||||
fileio_write(s.fileio, s.oob_size, s.oob, &size_written);
|
||||
|
||||
s.size -= nand->page_size;
|
||||
s.address += nand->page_size;
|
||||
}
|
||||
|
||||
retval = fileio_size(&s.fileio, &filesize);
|
||||
retval = fileio_size(s.fileio, &filesize);
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
|
||||
|
|
|
@ -556,7 +556,7 @@ COMMAND_HANDLER(lpc2900_handle_read_custom_command)
|
|||
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
|
||||
|
||||
/* Try and open the file */
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
const char *filename = CMD_ARGV[1];
|
||||
int ret = fileio_open(&fileio, filename, FILEIO_WRITE, FILEIO_BINARY);
|
||||
if (ret != ERROR_OK) {
|
||||
|
@ -565,14 +565,14 @@ COMMAND_HANDLER(lpc2900_handle_read_custom_command)
|
|||
}
|
||||
|
||||
size_t nwritten;
|
||||
ret = fileio_write(&fileio, sizeof(customer), customer, &nwritten);
|
||||
ret = fileio_write(fileio, sizeof(customer), customer, &nwritten);
|
||||
if (ret != ERROR_OK) {
|
||||
LOG_ERROR("Write operation to file %s failed", filename);
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return ret;
|
||||
}
|
||||
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
|
|
@ -555,7 +555,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
|
|||
{
|
||||
uint32_t offset;
|
||||
uint8_t *buffer;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
|
||||
if (CMD_ARGC != 3)
|
||||
return ERROR_COMMAND_SYNTAX_ERROR;
|
||||
|
@ -574,22 +574,22 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
|
|||
return ERROR_OK;
|
||||
|
||||
size_t filesize;
|
||||
retval = fileio_size(&fileio, &filesize);
|
||||
retval = fileio_size(fileio, &filesize);
|
||||
if (retval != ERROR_OK) {
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return retval;
|
||||
}
|
||||
|
||||
buffer = malloc(filesize);
|
||||
if (buffer == NULL) {
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
LOG_ERROR("Out of memory");
|
||||
return ERROR_FAIL;
|
||||
}
|
||||
size_t buf_cnt;
|
||||
if (fileio_read(&fileio, filesize, buffer, &buf_cnt) != ERROR_OK) {
|
||||
if (fileio_read(fileio, filesize, buffer, &buf_cnt) != ERROR_OK) {
|
||||
free(buffer);
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
|
@ -605,7 +605,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
|
|||
duration_elapsed(&bench), duration_kbps(&bench, filesize));
|
||||
}
|
||||
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
@ -614,7 +614,7 @@ COMMAND_HANDLER(handle_flash_read_bank_command)
|
|||
{
|
||||
uint32_t offset;
|
||||
uint8_t *buffer;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
uint32_t length;
|
||||
size_t written;
|
||||
|
||||
|
@ -652,8 +652,8 @@ COMMAND_HANDLER(handle_flash_read_bank_command)
|
|||
return retval;
|
||||
}
|
||||
|
||||
retval = fileio_write(&fileio, length, buffer, &written);
|
||||
fileio_close(&fileio);
|
||||
retval = fileio_write(fileio, length, buffer, &written);
|
||||
fileio_close(fileio);
|
||||
free(buffer);
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("Could not write file");
|
||||
|
@ -674,7 +674,7 @@ COMMAND_HANDLER(handle_flash_verify_bank_command)
|
|||
{
|
||||
uint32_t offset;
|
||||
uint8_t *buffer_file, *buffer_flash;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
size_t read_cnt;
|
||||
size_t filesize;
|
||||
int differ;
|
||||
|
@ -698,21 +698,21 @@ COMMAND_HANDLER(handle_flash_verify_bank_command)
|
|||
return retval;
|
||||
}
|
||||
|
||||
retval = fileio_size(&fileio, &filesize);
|
||||
retval = fileio_size(fileio, &filesize);
|
||||
if (retval != ERROR_OK) {
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return retval;
|
||||
}
|
||||
|
||||
buffer_file = malloc(filesize);
|
||||
if (buffer_file == NULL) {
|
||||
LOG_ERROR("Out of memory");
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
return ERROR_FAIL;
|
||||
}
|
||||
|
||||
retval = fileio_read(&fileio, filesize, buffer_file, &read_cnt);
|
||||
fileio_close(&fileio);
|
||||
retval = fileio_read(fileio, filesize, buffer_file, &read_cnt);
|
||||
fileio_close(fileio);
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("File read failure");
|
||||
free(buffer_file);
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
#include "configuration.h"
|
||||
#include "fileio.h"
|
||||
|
||||
struct fileio_internal {
|
||||
struct fileio {
|
||||
char *url;
|
||||
size_t size;
|
||||
enum fileio_type type;
|
||||
|
@ -40,8 +40,22 @@ struct fileio_internal {
|
|||
FILE *file;
|
||||
};
|
||||
|
||||
static inline int fileio_close_local(struct fileio_internal *fileio);
|
||||
static inline int fileio_open_local(struct fileio_internal *fileio)
|
||||
static inline int fileio_close_local(struct fileio *fileio)
|
||||
{
|
||||
int retval = fclose(fileio->file);
|
||||
if (retval != 0) {
|
||||
if (retval == EBADF)
|
||||
LOG_ERROR("BUG: fileio->file not a valid file descriptor");
|
||||
else
|
||||
LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
|
||||
|
||||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
static inline int fileio_open_local(struct fileio *fileio)
|
||||
{
|
||||
char file_access[4];
|
||||
ssize_t file_size;
|
||||
|
@ -104,69 +118,49 @@ static inline int fileio_open_local(struct fileio_internal *fileio)
|
|||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int fileio_open(struct fileio *fileio_p,
|
||||
const char *url,
|
||||
enum fileio_access access_type,
|
||||
enum fileio_type type)
|
||||
int fileio_open(struct fileio **fileio, const char *url,
|
||||
enum fileio_access access_type, enum fileio_type type)
|
||||
{
|
||||
int retval;
|
||||
struct fileio_internal *fileio;
|
||||
struct fileio *tmp;
|
||||
|
||||
fileio = malloc(sizeof(struct fileio_internal));
|
||||
tmp = malloc(sizeof(struct fileio));
|
||||
|
||||
fileio->type = type;
|
||||
fileio->access = access_type;
|
||||
fileio->url = strdup(url);
|
||||
tmp->type = type;
|
||||
tmp->access = access_type;
|
||||
tmp->url = strdup(url);
|
||||
|
||||
retval = fileio_open_local(fileio);
|
||||
retval = fileio_open_local(tmp);
|
||||
|
||||
if (retval != ERROR_OK) {
|
||||
free(fileio->url);
|
||||
free(fileio);
|
||||
free(tmp->url);
|
||||
free(tmp);
|
||||
return retval;
|
||||
}
|
||||
|
||||
fileio_p->fp = fileio;
|
||||
*fileio = tmp;
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
static inline int fileio_close_local(struct fileio_internal *fileio)
|
||||
{
|
||||
int retval = fclose(fileio->file);
|
||||
if (retval != 0) {
|
||||
if (retval == EBADF)
|
||||
LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
|
||||
else
|
||||
LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
|
||||
|
||||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int fileio_close(struct fileio *fileio_p)
|
||||
int fileio_close(struct fileio *fileio)
|
||||
{
|
||||
int retval;
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
|
||||
retval = fileio_close_local(fileio);
|
||||
|
||||
free(fileio->url);
|
||||
fileio->url = NULL;
|
||||
|
||||
free(fileio);
|
||||
fileio_p->fp = NULL;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int fileio_seek(struct fileio *fileio_p, size_t position)
|
||||
int fileio_seek(struct fileio *fileio, size_t position)
|
||||
{
|
||||
int retval;
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
|
||||
retval = fseek(fileio->file, position, SEEK_SET);
|
||||
|
||||
if (retval != 0) {
|
||||
LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
|
||||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
|
@ -175,36 +169,40 @@ int fileio_seek(struct fileio *fileio_p, size_t position)
|
|||
return ERROR_OK;
|
||||
}
|
||||
|
||||
static int fileio_local_read(struct fileio_internal *fileio,
|
||||
size_t size, void *buffer, size_t *size_read)
|
||||
static int fileio_local_read(struct fileio *fileio, size_t size, void *buffer,
|
||||
size_t *size_read)
|
||||
{
|
||||
ssize_t retval = fread(buffer, 1, size, fileio->file);
|
||||
ssize_t retval;
|
||||
|
||||
retval = fread(buffer, 1, size, fileio->file);
|
||||
*size_read = (retval >= 0) ? retval : 0;
|
||||
|
||||
return (retval < 0) ? retval : ERROR_OK;
|
||||
}
|
||||
|
||||
int fileio_read(struct fileio *fileio_p, size_t size, void *buffer,
|
||||
size_t *size_read)
|
||||
int fileio_read(struct fileio *fileio, size_t size, void *buffer,
|
||||
size_t *size_read)
|
||||
{
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
return fileio_local_read(fileio, size, buffer, size_read);
|
||||
}
|
||||
|
||||
int fileio_read_u32(struct fileio *fileio_p, uint32_t *data)
|
||||
int fileio_read_u32(struct fileio *fileio, uint32_t *data)
|
||||
{
|
||||
int retval;
|
||||
uint8_t buf[4];
|
||||
size_t size_read;
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
|
||||
|
||||
retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
|
||||
|
||||
if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
|
||||
retval = -EIO;
|
||||
if (ERROR_OK == retval)
|
||||
*data = be_to_h_u32(buf);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int fileio_local_fgets(struct fileio_internal *fileio,
|
||||
size_t size, void *buffer)
|
||||
static int fileio_local_fgets(struct fileio *fileio, size_t size, void *buffer)
|
||||
{
|
||||
if (fgets(buffer, size, fileio->file) == NULL)
|
||||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
|
@ -212,36 +210,44 @@ static int fileio_local_fgets(struct fileio_internal *fileio,
|
|||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int fileio_fgets(struct fileio *fileio_p, size_t size, void *buffer)
|
||||
int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
|
||||
{
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
return fileio_local_fgets(fileio, size, buffer);
|
||||
}
|
||||
|
||||
static int fileio_local_write(struct fileio_internal *fileio,
|
||||
size_t size, const void *buffer, size_t *size_written)
|
||||
static int fileio_local_write(struct fileio *fileio, size_t size,
|
||||
const void *buffer, size_t *size_written)
|
||||
{
|
||||
ssize_t retval = fwrite(buffer, 1, size, fileio->file);
|
||||
ssize_t retval;
|
||||
|
||||
retval = fwrite(buffer, 1, size, fileio->file);
|
||||
*size_written = (retval >= 0) ? retval : 0;
|
||||
|
||||
return (retval < 0) ? retval : ERROR_OK;
|
||||
}
|
||||
|
||||
int fileio_write(struct fileio *fileio_p,
|
||||
size_t size, const void *buffer, size_t *size_written)
|
||||
int fileio_write(struct fileio *fileio, size_t size, const void *buffer,
|
||||
size_t *size_written)
|
||||
{
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
int retval = fileio_local_write(fileio, size, buffer, size_written);
|
||||
int retval;
|
||||
|
||||
retval = fileio_local_write(fileio, size, buffer, size_written);
|
||||
|
||||
if (retval == ERROR_OK)
|
||||
fileio->size += *size_written;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int fileio_write_u32(struct fileio *fileio_p, uint32_t data)
|
||||
int fileio_write_u32(struct fileio *fileio, uint32_t data)
|
||||
{
|
||||
int retval;
|
||||
uint8_t buf[4];
|
||||
h_u32_to_be(buf, data);
|
||||
size_t size_written;
|
||||
int retval = fileio_write(fileio_p, 4, buf, &size_written);
|
||||
|
||||
retval = fileio_write(fileio, 4, buf, &size_written);
|
||||
|
||||
if (ERROR_OK == retval && size_written != sizeof(uint32_t))
|
||||
retval = -EIO;
|
||||
|
||||
|
@ -257,9 +263,8 @@ int fileio_write_u32(struct fileio *fileio_p, uint32_t data)
|
|||
* Avoiding the seek on startup opens up for using streams.
|
||||
*
|
||||
*/
|
||||
int fileio_size(struct fileio *fileio_p, size_t *size)
|
||||
int fileio_size(struct fileio *fileio, size_t *size)
|
||||
{
|
||||
struct fileio_internal *fileio = fileio_p->fp;
|
||||
*size = fileio->size;
|
||||
|
||||
return ERROR_OK;
|
||||
|
|
|
@ -43,13 +43,10 @@ enum fileio_access {
|
|||
FILEIO_APPENDREAD, /* open for writing, position at end, allow reading */
|
||||
};
|
||||
|
||||
struct fileio {
|
||||
/* The structure is opaque */
|
||||
struct fileio_internal *fp;
|
||||
};
|
||||
struct fileio;
|
||||
|
||||
int fileio_open(struct fileio *fileio,
|
||||
const char *url, enum fileio_access access_type, enum fileio_type type);
|
||||
int fileio_open(struct fileio **fileio, const char *url,
|
||||
enum fileio_access access_type, enum fileio_type type);
|
||||
int fileio_close(struct fileio *fileio);
|
||||
|
||||
int fileio_seek(struct fileio *fileio, size_t position);
|
||||
|
|
|
@ -3063,7 +3063,7 @@ COMMAND_HANDLER(handle_gdb_save_tdesc_command)
|
|||
|
||||
tdesc_length = strlen(tdesc);
|
||||
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
size_t size_written;
|
||||
|
||||
char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
|
||||
|
@ -3079,9 +3079,9 @@ COMMAND_HANDLER(handle_gdb_save_tdesc_command)
|
|||
goto out;
|
||||
}
|
||||
|
||||
retval = fileio_write(&fileio, tdesc_length, tdesc, &size_written);
|
||||
retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
|
||||
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
if (retval != ERROR_OK)
|
||||
LOG_ERROR("Error while writing the tdesc file");
|
||||
|
|
|
@ -1701,7 +1701,7 @@ COMMAND_HANDLER(handle_etm_image_command)
|
|||
|
||||
COMMAND_HANDLER(handle_etm_dump_command)
|
||||
{
|
||||
struct fileio file;
|
||||
struct fileio *file;
|
||||
struct target *target;
|
||||
struct arm *arm;
|
||||
struct etm_context *etm_ctx;
|
||||
|
@ -1741,24 +1741,24 @@ COMMAND_HANDLER(handle_etm_dump_command)
|
|||
if (fileio_open(&file, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
|
||||
return ERROR_FAIL;
|
||||
|
||||
fileio_write_u32(&file, etm_ctx->capture_status);
|
||||
fileio_write_u32(&file, etm_ctx->control);
|
||||
fileio_write_u32(&file, etm_ctx->trace_depth);
|
||||
fileio_write_u32(file, etm_ctx->capture_status);
|
||||
fileio_write_u32(file, etm_ctx->control);
|
||||
fileio_write_u32(file, etm_ctx->trace_depth);
|
||||
|
||||
for (i = 0; i < etm_ctx->trace_depth; i++) {
|
||||
fileio_write_u32(&file, etm_ctx->trace_data[i].pipestat);
|
||||
fileio_write_u32(&file, etm_ctx->trace_data[i].packet);
|
||||
fileio_write_u32(&file, etm_ctx->trace_data[i].flags);
|
||||
fileio_write_u32(file, etm_ctx->trace_data[i].pipestat);
|
||||
fileio_write_u32(file, etm_ctx->trace_data[i].packet);
|
||||
fileio_write_u32(file, etm_ctx->trace_data[i].flags);
|
||||
}
|
||||
|
||||
fileio_close(&file);
|
||||
fileio_close(file);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
COMMAND_HANDLER(handle_etm_load_command)
|
||||
{
|
||||
struct fileio file;
|
||||
struct fileio *file;
|
||||
struct target *target;
|
||||
struct arm *arm;
|
||||
struct etm_context *etm_ctx;
|
||||
|
@ -1789,15 +1789,15 @@ COMMAND_HANDLER(handle_etm_load_command)
|
|||
return ERROR_FAIL;
|
||||
|
||||
size_t filesize;
|
||||
int retval = fileio_size(&file, &filesize);
|
||||
int retval = fileio_size(file, &filesize);
|
||||
if (retval != ERROR_OK) {
|
||||
fileio_close(&file);
|
||||
fileio_close(file);
|
||||
return retval;
|
||||
}
|
||||
|
||||
if (filesize % 4) {
|
||||
command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data");
|
||||
fileio_close(&file);
|
||||
fileio_close(file);
|
||||
return ERROR_FAIL;
|
||||
}
|
||||
|
||||
|
@ -1808,28 +1808,28 @@ COMMAND_HANDLER(handle_etm_load_command)
|
|||
|
||||
{
|
||||
uint32_t tmp;
|
||||
fileio_read_u32(&file, &tmp); etm_ctx->capture_status = tmp;
|
||||
fileio_read_u32(&file, &tmp); etm_ctx->control = tmp;
|
||||
fileio_read_u32(&file, &etm_ctx->trace_depth);
|
||||
fileio_read_u32(file, &tmp); etm_ctx->capture_status = tmp;
|
||||
fileio_read_u32(file, &tmp); etm_ctx->control = tmp;
|
||||
fileio_read_u32(file, &etm_ctx->trace_depth);
|
||||
}
|
||||
etm_ctx->trace_data = malloc(sizeof(struct etmv1_trace_data) * etm_ctx->trace_depth);
|
||||
if (etm_ctx->trace_data == NULL) {
|
||||
command_print(CMD_CTX, "not enough memory to perform operation");
|
||||
fileio_close(&file);
|
||||
fileio_close(file);
|
||||
return ERROR_FAIL;
|
||||
}
|
||||
|
||||
for (i = 0; i < etm_ctx->trace_depth; i++) {
|
||||
uint32_t pipestat, packet, flags;
|
||||
fileio_read_u32(&file, &pipestat);
|
||||
fileio_read_u32(&file, &packet);
|
||||
fileio_read_u32(&file, &flags);
|
||||
fileio_read_u32(file, &pipestat);
|
||||
fileio_read_u32(file, &packet);
|
||||
fileio_read_u32(file, &flags);
|
||||
etm_ctx->trace_data[i].pipestat = pipestat & 0xff;
|
||||
etm_ctx->trace_data[i].packet = packet & 0xffff;
|
||||
etm_ctx->trace_data[i].flags = flags;
|
||||
}
|
||||
|
||||
fileio_close(&file);
|
||||
fileio_close(file);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
static int autodetect_image_type(struct image *image, const char *url)
|
||||
{
|
||||
int retval;
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
size_t read_bytes;
|
||||
uint8_t buffer[9];
|
||||
|
||||
|
@ -55,13 +55,13 @@ static int autodetect_image_type(struct image *image, const char *url)
|
|||
retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY);
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
retval = fileio_read(&fileio, 9, buffer, &read_bytes);
|
||||
retval = fileio_read(fileio, 9, buffer, &read_bytes);
|
||||
|
||||
if (retval == ERROR_OK) {
|
||||
if (read_bytes != 9)
|
||||
retval = ERROR_FILEIO_OPERATION_FAILED;
|
||||
}
|
||||
fileio_close(&fileio);
|
||||
fileio_close(fileio);
|
||||
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
|
@ -122,7 +122,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
|
|||
struct imagesection *section)
|
||||
{
|
||||
struct image_ihex *ihex = image->type_private;
|
||||
struct fileio *fileio = &ihex->fileio;
|
||||
struct fileio *fileio = ihex->fileio;
|
||||
uint32_t full_address = 0x0;
|
||||
uint32_t cooked_bytes;
|
||||
int i;
|
||||
|
@ -352,7 +352,7 @@ static int image_elf_read_headers(struct image *image)
|
|||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
}
|
||||
|
||||
retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t *)elf->header, &read_bytes);
|
||||
retval = fileio_read(elf->fileio, sizeof(Elf32_Ehdr), (uint8_t *)elf->header, &read_bytes);
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("cannot read ELF file header, read failed");
|
||||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
|
@ -384,7 +384,7 @@ static int image_elf_read_headers(struct image *image)
|
|||
return ERROR_IMAGE_FORMAT_ERROR;
|
||||
}
|
||||
|
||||
retval = fileio_seek(&elf->fileio, field32(elf, elf->header->e_phoff));
|
||||
retval = fileio_seek(elf->fileio, field32(elf, elf->header->e_phoff));
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("cannot seek to ELF program header table, read failed");
|
||||
return retval;
|
||||
|
@ -396,7 +396,7 @@ static int image_elf_read_headers(struct image *image)
|
|||
return ERROR_FILEIO_OPERATION_FAILED;
|
||||
}
|
||||
|
||||
retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr),
|
||||
retval = fileio_read(elf->fileio, elf->segment_count*sizeof(Elf32_Phdr),
|
||||
(uint8_t *)elf->segments, &read_bytes);
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("cannot read ELF segment headers, read failed");
|
||||
|
@ -486,12 +486,12 @@ static int image_elf_read_section(struct image *image,
|
|||
LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32 "", read_size,
|
||||
field32(elf, segment->p_offset) + offset);
|
||||
/* read initialized area of the segment */
|
||||
retval = fileio_seek(&elf->fileio, field32(elf, segment->p_offset) + offset);
|
||||
retval = fileio_seek(elf->fileio, field32(elf, segment->p_offset) + offset);
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("cannot find ELF segment content, seek failed");
|
||||
return retval;
|
||||
}
|
||||
retval = fileio_read(&elf->fileio, read_size, buffer, &really_read);
|
||||
retval = fileio_read(elf->fileio, read_size, buffer, &really_read);
|
||||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR("cannot read ELF segment content, read failed");
|
||||
return retval;
|
||||
|
@ -511,7 +511,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
|
|||
struct imagesection *section)
|
||||
{
|
||||
struct image_mot *mot = image->type_private;
|
||||
struct fileio *fileio = &mot->fileio;
|
||||
struct fileio *fileio = mot->fileio;
|
||||
uint32_t full_address = 0x0;
|
||||
uint32_t cooked_bytes;
|
||||
int i;
|
||||
|
@ -708,9 +708,9 @@ int image_open(struct image *image, const char *url, const char *type_string)
|
|||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
size_t filesize;
|
||||
retval = fileio_size(&image_binary->fileio, &filesize);
|
||||
retval = fileio_size(image_binary->fileio, &filesize);
|
||||
if (retval != ERROR_OK) {
|
||||
fileio_close(&image_binary->fileio);
|
||||
fileio_close(image_binary->fileio);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
@ -732,7 +732,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
|
|||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR(
|
||||
"failed buffering IHEX image, check daemon output for additional information");
|
||||
fileio_close(&image_ihex->fileio);
|
||||
fileio_close(image_ihex->fileio);
|
||||
return retval;
|
||||
}
|
||||
} else if (image->type == IMAGE_ELF) {
|
||||
|
@ -746,7 +746,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
|
|||
|
||||
retval = image_elf_read_headers(image);
|
||||
if (retval != ERROR_OK) {
|
||||
fileio_close(&image_elf->fileio);
|
||||
fileio_close(image_elf->fileio);
|
||||
return retval;
|
||||
}
|
||||
} else if (image->type == IMAGE_MEMORY) {
|
||||
|
@ -783,7 +783,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
|
|||
if (retval != ERROR_OK) {
|
||||
LOG_ERROR(
|
||||
"failed buffering S19 image, check daemon output for additional information");
|
||||
fileio_close(&image_mot->fileio);
|
||||
fileio_close(image_mot->fileio);
|
||||
return retval;
|
||||
}
|
||||
} else if (image->type == IMAGE_BUILDER) {
|
||||
|
@ -835,12 +835,12 @@ int image_read_section(struct image *image,
|
|||
return ERROR_COMMAND_SYNTAX_ERROR;
|
||||
|
||||
/* seek to offset */
|
||||
retval = fileio_seek(&image_binary->fileio, offset);
|
||||
retval = fileio_seek(image_binary->fileio, offset);
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
|
||||
/* return requested bytes */
|
||||
retval = fileio_read(&image_binary->fileio, size, buffer, size_read);
|
||||
retval = fileio_read(image_binary->fileio, size, buffer, size_read);
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
} else if (image->type == IMAGE_IHEX) {
|
||||
|
@ -945,11 +945,11 @@ void image_close(struct image *image)
|
|||
if (image->type == IMAGE_BINARY) {
|
||||
struct image_binary *image_binary = image->type_private;
|
||||
|
||||
fileio_close(&image_binary->fileio);
|
||||
fileio_close(image_binary->fileio);
|
||||
} else if (image->type == IMAGE_IHEX) {
|
||||
struct image_ihex *image_ihex = image->type_private;
|
||||
|
||||
fileio_close(&image_ihex->fileio);
|
||||
fileio_close(image_ihex->fileio);
|
||||
|
||||
if (image_ihex->buffer) {
|
||||
free(image_ihex->buffer);
|
||||
|
@ -958,7 +958,7 @@ void image_close(struct image *image)
|
|||
} else if (image->type == IMAGE_ELF) {
|
||||
struct image_elf *image_elf = image->type_private;
|
||||
|
||||
fileio_close(&image_elf->fileio);
|
||||
fileio_close(image_elf->fileio);
|
||||
|
||||
if (image_elf->header) {
|
||||
free(image_elf->header);
|
||||
|
@ -979,7 +979,7 @@ void image_close(struct image *image)
|
|||
} else if (image->type == IMAGE_SRECORD) {
|
||||
struct image_mot *image_mot = image->type_private;
|
||||
|
||||
fileio_close(&image_mot->fileio);
|
||||
fileio_close(image_mot->fileio);
|
||||
|
||||
if (image_mot->buffer) {
|
||||
free(image_mot->buffer);
|
||||
|
|
|
@ -66,11 +66,11 @@ struct image {
|
|||
};
|
||||
|
||||
struct image_binary {
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
};
|
||||
|
||||
struct image_ihex {
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
uint8_t *buffer;
|
||||
};
|
||||
|
||||
|
@ -81,7 +81,7 @@ struct image_memory {
|
|||
};
|
||||
|
||||
struct image_elf {
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
Elf32_Ehdr *header;
|
||||
Elf32_Phdr *segments;
|
||||
uint32_t segment_count;
|
||||
|
@ -89,7 +89,7 @@ struct image_elf {
|
|||
};
|
||||
|
||||
struct image_mot {
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
uint8_t *buffer;
|
||||
};
|
||||
|
||||
|
|
|
@ -3221,7 +3221,7 @@ COMMAND_HANDLER(handle_load_image_command)
|
|||
|
||||
COMMAND_HANDLER(handle_dump_image_command)
|
||||
{
|
||||
struct fileio fileio;
|
||||
struct fileio *fileio;
|
||||
uint8_t *buffer;
|
||||
int retval, retvaltemp;
|
||||
uint32_t address, size;
|
||||
|
@ -3254,7 +3254,7 @@ COMMAND_HANDLER(handle_dump_image_command)
|
|||
if (retval != ERROR_OK)
|
||||
break;
|
||||
|
||||
retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
|
||||
retval = fileio_write(fileio, this_run_size, buffer, &size_written);
|
||||
if (retval != ERROR_OK)
|
||||
break;
|
||||
|
||||
|
@ -3266,7 +3266,7 @@ COMMAND_HANDLER(handle_dump_image_command)
|
|||
|
||||
if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
|
||||
size_t filesize;
|
||||
retval = fileio_size(&fileio, &filesize);
|
||||
retval = fileio_size(fileio, &filesize);
|
||||
if (retval != ERROR_OK)
|
||||
return retval;
|
||||
command_print(CMD_CTX,
|
||||
|
@ -3274,7 +3274,7 @@ COMMAND_HANDLER(handle_dump_image_command)
|
|||
duration_elapsed(&bench), duration_kbps(&bench, filesize));
|
||||
}
|
||||
|
||||
retvaltemp = fileio_close(&fileio);
|
||||
retvaltemp = fileio_close(fileio);
|
||||
if (retvaltemp != ERROR_OK)
|
||||
return retvaltemp;
|
||||
|
||||
|
|
|
@ -3425,7 +3425,7 @@ COMMAND_HANDLER(xscale_handle_dump_trace_command)
|
|||
struct target *target = get_current_target(CMD_CTX);
|
||||
struct xscale_common *xscale = target_to_xscale(target);
|
||||
struct xscale_trace_data *trace_data;
|
||||
struct fileio file;
|
||||
struct fileio *file;
|
||||
int retval;
|
||||
|
||||
retval = xscale_verify_pointer(CMD_CTX, xscale);
|
||||
|
@ -3453,19 +3453,19 @@ COMMAND_HANDLER(xscale_handle_dump_trace_command)
|
|||
while (trace_data) {
|
||||
int i;
|
||||
|
||||
fileio_write_u32(&file, trace_data->chkpt0);
|
||||
fileio_write_u32(&file, trace_data->chkpt1);
|
||||
fileio_write_u32(&file, trace_data->last_instruction);
|
||||
fileio_write_u32(&file, trace_data->depth);
|
||||
fileio_write_u32(file, trace_data->chkpt0);
|
||||
fileio_write_u32(file, trace_data->chkpt1);
|
||||
fileio_write_u32(file, trace_data->last_instruction);
|
||||
fileio_write_u32(file, trace_data->depth);
|
||||
|
||||
for (i = 0; i < trace_data->depth; i++)
|
||||
fileio_write_u32(&file, trace_data->entries[i].data |
|
||||
fileio_write_u32(file, trace_data->entries[i].data |
|
||||
((trace_data->entries[i].type & 0xffff) << 16));
|
||||
|
||||
trace_data = trace_data->next;
|
||||
}
|
||||
|
||||
fileio_close(&file);
|
||||
fileio_close(file);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue