common: Drop uuid.h from common header
[oweals/u-boot.git] / lib / uuid.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2011 Calxeda, Inc.
4  */
5
6 #include <common.h>
7 #include <env.h>
8 #include <time.h>
9 #include <uuid.h>
10 #include <linux/ctype.h>
11 #include <errno.h>
12 #include <common.h>
13 #include <asm/io.h>
14 #include <part_efi.h>
15 #include <malloc.h>
16
17 /*
18  * UUID - Universally Unique IDentifier - 128 bits unique number.
19  *        There are 5 versions and one variant of UUID defined by RFC4122
20  *        specification. A UUID contains a set of fields. The set varies
21  *        depending on the version of the UUID, as shown below:
22  *        - time, MAC address(v1),
23  *        - user ID(v2),
24  *        - MD5 of name or URL(v3),
25  *        - random data(v4),
26  *        - SHA-1 of name or URL(v5),
27  *
28  * Layout of UUID:
29  * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
30  * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
31  * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
32  * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
33  * node      - 48 bit
34  *
35  * source: https://www.ietf.org/rfc/rfc4122.txt
36  *
37  * UUID binary format (16 bytes):
38  *
39  * 4B-2B-2B-2B-6B (big endian - network byte order)
40  *
41  * UUID string is 36 length of characters (36 bytes):
42  *
43  * 0        9    14   19   24
44  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
45  *    be     be   be   be       be
46  *
47  * where x is a hexadecimal character. Fields are separated by '-'s.
48  * When converting to a binary UUID, le means the field should be converted
49  * to little endian and be means it should be converted to big endian.
50  *
51  * UUID is also used as GUID (Globally Unique Identifier) with the same binary
52  * format but it differs in string format like below.
53  *
54  * GUID:
55  * 0        9    14   19   24
56  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
57  *    le     le   le   be       be
58  *
59  * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
60  */
61 int uuid_str_valid(const char *uuid)
62 {
63         int i, valid;
64
65         if (uuid == NULL)
66                 return 0;
67
68         for (i = 0, valid = 1; uuid[i] && valid; i++) {
69                 switch (i) {
70                 case 8: case 13: case 18: case 23:
71                         valid = (uuid[i] == '-');
72                         break;
73                 default:
74                         valid = isxdigit(uuid[i]);
75                         break;
76                 }
77         }
78
79         if (i != UUID_STR_LEN || !valid)
80                 return 0;
81
82         return 1;
83 }
84
85 #ifdef CONFIG_PARTITION_TYPE_GUID
86 static const struct {
87         const char *string;
88         efi_guid_t guid;
89 } list_guid[] = {
90         {"system",      PARTITION_SYSTEM_GUID},
91         {"mbr",         LEGACY_MBR_PARTITION_GUID},
92         {"msft",        PARTITION_MSFT_RESERVED_GUID},
93         {"data",        PARTITION_BASIC_DATA_GUID},
94         {"linux",       PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
95         {"raid",        PARTITION_LINUX_RAID_GUID},
96         {"swap",        PARTITION_LINUX_SWAP_GUID},
97         {"lvm",         PARTITION_LINUX_LVM_GUID}
98 };
99
100 /*
101  * uuid_guid_get_bin() - this function get GUID bin for string
102  *
103  * @param guid_str - pointer to partition type string
104  * @param guid_bin - pointer to allocated array for big endian output [16B]
105  */
106 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
107 {
108         int i;
109
110         for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
111                 if (!strcmp(list_guid[i].string, guid_str)) {
112                         memcpy(guid_bin, &list_guid[i].guid, 16);
113                         return 0;
114                 }
115         }
116         return -ENODEV;
117 }
118
119 /*
120  * uuid_guid_get_str() - this function get string for GUID.
121  *
122  * @param guid_bin - pointer to string with partition type guid [16B]
123  * @param guid_str - pointer to allocated partition type string [7B]
124  */
125 int uuid_guid_get_str(const unsigned char *guid_bin, char *guid_str)
126 {
127         int i;
128
129         *guid_str = 0;
130         for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
131                 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
132                         strcpy(guid_str, list_guid[i].string);
133                         return 0;
134                 }
135         }
136         return -ENODEV;
137 }
138 #endif
139
140 /*
141  * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
142  *
143  * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
144  * @param uuid_bin - pointer to allocated array for big endian output [16B]
145  * @str_format     - UUID string format: 0 - UUID; 1 - GUID
146  */
147 int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
148                     int str_format)
149 {
150         uint16_t tmp16;
151         uint32_t tmp32;
152         uint64_t tmp64;
153
154         if (!uuid_str_valid(uuid_str)) {
155 #ifdef CONFIG_PARTITION_TYPE_GUID
156                 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
157                         return 0;
158 #endif
159                 return -EINVAL;
160         }
161
162         if (str_format == UUID_STR_FORMAT_STD) {
163                 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
164                 memcpy(uuid_bin, &tmp32, 4);
165
166                 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
167                 memcpy(uuid_bin + 4, &tmp16, 2);
168
169                 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
170                 memcpy(uuid_bin + 6, &tmp16, 2);
171         } else {
172                 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
173                 memcpy(uuid_bin, &tmp32, 4);
174
175                 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
176                 memcpy(uuid_bin + 4, &tmp16, 2);
177
178                 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
179                 memcpy(uuid_bin + 6, &tmp16, 2);
180         }
181
182         tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
183         memcpy(uuid_bin + 8, &tmp16, 2);
184
185         tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
186         memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
187
188         return 0;
189 }
190
191 /*
192  * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
193  *
194  * @param uuid_bin:     pointer to binary data of UUID (big endian) [16B]
195  * @param uuid_str:     pointer to allocated array for output string [37B]
196  * @str_format:         bit 0: 0 - UUID; 1 - GUID
197  *                      bit 1: 0 - lower case; 2 - upper case
198  */
199 void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
200                      int str_format)
201 {
202         const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
203                                                   9, 10, 11, 12, 13, 14, 15};
204         const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
205                                                   9, 10, 11, 12, 13, 14, 15};
206         const u8 *char_order;
207         const char *format;
208         int i;
209
210         /*
211          * UUID and GUID bin data - always in big endian:
212          * 4B-2B-2B-2B-6B
213          * be be be be be
214          */
215         if (str_format & UUID_STR_FORMAT_GUID)
216                 char_order = guid_char_order;
217         else
218                 char_order = uuid_char_order;
219         if (str_format & UUID_STR_UPPER_CASE)
220                 format = "%02X";
221         else
222                 format = "%02x";
223
224         for (i = 0; i < 16; i++) {
225                 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
226                 uuid_str += 2;
227                 switch (i) {
228                 case 3:
229                 case 5:
230                 case 7:
231                 case 9:
232                         *uuid_str++ = '-';
233                         break;
234                 }
235         }
236 }
237
238 /*
239  * gen_rand_uuid() - this function generates a random binary UUID version 4.
240  *                   In this version all fields beside 4 bits of version and
241  *                   2 bits of variant are randomly generated.
242  *
243  * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
244 */
245 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
246 void gen_rand_uuid(unsigned char *uuid_bin)
247 {
248         u32 ptr[4];
249         struct uuid *uuid = (struct uuid *)ptr;
250         int i;
251
252         srand(get_ticks() + rand());
253
254         /* Set all fields randomly */
255         for (i = 0; i < 4; i++)
256                 ptr[i] = rand();
257
258         clrsetbits_be16(&uuid->time_hi_and_version,
259                         UUID_VERSION_MASK,
260                         UUID_VERSION << UUID_VERSION_SHIFT);
261
262         clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
263                      UUID_VARIANT_MASK,
264                      UUID_VARIANT << UUID_VARIANT_SHIFT);
265
266         memcpy(uuid_bin, uuid, 16);
267 }
268
269 /*
270  * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
271  *                       formats UUID or GUID.
272  *
273  * @param uuid_str - pointer to allocated array [37B].
274  * @param          - uuid output type: UUID - 0, GUID - 1
275  */
276 void gen_rand_uuid_str(char *uuid_str, int str_format)
277 {
278         unsigned char uuid_bin[UUID_BIN_LEN];
279
280         /* Generate UUID (big endian) */
281         gen_rand_uuid(uuid_bin);
282
283         /* Convert UUID bin to UUID or GUID formated STRING  */
284         uuid_bin_to_str(uuid_bin, uuid_str, str_format);
285 }
286
287 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
288 int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
289 {
290         char uuid[UUID_STR_LEN + 1];
291         int str_format;
292
293         if (!strcmp(argv[0], "uuid"))
294                 str_format = UUID_STR_FORMAT_STD;
295         else
296                 str_format = UUID_STR_FORMAT_GUID;
297
298         if (argc > 2)
299                 return CMD_RET_USAGE;
300
301         gen_rand_uuid_str(uuid, str_format);
302
303         if (argc == 1)
304                 printf("%s\n", uuid);
305         else
306                 env_set(argv[1], uuid);
307
308         return CMD_RET_SUCCESS;
309 }
310
311 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
312            "UUID - generate random Universally Unique Identifier",
313            "[<varname>]\n"
314            "Argument:\n"
315            "varname: for set result in a environment variable\n"
316            "e.g. uuid uuid_env"
317 );
318
319 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
320            "GUID - generate Globally Unique Identifier based on random UUID",
321            "[<varname>]\n"
322            "Argument:\n"
323            "varname: for set result in a environment variable\n"
324            "e.g. guid guid_env"
325 );
326 #endif /* CONFIG_CMD_UUID */
327 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */