1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2011 Calxeda, Inc.
17 #include <linux/ctype.h>
23 #include <dm/uclass.h>
27 * UUID - Universally Unique IDentifier - 128 bits unique number.
28 * There are 5 versions and one variant of UUID defined by RFC4122
29 * specification. A UUID contains a set of fields. The set varies
30 * depending on the version of the UUID, as shown below:
31 * - time, MAC address(v1),
33 * - MD5 of name or URL(v3),
35 * - SHA-1 of name or URL(v5),
38 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
39 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
40 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
41 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
44 * source: https://www.ietf.org/rfc/rfc4122.txt
46 * UUID binary format (16 bytes):
48 * 4B-2B-2B-2B-6B (big endian - network byte order)
50 * UUID string is 36 length of characters (36 bytes):
53 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
56 * where x is a hexadecimal character. Fields are separated by '-'s.
57 * When converting to a binary UUID, le means the field should be converted
58 * to little endian and be means it should be converted to big endian.
60 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
61 * format but it differs in string format like below.
65 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
68 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
70 int uuid_str_valid(const char *uuid)
77 for (i = 0, valid = 1; uuid[i] && valid; i++) {
79 case 8: case 13: case 18: case 23:
80 valid = (uuid[i] == '-');
83 valid = isxdigit(uuid[i]);
88 if (i != UUID_STR_LEN || !valid)
98 #ifdef CONFIG_PARTITION_TYPE_GUID
99 {"system", PARTITION_SYSTEM_GUID},
100 {"mbr", LEGACY_MBR_PARTITION_GUID},
101 {"msft", PARTITION_MSFT_RESERVED_GUID},
102 {"data", PARTITION_BASIC_DATA_GUID},
103 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
104 {"raid", PARTITION_LINUX_RAID_GUID},
105 {"swap", PARTITION_LINUX_SWAP_GUID},
106 {"lvm", PARTITION_LINUX_LVM_GUID},
107 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
109 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
112 EFI_DEVICE_PATH_PROTOCOL_GUID,
115 "Device Path To Text",
116 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
119 "Device Path Utilities",
120 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
123 "Unicode Collation 2",
124 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
128 EFI_DRIVER_BINDING_PROTOCOL_GUID,
132 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
135 "Simple Text Input Ex",
136 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
139 "Simple Text Output",
140 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
144 EFI_BLOCK_IO_PROTOCOL_GUID,
147 "Simple File System",
148 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
152 EFI_LOADED_IMAGE_PROTOCOL_GUID,
156 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
160 EFI_HII_STRING_PROTOCOL_GUID,
164 EFI_HII_DATABASE_PROTOCOL_GUID,
167 "HII Config Routing",
168 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
172 EFI_LOAD_FILE2_PROTOCOL_GUID,
175 "Random Number Generator",
176 EFI_RNG_PROTOCOL_GUID,
180 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
184 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
188 EFI_DT_FIXUP_PROTOCOL_GUID,
192 EFI_TCG2_PROTOCOL_GUID,
196 PARTITION_SYSTEM_GUID
199 "Firmware Management",
200 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
202 /* Configuration table GUIDs */
208 "EFI System Resource Table",
209 EFI_SYSTEM_RESOURCE_TABLE_GUID,
220 "Runtime properties",
221 EFI_RT_PROPERTIES_TABLE_GUID,
224 "TCG2 Final Events Table",
225 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
228 "EFI Conformance Profiles Table",
229 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
231 #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
234 RISCV_EFI_BOOT_PROTOCOL_GUID,
237 #endif /* CONFIG_CMD_EFIDEBUG */
238 #ifdef CONFIG_CMD_NVEDIT_EFI
239 /* signature database */
241 "EFI_GLOBAL_VARIABLE_GUID",
242 EFI_GLOBAL_VARIABLE_GUID,
245 "EFI_IMAGE_SECURITY_DATABASE_GUID",
246 EFI_IMAGE_SECURITY_DATABASE_GUID,
248 /* certificate types */
250 "EFI_CERT_SHA256_GUID",
251 EFI_CERT_SHA256_GUID,
254 "EFI_CERT_X509_GUID",
258 "EFI_CERT_TYPE_PKCS7_GUID",
259 EFI_CERT_TYPE_PKCS7_GUID,
262 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
263 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
264 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
265 { "EFI_HOB_LIST", EFI_HOB_LIST },
266 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
267 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
268 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
273 * uuid_guid_get_bin() - this function get GUID bin for string
275 * @param guid_str - pointer to partition type string
276 * @param guid_bin - pointer to allocated array for big endian output [16B]
278 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
282 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
283 if (!strcmp(list_guid[i].string, guid_str)) {
284 memcpy(guid_bin, &list_guid[i].guid, 16);
292 * uuid_guid_get_str() - this function get string for GUID.
294 * @param guid_bin - pointer to string with partition type guid [16B]
296 * Returns NULL if the type GUID is not known.
298 const char *uuid_guid_get_str(const unsigned char *guid_bin)
302 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
303 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
304 return list_guid[i].string;
311 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
313 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
314 * @param uuid_bin - pointer to allocated array for big endian output [16B]
315 * @str_format - UUID string format: 0 - UUID; 1 - GUID
317 int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
324 if (!uuid_str_valid(uuid_str)) {
325 #ifdef CONFIG_PARTITION_TYPE_GUID
326 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
332 if (str_format == UUID_STR_FORMAT_STD) {
333 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
334 memcpy(uuid_bin, &tmp32, 4);
336 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
337 memcpy(uuid_bin + 4, &tmp16, 2);
339 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
340 memcpy(uuid_bin + 6, &tmp16, 2);
342 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
343 memcpy(uuid_bin, &tmp32, 4);
345 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
346 memcpy(uuid_bin + 4, &tmp16, 2);
348 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
349 memcpy(uuid_bin + 6, &tmp16, 2);
352 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
353 memcpy(uuid_bin + 8, &tmp16, 2);
355 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
356 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
362 * uuid_str_to_le_bin() - Convert string UUID to little endian binary data.
363 * @uuid_str: pointer to UUID string
364 * @uuid_bin: pointer to allocated array for little endian output [16B]
366 * UUID string is 36 characters (36 bytes):
368 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
370 * where x is a hexadecimal character. Fields are separated by '-'s.
371 * When converting to a little endian binary UUID, the string fields are reversed.
375 * uuid_bin filled with little endian UUID data
376 * On success 0 is returned. Otherwise, failure code.
378 int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
384 if (!uuid_str_valid(uuid_str) || !uuid_bin)
387 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
388 memcpy(uuid_bin, &tmp32, 4);
390 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
391 memcpy(uuid_bin + 4, &tmp16, 2);
393 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
394 memcpy(uuid_bin + 6, &tmp16, 2);
396 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
397 memcpy(uuid_bin + 8, &tmp16, 2);
399 tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
400 memcpy(uuid_bin + 10, &tmp64, 6);
406 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
408 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
409 * @param uuid_str: pointer to allocated array for output string [37B]
410 * @str_format: bit 0: 0 - UUID; 1 - GUID
411 * bit 1: 0 - lower case; 2 - upper case
413 void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
416 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
417 9, 10, 11, 12, 13, 14, 15};
418 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
419 9, 10, 11, 12, 13, 14, 15};
420 const u8 *char_order;
425 * UUID and GUID bin data - always in big endian:
429 if (str_format & UUID_STR_FORMAT_GUID)
430 char_order = guid_char_order;
432 char_order = uuid_char_order;
433 if (str_format & UUID_STR_UPPER_CASE)
438 for (i = 0; i < 16; i++) {
439 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
453 * gen_rand_uuid() - this function generates a random binary UUID version 4.
454 * In this version all fields beside 4 bits of version and
455 * 2 bits of variant are randomly generated.
457 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
459 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
460 void gen_rand_uuid(unsigned char *uuid_bin)
463 struct uuid *uuid = (struct uuid *)ptr;
465 struct udevice *devp;
468 if (IS_ENABLED(CONFIG_DM_RNG)) {
469 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
471 ret = dm_rng_read(devp, &randv, sizeof(randv));
479 srand(get_ticks() + rand());
481 /* Set all fields randomly */
482 for (i = 0; i < 4; i++)
485 clrsetbits_be16(&uuid->time_hi_and_version,
487 UUID_VERSION << UUID_VERSION_SHIFT);
489 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
491 UUID_VARIANT << UUID_VARIANT_SHIFT);
493 memcpy(uuid_bin, uuid, 16);
497 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
498 * formats UUID or GUID.
500 * @param uuid_str - pointer to allocated array [37B].
501 * @param - uuid output type: UUID - 0, GUID - 1
503 void gen_rand_uuid_str(char *uuid_str, int str_format)
505 unsigned char uuid_bin[UUID_BIN_LEN];
507 /* Generate UUID (big endian) */
508 gen_rand_uuid(uuid_bin);
510 /* Convert UUID bin to UUID or GUID formated STRING */
511 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
514 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
515 int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
517 char uuid[UUID_STR_LEN + 1];
520 if (!strcmp(argv[0], "uuid"))
521 str_format = UUID_STR_FORMAT_STD;
523 str_format = UUID_STR_FORMAT_GUID;
526 return CMD_RET_USAGE;
528 gen_rand_uuid_str(uuid, str_format);
531 printf("%s\n", uuid);
533 env_set(argv[1], uuid);
535 return CMD_RET_SUCCESS;
538 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
539 "UUID - generate random Universally Unique Identifier",
542 "varname: for set result in a environment variable\n"
546 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
547 "GUID - generate Globally Unique Identifier based on random UUID",
550 "varname: for set result in a environment variable\n"
553 #endif /* CONFIG_CMD_UUID */
554 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */