Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C7A39C433FE for ; Mon, 10 Oct 2022 08:05:23 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0F00384E22; Mon, 10 Oct 2022 10:05:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20210112.gappssmtp.com header.i=@baylibre-com.20210112.gappssmtp.com header.b="RAZUxKVF"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 833B684E1B; Mon, 10 Oct 2022 10:05:19 +0200 (CEST) Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6FA1084E2C for ; Mon, 10 Oct 2022 10:05:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mkorpershoek@baylibre.com Received: by mail-wr1-x434.google.com with SMTP id b4so15820066wrs.1 for ; Mon, 10 Oct 2022 01:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=7TleM2JemdWFPxTHGp+YzscPcNynnxRi0GYPG9x4k8Y=; b=RAZUxKVFgzg7DVUGiGgezbDdOytbqUueDDJGfu/nOu3nmXzkSg2I438y/hacnKKdmP s7557zU/gaZcAKoV1siRMBy+NIovFrycB4kjQctdmPWjG8HJXQIYlmdUiNBhlqSx0L+z dIQR0rtRYV2evnLVhDX7n/jFRxruEt29Rgm8PWkLpADMFD3Vdpgwilrte2pbK18w15B2 opVNLNeF29z5HQCtiAdzdtP5gkgHwPOJr8coZKTSp8+1evG4p0yXPh/DAA9Kx4oAXFpj JclHjdMHJVAQfMqPAqQYFMgAnyt4WGxiBYroWuGluxmaVxxTEIQ0ir5+amrk40szkoCm KEVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7TleM2JemdWFPxTHGp+YzscPcNynnxRi0GYPG9x4k8Y=; b=6nN7PPrvmtTE2vfqdJUbEOmVpDMKdmjdffNfiHBWMvgDIz1Esh6tFjPcpb/8GuN3vU 3lWm/2ANOGqyr/cMt4ktCEObc5lKRpt8GgT5nM+gBP7FZTgH2KZ5RlxSBulzh35Zwk3q bPa74mtG6kDBTbWCfcGRp5LRZ6z8LwiQsJ1Tv4eWLbKHrk8Y2XRyPIqauBp9+mtp+eap W3xfXajji12gx7LwNPZc/wPEq4e9JOenae194VvPnzGhiN7Yvzu/YQgCyQ2tevVfAr19 IM9rOFy3BjCjk+kRyJIs9d56fqc1aqS41Z8vdPsnYtHRsSAqf7aDaOF70DVzD56HNbdW 43BA== X-Gm-Message-State: ACrzQf0P0Pd/XJsykDbTrZfH/5/QO1eIOq4pyAJ53px9OiPKr4iqMoew ZGkkz5tDQaxLZ9Q+KtUlmqIN/ivgga1kSWg8 X-Google-Smtp-Source: AMsMyM7L1dB0QVx6M+N4r7sIW4zSBQ3h2sYAt3aGzMm2VQrF23Wg4Wrx0jdyDKC35dYHA4455BCQ6g== X-Received: by 2002:a05:6000:1f04:b0:22e:5e0b:e1fb with SMTP id bv4-20020a0560001f0400b0022e5e0be1fbmr9954700wrb.222.1665389114904; Mon, 10 Oct 2022 01:05:14 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb19:85e6:1900:5824:6e63:e7a6:5af]) by smtp.gmail.com with ESMTPSA id o19-20020a05600c511300b003b95ed78275sm9904685wms.20.2022.10.10.01.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 01:05:14 -0700 (PDT) From: Mattijs Korpershoek Date: Mon, 10 Oct 2022 10:05:05 +0200 Subject: [PATCH] fastboot: mmc: switch to user hwpart after erase/flash MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20221010-switch-hwpart-v1-0-52cbd34c72b7@baylibre.com> To: Patrick Delaunay , Sean Anderson Cc: u-boot@lists.denx.de, Simon Glass , Jaehoon Chung , Matthias Schiffer , Mattijs Korpershoek X-Mailer: b4 0.11.0-dev-5166b X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Before erasing (or flashing) an mmc hardware partition, such as mmc0boot0 or mmc0boot1, we select it using blk_dselect_hwpart(). However, we don't select back the normal (userdata) hwpartition afterwards. For example, the following sequence is broken: $ fastboot erase mmc2boot1 # switch to hwpart 1 $ fastboot reboot bootloader # attempts to read GPT from hwpart 1 > writing 128 blocks starting at 8064... > ........ wrote 65536 bytes to 'mmc0boot1' > 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) The GPT is stored in hwpart0 (userdata), not hwpart1 (mmc0boot1) As the blk documentation states: > Once selected only the region of the device > covered by that partition is accessible. Add a cleanup function, fastboot_mmc_select_default_hwpart() which selects back the default hwpartition (userdata). Note: this is more visible since commit a362ce214f03 ("fastboot: Implement generic fastboot_set_reboot_flag") because "fastboot reboot bootloader" will access the "misc" partition. Signed-off-by: Mattijs Korpershoek --- This fix has been used for over a year in the AOSP tree at [1] [1] https://android.googlesource.com/device/amlogic/yukawa/+/c008ddaec0943adda394b229e83e147f6165773c --- drivers/fastboot/fb_mmc.c | 20 +++++++++++++++++--- include/mmc.h | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/fastboot/fb_mmc.c b/drivers/fastboot/fb_mmc.c index 033c510bc096..dedef7c4deb1 100644 --- a/drivers/fastboot/fb_mmc.c +++ b/drivers/fastboot/fb_mmc.c @@ -199,6 +199,12 @@ static void write_raw_image(struct blk_desc *dev_desc, fastboot_okay(NULL, response); } +static void fastboot_mmc_select_default_hwpart(struct blk_desc *dev_desc) +{ + if (blk_dselect_hwpart(dev_desc, MMC_PART_USERDATA)) + pr_err("Failed to restore hwpart to userdata\n"); +} + #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \ defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT) static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc) @@ -246,7 +252,7 @@ static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer, if (blkcnt > dev_desc->lba) { pr_err("Image size too large\n"); fastboot_fail("Image size too large", response); - return; + goto fail_restore_hwpart; } debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart); @@ -257,7 +263,7 @@ static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer, pr_err("Failed to write EMMC_BOOT%d\n", hwpart); fastboot_fail("Failed to write EMMC_BOOT part", response); - return; + goto fail_restore_hwpart; } printf("........ wrote %lu bytes to EMMC_BOOT%d\n", @@ -267,11 +273,15 @@ static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer, pr_err("Failed to erase EMMC_BOOT%d\n", hwpart); fastboot_fail("Failed to erase EMMC_BOOT part", response); - return; + goto fail_restore_hwpart; } } fastboot_okay(NULL, response); + return; + +fail_restore_hwpart: + fastboot_mmc_select_default_hwpart(dev_desc); } #endif @@ -630,6 +640,8 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer, write_raw_image(dev_desc, &info, cmd, download_buffer, download_bytes, response); } + + fastboot_mmc_select_default_hwpart(dev_desc); } /** @@ -694,6 +706,8 @@ void fastboot_mmc_erase(const char *cmd, char *response) blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL); + fastboot_mmc_select_default_hwpart(dev_desc); + if (blks != blks_size) { pr_err("failed erasing from device %d\n", dev_desc->devnum); fastboot_fail("failed erasing from device", response); diff --git a/include/mmc.h b/include/mmc.h index 027e8bcc73a6..cdd457a60a5b 100644 --- a/include/mmc.h +++ b/include/mmc.h @@ -362,6 +362,7 @@ enum mmc_voltage { /* The number of MMC physical partitions. These consist of: * boot partitions (2), general purpose partitions (4) in MMC v4.4. */ +#define MMC_PART_USERDATA 0 #define MMC_NUM_BOOT_PARTITION 2 #define MMC_PART_RPMB 3 /* RPMB partition number */ --- base-commit: f5717231abad983b4d8f87db612ae60dec86cb1b change-id: 20221010-switch-hwpart-7fed7746db59 Best regards, -- Mattijs Korpershoek