From patchwork Thu Sep 25 17:47:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 423 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1758822530; bh=ozpGg2Ecai4jWjk+pEdJiQgC9vTJ63P/6yIBmskzqIE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Gou5sDOBj5cs4l4HQDhN9VvY320yidtrLEpCHALn8GB3AGdvKv0JfpNU6eFknkmt5 HdO9XbU9I6s3mRkIcGmmGUYMQvZ7VS2eIoLledKM5gYEzpGD5GZ4tIij3UZnNCJYkF X6+dSpfBM30O8QmLXavMlYzZcslKtYc9zYd688gjbM1wGu90yD71l6djTvQu1mefLB 7NXcFGH4teElKlJ/4UGzF2dCVkn7XvRHLi5YDx3pf1i8rOCISC6ntmyWYezfhdcnIx uQf4lDRdISZrn8sLwZhPc8yUPYDJqg0oaMSZAtt14FENEFFzpZGEu8rk3wvs9N+AMH 15W3BazP3xALw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7DED467CA2 for ; Thu, 25 Sep 2025 11:48:50 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id twOcS76s10Sa for ; Thu, 25 Sep 2025 11:48:50 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1758822530; bh=ozpGg2Ecai4jWjk+pEdJiQgC9vTJ63P/6yIBmskzqIE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Gou5sDOBj5cs4l4HQDhN9VvY320yidtrLEpCHALn8GB3AGdvKv0JfpNU6eFknkmt5 HdO9XbU9I6s3mRkIcGmmGUYMQvZ7VS2eIoLledKM5gYEzpGD5GZ4tIij3UZnNCJYkF X6+dSpfBM30O8QmLXavMlYzZcslKtYc9zYd688gjbM1wGu90yD71l6djTvQu1mefLB 7NXcFGH4teElKlJ/4UGzF2dCVkn7XvRHLi5YDx3pf1i8rOCISC6ntmyWYezfhdcnIx uQf4lDRdISZrn8sLwZhPc8yUPYDJqg0oaMSZAtt14FENEFFzpZGEu8rk3wvs9N+AMH 15W3BazP3xALw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6D8F867D29 for ; Thu, 25 Sep 2025 11:48:50 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1758822528; bh=aeBwjZRgOfFkRszRjSI2uFsWkhgP4K7wyqxZ43bxElA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jhZWxvzd6VaZ6wSEdNJ3uGkFYkINfhzTl2JkpJSpncp3AubWORQvNBcU+8aSIQ+dZ 0AHkgniMynV1hZpPLXnJ2kiY1+mT0bYWHUFUTp6FBUCJsiAQN0tGnYGiZCchqLKKZf 0BM4omrC721U26q9+TiUtknmi8brfAoFMj/dJf8aOw6aV7LJQr2O2hGsubc60JzT1t wirZKH0Stvw2Wc2RM0cU5xbArqxATOrUGBbTaL90WIsR3PTfr5tHlqSpFPQTejkQUc y51RL1b/JPjXiUvyD6/DCrQnMIJHn9/YRQqllre/IHSxWk4iVE74ujge3bcD7LZ68C QsdAEL2WOBNCA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9757867CA2; Thu, 25 Sep 2025 11:48:48 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id fON4t-vlfuVG; Thu, 25 Sep 2025 11:48:48 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1758822524; bh=PCzkucpILxzVvFU9A3rkC/bp78wJLLyrD8CJVYCMjLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kUtqebKLQGLPzLs3VmVE+UrKaHycorkGWmmqaFyKnBHl26/1odK2903ZQRhqGxxZ6 ygw36hYCuqlQ9DQT14g3mqwAva9cbtNZzpNUX9rjGrQhjd+r8X+TszSt9GtlmfQBVI RfRyB+WThqMke5FgGGB8GJBVCafg7kKMZj79GIl0X69w0RESnfmxMgIzTbRgVTZolO KP8sk5i7WmQ0M/I8YDsozxKDDxSWhgPKKiTr2S7neMHtTnXwclC5acBBSNifo/iVfa X7djNA1o0NQvPWhaTLAeKLrDm6nCYx59K1PYCYZvSsDlINmwHU8t78QyolHUz4uPul nSGood7qjOUjQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 3DFD467D29; Thu, 25 Sep 2025 11:48:44 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Thu, 25 Sep 2025 11:47:35 -0600 Message-ID: <20250925174753.3429102-12-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250925174753.3429102-1-sjg@u-boot.org> References: <20250925174753.3429102-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 34YCIVNINYBIBS3MWB6ZH7ZYHEN5PGBT X-Message-ID-Hash: 34YCIVNINYBIBS3MWB6ZH7ZYHEN5PGBT X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 11/22] cmd: Refactor part_find() into separate functions List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass This function uses #ifdefs and ends up with a very large indent, Split it into two separate functions, one for the EFI app and one for other builds. Drop the use of config.h while we are here. Signed-off-by: Simon Glass --- cmd/part_find.c | 154 +++++++++++++++++++++++++++++------------------- 1 file changed, 94 insertions(+), 60 deletions(-) diff --git a/cmd/part_find.c b/cmd/part_find.c index 68b66569204..f3ce5b417a8 100644 --- a/cmd/part_find.c +++ b/cmd/part_find.c @@ -5,12 +5,10 @@ */ #include -#include #include #include #include #include -#if defined(CONFIG_EFI_CLIENT) || defined(CONFIG_EFI_APP) #include #include @@ -47,21 +45,86 @@ static bool partition_is_on_device(const struct efi_device_path *device, } return false; } -#endif + +/** + * part_self_find() - Check if a device contains the loaded-image path + * + * @udev: Block device to check + * @loaded_image_path: EFI path of the loaded image + * Return 0 if found, -ENOENT if not, other -ve value on error + */ +static int part_self_find(struct udevice *udev, + struct efi_device_path *loaded_image_path) +{ + struct blk_desc *desc = dev_get_uclass_plat(udev); + + if (desc->uclass_id == UCLASS_EFI_MEDIA) { + struct efi_media_plat *plat = dev_get_plat(udev->parent); + u32 loader_part_no; + + if (partition_is_on_device(plat->device_path, loaded_image_path, + &loader_part_no)) { + char env[256]; + int ret; + + ret = snprintf(env, sizeof(env), "%s %x:%x", + blk_get_uclass_name(desc->uclass_id), + desc->devnum, loader_part_no); + if (ret < 0 || ret == sizeof(env)) + return -ENOSPC; + if (env_set("target_part", env)) + return -ENOMEM; + return 0; + } + } + + return -ENOENT; +} + +/** + * part_blk_find() - Check if a device contains a partition with a type uuid + * + * @udev: Block device to check + * @uuid: UUID to search for (in string form) + * Return 0 if found, -ENOENT if not, other -ve value on error + */ +static int part_blk_find(struct udevice *udev, const char *uuid) +{ + struct blk_desc *desc = dev_get_uclass_plat(udev); + int i; + + for (i = 1; i <= MAX_SEARCH_PARTITIONS; i++) { + struct disk_partition info; + int ret; + + ret = part_get_info(desc, i, &info); + if (ret) + break; + if (strcasecmp(uuid, info.type_guid) == 0) { + char env[256]; + + ret = snprintf(env, sizeof(env), "%s %x:%x", + blk_get_uclass_name(desc->uclass_id), + desc->devnum, i); + if (ret < 0 || ret == sizeof(env)) + return -ENOSPC; + debug("Setting target_part to %s\n", env); + if (env_set("target_part", env)) + return -ENOMEM; + return 0; + } + } + + return -ENOENT; +} static int part_find(int argc, char *const argv[]) { -#if defined(CONFIG_EFI_CLIENT) || defined(CONFIG_EFI_APP) efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID; struct efi_device_path *loaded_image_path = NULL; - struct efi_boot_services *boot = efi_get_boot(); - struct efi_priv *priv = efi_get_priv(); bool part_self = false; -#endif struct driver *d = ll_entry_start(struct driver, driver); const int n_ents = ll_entry_count(struct driver, driver); - struct disk_partition info; - struct blk_desc *desc; struct driver *entry; struct udevice *udev; struct uclass *uc; @@ -70,16 +133,20 @@ static int part_find(int argc, char *const argv[]) if (argc != 2) return CMD_RET_USAGE; -#if defined(CONFIG_EFI_CLIENT) || defined(CONFIG_EFI_APP) - part_self = !strncmp(argv[1], "self", 6); - if (part_self) { - ret = boot->handle_protocol(priv->loaded_image->device_handle, - &efi_devpath_guid, - (void **)&loaded_image_path); - if (ret) - log_warning("failed to get device path for loaded image (ret=%d)", ret); + if (IS_ENABLED(CONFIG_EFI_CLIENT)) { + struct efi_boot_services *boot = efi_get_boot(); + struct efi_priv *priv = efi_get_priv(); + + part_self = !strncmp(argv[1], "self", 6); + if (part_self) { + ret = boot->handle_protocol(priv->loaded_image->device_handle, + &efi_devpath_guid, + (void **)&loaded_image_path); + if (ret) + log_warning("failed to get device path for loaded image (ret=%d)", + ret); + } } -#endif ret = uclass_get(UCLASS_BLK, &uc); if (ret) { @@ -90,50 +157,17 @@ static int part_find(int argc, char *const argv[]) if (entry->id != UCLASS_BLK) continue; uclass_foreach_dev(udev, uc) { - int i; - if (udev->driver != entry) continue; - desc = dev_get_uclass_plat(udev); -#if defined(CONFIG_EFI_CLIENT) || defined(CONFIG_EFI_APP) - if (part_self) { - if (desc->if_type == IF_TYPE_EFI_MEDIA) { - struct efi_media_plat *plat = - dev_get_plat(udev->parent); - __u32 loader_part_no; - - if (partition_is_on_device(plat->device_path, - loaded_image_path, - &loader_part_no)) { - char env[256]; - - ret = snprintf(env, sizeof(env), "%s %x:%x", blk_get_if_type_name(desc->if_type), desc->devnum, loader_part_no); - if (ret < 0 || ret == sizeof(env)) - return CMD_RET_FAILURE; - if (env_set("target_part", env)) - return CMD_RET_FAILURE; - return CMD_RET_SUCCESS; - } - } - } else { -#endif - for (i = 1; i <= MAX_SEARCH_PARTITIONS; i++) { - ret = part_get_info(desc, i, &info); - if (ret) - break; - if (strcasecmp(argv[1], info.type_guid) == 0) { - char env[256]; - ret = snprintf(env, sizeof(env), "%s %x:%x", blk_get_uclass_name(desc->uclass_id), desc->devnum, i); - if (ret < 0 || ret == sizeof(env)) - return CMD_RET_FAILURE; - env_set("target_part", env); - debug("Setting target_part to %s\n", env); - return CMD_RET_SUCCESS; - } - } -#if defined(CONFIG_EFI_CLIENT) || defined(CONFIG_EFI_APP) - } -#endif + + if (IS_ENABLED(CONFIG_EFI_CLIENT) && part_self) + ret = part_self_find(udev, loaded_image_path); + else + ret = part_blk_find(udev, argv[1]); + if (!ret) + return 0; + if (ret != -ENOENT) + break; } }