mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v1 43/54] efi: loader: add ESP boot entry provider
Date: Thu, 18 Dec 2025 11:38:03 +0100	[thread overview]
Message-ID: <20251218111242.1527495-44-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251218111242.1527495-1-a.fatoum@pengutronix.de>

When no Boot variables have been configured, the default per spec
is to look through known media for an EFI payload at the removable
media path (CONFIG_EFI_PAYLOAD_DEFAULT_PATH).

Implement a boot entry provider that does just that.

This is going to be called by an incoming efibootmgr target, so bump the
priority below the existing ones, so it's not taken as the default.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 efi/loader/Makefile  |   1 +
 efi/loader/bootesp.c | 260 +++++++++++++++++++++++++++++++++++++++++++
 include/libfile.h    |   2 +
 lib/libfile.c        |  56 ++++++++++
 4 files changed, 319 insertions(+)
 create mode 100644 efi/loader/bootesp.c

diff --git a/efi/loader/Makefile b/efi/loader/Makefile
index 44ba3a86e4d4..84a8bf1ca229 100644
--- a/efi/loader/Makefile
+++ b/efi/loader/Makefile
@@ -12,3 +12,4 @@ obj-y += setup.o
 obj-y += watchdog.o
 obj-y += pe.o
 obj-y += loadopts.o
