[PATCH] MIPS: add generic device tree 2nd stage support

Antony Pavlov antonynpavlov at gmail.com
Sun Mar 10 14:54:34 PDT 2024


Building and running barebox for 32-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config
make -s oldconfig
make -s
qemu-system-mips -M malta -cpu 24Kf -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img \
            -net user,tftp=$(pwd) -net nic,model=e1000

Building and running barebox for 64-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta64el_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config
make -s oldconfig
make -s
qemu-system-mips64el -M malta -cpu MIPS64R2-generic -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img.swapped \
            -net user,tftp=$(pwd) -net nic,model=e1000

Use bootm to run barebox-dt-2nd.img (external device tree):

barebox at qemu malta:/ dhcp
barebox at qemu malta:/ cp /mnt/tftp/images/barebox-dt-2nd.img barebox.img
barebox at qemu malta:/ cp /mnt/tftp/arch/mips/dts/qemu-malta.dtb .
barebox at qemu malta:/ imd barebox.img
barebox at qemu malta:/ bootm -o qemu-malta.dtb barebox.img

Use bootm to run barebox-qemu-malta.img (encapsulated device tree):

barebox at qemu malta:/ dhcp
barebox at qemu malta:/ cp /mnt/tftp/images/barebox-qemu-malta.img barebox.img
barebox at qemu malta:/ imd barebox.img
barebox at qemu malta:/ bootm barebox.img

N.B. Use just the same commands for both 32-bit and 64-bit malta boards.

Signed-off-by: Antony Pavlov <antonynpavlov at gmail.com>
---
 arch/mips/Kconfig               | 10 ++++++++++
 arch/mips/boards/Makefile       |  2 ++
 arch/mips/boards/board-dt-2nd.S | 35 +++++++++++++++++++++++++++++++++
 arch/mips/boot/main_entry-pbl.c |  4 ++++
 arch/mips/lib/bootm.c           | 13 ++++++++++--
 5 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index 63ba6aa9d5a..14062dee347 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -319,6 +319,16 @@ config 64BIT
 
 endchoice
 
+config BOARD_MIPS_GENERIC_DT
+	select BOARD_GENERIC_DT
+	depends on OFDEVICE
+	bool "Build generic MIPS device tree 2nd stage image"
+	help
+	  This enables compilation of a generic image that can be started 2nd
+	  stage from barebox or from qemu. It picks up a device tree passed
+	  in a1 like the Kernel does.
+	  The image will be called images/barebox-dt-2nd.img
+
 menu "MIPS specific settings"
 
 config CMD_MIPS_CPUINFO
diff --git a/arch/mips/boards/Makefile b/arch/mips/boards/Makefile
index 9402035856c..c4ce599c934 100644
--- a/arch/mips/boards/Makefile
+++ b/arch/mips/boards/Makefile
@@ -13,3 +13,5 @@ obj-$(CONFIG_BOARD_QEMU_MALTA) += qemu-malta/
 obj-$(CONFIG_BOARD_RZX50) += ritmix-rzx50/
 obj-$(CONFIG_BOARD_TPLINK_MR3020) += tplink-mr3020/
 obj-$(CONFIG_BOARD_TPLINK_WDR4300) += tplink-wdr4300/
+
+pbl-$(CONFIG_BOARD_MIPS_GENERIC_DT) += board-dt-2nd.o
diff --git a/arch/mips/boards/board-dt-2nd.S b/arch/mips/boards/board-dt-2nd.S
new file mode 100644
index 00000000000..a1465f09e36
--- /dev/null
+++ b/arch/mips/boards/board-dt-2nd.S
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Startup Code for generic MIPS device tree 2nd stage image
+ *
+ * Copyright (C) 2024 Antony Pavlov <antonynpavlov at gmail.com>
+ */
+
+#include <asm/asm.h>
+#include <asm/pbl_macros.h>
+#include <linux/sizes.h>
+
+ENTRY_FUNCTION(start_dt_2nd)
+
+	/* save device tree address in v1 */
+	move	v1, a1
+
+	mips_cpu_setup
+
+	copy_to_link_location	start_dt_2nd
+
+	stack_setup
+
+	/* pbl_main_entry() computes fdt_len by itself
+	 * if fdt == fdt_end */
+	move	a0, v1 /* fdt */
+	move	a1, v1 /* fdt_end */
+	PTR_LI	a2, SZ_256M /* ram_size */
+	PTR_LA	v0, pbl_main_entry
+	jal	v0
+	 nop
+
+	/* No return */
+1:
+	b	1b
+	 nop
diff --git a/arch/mips/boot/main_entry-pbl.c b/arch/mips/boot/main_entry-pbl.c
index 78982fd995e..389dc94f370 100644
--- a/arch/mips/boot/main_entry-pbl.c
+++ b/arch/mips/boot/main_entry-pbl.c
@@ -11,6 +11,7 @@
 #include <asm/sections.h>
 #include <asm-generic/memory_layout.h>
 #include <debug_ll.h>
+#include <asm/unaligned.h>
 
 extern void *input_data;
 extern void *input_data_end;
@@ -45,6 +46,9 @@ void __section(.text_entry) pbl_main_entry(void *fdt, void *fdt_end,
 	barebox_uncompress(&input_data, piggy_len);
 
 	fdt_len = (unsigned long)fdt_end - (unsigned long)fdt;
+	if (!fdt_len) {
+		fdt_len = get_unaligned_be32((void *)((unsigned long)fdt + 4));
+	}
 	fdt_new = (void *)PAGE_ALIGN_DOWN(TEXT_BASE - MALLOC_SIZE - STACK_SIZE - fdt_len);
 	memcpy(fdt_new, fdt, fdt_len);
 
diff --git a/arch/mips/lib/bootm.c b/arch/mips/lib/bootm.c
index 643ad57e4db..86573dec7f1 100644
--- a/arch/mips/lib/bootm.c
+++ b/arch/mips/lib/bootm.c
@@ -16,12 +16,21 @@
 
 static int do_bootm_barebox(struct image_data *data)
 {
-	void (*barebox)(void);
+	void (*barebox)(int, void *);
+	void *fdt = NULL;
 
 	barebox = read_file(data->os_file, NULL);
 	if (!barebox)
 		return -EINVAL;
 
+	if (data->oftree_file) {
+		fdt = bootm_get_devicetree(data);
+		if (IS_ERR(fdt)) {
+			pr_err("Failed to load dtb\n");
+			return PTR_ERR(fdt);
+		}
+	}
+
 	if (data->dryrun) {
 		free(barebox);
 		return 0;
@@ -29,7 +38,7 @@ static int do_bootm_barebox(struct image_data *data)
 
 	shutdown_barebox();
 
-	barebox();
+	barebox(-2, fdt);
 
 	restart_machine();
 }
-- 
2.39.0




More information about the barebox mailing list