allow flash/nand banks commands to accept names
authorZachary T Welch <zw@superlucidity.net>
Tue, 17 Nov 2009 21:04:49 +0000 (13:04 -0800)
committerZachary T Welch <zw@superlucidity.net>
Thu, 19 Nov 2009 21:39:41 +0000 (13:39 -0800)
Add get_flash_bank_by_name (and get_nand_device_by_name) helpers
to retrieves struct flash_bank * (struct nand_device *) given a
driver name and an (optional) driver-specific bank index.

These are used to extend flash_command_get_bank_by_num (and
nand_command_get_device_by_num) to allow all flash (nand) commands to
reference defined banks by name, not just by number.

To avoid some code duplication, add the flash/common.[ch] files to hold
functionality common to both types driver.  The first two methods are
helpers for the above routines to find a bank specified by a "name" or
"name.index" string.  get_flash_name_index() finds the '.index' portion,
while flash_driver_name_matches() performs the string portion matching.

src/flash/Makefile.am
src/flash/common.c [new file with mode: 0644]
src/flash/common.h [new file with mode: 0644]
src/flash/flash.c
src/flash/flash.h
src/flash/nand.c
src/flash/nand.h

index b687182d4bd114890f8c6c0e858ef911435c0795..840323028ca1cbe26736f240ade3d7911dfc9169 100644 (file)
@@ -11,6 +11,7 @@ libflash_la_SOURCES = \
        mflash.c
 
 FLASH_SRCS = \
+       common.c \
        cfi.c \
        non_cfi.c \
        faux.c \
@@ -60,6 +61,7 @@ noinst_HEADERS = \
        at91sam3.h \
        avrf.h \
        cfi.h \
+       common.h \
        flash.h \
        lpc2000.h \
        lpc288x.h \
diff --git a/src/flash/common.c b/src/flash/common.c
new file mode 100644 (file)
index 0000000..253ed9d
--- /dev/null
@@ -0,0 +1,46 @@
+/***************************************************************************
+ *   Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net>          *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "common.h"
+#include "log.h"
+
+unsigned get_flash_name_index(const char *name)
+{
+       const char *index = strchr(name, '.');
+       if (NULL == index)
+               return 0;
+       unsigned requested;
+       int retval = parse_uint(index + 1, &requested);
+       // detect parsing error by forcing past end of bank list
+       return (ERROR_OK == retval) ? requested : ~0U;
+}
+
+bool flash_driver_name_matches(const char *name, const char *expected)
+{
+       unsigned blen = strlen(name);
+       // only match up to the length of the driver name...
+       if (strncmp(name, expected, blen) != 0)
+               return false;
+
+       // ...then check that name terminates at this spot.
+       return expected[blen] == '.' || expected[blen] == '\0';
+}
diff --git a/src/flash/common.h b/src/flash/common.h
new file mode 100644 (file)
index 0000000..1fd0d77
--- /dev/null
@@ -0,0 +1,39 @@
+/***************************************************************************
+ *   Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net>          *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifndef FLASH_COMMON_H
+#define FLASH_COMMON_H
+
+#include "types.h"
+
+/**
+ * Parses the optional '.index' portion of a flash bank identifier.
+ * @param name The desired driver name, passed by the user.
+ * @returns The parsed index request, or 0 if not present.  If the
+ * name provides a suffix but it does not parse as an unsigned integer, 
+ * the routine returns ~0U.  This will prevent further matching.
+ */ 
+unsigned get_flash_name_index(const char *name);
+/**
+ * Attempt to match the @c expected name with the @c name of a driver.
+ * @param name The name of the driver (from the bank's device structure).
+ * @param expected The expected driver name, passed by the user.
+ */
+bool flash_driver_name_matches(const char *name, const char *expected);
+
+#endif // FLASH_COMMON_H
index 98e5ee0e31e871ad8fceb16c91c24a1598ed71a1..071503f162c360fe589ba1fce12e39455788e1d3 100644 (file)
@@ -28,6 +28,7 @@
 #endif
 
 #include "flash.h"
+#include "common.h"
 #include "image.h"
 #include "time_support.h"
 
@@ -180,6 +181,23 @@ int flash_get_bank_count(void)
        return i;
 }
 