+obj-$(CONFIG_BOOT) += bootesp.o
diff --git a/efi/loader/bootesp.c b/efi/loader/bootesp.c
new file mode 100644
index 000000000000..afce5118aa68
--- /dev/null
+++ b/efi/loader/bootesp.c
@@ -0,0 +1,260 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define pr_fmt(fmt)  "efi-loader: bootesp: " fmt
+
+#include <malloc.h>
+#include <fcntl.h>
+#include <libfile.h>
+#include <libbb.h>
+#include <init.h>
+#include <bootm.h>
+#include <driver.h>
+#include <fs.h>
+#include <globalvar.h>
+#include <linux/stat.h>
+#include <linux/list.h>
+#include <linux/err.h>
+#include <spec/dps.h>
+#include <boot.h>
+#include <memory.h>
+#include <firmware.h>
+#include <command.h>
+
+#include <bootscan.h>
+
+struct esp_entry {
+	struct bootentry entry;
+
+	struct cdev *cdev;
+	const char *exepath;
+	const char *rootpath;
+};
+
+/*
+ * esp_boot - boot an entry
+ *
+ * This boots an entry. On success this function does not return.
+ * In case of an error the error code is returned. This function may
+ * return 0 in case of a successful dry run.
+ */
+static int esp_boot(struct bootentry *be, int verbose, int dryrun)
+{
+	struct esp_entry *entry = container_of(be, struct esp_entry, entry);
+	struct bootm_data data = {};
+	int ret;
+
+	bootm_data_init_defaults(&data);
+	data.verbose = max(verbose, data.verbose);
+	data.dryrun = dryrun;
+	data.efi_boot = BOOTM_EFI_REQUIRED;
+
+	data.os_file = strdup_const(entry->exepath);
+
+	/* TODO:
+	 * 1) move firmware/overlay handling into common/bootm.c and then
+	 * 2) implement device tree overlay patching protocol using it?
+	 */
+
+	ret = bootm_boot(&data);
+	if (ret)
+		pr_err("Booting failed\n");
+
+	free_const(data.os_file);
+
+	return ret;
+}
+
+static void esp_entry_free(struct bootentry *be)
+{
+	struct esp_entry *entry = container_of(be, struct esp_entry, entry);
+
+	free_const(entry->exepath);
+	free_const(entry->rootpath);
+	free(entry);
+}
+
+static struct esp_entry *esp_entry_alloc(struct bootentries *bootentries)
+{
+	struct esp_entry *entry;
+
+	entry = xzalloc(sizeof(*entry));
+
+	entry->entry.release = esp_entry_free;
+	entry->entry.boot = esp_boot;
+
+	return entry;
+}
+
+static int __esp_scan_file(struct bootentries *bootentries, const char *root,
+			   const char *exename)
+{
+	char *devname = NULL, *hwdevname = NULL;
+	struct esp_entry *entry;
+
+	pr_debug("%s: %s\n", __func__, root);
+
+	entry = esp_entry_alloc(bootentries);
+	if (IS_ERR(entry))
+		return PTR_ERR(entry);
+
+	root = root ?: get_mounted_path(exename);
+	entry->rootpath = xstrdup_const(root);
+	entry->exepath = xstrdup_const(exename);
+	entry->cdev = get_cdev_by_mountpath(root);
+
+	if (entry->cdev && entry->cdev->dev) {
+		devname = xstrdup(dev_name(entry->cdev->dev));
+		if (entry->cdev->dev->parent)
+			hwdevname = xstrdup(dev_name(entry->cdev->dev->parent));
+	}
+
+	entry->entry.title = xasprintf("EFI payload (%s)", exename);
+	entry->entry.description = basprintf("ESP entry, device: %s hwdevice: %s",
+					    devname ? devname : "none",
+					    hwdevname ? hwdevname : "none");
+	free(devname);
+	free(hwdevname);
+
+	entry->entry.me.type = MENU_ENTRY_NORMAL;
+	entry->entry.release = esp_entry_free;
+
+	bootentries_add_entry(bootentries, &entry->entry);
+	return 1;
+}
+
+static int esp_scan_file(struct bootscanner *scanner,
+			    struct bootentries *bootentries,
+			    const char *exename)
+{
+	u8 magic[2];
+
+	if (read_file_into_buf(exename, &magic, 2) != 2 ||!is_dos_exe(magic))
+		return 0;
+
+	return __esp_scan_file(bootentries, NULL, exename);
+}
+
+/*
+ * esp_scan_directory - scan over a directory
+ *
+ * Given a root path collects all bootentries.
+ *
+ * returns the number of entries found or a negative error value otherwise.
+ */
+static int esp_scan_directory(struct bootscanner *bootscanner,
+				  struct bootentries *bootentries,
+				  const char *root)
+{
+	const char *path = CONFIG_EFI_PAYLOAD_DEFAULT_PATH;
+	char *abspath;
+	int fd, ret, found = 0;
+	struct stat s;
+
+	pr_debug("%s: %s\n", __func__, root);
+
+	fd = open(root, O_DIRECTORY);
+	if (fd < 0)
+		return fd;
+
+	ret = fixup_path_case(fd, &path);
+	if (ret < 0)
+		goto out;
+	fd = ret;
+
+	ret = fstat(fd, &s);
+	if (ret || !S_ISREG(s.st_mode))
+		goto out;
+
+	abspath = xasprintf("%s/%s", root, path);
+
+	ret = __esp_scan_file(bootentries, root, abspath);
+	if (ret > 0)
+		found += ret;
+
+	free(abspath);
+
+	ret = found;
+out:
+	close(fd);
+	return ret;
+}
+
+static int esp_scan_with_flag(struct bootscanner *scanner,
+			      struct bootentries *bootentries, struct cdev *cdev,
+			      unsigned flags)
+{
+	struct cdev *partcdev;
+	int found = 0, err = 0;
+	int ret;
+
+	for_each_cdev_partition(partcdev, cdev) {
+		if ((partcdev->flags & flags) != flags)
+			continue;
+
+		err = -ENOENT;
+
+		if (partcdev->typeflags & DPS_TYPE_FLAG_NO_AUTO) {
+			pr_debug("%s: partition skipped from autodiscovery\n",
+				 partcdev->name);
+			continue;
+		}
+
+		ret = boot_scan_cdev(scanner, bootentries, partcdev, true);
+		if (ret > 0)
+			found += ret;
+	}
+
+	return found ?: err;
+}
+
+/*
+ * esp_scan_disk - scan a disk cdev for ESP
+ *
+ * Given a cdev this functions scans over all child partitions looking
+ * for the ESP.
+ * Returns the number of entries found or a negative error code if some unexpected
+ * error occurred.
+ */
+static int esp_scan_disk(struct bootscanner *scanner,
+			 struct bootentries *bootentries, struct cdev *cdev)
+{
+	int nesp = 0, nlegacy = 0;
+
+	pr_debug("%s: %s\n", __func__, cdev->name);
+
+	nesp = esp_scan_with_flag(scanner, bootentries, cdev,
+				  DEVFS_PARTITION_BOOTABLE_ESP);
+	if (nesp < 0)
+		return nesp;
+
+	nlegacy = esp_scan_with_flag(scanner, bootentries, cdev,
+				     DEVFS_PARTITION_BOOTABLE_LEGACY);
+	if (nlegacy < 0)
+		return nlegacy;
+
+	return nesp + nlegacy;
+}
+
+static struct bootscanner esp_scanner = {
+	.name		= "esp",
+	.scan_file	= esp_scan_file,
+	.scan_directory	= esp_scan_directory,
+	.scan_disk	= esp_scan_disk,
+};
+
+static int esp_bootentry_generate(struct bootentries *bootentries, const char *name)
+{
+	return bootentry_scan_generate(&esp_scanner, bootentries, name);
+}
+
+static struct bootentry_provider esp_bootentry_provider = {
+	.name = "esp",
+	.generate = esp_bootentry_generate,
+	.priority = -50,
+};
+
+static int esp_init(void)
+{
+	return bootentry_register_provider(&esp_bootentry_provider);
+}
+device_initcall(esp_init);
diff --git a/include/libfile.h b/include/libfile.h
index cdcad4b173d0..dc292e422e0b 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -57,4 +57,6 @@ int cache_file(const char *path, char **newpath);
 struct resource *file_to_sdram(const char *filename, unsigned long adr,
 			       enum resource_memtype memtype);
 
+int fixup_path_case(int dirfd, const char **path);
+
 #endif /* __LIBFILE_H */
diff --git a/lib/libfile.c b/lib/libfile.c
index a6690b0b904a..01189773b7e3 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -20,6 +20,7 @@
 #include <libfile.h>
 #include <progress.h>
 #include <stdlib.h>
