1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2011 Calxeda, Inc.
13 #include <linux/ctype.h>
19 #include <dm/uclass.h>
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),
29 * - MD5 of name or URL(v3),
31 * - SHA-1 of name or URL(v5),
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
40 * source: https://www.ietf.org/rfc/rfc4122.txt
42 * UUID binary format (16 bytes):
44 * 4B-2B-2B-2B-6B (big endian - network byte order)
46 * UUID string is 36 length of characters (36 bytes):
49 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
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.
56 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
57 * format but it differs in string format like below.
61 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
64 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
66 int uuid_str_valid(const char *uuid)
73 for (i = 0, valid = 1; uuid[i] && valid; i++) {
75 case 8: case 13: case 18: case 23:
76 valid = (uuid[i] == '-');
79 valid = isxdigit(uuid[i]);
84 if (i != UUID_STR_LEN || !valid)
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},
105 #ifdef CONFIG_CMD_EFIDEBUG
108 EFI_DEVICE_PATH_PROTOCOL_GUID,
111 "Device Path To Text",
112 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
115 "Device Path Utilities",
116 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
119 "Unicode Collation 2",
120 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
124 EFI_DRIVER_BINDING_PROTOCOL_GUID,
128 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
131 "Simple Text Input Ex",
132 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
135 "Simple Text Output",
136 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
140 EFI_BLOCK_IO_PROTOCOL_GUID,
143 "Simple File System",
144 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
148 EFI_LOADED_IMAGE_PROTOCOL_GUID,
152 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
156 EFI_HII_STRING_PROTOCOL_GUID,
160 EFI_HII_DATABASE_PROTOCOL_GUID,
163 "HII Config Routing",
164 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
168 EFI_LOAD_FILE2_PROTOCOL_GUID,
171 "Random Number Generator",
172 EFI_RNG_PROTOCOL_GUID,
176 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
180 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
184 EFI_DT_FIXUP_PROTOCOL_GUID,
188 PARTITION_SYSTEM_GUID
191 "Firmware Management",
192 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
194 /* Configuration table GUIDs */
200 "EFI System Resource Table",
201 EFI_SYSTEM_RESOURCE_TABLE_GUID,
212 "Runtime properties",
213 EFI_RT_PROPERTIES_TABLE_GUID,
216 "TCG2 Final Events Table",
217 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
223 * uuid_guid_get_bin() - this function get GUID bin for string
225 * @param guid_str - pointer to partition type string
226 * @param guid_bin - pointer to allocated array for big endian output [16B]
228 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
232 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
233 if (!strcmp(list_guid[i].string, guid_str)) {
234 memcpy(guid_bin, &list_guid[i].guid, 16);
242 * uuid_guid_get_str() - this function get string for GUID.
244 * @param guid_bin - pointer to string with partition type guid [16B]
246 * Returns NULL if the type GUID is not known.
248 const char *uuid_guid_get_str(const unsigned char *guid_bin)
252 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
253 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
254 return list_guid[i].string;
261 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
263 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
264 * @param uuid_bin - pointer to allocated array for big endian output [16B]
265 * @str_format - UUID string format: 0 - UUID; 1 - GUID
267 int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
274 if (!uuid_str_valid(uuid_str)) {
275 #ifdef CONFIG_PARTITION_TYPE_GUID
276 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
282 if (str_format == UUID_STR_FORMAT_STD) {
283 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
284 memcpy(uuid_bin, &tmp32, 4);
286 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
287 memcpy(uuid_bin + 4, &tmp16, 2);
289 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
290 memcpy(uuid_bin + 6, &tmp16, 2);
292 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
293 memcpy(uuid_bin, &tmp32, 4);
295 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
296 memcpy(uuid_bin + 4, &tmp16, 2);
298 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
299 memcpy(uuid_bin + 6, &tmp16, 2);
302 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
303 memcpy(uuid_bin + 8, &tmp16, 2);
305 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
306 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
312 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
314 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
315 * @param uuid_str: pointer to allocated array for output string [37B]
316 * @str_format: bit 0: 0 - UUID; 1 - GUID
317 * bit 1: 0 - lower case; 2 - upper case
319 void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
322 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
323 9, 10, 11, 12, 13, 14, 15};
324 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
325 9, 10, 11, 12, 13, 14, 15};
326 const u8 *char_order;
331 * UUID and GUID bin data - always in big endian:
335 if (str_format & UUID_STR_FORMAT_GUID)
336 char_order = guid_char_order;
338 char_order = uuid_char_order;
339 if (str_format & UUID_STR_UPPER_CASE)
344 for (i = 0; i < 16; i++) {
345 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
359 * gen_rand_uuid() - this function generates a random binary UUID version 4.
360 * In this version all fields beside 4 bits of version and
361 * 2 bits of variant are randomly generated.
363 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
365 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
366 void gen_rand_uuid(unsigned char *uuid_bin)
369 struct uuid *uuid = (struct uuid *)ptr;
371 struct udevice *devp;
374 if (IS_ENABLED(CONFIG_DM_RNG)) {
375 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
377 ret = dm_rng_read(devp, &randv, sizeof(randv));
385 srand(get_ticks() + rand());
387 /* Set all fields randomly */
388 for (i = 0; i < 4; i++)
391 clrsetbits_be16(&uuid->time_hi_and_version,
393 UUID_VERSION << UUID_VERSION_SHIFT);
395 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
397 UUID_VARIANT << UUID_VARIANT_SHIFT);
399 memcpy(uuid_bin, uuid, 16);
403 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
404 * formats UUID or GUID.
406 * @param uuid_str - pointer to allocated array [37B].
407 * @param - uuid output type: UUID - 0, GUID - 1
409 void gen_rand_uuid_str(char *uuid_str, int str_format)
411 unsigned char uuid_bin[UUID_BIN_LEN];
413 /* Generate UUID (big endian) */
414 gen_rand_uuid(uuid_bin);
416 /* Convert UUID bin to UUID or GUID formated STRING */
417 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
420 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
421 int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
423 char uuid[UUID_STR_LEN + 1];
426 if (!strcmp(argv[0], "uuid"))
427 str_format = UUID_STR_FORMAT_STD;
429 str_format = UUID_STR_FORMAT_GUID;
432 return CMD_RET_USAGE;
434 gen_rand_uuid_str(uuid, str_format);
437 printf("%s\n", uuid);
439 env_set(argv[1], uuid);
441 return CMD_RET_SUCCESS;
444 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
445 "UUID - generate random Universally Unique Identifier",
448 "varname: for set result in a environment variable\n"
452 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
453 "GUID - generate Globally Unique Identifier based on random UUID",
456 "varname: for set result in a environment variable\n"
459 #endif /* CONFIG_CMD_UUID */
460 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */