RITY boot environments use bootembedded as the legacy boot target. The
command is part of the interface shared by the generated environment and
U-Boot.

Locate the appended RITY FIT script after U-Boot's separate control
devicetree and execute it through the standard source command. This avoids
duplicating the legacy boot policy in C and retains source and bootm
authentication for secure builds.

Keep the command independently selectable because legacy FIT boot does
not depend on mtk_fdt_prepare.

Signed-off-by: Carlo Caione <[email protected]>
---
 arch/arm/mach-mediatek/Kconfig            |  7 ++++
 arch/arm/mach-mediatek/Makefile           |  1 +
 arch/arm/mach-mediatek/cmd_bootembedded.c | 59 +++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)

diff --git a/arch/arm/mach-mediatek/Kconfig b/arch/arm/mach-mediatek/Kconfig
index aedc5992965..a5541db13cf 100644
--- a/arch/arm/mach-mediatek/Kconfig
+++ b/arch/arm/mach-mediatek/Kconfig
@@ -243,6 +243,13 @@ config CMD_MTK_FDT_PREPARE
          Also provide the RITY-compatible dtbprobe command, or fdtprobe
          when devicetree authentication is enabled.
 
+config CMD_MTK_BOOTEMBEDDED
+       bool "Enable the RITY-compatible 'bootembedded' command"
+       depends on CMD_SOURCE && OF_SEPARATE
+       help
+         Provide the RITY legacy-boot command. Locate and run the boot script
+         appended after the separate U-Boot control devicetree.
+
 config MTK_FDT_AUTH
        bool "Authenticate MediaTek RITY devicetrees"
        depends on MTK_FDT_PREPARE && FIT_SIGNATURE
diff --git a/arch/arm/mach-mediatek/Makefile b/arch/arm/mach-mediatek/Makefile
index e11fddd27bc..9c595af75c4 100644
--- a/arch/arm/mach-mediatek/Makefile
+++ b/arch/arm/mach-mediatek/Makefile
@@ -5,6 +5,7 @@ obj-y   += cpu.o
 obj-$(CONFIG_MTK_TZ_MOVABLE)   += tzcfg.o
 obj-$(CONFIG_MTK_FDT_PREPARE)  += fdt_prepare.o
 obj-$(CONFIG_CMD_MTK_FDT_PREPARE)      += cmd_fdt_prepare.o
+obj-$(CONFIG_CMD_MTK_BOOTEMBEDDED)     += cmd_bootembedded.o
 obj-$(CONFIG_XPL_BUILD)        += spl.o
 
 obj-$(CONFIG_TARGET_MT7622) += mt7622/
diff --git a/arch/arm/mach-mediatek/cmd_bootembedded.c 
b/arch/arm/mach-mediatek/cmd_bootembedded.c
new file mode 100644
index 00000000000..99f92667223
--- /dev/null
+++ b/arch/arm/mach-mediatek/cmd_bootembedded.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2026 Carlo Caione <[email protected]>
+ */
+
+#include <asm-generic/sections.h>
+#include <command.h>
+#include <mapmem.h>
+#include <linux/kernel.h>
+#include <linux/libfdt.h>
+
+static int do_bootembedded(struct cmd_tbl *cmdtp, int flag, int argc,
+                          char *const argv[])
+{
+       ulong control_fdt_addr, script_addr;
+       const void *control_fdt, *script;
+       int ret;
+
+       /*
+        * The control devicetree and the boot script are appended to the
+        * U-Boot binary, so they stay at the load address even after U-Boot
+        * has relocated itself. Derive that address from the image size,
+        * which is relocation invariant, instead of from _end, which moves
+        * with the relocated image.
+        */
+       control_fdt_addr = CONFIG_TEXT_BASE + (ulong)(_end - (char *)&_start);
+       control_fdt = map_sysmem(control_fdt_addr, 0);
+       ret = fdt_check_header(control_fdt);
+       if (ret) {
+               printf("Invalid appended U-Boot control devicetree (%d)\n", 
ret);
+               goto unmap_control_fdt;
+       }
+
+       /*
+        * RITY image generation pads the image to a multiple of 8 bytes before
+        * appending the script ('dd bs=8 conv=sync'), because libfdt requires
+        * 8-byte alignment. Keep both sides in sync.
+        */
+       script_addr = ALIGN(control_fdt_addr + fdt_totalsize(control_fdt), 8);
+       unmap_sysmem(control_fdt);
+
+       script = map_sysmem(script_addr, 0);
+       ret = fdt_check_header(script);
+       unmap_sysmem(script);
+       if (ret) {
+               printf("No valid appended MediaTek boot script (%d)\n", ret);
+               return CMD_RET_FAILURE;
+       }
+
+       printf("Running appended MediaTek boot script at 0x%lx\n", script_addr);
+       return run_commandf("source %lx", script_addr);
+
+unmap_control_fdt:
+       unmap_sysmem(control_fdt);
+       return CMD_RET_FAILURE;
+}
+
+U_BOOT_CMD(bootembedded, 1, 1, do_bootembedded,
+          "run the appended MediaTek embedded boot script", "");

-- 
2.55.0

Reply via email to