[Concept,02/25] uuid: Refactor v5 GUID generation into BE and LE variants

Message ID 20250903133639.3235920-3-sjg@u-boot.org
State New
Headers
Series Selection of devicetree using CHIDs |

Commit Message

Simon Glass Sept. 3, 2025, 1:36 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Split the v5 GUID generation into separate big-endian and little-endian
functions with shared common code.

The big-endian version is used for CHIDs and standard UUID v5
operations, while the little-endian version seems to be needed for EFI
GUID compatibility.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/u-boot/uuid.h | 10 ++++++++++
 lib/uuid.c            | 30 ++++++++++++++++++++++++------
 2 files changed, 34 insertions(+), 6 deletions(-)
  

Patch

diff --git a/include/u-boot/uuid.h b/include/u-boot/uuid.h
index 124a42da297..940e7a32fe8 100644
--- a/include/u-boot/uuid.h
+++ b/include/u-boot/uuid.h
@@ -146,6 +146,16 @@  void gen_rand_uuid_str(char *uuid_str, int str_format);
 
 struct efi_guid;
 
+/**
+ * gen_v5_guid_be() - generate big-endian v5 GUID from namespace and data
+ *
+ * @namespace:   pointer to UUID namespace salt
+ * @guid:        pointer to allocated GUID output
+ * @...:         NULL terminated list of seed data as pairs of pointers
+ *               to data and their lengths
+ */
+void gen_v5_guid_be(const struct uuid *namespace, struct efi_guid *guid, ...);
+
 /**
  * gen_v5_guid_le() - generate little-endian v5 GUID from namespace and data
  *
diff --git a/lib/uuid.c b/lib/uuid.c
index 4ca36530710..70633f42aa0 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -432,19 +432,16 @@  static void configure_uuid(struct uuid *uuid, unsigned char version)
 	uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
 }
 
-void gen_v5_guid_le(const struct uuid *namespace, struct efi_guid *guid, ...)
+static void gen_v5_guid_common(const struct uuid *namespace,
+			       struct efi_guid *guid, va_list args)
 {
 	sha1_context ctx;
-	va_list args;
 	const uint8_t *data;
-	uint32_t *tmp32;
-	uint16_t *tmp16;
 	uint8_t hash[SHA1_SUM_LEN];
 
 	sha1_starts(&ctx);
 	/* Hash the namespace UUID as salt */
 	sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
-	va_start(args, guid);
 
 	while ((data = va_arg(args, const uint8_t *))) {
 		unsigned int len = va_arg(args, size_t);
@@ -452,13 +449,34 @@  void gen_v5_guid_le(const struct uuid *namespace, struct efi_guid *guid, ...)
 		sha1_update(&ctx, data, len);
 	}
 
-	va_end(args);
 	sha1_finish(&ctx, hash);
 
 	/* Truncate the hash into output UUID, it is already big endian */
 	memcpy(guid, hash, sizeof(*guid));
 
 	configure_uuid((struct uuid *)guid, 5);
+}
+
+void gen_v5_guid_be(const struct uuid *namespace, struct efi_guid *guid, ...)
+{
+	va_list args;
+
+	va_start(args, guid);
+	gen_v5_guid_common(namespace, guid, args);
+	va_end(args);
+
+	/* Keep big endian - no conversion needed */
+}
+
+void gen_v5_guid_le(const struct uuid *namespace, struct efi_guid *guid, ...)
+{
+	va_list args;
+	uint32_t *tmp32;
+	uint16_t *tmp16;
+
+	va_start(args, guid);
+	gen_v5_guid_common(namespace, guid, args);
+	va_end(args);
 
 	/* Make little endian */
 	tmp32 = (uint32_t *)&guid->b[0];