From patchwork Sun Nov 8 23:58:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paulo Alcantara X-Patchwork-Id: 1396441 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=cjr.nz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=cjr.nz header.i=@cjr.nz header.a=rsa-sha256 header.s=dkim header.b=U3B28m46; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CTrhc4s7vz9sSf for ; Mon, 9 Nov 2020 10:59:38 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2889482304; Mon, 9 Nov 2020 00:59:30 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=cjr.nz 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; secure) header.d=cjr.nz header.i=@cjr.nz header.b="U3B28m46"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 67AED8234E; Mon, 9 Nov 2020 00:59:28 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mx.cjr.nz (mx.cjr.nz [51.158.111.142]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0B08D822D2 for ; Mon, 9 Nov 2020 00:59:24 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=cjr.nz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pc@cjr.nz Received: from authenticated-user (mx.cjr.nz [51.158.111.142]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: pc) by mx.cjr.nz (Postfix) with ESMTPSA id E5CBF7FCE4; Sun, 8 Nov 2020 23:59:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cjr.nz; s=dkim; t=1604879963; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=qndrxzNqkPrfXCag0eyWKX6RazhxTyEkp5w/Mqlioz0=; b=U3B28m46g4lrEp1L0rQmVDj1rfCO9rLzjvSmPJ5kLuRbAOR2oRw6RqzC5jME/d/P+7FZRX GRth5VdorTSy5zom0pNz/PTGgyncer8kVGs3UW56ehXZD/2cpkn6wr32ww+egEH20n3iaZ 49vWGKNY+YsI1X9ckf9Dv/h5lWqWjRaXwCeTR+2v1K3I6keJKLv0Igu+XW3h1WuTu1l5WL K/ZzNe0+LbVitpHdM9Lx6pRXImLRx63RtDUsGrGfxg8/c2gAiju9C0Zmrz7kITrMTizacA NaIeGflqoElXEQienFUDQQ93MrKNT2PqgMCVwEcXIaogld9S+8c1cKv2drmiWA== From: Paulo Alcantara To: xypron.glpk@gmx.de, agraf@csgraf.de, u-boot@lists.denx.de Cc: Paulo Alcantara Subject: [PATCH] efi_loader: improve detection of ESP for storing UEFI variables Date: Sun, 8 Nov 2020 20:58:55 -0300 Message-Id: <20201108235855.28788-1-pc@cjr.nz> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.3 at phobos.denx.de X-Virus-Status: Clean The UEFI specification does not restrict on the number and location of ESPs in a system. They are discovered as required by looking at the partition type, but firmware implementations are allowed to support ESPs which do not contain a valid partition type. Besides checking for the partition type, for non-removable media check if /EFI directory exists, otherwise check if /EFI/BOOT/BOOT{ARCH}.EFI file exists as specified in UEFI 2.8 "13.3.1.3 Directory Structure". Signed-off-by: Paulo Alcantara (SUSE) --- lib/efi_loader/efi_disk.c | 60 +++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c index 7bd1ccec4501..2940a2edf2e8 100644 --- a/lib/efi_loader/efi_disk.c +++ b/lib/efi_loader/efi_disk.c @@ -335,6 +335,35 @@ static int efi_fs_exists(struct blk_desc *desc, int part) return 1; } +/** + * efi_part_is_esp() - check if a partition is an EFI system partition + * + * @desc: block device descriptor + * @part: partition number + * Return: true if a partition is an EFI system partition + * false otherwise + */ +static bool efi_part_is_esp(struct blk_desc *desc, int part) +{ + int ret; + struct disk_partition info; + + ret = part_get_info(desc, part, &info); + if (ret) + return false; + + if (info.bootable & PART_EFI_SYSTEM_PARTITION) + return true; + + if (fs_set_blk_dev_with_part(desc, part)) + return false; + + if (!desc->removable) + return !!fs_exists("/EFI"); + + return !!fs_exists("/EFI/BOOT/" BOOTEFI_NAME); +} + /** * efi_disk_add_dev() - create a handle for a partition or disk * @@ -436,21 +465,14 @@ static efi_status_t efi_disk_add_dev( *disk = diskobj; /* Store first EFI system partition */ - if (part && !efi_system_partition.if_type) { - int r; - struct disk_partition info; - - r = part_get_info(desc, part, &info); - if (r) - return EFI_DEVICE_ERROR; - if (info.bootable & PART_EFI_SYSTEM_PARTITION) { - efi_system_partition.if_type = desc->if_type; - efi_system_partition.devnum = desc->devnum; - efi_system_partition.part = part; - EFI_PRINT("EFI system partition: %s %d:%d\n", - blk_get_if_type_name(desc->if_type), - desc->devnum, part); - } + if (part && !efi_system_partition.if_type && + efi_part_is_esp(desc, part)) { + efi_system_partition.if_type = desc->if_type; + efi_system_partition.devnum = desc->devnum; + efi_system_partition.part = part; + EFI_PRINT("EFI system partition: %s %d:%d\n", + blk_get_if_type_name(desc->if_type), + desc->devnum, part); } return EFI_SUCCESS; } @@ -614,9 +636,7 @@ bool efi_disk_is_system_part(efi_handle_t handle) { struct efi_handler *handler; struct efi_disk_obj *diskobj; - struct disk_partition info; efi_status_t ret; - int r; /* check if this is a block device */ ret = efi_search_protocol(handle, &efi_block_io_guid, &handler); @@ -625,9 +645,5 @@ bool efi_disk_is_system_part(efi_handle_t handle) diskobj = container_of(handle, struct efi_disk_obj, header); - r = part_get_info(diskobj->desc, diskobj->part, &info); - if (r) - return false; - - return !!(info.bootable & PART_EFI_SYSTEM_PARTITION); + return efi_part_is_esp(diskobj->desc, diskobj->part); }