From mboxrd@z Thu Jan 1 00:00:00 1970 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 565CDC433F5 for ; Fri, 22 Apr 2022 15:47:38 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8722083DE4; Fri, 22 Apr 2022 17:47:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=softathome.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=softathome1.onmicrosoft.com header.i=@softathome1.onmicrosoft.com header.b="ozZLuKIH"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 893B683D6D; Fri, 22 Apr 2022 17:47:01 +0200 (CEST) Received: from FRA01-PR2-obe.outbound.protection.outlook.com (mail-pr2fra01on062b.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e18::62b]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C29D083D82 for ; Fri, 22 Apr 2022 17:46:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=softathome.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=philippe.reynes@softathome.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bWOL3zcF+ZX0z5NCnhCqOGWNd4Gyw9hOutG42sdipi2sW5tl8MrAVWCMI9DTl9wI/gWUbG41qC+FrF1llq4hpAsCQtKSJ5PjOTLBqnWHLbpxnUiH1rWINhHoAh64bA1MwbxXyCXwSgpgnThXbA4QDItWTH5pEF7DwReLt9K7aE8IaXKn+1kUaSGJlTklBevqpEJCBV6sh8EjXkEf0QCnXvnG4M7hRjNU1nP8VZtG9HaByOBlxBY0X7O/ipP+prEiH847As2inXhl0Q/2z2ppQsIGZNNbcWaHwSesoE9S3HVKPjjK59aWcrlbb9wt6hnZGjzLr3OsikztoA6MlFCIRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=LPZhyGw/2D6YlS8VJCGOUaw7JXLwGW0GkR9eYr5fOxg=; b=LtltAVVR5ll5E1vyLkXrDcCKImdokwE8epKQkaCXlizziaweCxyM3yk4naqZZ7c9ATF+ARTI97tCFb0Kg0pUOSkYOlCpfHrfmm9w/fegouc+vZ0jeMDK9EtAPjVeJo1Nkais+A/tmk3by3HIFseheY4mYUMlowJK8GJPerwo/zcNtoF6NQcW0gcRnhmSg4xV9DT/ad4XC+WWZJWEEqbM3Tys8aOs3SFTj9A3WTxnhXJP2lnRyIDJkibMr+FwGY2ogT0OBoQhIl2p1X7aMN91V++b5S16a7M7Rwr0Oph03wEqVFm16pJi6mCwtK8sVQkKirMpeIAm8F/gI1uPUEGHOw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.6.166.170) smtp.rcpttodomain=chromium.org smtp.mailfrom=softathome.com; dmarc=bestguesspass action=none header.from=softathome.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=softathome1.onmicrosoft.com; s=selector1-softathome1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LPZhyGw/2D6YlS8VJCGOUaw7JXLwGW0GkR9eYr5fOxg=; b=ozZLuKIHyPMgGLmqttRRb1fwdVtCzBCxPRWyHsAmuKKzA9qAlnq8Yt/rMjBiip8r1WnCEE9Y9FRTeNQfPeDds4xHAJZo8KCX2u6VBPkXmsV1x8Gfl4GDFvwYJ3DZ2xjhFQPk6tnNVICezxYUv4su5fn/Tivtu7ckkm1lwK8fYeblIW4QGsvV7Gsp0Ffw2Af1UVp3CU2i4xi9UYpwL1gprkzwCozJuWm15NawNVlV6RTsZgteowbtL41DcKgq/TC4iEOOdFcImsckRHQCiLjg43V4uzgFPezjJsVhsUHvB8+3Lbr0+X79yeEsIVkPoRMkRQw9pZZ89ep6JP6pjXSVYw== Received: from MR2P264CA0038.FRAP264.PROD.OUTLOOK.COM (2603:10a6:500::26) by MR1P264MB2916.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:39::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.13; Fri, 22 Apr 2022 15:46:53 +0000 Received: from MR2FRA01FT008.eop-fra01.prod.protection.outlook.com (2603:10a6:500:0:cafe::f) by MR2P264CA0038.outlook.office365.com (2603:10a6:500::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.13 via Frontend Transport; Fri, 22 Apr 2022 15:46:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.6.166.170) smtp.mailfrom=softathome.com; dkim=none (message not signed) header.d=none;dmarc=bestguesspass action=none header.from=softathome.com; Received-SPF: Pass (protection.outlook.com: domain of softathome.com designates 149.6.166.170 as permitted sender) receiver=protection.outlook.com; client-ip=149.6.166.170; helo=proxy.softathome.com; Received: from proxy.softathome.com (149.6.166.170) by MR2FRA01FT008.mail.protection.outlook.com (10.152.50.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.14 via Frontend Transport; Fri, 22 Apr 2022 15:46:53 +0000 Received: from sah1lpt571.softathome.com (unknown [192.168.72.32]) by proxy.softathome.com (Postfix) with ESMTPSA id E821520084; Fri, 22 Apr 2022 17:46:52 +0200 (CEST) From: Philippe Reynes To: sjg@chromium.org Cc: u-boot@lists.denx.de, Philippe Reynes Subject: [PATCH 1/3] disk: part_efi: add support to repair gpt table Date: Fri, 22 Apr 2022 17:46:48 +0200 Message-Id: <20220422154650.160435-2-philippe.reynes@softathome.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422154650.160435-1-philippe.reynes@softathome.com> References: <20220422154650.160435-1-philippe.reynes@softathome.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: aea8203b-b29d-449e-8635-08da247752b7 X-MS-TrafficTypeDiagnostic: MR1P264MB2916:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3TL8t10tg8ai/BcSIGz4b2mh1SoL4AdImJjzUZUsYmGlMnerfCptcfWeHvcs99DRnPecCm06yakmzvonRjdaX21Bay78tcvlGx0mfBFu0oxOgmzrNpeHKC/xrf2pqzpAc5lzJ9lU7ceVrL0BoJxVaoK9ik98KOZ43XZs7l7B2H3Jfxn6vDnKF2HCK3CAkO7f64ORQuNEmdhATVMikbEz/XCkfPbXmVPFq04kU8Vun4g6qNKzSY9d5fxHBHpnsgbppFlUFzwT+jJc2LJ7MdXQjnweM73eHSTaUi+RQKFJw2jg4xR0mz4PqBghqCQu9VUanWtytD5q5B+n2osUadyyXW8mp5PZCbwA7pZp9kVDV4b1UPJnWFJGbqc2LYKw+426FSGGnZxOpr9ewHE8BdA64+eXIkWo+2G4JmJjK0N87CFUnP4ocxMuEsv3o5aKgHDEbt0W0KK7l/k2k3NJXHn+4bz95hB6lQgvDYU4FqqjRZHKcjiRIoCN1bqvvP+14XssJHUUet68kvghjQ2ppMvDGbC0PRErGhkJ6efacsjkafZEtxpKpmWNtyfG/GB4wQqqoJNx8AFkfjsOYp3ENVDPPWBKrzo1/e2f849ZABigAHpNQUnv28H5248qs5+mhymn7TDIaiAAT0pedddzQhzbtgBQynguP6EBhpzPD2lXneXFSWB5+uq2cmoaTOZF9LMyME6anUC4DYUYeWoXpDSR1g== X-Forefront-Antispam-Report: CIP:149.6.166.170; CTRY:FR; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:proxy.softathome.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230001)(4636009)(40470700004)(46966006)(36840700001)(7696005)(316002)(81166007)(8676002)(47076005)(426003)(4326008)(83380400001)(6666004)(356005)(40460700003)(82960400001)(8936002)(508600001)(2906002)(86362001)(2616005)(36860700001)(44832011)(5660300002)(6266002)(186003)(26005)(82310400005)(36756003)(1076003)(336012)(6966003)(70586007)(107886003)(70206006)(6916009)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: softathome.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2022 15:46:53.2876 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: aea8203b-b29d-449e-8635-08da247752b7 X-MS-Exchange-CrossTenant-Id: aa10e044-e405-4c10-8353-36b4d0cce511 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=aa10e044-e405-4c10-8353-36b4d0cce511; Ip=[149.6.166.170]; Helo=[proxy.softathome.com] X-MS-Exchange-CrossTenant-AuthSource: MR2FRA01FT008.eop-fra01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MR1P264MB2916 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.5 at phobos.denx.de X-Virus-Status: Clean If a gpt table is corrupted (after a power cut for example), then the gpt table should repaired. The function gpt_repair_headers check if at least one gpt table is valid, and then only write the corrupted gpt table. Signed-off-by: Philippe Reynes --- disk/part_efi.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ include/part.h | 10 ++++++ 2 files changed, 96 insertions(+) diff --git a/disk/part_efi.c b/disk/part_efi.c index f1f3e5bcef..829ccb6bd1 100644 --- a/disk/part_efi.c +++ b/disk/part_efi.c @@ -705,6 +705,92 @@ int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head, return 0; } +static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *dev_desc) +{ + u32 calc_crc32; + u64 val; + + /* recalculate the values for the Primary GPT Header */ + val = le64_to_cpu(gpt_h->my_lba); + gpt_h->my_lba = gpt_h->alternate_lba; + gpt_h->alternate_lba = cpu_to_le64(val); + gpt_h->partition_entry_lba = cpu_to_le64(partition_entries_offset(dev_desc)); + + gpt_h->header_crc32 = 0; + + calc_crc32 = efi_crc32((const unsigned char *)gpt_h, + le32_to_cpu(gpt_h->header_size)); + gpt_h->header_crc32 = cpu_to_le32(calc_crc32); +} + +static int write_one_gpt_table(struct blk_desc *dev_desc, + gpt_header *gpt_h, gpt_entry *gpt_e) +{ + const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries + * sizeof(gpt_entry)), dev_desc); + lbaint_t start; + int ret = 0; + + start = le64_to_cpu(gpt_h->my_lba); + if (blk_dwrite(dev_desc, start, 1, gpt_h) != 1) { + ret = -1; + goto out; + } + + start = le64_to_cpu(gpt_h->partition_entry_lba); + if (blk_dwrite(dev_desc, start, pte_blk_cnt, gpt_e) != pte_blk_cnt) { + ret = -1; + goto out; + } + + out: + return ret; +} + +int gpt_repair_headers(struct blk_desc *dev_desc) +{ + ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, dev_desc->blksz); + ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, dev_desc->blksz); + gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL; + int is_gpt1_valid, is_gpt2_valid; + int ret = -1; + + is_gpt1_valid = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, + gpt_h1, &gpt_e1); + is_gpt2_valid = is_gpt_valid(dev_desc, dev_desc->lba - 1, + gpt_h2, &gpt_e2); + + if (is_gpt1_valid && is_gpt2_valid) { + ret = 0; + goto out; + } + + if (is_gpt1_valid && !is_gpt2_valid) { + prepare_backup_gpt_header(gpt_h1); + ret = write_one_gpt_table(dev_desc, gpt_h1, gpt_e1); + goto out; + } + + if (!is_gpt1_valid && is_gpt2_valid) { + restore_primary_gpt_header(gpt_h2, dev_desc); + ret = write_one_gpt_table(dev_desc, gpt_h2, gpt_e2); + goto out; + } + + if (!is_gpt1_valid && !is_gpt2_valid) { + ret = -1; + goto out; + } + + out: + if (is_gpt1_valid) + free(gpt_e1); + if (is_gpt2_valid) + free(gpt_e2); + + return ret; +} + int gpt_verify_partitions(struct blk_desc *dev_desc, struct disk_partition *partitions, int parts, gpt_header *gpt_head, gpt_entry **gpt_pte) diff --git a/include/part.h b/include/part.h index 53cfbdd876..5b15a99d69 100644 --- a/include/part.h +++ b/include/part.h @@ -433,6 +433,16 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf); int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head, gpt_entry **gpt_pte); +/** + * gpt_repair_headers() - Function to repair the GPT's header + * and partition table entries (PTE) + * + * @param dev_desc - block device descriptor + * + * Return: - '0' on success, otherwise error + */ +int gpt_repair_headers(struct blk_desc *dev_desc); + /** * gpt_verify_partitions() - Function to check if partitions' name, start and * size correspond to '$partitions' env variable -- 2.25.1