cmd: Update the memory-search command

Add various fixes and improvements to this command that were missed in
the original version. Unfortunately I forgot to send v2.

- Fix Kconfig name
- Use a separate variable for the remaining search length
- Correct a minor bug
- Move into a separate test suite
- Add -q flag to the 'quiet' test to test operation when console is enabled
- Enable the feature for sandbox

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/cmd/Kconfig b/cmd/Kconfig
index e111764..6ac274e 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -724,7 +724,7 @@
 	    base - print or set address offset
 	    loop - initialize loop on address range
 
-config MEM_SEARCH
+config CMD_MEM_SEARCH
 	bool "ms - Memory search"
 	help
 	  Memory-search command
diff --git a/cmd/mem.c b/cmd/mem.c
index 575893c..190e2b9 100644
--- a/cmd/mem.c
+++ b/cmd/mem.c
@@ -53,7 +53,8 @@
 static ulong	mm_last_addr, mm_last_size;
 
 static	ulong	base_address = 0;
-#ifdef CONFIG_MEM_SEARCH
+#ifdef CONFIG_CMD_MEM_SEARCH
+static ulong dp_last_ms_length;
 static u8 search_buf[64];
 static uint search_len;
 #endif
@@ -367,7 +368,7 @@
 	return 0;
 }
 
-#ifdef CONFIG_MEM_SEARCH
+#ifdef CONFIG_CMD_MEM_SEARCH
 static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
 			 char *const argv[])
 {
@@ -377,6 +378,7 @@
 	ulong last_pos;		/* Offset of last match in 'size' units*/
 	ulong last_addr;	/* Address of last displayed line */
 	int limit = 10;
+	int used_len;
 	int count;
 	int size;
 	int i;
@@ -384,12 +386,12 @@
 	/* We use the last specified parameters, unless new ones are entered */
 	addr = dp_last_addr;
 	size = dp_last_size;
-	length = dp_last_length;
+	length = dp_last_ms_length;
 
 	if (argc < 3)
 		return CMD_RET_USAGE;
 
-	if ((!flag & CMD_FLAG_REPEAT)) {
+	if (!(flag & CMD_FLAG_REPEAT)) {
 		/*
 		 * Check for a size specification.
 		 * Defaults to long if no or incorrect specification.
@@ -398,7 +400,8 @@
 		if (size < 0 && size != -2 /* string */)
 			return 1;
 
-		argc--; argv++;
+		argc--;
+		argv++;
 		while (argc && *argv[0] == '-') {
 			int ch = argv[0][1];
 
@@ -408,7 +411,8 @@
 				limit = simple_strtoul(argv[0] + 2, NULL, 16);
 			else
 				return CMD_RET_USAGE;
-			argc--; argv++;
+			argc--;
+			argv++;
 		}
 
 		/* Address is specified since argc > 1 */
@@ -421,7 +425,7 @@
 		/* Read the bytes to search for */
 		end = search_buf + sizeof(search_buf);
 		for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
-			if (SUPPORT_64BIT_DATA && size == 8) {
+			if (MEM_SUPPORT_64BIT_DATA && size == 8) {
 				u64 val = simple_strtoull(argv[i], NULL, 16);
 
 				*(u64 *)ptr = val;
@@ -460,7 +464,8 @@
 	last_pos = 0;
 	last_addr = 0;
 	count = 0;
-	for (offset = 0; offset <= bytes - search_len && count < limit;
+	for (offset = 0;
+	     offset < bytes && offset <= bytes - search_len && count < limit;
 	     offset += size) {
 		void *ptr = buf + offset;
 
@@ -495,9 +500,10 @@
 
 	unmap_sysmem(buf);
 
-	dp_last_addr = addr + offset / size;
+	used_len = offset / size;
+	dp_last_addr = addr + used_len;
 	dp_last_size = size;
-	dp_last_length = length - offset / size;
+	dp_last_ms_length = length < used_len ? 0 : length - used_len;
 
 	return count ? 0 : CMD_RET_FAILURE;
 }
@@ -1337,13 +1343,13 @@
 	"[.b, .w, .l" HELP_Q "] addr1 addr2 count"
 );
 
-#ifdef CONFIG_MEM_SEARCH
+#ifdef CONFIG_CMD_MEM_SEARCH
 /**************************************************/
 U_BOOT_CMD(
 	ms,	255,	1,	do_mem_search,
 	"memory search",
 	"[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
-	"  -q = quiet, -l<val> = match limit" :
+	"  -q = quiet, -l<val> = match limit"
 );
 #endif