Skip to contentSkip to conversation

mmc: fsl_esdhc_spl: pre-PBL: implement redundancy support

PATCH In review Pali Rohár 2 participants 4 messages Apr 4, 2022 – Apr 22, 2022 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: fsl_esdhc_spl: pre-PBL: implement redundancy support

QorIQ pre-PBL BootROM scans first 24 SD card sectors (each with fixed 512 bytes length) for boot signature. Implement same redundancy behavior in fsl_esdhc_spl driver to allow loading proper U-Boot when boot sector is not the first one.

---
 drivers/mmc/fsl_esdhc_spl.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/mmc/fsl_esdhc_spl.c +18 -7
index 0146a231b221..ea8f4cd66964 100644
@@ -20,7 +20,6 @@
 #define ESDHC_BOOT_IMAGE_ADDR	0x50
 #define MBRDBR_BOOT_SIG_55	0x1fe
 #define MBRDBR_BOOT_SIG_AA	0x1ff
-#define CONFIG_CFG_DATA_SECTOR	0
 
 
 void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
@@ -62,11 +61,13 @@ void __noreturn mmc_boot(void)
 #ifndef CONFIG_FSL_CORENET
 	uchar *tmp_buf;
 	u32 blklen;
+	u32 blk_off;
 	uchar val;
 #ifndef CONFIG_SPL_FSL_PBL
 	u32 val32;
 #endif
 	uint i, byte_num;
+	u32 sector;
 #endif
 	u32 offset, code_len;
 	struct mmc *mmc;
@@ -86,30 +87,37 @@ void __noreturn mmc_boot(void)
 	offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
 #else
 	blklen = mmc->read_bl_len;
+	if (blklen < 512)
+		blklen = 512;
 	tmp_buf = malloc(blklen);
 	if (!tmp_buf) {
 		puts("spl: malloc memory failed!!\n");
 		hang();
 	}
+
+	sector = 0;
+again:
 	memset(tmp_buf, 0, blklen);
 
 	/*
 	* Read source addr from sd card
 	*/
-	err = mmc->block_dev.block_read(&mmc->block_dev,
-					CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
+	blk_start = (sector * 512) / mmc->read_bl_len;
+	blk_off = (sector * 512) % mmc->read_bl_len;
+	blk_cnt = DIV_ROUND_UP(512,  mmc->read_bl_len);
+	err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, tmp_buf);
 	if (err != 1) {
 		puts("spl: mmc read failed!!\n");
 		hang();
 	}
 
 #ifdef CONFIG_SPL_FSL_PBL
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_55);
 	if (0x55 != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
 	}
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_AA);
 	if (0xAA != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
@@ -123,10 +131,13 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	val32 = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_SIGNATURE_OFF + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_SIGNATURE_OFF + i);
 		val32 = (val32 << 8) + val;
 	}
 	if (val32 != ESDHC_BOOT_SIGNATURE) {
+		/* BOOT signature may be on the first 24 sectors (each being 512 bytes) */
+		if (++sector < 24)
+			goto again;
 		puts("spl: mmc BOOT signature is not valid!!\n");
 		hang();
 	}
@@ -135,7 +146,7 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	offset = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_IMAGE_ADDR + i);
 		offset = (offset << 8) + val;
 	}
 	offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 39 lines · Pali Rohár

QorIQ pre-PBL BootROM scans first 24 SD card sectors (each with fixed 512 bytes length) for boot signature. Implement same redundancy behavior in fsl_esdhc_spl driver to allow loading proper U-Boot when boot sector is not the first one.

---
 drivers/mmc/fsl_esdhc_spl.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/mmc/fsl_esdhc_spl.c +2 -1
index 0146a231b221..ea8f4cd66964 100644
@@ -20,7 +20,6 @@
 #define ESDHC_BOOT_IMAGE_ADDR	0x50
 #define MBRDBR_BOOT_SIG_55	0x1fe
 #define MBRDBR_BOOT_SIG_AA	0x1ff
-#define CONFIG_CFG_DATA_SECTOR	0
 
 
 void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
@@ -62,11 +61,13 @@ void __noreturn mmc_boot(void)
 #ifndef CONFIG_FSL_CORENET
 	uchar *tmp_buf;
 	u32 blklen;
+	u32 blk_off;
 	uchar val;
 #ifndef CONFIG_SPL_FSL_PBL
 	u32 val32;
 #endif
 	uint i, byte_num;
+	u32 sector;
 #endif
 	u32 offset, code_len;
 	struct mmc *mmc;
@@ -86,30 +87,37 @@ void __noreturn mmc_boot(void)
 	offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
 #else

drivers/mmc/fsl_esdhc_spl.c void __noreturn mmc_boot(void) · line 98

Quoted 10 lines
drivers/mmc/fsl_esdhc_spl.c +4 -0
 	blklen = mmc->read_bl_len;
+	if (blklen < 512)
+		blklen = 512;
 	tmp_buf = malloc(blklen);
 	if (!tmp_buf) {
 		puts("spl: malloc memory failed!!\n");
 		hang();
 	}
+
+	sector = 0;

