[U-Boot] [RFC PATCH 17/44] sandbox: Add CONFIG_OF_HOSTFILE to read FDT from host file

Simon Glass
2013-01-05T01:51:46Z

With sandbox it is tricky to add an FDT to the image at build time (or
later) since we build an ELF file, not a plain binary, and the address
space of the whole U-Boot is not accessible in the emulated memory map
of sandbox.

Sandbox can read files directly from the host, though, so add an option
to read an FDT from a host file on start-up.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
arch/sandbox/cpu/start.c | 7 ++++++
arch/sandbox/include/asm/state.h | 1 +
arch/sandbox/lib/board.c | 42 ++++++++++++++++++++++++++++++++-----
doc/README.fdt-control | 6 ++++-
include/configs/sandbox.h | 2 +
5 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index 7603bf9..8589da2 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -104,6 +104,13 @@ static int sb_cmdline_cb_command(struct sandbox_state *state, const char *arg)
}
SB_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");

+static int sb_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
+{
+ state->fdt_fname = arg;
+ return 0;
+}
+SB_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
+
int main(int argc, char *argv[])
{
struct sandbox_state *state;
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 2b62b46..9552708 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -34,6 +34,7 @@ enum exit_type_id {
/* The complete state of the test system */
struct sandbox_state {
const char *cmd; /* Command to execute */
+ const char *fdt_fname; /* Filename of FDT binary */
enum exit_type_id exit_type; /* How we exited U-Boot */
const char *parse_err; /* Error to report from parsing */
int argc; /* Program arguments */
diff --git a/arch/sandbox/lib/board.c b/arch/sandbox/lib/board.c
index 83858c1..0e4df4c 100644
--- a/arch/sandbox/lib/board.c
+++ b/arch/sandbox/lib/board.c
@@ -39,13 +39,15 @@

#include <common.h>
#include <command.h>
+#include <fs.h>
+#include <fdtdec.h>
#include <malloc.h>
+#include <os.h>
#include <stdio_dev.h>
#include <timestamp.h>
#include <version.h>
#include <serial.h>
-
-#include <os.h>
+#include <asm/state.h>

DECLARE_GLOBAL_DATA_PTR;

@@ -89,6 +91,21 @@ static int display_dram_config(void)
return 0;
}

+static int read_fdt_from_file(void)
+{
+ struct sandbox_state *state = state_get_current();
+ int size;
+
+ if (fs_set_blk_dev("host", NULL, FS_TYPE_SANDBOX))
+ return 1;
+ size = fs_read(state->fdt_fname, CONFIG_SYS_FDT_LOAD_ADDR, 0, 0);
+ if (size < 0)
+ return 1;
+ gd->fdt_blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, size);
+
+ return 0;
+}
+
/*
* Breathe some life into the board...
*
@@ -127,6 +144,9 @@ init_fnc_t *init_sequence[] = {
#if defined(CONFIG_ARCH_CPU_INIT)
arch_cpu_init, /* basic arch cpu dependent setup */
#endif
+#ifdef CONFIG_OF_CONTROL
+ fdtdec_check_fdt,
+#endif
#if defined(CONFIG_BOARD_EARLY_INIT_F)
board_early_init_f,
#endif
@@ -156,6 +176,10 @@ void board_init_f(ulong bootflag)
assert(gd);

memset((void *)gd, 0, sizeof(gd_t));
+ mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
+
+ assert(mem);
+ gd->ram_buf = mem;

#if defined(CONFIG_OF_EMBED)
/* Get a pointer to the FDT */
@@ -163,6 +187,8 @@ void board_init_f(ulong bootflag)
#elif defined(CONFIG_OF_SEPARATE)
/* FDT is at end of image */
gd->fdt_blob = (void *)(_end_ofs + _TEXT_BASE);
+#elif defined(CONFIG_OF_HOSTFILE)
+ read_fdt_from_file();
#endif

for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
@@ -170,11 +196,15 @@ void board_init_f(ulong bootflag)
hang();
}

- size = CONFIG_SYS_SDRAM_SIZE;
- mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
+#ifdef CONFIG_OF_CONTROL
+ /* For now, put this check after the console is ready */
+ if (fdtdec_prepare_fdt()) {
+ panic("** CONFIG_OF_CONTROL defined but no FDT - please see "
+ "doc/README.fdt-control");
+ }
+#endif

- assert(mem);
- gd->ram_buf = mem;
+ size = CONFIG_SYS_SDRAM_SIZE;
addr = (ulong)(mem + size);

/*
diff --git a/doc/README.fdt-control b/doc/README.fdt-control
index 85bda03..69c69de 100644
--- a/doc/README.fdt-control
+++ b/doc/README.fdt-control
@@ -142,7 +142,11 @@ join the two:

and then flash image.bin onto your board.

-You cannot use both of these options at the same time.
+If CONFIG_OF_HOSTFILE os defined, then it will be read from a file on
+startup. This is only useful for sandbox. Use the -d flag to U-Boot to
+specify the file to read.
+
+You cannot use more than one of these options at the same time.

If you wish to put the fdt at a different address in memory, you can
define the "fdtcontroladdr" environment variable. This is the hex
diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index 67d788a..1c7bbe1 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -29,6 +29,7 @@
#define CONFIG_SANDBOX_BITS_PER_LONG 64

#define CONFIG_OF_CONTROL
+#define CONFIG_OF_HOSTFILE
#define CONFIG_OF_LIBFDT
#define CONFIG_LMB
#define CONFIG_FIT
@@ -76,6 +77,7 @@
#define CONFIG_SYS_MEMTEST_START 0x00100000
#define CONFIG_SYS_MEMTEST_END (CONFIG_SYS_MEMTEST_START + 0x1000)
#define CONFIG_PHYS_64BIT
+#define CONFIG_SYS_FDT_LOAD_ADDR 0x1000000

/* Size of our emulated memory */
#define CONFIG_SYS_SDRAM_SIZE (128 << 20)
--
1.7.7.3