Provide a function to append a device_path to a list of device paths
that is separated by final end nodes.

Signed-off-by: Heinrich Schuchardt <heinrich.schucha...@canonical.com>
---
 include/efi_loader.h             |  3 +++
 lib/efi_loader/efi_device_path.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 9600941aa32..a7d7b8324f1 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -944,6 +944,9 @@ struct efi_load_option {
 
 struct efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
                                       const efi_guid_t *guid);
+struct efi_device_path *efi_dp_merge(const struct efi_device_path *dp1,
+                                    efi_uintn_t *size,
+                                    const struct efi_device_path *dp2);
 struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
                                      const struct efi_device_path *dp2,
                                      bool split_end_node);
diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c
index 46aa59b9e40..16cbe41d32f 100644
--- a/lib/efi_loader/efi_device_path.c
+++ b/lib/efi_loader/efi_device_path.c
@@ -270,6 +270,37 @@ struct efi_device_path *efi_dp_dup(const struct 
efi_device_path *dp)
        return ndp;
 }
 
+/**
+ * efi_dp_merge() - Concatenate two device paths separated by final end node
+ *
+ * @dp1:       first device path
+ * @size:      pointer to length of @dp1, total size on return
+ * @dp2:       second device path
+ *
+ * Return:     concatenated device path or NULL
+ */
+struct efi_device_path *efi_dp_merge(const struct efi_device_path *dp1,
+                                    efi_uintn_t *size,
+                                    const struct efi_device_path *dp2)
+{
+       efi_uintn_t len, len1, len2;
+       struct efi_device_path *dp;
+       u8 *p;
+
+       len1 = *size;
+       len2 = efi_dp_size(dp2) + sizeof(END);
+       len = len1 + len2;
+       dp = efi_alloc(len);
+       if (!dp)
+               return NULL;
+       memcpy(dp, dp1, len1);
+       p = (u8 *)dp + len1;
+       memcpy(p, dp2, len2);
+       *size = len;
+
+       return dp;
+}
+
 /**
  * efi_dp_concat() - Concatenate two device paths and add and terminate them
  *                   with an end node.
-- 
2.43.0

Reply via email to