nand: factor init to 'nand init'
[openocd.git] / src / flash / nand.c
index 1249327ae03788f5993cd70e8b85c7f75e1f86d4..1c8c0c87c3cd1639fcf633ac12e8eef11044f76e 100644 (file)
 #endif
 
 #include "nand.h"
+#include "common.h"
 #include "time_support.h"
 #include "fileio.h"
 
-static int nand_read_page(struct nand_device_s *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
-//static int nand_read_plain(struct nand_device_s *nand, uint32_t address, uint8_t *data, uint32_t data_size);
+static int nand_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
+//static int nand_read_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size);
 
-static int nand_write_page(struct nand_device_s *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
+static int nand_write_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
 
 /* NAND flash controller
  */
+extern struct nand_flash_controller nonce_nand_controller;
 extern struct nand_flash_controller davinci_nand_controller;
 extern struct nand_flash_controller lpc3180_nand_controller;
 extern struct nand_flash_controller orion_nand_controller;
@@ -48,6 +50,7 @@ extern struct nand_flash_controller imx31_nand_flash_controller;
 
 static struct nand_flash_controller *nand_flash_controllers[] =
 {
+       &nonce_nand_controller,
        &davinci_nand_controller,
        &lpc3180_nand_controller,
        &orion_nand_controller,
@@ -61,8 +64,7 @@ static struct nand_flash_controller *nand_flash_controllers[] =
 };
 
 /* configured NAND devices and NAND Flash command handler */
-static nand_device_t *nand_devices = NULL;
-static command_t *nand_cmd;
+static struct nand_device *nand_devices = NULL;
 
 /*     Chip ID list
  *
@@ -74,7 +76,7 @@ static command_t *nand_cmd;
  *     256     256 Byte page size
  *     512     512 Byte page size
  */
-static nand_info_t nand_flash_ids[] =
+static struct nand_info nand_flash_ids[] =
 {
        /* start "museum" IDs */
        {"NAND 1MiB 5V 8-bit",          0x6e, 256, 1, 0x1000, 0},
@@ -154,7 +156,7 @@ static nand_info_t nand_flash_ids[] =
 
 /* Manufacturer ID list
  */
-static nand_manufacturer_t nand_manuf_ids[] =
+static struct nand_manufacturer nand_manuf_ids[] =
 {
        {0x0, "unknown"},
        {NAND_MFR_TOSHIBA, "Toshiba"},
@@ -173,7 +175,7 @@ static nand_manufacturer_t nand_manuf_ids[] =
  */
 
 #if 0
-static nand_ecclayout_t nand_oob_8 = {
+static struct nand_ecclayout nand_oob_8 = {
        .eccbytes = 3,
        .eccpos = {0, 1, 2},
        .oobfree = {
@@ -184,7 +186,7 @@ static nand_ecclayout_t nand_oob_8 = {
 };
 #endif
 
-static nand_ecclayout_t nand_oob_16 = {
+static struct nand_ecclayout nand_oob_16 = {
        .eccbytes = 6,
        .eccpos = {0, 1, 2, 3, 6, 7},
        .oobfree = {
@@ -192,7 +194,7 @@ static nand_ecclayout_t nand_oob_16 = {
                 . length = 8}}
 };
 
-static nand_ecclayout_t nand_oob_64 = {
+static struct nand_ecclayout nand_oob_64 = {
        .eccbytes = 24,
        .eccpos = {
                   40, 41, 42, 43, 44, 45, 46, 47,
@@ -203,94 +205,143 @@ static nand_ecclayout_t nand_oob_64 = {
                 .length = 38}}
 };
 
-/* nand device <nand_controller> [controller options]
- */
-COMMAND_HANDLER(handle_nand_device_command)
+COMMAND_HANDLER(handle_nand_list_drivers)
 {
-       int i;
-       int retval;
+       command_print(CMD_CTX, "Available NAND flash controller drivers:");
+       for (unsigned i = 0; nand_flash_controllers[i]; i++)
+               command_print(CMD_CTX, "  %s", nand_flash_controllers[i]->name);
+       return ERROR_OK;
+}
 
-       if (argc < 1)
+static COMMAND_HELPER(create_nand_device, const char *bank_name,
+               struct nand_flash_controller *controller)
+{
+       if (NULL != controller->commands)
+       {
+               int retval = register_commands(CMD_CTX, NULL,
+                               controller->commands);
+               if (ERROR_OK != retval)
+                       return retval;
+       }
+       struct nand_device *c = malloc(sizeof(struct nand_device));
+
+       c->name = strdup(bank_name);
+       c->controller = controller;
+       c->controller_priv = NULL;
+       c->manufacturer = NULL;
+       c->device = NULL;
+       c->bus_width = 0;
+       c->address_cycles = 0;
+       c->page_size = 0;
+       c->use_raw = 0;
+       c->next = NULL;
+
+       int retval = CALL_COMMAND_HANDLER(controller->nand_device_command, c);
+       if (ERROR_OK != retval)
        {
-               LOG_WARNING("incomplete flash device nand configuration");
-               return ERROR_FLASH_BANK_INVALID;
+               LOG_ERROR("'%s' driver rejected nand flash", controller->name);
+               free(c);
+               return ERROR_OK;
        }
 
-       for (i = 0; nand_flash_controllers[i]; i++)
+       if (nand_devices) {
+               struct nand_device *p = nand_devices;
+               while (p && p->next) p = p->next;
+               p->next = c;
+       } else
+               nand_devices = c;
+
+       return ERROR_OK;
+}
+
+COMMAND_HANDLER(handle_nand_device_command)
+{
+       if (CMD_ARGC < 1)
        {
-               nand_device_t *p, *c;
+               LOG_ERROR("incomplete nand device configuration");
+               return ERROR_FLASH_BANK_INVALID;
+       }
 
-               if (strcmp(args[0], nand_flash_controllers[i]->name) == 0)
-               {
-                       /* register flash specific commands */
-                       if ((retval = nand_flash_controllers[i]->register_commands(cmd_ctx)) != ERROR_OK)
-                       {
-                               LOG_ERROR("couldn't register '%s' commands", args[0]);
-                               return retval;
-                       }
+       // save name and increment (for compatibility) with drivers
+       const char *bank_name = *CMD_ARGV++;
+       CMD_ARGC--;
 
-                       c = malloc(sizeof(nand_device_t));
+       const char *driver_name = CMD_ARGV[0];
+       for (unsigned i = 0; nand_flash_controllers[i]; i++)
+       {
+               struct nand_flash_controller *controller = nand_flash_controllers[i];
+               if (strcmp(driver_name, controller->name) != 0)
+                       continue;
 
-                       c->controller = nand_flash_controllers[i];
-                       c->controller_priv = NULL;
-                       c->manufacturer = NULL;
-                       c->device = NULL;
-                       c->bus_width = 0;
-                       c->address_cycles = 0;
-                       c->page_size = 0;
-                       c->use_raw = 0;
-                       c->next = NULL;
+               return CALL_COMMAND_HANDLER(create_nand_device,
+                               bank_name, controller);
+       }
 
-                       retval = CALL_COMMAND_HANDLER(nand_flash_controllers[i]->nand_device_command, c);
-                       if (ERROR_OK != retval)
-                       {
-                               LOG_ERROR("'%s' driver rejected nand flash", c->controller->name);
-                               free(c);
-                               return ERROR_OK;
-                       }
+       LOG_ERROR("No valid NAND flash driver found (%s)", driver_name);
+       return CALL_COMMAND_HANDLER(handle_nand_list_drivers);
+}
 
-                       /* put NAND device in linked list */
-                       if (nand_devices)
-                       {
-                               /* find last flash device */
-                               for (p = nand_devices; p && p->next; p = p->next);
-                               if (p)
-                                       p->next = c;
-                       }
-                       else
-                       {
-                               nand_devices = c;
-                       }
 
-                       return ERROR_OK;
-               }
-       }
+COMMAND_HANDLER(handle_nand_init_command);
 
-       /* no valid NAND controller was found (i.e. the configuration option,
-        * didn't match one of the compiled-in controllers)
-        */
-       LOG_ERROR("No valid NAND flash controller found (%s)", args[0]);
-       LOG_ERROR("compiled-in NAND flash controllers:");
-       for (i = 0; nand_flash_controllers[i]; i++)
+static const struct command_registration nand_config_command_handlers[] = {
        {
-               LOG_ERROR("%i: %s", i, nand_flash_controllers[i]->name);
-       }
+               .name = "device",
+               .handler = &handle_nand_device_command,
+               .mode = COMMAND_CONFIG,
+               .help = "defines a new NAND bank",
+       },
+       {
+               .name = "drivers",
+               .handler = &handle_nand_list_drivers,
+               .mode = COMMAND_ANY,
+               .help = "lists available NAND drivers",
+       },
+       {
+               .name = "init",
+               .mode = COMMAND_CONFIG,
+               .handler = &handle_nand_init_command,
+               .help = "initialize NAND devices",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+static const struct command_registration nand_command_handlers[] = {
+       {
+               .name = "nand",
+               .mode = COMMAND_ANY,
+               .help = "NAND flash command group",
+               .chain = nand_config_command_handlers,
+       },
+       COMMAND_REGISTRATION_DONE
+};
 
-       return ERROR_OK;
+int nand_register_commands(struct command_context *cmd_ctx)
+{
+       return register_commands(cmd_ctx, NULL, nand_command_handlers);
 }
 
-int nand_register_commands(struct command_context_s *cmd_ctx)
+struct nand_device *get_nand_device_by_name(const char *name)
 {
-       nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, "NAND specific commands");
+       unsigned requested = get_flash_name_index(name);
+       unsigned found = 0;
 
-       register_command(cmd_ctx, nand_cmd, "device", handle_nand_device_command, COMMAND_CONFIG, NULL);
-
-       return ERROR_OK;
+       struct nand_device *nand;
+       for (nand = nand_devices; NULL != nand; nand = nand->next)
+       {
+               if (strcmp(nand->name, name) == 0)
+                       return nand;
+               if (!flash_driver_name_matches(nand->controller->name, name))
+                       continue;
+               if (++found < requested)
+                       continue;
+               return nand;
+       }
+       return NULL;
 }
 
-nand_device_t *get_nand_device_by_num(int num)
+struct nand_device *get_nand_device_by_num(int num)
 {
-       nand_device_t *p;
+       struct nand_device *p;
        int i = 0;
 
        for (p = nand_devices; p; p = p->next)
@@ -304,20 +355,25 @@ nand_device_t *get_nand_device_by_num(int num)
        return NULL;
 }
 
-int nand_command_get_device_by_num(struct command_context_s *cmd_ctx,
-               const char *str, nand_device_t **nand)
+COMMAND_HELPER(nand_command_get_device, unsigned name_index,
+               struct nand_device **nand)
 {
+       const char *str = CMD_ARGV[name_index];
+       *nand = get_nand_device_by_name(str);
+       if (*nand)
+               return ERROR_OK;
+
        unsigned num;
        COMMAND_PARSE_NUMBER(uint, str, num);
        *nand = get_nand_device_by_num(num);
        if (!*nand) {
-               command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", str);
+               command_print(CMD_CTX, "NAND flash device '%s' not found", str);
                return ERROR_INVALID_ARGUMENTS;
        }
        return ERROR_OK;
 }
 
-static int nand_build_bbt(struct nand_device_s *nand, int first, int last)
+static int nand_build_bbt(struct nand_device *nand, int first, int last)
 {
        uint32_t page = 0x0;
        int i;
@@ -351,7 +407,7 @@ static int nand_build_bbt(struct nand_device_s *nand, int first, int last)
        return ERROR_OK;
 }
 
-int nand_read_status(struct nand_device_s *nand, uint8_t *status)
+int nand_read_status(struct nand_device *nand, uint8_t *status)
 {
        if (!nand->device)
                return ERROR_NAND_DEVICE_NOT_PROBED;
@@ -376,7 +432,7 @@ int nand_read_status(struct nand_device_s *nand, uint8_t *status)
        return ERROR_OK;
 }
 
-static int nand_poll_ready(struct nand_device_s *nand, int timeout)
+static int nand_poll_ready(struct nand_device *nand, int timeout)
 {
        uint8_t status;
 
@@ -397,7 +453,7 @@ static int nand_poll_ready(struct nand_device_s *nand, int timeout)
        return (status & NAND_STATUS_READY) != 0;
 }
 
-int nand_probe(struct nand_device_s *nand)
+int nand_probe(struct nand_device *nand)
 {
        uint8_t manufacturer_id, device_id;
        uint8_t id_buff[6];
@@ -602,7 +658,7 @@ int nand_probe(struct nand_device_s *nand)
        }
 
        nand->num_blocks = (nand->device->chip_size * 1024) / (nand->erase_size / 1024);
-       nand->blocks = malloc(sizeof(nand_block_t) * nand->num_blocks);
+       nand->blocks = malloc(sizeof(struct nand_block) * nand->num_blocks);
 
        for (i = 0; i < nand->num_blocks; i++)
        {
@@ -615,7 +671,7 @@ int nand_probe(struct nand_device_s *nand)
        return ERROR_OK;
 }
 
-static int nand_erase(struct nand_device_s *nand, int first_block, int last_block)
+static int nand_erase(struct nand_device *nand, int first_block, int last_block)
 {
        int i;
        uint32_t page;
@@ -704,7 +760,7 @@ static int nand_erase(struct nand_device_s *nand, int first_block, int last_bloc
 }
 
 #if 0
-static int nand_read_plain(struct nand_device_s *nand, uint32_t address, uint8_t *data, uint32_t data_size)
+static int nand_read_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size)
 {
        uint8_t *page;
 
@@ -741,7 +797,7 @@ static int nand_read_plain(struct nand_device_s *nand, uint32_t address, uint8_t
        return ERROR_OK;
 }
 
-static int nand_write_plain(struct nand_device_s *nand, uint32_t address, uint8_t *data, uint32_t data_size)
+static int nand_write_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size)
 {
        uint8_t *page;
 
@@ -779,7 +835,7 @@ static int nand_write_plain(struct nand_device_s *nand, uint32_t address, uint8_
 }
 #endif
 
-int nand_write_page(struct nand_device_s *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
+int nand_write_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
 {
        uint32_t block;
 
@@ -796,7 +852,7 @@ int nand_write_page(struct nand_device_s *nand, uint32_t page, uint8_t *data, ui
                return nand->controller->write_page(nand, page, data, data_size, oob, oob_size);
 }
 
-static int nand_read_page(struct nand_device_s *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
+static int nand_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
 {
        if (!nand->device)
                return ERROR_NAND_DEVICE_NOT_PROBED;
@@ -807,7 +863,7 @@ static int nand_read_page(struct nand_device_s *nand, uint32_t page, uint8_t *da
                return nand->controller->read_page(nand, page, data, data_size, oob, oob_size);
 }
 
-int nand_read_page_raw(struct nand_device_s *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
+int nand_read_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
 {
        uint32_t i;
 
@@ -921,7 +977,7 @@ int nand_read_page_raw(struct nand_device_s *nand, uint32_t page, uint8_t *data,
        return ERROR_OK;
 }
 
-int nand_write_page_raw(struct nand_device_s *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
+int nand_write_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
 {
        uint32_t i;
        int retval;
@@ -1044,26 +1100,26 @@ int nand_write_page_raw(struct nand_device_s *nand, uint32_t page, uint8_t *data
 
 COMMAND_HANDLER(handle_nand_list_command)
 {
-       nand_device_t *p;
+       struct nand_device *p;
        int i;
 
        if (!nand_devices)
        {
-               command_print(cmd_ctx, "no NAND flash devices configured");
+               command_print(CMD_CTX, "no NAND flash devices configured");
                return ERROR_OK;
        }
 
        for (p = nand_devices, i = 0; p; p = p->next, i++)
        {
                if (p->device)
-                       command_print(cmd_ctx, "#%i: %s (%s) "
+                       command_print(CMD_CTX, "#%i: %s (%s) "
                                "pagesize: %i, buswidth: %i,\n\t"
                                "blocksize: %i, blocks: %i",
                                i, p->device->name, p->manufacturer->name,
                                p->page_size, p->bus_width,
                                p->erase_size, p->num_blocks);
                else
-                       command_print(cmd_ctx, "#%i: not probed", i);
+                       command_print(CMD_CTX, "#%i: not probed", i);
        }
 
        return ERROR_OK;
@@ -1076,12 +1132,7 @@ COMMAND_HANDLER(handle_nand_info_command)
        int first = -1;
        int last = -1;
 
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
-       if (ERROR_OK != retval)
-               return retval;
-
-       switch (argc) {
+       switch (CMD_ARGC) {
        default:
                return ERROR_COMMAND_SYNTAX_ERROR;
        case 1:
@@ -1089,19 +1140,24 @@ COMMAND_HANDLER(handle_nand_info_command)
                last = INT32_MAX;
                break;
        case 2:
-               COMMAND_PARSE_NUMBER(int, args[1], i);
+               COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], i);
                first = last = i;
                i = 0;
                break;
        case 3:
-               COMMAND_PARSE_NUMBER(int, args[1], first);
-               COMMAND_PARSE_NUMBER(int, args[2], last);
+               COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], first);
+               COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], last);
                break;
        }
 
+       struct nand_device *p;
+       int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
+       if (ERROR_OK != retval)
+               return retval;
+
        if (NULL == p->device)
        {
-               command_print(cmd_ctx, "#%s: not probed", args[0]);
+               command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
                return ERROR_OK;
        }
 
@@ -1111,7 +1167,7 @@ COMMAND_HANDLER(handle_nand_info_command)
        if (last >= p->num_blocks)
                last = p->num_blocks - 1;
 
-       command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
+       command_print(CMD_CTX, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
                i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
 
        for (j = first; j <= last; j++)
@@ -1132,7 +1188,7 @@ COMMAND_HANDLER(handle_nand_info_command)
                else
                        bad_state = " (block condition unknown)";
 
-               command_print(cmd_ctx,
+               command_print(CMD_CTX,
                              "\t#%i: 0x%8.8" PRIx32 " (%" PRId32 "kB) %s%s",
                              j,
                              p->blocks[j].offset,
@@ -1146,27 +1202,27 @@ COMMAND_HANDLER(handle_nand_info_command)
 
 COMMAND_HANDLER(handle_nand_probe_command)
 {
-       if (argc != 1)
+       if (CMD_ARGC != 1)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
+       struct nand_device *p;
+       int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
        if (ERROR_OK != retval)
                return retval;
 
        if ((retval = nand_probe(p)) == ERROR_OK)
        {
-               command_print(cmd_ctx, "NAND flash device '%s' found", p->device->name);
+               command_print(CMD_CTX, "NAND flash device '%s' found", p->device->name);
        }
        else if (retval == ERROR_NAND_OPERATION_FAILED)
        {
-               command_print(cmd_ctx, "probing failed for NAND flash device");
+               command_print(CMD_CTX, "probing failed for NAND flash device");
        }
        else
        {
-               command_print(cmd_ctx, "unknown error when probing NAND flash device");
+               command_print(CMD_CTX, "unknown error when probing NAND flash device");
        }
 
        return ERROR_OK;
@@ -1174,14 +1230,14 @@ COMMAND_HANDLER(handle_nand_probe_command)
 
 COMMAND_HANDLER(handle_nand_erase_command)
 {
-       if (argc != 1 && argc != 3)
+       if (CMD_ARGC != 1 && CMD_ARGC != 3)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        }
 
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
+       struct nand_device *p;
+       int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
        if (ERROR_OK != retval)
                return retval;
 
@@ -1189,14 +1245,14 @@ COMMAND_HANDLER(handle_nand_erase_command)
        unsigned long length;
 
        /* erase specified part of the chip; or else everything */
-       if (argc == 3) {
+       if (CMD_ARGC == 3) {
                unsigned long size = p->erase_size * p->num_blocks;
 
-               COMMAND_PARSE_NUMBER(ulong, args[1], offset);
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], offset);
                if ((offset % p->erase_size) != 0 || offset >= size)
                        return ERROR_INVALID_ARGUMENTS;
 
-               COMMAND_PARSE_NUMBER(ulong, args[2], length);
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], length);
                if ((length == 0) || (length % p->erase_size) != 0
                                || (length + offset) > size)
                        return ERROR_INVALID_ARGUMENTS;
@@ -1211,18 +1267,18 @@ COMMAND_HANDLER(handle_nand_erase_command)
        retval = nand_erase(p, offset, offset + length - 1);
        if (retval == ERROR_OK)
        {
-               command_print(cmd_ctx, "erased blocks %lu to %lu "
+               command_print(CMD_CTX, "erased blocks %lu to %lu "
                                "on NAND flash device #%s '%s'",
                                offset, offset + length,
-                               args[0], p->device->name);
+                               CMD_ARGV[0], p->device->name);
        }
        else if (retval == ERROR_NAND_OPERATION_FAILED)
        {
-               command_print(cmd_ctx, "erase failed");
+               command_print(CMD_CTX, "erase failed");
        }
        else
        {
-               command_print(cmd_ctx, "unknown error when erasing NAND flash device");
+               command_print(CMD_CTX, "unknown error when erasing NAND flash device");
        }
 
        return ERROR_OK;
@@ -1233,28 +1289,28 @@ COMMAND_HANDLER(handle_nand_check_bad_blocks_command)
        int first = -1;
        int last = -1;
 
-       if ((argc < 1) || (argc > 3) || (argc == 2))
+       if ((CMD_ARGC < 1) || (CMD_ARGC > 3) || (CMD_ARGC == 2))
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        }
 
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
+       struct nand_device *p;
+       int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
        if (ERROR_OK != retval)
                return retval;
 
-       if (argc == 3)
+       if (CMD_ARGC == 3)
        {
                unsigned long offset;
                unsigned long length;
 
-               COMMAND_PARSE_NUMBER(ulong, args[1], offset);
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], offset);
                if (offset % p->erase_size)
                        return ERROR_INVALID_ARGUMENTS;
                offset /= p->erase_size;
 
-               COMMAND_PARSE_NUMBER(ulong, args[2], length);
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], length);
                if (length % p->erase_size)
                        return ERROR_INVALID_ARGUMENTS;
 
@@ -1268,373 +1324,499 @@ COMMAND_HANDLER(handle_nand_check_bad_blocks_command)
        retval = nand_build_bbt(p, first, last);
        if (retval == ERROR_OK)
        {
-               command_print(cmd_ctx, "checked NAND flash device for bad blocks, "
+               command_print(CMD_CTX, "checked NAND flash device for bad blocks, "
                                "use \"nand info\" command to list blocks");
        }
        else if (retval == ERROR_NAND_OPERATION_FAILED)
        {
-               command_print(cmd_ctx, "error when checking for bad blocks on "
+               command_print(CMD_CTX, "error when checking for bad blocks on "
                                "NAND flash device");
        }
        else
        {
-               command_print(cmd_ctx, "unknown error when checking for bad "
+               command_print(CMD_CTX, "unknown error when checking for bad "
                                "blocks on NAND flash device");
        }
 
        return ERROR_OK;
 }
 
-COMMAND_HANDLER(handle_nand_write_command)
-{
-       uint32_t offset;
-       uint32_t binary_size;
-       uint32_t buf_cnt;
-       enum oob_formats oob_format = NAND_OOB_NONE;
+struct nand_fileio_state {
+       uint32_t address;
+       uint32_t size;
+
+       uint8_t *page;
+       uint32_t page_size;
 
+       enum oob_formats oob_format;
+       uint8_t *oob;
+       uint32_t oob_size;
+
+       const int *eccpos;
+
+       bool file_opened;
        struct fileio fileio;
 
+       struct duration bench;
+};
+
+static void nand_fileio_init(struct nand_fileio_state *state)
+{
+       memset(state, 0, sizeof(*state));
+       state->oob_format = NAND_OOB_NONE;
+}
 
-       if (argc < 3)
+static int nand_fileio_start(struct command_context *cmd_ctx,
+               struct nand_device *nand, const char *filename, int filemode,
+               struct nand_fileio_state *state)
+{
+       if (state->address % nand->page_size)
        {
+               command_print(cmd_ctx, "only page-aligned addresses are supported");
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
-       if (ERROR_OK != retval)
-               return retval;
-
-       uint8_t *page = NULL;
-       uint32_t page_size = 0;
-       uint8_t *oob = NULL;
-       uint32_t oob_size = 0;
-       const int *eccpos = NULL;
-
-       COMMAND_PARSE_NUMBER(u32, args[2], offset);
+       duration_start(&state->bench);
 
-       if (argc > 3)
+       if (NULL != filename)
        {
-               for (unsigned i = 3; i < argc; i++)
+               int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
+               if (ERROR_OK != retval)
                {
-                       if (!strcmp(args[i], "oob_raw"))
-                               oob_format |= NAND_OOB_RAW;
-                       else if (!strcmp(args[i], "oob_only"))
-                               oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
-                       else if (!strcmp(args[i], "oob_softecc"))
-                               oob_format |= NAND_OOB_SW_ECC;
-                       else if (!strcmp(args[i], "oob_softecc_kw"))
-                               oob_format |= NAND_OOB_SW_ECC_KW;
-                       else
-                       {
-                               command_print(cmd_ctx, "unknown option: %s", args[i]);
-                               return ERROR_COMMAND_SYNTAX_ERROR;
-                       }
+                       const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
+                       command_print(cmd_ctx, "failed to open '%s' for %s access",
+                                       filename, msg);
+                       return retval;
                }
+               state->file_opened = true;
        }
 
-       struct duration bench;
-       duration_start(&bench);
+       if (!(state->oob_format & NAND_OOB_ONLY))
+       {
+               state->page_size = nand->page_size;
+               state->page = malloc(nand->page_size);
+       }
 
-       if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
+       if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
        {
-               return ERROR_OK;
+               if (nand->page_size == 512)
+               {
+                       state->oob_size = 16;
+                       state->eccpos = nand_oob_16.eccpos;
+               }
+               else if (nand->page_size == 2048)
+               {
+                       state->oob_size = 64;
+                       state->eccpos = nand_oob_64.eccpos;
+               }
+               state->oob = malloc(state->oob_size);
        }
 
-       buf_cnt = binary_size = fileio.size;
+       return ERROR_OK;
+}
+static int nand_fileio_cleanup(struct nand_fileio_state *state)
+{
+       if (state->file_opened)
+               fileio_close(&state->fileio);
 
-       if (!(oob_format & NAND_OOB_ONLY))
+       if (state->oob)
        {
-               page_size = p->page_size;
-               page = malloc(p->page_size);
+               free(state->oob);
+               state->oob = NULL;
        }
-
-       if (oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
+       if (state->page)
        {
-               if (p->page_size == 512) {
-                       oob_size = 16;
-                       eccpos = nand_oob_16.eccpos;
-               } else if (p->page_size == 2048) {
-                       oob_size = 64;
-                       eccpos = nand_oob_64.eccpos;
-               }
-               oob = malloc(oob_size);
+               free(state->page);
+               state->page = NULL;
        }
+       return ERROR_OK;
+}
+static int nand_fileio_finish(struct nand_fileio_state *state)
+{
+       nand_fileio_cleanup(state);
+       return duration_measure(&state->bench);
+}
 
-       if (offset % p->page_size)
+static COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
+               struct nand_device **dev, enum fileio_access filemode,
+               bool need_size, bool sw_ecc)
+{
+       nand_fileio_init(state);
+
+       unsigned minargs = need_size ? 4 : 3;
+       if (CMD_ARGC < minargs)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       struct nand_device *nand;
+       int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
+       if (ERROR_OK != retval)
+               return retval;
+
+       if (NULL == nand->device)
        {
-               command_print(cmd_ctx, "only page size aligned offsets and sizes are supported");
-               fileio_close(&fileio);
-               free(oob);
-               free(page);
+               command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
                return ERROR_OK;
        }
 
-       while (buf_cnt > 0)
+       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
+       if (need_size)
        {
-               uint32_t size_read;
-
-               if (NULL != page)
-               {
-                       fileio_read(&fileio, page_size, page, &size_read);
-                       buf_cnt -= size_read;
-                       if (size_read < page_size)
+                       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
+                       if (state->size % nand->page_size)
                        {
-                               memset(page + size_read, 0xff, page_size - size_read);
+                               command_print(CMD_CTX, "only page-aligned sizes are supported");
+                               return ERROR_COMMAND_SYNTAX_ERROR;
                        }
-               }
+       }
 
-               if (oob_format & NAND_OOB_SW_ECC)
-               {
-                       uint32_t i, j;
-                       uint8_t ecc[3];
-                       memset(oob, 0xff, oob_size);
-                       for (i = 0, j = 0; i < page_size; i += 256) {
-                               nand_calculate_ecc(p, page + i, ecc);
-                               oob[eccpos[j++]] = ecc[0];
-                               oob[eccpos[j++]] = ecc[1];
-                               oob[eccpos[j++]] = ecc[2];
-                       }
-               } else if (oob_format & NAND_OOB_SW_ECC_KW)
-               {
-                       /*
-                        * In this case eccpos is not used as
-                        * the ECC data is always stored contigously
-                        * at the end of the OOB area.  It consists
-                        * of 10 bytes per 512-byte data block.
-                        */
-                       uint32_t i;
-                       uint8_t *ecc = oob + oob_size - page_size/512 * 10;
-                       memset(oob, 0xff, oob_size);
-                       for (i = 0; i < page_size; i += 512) {
-                               nand_calculate_ecc_kw(p, page + i, ecc);
-                               ecc += 10;
-                       }
-               }
-               else if (NULL != oob)
+       if (CMD_ARGC > minargs)
+       {
+               for (unsigned i = minargs; i < CMD_ARGC; i++)
                {
-                       fileio_read(&fileio, oob_size, oob, &size_read);
-                       buf_cnt -= size_read;
-                       if (size_read < oob_size)
+                       if (!strcmp(CMD_ARGV[i], "oob_raw"))
+                               state->oob_format |= NAND_OOB_RAW;
+                       else if (!strcmp(CMD_ARGV[i], "oob_only"))
+                               state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
+                       else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
+                               state->oob_format |= NAND_OOB_SW_ECC;
+                       else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
+                               state->oob_format |= NAND_OOB_SW_ECC_KW;
+                       else
                        {
-                               memset(oob + size_read, 0xff, oob_size - size_read);
+                               command_print(CMD_CTX, "unknown option: %s", CMD_ARGV[i]);
+                               return ERROR_COMMAND_SYNTAX_ERROR;
                        }
                }
+       }
 
-               if (nand_write_page(p, offset / p->page_size, page, page_size, oob, oob_size) != ERROR_OK)
-               {
-                       command_print(cmd_ctx, "failed writing file %s to NAND flash %s at offset 0x%8.8" PRIx32 "",
-                               args[1], args[0], offset);
-
-                       fileio_close(&fileio);
-                       free(oob);
-                       free(page);
+       retval = nand_fileio_start(CMD_CTX, nand, CMD_ARGV[1], filemode, state);
+       if (ERROR_OK != retval)
+               return retval;
 
-                       return ERROR_OK;
-               }
-               offset += page_size;
-       }
+       if (!need_size)
+               state->size = state->fileio.size;
 
-       fileio_close(&fileio);
-       free(oob);
-       free(page);
-       oob = NULL;
-       page = NULL;
-       if (duration_measure(&bench) == ERROR_OK)
-       {
-               command_print(cmd_ctx, "wrote file %s to NAND flash %s "
-                       "up to offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
-                       args[1], args[0], offset, duration_elapsed(&bench),
-                       duration_kbps(&bench, fileio.size));
-       }
+       *dev = nand;
 
        return ERROR_OK;
 }
 
-COMMAND_HANDLER(handle_nand_dump_command)
+/**
+ * @returns If no error occurred, returns number of bytes consumed;
+ * otherwise, returns a negative error code.)
+ */
+static int nand_fileio_read(struct nand_device *nand,
+               struct nand_fileio_state *s)
 {
-       if (argc < 4)
-       {
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
-       if (ERROR_OK != retval)
-               return retval;
+       size_t total_read = 0;
+       size_t one_read;
 
-       if (NULL == p->device)
+       if (NULL != s->page)
        {
-               command_print(cmd_ctx, "#%s: not probed", args[0]);
-               return ERROR_OK;
+               fileio_read(&s->fileio, s->page_size, s->page, &one_read);
+               if (one_read < s->page_size)
+                       memset(s->page + one_read, 0xff, s->page_size - one_read);
+               total_read += one_read;
        }
 
-       struct fileio fileio;
-
-       uint8_t *page = NULL;
-       uint32_t page_size = 0;
-       uint8_t *oob = NULL;
-       uint32_t oob_size = 0;
-       uint32_t address;
-       COMMAND_PARSE_NUMBER(u32, args[2], address);
-       uint32_t size;
-       COMMAND_PARSE_NUMBER(u32, args[3], size);
-       uint32_t bytes_done = 0;
-       enum oob_formats oob_format = NAND_OOB_NONE;
-
-       if (argc > 4)
+       if (s->oob_format & NAND_OOB_SW_ECC)
        {
-               for (unsigned i = 4; i < argc; i++)
+               uint8_t ecc[3];
+               memset(s->oob, 0xff, s->oob_size);
+               for (uint32_t i = 0, j = 0; i < s->page_size; i += 256)
                {
-                       if (!strcmp(args[i], "oob_raw"))
-                               oob_format |= NAND_OOB_RAW;
-                       else if (!strcmp(args[i], "oob_only"))
-                               oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
-                       else
-                               command_print(cmd_ctx, "unknown option: '%s'", args[i]);
+                       nand_calculate_ecc(nand, s->page + i, ecc);
+                       s->oob[s->eccpos[j++]] = ecc[0];
+                       s->oob[s->eccpos[j++]] = ecc[1];
+                       s->oob[s->eccpos[j++]] = ecc[2];
                }
        }
-
-       if ((address % p->page_size) || (size % p->page_size))
+       else if (s->oob_format & NAND_OOB_SW_ECC_KW)
        {
-               command_print(cmd_ctx, "only page size aligned addresses and sizes are supported");
-               return ERROR_OK;
+               /*
+                * In this case eccpos is not used as
+                * the ECC data is always stored contigously
+                * at the end of the OOB area.  It consists
+                * of 10 bytes per 512-byte data block.
+                */
+               uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
+               memset(s->oob, 0xff, s->oob_size);
+               for (uint32_t i = 0; i < s->page_size; i += 512)
+               {
+                       nand_calculate_ecc_kw(nand, s->page + i, ecc);
+                       ecc += 10;
+               }
        }
-
-       if (!(oob_format & NAND_OOB_ONLY))
+       else if (NULL != s->oob)
        {
-               page_size = p->page_size;
-               page = malloc(p->page_size);
+               fileio_read(&s->fileio, s->oob_size, s->oob, &one_read);
+               if (one_read < s->oob_size)
+                       memset(s->oob + one_read, 0xff, s->oob_size - one_read);
+               total_read += one_read;
        }
+       return total_read;
+}
 
-       if (oob_format & NAND_OOB_RAW)
+COMMAND_HANDLER(handle_nand_write_command)
+{
+       struct nand_device *nand = NULL;
+       struct nand_fileio_state s;
+       int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
+                       &s, &nand, FILEIO_READ, false, true);
+       if (ERROR_OK != retval)
+               return retval;
+
+       uint32_t total_bytes = s.size;
+       while (s.size > 0)
        {
-               if (p->page_size == 512)
-                       oob_size = 16;
-               else if (p->page_size == 2048)
-                       oob_size = 64;
-               oob = malloc(oob_size);
+               int bytes_read = nand_fileio_read(nand, &s);
+               if (bytes_read <= 0)
+               {
+                       command_print(CMD_CTX, "error while reading file");
+                       return nand_fileio_cleanup(&s);
+               }
+               s.size -= bytes_read;
+
+               retval = nand_write_page(nand, s.address / nand->page_size,
+                               s.page, s.page_size, s.oob, s.oob_size);
+               if (ERROR_OK != retval)
+               {
+                       command_print(CMD_CTX, "failed writing file %s "
+                               "to NAND flash %s at offset 0x%8.8" PRIx32,
+                               CMD_ARGV[1], CMD_ARGV[0], s.address);
+                       return nand_fileio_cleanup(&s);
+               }
+               s.address += s.page_size;
        }
 
-       if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
+       if (nand_fileio_finish(&s))
        {
-               return ERROR_OK;
+               command_print(CMD_CTX, "wrote file %s to NAND flash %s up to "
+                               "offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
+                               CMD_ARGV[1], CMD_ARGV[0], s.address, duration_elapsed(&s.bench),
+                               duration_kbps(&s.bench, total_bytes));
        }
+       return ERROR_OK;
+}
 
-       struct duration bench;
-       duration_start(&bench);
+COMMAND_HANDLER(handle_nand_verify_command)
+{
+       struct nand_device *nand = NULL;
+       struct nand_fileio_state file;
+       int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
+                       &file, &nand, FILEIO_READ, false, true);
+       if (ERROR_OK != retval)
+               return retval;
+
+       struct nand_fileio_state dev;
+       nand_fileio_init(&dev);
+       dev.address = file.address;
+       dev.size = file.size;
+       dev.oob_format = file.oob_format;
+       retval = nand_fileio_start(CMD_CTX, nand, NULL, FILEIO_NONE, &dev);
+       if (ERROR_OK != retval)
+               return retval;
 
-       while (size > 0)
+       while (file.size > 0)
        {
-               uint32_t size_written;
-               if ((retval = nand_read_page(p, address / p->page_size, page, page_size, oob, oob_size)) != ERROR_OK)
+               int retval = nand_read_page(nand, dev.address / dev.page_size,
+                               dev.page, dev.page_size, dev.oob, dev.oob_size);
+               if (ERROR_OK != retval)
                {
-                       command_print(cmd_ctx, "reading NAND flash page failed");
-                       free(page);
-                       free(oob);
-                       fileio_close(&fileio);
-                       return ERROR_OK;
+                       command_print(CMD_CTX, "reading NAND flash page failed");
+                       nand_fileio_cleanup(&dev);
+                       return nand_fileio_cleanup(&file);
                }
 
-               if (NULL != page)
+               int bytes_read = nand_fileio_read(nand, &file);
+               if (bytes_read <= 0)
                {
-                       fileio_write(&fileio, page_size, page, &size_written);
-                       bytes_done += page_size;
+                       command_print(CMD_CTX, "error while reading file");
+                       nand_fileio_cleanup(&dev);
+                       return nand_fileio_cleanup(&file);
                }
 
-               if (NULL != oob)
+               if ((dev.page && memcmp(dev.page, file.page, dev.page_size)) ||
+                   (dev.oob && memcmp(dev.oob, file.oob, dev.oob_size)) )
                {
-                       fileio_write(&fileio, oob_size, oob, &size_written);
-                       bytes_done += oob_size;
+                       command_print(CMD_CTX, "NAND flash contents differ "
+                                               "at 0x%8.8" PRIx32, dev.address);
+                       nand_fileio_cleanup(&dev);
+                       return nand_fileio_cleanup(&file);
                }
 
-               size -= p->page_size;
-               address += p->page_size;
+               file.size -= bytes_read;
+               dev.address += nand->page_size;
        }
 
-       free(page);
-       page = NULL;
-       free(oob);
-       oob = NULL;
-       fileio_close(&fileio);
+       if (nand_fileio_finish(&file) == ERROR_OK)
+       {
+               command_print(CMD_CTX, "verified file %s in NAND flash %s "
+                               "up to offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
+                               CMD_ARGV[1], CMD_ARGV[0], dev.address, duration_elapsed(&file.bench),
+                               duration_kbps(&file.bench, dev.size));
+       }
+
+       return nand_fileio_cleanup(&dev);
+}
+
+COMMAND_HANDLER(handle_nand_dump_command)
+{
+       struct nand_device *nand = NULL;
+       struct nand_fileio_state s;
+       int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
+                       &s, &nand, FILEIO_WRITE, true, false);
+       if (ERROR_OK != retval)
+               return retval;
 
-       if (duration_measure(&bench) == ERROR_OK)
+       while (s.size > 0)
        {
-               command_print(cmd_ctx, "dumped %lld byte in %fs (%0.3f kb/s)",
-                       fileio.size, duration_elapsed(&bench),
-                       duration_kbps(&bench, fileio.size));
+               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)
+               {
+                       command_print(CMD_CTX, "reading NAND flash page failed");
+                       return nand_fileio_cleanup(&s);
+               }
+
+               if (NULL != s.page)
+                       fileio_write(&s.fileio, s.page_size, s.page, &size_written);
+
+               if (NULL != s.oob)
+                       fileio_write(&s.fileio, s.oob_size, s.oob, &size_written);
+
+               s.size -= nand->page_size;
+               s.address += nand->page_size;
        }
 
+       if (nand_fileio_finish(&s) == ERROR_OK)
+       {
+               command_print(CMD_CTX, "dumped %zu bytes in %fs (%0.3f kb/s)", 
+                               s.fileio.size, duration_elapsed(&s.bench),
+                               duration_kbps(&s.bench, s.fileio.size));
+       }
        return ERROR_OK;
 }
 
 COMMAND_HANDLER(handle_nand_raw_access_command)
 {
-       if ((argc < 1) || (argc > 2))
+       if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       nand_device_t *p;
-       int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
+       struct nand_device *p;
+       int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
        if (ERROR_OK != retval)
                return retval;
 
        if (NULL == p->device)
        {
-               command_print(cmd_ctx, "#%s: not probed", args[0]);
+               command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
                return ERROR_OK;
        }
 
-       if (argc == 2)
-       {
-               if (strcmp("enable", args[1]) == 0)
-                       p->use_raw = 1;
-               else if (strcmp("disable", args[1]) == 0)
-                       p->use_raw = 0;
-               else
-                       return ERROR_COMMAND_SYNTAX_ERROR;
-       }
+       if (CMD_ARGC == 2)
+               COMMAND_PARSE_ENABLE(CMD_ARGV[1], p->use_raw);
 
        const char *msg = p->use_raw ? "enabled" : "disabled";
-       command_print(cmd_ctx, "raw access is %s", msg);
+       command_print(CMD_CTX, "raw access is %s", msg);
 
        return ERROR_OK;
 }
 
-int nand_init(struct command_context_s *cmd_ctx)
+static const struct command_registration nand_exec_command_handlers[] = {
+       {
+               .name = "list",
+               .handler = &handle_nand_list_command,
+               .mode = COMMAND_EXEC,
+               .help = "list configured NAND flash devices",
+       },
+       {
+               .name = "info",
+               .handler = &handle_nand_info_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank>",
+               .help = "print info about a NAND flash device",
+       },
+       {
+               .name = "probe",
+               .handler = &handle_nand_probe_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank>",
+               .help = "identify NAND flash device <num>",
+
+       },
+       {
+               .name = "check_bad_blocks",
+               .handler = &handle_nand_check_bad_blocks_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank> [<offset> <length>]",
+               .help = "check NAND flash device <num> for bad blocks",
+       },
+       {
+               .name = "erase",
+               .handler = &handle_nand_erase_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank> [<offset> <length>]",
+               .help = "erase blocks on NAND flash device",
+       },
+       {
+               .name = "dump",
+               .handler = &handle_nand_dump_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank> <filename> <offset> <length> "
+                       "[oob_raw | oob_only]",
+               .help = "dump from NAND flash device",
+       },
+       {
+               .name = "verify",
+               .handler = &handle_nand_verify_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank> <filename> <offset> "
+                       "[oob_raw | oob_only | oob_softecc | oob_softecc_kw]",
+               .help = "verify NAND flash device",
+       },
+       {
+               .name = "write",
+               .handler = &handle_nand_write_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<bank> <filename> <offset> "
+                       "[oob_raw | oob_only | oob_softecc | oob_softecc_kw]",
+               .help = "write to NAND flash device",
+       },
+       {
+               .name = "raw_access",
+               .handler = &handle_nand_raw_access_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<num> ['enable'|'disable']",
+               .help = "raw access to NAND flash device",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+
+int nand_init(struct command_context *cmd_ctx)
 {
        if (!nand_devices)
                return ERROR_OK;
+       struct command *parent = command_find_in_context(cmd_ctx, "nand");
+       return register_commands(cmd_ctx, parent, nand_exec_command_handlers);
+}
+
+COMMAND_HANDLER(handle_nand_init_command)
+{
+       if (CMD_ARGC != 0)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
-       register_command(cmd_ctx, nand_cmd, "list",
-                       handle_nand_list_command, COMMAND_EXEC,
-                       "list configured NAND flash devices");
-       register_command(cmd_ctx, nand_cmd, "info",
-                       handle_nand_info_command, COMMAND_EXEC,
-                       "print info about NAND flash device <num>");
-       register_command(cmd_ctx, nand_cmd, "probe",
-                       handle_nand_probe_command, COMMAND_EXEC,
-                       "identify NAND flash device <num>");
-
-       register_command(cmd_ctx, nand_cmd, "check_bad_blocks",
-                       handle_nand_check_bad_blocks_command, COMMAND_EXEC,
-                       "check NAND flash device <num> for bad blocks [<offset> <length>]");
-       register_command(cmd_ctx, nand_cmd, "erase",
-                       handle_nand_erase_command, COMMAND_EXEC,
-                       "erase blocks on NAND flash device <num> [<offset> <length>]");
-       register_command(cmd_ctx, nand_cmd, "dump",
-                       handle_nand_dump_command, COMMAND_EXEC,
-                       "dump from NAND flash device <num> <filename> "
-                        "<offset> <length> [oob_raw | oob_only]");
-       register_command(cmd_ctx, nand_cmd, "write",
-                       handle_nand_write_command, COMMAND_EXEC,
-                       "write to NAND flash device <num> <filename> <offset> "
-                       "[oob_raw | oob_only | oob_softecc | oob_softecc_kw]");
-
-       register_command(cmd_ctx, nand_cmd, "raw_access",
-                       handle_nand_raw_access_command, COMMAND_EXEC,
-                       "raw access to NAND flash device <num> ['enable'|'disable']");
+       static bool nand_initialized = false;
+       if (nand_initialized)
+       {
+               LOG_INFO("'nand init' has already been called");
+               return ERROR_OK;
+       }
+       nand_initialized = true;
 
-       return ERROR_OK;
+       LOG_DEBUG("Initializing NAND devices...");
+       return nand_init(CMD_CTX);
 }

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)