Skip to contentSkip to conversation

sandbox: fdt: Add support for CONFIG_OF_CONTROL

PATCH Superseded Simon Glass 2 participants 15 messages Jan 23, 2012 – Feb 21, 2012 GPIO

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/8 sandbox: fdt: Add support for CONFIG_OF_CONTROL

This adds support for a controlling fdt, mirroring the ARM implementation.

---
Changes in v3:
- Use #if defined()..#elif defined, instead of #ifdef..#elif defined

 arch/sandbox/include/asm/global_data.h |    1 +
 arch/sandbox/lib/board.c               |    8 ++++++++
 2 files changed, 9 insertions(+), 0 deletions(-)
arch/sandbox/include/asm/global_data.h +1 -0
index 8d47191..01a7063 100644
@@ -45,6 +45,7 @@ typedef	struct global_data {
 	unsigned long	fb_base;	/* base address of frame buffer */
 	u8		*ram_buf;	/* emulated RAM buffer */
 	phys_size_t	ram_size;	/* RAM size */
+	const void	*fdt_blob;	/* Our device tree, NULL if none */
 	void		**jt;		/* jump table */
 	char		env_buf[32];	/* buffer for getenv() before reloc. */
 } gd_t;
arch/sandbox/lib/board.c +8 -0
index b7997e9..6d464d6 100644
@@ -156,6 +156,14 @@ void board_init_f(ulong bootflag)
 
 	memset((void *)gd, 0, sizeof(gd_t));
 
+#if defined(CONFIG_OF_EMBED)
+	/* Get a pointer to the FDT */
+	gd->fdt_blob = _binary_dt_dtb_start;
+#elif defined(CONFIG_OF_SEPARATE)
+	/* FDT is at end of image */
+	gd->fdt_blob = (void *)(_end_ofs + _TEXT_BASE);
+#endif
+
 	for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
 		if ((*init_fnc_ptr)() != 0)
 			hang();
Signature · 1 line
1.7.7.3
#1 lore raw

PATCH v3 2/8 sandbox: config: Enable fdt and snprintf() options

Enable fdt code and safe snprintf() options for sandbox.

---

 include/configs/sandbox.h |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
include/configs/sandbox.h +6 -0
index 10565e6..6790216 100644
@@ -28,6 +28,12 @@
 /* Number of bits in a C 'long' on this architecture */
 #define CONFIG_SANDBOX_BITS_PER_LONG	64
 
+#define CONFIG_OF_CONTROL
+#define CONFIG_OF_LIBFDT
+#define CONFIG_LMB
+
+#define CONFIG_SYS_VSNPRINTF
+
 /*
  * Size of malloc() pool, although we don't actually use this yet.
  */
Signature · 1 line
1.7.7.3
#2 lore raw

PATCH v3 3/8 sandbox: gpio: Add basic driver for simulating GPIOs

In reply to Simon Glass

This provides a way of simulating GPIOs by setting values which are seen by the normal gpio_get/set_value() calls.

---
Changes in v2:
- Fix gpio_direction_output() to actually set the value
- Use generic GPIO command and interface

Changes in v3:
- Change GPIO numbers to unsigned as per new asm-generic/gpio.h
- Implement GPIO interface more fully (reservation, gpio state)
- Use __func__ when function names are required

 arch/sandbox/include/asm/gpio.h |   66 +++++++++++++
 drivers/gpio/Makefile           |    1 +
 drivers/gpio/sandbox.c          |  194 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 261 insertions(+), 0 deletions(-)
 create mode 100644 arch/sandbox/include/asm/gpio.h
 create mode 100644 drivers/gpio/sandbox.c
