- added manpage for OpenOCD (thanks to Uwe Hermann)
[openocd.git] / src / flash / flash.c
index 0a71309316be93cc4affec1ba94adafe1bf66cb9..fecb89b72d3d23c749c341bf3c0fc40b0bea92eb 100644 (file)
@@ -25,6 +25,7 @@
 #include "command.h"
 #include "log.h"
 #include "target.h"
+#include "time_support.h"
 
 #include <string.h>
 #include <unistd.h>
@@ -33,6 +34,9 @@
 #include <sys/stat.h>
 #include <errno.h>
 
+#include <fileio.h>
+#include <image.h>
+
 /* command handlers */
 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
@@ -50,6 +54,7 @@ extern flash_driver_t lpc2000_flash;
 extern flash_driver_t cfi_flash;
 extern flash_driver_t at91sam7_flash;
 extern flash_driver_t str7x_flash;
+extern flash_driver_t str9x_flash;
 
 flash_driver_t *flash_drivers[] =
 {
@@ -57,6 +62,7 @@ flash_driver_t *flash_drivers[] =
        &cfi_flash,
        &at91sam7_flash,
        &str7x_flash,
+       &str9x_flash,
        NULL,
 };
 
@@ -365,6 +371,10 @@ int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, cha
                int last = strtoul(args[2], NULL, 0);
                int retval;
                flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
+               struct timeval start, end, duration;
+
+               gettimeofday(&start, NULL);
+       
                if (!p)
                {
                        command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
@@ -397,6 +407,13 @@ int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, cha
                                        command_print(cmd_ctx, "unknown error");
                        }
                }
+               else
+               {
+                       gettimeofday(&end, NULL);       
+                       timeval_subtract(&duration, &end, &start);
+               
+                       command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %is %ius", first, last, strtoul(args[0], 0, 0), duration.tv_sec, duration.tv_usec);
+               }
        }
        else
        {
@@ -457,6 +474,10 @@ int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, c
                                        command_print(cmd_ctx, "unknown error");
                        }
                }
+               else
+               {
+                       command_print(cmd_ctx, "%s protection for sectors %i through %i on flash bank %i", (set) ? "set" : "cleared", first, last, strtoul(args[0], 0, 0));
+               }
        }
        else
        {
@@ -468,21 +489,33 @@ int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, c
 
 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       FILE *binary;
        u32 offset;
-       struct stat binary_stat;
-       u32 binary_size;
        u8 *buffer;
        u32 buf_cnt;
+       u32 image_size;
+       int i;
+
+       image_t image;
+       
+       duration_t duration;
+       char *duration_text;
+       
        int retval;
        flash_bank_t *p;
 
        if (argc < 3)
        {
-               command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
+               command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
                return ERROR_OK;
        }
        
+       duration_start_measure(&duration);
+       
+       image.base_address_set = 1;
+       image.base_address = strtoul(args[1], NULL, 0);
+       
+       image.start_address_set = 0;
+       
        offset = strtoul(args[2], NULL, 0);
        p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
        if (!p)
@@ -491,70 +524,69 @@ int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, cha
                return ERROR_OK;
        }
        
-       if (stat(args[1], &binary_stat) == -1)
+       if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
        {
-               ERROR("couldn't stat() %s: %s", args[1], strerror(errno));
+               command_print(cmd_ctx, "flash write error: %s", image.error_str);
                return ERROR_OK;
        }
-
-       if (S_ISDIR(binary_stat.st_mode))
+       
+       image_size = 0x0;
+       for (i = 0; i < image.num_sections; i++)
        {
-               ERROR("%s is a directory", args[1]);
-               command_print(cmd_ctx,"%s is a directory", args[1]);
-               return ERROR_OK;
-       }
-               
-       if (binary_stat.st_size == 0){
-               ERROR("Empty file %s", args[1]);
-               command_print(cmd_ctx,"Empty file %s", args[1]);
-               return ERROR_OK;
-       }
+               buffer = malloc(image.sections[i].size);
+               if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
+               {
+                       ERROR("image_read_section failed with error code: %i", retval);
+                       command_print(cmd_ctx, "image reading failed, flash write aborted");
+                       free(buffer);
+                       image_close(&image);
+                       return ERROR_OK;
+               }
                
-       if (!(binary = fopen(args[1], "rb")))
-       {
-               ERROR("couldn't open %s: %s", args[1], strerror(errno));
-               command_print(cmd_ctx, "couldn't open %s", args[1]);
-               return ERROR_OK;
-       }
-
-       binary_size = binary_stat.st_size;
-       buffer = malloc(binary_size);
-       buf_cnt = fread(buffer, 1, binary_size, binary);
-
-       if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
-       {
-               switch (retval)
+               if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
                {
-                       case ERROR_TARGET_NOT_HALTED:
-                               command_print(cmd_ctx, "can't work with this flash while target is running");
-                               break;
-                       case ERROR_INVALID_ARGUMENTS:
-                               command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
-                               break;
-                       case ERROR_FLASH_BANK_INVALID:
-                               command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
-                               break;
-                       case ERROR_FLASH_OPERATION_FAILED:
-                               command_print(cmd_ctx, "flash program error");
-                               break;
-                       case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
-                               command_print(cmd_ctx, "offset breaks required alignment");
-                               break;
-                       case ERROR_FLASH_DST_OUT_OF_BANK:
-                               command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
-                               break;
-                       case ERROR_FLASH_SECTOR_NOT_ERASED:
-                               command_print(cmd_ctx, "destination sector(s) not erased");
-                               break;
-                       default:
-                               command_print(cmd_ctx, "unknown error");
+                       command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
+                               args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
+                       switch (retval)
+                       {
+                               case ERROR_TARGET_NOT_HALTED:
+                                       command_print(cmd_ctx, "can't work with this flash while target is running");
+                                       break;
+                               case ERROR_INVALID_ARGUMENTS:
+                                       command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
+                                       break;
+                               case ERROR_FLASH_BANK_INVALID:
+                                       command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
+                                       break;
+                               case ERROR_FLASH_OPERATION_FAILED:
+                                       command_print(cmd_ctx, "flash program error");
+                                       break;
+                               case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
+                                       command_print(cmd_ctx, "offset breaks required alignment");
+                                       break;
+                               case ERROR_FLASH_DST_OUT_OF_BANK:
+                                       command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
+                                       break;
+                               case ERROR_FLASH_SECTOR_NOT_ERASED:
+                                       command_print(cmd_ctx, "destination sector(s) not erased");
+                                       break;
+                               default:
+                                       command_print(cmd_ctx, "unknown error");
+                       }
                }
+               image_size += buf_cnt;
+
+               free(buffer);
        }
-       free(buffer);
-       fclose(binary);
+
        
-       command_print(cmd_ctx, "wrote file %s to flash bank %i at offset 0x%8.8x", args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
+       duration_stop_measure(&duration, &duration_text);
+       command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
+               image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
+               (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
+       free(duration_text);
+
+       image_close(&image);
        
        return ERROR_OK;
-
 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)