Why does it assign to 0 at here? Doesn't initialize this at above? Does it need to check whether CONFIG_SPL_FSL_PBL enabled or not?

Best Regards,
Jaehoon Chung

Quoted 53 lines
drivers/mmc/fsl_esdhc_spl.c +12 -6
+again:
 	memset(tmp_buf, 0, blklen);
 
 	/*
 	* Read source addr from sd card
 	*/
-	err = mmc->block_dev.block_read(&mmc->block_dev,
-					CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
+	blk_start = (sector * 512) / mmc->read_bl_len;
+	blk_off = (sector * 512) % mmc->read_bl_len;
+	blk_cnt = DIV_ROUND_UP(512,  mmc->read_bl_len);
+	err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, tmp_buf);
 	if (err != 1) {
 		puts("spl: mmc read failed!!\n");
 		hang();
 	}
 
 #ifdef CONFIG_SPL_FSL_PBL
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_55);
 	if (0x55 != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
 	}
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_AA);
 	if (0xAA != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
@@ -123,10 +131,13 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	val32 = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_SIGNATURE_OFF + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_SIGNATURE_OFF + i);
 		val32 = (val32 << 8) + val;
 	}
 	if (val32 != ESDHC_BOOT_SIGNATURE) {
+		/* BOOT signature may be on the first 24 sectors (each being 512 bytes) */
+		if (++sector < 24)
+			goto again;
 		puts("spl: mmc BOOT signature is not valid!!\n");
 		hang();
 	}
@@ -135,7 +146,7 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	offset = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_IMAGE_ADDR + i);
 		offset = (offset << 8) + val;
 	}
 	offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
#2 lore raw
Quoted 44 lines · On Friday 22 April 2022 21:32:54 Jaehoon Chung wrote:
Quoted 43 lines · Pali Rohár

QorIQ pre-PBL BootROM scans first 24 SD card sectors (each with fixed 512 bytes length) for boot signature. Implement same redundancy behavior in fsl_esdhc_spl driver to allow loading proper U-Boot when boot sector is not the first one.

---
 drivers/mmc/fsl_esdhc_spl.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/mmc/fsl_esdhc_spl.c +4 -1
index 0146a231b221..ea8f4cd66964 100644
@@ -20,7 +20,6 @@
 #define ESDHC_BOOT_IMAGE_ADDR	0x50
 #define MBRDBR_BOOT_SIG_55	0x1fe
 #define MBRDBR_BOOT_SIG_AA	0x1ff
-#define CONFIG_CFG_DATA_SECTOR	0
 
 
 void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
@@ -62,11 +61,13 @@ void __noreturn mmc_boot(void)
 #ifndef CONFIG_FSL_CORENET
 	uchar *tmp_buf;
 	u32 blklen;
+	u32 blk_off;
 	uchar val;
 #ifndef CONFIG_SPL_FSL_PBL
 	u32 val32;
 #endif
 	uint i, byte_num;
+	u32 sector;
 #endif
 	u32 offset, code_len;
 	struct mmc *mmc;
@@ -86,30 +87,37 @@ void __noreturn mmc_boot(void)
 	offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
 #else
 	blklen = mmc->read_bl_len;
+	if (blklen < 512)
+		blklen = 512;
 	tmp_buf = malloc(blklen);
Quoted 8 lines

drivers/mmc/fsl_esdhc_spl.c void __noreturn mmc_boot(void) · line 98

Quoted 6 lines
drivers/mmc/fsl_esdhc_spl.c +2 -0
 	if (!tmp_buf) {
 		puts("spl: malloc memory failed!!\n");
 		hang();
 	}
+
+	sector = 0;

Why does it assign to 0 at here? Doesn't initialize this at above?

All variables in this file are initialized after declaration. So I used this same coding style also for newly introduced variables.

Quoted 1 line

Does it need to check whether CONFIG_SPL_FSL_PBL enabled or not?

This redundancy (retry) is implemented only for pre-PBL BootROMs (when CONFIG_SPL_FSL_PBL is not enabled). I do not know how it works in PBL BootROMs, so PBL code scans only sector 0, like before.

Quoted 56 lines

Best Regards,
Jaehoon Chung

Quoted 53 lines
drivers/mmc/fsl_esdhc_spl.c +12 -6
+again:
 	memset(tmp_buf, 0, blklen);
 
 	/*
 	* Read source addr from sd card
 	*/
-	err = mmc->block_dev.block_read(&mmc->block_dev,
-					CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
+	blk_start = (sector * 512) / mmc->read_bl_len;
+	blk_off = (sector * 512) % mmc->read_bl_len;
+	blk_cnt = DIV_ROUND_UP(512,  mmc->read_bl_len);
+	err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, tmp_buf);
 	if (err != 1) {
 		puts("spl: mmc read failed!!\n");
 		hang();
 	}
 
 #ifdef CONFIG_SPL_FSL_PBL
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_55);
 	if (0x55 != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
 	}
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_AA);
 	if (0xAA != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
@@ -123,10 +131,13 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	val32 = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_SIGNATURE_OFF + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_SIGNATURE_OFF + i);
 		val32 = (val32 << 8) + val;
 	}
 	if (val32 != ESDHC_BOOT_SIGNATURE) {
+		/* BOOT signature may be on the first 24 sectors (each being 512 bytes) */
+		if (++sector < 24)
+			goto again;
 		puts("spl: mmc BOOT signature is not valid!!\n");
 		hang();
 	}
