Skip to contentSkip to conversation

board_f: Add support for CONFIG_OF_BOARD_FIXUP for XIP images

PATCH In review Pali Rohár 4 participants 10 messages Aug 27, 2022 – Nov 21, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 board_f: Add support for CONFIG_OF_BOARD_FIXUP for XIP images

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.

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
common/board_f.c +7 -1
index 18e2246733b0..56b4eea24885 100644
@@ -911,7 +911,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 CONFIG_PRAM
@@ -926,6 +926,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,
@@ -936,7 +940,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,
dts/Kconfig +6 -0
index bc5f22029ff9..0bc3d741f8bc 100644
@@ -105,6 +105,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
Signature · 1 line
2.20.1
#1 lore raw

Can you rename the option to
OF_FDT_READONLY_BEFORE_RELOC
?

The INITIAL in the name may confuse people.

The functions are called *_fdt (reloc_fdt, fix_fdt), so maybe put FDT instead of DTB in the config option name.

Marek

#2 lore raw
Quoted 3 lines · On Sunday 28 August 2022 05:47:51 Marek Behún wrote:

Can you rename the option to
OF_FDT_READONLY_BEFORE_RELOC
?

I do not care what is the name of this option. All other options for this configuration have OF and DTB in its name, not FDT, and that is why I chose the current name.

Quoted 6 lines

The INITIAL in the name may confuse people.

The functions are called *_fdt (reloc_fdt, fix_fdt), so maybe put FDT instead of DTB in the config option name.

Marek

#3 lore raw

PATCH v2 1/1 board_f: Add support for CONFIG_OF_BOARD_FIXUP for XIP images

In reply to Pali Rohár

From: Pali Rohár <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
common/board_f.c +7 -1
index 18e2246733..35fb4a0753 100644
@@ -911,7 +911,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_DTB_READONLY_BEFORE_RELOC)
 	fix_fdt,
 #endif
 #ifdef CONFIG_PRAM
@@ -926,6 +926,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_DTB_READONLY_BEFORE_RELOC)
+	reloc_fdt,
+	fix_fdt,
+#endif
 	reserve_bootstage,
 	reserve_bloblist,
 	reserve_arch,
@@ -936,7 +940,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_DTB_READONLY_BEFORE_RELOC)
 	reloc_fdt,
+#endif
 	reloc_bootstage,
 	reloc_bloblist,
 	setup_reloc,
dts/Kconfig +6 -0
index bc5f22029f..29e5248b15 100644
@@ -105,6 +105,12 @@ config OF_EMBED
 
 endchoice
 
+config OF_DTB_READONLY_BEFORE_RELOC
+	bool "Initial DTB (before relocation) for DT control is read-only"
+	help
+	  If initial (before relocation) DTB for DT control is read-only,
+	  (e.g. points to memory-mapped flash memory), 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
Signature · 1 line
2.35.1
#4 lore raw

Simon, could you review this patch? If not, who can do it?

Quoted 79 lines · On Sunday 28 August 2022 17:19:29 Marek Behún wrote:

From: Pali Rohár <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
common/board_f.c +7 -1
index 18e2246733..35fb4a0753 100644
@@ -911,7 +911,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_DTB_READONLY_BEFORE_RELOC)
 	fix_fdt,
 #endif
 #ifdef CONFIG_PRAM
@@ -926,6 +926,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_DTB_READONLY_BEFORE_RELOC)
+	reloc_fdt,
+	fix_fdt,
+#endif
 	reserve_bootstage,
 	reserve_bloblist,
 	reserve_arch,
@@ -936,7 +940,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_DTB_READONLY_BEFORE_RELOC)
 	reloc_fdt,
+#endif
 	reloc_bootstage,
 	reloc_bloblist,
 	setup_reloc,
dts/Kconfig +6 -0
index bc5f22029f..29e5248b15 100644
@@ -105,6 +105,12 @@ config OF_EMBED
 
 endchoice
 
+config OF_DTB_READONLY_BEFORE_RELOC
+	bool "Initial DTB (before relocation) for DT control is read-only"
+	help
+	  If initial (before relocation) DTB for DT control is read-only,
+	  (e.g. points to memory-mapped flash memory), 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
Signature · 1 line
2.35.1
#5 lore raw
In reply to Marek Behún

Hi,

Quoted 18 lines · Marek Behún

From: Pali Rohár <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

Quoted 7 lines

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

Can we just make this the normal behaviour? I think it makes more sense to change the FDT after we have relocated it.

Regards,
Simon

#6 lore raw
Quoted 29 lines · On Monday 10 October 2022 17:48:58 Simon Glass wrote:

Hi,

Quoted 18 lines · Marek Behún

From: Pali Rohár <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

Quoted 7 lines

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
Quoted 5 lines

Can we just make this the normal behaviour? I think it makes more sense to change the FDT after we have relocated it.

Regards,
Simon

Marek? Any comments?

#7 lore raw

+ Tom

Quoted 37 lines · On Wednesday 02 November 2022 00:23:03 Pali Rohár wrote:
Quoted 29 lines · On Monday 10 October 2022 17:48:58 Simon Glass wrote:

Hi,

Quoted 18 lines · Marek Behún

From: Pali Rohár <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

Quoted 7 lines

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
Quoted 5 lines

Can we just make this the normal behaviour? I think it makes more sense to change the FDT after we have relocated it.

Regards,
Simon

Marek? Any comments?

#8 lore raw
Quoted 35 lines · Pali Rohár

+ Tom

Quoted 32 lines · On Wednesday 02 November 2022 00:23:03 Pali Rohár wrote:
Quoted 31 lines · On Monday 10 October 2022 17:48:58 Simon Glass wrote:

Hi,

Quoted 18 lines · Marek Behún

From: Pali Rohár <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

Quoted 7 lines

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

Can we just make this the normal behaviour? I think it makes more sense to change the FDT after we have relocated it.

Can we?

Signature · 1 line
Tom
#9 lore raw
Quoted 34 lines · On Mon, 21 Nov 2022 12:45:32 -0500 Tom Rini <[email protected]> wrote:
Quoted 33 lines · Pali Roh_r

+ Tom

Quoted 30 lines · On Wednesday 02 November 2022 00:23:03 Pali Roh_r wrote:
Quoted 29 lines · On Monday 10 October 2022 17:48:58 Simon Glass wrote:

Hi,

Quoted 25 lines · Marek Beh_n

From: Pali Roh_r <[email protected]>

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_DTB_READONLY_BEFORE_RELOC 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.

---
Changes since v1:
- just changed the new Kconfig option name and associated help string

---
 common/board_f.c | 8 +++++++-
 dts/Kconfig      | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)  
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Can we just make this the normal behaviour? I think it makes more sense to change the FDT after we have relocated it.

Can we?

Sorry, I didn't notice this thread. Do we know if there aren't boards that change fdt before reloc?

Marek