env: mmc: Use correct eMMC HW partition size when calculating end of HW partition
PATCH 1/1 env: mmc: Use correct eMMC HW partition size when calculating end of HW partition
When calculating the end of eMMC HW partition, it is necessary to use the length of that eMMC HW partition, not the length of the eMMC HW USER partition unconditionally, otherwise the end of e.g. eMMC HW BOOT partition will be way past the actual end of that eMMC HW BOOT partition. Add the missing distinction. eMMC HW GP partitions are not supported, so those are not considered here.
---
Cc: Jaehoon Chung <[email protected]>
Cc: Jasper Orschulko <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Michael Heimpold <[email protected]>
Cc: Michael Walle <[email protected]>
Cc: Patrice Chotard <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Peng Fan <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
--- env/mmc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
env/mmc.c +6 -2
@@ -239,8 +239,12 @@ __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)return -ENOENT ; } -if (offset <0 ) - offset += mmc->capacity; +if (offset <0 ) { +if (CONFIG_ENV_MMC_EMMC_HW_PARTITION ) + offset += mmc->capacity_boot; +else + offset += mmc->capacity; + } *env_addr = offset;
Signature · 1 line
2.53.0
Hi Marek,
Quoted 9 lines · Marek Vasut
env: mmc: Use correct eMMC HW partition size when calculating end of HW partition
When calculating the end of eMMC HW partition, it is necessary to use the length of that eMMC HW partition, not the length of the eMMC HW USER partition unconditionally, otherwise the end of e.g. eMMC HW BOOT partition will be way past the actual end of that eMMC HW BOOT partition. Add the missing distinction. eMMC HW GP partitions are not supported, so those are not considered here.
Quoted 6 lines
Signed-off-by Marek Vasut <[email protected]>
env/mmc.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)env: mmc: Use correct eMMC HW partition size when calculating end of HW partition
nit: subject is 84 chars - please trim to <=72, e.g. "env: mmc: Use correct HW partition size for end-relative offset".
env/mmc.c __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) · line 247
Quoted 13 lines
env/mmc.c +6
-2@@ -239,8 +239,12 @@ __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)return -ENOENT ; } -if (offset <0 ) - offset += mmc->capacity; +if (offset <0 ) { +if (CONFIG_ENV_MMC_EMMC_HW_PARTITION ) + offset += mmc->capacity_boot; +else + offset += mmc->capacity; + }
Just to clarify the motivation: every caller of mmc_get_env_addr() goes through init_mmc_for_env(), which calls mmc_set_env_part_init() -> blk_select_hwpart_devnum() -> mmc_switch_part() -> mmc_set_capacity(). After that switch mmc->capacity should already equal mmc->capacity_boot when CONFIG_ENV_MMC_EMMC_HW_PARTITION is 1 or
2. Can you describe the path where mmc->capacity is still capacity_user here? Perhaps the fix belongs instead in the partition-switch path?
env/mmc.c __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) · line 242
Quoted 6 lines
env/mmc.c +4
-0@@ -239,8 +239,12 @@ __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) +if (CONFIG_ENV_MMC_EMMC_HW_PARTITION ) + offset += mmc->capacity_boot; +else + offset += mmc->capacity;
This seems to do the wrong thing for CONFIG_ENV_MMC_EMMC_HW_PARTITION=3 (RPMB) - it picks capacity_boot when capacity_rpmb is meant. The Kconfig is a plain int with no upper bound, so this is reachable. If you want to keep the explicit dispatch, you could key it off mmc_get_blk_desc(mmc)->hwpart so all partition kinds (user/boot/rpmb) fall out correctly. What do you think?
Regards,
Simon
On 5/4/26 2:22 PM, Simon Glass wrote:
Hello Simon,
Quoted 14 lines
env/mmc.c __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) · line 247
Quoted 13 lines
env/mmc.c +6
-2@@ -239,8 +239,12 @@ __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)return -ENOENT ; } -if (offset <0 ) - offset += mmc->capacity; +if (offset <0 ) { +if (CONFIG_ENV_MMC_EMMC_HW_PARTITION ) + offset += mmc->capacity_boot; +else + offset += mmc->capacity; + }
Quoted 8 lines
Just to clarify the motivation: every caller of
mmc_get_env_addr()goes throughinit_mmc_for_env(), which callsmmc_set_env_part_init()->blk_select_hwpart_devnum()->mmc_switch_part()->mmc_set_capacity(). After that switchmmc->capacityshould already equalmmc->capacity_bootwhenCONFIG_ENV_MMC_EMMC_HW_PARTITIONis 1 or
2. Can you describe the path wheremmc->capacityis stillcapacity_userhere? Perhaps the fix belongs instead in the partition-switch path?
The mmc_get_env_addr() is a public function, you can not predict the caller, so it should return the correct offset no matter which eMMC HW partition is selected.
Quoted 9 lines
env/mmc.c __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) · line 242
Quoted 6 lines
env/mmc.c +4
-0@@ -239,8 +239,12 @@ __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) +if (CONFIG_ENV_MMC_EMMC_HW_PARTITION ) + offset += mmc->capacity_boot; +else + offset += mmc->capacity;This seems to do the wrong thing for
CONFIG_ENV_MMC_EMMC_HW_PARTITION=3(RPMB)
I don't think we support env in eMMC RPMB . Also, CONFIG_ENV_MMC_EMMC_HW_PARTITION=3 would be GP0 , not RPMB, which we also do not support.
Quoted 6 lines
- it picks
capacity_boot
whencapacity_rpmbis meant. TheKconfigis a plain int with no upper bound, so this is reachable. If you want to keep the explicit dispatch, you could key it off mmc_get_blk_desc(mmc)->hwpart so all partition kinds (user/boot/rpmb) fall out correctly. What do you think?
I could special-case it this way:
"
if (offset < 0) {
- if (CONFIG_ENV_MMC_EMMC_HW_PARTITION)
+ if (CONFIG_ENV_MMC_EMMC_HW_PARTITION == 1 ||
+ CONFIG_ENV_MMC_EMMC_HW_PARTITION == 2) {
offset += mmc->capacity_boot;- else
+ } else if (CONFIG_ENV_MMC_EMMC_HW_PARTITION >= 3 ||
+ CONFIG_ENV_MMC_EMMC_HW_PARTITION <= 6) {
+ offset += mmc->capacity_gp[CONFIG_ENV_MMC_EMMC_HW_PARTITION - 3];
+ } else {
offset += mmc->capacity;
+ }
}"
[...]