]> git.sur5r.net Git - openocd/commitdiff
fileio: fileio_size() can now fail
authorØyvind Harboe <oyvind.harboe@zylin.com>
Wed, 29 Sep 2010 07:11:01 +0000 (09:11 +0200)
committerØyvind Harboe <oyvind.harboe@zylin.com>
Wed, 29 Sep 2010 16:56:07 +0000 (18:56 +0200)
Part of making the fileio API more robust.

Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
src/flash/mflash.c
src/flash/nand/fileio.c
src/flash/nand/tcl.c
src/flash/nor/tcl.c
src/helper/fileio.c
src/helper/fileio.h
src/target/etm.c
src/target/image.c
src/target/target.c

index 272127be5746af38a1cd1a206f167557969fa2c6..ba34422237d10cd3c6ac9e1f6e3a30a59f7e5f96 100644 (file)
@@ -720,14 +720,20 @@ COMMAND_HANDLER(mg_write_cmd)
        if (ret != ERROR_OK)
                return ret;
 
+       int filesize;
        buffer = malloc(MG_FILEIO_CHUNK);
        if (!buffer) {
                fileio_close(&fileio);
                return ERROR_FAIL;
        }
+       int retval = fileio_size(&fileio, &filesize);
+       if (retval != ERROR_OK) {
+               fileio_close(&fileio);
+               return retval;
+       }
 
-       cnt = fileio_size(&fileio) / MG_FILEIO_CHUNK;
-       res = fileio_size(&fileio) % MG_FILEIO_CHUNK;
+       cnt = filesize / MG_FILEIO_CHUNK;
+       res = filesize % MG_FILEIO_CHUNK;
 
        struct duration bench;
        duration_start(&bench);
@@ -752,8 +758,8 @@ COMMAND_HANDLER(mg_write_cmd)
        if (duration_measure(&bench) == ERROR_OK)
        {
                command_print(CMD_CTX, "wrote %ld bytes from file %s "
-                               "in %fs (%0.3f kB/s)", (long)fileio_size(&fileio), CMD_ARGV[1],
-                               duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
+                               "in %fs (%0.3f kB/s)", (long)filesize, CMD_ARGV[1],
+                               duration_elapsed(&bench), duration_kbps(&bench, filesize));
        }
 
        free(buffer);
index 0a006fc02b01646366f2639d8cc16f63b241ce3b..c7515e2f45bdd58eb964b278bdaf5a68cbab6ff2 100644 (file)
@@ -180,7 +180,13 @@ COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
                return retval;
 
        if (!need_size)
-               state->size = fileio_size(&state->fileio);
+       {
+               int filesize;
+               retval = fileio_size(&state->fileio, &filesize);
+               if (retval != ERROR_OK)
+                       return retval;
+               state->size = filesize;
+       }
 
        *dev = nand;
 
index a54f8ea8fe6ddffdf5734f7cc6dc126a841b756e..15cf1797d5fe0be9fcd7bfdf6a382bce12617004 100644 (file)
@@ -388,9 +388,14 @@ COMMAND_HANDLER(handle_nand_dump_command)
 
        if (nand_fileio_finish(&s) == ERROR_OK)
        {
+               int filesize;
+               retval = fileio_size(&s.fileio, &filesize);
+               if (retval != ERROR_OK)
+                       return retval;
+
                command_print(CMD_CTX, "dumped %ld bytes in %fs (%0.3f KiB/s)",
-                               (long)fileio_size(&s.fileio), duration_elapsed(&s.bench),
-                               duration_kbps(&s.bench, fileio_size(&s.fileio)));
+                               (long)filesize, duration_elapsed(&s.bench),
+                               duration_kbps(&s.bench, filesize));
        }
        return ERROR_OK;
 }
index f36ab7da56a64cf7e05320ab8a0d5a1c10d2d2a1..142f31f71f06264302cd49c329f56586ddf1b18e 100644 (file)
@@ -604,9 +604,23 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
                return ERROR_OK;
        }
 
-       buffer = malloc(fileio_size(&fileio));
+       int filesize;
+       retval = fileio_size(&fileio, &filesize);
+       if (retval != ERROR_OK)
+       {
+               fileio_close(&fileio);
+               return retval;
+       }
+
+       buffer = malloc(filesize);
+       if (buffer == NULL)
+       {
+               fileio_close(&fileio);
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
        size_t buf_cnt;
-       if (fileio_read(&fileio, fileio_size(&fileio), buffer, &buf_cnt) != ERROR_OK)
+       if (fileio_read(&fileio, filesize, buffer, &buf_cnt) != ERROR_OK)
        {
                free(buffer);
                fileio_close(&fileio);
@@ -622,8 +636,8 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
        {
                command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u"
                                " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
-                               (long)fileio_size(&fileio), CMD_ARGV[1], p->bank_number, offset,
-                               duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
+                               (long)filesize, CMD_ARGV[1], p->bank_number, offset,
+                               duration_elapsed(&bench), duration_kbps(&bench, filesize));
        }
 
        fileio_close(&fileio);
index 7c862e47135e6a8542f9c42905184e55c2ac1ff2..9ae0134454c8ce6ef9a17eb530e4f453ade27401 100644 (file)
@@ -245,8 +245,18 @@ int fileio_write_u32(struct fileio *fileio_p, uint32_t data)
        return retval;
 }
 
