diff mbox series

[v3,4/6] rockchip: find U-boot proper boot device by inverting the logic that sets it

Message ID 20240117-rk3588-spl-boot-dev-v3-4-72cb989e6da8@theobroma-systems.com
State Superseded
Delegated to: Kever Yang
Headers show
Series rockchip: add vendor-wide support for detecting U-Boot proper boot medium | expand

Commit Message

Quentin Schulz Jan. 17, 2024, 5:22 p.m. UTC
From: Quentin Schulz <quentin.schulz@theobroma-systems.com>

BOOT_DEVICE_* is set by spl_node_to_boot_device() depending on the block
device number associated with the MMC device the SPL used to load U-Boot
proper from. It is NOT related to the mmc alias in the Device Tree.

For SPI flashes, all SPI flashes will return BOOT_DEVICE_SPI so there's
currently no way to know from which one the SPL loaded U-Boot proper
from. Therefore, let's just find the first valid candidate in
/chosen/u-boot,spl-boot-order that is a SPI flash and return that path.
This is a best effort.

While the original implementation may have worked, using the exact same
mechanism but in inverted fashion makes it less likely to have
surprising corner-cases or side-effects.

A nice side-effect is that all existing and future Rockchip SoCs now
automatically have their /chosen/u-boot,spl-boot-device set.

Cc: Quentin Schulz <foss+uboot@0leil.net>
Signed-off-by: Quentin Schulz <quentin.schulz@theobroma-systems.com>
---
 arch/arm/mach-rockchip/px30/px30.c      |  7 ---
 arch/arm/mach-rockchip/rk3399/rk3399.c  |  6 ---
 arch/arm/mach-rockchip/spl-boot-order.c | 91 ++++++++++++++++++++++++++-------
 3 files changed, 73 insertions(+), 31 deletions(-)

Comments

Kever Yang Jan. 18, 2024, 6:57 a.m. UTC | #1
On 2024/1/18 01:22, Quentin Schulz wrote:
> From: Quentin Schulz <quentin.schulz@theobroma-systems.com>
>
> BOOT_DEVICE_* is set by spl_node_to_boot_device() depending on the block
> device number associated with the MMC device the SPL used to load U-Boot
> proper from. It is NOT related to the mmc alias in the Device Tree.
>
> For SPI flashes, all SPI flashes will return BOOT_DEVICE_SPI so there's
> currently no way to know from which one the SPL loaded U-Boot proper
> from. Therefore, let's just find the first valid candidate in
> /chosen/u-boot,spl-boot-order that is a SPI flash and return that path.
> This is a best effort.
>
> While the original implementation may have worked, using the exact same
> mechanism but in inverted fashion makes it less likely to have
> surprising corner-cases or side-effects.
>
> A nice side-effect is that all existing and future Rockchip SoCs now
> automatically have their /chosen/u-boot,spl-boot-device set.
>
> Cc: Quentin Schulz <foss+uboot@0leil.net>
> Signed-off-by: Quentin Schulz <quentin.schulz@theobroma-systems.com>
Reviewed-by: Kever Yang <kever.yang@rock-chips.com>

