]> git.sur5r.net Git - openocd/commitdiff
fileio: improve API types
authorZachary T Welch <zw@superlucidity.net>
Fri, 13 Nov 2009 22:44:53 +0000 (14:44 -0800)
committerZachary T Welch <zw@superlucidity.net>
Mon, 16 Nov 2009 23:47:09 +0000 (15:47 -0800)
Use size_t instead of uint32_t when specifying file sizes.  Update all
consumers up through the layers to use size_t when required.  These
changes should be safe, but the higher-levels will need to be updated
further to receive the intended benefits (i.e. large file support).

Add error checking for fileio_read and file_write.  Previously, all
errors were being silently ignored, so this change might cause some
problems for some people in some cases.  However, it gives us the chance
to handle any errors that do occur at higher-levels, rather than burying
our heads in the sand.

12 files changed:
src/flash/ecos.c
src/flash/flash.c
src/flash/lpc2900.c
src/flash/mflash.c
src/flash/nand.c
src/helper/fileio.c
src/helper/fileio.h
src/target/etm.c
src/target/image.c
src/target/image.h
src/target/target.c
src/target/xscale.c

index c12ed9d247084d4da71c214900d2604a36c20ea9..b2ffadffaf8cb168c4ec314495a5361c4bed7a1d 100644 (file)
@@ -152,8 +152,8 @@ FLASH_BANK_COMMAND_HANDLER(ecosflash_flash_bank_command)
 
 static int loadDriver(struct ecosflash_flash_bank *info)
 {
-       uint32_t buf_cnt;
-       uint32_t image_size;
+       size_t buf_cnt;
+       size_t image_size;
        struct image image;
 
        image.base_address_set = 0;
@@ -182,7 +182,8 @@ static int loadDriver(struct ecosflash_flash_bank *info)
                }
                target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer);
                image_size += buf_cnt;
-               LOG_DEBUG("%" PRIu32 " byte written at address 0x%8.8" PRIx32 "", buf_cnt, image.sections[i].base_address);
+               LOG_DEBUG("%zu bytes written at address 0x%8.8" PRIx32 "",
+                               buf_cnt, image.sections[i].base_address);
 
                free(buffer);
        }
index 03d4547d407df8307e6585479e991108da2ad1c1..adc1411958a1c1385cc70ff176f6491a923ae47e 100644 (file)
@@ -811,7 +811,6 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
 {
        uint32_t offset;
        uint8_t *buffer;
-       uint32_t buf_cnt;
        struct fileio fileio;
 
        if (argc != 3)
@@ -833,6 +832,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
        }
 
        buffer = malloc(fileio.size);
