All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition
@ 2025-12-11 21:00 Ahmad Fatoum
  2025-12-11 21:00 ` [PATCH 2/5] cdev-alias: fix memory leak in diskuuid handling Ahmad Fatoum
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2025-12-11 21:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

This is already the case with other cdev aliases, so support this for
symmetry.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/cdev-alias.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/common/cdev-alias.c b/common/cdev-alias.c
index e5a059ad7f4d..1878553615d2 100644
--- a/common/cdev-alias.c
+++ b/common/cdev-alias.c
@@ -57,9 +57,11 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
 		if (strcasecmp(cdev->diskuuid, uuid))
 			continue;
 
-		cdev = cdev_find_partition(cdev, arg);
-		if (!cdev)
-			return -ENODEV;
+		if (arg) {
+			cdev = cdev_find_partition(cdev, arg);
+			if (!cdev)
+				return -ENODEV;
+		}
 
 		return fn(cdev, data);
 	}
-- 
2.47.3




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/5] cdev-alias: fix memory leak in diskuuid handling
  2025-12-11 21:00 [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Ahmad Fatoum
@ 2025-12-11 21:00 ` Ahmad Fatoum
  2025-12-11 21:00 ` [PATCH 3/5] cdev-alias: add support for storage{.removable,.builtin} Ahmad Fatoum
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2025-12-11 21:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

uuid was duplicated but not free'd in error cases. Fix that.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/cdev-alias.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/common/cdev-alias.c b/common/cdev-alias.c
index 1878553615d2..3732fd90525f 100644
--- a/common/cdev-alias.c
+++ b/common/cdev-alias.c
@@ -43,12 +43,15 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
 				       void *data)
 {
 	struct cdev *cdev;
-	char *arg;
+	char *str, *arg;
+	int ret = 0;
 
-	arg = xstrdup(uuid);
+	arg = str = xstrdup(uuid);
 	uuid = strsep(&arg, ".");
-	if (!uuid || !*uuid)
-		return -EINVAL;
+	if (!uuid || !*uuid) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	for_each_cdev(cdev) {
 		if (cdev_is_partition(cdev))
@@ -59,14 +62,19 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
 
 		if (arg) {
 			cdev = cdev_find_partition(cdev, arg);
-			if (!cdev)
-				return -ENODEV;
+			if (!cdev) {
+				ret = -ENODEV;
+				break;
+			}
 		}
 
-		return fn(cdev, data);
+		ret = fn(cdev, data);
+		break;
 	}
 
-	return 0;
+out:
+	free(str);
+	return ret;
 }
 
 static struct cdev_alias_res cdev_alias_aliases[] = {
-- 
2.47.3




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/5] cdev-alias: add support for storage{.removable,.builtin}
  2025-12-11 21:00 [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Ahmad Fatoum
  2025-12-11 21:00 ` [PATCH 2/5] cdev-alias: fix memory leak in diskuuid handling Ahmad Fatoum
