[PATCH v1 34/54] efi: loader: support formatting only first device path node to text

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Dec 18 02:37:54 PST 2025


For use in the follow-up efi_device_path_to_text, teach
device_path_to_str_buf() to support formatting the first node only.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/efi_handle_dump.c |  2 +-
 drivers/efi/efi-device.c   |  2 +-
 efi/devicepath.c           | 17 +++++++++++------
 efi/loader/devicepath.c    |  4 ++--
 efi/payload/init.c         |  2 +-
 include/efi/devicepath.h   |  5 +++--
 lib/vsprintf.c             |  2 +-
 7 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/commands/efi_handle_dump.c b/commands/efi_handle_dump.c
index c5c6b6bbc3bc..183a7d4bc3a6 100644
--- a/commands/efi_handle_dump.c
+++ b/commands/efi_handle_dump.c
@@ -28,7 +28,7 @@ static void efi_devpath(struct efi_boot_services *bs,
 	if (EFI_ERROR(efiret))
 		return;
 
-	dev_path_str = device_path_to_str(devpath);
+	dev_path_str = device_path_to_str(devpath, true);
 	if (dev_path_str) {
 		printf("  %s: \n  %s\n", desc, dev_path_str);
 		free(dev_path_str);
diff --git a/drivers/efi/efi-device.c b/drivers/efi/efi-device.c
index 6bf43cebea8d..490faff6f53d 100644
--- a/drivers/efi/efi-device.c
+++ b/drivers/efi/efi-device.c
@@ -196,7 +196,7 @@ static int efi_register_device(struct efi_device *efidev)
 	if (ret)
 		return ret;
 
-	dev_path_str = device_path_to_str(efidev->devpath);
+	dev_path_str = device_path_to_str(efidev->devpath, true);
 	if (dev_path_str) {
 		dev_add_param_fixed(&efidev->dev, "devpath", "%s", dev_path_str);
 		free(dev_path_str);
diff --git a/efi/devicepath.c b/efi/devicepath.c
index e4170d701e47..8298073572ae 100644
--- a/efi/devicepath.c
+++ b/efi/devicepath.c
@@ -674,7 +674,7 @@ struct {
 };
 
 static void __device_path_to_str(struct string *str,
-				 const struct efi_device_path *dev_path)
+				 const struct efi_device_path *dev_path, bool all_nodes)
 {
 	const struct efi_device_path *dev_path_node;
 	void (*dump_node) (struct string *, const void *);
@@ -702,6 +702,9 @@ static void __device_path_to_str(struct string *str,
 
 		dump_node(str, dev_path_node);
 
+		if (!all_nodes)
+			return;
+
 		dev_path_node = next_device_path_node(dev_path_node);
 	}
 }
@@ -712,19 +715,20 @@ static void __device_path_to_str(struct string *str,
  * @dev_path:	The EFI device path to format
  * @buf:	The buffer to format into or optionally NULL if @len is zero
  * @len:	The number of bytes that may be written into @buf
+ * @all_nodes:  Whether to format the whole path or only the first node
  * Return:	total number of bytes that are required to store the formatted
  *		result, excluding the terminating NUL byte, which is always
  *		written.
  */
 size_t device_path_to_str_buf(const struct efi_device_path *dev_path,
-			      char *buf, size_t len)
+			      char *buf, size_t len, bool all_nodes)
 {
 	struct string str = {
 		.str = buf,
 		.allocated = len,
 	};
 
-	__device_path_to_str(&str, dev_path);
+	__device_path_to_str(&str, dev_path, all_nodes);
 
 	return str.used;
 }
@@ -733,20 +737,21 @@ size_t device_path_to_str_buf(const struct efi_device_path *dev_path,
  * device_path_to_str() - formats a device path into a newly allocated buffer
  *
  * @dev_path:	The EFI device path to format
+ * @all_nodes:  Whether to format the whole path or only the first node
  * Return:	A pointer to the nul-terminated formatted device path.
  */
-char *device_path_to_str(const struct efi_device_path *dev_path)
+char *device_path_to_str(const struct efi_device_path *dev_path, bool all_nodes)
 {
 	void *buf;
 	size_t size;
 
-	size = device_path_to_str_buf(dev_path, NULL, 0);
+	size = device_path_to_str_buf(dev_path, NULL, 0, all_nodes);
 
 	buf = malloc(size + 1);
 	if (!buf)
 		return NULL;
 
-	device_path_to_str_buf(dev_path, buf, size + 1);
+	device_path_to_str_buf(dev_path, buf, size + 1, all_nodes);
 
 	return buf;
 }
diff --git a/efi/loader/devicepath.c b/efi/loader/devicepath.c
index cd783477ef3e..17c659e4c5d8 100644
--- a/efi/loader/devicepath.c
+++ b/efi/loader/devicepath.c
@@ -1050,7 +1050,7 @@ struct efi_device_path *efi_dp_from_file(int dirfd, const char *path)
 /**
  * efi_dp_from_file_tostr() - format barebox VFS path as EFI device path string
  *
- * Convert any barebox virtual file system path to an UEFI device path
+ * Convert any barebox virtual file system path to a full UEFI device path
  * and then return its full string representation.
  *
  * @dirfd:	directory fd
@@ -1065,7 +1065,7 @@ char *efi_dp_from_file_tostr(int dirfd, const char *path)
 
 	dp = efi_dp_from_file(dirfd, path);
 	if (dp)
-		dpstr = device_path_to_str(dp);
+		dpstr = device_path_to_str(dp, true);
 
 	efi_free_pool(dp);
 
diff --git a/efi/payload/init.c b/efi/payload/init.c
index 7e361bab8ab3..d8a4410f22e6 100644
--- a/efi/payload/init.c
+++ b/efi/payload/init.c
@@ -94,7 +94,7 @@ static struct efi_boot *efi_get_boot(int num)
 
 	boot->path = memdup(ptr, boot->file_path_len);
 
-	printf("path: %s\n", device_path_to_str(boot->path));
+	printf("path: %s\n", device_path_to_str(boot->path, true));
 
 	return boot;
 }
diff --git a/include/efi/devicepath.h b/include/efi/devicepath.h
index 405f4446310e..125f114bd846 100644
--- a/include/efi/devicepath.h
+++ b/include/efi/devicepath.h
@@ -397,8 +397,9 @@ struct efi_device_path_bbs_bbs {
 #define DEVICE_PATH_END_LENGTH			(sizeof(struct efi_device_path))
 
 const struct efi_device_path *device_path_from_handle(efi_handle_t handle);
-char *device_path_to_str(const struct efi_device_path *dev_path);
-size_t device_path_to_str_buf(const struct efi_device_path *dev_path, char buf[], size_t size);
+char *device_path_to_str(const struct efi_device_path *dev_path, bool all_nodes);
+size_t device_path_to_str_buf(const struct efi_device_path *dev_path,
+			      char buf[], size_t size, bool all_nodes);
 u8 device_path_to_type(const struct efi_device_path *dev_path);
 u8 device_path_to_subtype(const struct efi_device_path *dev_path);
 char *device_path_to_partuuid(const struct efi_device_path *dev_path);
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 5ef4a075d0f6..6c9dae467496 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -395,7 +395,7 @@ static char *device_path_string(char *buf, const char *end, const struct efi_dev
 	if (!dp)
 		return string(buf, end, NULL, field_width, precision, flags);
 
-	return buf + device_path_to_str_buf(dp, buf, end - buf);
+	return buf + device_path_to_str_buf(dp, buf, end - buf, true);
 }
 
 static noinline_for_stack
-- 
2.47.3




More information about the barebox mailing list