arch/sandbox/include/asm/gpio.h new file +66 -0
new file mode 100644
index 0000000..fddb551
@@ -0,0 +1,66 @@
+/*
+ * This is the interface to the sandbox GPIO driver for test code which
+ * wants to change the GPIO values reported to U-Boot.
+ *
+ * Copyright (c) 2011 The Chromium OS Authors.
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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 __ASM_SANDBOX_GPIO_H
+#define __ASM_SANDBOX_GPIO_H
+
+/* We use the generic interface, and add a back channel */
+#include <asm-generic/gpio.h>
+
+/**
+ * Return the value of a GPIO
+ *
+ * @param gp	GPIO number
+ * @return -1 on error, 0 if GPIO is low, >0 if high
+ */
+int sandbox_gpio_get_value(unsigned gp);
+
+/**
+ * @param gp	GPIO number
+ * @param value	value to set (0 for low, non-zero for high)
+ * @return -1 on error, 0 if ok
+ */
+int sandbox_gpio_set_value(unsigned gp, int value);
+
+/**
+ * Return the direction of a GPIO
+ *
+ * @param gp	GPIO number
+ * @return -1 on error, 0 if GPIO is input, >0 if output
+ */
+int sandbox_gpio_get_direction(unsigned gp);
+
+/**
+ * @param gp	GPIO number
+ * @param output 0 to set as input, 1 to set as output
+ * @return -1 on error, 0 if ok
+ */
+int sandbox_gpio_set_direction(unsigned gp, int output);
+
+/* Display information about each GPIO */
+void gpio_info(void);
+
+#define gpio_status()	gpio_info()
+
+#endif
drivers/gpio/Makefile +1 -0
index 4375a55..fb3b09a 100644
@@ -34,6 +34,7 @@ COBJS-$(CONFIG_MXS_GPIO)	+= mxs_gpio.o
 COBJS-$(CONFIG_PCA953X)		+= pca953x.o
 COBJS-$(CONFIG_PCA9698)		+= pca9698.o
 COBJS-$(CONFIG_S5P)		+= s5p_gpio.o
+COBJS-$(CONFIG_SANDBOX_GPIO)	+= sandbox.o
 COBJS-$(CONFIG_TEGRA2_GPIO)	+= tegra2_gpio.o
 COBJS-$(CONFIG_DA8XX_GPIO)	+= da8xx_gpio.o
 COBJS-$(CONFIG_ALTERA_PIO)	+= altera_pio.o
drivers/gpio/sandbox.c new file +194 -0

194 lines not on this page · raw message

Signature · 1 line
1.7.7.3
#3 lore raw

PATCH v3 4/8 sandbox: Enable GPIO driver

In reply to Simon Glass

Enable the new GPIO driver for sandbox.

---
Changes in v3:
- Reduce number of GPIOs from 224 to 20

 include/configs/sandbox.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/configs/sandbox.h +4 -0
index 6790216..a58a34e 100644
@@ -34,6 +34,10 @@
 
 #define CONFIG_SYS_VSNPRINTF
 
+#define CONFIG_CMD_GPIO
+#define CONFIG_SANDBOX_GPIO
+#define CONFIG_SANDBOX_GPIO_COUNT	20
+
 /*
  * Size of malloc() pool, although we don't actually use this yet.
  */
Signature · 1 line
1.7.7.3
#4 lore raw

PATCH v3 5/8 sandbox: Add concept of sandbox state

In reply to Simon Glass

The state exists through the life of U-Boot. It can be adjusted by command line options and perhaps later through a config file. It is available to U-Boot through state_...() calls (within sandbox code).

---

 arch/sandbox/cpu/Makefile                     |    2 +-
 arch/sandbox/cpu/start.c                      |   10 ++++
 arch/sandbox/cpu/state.c                      |   57 ++++++++++++++++++++++++
 arch/sandbox/include/asm/arch-sandbox/state.h |   59 +++++++++++++++++++++++++
 4 files changed, 127 insertions(+), 1 deletions(-)
 create mode 100644 arch/sandbox/cpu/state.c
 create mode 100644 arch/sandbox/include/asm/arch-sandbox/state.h
arch/sandbox/cpu/Makefile +1 -1
index 2ae0f71..6fd09ff 100644
@@ -27,7 +27,7 @@ include $(TOPDIR)/config.mk
 
 LIB	= $(obj)lib$(CPU).o
 
-COBJS	:= cpu.o start.o os.o
+COBJS	:= cpu.o os.o start.o state.o
 
 SRCS	:= $(COBJS:.o=.c)
 OBJS	:= $(addprefix $(obj),$(COBJS))