@ 2025-12-11 21:00 ` Ahmad Fatoum
  2025-12-12  8:32   ` Sascha Hauer
  2025-12-11 21:00 ` [PATCH 4/5] boot: try builtin and removable media before net for boot.default Ahmad Fatoum
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: Ahmad Fatoum @ 2025-12-11 21:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Our current of global.boot.default expanding to bootsource doesn't work
when booting from boot-only flash. Improve upon this by adding cdev
aliases for removable and builtin storage devices.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 Documentation/user/booting-linux.rst | 25 ++++++++
 common/cdev-alias.c                  | 86 ++++++++++++++++++++++++++++
 include/driver.h                     |  5 ++
 3 files changed, 116 insertions(+)

diff --git a/Documentation/user/booting-linux.rst b/Documentation/user/booting-linux.rst
index b164c538c62a..f1df5ee2bbf0 100644
--- a/Documentation/user/booting-linux.rst
+++ b/Documentation/user/booting-linux.rst
@@ -182,6 +182,31 @@ and configure the overrides as arguments to the ``boot`` command:
 
   boot -o bootm.image=/mnt/tftp/oftree mmc
 
+Generic Boot Targets
+^^^^^^^^^^^^^^^^^^^^
+
+A single boot target can yield multiple entries, e.g., one for each
+bootloader spec file detected at runtime as described in the next section.
+
+There is also a number of generic default boot targets available, when
+``CONFIG_BOOT_DEFAULTS`` is enabled. These expands to a single device at most:
+
+* ``bootsource``: expands to the device barebox booted from
+* ``diskuuid.*``: expands to the device with specified ``*`` diskuuid
+
+For these targets that expand to a single device, a partition can also be specified,
+e.g., ``bootsource.esp`` to reference the partition with the ``esp`` partition
+label within the bootsource.
+
+Following target can expand to multiple devices:
+
+* ``storage.removable``: expands to built-in storage devices, like USB sticks
+* ``storage.builtin``: expands to built-in storage devices, like eMMC
+* ``storage``: expands to all of the above ``storage.*``
+
+If the bootsource exists within any of these targets, it will be the first
+device in the returned list.
+
 .. _bootloader_spec:
 
 Boot Loader Specification
diff --git a/common/cdev-alias.c b/common/cdev-alias.c
index 3732fd90525f..82b91108c1fc 100644
--- a/common/cdev-alias.c
+++ b/common/cdev-alias.c
@@ -9,7 +9,9 @@
 #include <stringlist.h>
 #include <bootsource.h>
 #include <driver.h>
+#include <block.h>
 #include <init.h>
+#include <linux/bits.h>
 
 struct cdev_alias_res {
 	const char *name;
@@ -77,9 +79,93 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
 	return ret;
 }
 
+#define STORAGE_REMOVABLE	BIT(0)
+#define STORAGE_BUILTIN		BIT(1)
+
+/**
+ * call_for_each_storage() - invoke callback for each storage medium
+ *
+ * @fn:			callback to invoke
+ * @data:		callback-specific data
+ * @filter:		OR-ed types of STORAGE_* to filter for
+ * @only_bootsource:	If true, include only bootsource if available,
+ *			otherwise omit always
+ * Return:		number of successful callback invocations or a negative error
+ */
+static int call_for_each_storage(cdev_alias_processor_t fn,
+				 void *data,
+				 unsigned filter,
+				 bool only_bootsource)
+{
+	struct cdev *cdev, *bootcdev;
+	int ret, nmatches = 0;
+
+	bootcdev = bootsource_of_cdev_find();
+
+	for_each_cdev(cdev) {
+		struct block_device *bdev;
+
+		if (!cdev_is_storage(cdev) || cdev_is_partition(cdev))
+			continue;
+
+		bdev = cdev_get_block_device(cdev);
+
+		if (((filter & STORAGE_REMOVABLE) && bdev && bdev->removable) ||
+		    ((filter & STORAGE_BUILTIN) && (!bdev || !bdev->removable))) {
+			if (only_bootsource && cdev != bootcdev)
+				continue;
+			if (!only_bootsource && cdev == bootcdev)
+				continue;
+
+			ret = fn(cdev, data);
+			if (ret < 0)
+				return ret;
+			nmatches++;
+
+			/* Got our bootsource, no need to continue iteration */
+			if (only_bootsource)
+				break;
+		}
+	}
+
+	return nmatches;
+}
+
+static int cdev_alias_resolve_storage(struct cdev_alias_res *cdev_alias_res,
+				      const char *class,
+				      cdev_alias_processor_t fn,
+				      void *data)
+{
+	struct cdev *bootcdev;
+	unsigned filter = 0;
+	int bootsource, nmatches;
+
+	if (!class)
+		filter = ~0;
+	else if (streq_ptr(class, "removable"))
+		filter |= STORAGE_REMOVABLE;
+	else if (streq_ptr(class, "builtin"))
+		filter |= STORAGE_BUILTIN;
+	else
+		return -EINVAL;
+
+	bootcdev = bootsource_of_cdev_find();
+
+	bootsource = call_for_each_storage(fn, data, filter, true);
+	if (bootsource < 0)
+		return bootsource;
+
+	nmatches = call_for_each_storage(fn, data, filter, false);
+	if (nmatches < 0)
+		return nmatches;
+
+	return bootsource + nmatches;
+}
+
 static struct cdev_alias_res cdev_alias_aliases[] = {
 	{ "bootsource", cdev_alias_resolve_bootsource },
 	{ "diskuuid", cdev_alias_resolve_diskuuid },
+	{ "storage", cdev_alias_resolve_storage },
 	{ /* sentinel */}
 };
 
diff --git a/include/driver.h b/include/driver.h
index a941ca6127e6..de5a63c379eb 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -597,6 +597,11 @@ get_inheritable_devfs_flags(const struct cdev *parent_cdev)
 	return parent_cdev->flags & DEVFS_INHERITABLE_FLAGS;
 }
 
+static inline bool cdev_is_storage(const struct cdev *cdev)
+{
+	return (cdev->flags & DEVFS_IS_BLOCK_DEV) || cdev->mtd;
+}
+
 struct cdev *
 cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid);
 
-- 
2.47.3




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/5] boot: try builtin and removable media before net for boot.default
  2025-12-11 21:00 [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Ahmad Fatoum
  2025-12-11 21:00 ` [PATCH 2/5] cdev-alias: fix memory leak in diskuuid handling Ahmad Fatoum
  2025-12-11 21:00 ` [PATCH 3/5] cdev-alias: add support for storage{.removable,.builtin} Ahmad Fatoum
@ 2025-12-11 21:00 ` Ahmad Fatoum
  2025-12-11 21:00 ` [PATCH 5/5] boot: assign names to bootentry providers Ahmad Fatoum
  2025-12-12  8:36 ` [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Sascha Hauer
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2025-12-11 21:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

This is the same sequence to automatically probe the environment, so it
makes sense that the automatic boot order would follow.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/boot.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/common/boot.c b/common/boot.c
index 07bd288df29d..14525d31ae2d 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -137,6 +137,8 @@ static int init_boot(void)
 	if (!global_boot_default)
 		global_boot_default = xstrdup(
 			IF_ENABLED(CONFIG_BOOT_DEFAULTS, "bootsource ")
+			IF_ENABLED(CONFIG_BOOT_DEFAULTS, "storage.builtin ")
+			IF_ENABLED(CONFIG_BOOT_DEFAULTS, "storage.removable ")
 			"net"
 		);
 
-- 
2.47.3




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 5/5] boot: assign names to bootentry providers
  2025-12-11 21:00 [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2025-12-11 21:00 ` [PATCH 4/5] boot: try builtin and removable media before net for boot.default Ahmad Fatoum
@ 2025-12-11 21:00 ` Ahmad Fatoum
  2025-12-12  8:36 ` [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Sascha Hauer
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2025-12-11 21:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

This allows board- or functionality-specific (like EFI loader support)
to invoke a specific bootentry provider by name.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/boards/protonic-imx6/board.c |  1 +
 common/blspec.c                       |  1 +
 common/boot.c                         | 15 +++++++++++++++
 common/bootchooser.c                  |  1 +
 common/bootdef.c                      |  1 +
 include/boot.h                        |  2 ++
 6 files changed, 21 insertions(+)

diff --git a/arch/arm/boards/protonic-imx6/board.c b/arch/arm/boards/protonic-imx6/board.c
index 80558ce15817..c15a349c8a88 100644
--- a/arch/arm/boards/protonic-imx6/board.c
+++ b/arch/arm/boards/protonic-imx6/board.c
@@ -461,6 +461,7 @@ static int prt_imx6_bootentry_generate(struct bootentries *bootentries,
 }
 
 static struct bootentry_provider prt_imx6_bootentry_provider = {
+	.name = "prt-imx6",
 	.generate = prt_imx6_bootentry_generate,
 };
 
diff --git a/common/blspec.c b/common/blspec.c
index bc2c3204ad62..624e4c115272 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -639,6 +639,7 @@ static int blspec_bootentry_generate(struct bootentries *bootentries,
 }
 
 static struct bootentry_provider blspec_bootentry_provider = {
+	.name = "blspec",
 	.generate = blspec_bootentry_generate,
 };
 
diff --git a/common/boot.c b/common/boot.c
index 14525d31ae2d..1059e6d995d3 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -288,6 +288,21 @@ int bootentry_register_provider(struct bootentry_provider *p)
 	return 0;
 }
 
+struct bootentry_provider *get_bootentry_provider(const char *name)
+{
+	struct bootentry_provider *p;
+
+	if (!name)
+		return NULL;
+
+	list_for_each_entry(p, &bootentry_providers, list) {
+		if (streq_ptr(p->name, name))
+			return p;
+	}
+
+	return NULL;
+}
+
 /*
  * nfs_find_mountpath - Check if a given url is already mounted
  */
diff --git a/common/bootchooser.c b/common/bootchooser.c
index a79eee8ca2c4..2905a0ca677e 100644
--- a/common/bootchooser.c
+++ b/common/bootchooser.c
@@ -962,6 +962,7 @@ static int bootchooser_add_entry(struct bootentries *entries, const char *name)
 }
 
 static struct bootentry_provider bootchooser_entry_provider = {
+	.name = "bootchooser",
 	.generate = bootchooser_add_entry,
 };
 
diff --git a/common/bootdef.c b/common/bootdef.c
index e8140d3a6bce..85471d92d3c5 100644
--- a/common/bootdef.c
+++ b/common/bootdef.c
@@ -31,6 +31,7 @@ static int bootdef_add_entry(struct bootentries *entries, const char *name)
 }
 
 static struct bootentry_provider bootdef_entry_provider = {
+	.name = "bootdef",
 	.generate = bootdef_add_entry,
 };
 
diff --git a/include/boot.h b/include/boot.h
index c1676364cadc..ae32b9ead93f 100644
--- a/include/boot.h
+++ b/include/boot.h
@@ -26,12 +26,14 @@ struct bootentry {
 int bootentries_add_entry(struct bootentries *entries, struct bootentry *entry);
 
 struct bootentry_provider {
+	const char *name;
 	int (*generate)(struct bootentries *bootentries, const char *name);
 	/* internal fields */
 	struct list_head list;
 };
 
 int bootentry_register_provider(struct bootentry_provider *provider);
+struct bootentry_provider *get_bootentry_provider(const char *name);
 
 #define bootentries_for_each_entry(bootentries, entry) \
 	list_for_each_entry(entry, &bootentries->entries, list)
-- 
2.47.3




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/5] cdev-alias: add support for storage{.removable,.builtin}
  2025-12-11 21:00 ` [PATCH 3/5] cdev-alias: add support for storage{.removable,.builtin} Ahmad Fatoum
@ 2025-12-12  8:32   ` Sascha Hauer
  0 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2025-12-12  8:32 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Thu, Dec 11, 2025 at 10:00:45PM +0100, Ahmad Fatoum wrote:
> Our current of global.boot.default expanding to bootsource doesn't work
> when booting from boot-only flash.

I wondered what boot-only flash is. Maybe better "flash containing only
the bootloader"

> +Generic Boot Targets
> +^^^^^^^^^^^^^^^^^^^^
> +
> +A single boot target can yield multiple entries, e.g., one for each
> +bootloader spec file detected at runtime as described in the next section.
> +
> +There is also a number of generic default boot targets available, when
> +``CONFIG_BOOT_DEFAULTS`` is enabled. These expands to a single device at most:
> +
> +* ``bootsource``: expands to the device barebox booted from
> +* ``diskuuid.*``: expands to the device with specified ``*`` diskuuid
> +
> +For these targets that expand to a single device, a partition can also be specified,
> +e.g., ``bootsource.esp`` to reference the partition with the ``esp`` partition
> +label within the bootsource.
> +
> +Following target can expand to multiple devices:
> +
> +* ``storage.removable``: expands to built-in storage devices, like USB sticks

s/built-in/removable/

"like USB sticks or SD cards in slots" to better differentiate against
eMMC.

> +* ``storage.builtin``: expands to built-in storage devices, like eMMC
> +* ``storage``: expands to all of the above ``storage.*``

These are explained in the boot command help text. Please add these new
targets there as well.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition
  2025-12-11 21:00 [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2025-12-11 21:00 ` [PATCH 5/5] boot: assign names to bootentry providers Ahmad Fatoum
@ 2025-12-12  8:36 ` Sascha Hauer
  4 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2025-12-12  8:36 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

Apart from the comments I made, for this series:

Reviewed-by: Sascha Hauer <s.hauer@pengutrinix.de>

On Thu, Dec 11, 2025 at 10:00:43PM +0100, Ahmad Fatoum wrote:
> This is already the case with other cdev aliases, so support this for
> symmetry.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  common/cdev-alias.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/common/cdev-alias.c b/common/cdev-alias.c
> index e5a059ad7f4d..1878553615d2 100644
> --- a/common/cdev-alias.c
> +++ b/common/cdev-alias.c
> @@ -57,9 +57,11 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
>  		if (strcasecmp(cdev->diskuuid, uuid))
>  			continue;
>  
> -		cdev = cdev_find_partition(cdev, arg);
> -		if (!cdev)
> -			return -ENODEV;
> +		if (arg) {
> +			cdev = cdev_find_partition(cdev, arg);
> +			if (!cdev)
> +				return -ENODEV;
> +		}
>  
>  		return fn(cdev, data);
>  	}
> -- 
> 2.47.3
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-12-12  8:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-11 21:00 [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Ahmad Fatoum
2025-12-11 21:00 ` [PATCH 2/5] cdev-alias: fix memory leak in diskuuid handling Ahmad Fatoum
2025-12-11 21:00 ` [PATCH 3/5] cdev-alias: add support for storage{.removable,.builtin} Ahmad Fatoum
2025-12-12  8:32   ` Sascha Hauer
2025-12-11 21:00 ` [PATCH 4/5] boot: try builtin and removable media before net for boot.default Ahmad Fatoum
2025-12-11 21:00 ` [PATCH 5/5] boot: assign names to bootentry providers Ahmad Fatoum
2025-12-12  8:36 ` [PATCH 1/5] cdev-alias: support referencing diskuuid without child partition Sascha Hauer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.