Thanks,
- Kever
> ---
>   arch/arm/mach-rockchip/px30/px30.c      |  7 ---
>   arch/arm/mach-rockchip/rk3399/rk3399.c  |  6 ---
>   arch/arm/mach-rockchip/spl-boot-order.c | 91 ++++++++++++++++++++++++++-------
>   3 files changed, 73 insertions(+), 31 deletions(-)
>
> diff --git a/arch/arm/mach-rockchip/px30/px30.c b/arch/arm/mach-rockchip/px30/px30.c
> index 7676adcb044..fc7456e680c 100644
> --- a/arch/arm/mach-rockchip/px30/px30.c
> +++ b/arch/arm/mach-rockchip/px30/px30.c
> @@ -443,10 +443,3 @@ void board_debug_uart_init(void)
>   #endif /* CONFIG_DEBUG_UART_BASE && CONFIG_DEBUG_UART_BASE == ... */
>   }
>   #endif /* CONFIG_DEBUG_UART_BOARD_INIT */
> -
> -#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
> -const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {
> -	[BOOT_DEVICE_MMC2] = "/mmc@ff370000",
> -	[BOOT_DEVICE_MMC1] = "/mmc@ff390000",
> -};
> -#endif
> diff --git a/arch/arm/mach-rockchip/rk3399/rk3399.c b/arch/arm/mach-rockchip/rk3399/rk3399.c
> index 6929de5603c..801a4a6662e 100644
> --- a/arch/arm/mach-rockchip/rk3399/rk3399.c
> +++ b/arch/arm/mach-rockchip/rk3399/rk3399.c
> @@ -175,12 +175,6 @@ void board_debug_uart_init(void)
>   #endif
>   
>   #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
> -const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {
> -	[BOOT_DEVICE_MMC2] = "/mmc@fe320000",
> -	[BOOT_DEVICE_MMC1] = "/mmc@fe330000",
> -	[BOOT_DEVICE_SPI] = "/spi@ff1d0000/flash@0",
> -};
> -
>   static void rk3399_force_power_on_reset(void)
>   {
>   	ofnode node;
> diff --git a/arch/arm/mach-rockchip/spl-boot-order.c b/arch/arm/mach-rockchip/spl-boot-order.c
> index 55d0976fb0a..f2cb17224e8 100644
> --- a/arch/arm/mach-rockchip/spl-boot-order.c
> +++ b/arch/arm/mach-rockchip/spl-boot-order.c
> @@ -10,6 +10,7 @@
>   #include <mmc.h>
>   #include <spl.h>
>   #include <asm/global_data.h>
> +#include <dm/uclass-internal.h>
>   
>   #if CONFIG_IS_ENABLED(OF_LIBFDT)
>   /**
> @@ -163,30 +164,84 @@ void board_boot_order(u32 *spl_boot_list)
>   		spl_boot_list[0] = spl_boot_device();
>   }
>   
> -__weak const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {};
> -
> -const char *spl_decode_boot_device(u32 boot_device)
> +int spl_decode_boot_device(u32 boot_device, char *buf, size_t buflen)
>   {
> -	const char *spl_bootdevice_ofpath = NULL;
> +	struct udevice *dev;
> +	int dev_num, ret;
> +
> +	if (boot_device == BOOT_DEVICE_SPI) {
> +		/* Revert spl_node_to_boot_device() logic to find appropriate SPI flash device */
> +
> +		/*
> +		 * Devices with multiple SPI flash devices will take the first SPI flash found in
> +		 * /chosen/u-boot,spl-boot-order.
> +		 */
> +		const void *blob = gd->fdt_blob;
> +		int chosen_node = fdt_path_offset(blob, "/chosen");
> +		int elem;
> +		int node;
> +		const char *conf;
> +
> +		if (chosen_node < 0) {
> +			debug("%s: /chosen not found\n", __func__);
> +			return -ENODEV;
> +		}
> +
> +		for (elem = 0;
> +		     (conf = fdt_stringlist_get(blob, chosen_node,
> +						"u-boot,spl-boot-order", elem, NULL));
> +		     elem++) {
> +			const char *alias;
> +
> +			/* Handle the case of 'same device the SPL was loaded from' */
> +			if (strncmp(conf, "same-as-spl", 11) == 0) {
> +				conf = board_spl_was_booted_from();
> +				if (!conf)
> +					continue;
> +			}
> +
> +			/* First check if the list element is an alias */
> +			alias = fdt_get_alias(blob, conf);
> +			if (alias)
> +				conf = alias;
> +
> +			/* Try to resolve the config item (or alias) as a path */
> +			node = fdt_path_offset(blob, conf);
> +			if (node < 0) {
> +				debug("%s: could not find %s in FDT\n", __func__, conf);
> +				continue;
> +			}
> +
> +			ret = uclass_find_device_by_of_offset(UCLASS_SPI_FLASH, node, &dev);
> +			if (ret) {
> +				debug("%s: could not find udevice for %s\n", __func__, conf);
> +				continue;
> +			}
>   
> -	if (boot_device < ARRAY_SIZE(spl_boot_devices))
> -		spl_bootdevice_ofpath = spl_boot_devices[boot_device];
> +			return ofnode_get_path(dev_ofnode(dev), buf, buflen);
> +		}
> +
> +		return -ENODEV;
> +	}
>   
> -	if (spl_bootdevice_ofpath)
> -		debug("%s: spl_bootdevice_id %x maps to '%s'\n",
> -		      __func__, boot_device, spl_bootdevice_ofpath);
> -	else
> -		debug("%s: failed to resolve spl_bootdevice_id %x\n",
> -		      __func__, boot_device);
> +	dev_num = (boot_device == BOOT_DEVICE_MMC1) ? 0 : 1;
> +
> +	ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
> +	if (ret) {
> +		debug("%s: could not find blk device for MMC device %d: %d\n",
> +		      __func__, dev_num, ret);
> +		return ret;
> +	}
>   
> -	return spl_bootdevice_ofpath;
> +	dev = dev_get_parent(dev);
> +	return ofnode_get_path(dev_ofnode(dev), buf, buflen);
>   }
>   
>   void spl_perform_fixups(struct spl_image_info *spl_image)
>   {
>   	void *blob = spl_image->fdt_addr;
> -	const char *boot_ofpath;
> -	int chosen;
> +	char boot_ofpath[512];
> +	int chosen, ret;
>   
>   	/*
>   	 * Inject the ofpath of the device the full U-Boot (or Linux in
> @@ -196,9 +251,9 @@ void spl_perform_fixups(struct spl_image_info *spl_image)
>   	if (!blob)
>   		return;
>   
> -	boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
> -	if (!boot_ofpath) {
> -		pr_err("%s: could not map boot_device to ofpath\n", __func__);
> +	ret = spl_decode_boot_device(spl_image->boot_device, boot_ofpath, sizeof(boot_ofpath));
> +	if (ret) {
> +		pr_err("%s: could not map boot_device to ofpath: %d\n", __func__, ret);
>   		return;
>   	}
>   
>
Kever Yang Jan. 18, 2024, 10:12 a.m. UTC | #2
Hi Quentin,

On 2024/1/18 01:22, Quentin Schulz wrote:
> From: Quentin Schulz <quentin.schulz@theobroma-systems.com>
>
> BOOT_DEVICE_* is set by spl_node_to_boot_device() depending on the block
> device number associated with the MMC device the SPL used to load U-Boot
> proper from. It is NOT related to the mmc alias in the Device Tree.
>
> For SPI flashes, all SPI flashes will return BOOT_DEVICE_SPI so there's
> currently no way to know from which one the SPL loaded U-Boot proper
> from. Therefore, let's just find the first valid candidate in
> /chosen/u-boot,spl-boot-order that is a SPI flash and return that path.
> This is a best effort.
>
> While the original implementation may have worked, using the exact same
> mechanism but in inverted fashion makes it less likely to have
> surprising corner-cases or side-effects.
>
> A nice side-effect is that all existing and future Rockchip SoCs now
> automatically have their /chosen/u-boot,spl-boot-device set.

Error happen in some 32bit SoC:

+arch/arm/mach-rockchip/spl-boot-order.c: In function 'spl_perform_fixups':
+arch/arm/mach-rockchip/spl-boot-order.c:242:31: error: 'struct 
spl_image_info' has no member named 'fdt_addr'
+  242 |         void *blob = spl_image->fdt_addr;
+      |                               ^~
+make[3]: *** [scripts/Makefile.build:257: 
spl/arch/arm/mach-rockchip/spl-boot-order.o] Error 1


Please add an option for this feature so that different SoC can have 
their choice.


Thanks,
- Kever
>
> Cc: Quentin Schulz <foss+uboot@0leil.net>
> Signed-off-by: Quentin Schulz <quentin.schulz@theobroma-systems.com>
> ---
>   arch/arm/mach-rockchip/px30/px30.c      |  7 ---
>   arch/arm/mach-rockchip/rk3399/rk3399.c  |  6 ---
>   arch/arm/mach-rockchip/spl-boot-order.c | 91 ++++++++++++++++++++++++++-------
>   3 files changed, 73 insertions(+), 31 deletions(-)
>
> diff --git a/arch/arm/mach-rockchip/px30/px30.c b/arch/arm/mach-rockchip/px30/px30.c
> index 7676adcb044..fc7456e680c 100644
> --- a/arch/arm/mach-rockchip/px30/px30.c
> +++ b/arch/arm/mach-rockchip/px30/px30.c
> @@ -443,10 +443,3 @@ void board_debug_uart_init(void)
>   #endif /* CONFIG_DEBUG_UART_BASE && CONFIG_DEBUG_UART_BASE == ... */
>   }
>   #endif /* CONFIG_DEBUG_UART_BOARD_INIT */
> -
> -#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
> -const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {
> -	[BOOT_DEVICE_MMC2] = "/mmc@ff370000",
> -	[BOOT_DEVICE_MMC1] = "/mmc@ff390000",
> -};
> -#endif
> diff --git a/arch/arm/mach-rockchip/rk3399/rk3399.c b/arch/arm/mach-rockchip/rk3399/rk3399.c
> index 6929de5603c..801a4a6662e 100644
> --- a/arch/arm/mach-rockchip/rk3399/rk3399.c
> +++ b/arch/arm/mach-rockchip/rk3399/rk3399.c
> @@ -175,12 +175,6 @@ void board_debug_uart_init(void)
>   #endif
>   
>   #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
> -const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {
> -	[BOOT_DEVICE_MMC2] = "/mmc@fe320000",
> -	[BOOT_DEVICE_MMC1] = "/mmc@fe330000",
> -	[BOOT_DEVICE_SPI] = "/spi@ff1d0000/flash@0",
> -};
> -
>   static void rk3399_force_power_on_reset(void)
>   {
>   	ofnode node;
> diff --git a/arch/arm/mach-rockchip/spl-boot-order.c b/arch/arm/mach-rockchip/spl-boot-order.c
> index 55d0976fb0a..f2cb17224e8 100644
> --- a/arch/arm/mach-rockchip/spl-boot-order.c
> +++ b/arch/arm/mach-rockchip/spl-boot-order.c
> @@ -10,6 +10,7 @@
>   #include <mmc.h>
>   #include <spl.h>
>   #include <asm/global_data.h>
> +#include <dm/uclass-internal.h>
>   
>   #if CONFIG_IS_ENABLED(OF_LIBFDT)
>   /**
> @@ -163,30 +164,84 @@ void board_boot_order(u32 *spl_boot_list)
>   		spl_boot_list[0] = spl_boot_device();
>   }
>   
> -__weak const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {};
> -
> -const char *spl_decode_boot_device(u32 boot_device)
> +int spl_decode_boot_device(u32 boot_device, char *buf, size_t buflen)
>   {
> -	const char *spl_bootdevice_ofpath = NULL;
> +	struct udevice *dev;
> +	int dev_num, ret;
> +
> +	if (boot_device == BOOT_DEVICE_SPI) {
> +		/* Revert spl_node_to_boot_device() logic to find appropriate SPI flash device */
> +
> +		/*
> +		 * Devices with multiple SPI flash devices will take the first SPI flash found in
> +		 * /chosen/u-boot,spl-boot-order.
> +		 */
> +		const void *blob = gd->fdt_blob;
> +		int chosen_node = fdt_path_offset(blob, "/chosen");
> +		int elem;
> +		int node;
> +		const char *conf;
> +
> +		if (chosen_node < 0) {
> +			debug("%s: /chosen not found\n", __func__);
> +			return -ENODEV;
> +		}
> +
> +		for (elem = 0;
> +		     (conf = fdt_stringlist_get(blob, chosen_node,
> +						"u-boot,spl-boot-order", elem, NULL));
> +		     elem++) {
> +			const char *alias;
> +
> +			/* Handle the case of 'same device the SPL was loaded from' */
> +			if (strncmp(conf, "same-as-spl", 11) == 0) {
> +				conf = board_spl_was_booted_from();
> +				if (!conf)
> +					continue;
> +			}
> +
> +			/* First check if the list element is an alias */
> +			alias = fdt_get_alias(blob, conf);
> +			if (alias)
> +				conf = alias;
> +
> +			/* Try to resolve the config item (or alias) as a path */
> +			node = fdt_path_offset(blob, conf);
> +			if (node < 0) {
> +				debug("%s: could not find %s in FDT\n", __func__, conf);
> +				continue;
> +			}
> +
> +			ret = uclass_find_device_by_of_offset(UCLASS_SPI_FLASH, node, &dev);
> +			if (ret) {
> +				debug("%s: could not find udevice for %s\n", __func__, conf);
> +				continue;
> +			}
>   
> -	if (boot_device < ARRAY_SIZE(spl_boot_devices))
> -		spl_bootdevice_ofpath = spl_boot_devices[boot_device];
> +			return ofnode_get_path(dev_ofnode(dev), buf, buflen);
> +		}
> +
> +		return -ENODEV;
> +	}
>   
> -	if (spl_bootdevice_ofpath)
> -		debug("%s: spl_bootdevice_id %x maps to '%s'\n",
> -		      __func__, boot_device, spl_bootdevice_ofpath);
> -	else
> -		debug("%s: failed to resolve spl_bootdevice_id %x\n",
> -		      __func__, boot_device);
> +	dev_num = (boot_device == BOOT_DEVICE_MMC1) ? 0 : 1;
> +
> +	ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
> +	if (ret) {
> +		debug("%s: could not find blk device for MMC device %d: %d\n",
> +		      __func__, dev_num, ret);
> +		return ret;
> +	}
>   
> -	return spl_bootdevice_ofpath;
> +	dev = dev_get_parent(dev);
> +	return ofnode_get_path(dev_ofnode(dev), buf, buflen);
>   }
>   
>   void spl_perform_fixups(struct spl_image_info *spl_image)
>   {
>   	void *blob = spl_image->fdt_addr;
> -	const char *boot_ofpath;
> -	int chosen;
> +	char boot_ofpath[512];
> +	int chosen, ret;
>   
>   	/*
>   	 * Inject the ofpath of the device the full U-Boot (or Linux in
> @@ -196,9 +251,9 @@ void spl_perform_fixups(struct spl_image_info *spl_image)
>   	if (!blob)
>   		return;
>   
> -	boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
> -	if (!boot_ofpath) {
> -		pr_err("%s: could not map boot_device to ofpath\n", __func__);
> +	ret = spl_decode_boot_device(spl_image->boot_device, boot_ofpath, sizeof(boot_ofpath));
> +	if (ret) {
> +		pr_err("%s: could not map boot_device to ofpath: %d\n", __func__, ret);
>   		return;
>   	}
>   
>
Quentin Schulz Jan. 22, 2024, 10:49 a.m. UTC | #3
Hi Kever,

On 1/18/24 11:12, Kever Yang wrote:
> Hi Quentin,
> 
> On 2024/1/18 01:22, Quentin Schulz wrote:
>> From: Quentin Schulz <quentin.schulz@theobroma-systems.com>
>>
>> BOOT_DEVICE_* is set by spl_node_to_boot_device() depending on the block
>> device number associated with the MMC device the SPL used to load U-Boot
>> proper from. It is NOT related to the mmc alias in the Device Tree.
>>
>> For SPI flashes, all SPI flashes will return BOOT_DEVICE_SPI so there's
>> currently no way to know from which one the SPL loaded U-Boot proper
>> from. Therefore, let's just find the first valid candidate in
>> /chosen/u-boot,spl-boot-order that is a SPI flash and return that path.
>> This is a best effort.
>>
>> While the original implementation may have worked, using the exact same
>> mechanism but in inverted fashion makes it less likely to have
>> surprising corner-cases or side-effects.
>>
>> A nice side-effect is that all existing and future Rockchip SoCs now
>> automatically have their /chosen/u-boot,spl-boot-device set.
> 
> Error happen in some 32bit SoC:
> 
> +arch/arm/mach-rockchip/spl-boot-order.c: In function 'spl_perform_fixups':
> +arch/arm/mach-rockchip/spl-boot-order.c:242:31: error: 'struct 
> spl_image_info' has no member named 'fdt_addr'
> +  242 |         void *blob = spl_image->fdt_addr;
> +      |                               ^~
> +make[3]: *** [scripts/Makefile.build:257: 
> spl/arch/arm/mach-rockchip/spl-boot-order.o] Error 1
> 
> 

It'd be nice to say **which** boards aren't working so that I can 
reproduce locally :)

I eventually figured we have GitLab CI/CD for your maintainer 
branch/repo here: 
https://source.denx.de/u-boot/custodians/u-boot-rockchip/-/pipelines. 
This also helped me figure out this wasn't the only build failure and I 
could send another patch before you had the opportunity to tell me I had 
broken something else :)

Giving the link of the failed pipeline would really help, please think 
about it for next time :) Thanks!

For people interested in build-testing all Rockchip platforms locally, I 
used the following script:

"""
#!/bin/sh

# Copy **some** TF-A binary in the parent directory of U-Boot git repo
export BL31=../bl31.elf
dd if=/dev/zero of=../zero.bin count=1
export ROCKCHIP_TPL=../zero.bin

for conf in $(git grep --files-with-matches ARCH_ROCKCHIP configs); do
         git clean -ffdx > /dev/null
	echo -n $conf=
         make $(basename "$conf") > /dev/null
	if grep -q -E "^CONFIG_ARM64=y" .config; then
		CROSS_COMPILE="aarch64-linux-gnu-"
		export TEE=
	else
		CROSS_COMPILE="arm-linux-gnu-"
		export TEE=../zero.bin
	fi
	LOG=$(mktemp)
	make KCFLAGS=-Werror CROSS_COMPILE="ccache $CROSS_COMPILE" -j$(nproc) > 
"$LOG" 2>&1
	RET=$?
	echo $RET
	if [ "$RET" != 0 ]; then
		cat "$LOG"
		exit $RET
	fi
done

exit 0
"""

Cheers,
Quentin
Tom Rini Jan. 22, 2024, 5:54 p.m. UTC | #4
On Mon, Jan 22, 2024 at 11:49:23AM +0100, Quentin Schulz wrote:
> Hi Kever,
> 
> On 1/18/24 11:12, Kever Yang wrote:
> > Hi Quentin,
> > 
> > On 2024/1/18 01:22, Quentin Schulz wrote:
> > > From: Quentin Schulz <quentin.schulz@theobroma-systems.com>
> > > 
> > > BOOT_DEVICE_* is set by spl_node_to_boot_device() depending on the block
> > > device number associated with the MMC device the SPL used to load U-Boot
> > > proper from. It is NOT related to the mmc alias in the Device Tree.
> > > 
> > > For SPI flashes, all SPI flashes will return BOOT_DEVICE_SPI so there's
> > > currently no way to know from which one the SPL loaded U-Boot proper
> > > from. Therefore, let's just find the first valid candidate in
> > > /chosen/u-boot,spl-boot-order that is a SPI flash and return that path.
> > > This is a best effort.
> > > 
> > > While the original implementation may have worked, using the exact same
> > > mechanism but in inverted fashion makes it less likely to have
> > > surprising corner-cases or side-effects.
> > > 
> > > A nice side-effect is that all existing and future Rockchip SoCs now
> > > automatically have their /chosen/u-boot,spl-boot-device set.
> > 
> > Error happen in some 32bit SoC:
> > 
> > +arch/arm/mach-rockchip/spl-boot-order.c: In function 'spl_perform_fixups':
> > +arch/arm/mach-rockchip/spl-boot-order.c:242:31: error: 'struct
> > spl_image_info' has no member named 'fdt_addr'
> > +  242 |         void *blob = spl_image->fdt_addr;
> > +      |                               ^~
> > +make[3]: *** [scripts/Makefile.build:257:
> > spl/arch/arm/mach-rockchip/spl-boot-order.o] Error 1
> > 
> > 
> 
> It'd be nice to say **which** boards aren't working so that I can reproduce
> locally :)
> 
> I eventually figured we have GitLab CI/CD for your maintainer branch/repo
> here: https://source.denx.de/u-boot/custodians/u-boot-rockchip/-/pipelines.
> This also helped me figure out this wasn't the only build failure and I
> could send another patch before you had the opportunity to tell me I had
> broken something else :)
> 
> Giving the link of the failed pipeline would really help, please think about
> it for next time :) Thanks!
> 
> For people interested in build-testing all Rockchip platforms locally, I
> used the following script:

Please note that you can also build all of the rockchip platforms
locally with:
tools/buildman/buildman --allow-missing rk rv

It won't be bootable as it will fake all require blobs, but all
platforms will be built. And buildman can be told a number of things to
be built, but "rockchip" only catches the cases where the board vendor
is "rockhip" rather than ARCH_ROCKCHIP, but "rk" and "rv" catch all of
the rkXXXX and rvXXXX SoCs.
Quentin Schulz Jan. 23, 2024, 6:06 p.m. UTC | #5
Hi Tom,

On 1/22/24 18:54, Tom Rini wrote:
> On Mon, Jan 22, 2024 at 11:49:23AM +0100, Quentin Schulz wrote:
>> Hi Kever,
>>
>> On 1/18/24 11:12, Kever Yang wrote:
>>> Hi Quentin,
>>>
>>> On 2024/1/18 01:22, Quentin Schulz wrote:
>>>> From: Quentin Schulz <quentin.schulz@theobroma-systems.com>
>>>>
>>>> BOOT_DEVICE_* is set by spl_node_to_boot_device() depending on the block
>>>> device number associated with the MMC device the SPL used to load U-Boot
>>>> proper from. It is NOT related to the mmc alias in the Device Tree.
>>>>
>>>> For SPI flashes, all SPI flashes will return BOOT_DEVICE_SPI so there's
>>>> currently no way to know from which one the SPL loaded U-Boot proper
>>>> from. Therefore, let's just find the first valid candidate in
>>>> /chosen/u-boot,spl-boot-order that is a SPI flash and return that path.
>>>> This is a best effort.
>>>>
>>>> While the original implementation may have worked, using the exact same
>>>> mechanism but in inverted fashion makes it less likely to have
>>>> surprising corner-cases or side-effects.
>>>>
>>>> A nice side-effect is that all existing and future Rockchip SoCs now
>>>> automatically have their /chosen/u-boot,spl-boot-device set.
>>>
>>> Error happen in some 32bit SoC:
>>>
>>> +arch/arm/mach-rockchip/spl-boot-order.c: In function 'spl_perform_fixups':
>>> +arch/arm/mach-rockchip/spl-boot-order.c:242:31: error: 'struct
>>> spl_image_info' has no member named 'fdt_addr'
>>> +  242 |         void *blob = spl_image->fdt_addr;
>>> +      |                               ^~
>>> +make[3]: *** [scripts/Makefile.build:257:
>>> spl/arch/arm/mach-rockchip/spl-boot-order.o] Error 1
>>>
>>>
>>
>> It'd be nice to say **which** boards aren't working so that I can reproduce
>> locally :)
>>
>> I eventually figured we have GitLab CI/CD for your maintainer branch/repo
>> here: https://source.denx.de/u-boot/custodians/u-boot-rockchip/-/pipelines.
>> This also helped me figure out this wasn't the only build failure and I
>> could send another patch before you had the opportunity to tell me I had
>> broken something else :)
>>
>> Giving the link of the failed pipeline would really help, please think about
>> it for next time :) Thanks!
>>
>> For people interested in build-testing all Rockchip platforms locally, I
>> used the following script:
> 
> Please note that you can also build all of the rockchip platforms
> locally with:
> tools/buildman/buildman --allow-missing rk rv
> 
> It won't be bootable as it will fake all require blobs, but all
> platforms will be built. And buildman can be told a number of things to
> be built, but "rockchip" only catches the cases where the board vendor
> is "rockhip" rather than ARCH_ROCKCHIP, but "rk" and "rv" catch all of
> the rkXXXX and rvXXXX SoCs.
> 

Ack, will be trying this one out, thanks for the heads up.

FYI, it was also missing "px30" among the terms to pass to buildman. Now 
I get 101 boards, the same as with my script :)

Cheers,
Quentin
diff mbox series

Patch

diff --git a/arch/arm/mach-rockchip/px30/px30.c b/arch/arm/mach-rockchip/px30/px30.c
index 7676adcb044..fc7456e680c 100644
--- a/arch/arm/mach-rockchip/px30/px30.c
+++ b/arch/arm/mach-rockchip/px30/px30.c
@@ -443,10 +443,3 @@  void board_debug_uart_init(void)
 #endif /* CONFIG_DEBUG_UART_BASE && CONFIG_DEBUG_UART_BASE == ... */
 }
 #endif /* CONFIG_DEBUG_UART_BOARD_INIT */