arch/sandbox/cpu/start.c +10 -0
index a429e29..b3442e8 100644
@@ -20,9 +20,19 @@
  */
 
 #include <common.h>
+#include <asm/arch/state.h>
 
 int main(int argc, char *argv[])
 {
+	struct sandbox_state *state = NULL;
+	int err;
+
+	err = state_init();
+	if (!err) {
+		state = state_get_current();
+		assert(state);
+	}
+
 	/*
 	 * Do pre- and post-relocation init, then start up U-Boot. This will
 	 * never return.
arch/sandbox/cpu/state.c new file +57 -0

57 lines not on this page · raw message

arch/sandbox/include/asm/arch-sandbox/state.h new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
1.7.7.3
#5 lore raw

PATCH v3 5/8 sandbox: Add concept of sandbox state

arch/sandbox/cpu/start.c line 34

Quoted 13 lines · On Monday 23 January 2012 01:48:50 Simon Glass wrote:
arch/sandbox/cpu/start.c +8 -0
 
 int main(int argc, char *argv[])
 {
+	struct sandbox_state *state = NULL;
+	int err;
+
+	err = state_init();
+	if (!err) {
+		state = state_get_current();
+		assert(state);
+	}

i guess a lot of this checking/plumbing only makes sense in the context of future work where the state represents a lot more stuff

arch/sandbox/cpu/state.c line 28

Quoted 5 lines
arch/sandbox/cpu/state.c new file +2 -0
 
+struct sandbox_state main_state;
+struct sandbox_state *state;	/* Pointer to current state record */

static

arch/sandbox/cpu/state.c line 33

Quoted 4 lines
arch/sandbox/cpu/state.c new file +4 -0
+int state_is_processor_reset(void)
+{
+	return 1;
+}

if the cpu is in reset, then u-boot is dead ...

arch/sandbox/cpu/state.c line 44

Quoted 5 lines
arch/sandbox/cpu/state.c new file +5 -0
+struct sandbox_state *state_get_current(void)
+{
+	assert(state);
+	return state;
+}

you assert() here and in main() ... i think we can get rid of the one in the main() func

arch/sandbox/include/asm/arch-sandbox/state.h

Quoted 2 lines
arch/sandbox/include/asm/arch-sandbox/state.h new file +0 -0

needs ifdef protection against multiple inclusion

arch/sandbox/include/asm/arch-sandbox/state.h line 33

Quoted 5 lines
arch/sandbox/include/asm/arch-sandbox/state.h new file +5 -0
+/* The complete state of the test system */
+struct sandbox_state {
+	const char *cmd;		/* Command to execute */
+	enum exit_type_id exit_type;	/* How we exited U-Boot */
+};

the argc/argv given to main() should be in here
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120124/5a846502/attachment.pgp>

#6 lore raw

PATCH v3 5/8 sandbox: Add concept of sandbox state

Hi Mike,

Quoted 4 lines · Mike Frysinger
Quoted 3 lines · On Monday 23 January 2012 01:48:50 Simon Glass wrote:
arch/sandbox/cpu/start.c +0 -0
Quoted 13 lines
Quoted 10 lines

?int main(int argc, char *argv[])
?{
+ ? ? struct sandbox_state *state = NULL;
+ ? ? int err;
+
+ ? ? err = state_init();
+ ? ? if (!err) {
+ ? ? ? ? ? ? state = state_get_current();
+ ? ? ? ? ? ? assert(state);
+ ? ? }

i guess a lot of this checking/plumbing only makes sense in the context of future work where the state represents a lot more stuff

Well we use it for a few things so we need something here, but yes.

Quoted 8 lines

arch/sandbox/cpu/state.c line 28

Quoted 6 lines
arch/sandbox/cpu/state.c new file +2 -0
 
+struct sandbox_state main_state;
+struct sandbox_state *state; /* Pointer to current state record */

static

Done

Quoted 7 lines

arch/sandbox/cpu/state.c line 33

Quoted 5 lines
arch/sandbox/cpu/state.c new file +4 -0
+int state_is_processor_reset(void)
+{
+ ? ? return 1;
+}

if the cpu is in reset, then u-boot is dead ...

That code should not be there, sorry, have removed it.

Quoted 10 lines

arch/sandbox/cpu/state.c line 44

Quoted 6 lines
arch/sandbox/cpu/state.c new file +5 -0
+struct sandbox_state *state_get_current(void)
+{
+ ? ? assert(state);
+ ? ? return state;
+}

you assert() here and in main() ... i think we can get rid of the one in the main() func

Quoted 4 lines

arch/sandbox/include/asm/arch-sandbox/state.h

Quoted 2 lines
arch/sandbox/include/asm/arch-sandbox/state.h new file +0 -0

needs ifdef protection against multiple inclusion

Done

Quoted 9 lines

arch/sandbox/include/asm/arch-sandbox/state.h line 33

Quoted 6 lines
arch/sandbox/include/asm/arch-sandbox/state.h new file +5 -0
+/* The complete state of the test system */
+struct sandbox_state {
+ ? ? const char *cmd; ? ? ? ? ? ? ? ?/* Command to execute */
+ ? ? enum exit_type_id exit_type; ? ?/* How we exited U-Boot */
+};

the argc/argv given to main() should be in here
-mike

OK I will add this in the last patch which deals with arguments.

Regards,
Simon

#7 lore raw

PATCH v3 6/8 sandbox: Allow processing instead of or before main loop

In reply to Simon Glass

In order to pass command line arguments to sandbox we need to be able to act on them. So take control back at the end of board_init_r() from where we can call the main loop or do something else.

---
Changes in v2:
- Call cpu_main_loop() from board_init_r()

 arch/sandbox/cpu/start.c                  |    8 ++++++++
 arch/sandbox/include/asm/u-boot-sandbox.h |    3 +++
 arch/sandbox/lib/board.c                  |    7 ++++---
 3 files changed, 15 insertions(+), 3 deletions(-)
arch/sandbox/cpu/start.c +8 -0
index b3442e8..d7402be 100644
@@ -22,6 +22,14 @@
 #include <common.h>
 #include <asm/arch/state.h>
 
+#include <os.h>
+
+void start_main_loop(void)
+{
+	for (;;)
+		main_loop();
+}
+
 int main(int argc, char *argv[])
 {
 	struct sandbox_state *state = NULL;
arch/sandbox/include/asm/u-boot-sandbox.h +3 -0
index 236b4ee..3743289 100644
@@ -35,4 +35,7 @@
 int board_init(void);
 int dram_init(void);
 
+/* start.c */
+void start_main_loop(void);
+
 #endif	/* _U_BOOT_SANDBOX_H_ */
arch/sandbox/lib/board.c +4 -3
index 6d464d6..331ce16 100644
@@ -270,11 +270,12 @@ void board_init_r(gd_t *id, ulong dest_addr)
 #endif
 
 	/*
-	 * For now, run the main loop. Later we might let this be done
-	 * in the main program.
+	 * This function can't return (to match other archs) so call back
+	 * into start.c so that sandbox can do something other than the main
+	 * loop if it likes
 	 */
 	while (1)
-		main_loop();
+		start_main_loop();
 
 	/* NOTREACHED - no way out of command loop except booting */
 }
Signature · 1 line
1.7.7.3
#8 lore raw

PATCH v3 7/8 sandbox: Add flags for open() call

In reply to Simon Glass

This provides a way for callers to create files for writing. We define flags which mirror the POSIX values.

Another approach would be to translate the flags at runtime. Perhaps we can leave to whoever wants to port this to another OS?

---
Changes in v3:
- Change open() flags enum to #define

 arch/sandbox/cpu/os.c |    2 +-
 include/os.h          |   16 +++++++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)
arch/sandbox/cpu/os.c +1 -1
index 093e7dc..9f93f83 100644
@@ -47,7 +47,7 @@ ssize_t os_write(int fd, const void *buf, size_t count)
 
 int os_open(const char *pathname, int flags)
 {
-	return open(pathname, flags);
+	return open(pathname, flags, 0777);
 }
 
 int os_close(int fd)
include/os.h +11 -5
index f3af4f0..a3c1e84 100644
@@ -1,4 +1,9 @@
 /*
+ * Operating System Interface
+ *
+ * This provides access to useful OS routines for the sandbox architecture.
+ * They are kept in a separate file so we can include system headers.
+ *
  * Copyright (c) 2011 The Chromium OS Authors.
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -19,11 +24,7 @@
  * MA 02111-1307 USA
  */
 
-/*
- * Operating System Interface
- *
- * This provides access to useful OS routines from the sandbox architecture
- */
+struct sandbox_state;
 
 /**
  * Access to the OS read() system call
@@ -45,6 +46,11 @@ ssize_t os_read(int fd, void *buf, size_t count);
  */
 ssize_t os_write(int fd, const void *buf, size_t count);
 
+#define OS_O_RDONLY	0
+#define OS_O_WRONLY	1
+#define	OS_O_RDWR	2
+#define OS_O_CREAT	0100
+
 /**
  * Access to the OS open() system call
  *
Signature · 1 line
1.7.7.3
#9 lore raw

PATCH v3 7/8 sandbox: Add flags for open() call

Quoted 2 lines · On Monday 23 January 2012 01:48:52 Simon Glass wrote:

This provides a way for callers to create files for writing. We define flags which mirror the POSIX values.

there are no POSIX values, just names. the way the defines get interpreted is left up to implementations.

Quoted 10 lines
include/os.h +0 -4
@@ -19,11 +24,7 @@
  * MA 02111-1307 USA
  */
 
-/*
- * Operating System Interface
- *
- * This provides access to useful OS routines from the sandbox

architecture - */
+struct sandbox_state;

looks like unrelated stuff got squished into here

include/os.h ssize_t os_read(int fd, void *buf, size_t count); · line 52

Quoted 4 lines
include/os.h +4 -0
+#define OS_O_RDONLY	0
+#define OS_O_WRONLY	1
+#define	OS_O_RDWR	2
+#define OS_O_CREAT	0100

seems to be whitespace mismatch between "#define" and the actual define. some use tabs and some spaces ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120124/854f0820/attachment.pgp>

#10 lore raw

PATCH v3 7/8 sandbox: Add flags for open() call

Hi Mike,

Quoted 11 lines · Mike Frysinger
Quoted 2 lines · On Monday 23 January 2012 01:48:52 Simon Glass wrote:

This provides a way for callers to create files for writing. We define flags which mirror the POSIX values.

there are no POSIX values, just names. ?the way the defines get interpreted is left up to implementations.

Quoted 4 lines
(quoted diff) +0 -0
@@ -19,11 +24,7 @@

? * MA 02111-1307 USA
? */

Quoted 8 lines
Quoted 6 lines

-/*
- * Operating System Interface
- *
- * This provides access to useful OS routines from the sandbox architecture - */
+struct sandbox_state;

looks like unrelated stuff got squished into here

We need to declare the struct. The comment change is because you said we should put the purpose of the file at the top, and I thought it didn't really warrant a new commit?

Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+#define OS_O_RDONLY ?0
+#define OS_O_WRONLY ?1
+#define ? ? ?OS_O_RDWR ? ? ? 2
+#define OS_O_CREAT ? 0100

seems to be whitespace mismatch between "#define" and the actual define. ?some use tabs and some spaces ?
-mike

Fixed.

Regards,
Simon

#11 lore raw

PATCH v3 7/8 sandbox: Add flags for open() call

Quoted 11 lines · On Wednesday 15 February 2012 17:58:35 Simon Glass wrote:
Quoted 9 lines · Mike Frysinger
Quoted 6 lines · On Monday 23 January 2012 01:48:52 Simon Glass wrote:

-/*
- * Operating System Interface
- *
- * This provides access to useful OS routines from the sandbox architecture - */
+struct sandbox_state;

looks like unrelated stuff got squished into here

Quoted 3 lines

We need to declare the struct. The comment change is because you said we should put the purpose of the file at the top, and I thought it didn't really warrant a new commit?

i meant that adding the struct forward decl in this commit doesn't make sense. it should be part of one of the sandbox state patches.

deleting the comment is fine, but the changelog should mention that this was on purpose and not just an accident.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120215/f57f1619/attachment.pgp>

#12 lore raw

PATCH v5 7/9 sandbox: Add flags for open() call

In reply to Simon Glass

This provides a way for callers to create files for writing. The flags are translated at runtime, for the ones we support.

---
Changes in v3:
- Change open() flags enum to #define

Changes in v4:
- Change space to tab in os.h
- Remove unneeded declaration of struct sandbox_state in os.h

Changes in v5:
- Add belts-and-braces decode of OS_O_... flags

 arch/sandbox/cpu/os.c |   24 ++++++++++++++++++++++--
 include/os.h          |   17 +++++++++++------
 2 files changed, 33 insertions(+), 8 deletions(-)
arch/sandbox/cpu/os.c +22 -2
index 093e7dc..a5c1c0b 100644
@@ -45,9 +45,29 @@ ssize_t os_write(int fd, const void *buf, size_t count)
 	return write(fd, buf, count);
 }
 