+struct flash_bank *get_flash_bank_by_name(const char *name)
+{
+       unsigned requested = get_flash_name_index(name);
+       unsigned found = 0;
+
+       struct flash_bank *bank;
+       for (bank = flash_banks; NULL != bank; bank = bank->next)
+       {
+               if (!flash_driver_name_matches(bank->driver->name, name))
+                       continue;
+               if (++found < requested)
+                       continue;
+               return bank;
+       }
+       return NULL;
+}
+
 struct flash_bank *get_flash_bank_by_num(int num)
 {
        struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
@@ -198,10 +216,14 @@ struct flash_bank *get_flash_bank_by_num(int num)
        return p;
 }
 
-COMMAND_HELPER(flash_command_get_bank_by_num,
-       unsigned name_index, struct flash_bank **bank)
+COMMAND_HELPER(flash_command_get_bank_by_num, unsigned name_index,
+               struct flash_bank **bank)
 {
        const char *name = CMD_ARGV[name_index];
+       *bank = get_flash_bank_by_name(name);
+       if (*bank)
+               return ERROR_OK;
+
        unsigned bank_num;
        COMMAND_PARSE_NUMBER(uint, name, bank_num);
 
index 23a7b81839c0707f0f4f1edeeefb8712d59d1400..fb88c35398a51321fc904051952798151d46cb44 100644 (file)
@@ -309,6 +309,14 @@ int default_flash_blank_check(struct flash_bank *bank);
  */
 int default_flash_mem_blank_check(struct flash_bank *bank);
 
+/**
+ * Returns the flash bank specified by @a name, which matches the
+ * driver name and a suffix (option) specify the driver-specific
+ * bank number. The suffix consists of the '.' and the driver-specific
+ * bank number: when two str9x banks are defined, then 'str9x.1' refers
+ * to the second.
+ */
+struct flash_bank *get_flash_bank_by_name(const char *name);
 /**
  * Returns a flash bank by the specified flash_bank_s bank_number, @a num.
  * @param num The flash bank number.
@@ -317,7 +325,9 @@ int default_flash_mem_blank_check(struct flash_bank *bank);
 struct flash_bank *get_flash_bank_by_num(int num);
 /**
  * Retreives @a bank from a command argument, reporting errors parsing
- * the bank identifier or retreiving the specified bank.
+ * the bank identifier or retreiving the specified bank.  The bank
+ * may be identified by its bank number or by @c name.instance, where
+ * @a instance is driver-specific.
  * @param name_index The index to the string in args containing the
  * bank identifier.
  * @param bank On output, contians a pointer to the bank or NULL.
index 53b6531d76573caabd64ee3473bdae5bbc13dcb0..d812805ab1ed885e2d96f3febb2e99d8ba8dd8f2 100644 (file)
@@ -25,6 +25,7 @@
 #endif
 
 #include "nand.h"
+#include "common.h"
 #include "time_support.h"
 #include "fileio.h"
 
@@ -288,6 +289,23 @@ int nand_register_commands(struct command_context *cmd_ctx)
        return ERROR_OK;
 }
 
+struct nand_device *get_nand_device_by_name(const char *name)
+{
+       unsigned requested = get_flash_name_index(name);
+       unsigned found = 0;
+
+       struct nand_device *nand;
+       for (nand = nand_devices; NULL != nand; nand = nand->next)
+       {
+               if (!flash_driver_name_matches(nand->controller->name, name))
+                       continue;
+               if (++found < requested)
+                       continue;
+               return nand;
+       }
+       return NULL;
+}
+
 struct nand_device *get_nand_device_by_num(int num)
 {
        struct nand_device *p;
@@ -308,11 +326,15 @@ COMMAND_HELPER(nand_command_get_device_by_num, 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;
index ddc4520e92ddc8da69253cdb39426b55027a0e48..a10877153169ef5689515cae2809f69928ae6d93 100644 (file)
@@ -212,6 +212,15 @@ enum oob_formats
 };
 
 
+/**
+ * Returns the flash bank specified by @a name, which matches the
+ * driver name and a suffix (option) specify the driver-specific
+ * bank number. The suffix consists of the '.' and the driver-specific
+ * bank number: when two davinci banks are defined, then 'davinci.1' refers
+ * to the second (e.g. DM355EVM).
+ */
+struct nand_device *get_nand_device_by_name(const char *name);
+
 struct nand_device *get_nand_device_by_num(int num);
 
 int nand_read_page_raw(struct nand_device *nand, uint32_t page,