diff mbox series

[1/6] board_f: Add support for CONFIG_OF_BOARD_FIXUP for XIP images

Message ID 20240606163326.386950-2-marek.mojik@nic.cz
State Accepted
Commit 0858e03b3d1f5d8270fe658f11cc8159dd32e595
Delegated to: Tom Rini
Headers show
Series Add Turris 1.x board | expand

Commit Message

Marek Mojík June 6, 2024, 4:33 p.m. UTC
From: Pali Rohár <pali@kernel.org>

When U-Boot is running from flash memory (execute in place) then
gd->fdt_blob before relocation points to read-only flash memory.

So U-Boot calls board_fix_fdt() with read-only gd->fdt_blob pointer which
cause immediate CPU crash when callback is trying to modify gd->fdt_blob.

Fix this issue by introducing a new config option OF_INITIAL_DTB_READONLY
which moves fix_fdt callback after the reloc_fdt callback. This makes
CONFIG_OF_BOARD_FIXUP working also if U-Boot before relocation is not
running from read/write (S)RAM memory.

This is required for mpc85xx boards when booting from flash NOR.

Signed-off-by: Pali Rohár <pali@kernel.org>
Signed-off-by: Marek Mojík <marek.mojik@nic.cz>
---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

Comments

Marek Behún July 11, 2024, 2:50 p.m. UTC | #1
Reviewed-by: Marek Behún <kabel@kernel.org>
Simon Glass July 13, 2024, 3:13 p.m. UTC | #2
Hi Marek,

On Thu, 6 Jun 2024 at 17:33, Marek Mojík <marek.mojik@nic.cz> wrote:
>
> From: Pali Rohár <pali@kernel.org>
>
> When U-Boot is running from flash memory (execute in place) then
> gd->fdt_blob before relocation points to read-only flash memory.
>
> So U-Boot calls board_fix_fdt() with read-only gd->fdt_blob pointer which
> cause immediate CPU crash when callback is trying to modify gd->fdt_blob.
>
> Fix this issue by introducing a new config option OF_INITIAL_DTB_READONLY
> which moves fix_fdt callback after the reloc_fdt callback. This makes
> CONFIG_OF_BOARD_FIXUP working also if U-Boot before relocation is not
> running from read/write (S)RAM memory.
>
> This is required for mpc85xx boards when booting from flash NOR.
>
> Signed-off-by: Pali Rohár <pali@kernel.org>
> Signed-off-by: Marek Mojík <marek.mojik@nic.cz>
> ---
>  common/board_f.c | 8 +++++++-
>  dts/Kconfig      | 6 ++++++
>  2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/common/board_f.c b/common/board_f.c
> index 212ffb3090..22c180b218 100644
> --- a/common/board_f.c
> +++ b/common/board_f.c
> @@ -949,7 +949,7 @@ static const init_fnc_t init_sequence_f[] = {
>          *  - board info struct
>          */
>         setup_dest_addr,
> -#ifdef CONFIG_OF_BOARD_FIXUP
> +#if defined(CONFIG_OF_BOARD_FIXUP) && !defined(CONFIG_OF_INITIAL_DTB_READONLY)
>         fix_fdt,
>  #endif
>  #ifdef CFG_PRAM
> @@ -965,6 +965,10 @@ static const init_fnc_t init_sequence_f[] = {
>         reserve_board,
>         reserve_global_data,
>         reserve_fdt,
> +#if defined(CONFIG_OF_BOARD_FIXUP) && defined(CONFIG_OF_INITIAL_DTB_READONLY)
> +       reloc_fdt,
> +       fix_fdt,
> +#endif
>         reserve_bootstage,
>         reserve_bloblist,
>         reserve_arch,
> @@ -975,7 +979,9 @@ static const init_fnc_t init_sequence_f[] = {
>         setup_bdinfo,
>         display_new_sp,
>         INIT_FUNC_WATCHDOG_RESET
> +#if !defined(CONFIG_OF_BOARD_FIXUP) || !defined(CONFIG_OF_INITIAL_DTB_READONLY)
>         reloc_fdt,

Can you not do the fix here? We should keep all the reserve_... calls
together, and have all the reloc_... calls after that.

> +#endif
>         reloc_bootstage,
>         reloc_bloblist,
>         setup_reloc,
> diff --git a/dts/Kconfig b/dts/Kconfig
> index 6883a000a0..569d4be338 100644
> --- a/dts/Kconfig
> +++ b/dts/Kconfig
> @@ -145,6 +145,12 @@ config OF_EMBED
>
>  endchoice
>
> +config OF_INITIAL_DTB_READONLY
> +       bool "Initial DTB for DT control is read-only"
> +       help
> +         If initial DTB for DT control is read-only (e.g. points to
> +         memory-mapped flash memory), then set this option.
> +
>  config OF_BOARD
>         bool "Provided by the board (e.g a previous loader) at runtime"
>         default y if SANDBOX || OF_HAS_PRIOR_STAGE
> --
> 2.45.1
>

This should really use events. You can have an event like
INITCALL_EVENT(EVT_RESERVE_DONE), for example.

Regards,
Simon
diff mbox series

Patch

diff --git a/common/board_f.c b/common/board_f.c
index 212ffb3090..22c180b218 100644
--- a/common/board_f.c
+++ b/common/board_f.c
@@ -949,7 +949,7 @@  static const init_fnc_t init_sequence_f[] = {
 	 *  - board info struct
 	 */
 	setup_dest_addr,
-#ifdef CONFIG_OF_BOARD_FIXUP
+#if defined(CONFIG_OF_BOARD_FIXUP) && !defined(CONFIG_OF_INITIAL_DTB_READONLY)
 	fix_fdt,
 #endif
 #ifdef CFG_PRAM
@@ -965,6 +965,10 @@  static const init_fnc_t init_sequence_f[] = {
 	reserve_board,
 	reserve_global_data,
 	reserve_fdt,
+#if defined(CONFIG_OF_BOARD_FIXUP) && defined(CONFIG_OF_INITIAL_DTB_READONLY)
+	reloc_fdt,
+	fix_fdt,
+#endif
 	reserve_bootstage,
 	reserve_bloblist,
 	reserve_arch,
@@ -975,7 +979,9 @@  static const init_fnc_t init_sequence_f[] = {
 	setup_bdinfo,
 	display_new_sp,
 	INIT_FUNC_WATCHDOG_RESET
+#if !defined(CONFIG_OF_BOARD_FIXUP) || !defined(CONFIG_OF_INITIAL_DTB_READONLY)
 	reloc_fdt,
+#endif
 	reloc_bootstage,
 	reloc_bloblist,
 	setup_reloc,
diff --git a/dts/Kconfig b/dts/Kconfig
index 6883a000a0..569d4be338 100644
--- a/dts/Kconfig
+++ b/dts/Kconfig
@@ -145,6 +145,12 @@  config OF_EMBED
 
 endchoice
 
+config OF_INITIAL_DTB_READONLY
+	bool "Initial DTB for DT control is read-only"
+	help
+	  If initial DTB for DT control is read-only (e.g. points to
+	  memory-mapped flash memory), then set this option.
+
 config OF_BOARD
 	bool "Provided by the board (e.g a previous loader) at runtime"
 	default y if SANDBOX || OF_HAS_PRIOR_STAGE