use CALL_COMMAND_HANDLER instead of direct calls
[fw/openocd] / src / flash / flash.c
index 329ade6364d0cb39378d4e2ba6baaceea5bcbc7d..da43e1a8130b0885e8d136a886b8b1ede779160e 100644 (file)
 #include "image.h"
 #include "time_support.h"
 
-/* command handlers */
-static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 static int flash_write_unlock(target_t *target, image_t *image, uint32_t *written, int erase, bool unlock);
 
 /* flash drivers
@@ -132,14 +120,6 @@ int flash_driver_protect(struct flash_bank_s *bank, int set, int first, int last
        return retval;
 }
 
-int flash_register_commands(struct command_context_s *cmd_ctx)
-{
-       flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
-
-       register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, "flash bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
-       return ERROR_OK;
-}
-
 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
        flash_bank_t *p;
@@ -173,43 +153,6 @@ static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        return JIM_OK;
 }
 
-int flash_init_drivers(struct command_context_s *cmd_ctx)
-{
-       register_jim(cmd_ctx, "ocd_flash_banks", jim_flash_banks, "return information about the flash banks");
-
-       if (!flash_banks)
-               return ERROR_OK;
-
-       register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
-                                        "print info about flash bank <num>");
-       register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
-                                        "identify flash bank <num>");
-       register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
-                                        "check erase state of sectors in flash bank <num>");
-       register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
-                                        "check protection state of sectors in flash bank <num>");
-       register_command(cmd_ctx, flash_cmd, "erase_sector", handle_flash_erase_command, COMMAND_EXEC,
-                                        "erase sectors at <bank> <first> <last>");
-       register_command(cmd_ctx, flash_cmd, "erase_address", handle_flash_erase_address_command, COMMAND_EXEC,
-                                        "erase address range <address> <length>");
-
-       register_command(cmd_ctx, flash_cmd, "fillw", handle_flash_fill_command, COMMAND_EXEC,
-                                        "fill with pattern (no autoerase) <address> <word_pattern> <count>");
-       register_command(cmd_ctx, flash_cmd, "fillh", handle_flash_fill_command, COMMAND_EXEC,
-                                        "fill with pattern <address> <halfword_pattern> <count>");
-       register_command(cmd_ctx, flash_cmd, "fillb", handle_flash_fill_command, COMMAND_EXEC,
-                                        "fill with pattern <address> <byte_pattern> <count>");
-
-       register_command(cmd_ctx, flash_cmd, "write_bank", handle_flash_write_bank_command, COMMAND_EXEC,
-                                        "write binary data to <bank> <file> <offset>");
-       register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
-                                        "write_image [erase] [unlock] <file> [offset] [type]");
-       register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
-                                        "set protection of sectors at <bank> <first> <last> <on | off>");
-
-       return ERROR_OK;
-}
-
 flash_bank_t *get_flash_bank_by_num_noprobe(int num)
 {
        flash_bank_t *p;
@@ -256,7 +199,7 @@ flash_bank_t *get_flash_bank_by_num(int num)
 }
 
 int flash_command_get_bank_by_num(
-       struct command_context_s *cmd_ctx, char *str, flash_bank_t **bank)
+       struct command_context_s *cmd_ctx, const char *str, flash_bank_t **bank)
 {
        unsigned bank_num;
        COMMAND_PARSE_NUMBER(uint, str, bank_num);
@@ -272,7 +215,7 @@ int flash_command_get_bank_by_num(
 }
 
 
-static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_bank_command)
 {
        int retval;
        int i;
@@ -316,7 +259,8 @@ static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cm
                c->sectors = NULL;
                c->next = NULL;
 
-               if ((retval = flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
+               retval = CALL_COMMAND_HANDLER(flash_drivers[i]->flash_bank_command, c);
+               if (ERROR_OK != retval)
                {
                        LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 , args[0], c->base);
                        free(c);
@@ -352,7 +296,7 @@ static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cm
        return ERROR_OK;
 }
 
-static int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_info_command)
 {
        flash_bank_t *p;
        uint32_t i = 0;
@@ -414,7 +358,7 @@ static int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cm
        return ERROR_OK;
 }
 
-static int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_probe_command)
 {
        int retval;
 
@@ -451,7 +395,7 @@ static int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *c
        return ERROR_OK;
 }
 
-static int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_erase_check_command)
 {
        if (argc != 1)
        {
@@ -497,14 +441,12 @@ static int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, c
        return ERROR_OK;
 }
 
-static int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_erase_address_command)
 {
        flash_bank_t *p;
        int retval;
        int address;
        int length;
-       duration_t duration;
-       char *duration_text;
 
        target_t *target = get_current_target(cmd_ctx);
 
@@ -528,22 +470,22 @@ static int handle_flash_erase_address_command(struct command_context_s *cmd_ctx,
        /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
        flash_set_dirty();
 
-       duration_start_measure(&duration);
+       struct duration bench;
+       duration_start(&bench);
+
+       retval = flash_erase_address_range(target, address, length);
 
-       if ((retval = flash_erase_address_range(target, address, length)) == ERROR_OK)
+       if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
        {
-               if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
-               {
-                       return retval;
-               }
-               command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
-               free(duration_text);
+               command_print(cmd_ctx, "erased address 0x%8.8x (length %i)"
+                               " in %fs (%0.3f kb/s)", address, length,
+                               duration_elapsed(&bench), duration_kbps(&bench, length));
        }
 
        return retval;
 }
 
-static int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_protect_check_command)
 {
        if (argc != 1)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -587,8 +529,7 @@ static int flash_check_sector_parameters(struct command_context_s *cmd_ctx,
        return ERROR_OK;
 }
 
-static int handle_flash_erase_command(struct command_context_s *cmd_ctx,
-               char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_erase_command)
 {
        if (argc != 2)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -613,26 +554,22 @@ static int handle_flash_erase_command(struct command_context_s *cmd_ctx,
                        first, last, p->num_sectors)) != ERROR_OK)
                return retval;
 
-       duration_t duration;
-       char *duration_text;
-       duration_start_measure(&duration);
+       struct duration bench;
+       duration_start(&bench);
 
-       if ((retval = flash_driver_erase(p, first, last)) == ERROR_OK) {
-               if ((retval = duration_stop_measure(&duration,
-                                       &duration_text)) != ERROR_OK)
-                       return retval;
-               command_print(cmd_ctx, "erased sectors %i through %i "
-                               "on flash bank %i in %s",
-                       (int) first, (int) last, (int) bank_nr,
-                       duration_text);
-               free(duration_text);
+       retval = flash_driver_erase(p, first, last);
+
+       if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
+       {
+               command_print(cmd_ctx, "erased sectors %" PRIu32 " "
+                               "through %" PRIu32" on flash bank %" PRIu32 " "
+                               "in %fs", first, last, bank_nr, duration_elapsed(&bench));
        }
 
        return ERROR_OK;
 }
 
-static int handle_flash_protect_command(struct command_context_s *cmd_ctx,
-               char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_protect_command)
 {
        if (argc != 3)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -676,17 +613,14 @@ static int handle_flash_protect_command(struct command_context_s *cmd_ctx,
        return ERROR_OK;
 }
 
-static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_write_image_command)
 {
        target_t *target = get_current_target(cmd_ctx);
 
        image_t image;
        uint32_t written;
 
-       duration_t duration;
-       char *duration_text;
-
-       int retval, retvaltemp;
+       int retval;
 
        if (argc < 1)
        {
@@ -728,7 +662,8 @@ static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, c
                return ERROR_FAIL;
        }
 
-       duration_start_measure(&duration);
+       struct duration bench;
+       duration_start(&bench);
 
        if (argc >= 2)
        {
@@ -756,31 +691,21 @@ static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, c
                return retval;
        }
 
-       if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+       if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
        {
-               image_close(&image);
-               return retvaltemp;
+               command_print(cmd_ctx, "wrote %" PRIu32 " byte from file %s "
+                               "in %fs (%0.3f kb/s)", written, args[0],
+                               duration_elapsed(&bench), duration_kbps(&bench, written));
        }
 
-       float speed;
-
-       speed = written / 1024.0;
-       speed /= ((float)duration.duration.tv_sec
-                       + ((float)duration.duration.tv_usec / 1000000.0));
-       command_print(cmd_ctx,
-                       "wrote %" PRIu32 " byte from file %s in %s (%f kb/s)",
-                       written, args[0], duration_text, speed);
-
-       free(duration_text);
-
        image_close(&image);
 
        return retval;
 }
 
-static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_fill_command)
 {
-       int err = ERROR_OK, retval;
+       int err = ERROR_OK;
        uint32_t address;
        uint32_t pattern;
        uint32_t count;
@@ -789,8 +714,6 @@ static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cm
        uint32_t wrote = 0;
        uint32_t cur_size = 0;
        uint32_t chunk_count;
-       char *duration_text;
-       duration_t duration;
        target_t *target = get_current_target(cmd_ctx);
        uint32_t i;
        uint32_t wordsize;
@@ -843,7 +766,8 @@ static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cm
                exit(-1);
        }
 
-       duration_start_measure(&duration);
+       struct duration bench;
+       duration_start(&bench);
 
        for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
        {
@@ -872,43 +796,29 @@ static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cm
                                return ERROR_FAIL;
                        }
                }
-
        }
 
-       if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+       if (duration_measure(&bench) == ERROR_OK)
        {
-               return retval;
+               command_print(cmd_ctx, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32 
+                               " in %fs (%0.3f kb/s)", wrote, address,
+                               duration_elapsed(&bench), duration_kbps(&bench, wrote));
        }
-
-       float speed;
-
-       speed = wrote / 1024.0;
-       speed /= ((float)duration.duration.tv_sec
-                       + ((float)duration.duration.tv_usec / 1000000.0));
-       command_print(cmd_ctx,
-                       "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32 " in %s (%f kb/s)",
-                       wrote, address, duration_text, speed);
-
-       free(duration_text);
        return ERROR_OK;
 }
 
-static int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(handle_flash_write_bank_command)
 {
        uint32_t offset;
        uint8_t *buffer;
        uint32_t buf_cnt;
-
        fileio_t fileio;
 
-       duration_t duration;
-       char *duration_text;
-
-
        if (argc != 3)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       duration_start_measure(&duration);
+       struct duration bench;
+       duration_start(&bench);
 
        flash_bank_t *p;
        int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &p);
@@ -935,24 +845,13 @@ static int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, ch
        free(buffer);
        buffer = NULL;
 
-       int retvaltemp;
-       if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
-       {
-               fileio_close(&fileio);
-               return retvaltemp;
-       }
-       if (retval == ERROR_OK)
+       if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
        {
-               float elapsed = (float)duration.duration.tv_sec;
-               elapsed += (float)duration.duration.tv_usec / 1000000.0;
-               float speed = (float)fileio.size / elapsed;
-               command_print(cmd_ctx,
-                               "wrote  %lld byte from file %s to flash bank %u "
-                               "at offset 0x%8.8" PRIx32 " in %s (%f kb/s)",
+               command_print(cmd_ctx, "wrote %lld byte from file %s to flash bank %u"
+                               " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
                                fileio.size, args[1], p->bank_number, offset,
-                               duration_text, speed / 1024);
+                               duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
        }
-       free(duration_text);
 
        fileio_close(&fileio);
 
@@ -1317,3 +1216,65 @@ int default_flash_blank_check(struct flash_bank_s *bank)
 
        return ERROR_OK;
 }
+
+int flash_init_drivers(struct command_context_s *cmd_ctx)
+{
+       register_jim(cmd_ctx, "ocd_flash_banks",
+                       jim_flash_banks, "return information about the flash banks");
+
+       if (!flash_banks)
+               return ERROR_OK;
+
+       register_command(cmd_ctx, flash_cmd, "info",
+                       handle_flash_info_command, COMMAND_EXEC,
+                       "print info about flash bank <num>");
+       register_command(cmd_ctx, flash_cmd, "probe",
+                       handle_flash_probe_command, COMMAND_EXEC,
+                       "identify flash bank <num>");
+       register_command(cmd_ctx, flash_cmd, "erase_check",
+                       handle_flash_erase_check_command, COMMAND_EXEC,
+                       "check erase state of sectors in flash bank <num>");
+       register_command(cmd_ctx, flash_cmd, "protect_check",
+                       handle_flash_protect_check_command, COMMAND_EXEC,
+                       "check protection state of sectors in flash bank <num>");
+       register_command(cmd_ctx, flash_cmd, "erase_sector",
+                       handle_flash_erase_command, COMMAND_EXEC,
+                       "erase sectors at <bank> <first> <last>");
+       register_command(cmd_ctx, flash_cmd, "erase_address",
+                       handle_flash_erase_address_command, COMMAND_EXEC,
+                       "erase address range <address> <length>");
+
+       register_command(cmd_ctx, flash_cmd, "fillw",
+                       handle_flash_fill_command, COMMAND_EXEC,
+                       "fill with pattern (no autoerase) <address> <word_pattern> <count>");
+       register_command(cmd_ctx, flash_cmd, "fillh",
+                       handle_flash_fill_command, COMMAND_EXEC,
+                       "fill with pattern <address> <halfword_pattern> <count>");
+       register_command(cmd_ctx, flash_cmd, "fillb",
+                       handle_flash_fill_command, COMMAND_EXEC,
+                       "fill with pattern <address> <byte_pattern> <count>");
+
+       register_command(cmd_ctx, flash_cmd, "write_bank",
+                       handle_flash_write_bank_command, COMMAND_EXEC,
+                       "write binary data to <bank> <file> <offset>");
+       register_command(cmd_ctx, flash_cmd, "write_image",
+                       handle_flash_write_image_command, COMMAND_EXEC,
+                       "write_image [erase] [unlock] <file> [offset] [type]");
+       register_command(cmd_ctx, flash_cmd, "protect",
+                       handle_flash_protect_command, COMMAND_EXEC,
+                       "set protection of sectors at <bank> <first> <last> <on | off>");
+
+       return ERROR_OK;
+}
+
+int flash_register_commands(struct command_context_s *cmd_ctx)
+{
+       flash_cmd = register_command(cmd_ctx, NULL, "flash",
+                       NULL, COMMAND_ANY, NULL);
+
+       register_command(cmd_ctx, flash_cmd, "bank",
+                       handle_flash_bank_command, COMMAND_CONFIG,
+                       "flash bank <driver> <base> <size> "
+                       "<chip_width> <bus_width> <target> [driver_options ...]");
+       return ERROR_OK;
+}