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.
This commit is contained in:
Zachary T Welch 2009-11-17 13:04:49 -08:00
parent 8f446fcf67
commit 870b8c0455
7 changed files with 154 additions and 4 deletions

View 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 \

46
src/flash/common.c Normal file
View File

@ -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';
}

39
src/flash/common.h Normal file
View File

@ -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

View 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);

View 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.

View 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;

View 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,