+       size_t buf_cnt;
        if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
        {
                free(buffer);
@@ -1059,7 +1059,7 @@ static int flash_write_unlock(struct target *target, struct image *image, uint32
                /* read sections to the buffer */
                while (buffer_size < run_size)
                {
-                       uint32_t size_read;
+                       size_t size_read;
 
                        size_read = run_size - buffer_size;
                        if (size_read > image->sections[section].size - section_offset)
index b80079df00e5d8c180399ff991e5cca9ce530ccf..dc916689d00ddff03a1627de04b63cdec9317734 100644 (file)
@@ -631,7 +631,7 @@ COMMAND_HANDLER(lpc2900_handle_read_custom_command)
                return ret;
        }
 
-       uint32_t nwritten;
+       size_t nwritten;
        ret = fileio_write( &fileio, sizeof(customer),
                         (const uint8_t *)customer, &nwritten );
        if( ret != ERROR_OK )
@@ -755,7 +755,7 @@ COMMAND_HANDLER(lpc2900_handle_write_custom_command)
        /* Page 4 */
        uint32_t offset = ISS_CUSTOMER_START1 % FLASH_PAGE_SIZE;
        memset( page, 0xff, FLASH_PAGE_SIZE );
-       uint32_t size_read;
+       size_t size_read;
        retval = image_read_section( &image, 0, 0,
                                     ISS_CUSTOMER_SIZE1, &page[offset], &size_read);
        if( retval != ERROR_OK )
index 4356f2709b30e812e8f836ad3addc8b0b133036e..062064275aaa84c2f5be6cb157e4afc6b7fba072 100644 (file)
@@ -705,7 +705,7 @@ static int mg_mflash_write(uint32_t addr, uint8_t *buff, uint32_t len)
 
 COMMAND_HANDLER(mg_write_cmd)
 {
-       uint32_t address, buf_cnt, cnt, res, i;
+       uint32_t address, cnt, res, i;
        uint8_t *buffer;
        struct fileio fileio;
        int ret;
@@ -732,6 +732,7 @@ COMMAND_HANDLER(mg_write_cmd)
        struct duration bench;
        duration_start(&bench);
 
+       size_t buf_cnt;
        for (i = 0; i < cnt; i++) {
                if ((ret = fileio_read(&fileio, MG_FILEIO_CHUNK, buffer, &buf_cnt)) !=
                                ERROR_OK)
@@ -769,7 +770,7 @@ mg_write_cmd_err:
 
 COMMAND_HANDLER(mg_dump_cmd)
 {
-       uint32_t address, size_written, size, cnt, res, i;
+       uint32_t address, size, cnt, res, i;
        uint8_t *buffer;
        struct fileio fileio;
        int ret;
@@ -797,6 +798,7 @@ COMMAND_HANDLER(mg_dump_cmd)
        struct duration bench;
        duration_start(&bench);
 
+       size_t size_written;
        for (i = 0; i < cnt; i++) {
                if ((ret = mg_mflash_read(address, buffer, MG_FILEIO_CHUNK)) != ERROR_OK)
                        goto mg_dump_cmd_err;
index 9d997fadd8a25127cadc9f3b835e7db5e21a26b3..71a67c45b333fd1849e4964b7534f0fdd7720a8f 100644 (file)
@@ -1453,8 +1453,8 @@ static COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
 static int nand_fileio_read(struct nand_device *nand,
                struct nand_fileio_state *s)
 {
-       uint32_t total_read = 0;
-       uint32_t one_read;
+       size_t total_read = 0;
+       size_t one_read;
 
        if (NULL != s->page)
        {
@@ -1616,7 +1616,7 @@ COMMAND_HANDLER(handle_nand_dump_command)
 
        while (s.size > 0)
        {
-               uint32_t size_written;
+               size_t size_written;
                int retval = nand_read_page(nand, s.address / nand->page_size,
                                s.page, s.page_size, s.oob, s.oob_size);
                if (ERROR_OK != retval)
index 84d46bd57665f3ff3457a4fddf1f09ed4a281f6a..8f2ce220473f74402645a4f413935cb1b6a042ee 100644 (file)
@@ -143,7 +143,7 @@ int fileio_close(struct fileio *fileio)
        return retval;
 }
 
-int fileio_seek(struct fileio *fileio, uint32_t position)
+int fileio_seek(struct fileio *fileio, size_t position)
 {
        int retval;
        if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
@@ -155,14 +155,16 @@ int fileio_seek(struct fileio *fileio, uint32_t position)
        return ERROR_OK;
 }
 
-static inline int fileio_local_read(struct fileio *fileio, uint32_t size, uint8_t *buffer, uint32_t *size_read)
+static int fileio_local_read(struct fileio *fileio,
+               size_t size, void *buffer, size_t *size_read)
 {
-       *size_read = fread(buffer, 1, size, fileio->file);
-
-       return ERROR_OK;
+       ssize_t 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, uint32_t size, uint8_t *buffer, uint32_t *size_read)
+int fileio_read(struct fileio *fileio, size_t size, void *buffer,
+               size_t *size_read)
 {
        return fileio_local_read(fileio, size, buffer, size_read);
 }
@@ -170,17 +172,17 @@ int fileio_read(struct fileio *fileio, uint32_t size, uint8_t *buffer, uint32_t
 int fileio_read_u32(struct fileio *fileio, uint32_t *data)
 {
        uint8_t buf[4];
-       uint32_t size_read;
-       int retval;
-
-       if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
-               return retval;
-       *data = be_to_h_u32(buf);
-
-       return ERROR_OK;
+       size_t size_read;
+       int 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 inline int fileio_local_fgets(struct fileio *fileio, uint32_t size, char *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;
@@ -188,40 +190,37 @@ static inline int fileio_local_fgets(struct fileio *fileio, uint32_t size, char
        return ERROR_OK;
 }
 
-int fileio_fgets(struct fileio *fileio, uint32_t size, char *buffer)
+int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
 {
        return fileio_local_fgets(fileio, size, buffer);
 }
 
-static inline int fileio_local_write(struct fileio *fileio, uint32_t size, const uint8_t *buffer, uint32_t *size_written)
+static int fileio_local_write(struct fileio *fileio,
+               size_t size, const void *buffer, size_t *size_written)
 {
-       *size_written = fwrite(buffer, 1, size, fileio->file);
-
-       return ERROR_OK;
+       ssize_t 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, uint32_t size, const uint8_t *buffer, uint32_t *size_written)
+int fileio_write(struct fileio *fileio,
+               size_t size, const void *buffer, size_t *size_written)
 {
-       int retval;
-
-       retval = fileio_local_write(fileio, size, buffer, size_written);
-
+       int retval = fileio_local_write(fileio, size, buffer, size_written);
        if (retval == ERROR_OK)
                fileio->size += *size_written;
-
-       return retval;;
+       return retval;
 }
 
 int fileio_write_u32(struct fileio *fileio, uint32_t data)
 {
        uint8_t buf[4];
-       uint32_t size_written;
-       int retval;
-
        h_u32_to_be(buf, data);
 
-       if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
-               return retval;
+       size_t size_written;
+       int retval = fileio_write(fileio, 4, buf, &size_written);
+       if (ERROR_OK == retval && size_written != sizeof(uint32_t))
+               retval = -EIO;
 
-       return ERROR_OK;
+       return retval;
 }
index 6ed6fe454e7f4c918e882b584e2e3c3459018f04..325b441ff9884191b438db8ac9be1697214f2d9f 100644 (file)
@@ -58,13 +58,13 @@ int fileio_open(struct fileio *fileio,
        const char *url, enum fileio_access access, enum fileio_type type);
 int fileio_close(struct fileio *fileio);
 
-int fileio_seek(struct fileio *fileio, uint32_t position);
-int fileio_fgets(struct fileio *fileio, uint32_t size, char *buffer);
+int fileio_seek(struct fileio *fileio, size_t position);
+int fileio_fgets(struct fileio *fileio, size_t size, void *buffer);
 
 int fileio_read(struct fileio *fileio,
-               uint32_t size, uint8_t *buffer, uint32_t *size_read);
+               size_t size, void *buffer, size_t *size_read);
 int fileio_write(struct fileio *fileio,
-               uint32_t size, const uint8_t *buffer, uint32_t *size_written);
+               size_t size, const void *buffer, size_t *size_written);
 
 int fileio_read_u32(struct fileio *fileio, uint32_t *data);
 int fileio_write_u32(struct fileio *fileio, uint32_t data);
index 5a0ffed553c512ba73a952a965c4119bdb84e19d..fa7a71b2c4972c76d587d756aa3d22841128682b 100644 (file)
@@ -640,7 +640,7 @@ static int etm_read_instruction(struct etm_context *ctx, struct arm_instruction
 {
        int i;
        int section = -1;
-       uint32_t size_read;
+       size_t size_read;
        uint32_t opcode;
        int retval;
 
index bba4675ff2806476f7d5a5a261e9238e1f62013c..8774c2517ba6e1f500e1f353466d348503e1595a 100644 (file)
@@ -48,7 +48,7 @@ static int autodetect_image_type(struct image *image, const char *url)
 {
        int retval;
        struct fileio fileio;
-       uint32_t read_bytes;
+       size_t read_bytes;
        uint8_t buffer[9];
 
        /* read the first 4 bytes of image */
@@ -175,7 +175,7 @@ static int image_ihex_buffer_complete(struct image *image)
                uint32_t record_type;
                uint32_t checksum;
                uint8_t cal_checksum = 0;
-               uint32_t bytes_read = 0;
+               size_t bytes_read = 0;
 
                if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32 , &count, &address, &record_type) != 3)
                {
@@ -360,7 +360,7 @@ static int image_ihex_buffer_complete(struct image *image)
 static int image_elf_read_headers(struct image *image)
 {
        struct image_elf *elf = image->type_private;
-       uint32_t read_bytes;
+       size_t read_bytes;
        uint32_t i,j;
        int retval;
 
@@ -458,11 +458,11 @@ static int image_elf_read_headers(struct image *image)
        return ERROR_OK;
 }
 
-static int image_elf_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
+static int image_elf_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
 {
        struct image_elf *elf = image->type_private;
        Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
-       uint32_t read_size,really_read;
+       size_t read_size,really_read;
        int retval;
 
        *size_read = 0;
@@ -474,7 +474,7 @@ static int image_elf_read_section(struct image *image, int section, uint32_t off
        {
                /* maximal size present in file for the current segment */
                read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
-               LOG_DEBUG("read elf: size = 0x%" PRIx32 " at 0x%" PRIx32 "",read_size,
+               LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32 "", read_size,
                        field32(elf,segment->p_offset) + offset);
                /* read initialized area of the segment */
                if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset) + offset)) != ERROR_OK)
@@ -797,7 +797,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
        return retval;
 };
 
-int image_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
+int image_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
 {
        int retval;
 
index 06d47bf874612bdd8e99d8a74ff7e266a8968ecd..0dac5baed7d83d4e843ee736a571c5f51f5556c7 100644 (file)
@@ -102,7 +102,7 @@ struct image_mot
 
 int image_open(struct image *image, const char *url, const char *type_string);
 int image_read_section(struct image *image, int section, uint32_t offset,
-               uint32_t size, uint8_t *buffer, uint32_t *size_read);
+               uint32_t size, uint8_t *buffer, size_t *size_read);
 void image_close(struct image *image);
 
 int image_add_section(struct image *image, uint32_t base, uint32_t size,
index f141e933f3865214290ef2ae03ddafb1e0058460..c00c2ed9d78075e7e3471408764e141047f557f2 100644 (file)
@@ -2399,7 +2399,7 @@ static COMMAND_HELPER(parse_load_image_command_args, struct image *image,
 COMMAND_HANDLER(handle_load_image_command)
 {
        uint8_t *buffer;
-       uint32_t buf_cnt;
+       size_t buf_cnt;
        uint32_t image_size;
        uint32_t min_address = 0;
        uint32_t max_address = 0xffffffff;
@@ -2519,7 +2519,7 @@ COMMAND_HANDLER(handle_dump_image_command)
        int retval = ERROR_OK;
        while (size > 0)
        {
-               uint32_t size_written;
+               size_t size_written;
                uint32_t this_run_size = (size > 560) ? 560 : size;
                retval = target_read_buffer(target, address, this_run_size, buffer);
                if (retval != ERROR_OK)
@@ -2553,7 +2553,7 @@ COMMAND_HANDLER(handle_dump_image_command)
 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
 {
        uint8_t *buffer;
-       uint32_t buf_cnt;
+       size_t buf_cnt;
        uint32_t image_size;
        int i;
        int retval;
@@ -2674,7 +2674,7 @@ static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
                        }
                } else
                {
-                       command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
+                       command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08zx",
                                                  image.sections[i].base_address,
                                                  buf_cnt);
                }
@@ -4537,7 +4537,7 @@ static void free_fastload(void)
 COMMAND_HANDLER(handle_fast_load_image_command)
 {
        uint8_t *buffer;
-       uint32_t buf_cnt;
+       size_t buf_cnt;
        uint32_t image_size;
        uint32_t min_address = 0;
        uint32_t max_address = 0xffffffff;
index 30ca3bf3287697e53418e2b18bf07a67944534a4..9e93c020c8a78618b28c5b21d05c19cc6b5d92b8 100644 (file)
@@ -2557,7 +2557,7 @@ static int xscale_read_instruction(struct target *target,
        struct xscale_common *xscale = target_to_xscale(target);
        int i;
        int section = -1;
-       uint32_t size_read;
+       size_t size_read;
        uint32_t opcode;
        int retval;