flash/nor/tcl: Fix usage of 'flash erase_sector' command
[openocd.git] / src / flash / nor / tcl.c
index 34681db146af746d17121252e5ecd04a57a0387a..bd313a0b5bc3f258b426e65bf0dd6e12a9fa3ff5 100644 (file)
@@ -98,13 +98,21 @@ COMMAND_HANDLER(handle_flash_info_command)
                if (retval != ERROR_OK)
                        return retval;
 
-               /* We must query the hardware to avoid printing stale information! */
-               retval = p->driver->protect_check(p);
-               if (retval != ERROR_OK)
-                       return retval;
+               /* If the driver does not implement protection, we show the default
+                * state of is_protected array - usually protection state unknown */
+               if (p->driver->protect_check == NULL) {
+                       retval = ERROR_FLASH_OPER_UNSUPPORTED;
+               } else {
+                       /* We must query the hardware to avoid printing stale information! */
+                       retval = p->driver->protect_check(p);
+                       if (retval != ERROR_OK && retval != ERROR_FLASH_OPER_UNSUPPORTED)
+                               return retval;
+               }
+               if (retval == ERROR_FLASH_OPER_UNSUPPORTED)
+                       LOG_WARNING("Flash protection check is not implemented.");
 
-               command_print(CMD_CTX,
-                       "#%d : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32
+               command_print(CMD,
+                       "#%d : %s at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32
                        ", buswidth %i, chipwidth %i",
                        p->bank_number,
                        p->driver->name,
@@ -132,7 +140,7 @@ COMMAND_HANDLER(handle_flash_info_command)
                        else if (!show_sectors || !prot_block_available)
                                protect_state = "protection state unknown";
 
-                       command_print(CMD_CTX,
+                       command_print(CMD,
                                "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
                                j,
                                block_array[j].offset,
@@ -144,7 +152,7 @@ COMMAND_HANDLER(handle_flash_info_command)
                if (p->driver->info != NULL) {
                        retval = p->driver->info(p, buf, sizeof(buf));
                        if (retval == ERROR_OK)
-                               command_print(CMD_CTX, "%s", buf);
+                               command_print(CMD, "%s", buf);
                        else
                                LOG_ERROR("error retrieving flash info");
                }
@@ -168,12 +176,12 @@ COMMAND_HANDLER(handle_flash_probe_command)
        if (p) {
                retval = p->driver->probe(p);
                if (retval == ERROR_OK)
-                       command_print(CMD_CTX,
-                               "flash '%s' found at 0x%8.8" PRIx32,
+                       command_print(CMD,
+                               "flash '%s' found at " TARGET_ADDR_FMT,
                                p->driver->name,
                                p->base);
        } else {
-               command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
+               command_print(CMD, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
                retval = ERROR_FAIL;
        }
 
@@ -194,10 +202,11 @@ COMMAND_HANDLER(handle_flash_erase_check_command)
        int j;
        retval = p->driver->erase_check(p);
        if (retval == ERROR_OK)
-               command_print(CMD_CTX, "successfully checked erase state");
+               command_print(CMD, "successfully checked erase state");
        else {
-               command_print(CMD_CTX,
-                       "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
+               command_print(CMD,
+                       "unknown error when checking erase state of flash bank #%s at "
+                       TARGET_ADDR_FMT,
                        CMD_ARGV[0],
                        p->base);
        }
@@ -213,7 +222,7 @@ COMMAND_HANDLER(handle_flash_erase_check_command)
                        erase_state = "erase state unknown";
 
                blank = false;
-               command_print(CMD_CTX,
+               command_print(CMD,
                        "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
                        j,
                        p->sectors[j].offset,
@@ -223,7 +232,7 @@ COMMAND_HANDLER(handle_flash_erase_check_command)
        }
 
        if (blank)
-               command_print(CMD_CTX, "\tBank is erased");
+               command_print(CMD, "\tBank is erased");
        return retval;
 }
 
@@ -231,7 +240,7 @@ COMMAND_HANDLER(handle_flash_erase_address_command)
 {
        struct flash_bank *p;
        int retval = ERROR_OK;
-       uint32_t address;
+       target_addr_t address;
        uint32_t length;
        bool do_pad = false;
        bool do_unlock = false;
@@ -254,11 +263,11 @@ COMMAND_HANDLER(handle_flash_erase_address_command)
        if (CMD_ARGC != 2)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
+       COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
 
        if (length <= 0) {
-               command_print(CMD_CTX, "Length must be >0");
+               command_print(CMD, "Length must be >0");
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
@@ -280,7 +289,8 @@ COMMAND_HANDLER(handle_flash_erase_address_command)
                retval = flash_erase_address_range(target, do_pad, address, length);
 
        if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
-               command_print(CMD_CTX, "erased address 0x%8.8" PRIx32 " (length %" PRIi32 ")"
+               command_print(CMD, "erased address " TARGET_ADDR_FMT " (length %"
+                               PRIi32 ")"
                        " in %fs (%0.3f KiB/s)", address, length,
                        duration_elapsed(&bench), duration_kbps(&bench, length));
        }
@@ -288,24 +298,6 @@ COMMAND_HANDLER(handle_flash_erase_address_command)
        return retval;
 }
 
-static int flash_check_sector_parameters(struct command_context *cmd_ctx,
-       uint32_t first, uint32_t last, uint32_t num_sectors)
-{
-       if (!(first <= last)) {
-               command_print(cmd_ctx, "ERROR: "
-                       "first sector must be <= last sector");
-               return ERROR_FAIL;
-       }
-
-       if (!(last <= (num_sectors - 1))) {
-               command_print(cmd_ctx, "ERROR: last sector must be <= %" PRIu32,
-                       num_sectors - 1);
-               return ERROR_FAIL;
-       }
-
-       return ERROR_OK;
-}
-
 COMMAND_HANDLER(handle_flash_erase_command)
 {
        if (CMD_ARGC != 3)
@@ -327,9 +319,18 @@ COMMAND_HANDLER(handle_flash_erase_command)
        else
                COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
 
-       retval = flash_check_sector_parameters(CMD_CTX, first, last, p->num_sectors);
-       if (retval != ERROR_OK)
-               return retval;
+       if (!(first <= last)) {
+               command_print(CMD, "ERROR: "
+                       "first sector must be <= last");
+               return ERROR_FAIL;
+       }
+
+       if (!(last <= (uint32_t)(p->num_sectors - 1))) {
+               command_print(CMD, "ERROR: "
+                       "last sector must be <= %" PRIu32,
+                       p->num_sectors - 1);
+               return ERROR_FAIL;
+       }
 
        struct duration bench;
        duration_start(&bench);
@@ -337,7 +338,7 @@ COMMAND_HANDLER(handle_flash_erase_command)
        retval = flash_driver_erase(p, first, last);
 
        if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
-               command_print(CMD_CTX, "erased sectors %" PRIu32 " "
+               command_print(CMD, "erased sectors %" PRIu32 " "
                        "through %" PRIu32 " on flash bank %d "
                        "in %fs", first, last, p->bank_number, duration_elapsed(&bench));
        }
@@ -375,15 +376,28 @@ COMMAND_HANDLER(handle_flash_protect_command)
        bool set;
        COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
 
-       retval = flash_check_sector_parameters(CMD_CTX, first, last, num_blocks);
-       if (retval != ERROR_OK)
-               return retval;
+       if (!(first <= last)) {
+               command_print(CMD, "ERROR: "
+                       "first %s must be <= last",
+                       (p->num_prot_blocks) ? "block" : "sector");
+               return ERROR_FAIL;
+       }
+
+       if (!(last <= (uint32_t)(num_blocks - 1))) {
+               command_print(CMD, "ERROR: "
+                       "last %s must be <= %" PRIu32,
+                       (p->num_prot_blocks) ? "block" : "sector",
+                       num_blocks - 1);
+               return ERROR_FAIL;
+       }
 
        retval = flash_driver_protect(p, set, first, last);
        if (retval == ERROR_OK) {
-               command_print(CMD_CTX, "%s protection for sectors %" PRIu32
+               command_print(CMD, "%s protection for %s %" PRIu32
                        " through %" PRIu32 " on flash bank %d",
-                       (set) ? "set" : "cleared", first, last, p->bank_number);
+                       (set) ? "set" : "cleared",
+                       (p->num_prot_blocks) ? "blocks" : "sectors",
+                       first, last, p->bank_number);
        }
 
        return retval;
@@ -407,12 +421,12 @@ COMMAND_HANDLER(handle_flash_write_image_command)
                        auto_erase = 1;
                        CMD_ARGV++;
                        CMD_ARGC--;
-                       command_print(CMD_CTX, "auto erase enabled");
+                       command_print(CMD, "auto erase enabled");
                } else if (strcmp(CMD_ARGV[0], "unlock") == 0) {
                        auto_unlock = true;
                        CMD_ARGV++;
                        CMD_ARGC--;
-                       command_print(CMD_CTX, "auto unlock enabled");
+                       command_print(CMD, "auto unlock enabled");
                } else
                        break;
        }
@@ -449,7 +463,7 @@ COMMAND_HANDLER(handle_flash_write_image_command)
        }
 
        if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
-               command_print(CMD_CTX, "wrote %" PRIu32 " bytes from file %s "
+               command_print(CMD, "wrote %" PRIu32 " bytes from file %s "
                        "in %fs (%0.3f KiB/s)", written, CMD_ARGV[0],
                        duration_elapsed(&bench), duration_kbps(&bench, written));
        }
@@ -472,11 +486,7 @@ COMMAND_HANDLER(handle_flash_fill_command)
        if (CMD_ARGC != 3)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-#if BUILD_TARGET64
-       COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], address);
-#else
-       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
-#endif
+       COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
 
@@ -502,7 +512,7 @@ COMMAND_HANDLER(handle_flash_fill_command)
        if (count == 0)
                return ERROR_OK;
 
-       if (address + count >= bank->base + bank->size) {
+       if (address + count * wordsize > bank->base + bank->size) {
                LOG_ERROR("Cannot cross flash bank borders");
                return ERROR_FAIL;
        }
@@ -592,7 +602,7 @@ COMMAND_HANDLER(handle_flash_fill_command)
        }
 
        if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
-               command_print(CMD_CTX, "wrote %" PRIu32 " bytes to " TARGET_ADDR_FMT
+               command_print(CMD, "wrote %" PRIu32 " bytes to " TARGET_ADDR_FMT
                        " in %fs (%0.3f KiB/s)", size_bytes, address,
                        duration_elapsed(&bench), duration_kbps(&bench, size_bytes));
        }
@@ -706,7 +716,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
        free(buffer);
 
        if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
-               command_print(CMD_CTX, "wrote %zu bytes from file %s to flash bank %u"
+               command_print(CMD, "wrote %zu bytes from file %s to flash bank %u"
                        " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
                        length, CMD_ARGV[1], bank->bank_number, offset,
                        duration_elapsed(&bench), duration_kbps(&bench, length));
@@ -788,7 +798,7 @@ COMMAND_HANDLER(handle_flash_read_bank_command)
        }
 
        if (duration_measure(&bench) == ERROR_OK)
-               command_print(CMD_CTX, "wrote %zd bytes to file %s from flash bank %u"
+               command_print(CMD, "wrote %zd bytes to file %s from flash bank %u"
                        " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
                        written, CMD_ARGV[1], p->bank_number, offset,
                        duration_elapsed(&bench), duration_kbps(&bench, written));
@@ -890,23 +900,23 @@ COMMAND_HANDLER(handle_flash_verify_bank_command)
        }
 
        if (duration_measure(&bench) == ERROR_OK)
-               command_print(CMD_CTX, "read %zd bytes from file %s and flash bank %u"
+               command_print(CMD, "read %zd bytes from file %s and flash bank %u"
                        " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
                        length, CMD_ARGV[1], p->bank_number, offset,
                        duration_elapsed(&bench), duration_kbps(&bench, length));
 
        differ = memcmp(buffer_file, buffer_flash, length);
-       command_print(CMD_CTX, "contents %s", differ ? "differ" : "match");
+       command_print(CMD, "contents %s", differ ? "differ" : "match");
        if (differ) {
                uint32_t t;
                int diffs = 0;
                for (t = 0; t < length; t++) {
                        if (buffer_flash[t] == buffer_file[t])
                                continue;
-                       command_print(CMD_CTX, "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
+                       command_print(CMD, "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
                                        diffs, t + offset, buffer_flash[t], buffer_file[t]);
                        if (diffs++ >= 127) {
-                               command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
+                               command_print(CMD, "More than 128 errors, the rest are not printed.");
                                break;
                        }
                        keep_alive();
@@ -942,7 +952,7 @@ COMMAND_HANDLER(handle_flash_padded_value_command)
 
        COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], p->default_padded_value);
 
-       command_print(CMD_CTX, "Default padded value set to 0x%" PRIx8 " for flash bank %u", \
+       command_print(CMD, "Default padded value set to 0x%" PRIx8 " for flash bank %u", \
                        p->default_padded_value, p->bank_number);
 
        return retval;
@@ -975,7 +985,7 @@ static const struct command_registration flash_exec_command_handlers[] = {
                .name = "erase_sector",
                .handler = handle_flash_erase_command,
                .mode = COMMAND_EXEC,
-               .usage = "bank_id first_sector_num last_sector_num",
+               .usage = "bank_id first_sector_num (last_sector_num|'last')",
                .help = "Erase a range of sectors in a flash bank.",
        },
        {
@@ -1097,7 +1107,7 @@ COMMAND_HANDLER(handle_flash_bank_command)
        }
 
        const char *driver_name = CMD_ARGV[0];
-       struct flash_driver *driver = flash_driver_find_by_name(driver_name);
+       const struct flash_driver *driver = flash_driver_find_by_name(driver_name);
        if (NULL == driver) {
                /* no matching flash driver found */
                LOG_ERROR("flash driver '%s' not found", driver_name);
@@ -1126,7 +1136,7 @@ COMMAND_HANDLER(handle_flash_bank_command)
        c->name = strdup(bank_name);
        c->target = target;
        c->driver = driver;
-       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
+       COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[1], c->base);
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
        COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
        COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
@@ -1136,8 +1146,8 @@ COMMAND_HANDLER(handle_flash_bank_command)
        int retval;
        retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
        if (ERROR_OK != retval) {
-               LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 "; usage: %s",
-                       driver_name, c->base, driver->usage);
+               LOG_ERROR("'%s' driver rejected flash bank at " TARGET_ADDR_FMT
+                               "; usage: %s", driver_name, c->base, driver->usage);
                free(c);
                return retval;
        }
@@ -1157,7 +1167,7 @@ COMMAND_HANDLER(handle_flash_banks_command)
 
        unsigned n = 0;
        for (struct flash_bank *p = flash_bank_list(); p; p = p->next, n++) {
-               LOG_USER("#%d : %s (%s) at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", "
+               command_print(CMD, "#%d : %s (%s) at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32 ", "
                        "buswidth %u, chipwidth %u", p->bank_number,
                        p->name, p->driver->name, p->base, p->size,
                        p->bus_width, p->chip_width);
@@ -1229,12 +1239,14 @@ static const struct command_registration flash_config_command_handlers[] = {
                .mode = COMMAND_CONFIG,
                .handler = handle_flash_init_command,
                .help = "Initialize flash devices.",
+               .usage = "",
        },
        {
                .name = "banks",
                .mode = COMMAND_ANY,
                .handler = handle_flash_banks_command,
                .help = "Display table with information about flash banks.",
+               .usage = "",
        },
        {
                .name = "list",
@@ -1250,6 +1262,7 @@ static const struct command_registration flash_command_handlers[] = {
                .mode = COMMAND_ANY,
                .help = "NOR flash command group",
                .chain = flash_config_command_handlers,
+               .usage = "",
        },
        COMMAND_REGISTRATION_DONE
 };

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)