-int os_open(const char *pathname, int flags)
+int os_open(const char *pathname, int os_flags)
 {
-	return open(pathname, flags);
+	int flags;
+
+	switch (os_flags & OS_O_MASK) {
+	case OS_O_RDONLY:
+	default:
+		flags = O_RDONLY;
+		break;
+
+	case OS_O_WRONLY:
+		flags = O_WRONLY;
+		break;
+
+	case OS_O_RDWR:
+		flags = O_RDWR;
+		break;
+	}
+
+	if (os_flags & OS_O_CREAT)
+		flags |= O_CREAT;
+
+	return open(pathname, flags, 0777);
 }
 
 int os_close(int fd)
include/os.h +11 -6
index f3af4f0..16fb5de 100644
@@ -1,4 +1,9 @@
 /*
+ * Operating System Interface
+ *
+ * This provides access to useful OS routines for the sandbox architecture.
+ * They are kept in a separate file so we can include system headers.
+ *
  * Copyright (c) 2011 The Chromium OS Authors.
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -19,12 +24,6 @@
  * MA 02111-1307 USA
  */
 
-/*
- * Operating System Interface
- *
- * This provides access to useful OS routines from the sandbox architecture
- */
-
 /**
  * Access to the OS read() system call
  *
@@ -45,6 +44,12 @@ ssize_t os_read(int fd, void *buf, size_t count);
  */
 ssize_t os_write(int fd, const void *buf, size_t count);
 