-int fileio_size(struct fileio *fileio_p)
+/**
+ * FIX!!!!
+ *
+ * For now this can not fail, but that's because a seek was executed
+ * on startup.
+ *
+ * Avoiding the seek on startup opens up for using streams.
+ *
+ */
+int fileio_size(struct fileio *fileio_p, int *size)
 {
        struct fileio_internal *fileio = fileio_p->fp;
-       return fileio->size;
+       *size = fileio->size;
+       return ERROR_OK;
 }
index 40c4ef0e6d78ba96a495134cb7aa53f33146b307..fa499ab7d8b0fca34542f2862228f3e6dc6e477e 100644 (file)
@@ -66,7 +66,7 @@ int fileio_write(struct fileio *fileio,
 
 int fileio_read_u32(struct fileio *fileio, uint32_t *data);
 int fileio_write_u32(struct fileio *fileio, uint32_t data);
-int fileio_size(struct fileio *fileio);
+int fileio_size(struct fileio *fileio, int *size);
 
 #define ERROR_FILEIO_LOCATION_UNKNOWN  (-1200)
 #define ERROR_FILEIO_NOT_FOUND                 (-1201)
index c71c5d199fabbf1827b465e4267c14c0d55db835..9f7bc83d2073f091998b361ed1f0eaef7b05c5e0 100644 (file)
@@ -1897,7 +1897,15 @@ COMMAND_HANDLER(handle_etm_load_command)
                return ERROR_FAIL;
        }
 
-       if (fileio_size(&file) % 4)
+       int filesize;
+       int retval = fileio_size(&file, &filesize);
+       if (retval != ERROR_OK)
+       {
+               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);
index e77e190115ab6bbe6991e55d2998b03e663d79e9..b0d957f8ab62164f7d9a1d63be442655ef2393f0 100644 (file)
@@ -158,7 +158,13 @@ static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine,
        /* we can't determine the number of sections that we'll have to create ahead of time,
         * so we locally hold them until parsing is finished */
 
-       ihex->buffer = malloc(fileio_size(fileio) >> 1);
+       int filesize;
+       int retval;
+       retval = fileio_size(fileio, &filesize);
+       if (retval != ERROR_OK)
+               return retval;
+
+       ihex->buffer = malloc(filesize >> 1);
        cooked_bytes = 0x0;
        image->num_sections = 0;
        section[image->num_sections].private = &ihex->buffer[cooked_bytes];
@@ -537,7 +543,13 @@ static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine,
        /* we can't determine the number of sections that we'll have to create ahead of time,
         * so we locally hold them until parsing is finished */
 
-       mot->buffer = malloc(fileio_size(fileio) >> 1);
+       int retval;
+       int filesize;
+       retval = fileio_size(fileio, &filesize);
+       if (retval != ERROR_OK)
+               return retval;
+
+       mot->buffer = malloc(filesize >> 1);
        cooked_bytes = 0x0;
        image->num_sections = 0;
        section[image->num_sections].private = &mot->buffer[cooked_bytes];
@@ -743,11 +755,18 @@ int image_open(struct image *image, const char *url, const char *type_string)
                {
                        return retval;
                }
+               int filesize;
+               retval = fileio_size(&image_binary->fileio, &filesize);
+               if (retval != ERROR_OK)
+               {
+                       fileio_close(&image_binary->fileio);
+                       return retval;
+               }
 
                image->num_sections = 1;
                image->sections = malloc(sizeof(struct imagesection));
                image->sections[0].base_address = 0x0;
-               image->sections[0].size = fileio_size(&image_binary->fileio);
+               image->sections[0].size = filesize;
                image->sections[0].flags = 0;
        }
        else if (image->type == IMAGE_IHEX)
index fcdcc3633489465dd6edccc5a296ba1ae6360de8..82cbbff547340b767c192828c38c4b2d1190090a 100644 (file)
@@ -2648,9 +2648,13 @@ COMMAND_HANDLER(handle_dump_image_command)
 
        if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
        {
+               int filesize;
+               retval = fileio_size(&fileio, &filesize);
+               if (retval != ERROR_OK)
+                       return retval;
                command_print(CMD_CTX,
-                               "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio_size(&fileio),
-                               duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
+                               "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
+                               duration_elapsed(&bench), duration_kbps(&bench, filesize));
        }
 
        return retval;