Skip to contentSkip to conversation

spl: mmc: Use correct MMC device when loading image

PATCH Superseded Harald Seiler 3 participants 4 messages Jul 6, 2022 – Jul 11, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spl: mmc: Use correct MMC device when loading image

When attempting to load images from multiple MMC devices in sequence, spl_mmc_load() chooses the wrong device from the second attempt onwards.

The reason is that MMC initialization is only done on its first call and spl_mmc_load() will then continue using this same device for all future calls.

Fix this by checking the devnum of the "cached" device struct against the one which is requested. If they match, use the cached one but if they do not match, initialize the new device.

This fixes specifying multiple MMC devices in the SPL's boot order to fall back when U-Boot Proper is corrupted or missing on the first attempted MMC device.

Fixes e1eb6ada4e38 ("spl: Make image loader infrastructure more universal")
---
 common/spl/spl_mmc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
common/spl/spl_mmc.c +4 -2
index e1a7d25bd0..22c8a8097c 100644
@@ -371,9 +371,11 @@ int spl_mmc_load(struct spl_image_info *spl_image,
 	u32 boot_mode;
 	int err = 0;
 	__maybe_unused int part = 0;
+	int mmc_dev;
 
-	/* Perform peripheral init only once */
-	if (!mmc) {
+	/* Perform peripheral init only once for an mmc device */
+	mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
+	if (!mmc || mmc->block_dev.devnum != mmc_dev) {
 		err = spl_mmc_find_device(&mmc, bootdev->boot_device);
 		if (err)
 			return err;
Signature · 1 line
2.36.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Harald Seiler

When attempting to load images from multiple MMC devices in sequence, spl_mmc_load() chooses the wrong device from the second attempt onwards.

The reason is that MMC initialization is only done on its first call and spl_mmc_load() will then continue using this same device for all future calls.

Fix this by checking the devnum of the "cached" device struct against the one which is requested. If they match, use the cached one but if they do not match, initialize the new device.

Quoted 6 lines

This fixes specifying multiple MMC devices in the SPL's boot order to fall back when U-Boot Proper is corrupted or missing on the first attempted MMC device.

Fixes e1eb6ada4e38 ("spl: Make image loader infrastructure more universal")
#2 lore raw
In reply to Harald Seiler

Hi Harald,

Quoted 26 lines · Harald Seiler

When attempting to load images from multiple MMC devices in sequence, spl_mmc_load() chooses the wrong device from the second attempt onwards.

The reason is that MMC initialization is only done on its first call and spl_mmc_load() will then continue using this same device for all future calls.

Fix this by checking the devnum of the "cached" device struct against the one which is requested. If they match, use the cached one but if they do not match, initialize the new device.

This fixes specifying multiple MMC devices in the SPL's boot order to fall back when U-Boot Proper is corrupted or missing on the first attempted MMC device.

Fixes e1eb6ada4e38 ("spl: Make image loader infrastructure more universal")
---
  common/spl/spl_mmc.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
common/spl/spl_mmc.c +0 -0
index e1a7d25bd0..22c8a8097c 100644

common/spl/spl_mmc.c int spl_mmc_load(struct spl_image_info *spl_image, · line 378

Quoted 10 lines
common/spl/spl_mmc.c +4 -2
@@ -371,9 +371,11 @@ int spl_mmc_load(struct spl_image_info *spl_image,
  	u32 boot_mode;
  	int err = 0;
  	__maybe_unused int part = 0;
+	int mmc_dev;
 
-	/* Perform peripheral init only once */
-	if (!mmc) {
+	/* Perform peripheral init only once for an mmc device */
+	mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
+	if (!mmc || mmc->block_dev.devnum != mmc_dev) {

block_dev field only exists when CONFIG_BLK is not defined, c.f. https://elixir.bootlin.com/u-boot/latest/source/include/mmc.h#L705

Considering the commit introducing this, c.f. https://source.denx.de/u-boot/u-boot/-/commit/33fb211dd2706e666db4008801dc0d5903fd82f6, I can suggest the following diff (which makes it compile with rk3399-puma_defconfig):

common/spl/spl_mmc.c +8 -1
index e0e1a80536..c40b436a11 100644
@@ -409,10 +409,17 @@ int spl_mmc_load(struct spl_image_info *spl_image,
        int err = 0;
        __maybe_unused int part = 0;
        int mmc_dev;
+       struct blk_desc *block_dev;
 
        /* Perform peripheral init only once for an mmc device */
        mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
-       if (!mmc || mmc->block_dev.devnum != mmc_dev) {
+#if !CONFIG_IS_ENABLED(BLK)
+       block_dev = &mmc->block_dev;
+#else
+       block_dev = dev_get_uclass_plat(mmc->dev);
+#endif
+
+       if (!mmc || block_dev->devnum != mmc_dev) {
                err = spl_mmc_find_device(&mmc, bootdev->boot_device);
                if (err)
                        return err;

Note: Only build tested.

Cheers,
Quentin

#3 lore raw

Hi,

Quoted 72 lines · Quentin Schulz

Hi Harald,

Quoted 26 lines · Harald Seiler

When attempting to load images from multiple MMC devices in sequence, spl_mmc_load() chooses the wrong device from the second attempt onwards.

The reason is that MMC initialization is only done on its first call and spl_mmc_load() will then continue using this same device for all future calls.

Fix this by checking the devnum of the "cached" device struct against the one which is requested. If they match, use the cached one but if they do not match, initialize the new device.

This fixes specifying multiple MMC devices in the SPL's boot order to fall back when U-Boot Proper is corrupted or missing on the first attempted MMC device.

Fixes e1eb6ada4e38 ("spl: Make image loader infrastructure more universal")
---
  common/spl/spl_mmc.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
common/spl/spl_mmc.c +0 -0
index e1a7d25bd0..22c8a8097c 100644

common/spl/spl_mmc.c int spl_mmc_load(struct spl_image_info *spl_image, · line 378

Quoted 10 lines
common/spl/spl_mmc.c +4 -2
@@ -371,9 +371,11 @@ int spl_mmc_load(struct spl_image_info *spl_image,
  	u32 boot_mode;
  	int err = 0;
  	__maybe_unused int part = 0;
+	int mmc_dev;
 
-	/* Perform peripheral init only once */
-	if (!mmc) {
+	/* Perform peripheral init only once for an mmc device */
+	mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
+	if (!mmc || mmc->block_dev.devnum != mmc_dev) {

block_dev field only exists when CONFIG_BLK is not defined, c.f.
https://elixir.bootlin.com/u-boot/latest/source/include/mmc.h#L705

Considering the commit introducing this, c.f.
https://source.denx.de/u-boot/u-boot/-/commit/33fb211dd2706e666db4008801dc0d5903fd82f6,
I can suggest the following diff (which makes it compile with rk3399-puma_defconfig):

common/spl/spl_mmc.c +8 -1
index e0e1a80536..c40b436a11 100644
@@ -409,10 +409,17 @@ int spl_mmc_load(struct spl_image_info *spl_image,
         int err = 0;
         __maybe_unused int part = 0;
         int mmc_dev;
+       struct blk_desc *block_dev;
 
         /* Perform peripheral init only once for an mmc device */
         mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
-       if (!mmc || mmc->block_dev.devnum != mmc_dev) {
+#if !CONFIG_IS_ENABLED(BLK)
+       block_dev = &mmc->block_dev;
+#else
+       block_dev = dev_get_uclass_plat(mmc->dev);
+#endif
+
+       if (!mmc || block_dev->devnum != mmc_dev) {
                 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
                 if (err)
                         return err;
Quoted 1 line

Note: Only build tested.

Thanks for reporting. It's not quite that simple because mmc is NULL on first call to spl_mmc_load(). I'll send a v2 to fix this.

Signature · 1 line
Harald