@@ -135,7 +146,7 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	offset = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_IMAGE_ADDR + i);
 		offset = (offset << 8) + val;
 	}
 	offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
#3 lore raw
Quoted 52 lines · Pali Rohár
Quoted 43 lines · On Friday 22 April 2022 21:32:54 Jaehoon Chung wrote:
Quoted 42 lines · Pali Rohár

QorIQ pre-PBL BootROM scans first 24 SD card sectors (each with fixed 512 bytes length) for boot signature. Implement same redundancy behavior in fsl_esdhc_spl driver to allow loading proper U-Boot when boot sector is not the first one.

---
 drivers/mmc/fsl_esdhc_spl.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/mmc/fsl_esdhc_spl.c +4 -1
index 0146a231b221..ea8f4cd66964 100644
@@ -20,7 +20,6 @@
 #define ESDHC_BOOT_IMAGE_ADDR	0x50
 #define MBRDBR_BOOT_SIG_55	0x1fe
 #define MBRDBR_BOOT_SIG_AA	0x1ff
-#define CONFIG_CFG_DATA_SECTOR	0
 
 
 void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
@@ -62,11 +61,13 @@ void __noreturn mmc_boot(void)
 #ifndef CONFIG_FSL_CORENET
 	uchar *tmp_buf;
 	u32 blklen;
+	u32 blk_off;
 	uchar val;
 #ifndef CONFIG_SPL_FSL_PBL
 	u32 val32;
 #endif
 	uint i, byte_num;
+	u32 sector;
 #endif
 	u32 offset, code_len;
 	struct mmc *mmc;
@@ -86,30 +87,37 @@ void __noreturn mmc_boot(void)
 	offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
 #else
 	blklen = mmc->read_bl_len;
+	if (blklen < 512)
+		blklen = 512;
Quoted 8 lines

drivers/mmc/fsl_esdhc_spl.c void __noreturn mmc_boot(void) · line 98

Quoted 7 lines
drivers/mmc/fsl_esdhc_spl.c +2 -0
 	tmp_buf = malloc(blklen);
 	if (!tmp_buf) {
 		puts("spl: malloc memory failed!!\n");
 		hang();
 	}
+
+	sector = 0;

Why does it assign to 0 at here? Doesn't initialize this at above?

Quoted 7 lines

All variables in this file are initialized after declaration. So I used this same coding style also for newly introduced variables.

Quoted 1 line

Does it need to check whether CONFIG_SPL_FSL_PBL enabled or not?

This redundancy (retry) is implemented only for pre-PBL BootROMs (when CONFIG_SPL_FSL_PBL is not enabled). I do not know how it works in PBL BootROMs, so PBL code scans only sector 0, like before.

It's my mistake. I didn't see "#else"  for CONFIG_FSL_CORENET. Discard previous my comment.

Beet Regards,

Jaehoon Chung

Quoted 57 lines
Quoted 57 lines

Best Regards,
Jaehoon Chung

Quoted 53 lines
drivers/mmc/fsl_esdhc_spl.c +12 -6
+again:
 	memset(tmp_buf, 0, blklen);
 
 	/*
 	* Read source addr from sd card
 	*/
-	err = mmc->block_dev.block_read(&mmc->block_dev,
-					CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
+	blk_start = (sector * 512) / mmc->read_bl_len;
+	blk_off = (sector * 512) % mmc->read_bl_len;
+	blk_cnt = DIV_ROUND_UP(512,  mmc->read_bl_len);
+	err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, tmp_buf);
 	if (err != 1) {
 		puts("spl: mmc read failed!!\n");
 		hang();
 	}
 
 #ifdef CONFIG_SPL_FSL_PBL
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_55);
 	if (0x55 != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
 	}
-	val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
+	val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_AA);
 	if (0xAA != val) {
 		puts("spl: mmc MBR/DBR signature is not valid!!\n");
 		hang();
@@ -123,10 +131,13 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	val32 = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_SIGNATURE_OFF + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_SIGNATURE_OFF + i);
 		val32 = (val32 << 8) + val;
 	}
 	if (val32 != ESDHC_BOOT_SIGNATURE) {
+		/* BOOT signature may be on the first 24 sectors (each being 512 bytes) */
+		if (++sector < 24)
+			goto again;
 		puts("spl: mmc BOOT signature is not valid!!\n");
 		hang();
 	}
@@ -135,7 +146,7 @@ void __noreturn mmc_boot(void)
 	byte_num = 4;
 	offset = 0;
 	for (i = 0; i < byte_num; i++) {
-		val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
+		val = *(tmp_buf + blk_off + ESDHC_BOOT_IMAGE_ADDR + i);
 		offset = (offset << 8) + val;
 	}
 	offset += CONFIG_SYS_MMC_U_BOOT_OFFS;