-
-#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
-const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {
-	[BOOT_DEVICE_MMC2] = "/mmc@ff370000",
-	[BOOT_DEVICE_MMC1] = "/mmc@ff390000",
-};
-#endif
diff --git a/arch/arm/mach-rockchip/rk3399/rk3399.c b/arch/arm/mach-rockchip/rk3399/rk3399.c
index 6929de5603c..801a4a6662e 100644
--- a/arch/arm/mach-rockchip/rk3399/rk3399.c
+++ b/arch/arm/mach-rockchip/rk3399/rk3399.c
@@ -175,12 +175,6 @@  void board_debug_uart_init(void)
 #endif
 
 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
-const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {
-	[BOOT_DEVICE_MMC2] = "/mmc@fe320000",
-	[BOOT_DEVICE_MMC1] = "/mmc@fe330000",
-	[BOOT_DEVICE_SPI] = "/spi@ff1d0000/flash@0",
-};
-
 static void rk3399_force_power_on_reset(void)
 {
 	ofnode node;
diff --git a/arch/arm/mach-rockchip/spl-boot-order.c b/arch/arm/mach-rockchip/spl-boot-order.c
index 55d0976fb0a..f2cb17224e8 100644
--- a/arch/arm/mach-rockchip/spl-boot-order.c
+++ b/arch/arm/mach-rockchip/spl-boot-order.c
@@ -10,6 +10,7 @@ 
 #include <mmc.h>
 #include <spl.h>
 #include <asm/global_data.h>
+#include <dm/uclass-internal.h>
 
 #if CONFIG_IS_ENABLED(OF_LIBFDT)
 /**
@@ -163,30 +164,84 @@  void board_boot_order(u32 *spl_boot_list)
 		spl_boot_list[0] = spl_boot_device();
 }
 
-__weak const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {};
-
-const char *spl_decode_boot_device(u32 boot_device)
+int spl_decode_boot_device(u32 boot_device, char *buf, size_t buflen)
 {
-	const char *spl_bootdevice_ofpath = NULL;
+	struct udevice *dev;
+	int dev_num, ret;
+
+	if (boot_device == BOOT_DEVICE_SPI) {
+		/* Revert spl_node_to_boot_device() logic to find appropriate SPI flash device */
+
+		/*
+		 * Devices with multiple SPI flash devices will take the first SPI flash found in
+		 * /chosen/u-boot,spl-boot-order.
+		 */
+		const void *blob = gd->fdt_blob;
+		int chosen_node = fdt_path_offset(blob, "/chosen");
+		int elem;
+		int node;
+		const char *conf;
+
+		if (chosen_node < 0) {
+			debug("%s: /chosen not found\n", __func__);
+			return -ENODEV;
+		}
+
+		for (elem = 0;
+		     (conf = fdt_stringlist_get(blob, chosen_node,
+						"u-boot,spl-boot-order", elem, NULL));
+		     elem++) {
+			const char *alias;
+
+			/* Handle the case of 'same device the SPL was loaded from' */
+			if (strncmp(conf, "same-as-spl", 11) == 0) {
+				conf = board_spl_was_booted_from();
+				if (!conf)
+					continue;
+			}
+
+			/* First check if the list element is an alias */
+			alias = fdt_get_alias(blob, conf);
+			if (alias)
+				conf = alias;
+
+			/* Try to resolve the config item (or alias) as a path */
+			node = fdt_path_offset(blob, conf);
+			if (node < 0) {
+				debug("%s: could not find %s in FDT\n", __func__, conf);
+				continue;
+			}
+
+			ret = uclass_find_device_by_of_offset(UCLASS_SPI_FLASH, node, &dev);
+			if (ret) {
+				debug("%s: could not find udevice for %s\n", __func__, conf);
+				continue;
+			}
 
-	if (boot_device < ARRAY_SIZE(spl_boot_devices))
-		spl_bootdevice_ofpath = spl_boot_devices[boot_device];
+			return ofnode_get_path(dev_ofnode(dev), buf, buflen);
+		}
+
+		return -ENODEV;
+	}
 