+#include <string.h>
 #include <linux/stat.h>
 
 /*
@@ -831,3 +832,58 @@ struct resource *file_to_sdram(const char *filename, unsigned long adr,
 
 	return res;
 }
+
+int fixup_path_case(int fd, const char **path)
+{
+	DIR *dir;
+	struct dirent *entry;
+	char *resolved_path, *curr, *next;
+
+	next = resolved_path = xstrdup(*path);
+
+	while ((curr = strsep(&next, "/"))) {
+		size_t nextlen = strlen(curr);
+		char *imatch = NULL;
+
+		dir = fdopendir(fd);
+		if (!dir)
+			goto err;
+
+		while ((entry = readdir(dir)) != NULL) {
+			size_t d_namelen = strlen(entry->d_name);
+
+			if (nextlen != d_namelen)
+				continue;
+
+			if (!strcmp(entry->d_name, curr))
+				goto next_component;
+
+			if (!imatch && !strcasecmp(entry->d_name, curr))
+				imatch = xstrdup(entry->d_name);
+		}
+
+		if (!imatch) {
+			errno = ENOENT;
+			goto err;
+		}
+
+		strncpy(curr, imatch, nextlen);
+		free(imatch);
+
+next_component:
+		fd = openat(fd, curr, next ? O_DIRECTORY : 0);
+		closedir(dir);
+
+		if (fd < 0)
+			goto err;
+
+		if (next)
+			curr[nextlen] = '/';
+	}
+
+	*path = resolved_path;
+	return fd;
+err:
+	free(resolved_path);
+	return -errno;
+}
-- 
2.47.3




  parent reply	other threads:[~2025-12-18 11:41 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-18 10:37 [PATCH v1 00/54] efi: implement EFI loader support in barebox Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 01/54] efi: payload: initrd: fix type mismatch on 32-bit Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 02/54] efi: loader: switch over event/memory key type to efi_uintn_t Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 03/54] lib: vsprintf: print human-readable EFI GUIDs with %pUs Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 04/54] fs: fat: don't duplicate dentries when resolving differently cased paths Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 05/54] efi: loader: add memory accounting Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 06/54] efi: loader: add pool allocator Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 07/54] efi: types: add EFI_RUNTIME_SECTION Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 08/54] resource: assign memory banks a default type and attr Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 09/54] ARM: lds: add EFI runtime service section Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 10/54] ARM: move needed assembly routines into EFI runtime section Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 11/54] crypto: crc32: implement position independent CRC32 Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 12/54] efi: loader: add support for tracing calls back into UEFI Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 13/54] efi: loader: add table utility functions Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 14/54] lib: add charset helpers Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 15/54] efi: loader: add object handling API Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 16/54] efi: loader: add devicepath support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 17/54] efi: loader: add debug support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 18/54] efi: loader: add boot services support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 19/54] efi: loader: add support for runtime services before ExitBootServices Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 20/54] efi: loader: setup root node Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 21/54] efi: loader: add watchdog support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 22/54] efi: loader: move PE implementation out of common code Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 23/54] efi: loader: protocol: add file protocol support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 24/54] efi: loader: protocol: add Block IO support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 25/54] efi: loader: protocol: implement efi_file_from_path Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 26/54] efi: loader: boot: implement LoadImage BootService Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 27/54] efi: loader: add EFI load option handling Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 28/54] efi: loader: protocol: add graphical output protocol support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 29/54] efi: loader: protocol: add console support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 30/54] efi: loader: protocol: add HII support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 31/54] efi: loader: protocol: add unicode collation support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 32/54] efi: loader: protocol: add random number generator protocol Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 33/54] efi: loader: protocol: add device_path_utilities Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 34/54] efi: loader: support formatting only first device path node to text Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 35/54] efi: loader: protocol: add efi_device_path_to_text support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 36/54] restart: allow drivers to register runtime restart handler Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 37/54] poweroff: allow drivers to register runtime poweroff handler Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 38/54] ARM: psci: client: register runtime service " Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 39/54] ARM: psci: client: register runtime service restart handler Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 40/54] hardening: disable some features when EFI runtime support is enabled Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 41/54] filetype: add new filetype for efi-stubbed ARM zImages Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 42/54] bootm: add global.bootm.efi toggle Ahmad Fatoum
2025-12-18 10:38 ` Ahmad Fatoum [this message]
2025-12-18 10:38 ` [PATCH v1 44/54] efi: loader: add rudimentary EFI boot manager Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 45/54] efi: loader: implement bootm handler Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 46/54] efi: runtime: add EFI variable support Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 47/54] efi: loader: populate OsIndicationsSupported/PlatformLang variables Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 48/54] ARM: don't disable MMU when EFI booting Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 49/54] efi: runtime: add runtime service support after ExitBootServices Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 50/54] efi: runtime: add relocation check Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 51/54] efi: loader: CONFIG_EFI_RT_VOLATILE_STORE Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 52/54] efi: loader: support ExitBootServices Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 53/54] efi: loader: pass along SMBIOS table Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 54/54] ARM: configs: add multi_v7/8_efiloader_defconfig Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251218111242.1527495-44-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox