[Concept,08/17] ulib: riscv: Add demo binary and examples for qemu-riscv64

Message ID 20260216013511.4079770-9-sjg@u-boot.org
State New
Headers
Series ulib: Add multi-arch demo and EFI app support |

Commit Message

Simon Glass Feb. 16, 2026, 1:34 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a riscv64 example following the same pattern as ARM64. The demo
binary is re-linked with the example objects and then run through
prelink-riscv to resolve GOT entries, which is required for RISC-V
static PIE binaries.

Enable CONFIG_ULIB and CONFIG_EXAMPLES in qemu-riscv64_defconfig and
add a pytest for the demo.

Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 arch/riscv/Makefile            |  7 ++++++
 configs/qemu-riscv64_defconfig |  2 ++
 test/py/tests/test_ulib.py     | 42 +++++++++++++++++++++++++++-------
 3 files changed, 43 insertions(+), 8 deletions(-)
  

Patch

diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index aaa5a02e17c..afae33812e4 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -62,3 +62,10 @@  endif
 libs-y += arch/riscv/cpu/
 libs-y += arch/riscv/cpu/$(CPU)/
 libs-y += arch/riscv/lib/
+
+ifdef CONFIG_EXAMPLES
+EXAMPLE_ARCH := riscv
+EXAMPLE_APPEND_DTB := y
+EXAMPLE_POST_LINK = @tools/prelink-riscv $@
+include scripts/Makefile.ulib-example
+endif
diff --git a/configs/qemu-riscv64_defconfig b/configs/qemu-riscv64_defconfig
index 6b2fed4ad16..4423d36b782 100644
--- a/configs/qemu-riscv64_defconfig
+++ b/configs/qemu-riscv64_defconfig
@@ -22,3 +22,5 @@  CONFIG_FLASH_SHOW_PROGRESS=0
 CONFIG_SYS_MAX_FLASH_BANKS=2
 CONFIG_UTHREAD=y
 CONFIG_UNIT_TEST=y
+CONFIG_ULIB=y
+CONFIG_EXAMPLES=y
diff --git a/test/py/tests/test_ulib.py b/test/py/tests/test_ulib.py
index 94d40256c59..71fe751efc9 100644
--- a/test/py/tests/test_ulib.py
+++ b/test/py/tests/test_ulib.py
@@ -290,18 +290,44 @@  def test_ulib_demo_rom_64(ubman):
     """Test the ulib demo ROM image under QEMU x86_64."""
     run_x86_rom_demo(ubman, 'qemu-system-x86_64')
 
-@pytest.mark.localqemu
-@pytest.mark.boardspec('qemu_arm64')
-@pytest.mark.buildconfigspec("examples")
-def test_ulib_demo_arm64(ubman):
-    """Test the ulib demo binary under QEMU ARM64."""
+def run_bios_demo(ubman, qemu_binary, extra_qemu_args=None):
+    """Boot the demo.bin binary under QEMU and check for expected output.
+
+    Locates demo.bin in the build directory, launches the given QEMU
+    binary with it as -bios, and asserts that the expected demo output
+    is present.
+
+    Args:
+        ubman (ConsoleBase): Test fixture providing build directory
+            etc.
+        qemu_binary (str): QEMU system binary
+            (e.g. 'qemu-system-aarch64')
+        extra_qemu_args (list): Additional QEMU arguments
+            (e.g. ['-cpu', 'cortex-a57'])
+    """
     build = ubman.config.build_dir
     demo_bin = os.path.join(build, 'examples', 'ulib', 'demo.bin')
 
     assert os.path.exists(demo_bin), 'demo.bin not found in build directory'
-    assert shutil.which('qemu-system-aarch64'), 'qemu-system-aarch64 not found'
+    assert shutil.which(qemu_binary), f'{qemu_binary} not found'
 
-    cmd = ['qemu-system-aarch64', '-machine', 'virt', '-cpu', 'cortex-a57',
-           '-nographic', '-no-reboot', '-bios', demo_bin]
+    cmd = [qemu_binary, '-machine', 'virt', '-nographic', '-no-reboot',
+           '-bios', demo_bin]
+    if extra_qemu_args:
+        cmd += extra_qemu_args
     out = run_qemu_demo(cmd)
     assert_demo_output(out)
+
+@pytest.mark.localqemu
+@pytest.mark.boardspec('qemu_arm64')
+@pytest.mark.buildconfigspec("examples")
+def test_ulib_demo_arm64(ubman):
+    """Test the ulib demo binary under QEMU ARM64."""
+    run_bios_demo(ubman, 'qemu-system-aarch64', ['-cpu', 'cortex-a57'])
+
+@pytest.mark.localqemu
+@pytest.mark.boardspec('qemu-riscv64')
+@pytest.mark.buildconfigspec("examples")
+def test_ulib_demo_riscv64(ubman):
+    """Test the ulib demo binary under QEMU RISC-V 64."""
+    run_bios_demo(ubman, 'qemu-system-riscv64')