Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nHL6E-0007vT-0x for mharc-grub-devel@gnu.org; Tue, 08 Feb 2022 02:39:43 -0500 Received: from eggs.gnu.org ([209.51.188.92]:56430) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nHL5z-0007pn-Tn for grub-devel@gnu.org; Tue, 08 Feb 2022 02:39:29 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:57290) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nHL5w-00048I-FU for grub-devel@gnu.org; Tue, 08 Feb 2022 02:39:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644305964; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mgc99rkaK8sXWKqe/8GBh1LlJs+t9PV4EF/VHadgT6c=; b=RJp2M2oUFOVHHK0loe2jn8vc6HQdALs0ZiimhMha0txrSQY06Okp3uv/9t3oEllV9TaX3o SGJ9c2Ud1UBvU34r3xxIFObU5d5NWiirtm/XJlTUXXLlVYeYL3R93Ouu3QXxvANh9YbaBe 3ITuakfphGSDFi/eSTKtDopAW2ikn4Y= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-Nr77NsuSPcy1e6yQ40wKXg-1; Tue, 08 Feb 2022 02:39:22 -0500 X-MC-Unique: Nr77NsuSPcy1e6yQ40wKXg-1 Received: by mail-wm1-f71.google.com with SMTP id f26-20020a7bc8da000000b0037bd7f39dbbso236220wml.3 for ; Mon, 07 Feb 2022 23:39:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mgc99rkaK8sXWKqe/8GBh1LlJs+t9PV4EF/VHadgT6c=; b=EV4S3lrUHY4f2Nn5fWvCYGl9AYRu3umDoI76O0wOg3BZwRg1cRo2c06nrv7b3WM74F Dlix5y159QQezXcU/XynovupWhzob7ueVnhvWb3APnO1CsXCHXXpoT7BCtZIvLx9FP9D F7hoMLl5c5DJYxGpurSGmtWtTBLcwanw/WMoQOVK5gp1qQBq2qJSZS0faam8BEUZGI8H 3COYXI3dwBiH5ORhKlSSYLBgTcp1YRVLVlOsR4h1cdMk73gVlVbt1Hyie/okeLwq7MWc QcoOQ41r3pkHcDahbDX8SObf3c6FGRXinx+cy8zhBT0aUHlNsCu7QV3R2/Og6eucgTTt Z+Tw== X-Gm-Message-State: AOAM532Z0DBvpTkm3VsFixVRb2qgkH0yX6/Lf89GaHOSe2T6CK5vf+q6 540R8fko/lpaQZAlSjZ4OV1aWLNzGunojU0HTojl8s06egLHyzs/IgOVmDyoLNgnkvcLmJkWsVt EuDl+NlPIIOGxfZRSCy7mvsSHmbfeE0KLSf1kRq7T070f6sgczVyQGfEf3P0+30E= X-Received: by 2002:adf:a50c:: with SMTP id i12mr1292823wrb.172.1644305961493; Mon, 07 Feb 2022 23:39:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwOv3qxsYaTJZgTqeXUjrquhHZgtXkqQuHhrHWQzW5yrny0LJBm00s7qJ0AfV7yFulEDWcs6w== X-Received: by 2002:adf:a50c:: with SMTP id i12mr1292806wrb.172.1644305961151; Mon, 07 Feb 2022 23:39:21 -0800 (PST) Received: from t590.redhat.com (82-64-217-13.subs.proxad.net. [82.64.217.13]) by smtp.gmail.com with ESMTPSA id e3sm6184136wrr.94.2022.02.07.23.39.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 23:39:20 -0800 (PST) From: =?UTF-8?q?Renaud=20M=C3=A9trich?= To: grub-devel@gnu.org Cc: =?UTF-8?q?Renaud=20M=C3=A9trich?= Subject: [PATCH v3 2/2] search: new --efidisk-only option on EFI systems Date: Tue, 8 Feb 2022 08:39:11 +0100 Message-Id: <20220208073911.82147-2-rmetrich@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220208073911.82147-1-rmetrich@redhat.com> References: <20220208073911.82147-1-rmetrich@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=rmetrich@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.129.124; envelope-from=rmetrich@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Feb 2022 07:39:29 -0000 When using 'search' on EFI systems, we sometimes want to exclude devices that are not EFI disks (e.g. md, lvm). This is typically used when wanting to chainload when having a software raid (md) for EFI partition: with no option, 'search --file /EFI/redhat/shimx64.efi' sets root envvar to 'md/boot_efi' which cannot be used for chainloading since there is no effective EFI device behind. This commit also refactors handling of --no-floppy option. Signed-off-by: Renaud Métrich --- grub-core/commands/search.c | 27 +++++++++++++++++++++++---- grub-core/commands/search_wrap.c | 18 ++++++++++++------ include/grub/search.h | 15 ++++++++++++--- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/grub-core/commands/search.c b/grub-core/commands/search.c index 51656e361..57d26ced8 100644 --- a/grub-core/commands/search.c +++ b/grub-core/commands/search.c @@ -47,7 +47,7 @@ struct search_ctx { const char *key; const char *var; - int no_floppy; + enum search_flags flags; char **hints; unsigned nhints; int count; @@ -62,10 +62,29 @@ iterate_device (const char *name, void *data) int found = 0; /* Skip floppy drives when requested. */ - if (ctx->no_floppy && + if (ctx->flags & SEARCH_FLAGS_NO_FLOPPY && name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9') return 0; + /* Limit to EFI disks when requested. */ + if (ctx->flags & SEARCH_FLAGS_EFIDISK_ONLY) + { + grub_device_t dev; + dev = grub_device_open (name); + if (! dev) + { + grub_errno = GRUB_ERR_NONE; + return 0; + } + if (! dev->disk || dev->disk->dev->id != GRUB_DISK_DEVICE_EFIDISK_ID) + { + grub_device_close (dev); + grub_errno = GRUB_ERR_NONE; + return 0; + } + grub_device_close (dev); + } + #ifdef DO_SEARCH_FS_UUID #define compare_fn grub_strcasecmp #else @@ -261,13 +280,13 @@ try (struct search_ctx *ctx) } void -FUNC_NAME (const char *key, const char *var, int no_floppy, +FUNC_NAME (const char *key, const char *var, enum search_flags flags, char **hints, unsigned nhints) { struct search_ctx ctx = { .key = key, .var = var, - .no_floppy = no_floppy, + .flags = flags, .hints = hints, .nhints = nhints, .count = 0, diff --git a/grub-core/commands/search_wrap.c b/grub-core/commands/search_wrap.c index 47fc8eb99..464e6ebb1 100644 --- a/grub-core/commands/search_wrap.c +++ b/grub-core/commands/search_wrap.c @@ -40,6 +40,7 @@ static const struct grub_arg_option options[] = N_("Set a variable to the first device found."), N_("VARNAME"), ARG_TYPE_STRING}, {"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0}, + {"efidisk-only", 0, 0, N_("Only probe EFI disks."), 0, 0}, {"hint", 'h', GRUB_ARG_OPTION_REPEATABLE, N_("First try the device HINT. If HINT ends in comma, " "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING}, @@ -73,6 +74,7 @@ enum options SEARCH_FS_UUID, SEARCH_SET, SEARCH_NO_FLOPPY, + SEARCH_EFIDISK_ONLY, SEARCH_HINT, SEARCH_HINT_IEEE1275, SEARCH_HINT_BIOS, @@ -89,6 +91,7 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args) const char *id = 0; int i = 0, j = 0, nhints = 0; char **hints = NULL; + enum search_flags flags; if (state[SEARCH_HINT].set) for (i = 0; state[SEARCH_HINT].args[i]; i++) @@ -180,15 +183,18 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args) goto out; } + if (state[SEARCH_NO_FLOPPY].set) + flags |= SEARCH_FLAGS_NO_FLOPPY; + + if (state[SEARCH_EFIDISK_ONLY].set) + flags |= SEARCH_FLAGS_EFIDISK_ONLY; + if (state[SEARCH_LABEL].set) - grub_search_label (id, var, state[SEARCH_NO_FLOPPY].set, - hints, nhints); + grub_search_label (id, var, flags, hints, nhints); else if (state[SEARCH_FS_UUID].set) - grub_search_fs_uuid (id, var, state[SEARCH_NO_FLOPPY].set, - hints, nhints); + grub_search_fs_uuid (id, var, flags, hints, nhints); else if (state[SEARCH_FILE].set) - grub_search_fs_file (id, var, state[SEARCH_NO_FLOPPY].set, - hints, nhints); + grub_search_fs_file (id, var, flags, hints, nhints); else grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type"); diff --git a/include/grub/search.h b/include/grub/search.h index d80347df3..4190aeb2c 100644 --- a/include/grub/search.h +++ b/include/grub/search.h @@ -19,11 +19,20 @@ #ifndef GRUB_SEARCH_HEADER #define GRUB_SEARCH_HEADER 1 -void grub_search_fs_file (const char *key, const char *var, int no_floppy, +enum search_flags + { + SEARCH_FLAGS_NO_FLOPPY = 1, + SEARCH_FLAGS_EFIDISK_ONLY = 2 + }; + +void grub_search_fs_file (const char *key, const char *var, + enum search_flags flags, char **hints, unsigned nhints); -void grub_search_fs_uuid (const char *key, const char *var, int no_floppy, +void grub_search_fs_uuid (const char *key, const char *var, + enum search_flags flags, char **hints, unsigned nhints); -void grub_search_label (const char *key, const char *var, int no_floppy, +void grub_search_label (const char *key, const char *var, + enum search_flags flags, char **hints, unsigned nhints); #endif -- 2.34.1