Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pF1bo-0000rG-Nc for mharc-grub-devel@gnu.org; Mon, 09 Jan 2023 18:31:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pF1bg-0000og-Nz for grub-devel@gnu.org; Mon, 09 Jan 2023 18:31:08 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pF1bd-0003Q3-2Q for grub-devel@gnu.org; Mon, 09 Jan 2023 18:31:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673307064; 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=WimLZV5+3L15rq0hfdIRh/qcnkgWX4ss98h+Tsx2r48=; b=FWJnAoQ4B64bo4YY/617GDLENv+B/U6wSEQ1Q5bOmqpCS6F17w1oKB1hx+hdOmMZ4YQBtN ocin9urLSmjcRLSk2fMfySVAOZhDP3TGNBPoaD+84B3DkQy4fFbftabzOf4iznbTl+mNxv lfRKmOm46GNhZBdnQZYnCt6Pa8U59+Q= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-638-FoM5q69CMkmlqQvRBmfkSw-1; Mon, 09 Jan 2023 18:31:01 -0500 X-MC-Unique: FoM5q69CMkmlqQvRBmfkSw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EACCF80D0E3; Mon, 9 Jan 2023 23:31:00 +0000 (UTC) Received: from eesha.redhat.com (unknown [10.22.9.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id BEA6440C2004; Mon, 9 Jan 2023 23:31:00 +0000 (UTC) From: Robbie Harwood To: grub-devel@gnu.org Cc: Javier Martinez Canillas , dkiper@net-space.pl, Robbie Harwood Subject: [PATCH v4 18/19] normal/main: Discover the device to read the config from as a fallback Date: Mon, 9 Jan 2023 18:30:44 -0500 Message-Id: <20230109233045.85477-19-rharwood@redhat.com> In-Reply-To: <20230109233045.85477-1-rharwood@redhat.com> References: <20230109233045.85477-1-rharwood@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Received-SPF: pass client-ip=170.10.133.124; envelope-from=rharwood@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: Mon, 09 Jan 2023 23:31:09 -0000 From: Javier Martinez Canillas When core.img is generated locally, the grub2-probe tool figures out the device and partition that needs to be read to parse the GRUB configuration file. But in some cases the core.img can't be generated on the host and instead has to be done at package build time. In particular, this will be true when it needs to be signed with a key that's only available on the package building infrastructure. In that case, the prefix variable won't have a device and partition but only a directory path. So there's no way for GRUB to know from which device has to read the configuration file. To allow GRUB to continue working on that scenario, fallback to iterating over all the available devices if reading the config failed when using the prefix and fw_path variables. Signed-off-by: Javier Martinez Canillas Co-authored-by: Robbie Harwood Signed-off-by: Robbie Harwood --- grub-core/normal/main.c | 58 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c index 58d3dcee8d..4d727971ea 100644 --- a/grub-core/normal/main.c +++ b/grub-core/normal/main.c @@ -311,18 +311,13 @@ grub_enter_normal_mode (const char *config) } static grub_err_t -grub_try_normal (const char *variable) +grub_try_normal_prefix (const char *prefix) { char *config; - const char *prefix; grub_err_t err = GRUB_ERR_FILE_NOT_FOUND; const char *net_search_cfg; int disable_net_search = 0; - prefix = grub_env_get (variable); - if (!prefix) - return GRUB_ERR_FILE_NOT_FOUND; - net_search_cfg = grub_env_get ("feature_net_search_cfg"); if (net_search_cfg && net_search_cfg[0] == 'n') disable_net_search = 1; @@ -336,7 +331,7 @@ grub_try_normal (const char *variable) config = grub_malloc (config_len); if (! config) - return GRUB_ERR_FILE_NOT_FOUND; + return err; grub_snprintf (config, config_len, "%s/grub.cfg", prefix); err = grub_net_search_config_file (config); @@ -365,6 +360,53 @@ grub_try_normal (const char *variable) return err; } +static int +grub_try_normal_dev (const char *name, void *data) +{ + grub_err_t err; + const char *prefix = grub_xasprintf ("(%s)%s", name, (char *) data); + + if (!prefix) + return 0; + + err = grub_try_normal_prefix (prefix); + if (err == GRUB_ERR_NONE) + return 1; + + return 0; +} + +static grub_err_t +grub_try_normal_discover (void) +{ + const char *prefix = grub_env_get ("prefix"); + grub_err_t err = GRUB_ERR_FILE_NOT_FOUND; + + if (!prefix) + return err; + + if (grub_device_iterate (grub_try_normal_dev, (void *) prefix)) + return GRUB_ERR_NONE; + + return err; +} + +static grub_err_t +grub_try_normal (const char *variable) +{ + grub_err_t err = GRUB_ERR_FILE_NOT_FOUND; + const char *prefix; + + if (!variable) + return err; + + prefix = grub_env_get (variable); + if (!prefix) + return err; + + return grub_try_normal_prefix (prefix); +} + /* Enter normal mode from rescue mode. */ static grub_err_t grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)), @@ -379,6 +421,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)), err = grub_try_normal ("fw_path"); if (err == GRUB_ERR_FILE_NOT_FOUND) err = grub_try_normal ("prefix"); + if (err == GRUB_ERR_FILE_NOT_FOUND) + err = grub_try_normal_discover (); if (err == GRUB_ERR_FILE_NOT_FOUND) grub_enter_normal_mode (0); } -- 2.39.0