efi_loader: printing TCG2 protocol GUID
[platform/kernel/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 <command.h>
8 #include <efi_api.h>
9 #include <env.h>
10 #include <rand.h>
11 #include <time.h>
12 #include <uuid.h>
13 #include <linux/ctype.h>
14 #include <errno.h>
15 #include <common.h>
16 #include <asm/io.h>
17 #include <part_efi.h>
18 #include <malloc.h>
19 #include <dm/uclass.h>
20 #include <rng.h>
21
22 /*
23  * UUID - Universally Unique IDentifier - 128 bits unique number.
24  *        There are 5 versions and one variant of UUID defined by RFC4122
25  *        specification. A UUID contains a set of fields. The set varies
26  *        depending on the version of the UUID, as shown below:
27  *        - time, MAC address(v1),
28  *        - user ID(v2),
29  *        - MD5 of name or URL(v3),
30  *        - random data(v4),
31  *        - SHA-1 of name or URL(v5),
32  *
33  * Layout of UUID:
34  * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
35  * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
36  * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
37  * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
38  * node      - 48 bit
39  *
40  * source: https://www.ietf.org/rfc/rfc4122.txt
41  *
42  * UUID binary format (16 bytes):
43  *
44  * 4B-2B-2B-2B-6B (big endian - network byte order)
45  *
46  * UUID string is 36 length of characters (36 bytes):
47  *
48  * 0        9    14   19   24
49  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
50  *    be     be   be   be       be
51  *
52  * where x is a hexadecimal character. Fields are separated by '-'s.
53  * When converting to a binary UUID, le means the field should be converted
54  * to little endian and be means it should be converted to big endian.
55  *
56  * UUID is also used as GUID (Globally Unique Identifier) with the same binary
57  * format but it differs in string format like below.
58  *
59  * GUID:
60  * 0        9    14   19   24
61  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
62  *    le     le   le   be       be
63  *
64  * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
65  */
66 int uuid_str_valid(const char *uuid)
67 {
68         int i, valid;
69
70         if (uuid == NULL)
71                 return 0;
72
73         for (i = 0, valid = 1; uuid[i] && valid; i++) {
74                 switch (i) {
75                 case 8: case 13: case 18: case 23:
76                         valid = (uuid[i] == '-');
77                         break;
78                 default:
79                         valid = isxdigit(uuid[i]);
80                         break;
81                 }
82         }
83
84         if (i != UUID_STR_LEN || !valid)
85                 return 0;
86
87         return 1;
88 }
89
90 static const struct {
91         const char *string;
92         efi_guid_t guid;
93 } list_guid[] = {
94 #ifdef CONFIG_PARTITION_TYPE_GUID
95         {"system",      PARTITION_SYSTEM_GUID},
96         {"mbr",         LEGACY_MBR_PARTITION_GUID},
97         {"msft",        PARTITION_MSFT_RESERVED_GUID},
98         {"data",        PARTITION_BASIC_DATA_GUID},
99         {"linux",       PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
100         {"raid",        PARTITION_LINUX_RAID_GUID},
101         {"swap",        PARTITION_LINUX_SWAP_GUID},
102         {"lvm",         PARTITION_LINUX_LVM_GUID},
103         {"u-boot-env",  PARTITION_U_BOOT_ENVIRONMENT},
104 #endif
105 #ifdef CONFIG_CMD_EFIDEBUG
106         {
107                 "Device Path",
108                 EFI_DEVICE_PATH_PROTOCOL_GUID,
109         },
110         {
111                 "Device Path To Text",
112                 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
113         },
114         {
115                 "Device Path Utilities",
116                 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
117         },
118         {
119                 "Unicode Collation 2",
120                 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
121         },
122         {
123                 "Driver Binding",
124                 EFI_DRIVER_BINDING_PROTOCOL_GUID,
125         },
126         {
127                 "Simple Text Input",
128                 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
129         },
130         {
131                 "Simple Text Input Ex",
132                 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
133         },
134         {
135                 "Simple Text Output",
136                 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
137         },
138         {
139                 "Block IO",
140                 EFI_BLOCK_IO_PROTOCOL_GUID,
141         },
142         {
143                 "Simple File System",
144                 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
145         },
146         {
147                 "Loaded Image",
148                 EFI_LOADED_IMAGE_PROTOCOL_GUID,
149         },
150         {
151                 "Graphics Output",
152                 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
153         },
154         {
155                 "HII String",
156                 EFI_HII_STRING_PROTOCOL_GUID,
157         },
158         {
159                 "HII Database",
160                 EFI_HII_DATABASE_PROTOCOL_GUID,
161         },
162         {
163                 "HII Config Routing",
164                 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
165         },
166         {
167                 "Load File2",
168                 EFI_LOAD_FILE2_PROTOCOL_GUID,
169         },
170         {
171                 "Random Number Generator",
172                 EFI_RNG_PROTOCOL_GUID,
173         },
174         {
175                 "Simple Network",
176                 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
177         },
178         {
179                 "PXE Base Code",
180                 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
181         },
182         {
183                 "Device-Tree Fixup",
184                 EFI_DT_FIXUP_PROTOCOL_GUID,
185         },
186         {
187                 "TCG2",
188                 EFI_TCG2_PROTOCOL_GUID,
189                 },
190         {
191                 "System Partition",
192                 PARTITION_SYSTEM_GUID
193         },
194         {
195                 "Firmware Management",
196                 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
197         },
198         /* Configuration table GUIDs */
199         {
200                 "ACPI table",
201                 EFI_ACPI_TABLE_GUID,
202         },
203         {
204                 "EFI System Resource Table",
205                 EFI_SYSTEM_RESOURCE_TABLE_GUID,
206         },
207         {
208                 "device tree",
209                 EFI_FDT_GUID,
210         },
211         {
212                 "SMBIOS table",
213                 SMBIOS_TABLE_GUID,
214         },
215         {
216                 "Runtime properties",
217                 EFI_RT_PROPERTIES_TABLE_GUID,
218         },
219         {
220                 "TCG2 Final Events Table",
221                 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
222         },
223 #endif
224 #ifdef CONFIG_CMD_NVEDIT_EFI
225         /* signature database */
226         {
227                 "EFI_GLOBAL_VARIABLE_GUID",
228                 EFI_GLOBAL_VARIABLE_GUID,
229         },
230         {
231                 "EFI_IMAGE_SECURITY_DATABASE_GUID",
232                 EFI_IMAGE_SECURITY_DATABASE_GUID,
233         },
234         /* certificate types */
235         {
236                 "EFI_CERT_SHA256_GUID",
237                 EFI_CERT_SHA256_GUID,
238         },
239         {
240                 "EFI_CERT_X509_GUID",
241                 EFI_CERT_X509_GUID,
242         },
243         {
244                 "EFI_CERT_TYPE_PKCS7_GUID",
245                 EFI_CERT_TYPE_PKCS7_GUID,
246         },
247 #endif
248 };
249
250 /*
251  * uuid_guid_get_bin() - this function get GUID bin for string
252  *
253  * @param guid_str - pointer to partition type string
254  * @param guid_bin - pointer to allocated array for big endian output [16B]
255  */
256 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
257 {
258         int i;
259
260         for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
261                 if (!strcmp(list_guid[i].string, guid_str)) {
262                         memcpy(guid_bin, &list_guid[i].guid, 16);
263                         return 0;
264                 }
265         }
266         return -ENODEV;
267 }
268
269 /*
270  * uuid_guid_get_str() - this function get string for GUID.
271  *
272  * @param guid_bin - pointer to string with partition type guid [16B]
273  *
274  * Returns NULL if the type GUID is not known.
275  */
276 const char *uuid_guid_get_str(const unsigned char *guid_bin)
277 {
278         int i;
279
280         for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
281                 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
282                         return list_guid[i].string;
283                 }
284         }
285         return NULL;
286 }
287
288 /*
289  * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
290  *
291  * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
292  * @param uuid_bin - pointer to allocated array for big endian output [16B]
293  * @str_format     - UUID string format: 0 - UUID; 1 - GUID
294  */
295 int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
296                     int str_format)
297 {
298         uint16_t tmp16;
299         uint32_t tmp32;
300         uint64_t tmp64;
301
302         if (!uuid_str_valid(uuid_str)) {
303 #ifdef CONFIG_PARTITION_TYPE_GUID
304                 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
305                         return 0;
306 #endif
307                 return -EINVAL;
308         }
309
310         if (str_format == UUID_STR_FORMAT_STD) {
311                 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
312                 memcpy(uuid_bin, &tmp32, 4);
313
314                 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
315                 memcpy(uuid_bin + 4, &tmp16, 2);
316
317                 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
318                 memcpy(uuid_bin + 6, &tmp16, 2);
319         } else {
320                 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
321                 memcpy(uuid_bin, &tmp32, 4);
322
323                 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
324                 memcpy(uuid_bin + 4, &tmp16, 2);
325
326                 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
327                 memcpy(uuid_bin + 6, &tmp16, 2);
328         }
329
330         tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
331         memcpy(uuid_bin + 8, &tmp16, 2);
332
333         tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
334         memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
335
336         return 0;
337 }
338
339 /*
340  * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
341  *
342  * @param uuid_bin:     pointer to binary data of UUID (big endian) [16B]
343  * @param uuid_str:     pointer to allocated array for output string [37B]
344  * @str_format:         bit 0: 0 - UUID; 1 - GUID
345  *                      bit 1: 0 - lower case; 2 - upper case
346  */
347 void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
348                      int str_format)
349 {
350         const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
351                                                   9, 10, 11, 12, 13, 14, 15};
352         const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
353                                                   9, 10, 11, 12, 13, 14, 15};
354         const u8 *char_order;
355         const char *format;
356         int i;
357
358         /*
359          * UUID and GUID bin data - always in big endian:
360          * 4B-2B-2B-2B-6B
361          * be be be be be
362          */
363         if (str_format & UUID_STR_FORMAT_GUID)
364                 char_order = guid_char_order;
365         else
366                 char_order = uuid_char_order;
367         if (str_format & UUID_STR_UPPER_CASE)
368                 format = "%02X";
369         else
370                 format = "%02x";
371
372         for (i = 0; i < 16; i++) {
373                 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
374                 uuid_str += 2;
375                 switch (i) {
376                 case 3:
377                 case 5:
378                 case 7:
379                 case 9:
380                         *uuid_str++ = '-';
381                         break;
382                 }
383         }
384 }
385
386 /*
387  * gen_rand_uuid() - this function generates a random binary UUID version 4.
388  *                   In this version all fields beside 4 bits of version and
389  *                   2 bits of variant are randomly generated.
390  *
391  * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
392 */
393 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
394 void gen_rand_uuid(unsigned char *uuid_bin)
395 {
396         u32 ptr[4];
397         struct uuid *uuid = (struct uuid *)ptr;
398         int i, ret;
399         struct udevice *devp;
400         u32 randv = 0;
401
402         if (IS_ENABLED(CONFIG_DM_RNG)) {
403                 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
404                 if (!ret) {
405                         ret = dm_rng_read(devp, &randv, sizeof(randv));
406                         if (ret < 0)
407                                 randv = 0;
408                 }
409         }
410         if (randv)
411                 srand(randv);
412         else
413                 srand(get_ticks() + rand());
414
415         /* Set all fields randomly */
416         for (i = 0; i < 4; i++)
417                 ptr[i] = rand();
418
419         clrsetbits_be16(&uuid->time_hi_and_version,
420                         UUID_VERSION_MASK,
421                         UUID_VERSION << UUID_VERSION_SHIFT);
422
423         clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
424                      UUID_VARIANT_MASK,
425                      UUID_VARIANT << UUID_VARIANT_SHIFT);
426
427         memcpy(uuid_bin, uuid, 16);
428 }
429
430 /*
431  * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
432  *                       formats UUID or GUID.
433  *
434  * @param uuid_str - pointer to allocated array [37B].
435  * @param          - uuid output type: UUID - 0, GUID - 1
436  */
437 void gen_rand_uuid_str(char *uuid_str, int str_format)
438 {
439         unsigned char uuid_bin[UUID_BIN_LEN];
440
441         /* Generate UUID (big endian) */
442         gen_rand_uuid(uuid_bin);
443
444         /* Convert UUID bin to UUID or GUID formated STRING  */
445         uuid_bin_to_str(uuid_bin, uuid_str, str_format);
446 }
447
448 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
449 int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
450 {
451         char uuid[UUID_STR_LEN + 1];
452         int str_format;
453
454         if (!strcmp(argv[0], "uuid"))
455                 str_format = UUID_STR_FORMAT_STD;
456         else
457                 str_format = UUID_STR_FORMAT_GUID;
458
459         if (argc > 2)
460                 return CMD_RET_USAGE;
461
462         gen_rand_uuid_str(uuid, str_format);
463
464         if (argc == 1)
465                 printf("%s\n", uuid);
466         else
467                 env_set(argv[1], uuid);
468
469         return CMD_RET_SUCCESS;
470 }
471
472 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
473            "UUID - generate random Universally Unique Identifier",
474            "[<varname>]\n"
475            "Argument:\n"
476            "varname: for set result in a environment variable\n"
477            "e.g. uuid uuid_env"
478 );
479
480 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
481            "GUID - generate Globally Unique Identifier based on random UUID",
482            "[<varname>]\n"
483            "Argument:\n"
484            "varname: for set result in a environment variable\n"
485            "e.g. guid guid_env"
486 );
487 #endif /* CONFIG_CMD_UUID */
488 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */