blob: 4cba24ddbad3cf3c59b9e81383a2e0ddf457af24 [file] [log] [blame]
Patrick Williams864cc432023-02-09 14:54:44 -06001From 9fe30f542939824f731fda3991a1d4f66fbf3b4b Mon Sep 17 00:00:00 2001
Andrew Geisslerea144b032023-01-27 16:03:57 -06002From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
3Date: Thu, 4 Aug 2022 16:46:47 +0100
Patrick Williams864cc432023-02-09 14:54:44 -06004Subject: [PATCH 02/27] lib: uuid: introduce uuid_str_to_le_bin function
Andrew Geisslerea144b032023-01-27 16:03:57 -06005
6convert UUID string to little endian binary data
7
8Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
9Cc: Tom Rini <trini@konsulko.com>
10Cc: Simon Glass <sjg@chromium.org>
11Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>
12Cc: Jens Wiklander <jens.wiklander@linaro.org>
13Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20221122131751.22747-1-abdellatif.elkhlifi@arm.com/]
14
15Changelog:
16===============
17
18v8:
19
20* use simple_strtoull() in uuid_str_to_le_bin() to support 32-bit platforms
21
22v7:
23
24* rename be_uuid_str_to_le_bin() to uuid_str_to_le_bin()
25* make uuid_str_to_le_bin() implementation similar to uuid_str_to_bin()
26 by using same APIs
27
28v4:
29
30* rename ffa_uuid_str_to_bin to be_uuid_str_to_le_bin and put in
31 a standalone commit (the current)
32
33v3:
34
35* introduce ffa_uuid_str_to_bin (provided by
36 arm_ffa: introduce Arm FF-A low-level driver)
Patrick Williams864cc432023-02-09 14:54:44 -060037
38Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
Andrew Geisslerea144b032023-01-27 16:03:57 -060039---
40 include/uuid.h | 8 ++++++++
41 lib/uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
42 2 files changed, 54 insertions(+)
43
44diff --git a/include/uuid.h b/include/uuid.h
Patrick Williams864cc432023-02-09 14:54:44 -060045index 4a4883d3b5b6..293a8eb0a579 100644
Andrew Geisslerea144b032023-01-27 16:03:57 -060046--- a/include/uuid.h
47+++ b/include/uuid.h
48@@ -2,6 +2,8 @@
49 /*
50 * Copyright (C) 2014 Samsung Electronics
51 * Przemyslaw Marczak <p.marczak@samsung.com>
52+ * (C) Copyright 2022 ARM Limited
53+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
54 */
55 #ifndef __UUID_H__
56 #define __UUID_H__
57@@ -44,4 +46,10 @@ int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin);
58 const char *uuid_guid_get_str(const unsigned char *guid_bin);
59 void gen_rand_uuid(unsigned char *uuid_bin);
60 void gen_rand_uuid_str(char *uuid_str, int str_format);
61+
62+/**
63+ * uuid_str_to_le_bin - Converts a UUID string to little endian binary data
64+ */
65+int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin);
66+
67 #endif
68diff --git a/lib/uuid.c b/lib/uuid.c
Patrick Williams864cc432023-02-09 14:54:44 -060069index 465e1ac38f57..d29f561a70df 100644
Andrew Geisslerea144b032023-01-27 16:03:57 -060070--- a/lib/uuid.c
71+++ b/lib/uuid.c
72@@ -1,6 +1,8 @@
73 // SPDX-License-Identifier: GPL-2.0+
74 /*
75 * Copyright 2011 Calxeda, Inc.
76+ * (C) Copyright 2022 ARM Limited
77+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
78 */
79
80 #include <common.h>
81@@ -346,6 +348,50 @@ int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
82 return 0;
83 }
84
85+/**
86+ * uuid_str_to_le_bin() - Convert string UUID to little endian binary data.
87+ * @uuid_str: pointer to UUID string
88+ * @uuid_bin: pointer to allocated array for little endian output [16B]
89+ *
90+ * UUID string is 36 characters (36 bytes):
91+ *
92+ * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
93+ *
94+ * where x is a hexadecimal character. Fields are separated by '-'s.
95+ * When converting to a little endian binary UUID, the string fields are reversed.
96+ *
97+ * Return:
98+ *
99+ * uuid_bin filled with little endian UUID data
100+ * On success 0 is returned. Otherwise, failure code.
101+ */
102+int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
103+{
104+ u16 tmp16;
105+ u32 tmp32;
106+ u64 tmp64;
107+
108+ if (!uuid_str_valid(uuid_str) || !uuid_bin)
109+ return -EINVAL;
110+
111+ tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
112+ memcpy(uuid_bin, &tmp32, 4);
113+
114+ tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
115+ memcpy(uuid_bin + 4, &tmp16, 2);
116+
117+ tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
118+ memcpy(uuid_bin + 6, &tmp16, 2);
119+
120+ tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
121+ memcpy(uuid_bin + 8, &tmp16, 2);
122+
123+ tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
124+ memcpy(uuid_bin + 10, &tmp64, 6);
125+
126+ return 0;
127+}
128+
129 /*
130 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
131 *
132--
Patrick Williams864cc432023-02-09 14:54:44 -06001332.39.1
Andrew Geisslerea144b032023-01-27 16:03:57 -0600134