-	if (spl_bootdevice_ofpath)
-		debug("%s: spl_bootdevice_id %x maps to '%s'\n",
-		      __func__, boot_device, spl_bootdevice_ofpath);
-	else
-		debug("%s: failed to resolve spl_bootdevice_id %x\n",
-		      __func__, boot_device);
+	dev_num = (boot_device == BOOT_DEVICE_MMC1) ? 0 : 1;
+
+	ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
+	if (ret) {
+		debug("%s: could not find blk device for MMC device %d: %d\n",
+		      __func__, dev_num, ret);
+		return ret;
+	}
 
-	return spl_bootdevice_ofpath;
+	dev = dev_get_parent(dev);
+	return ofnode_get_path(dev_ofnode(dev), buf, buflen);
 }
 
 void spl_perform_fixups(struct spl_image_info *spl_image)
 {
 	void *blob = spl_image->fdt_addr;
-	const char *boot_ofpath;
-	int chosen;
+	char boot_ofpath[512];
+	int chosen, ret;
 
 	/*
 	 * Inject the ofpath of the device the full U-Boot (or Linux in
@@ -196,9 +251,9 @@  void spl_perform_fixups(struct spl_image_info *spl_image)
 	if (!blob)
 		return;
 
-	boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
-	if (!boot_ofpath) {
-		pr_err("%s: could not map boot_device to ofpath\n", __func__);
+	ret = spl_decode_boot_device(spl_image->boot_device, boot_ofpath, sizeof(boot_ofpath));
+	if (ret) {
+		pr_err("%s: could not map boot_device to ofpath: %d\n", __func__, ret);
 		return;
 	}