From 146573739805a4d3cdb4395bf96438f3231fac57 Mon Sep 17 00:00:00 2001
From: Mattijs Korpershoek <mkorpershoek@baylibre.com>
Date: Fri, 7 Jan 2022 16:52:48 +0100
Subject: [PATCH] HACK: fastboot: mmc: switch back to userdata hwpart after

To erase/flash hwpartitions, we first change hwpart from
EMMC_USER (0) to EMMC_BOOT{1,2} by calling raw_part_get_info_by_name().

However, we don't switch back to EMMC_USER at the end of an erase.
Therefore, all other fastboot commands are ran on the EMMC_BOOT{1,2}
hwpart instead of the EMMC_USER hwpart.

For example, the following sequence is broken:

$ fastboot erase bootenv # switch to hwpart 2
$ fastboot reboot bootloader  # attempts to read GPT from hwpart 2

The GPT being stored in EMMC_USER, the above command generates
this error:

>  writing 128 blocks starting at 8064...
>  ........ wrote 65536 bytes to 'bootenv'
>  GUID Partition Table Header signature is wrong: 0xFB4EC30FC5B7E5B2 != 0x5452415020494645
>  find_valid_gpt: *** ERROR: Invalid GPT ***
>  GUID Partition Table Header signature is wrong: 0x0 != 0x5452415020494645
>  find_valid_gpt: *** ERROR: Invalid Backup GPT ***
>  Error: mmc 2:misc read failed (-2)

Fix it by switching back to EMMC_USER at the end of mmc_boot_ops().

Note: this is more visible since commit a362ce214f ("fastboot: Implement generic fastboot_set_reboot_flag")
because "fastboot reboot bootloader" will access the "misc" partition.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
---
 drivers/fastboot/fb_mmc.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/fastboot/fb_mmc.c b/drivers/fastboot/fb_mmc.c
index c62e414306c..ad72c6979a9 100644
--- a/drivers/fastboot/fb_mmc.c
+++ b/drivers/fastboot/fb_mmc.c
@@ -701,6 +701,10 @@ void fastboot_mmc_erase(const char *cmd, char *response)
 		return;
 	}
 
+	if (blk_dselect_hwpart(dev_desc, 0)) {
+		pr_err("Failed to select hwpart\n");
+	}
+
 	printf("........ erased " LBAFU " bytes from '%s'\n",
 	       blks_size * info.blksz, cmd);
 	fastboot_okay(NULL, response);
-- 
GitLab