[U-Boot] [PATCH 2/5 v2] sandbox: add getopt support

Mike Frysinger vapier at gentoo.org
Tue Jan 31 20:53:31 CET 2012


Note: just a PoC that the current SPI flash code is based on.
Not meant to reject the getopt code Simon posted.

Signed-off-by: Mike Frysinger <vapier at gentoo.org>
---
 arch/sandbox/cpu/os.c    |   13 +++++++++++++
 arch/sandbox/cpu/start.c |   24 ++++++++++++++++++++++++
 include/os.h             |    2 ++
 3 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 13331d2..f7ba1fa 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#include <string.h>
 #include <termios.h>
 #include <time.h>
 #include <unistd.h>
@@ -135,3 +136,15 @@ u64 os_get_nsec(void)
 	return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
 #endif
 }
+
+extern char **sb_argv;
+const char *os_getopt(const char *name, int has_arg)
+{
+	size_t i;
+
+	for (i = 0; sb_argv[i]; ++i)
+		if (!strcmp(sb_argv[i], name))
+			return sb_argv[i + !!has_arg];
+
+	return NULL;
+}
diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index a429e29..3508a35 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -20,9 +20,33 @@
  */
 
 #include <common.h>
+#include <os.h>
+
+char **sb_argv;
+
+static const char usage[] =
+	"Usage: u-boot [options]\n"
+	"\n"
+	"Options: (note: not all options may be available)\n"
+	" -h, --help               this message (imagine that)\n"
+#ifdef CONFIG_SANDBOX_SPI
+	" --spi-<bus>-<cs> <spec>  connect client to spi <bus> on <cs>\n"
+# ifdef CONFIG_SPI_FLASH
+	"   spec: sf:<file>        treat <file> as spi flash\n"
+# endif
+#endif
+;
 
 int main(int argc, char *argv[])
 {
+	/* Save the argv for people to access */
+	sb_argv = argv;
+
+	if (os_getopt("-h", 0) || os_getopt("--help", 0)) {
+		serial_puts(usage);
+		return 0;
+	}
+
 	/*
 	 * Do pre- and post-relocation init, then start up U-Boot. This will
 	 * never return.
diff --git a/include/os.h b/include/os.h
index 11017b7..565b936 100644
--- a/include/os.h
+++ b/include/os.h
@@ -117,4 +117,6 @@ void os_usleep(unsigned long usec);
  */
 u64 os_get_nsec(void);
 
+const char *os_getopt(const char *name, int has_arg);
+
 #endif
-- 
1.7.8.4



More information about the U-Boot mailing list