From 870b8c04557f0b7441cc502debaf537984d77e2a Mon Sep 17 00:00:00 2001 From: Zachary T Welch Date: Tue, 17 Nov 2009 13:04:49 -0800 Subject: [PATCH] allow flash/nand banks commands to accept names 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 | 2 ++ src/flash/common.c | 46 +++++++++++++++++++++++++++++++++++++++++++ src/flash/common.h | 39 ++++++++++++++++++++++++++++++++++++ src/flash/flash.c | 26 ++++++++++++++++++++++-- src/flash/flash.h | 12 ++++++++++- src/flash/nand.c | 24 +++++++++++++++++++++- src/flash/nand.h | 9 +++++++++ 7 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 src/flash/common.c create mode 100644 src/flash/common.h diff --git a/src/flash/Makefile.am b/src/flash/Makefile.am index b687182d..84032302 100644 --- a/src/flash/Makefile.am +++ b/src/flash/Makefile.am @@ -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 index 00000000..253ed9d8 --- /dev/null +++ b/src/flash/common.c @@ -0,0 +1,46 @@ +/*************************************************************************** + * Copyright (C) 2009 by Zachary T Welch * + * * + * 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 index 00000000..1fd0d774 --- /dev/null +++ b/src/flash/common.h @@ -0,0 +1,39 @@ +/*************************************************************************** + * Copyright (C) 2009 by Zachary T Welch * + * * + * 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 diff --git a/src/flash/flash.c b/src/flash/flash.c index 98e5ee0e..071503f1 100644 --- a/src/flash/flash.c +++ b/src/flash/flash.c @@ -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); diff --git a/src/flash/flash.h b/src/flash/flash.h index 23a7b818..fb88c353 100644 --- a/src/flash/flash.h +++ b/src/flash/flash.h @@ -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. diff --git a/src/flash/nand.c b/src/flash/nand.c index 53b6531d..d812805a 100644 --- a/src/flash/nand.c +++ b/src/flash/nand.c @@ -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; diff --git a/src/flash/nand.h b/src/flash/nand.h index ddc4520e..a1087715 100644 --- a/src/flash/nand.h +++ b/src/flash/nand.h @@ -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, -- 2.39.5