+#define OS_O_RDONLY	0
+#define OS_O_WRONLY	1
+#define OS_O_RDWR	2
+#define OS_O_MASK	3	/* Mask for read/write flags */
+#define OS_O_CREAT	0100
+
 /**
  * Access to the OS open() system call
  *
Signature · 1 line
1.7.7.3
#13 lore raw

PATCH v5 7/9 sandbox: Add flags for open() call

Quoted 2 lines · On Monday 20 February 2012 23:41:36 Simon Glass wrote:

This provides a way for callers to create files for writing. The flags are translated at runtime, for the ones we support.

thanks, merged into my sandbox tree
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120220/5035459b/attachment.pgp>

#14 lore raw

PATCH v3 8/8 sandbox: Add basic command line parsing

In reply to Simon Glass

This adds simple command-line parsing to sandbox. The idea is that it sets up the state with options provided, and this state can then be queried later, as needed.

For now we just allow it to run a command.

Passing a command to U-Boot on stdin is not as convenient IMO.

The parsing code is in os.c since it gives us easy access to getopt() and friends. We could create a separate parse.c file if this grows.

---
Changes in v2:
- Clean up, added a few fdt and config patches
- Rebase on master

Changes in v3:
- Only output usage on stderr if we get an error
- Tidy code style around getopt()

 arch/sandbox/cpu/os.c                         |   43 +++++++++++++++++++++++++
 arch/sandbox/cpu/start.c                      |   23 ++++++++++++-
 arch/sandbox/include/asm/arch-sandbox/state.h |    1 +
 include/os.h                                  |   20 +++++++++++
 4 files changed, 85 insertions(+), 2 deletions(-)
arch/sandbox/cpu/os.c +43 -0
index 9f93f83..4b54fa6 100644
@@ -21,6 +21,8 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <termios.h>
 #include <time.h>
@@ -30,6 +32,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <linux/types.h>
+#include <asm/arch/state.h>
 
 #include <os.h>
 
@@ -122,3 +125,43 @@ u64 os_get_nsec(void)
 	return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
 #endif
 }
+
+static const char options[] = "c:h";
+
+static const struct option long_options[] = {
+	{"command", required_argument, NULL, 'c'},
+	{"help", no_argument, NULL, 'h'},
+	{NULL, 0, NULL, 0}
+};
+
+void os_usage(int err)
+{
+	if (err < 0)
+		fprintf(stderr, "Try `--help' for more information.\n");
+	fprintf(err < 0 ? stderr : stdout, "u-boot, "
+		"a command line test interface to U-Boot\n\n"
+		"usage:\tu-boot [-ch]\n"
+		"Options:\n"
+		"\t-h\tDisplay help\n"
+		"\t-c <command>\tExecute U-Boot command\n");
+	exit(1);
+}
+
+int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
+{
+	int c;
+
+	while ((c = getopt_long(argc, argv, options, long_options,
+				NULL)) != EOF) {
+		switch (c) {
+		case 'c':
+			state->cmd = optarg;
+			break;
+		case 'h':
+			return 1;
+		default: /* '?' */
+			return -1;
+		}
+	}
+	return 0;
+}
arch/sandbox/cpu/start.c +21 -2
index d7402be..fa7a907 100644
@@ -26,8 +26,26 @@
 
 void start_main_loop(void)
 {
-	for (;;)
-		main_loop();
+	struct sandbox_state *state = state_get_current();
+
+	if (state->err)
+		os_usage(state->err);	/* does not return */
+
+	/* Execute command if required */
+	if (state->cmd) {
+		/* TODO: redo this when cmd tidy-up series lands */
+#ifdef CONFIG_SYS_HUSH_PARSER
+		run_command(state->cmd, 0);
+#else
+		parse_string_outer(state->cmd, FLAG_PARSE_SEMICOLON |
+				    FLAG_EXIT_FROM_LOOP);
+#endif
+	} else {
+		for (;;)
+			main_loop();
+	}
+	printf("U-Boot exited with state %d\n", state->exit_type);
+	os_exit(state->exit_type);
 }
 
 int main(int argc, char *argv[])
