Improve nand.c command argument parsing.

__archive__
Zachary T Welch 2009-10-22 22:33:12 -07:00
parent e9566a4a6a
commit 93ab9ce888
1 changed files with 53 additions and 72 deletions

View File

@ -1107,12 +1107,16 @@ int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char
static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{ {
nand_device_t *p;
int i = 0; int i = 0;
int j = 0; int j = 0;
int first = -1; int first = -1;
int last = -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 (argc) {
default: default:
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
@ -1121,15 +1125,16 @@ static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd
last = INT32_MAX; last = INT32_MAX;
break; break;
case 2: case 2:
first = last = strtoul(args[1], NULL, 0); COMMAND_PARSE_NUMBER(int, args[1], i);
first = last = i;
i = 0;
break; break;
case 3: case 3:
first = strtoul(args[1], NULL, 0); COMMAND_PARSE_NUMBER(int, args[1], first);
last = strtoul(args[2], NULL, 0); COMMAND_PARSE_NUMBER(int, args[2], last);
break; break;
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
if (p) if (p)
{ {
if (p->device) if (p->device)
@ -1181,15 +1186,16 @@ static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd
static int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) static int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{ {
nand_device_t *p;
int retval;
if (argc != 1) if (argc != 1)
{ {
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0)); nand_device_t *p;
int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
if (ERROR_OK != retval)
return retval;
if (p) if (p)
{ {
if ((retval = nand_probe(p)) == ERROR_OK) if ((retval = nand_probe(p)) == ERROR_OK)
@ -1205,29 +1211,25 @@ static int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cm
command_print(cmd_ctx, "unknown error when probing NAND flash device"); command_print(cmd_ctx, "unknown error when probing NAND flash device");
} }
} }
else
{
command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
}
return ERROR_OK; return ERROR_OK;
} }
static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{ {
nand_device_t *p;
int retval;
if (argc != 1 && argc != 3) if (argc != 1 && argc != 3)
{ {
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0)); nand_device_t *p;
int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
if (ERROR_OK != retval)
return retval;
if (p) if (p)
{ {
char *cp;
unsigned long offset; unsigned long offset;
unsigned long length; unsigned long length;
@ -1235,16 +1237,12 @@ static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cm
if (argc == 3) { if (argc == 3) {
unsigned long size = p->erase_size * p->num_blocks; unsigned long size = p->erase_size * p->num_blocks;
offset = strtoul(args[1], &cp, 0); COMMAND_PARSE_NUMBER(ulong, args[1], offset);
if (*cp || (offset == ULONG_MAX) if ((offset % p->erase_size) != 0 || offset >= size)
|| (offset % p->erase_size) != 0
|| offset >= size)
return ERROR_INVALID_ARGUMENTS; return ERROR_INVALID_ARGUMENTS;
length = strtoul(args[2], &cp, 0); COMMAND_PARSE_NUMBER(ulong, args[2], length);
if (*cp || (length == ULONG_MAX) if ((length == 0) || (length % p->erase_size) != 0
|| (length == 0)
|| (length % p->erase_size) != 0
|| (length + offset) > size) || (length + offset) > size)
return ERROR_INVALID_ARGUMENTS; return ERROR_INVALID_ARGUMENTS;
@ -1272,18 +1270,12 @@ static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cm
command_print(cmd_ctx, "unknown error when erasing NAND flash device"); command_print(cmd_ctx, "unknown error when erasing NAND flash device");
} }
} }
else
{
command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
}
return ERROR_OK; return ERROR_OK;
} }
int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{ {
nand_device_t *p;
int retval;
int first = -1; int first = -1;
int last = -1; int last = -1;
@ -1293,31 +1285,25 @@ int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0)); nand_device_t *p;
if (!p) { int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", if (ERROR_OK != retval)
args[0]); return retval;
return ERROR_INVALID_ARGUMENTS;
}
if (argc == 3) if (argc == 3)
{ {
char *cp;
unsigned long offset; unsigned long offset;
unsigned long length; unsigned long length;
offset = strtoul(args[1], &cp, 0); COMMAND_PARSE_NUMBER(ulong, args[1], offset);
if (*cp || offset == ULONG_MAX || offset % p->erase_size) if (offset % p->erase_size)
{
return ERROR_INVALID_ARGUMENTS; return ERROR_INVALID_ARGUMENTS;
}
offset /= p->erase_size; offset /= p->erase_size;
length = strtoul(args[2], &cp, 0); COMMAND_PARSE_NUMBER(ulong, args[2], length);
if (*cp || length == ULONG_MAX || length % p->erase_size) if (length % p->erase_size)
{
return ERROR_INVALID_ARGUMENTS; return ERROR_INVALID_ARGUMENTS;
}
length -= 1; length -= 1;
length /= p->erase_size; length /= p->erase_size;
@ -1357,15 +1343,16 @@ static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cm
duration_t duration; duration_t duration;
char *duration_text; char *duration_text;
nand_device_t *p;
if (argc < 3) if (argc < 3)
{ {
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0)); nand_device_t *p;
int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
if (ERROR_OK != retval)
return retval;
if (p) if (p)
{ {
uint8_t *page = NULL; uint8_t *page = NULL;
@ -1374,7 +1361,7 @@ static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cm
uint32_t oob_size = 0; uint32_t oob_size = 0;
const int *eccpos = NULL; const int *eccpos = NULL;
offset = strtoul(args[2], NULL, 0); COMMAND_PARSE_NUMBER(u32, args[2], offset);
if (argc > 3) if (argc > 3)
{ {
@ -1509,24 +1496,22 @@ static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cm
free(duration_text); free(duration_text);
duration_text = NULL; duration_text = NULL;
} }
else
{
command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
}
return ERROR_OK; return ERROR_OK;
} }
static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{ {
nand_device_t *p;
if (argc < 4) if (argc < 4)
{ {
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0)); nand_device_t *p;
int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
if (ERROR_OK != retval)
return retval;
if (p) if (p)
{ {
if (p->device) if (p->device)
@ -1540,8 +1525,10 @@ static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd
uint32_t page_size = 0; uint32_t page_size = 0;
uint8_t *oob = NULL; uint8_t *oob = NULL;
uint32_t oob_size = 0; uint32_t oob_size = 0;
uint32_t address = strtoul(args[2], NULL, 0); uint32_t address;
uint32_t size = strtoul(args[3], NULL, 0); COMMAND_PARSE_NUMBER(u32, args[2], address);
uint32_t size;
COMMAND_PARSE_NUMBER(u32, args[3], size);
uint32_t bytes_done = 0; uint32_t bytes_done = 0;
enum oob_formats oob_format = NAND_OOB_NONE; enum oob_formats oob_format = NAND_OOB_NONE;
@ -1631,24 +1618,22 @@ static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd
command_print(cmd_ctx, "#%s: not probed", args[0]); command_print(cmd_ctx, "#%s: not probed", args[0]);
} }
} }
else
{
command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
}
return ERROR_OK; return ERROR_OK;
} }
static int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) static int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{ {
nand_device_t *p;
if ((argc < 1) || (argc > 2)) if ((argc < 1) || (argc > 2))
{ {
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
} }
p = get_nand_device_by_num(strtoul(args[0], NULL, 0)); nand_device_t *p;
int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
if (ERROR_OK != retval)
return retval;
if (p) if (p)
{ {
if (p->device) if (p->device)
@ -1676,10 +1661,6 @@ static int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, cha
command_print(cmd_ctx, "#%s: not probed", args[0]); command_print(cmd_ctx, "#%s: not probed", args[0]);
} }
} }
else
{
command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
}
return ERROR_OK; return ERROR_OK;
} }