vbe: Add a test for VBE device tree fixups

When a FIT includes some OS requests, U-Boot should process these and add
the requested info to corresponding subnodes of the /chosen node. Add a
pytest for this, which sets up the FIT, runs bootm and then uses a C
unit test to check that everything looks OK.

The test needs to run on sandbox_flattree since we don't support
device tree fixups on sandbox (live tree) yet. So enable BOOTMETH_VBE and
disable bootflow_system(), since EFI is not supported on
sandbox_flattree.

Add a link to the initial documentation.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/test/boot/Makefile b/test/boot/Makefile
index 9e9d5ae..5bb3f88 100644
--- a/test/boot/Makefile
+++ b/test/boot/Makefile
@@ -7,3 +7,4 @@
 ifdef CONFIG_OF_LIVE
 obj-$(CONFIG_BOOTMETH_VBE_SIMPLE) += vbe_simple.o
 endif
+obj-$(CONFIG_BOOTMETH_VBE) += vbe_fixup.o
diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c
index 8530523..1e8ea75 100644
--- a/test/boot/bootflow.c
+++ b/test/boot/bootflow.c
@@ -329,6 +329,8 @@
 {
 	struct udevice *dev;
 
+	if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
+		return 0;
 	ut_assertok(uclass_get_device_by_name(UCLASS_BOOTMETH, "efi_mgr",
 					      &dev));
 	sandbox_set_fake_efi_mgr_dev(dev, true);
diff --git a/test/boot/vbe_fixup.c b/test/boot/vbe_fixup.c
new file mode 100644
index 0000000..1b488e2
--- /dev/null
+++ b/test/boot/vbe_fixup.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for VBE device tree fix-ups
+ *
+ * Copyright 2022 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <dm/ofnode.h>
+#include <linux/libfdt.h>
+#include <test/test.h>
+#include <test/ut.h>
+#include "bootstd_common.h"
+
+/* Basic test of reading nvdata and updating a fwupd node in the device tree */
+static int vbe_test_fixup(struct unit_test_state *uts)
+{
+	ofnode chosen, node;
+	const char *data;
+	oftree tree;
+	int size;
+
+	/*
+	 * This test works when called from test_vbe.py and it must use the
+	 * flat tree, since device tree fix-ups do not yet support live tree.
+	 */
+	if (!working_fdt)
+		return 0;
+
+	tree = oftree_from_fdt(working_fdt);
+	ut_assert(oftree_valid(tree));
+
+	chosen = oftree_path(tree, "/chosen");
+	ut_assert(ofnode_valid(chosen));
+
+	/* check the things set up for the FIT in test_vbe.py */
+	node = ofnode_find_subnode(chosen, "random");
+
+	/* ignore if this test is run on its own */
+	if (!ofnode_valid(node))
+		return 0;
+	data = ofnode_read_prop(node, "data", &size);
+	ut_asserteq(0x40, size);
+
+	node = ofnode_find_subnode(chosen, "aslr2");
+	ut_assert(ofnode_valid(node));
+	data = ofnode_read_prop(node, "data", &size);
+	ut_asserteq(4, size);
+
+	node = ofnode_find_subnode(chosen, "efi-runtime");
+	ut_assert(ofnode_valid(node));
+	data = ofnode_read_prop(node, "data", &size);
+	ut_asserteq(4, size);
+
+	return 0;
+}
+BOOTSTD_TEST(vbe_test_fixup,
+	     UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
diff --git a/test/py/tests/fit_util.py b/test/py/tests/fit_util.py
index fcec4c4..79718d4 100644
--- a/test/py/tests/fit_util.py
+++ b/test/py/tests/fit_util.py
@@ -36,7 +36,7 @@
         print(base_its % params, file=outf)
     return its
 
-def make_fit(cons, mkimage, base_its, params, basename='test.fit'):
+def make_fit(cons, mkimage, base_its, params, basename='test.fit', base_fdt=None):
     """Make a sample .fit file ready for loading
 
     This creates a .its script with the selected parameters and uses mkimage to
@@ -55,6 +55,9 @@
     fit = make_fname(cons, basename)
     its = make_its(cons, base_its, params)
     util.run_and_log(cons, [mkimage, '-f', its, fit])
+    if base_fdt:
+        with open(make_fname(cons, 'u-boot.dts'), 'w') as fd:
+            fd.write(base_fdt)
     return fit
 
 def make_kernel(cons, basename, text):
diff --git a/test/py/tests/test_vbe.py b/test/py/tests/test_vbe.py
new file mode 100644
index 0000000..559c291
--- /dev/null
+++ b/test/py/tests/test_vbe.py
@@ -0,0 +1,123 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright 2022 Google LLC
+#
+# Test addition of VBE
+
+import pytest
+
+import fit_util
+
+# Define a base ITS which we can adjust using % and a dictionary
+base_its = '''
+/dts-v1/;
+
+/ {
+        description = "Example kernel";
+
+        images {
+            kernel-1 {
+                data = /incbin/("%(kernel)s");
+                type = "kernel";
+                arch = "sandbox";
+                os = "linux";
+                load = <0x40000>;
+                entry = <0x8>;
+                compression = "%(compression)s";
+
+                random {
+                    compatible = "vbe,random-rand";
+                    vbe,size = <0x40>;
+                    vbe,required;
+                };
+                aslr1 {
+                    compatible = "vbe,aslr-move";
+                    vbe,align = <0x100000>;
+                };
+                aslr2 {
+                    compatible = "vbe,aslr-rand";
+                };
+                efi-runtime {
+                    compatible = "vbe,efi-runtime-rand";
+                };
+                wibble {
+                    compatible = "vbe,wibble";
+                };
+            };
+
+            fdt-1 {
+                description = "snow";
+                data = /incbin/("%(fdt)s");
+                type = "flat_dt";
+                arch = "sandbox";
+                load = <%(fdt_addr)#x>;
+                compression = "%(compression)s";
+            };
+        };
+        configurations {
+            default = "conf-1";
+            conf-1 {
+                kernel = "kernel-1";
+                fdt = "fdt-1";
+            };
+        };
+};
+'''
+
+# Define a base FDT - currently we don't use anything in this
+base_fdt = '''
+/dts-v1/;
+
+/ {
+    chosen {
+    };
+};
+'''
+
+# This is the U-Boot script that is run for each test. First load the FIT,
+# then run the 'bootm' command, then run the unit test which checks that the
+# working tree has the required things filled in according to the OS requests
+# above (random, aslr2, etc.)
+base_script = '''
+host load hostfs 0 %(fit_addr)x %(fit)s
+fdt addr %(fit_addr)x
+bootm start %(fit_addr)x
+bootm loados
+bootm prep
+fdt addr
+fdt print
+ut bootstd vbe_test_fixup
+'''
+
+@pytest.mark.boardspec('sandbox_flattree')
+@pytest.mark.requiredtool('dtc')
+def test_vbe(u_boot_console):
+    cons = u_boot_console
+    kernel = fit_util.make_kernel(cons, 'vbe-kernel.bin', 'kernel')
+    fdt = fit_util.make_dtb(cons, base_fdt, 'vbe-fdt')
+    fdt_out = fit_util.make_fname(cons, 'fdt-out.dtb')
+
+    params = {
+        'fit_addr' : 0x1000,
+
+        'kernel' : kernel,
+
+        'fdt' : fdt,
+        'fdt_out' : fdt_out,
+        'fdt_addr' : 0x80000,
+        'fdt_size' : 0x1000,
+
+        'compression' : 'none',
+    }
+    mkimage = cons.config.build_dir + '/tools/mkimage'
+    fit = fit_util.make_fit(cons, mkimage, base_its, params, 'test-vbe.fit',
+                            base_fdt)
+    params['fit'] = fit
+    cmd = base_script % params
+
+    with cons.log.section('Kernel load'):
+        output = cons.run_command_list(cmd.splitlines())
+
+    # This is a little wonky since there are two tests running in CI. The final
+    # one is the 'ut bootstd' command above
+    failures = [line for line in output if 'Failures' in line]
+    assert len(failures) >= 1 and 'Failures: 0' in failures[-1]