@@ -39,6 +57,7 @@ int main(int argc, char *argv[])
 	if (!err) {
 		state = state_get_current();
 		assert(state);
+		state->err = os_parse_args(state, argc, argv);
 	}
 
 	/*
arch/sandbox/include/asm/arch-sandbox/state.h +1 -0
index 3023368..1e035fa 100644
@@ -30,6 +30,7 @@ enum exit_type_id {
 struct sandbox_state {
 	const char *cmd;		/* Command to execute */
 	enum exit_type_id exit_type;	/* How we exited U-Boot */
+	int err;			/* Error to report from parsing */
 };
 
 /**
include/os.h +20 -0
index a3c1e84..f7e8c31 100644
@@ -104,3 +104,23 @@ void os_usleep(unsigned long usec);
  * \return A monotonic increasing time scaled in nano seconds
  */
 u64 os_get_nsec(void);
+
+/**
+ * Parse arguments and update sandbox state.
+ *
+ * @param state		Sandbox state to update
+ * @param argc		Argument count
+ * @param argv		Argument vector
+ * @return 0 if ok, and program should continue;
+ *	1 if ok, but program should stop;
+ *	-1 on error: program should terminate.
+ */
+int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
+
+/**
+ * Display a usage message on stderr and exit
+ *
+ * @param err		Error code (-ve means the user entered an invalid
+ *			option)
+ */
+void os_usage(int err);
Signature · 1 line
1.7.7.3