1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2011 Calxeda, Inc.
19 #include <dm/uclass.h>
21 #include <linux/ctype.h>
26 #include <eficapsule.h>
29 #include <linux/types.h>
31 #include <linux/kconfig.h>
32 #include <u-boot/uuid.h>
33 #include <u-boot/sha1.h>
36 /* polyfill hextoul to avoid pulling in strto.c */
37 #define hextoul(cp, endp) strtoul(cp, endp, 16)
40 int uuid_str_valid(const char *uuid)
47 for (i = 0, valid = 1; uuid[i] && valid; i++) {
49 case 8: case 13: case 18: case 23:
50 valid = (uuid[i] == '-');
53 valid = isxdigit(uuid[i]);
58 if (i != UUID_STR_LEN || !valid)
69 #ifdef CONFIG_PARTITION_TYPE_GUID
70 {"system", PARTITION_SYSTEM_GUID},
71 {"mbr", LEGACY_MBR_PARTITION_GUID},
72 {"msft", PARTITION_MSFT_RESERVED_GUID},
73 {"data", PARTITION_BASIC_DATA_GUID},
74 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
75 {"raid", PARTITION_LINUX_RAID_GUID},
76 {"swap", PARTITION_LINUX_SWAP_GUID},
77 {"lvm", PARTITION_LINUX_LVM_GUID},
78 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
79 {"cros-kern", PARTITION_CROS_KERNEL},
80 {"cros-root", PARTITION_CROS_ROOT},
81 {"cros-fw", PARTITION_CROS_FIRMWARE},
82 {"cros-rsrv", PARTITION_CROS_RESERVED},
84 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
87 EFI_DEVICE_PATH_PROTOCOL_GUID,
90 "Device Path To Text",
91 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
94 "Device Path Utilities",
95 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
98 "Unicode Collation 2",
99 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
103 EFI_DRIVER_BINDING_PROTOCOL_GUID,
107 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
110 "Simple Text Input Ex",
111 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
114 "Simple Text Output",
115 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
119 EFI_BLOCK_IO_PROTOCOL_GUID,
122 "Simple File System",
123 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
127 EFI_LOADED_IMAGE_PROTOCOL_GUID,
131 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
135 EFI_HII_STRING_PROTOCOL_GUID,
139 EFI_HII_DATABASE_PROTOCOL_GUID,
142 "HII Config Routing",
143 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
147 EFI_LOAD_FILE2_PROTOCOL_GUID,
150 "Random Number Generator",
151 EFI_RNG_PROTOCOL_GUID,
155 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
159 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
163 EFI_DT_FIXUP_PROTOCOL_GUID,
167 EFI_TCG2_PROTOCOL_GUID,
171 PARTITION_SYSTEM_GUID
174 "Firmware Management",
175 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
177 /* Configuration table GUIDs */
183 "EFI System Resource Table",
184 EFI_SYSTEM_RESOURCE_TABLE_GUID,
199 "Runtime properties",
200 EFI_RT_PROPERTIES_TABLE_GUID,
203 "TCG2 Final Events Table",
204 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
207 "EFI Conformance Profiles Table",
208 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
210 #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
213 RISCV_EFI_BOOT_PROTOCOL_GUID,
216 #endif /* CONFIG_CMD_EFIDEBUG */
217 #ifdef CONFIG_CMD_NVEDIT_EFI
218 /* signature database */
220 "EFI_GLOBAL_VARIABLE_GUID",
221 EFI_GLOBAL_VARIABLE_GUID,
224 "EFI_IMAGE_SECURITY_DATABASE_GUID",
225 EFI_IMAGE_SECURITY_DATABASE_GUID,
227 /* certificate types */
229 "EFI_CERT_SHA256_GUID",
230 EFI_CERT_SHA256_GUID,
233 "EFI_CERT_X509_GUID",
237 "EFI_CERT_TYPE_PKCS7_GUID",
238 EFI_CERT_TYPE_PKCS7_GUID,
241 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
242 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
243 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
244 { "EFI_HOB_LIST", EFI_HOB_LIST },
245 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
246 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
247 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
249 #endif /* !USE_HOSTCC */
252 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
256 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
257 if (!strcmp(list_guid[i].string, guid_str)) {
258 memcpy(guid_bin, &list_guid[i].guid, 16);
265 const char *uuid_guid_get_str(const unsigned char *guid_bin)
269 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
270 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
271 return list_guid[i].string;
277 int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
284 if (!uuid_str_valid(uuid_str)) {
285 #ifdef CONFIG_PARTITION_TYPE_GUID
286 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
292 if (str_format == UUID_STR_FORMAT_STD) {
293 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
294 memcpy(uuid_bin, &tmp32, 4);
296 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
297 memcpy(uuid_bin + 4, &tmp16, 2);
299 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
300 memcpy(uuid_bin + 6, &tmp16, 2);
302 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
303 memcpy(uuid_bin, &tmp32, 4);
305 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
306 memcpy(uuid_bin + 4, &tmp16, 2);
308 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
309 memcpy(uuid_bin + 6, &tmp16, 2);
312 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
313 memcpy(uuid_bin + 8, &tmp16, 2);
315 tmp64 = cpu_to_be64(hextoul(uuid_str + 24, NULL));
316 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
321 int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
327 if (!uuid_str_valid(uuid_str) || !uuid_bin)
330 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
331 memcpy(uuid_bin, &tmp32, 4);
333 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
334 memcpy(uuid_bin + 4, &tmp16, 2);
336 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
337 memcpy(uuid_bin + 6, &tmp16, 2);
339 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
340 memcpy(uuid_bin + 8, &tmp16, 2);
342 tmp64 = cpu_to_le64(hextoul(uuid_str + 24, NULL));
343 memcpy(uuid_bin + 10, &tmp64, 6);
348 void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
351 const uint8_t uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
352 9, 10, 11, 12, 13, 14, 15};
353 const uint8_t guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
354 9, 10, 11, 12, 13, 14, 15};
355 const uint8_t *char_order;
360 * UUID and GUID bin data - always in big endian:
364 if (str_format & UUID_STR_FORMAT_GUID)
365 char_order = guid_char_order;
367 char_order = uuid_char_order;
368 if (str_format & UUID_STR_UPPER_CASE)
373 for (i = 0; i < 16; i++) {
374 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
387 static void configure_uuid(struct uuid *uuid, unsigned char version)
391 /* Configure variant/version bits */
392 tmp = be16_to_cpu(uuid->time_hi_and_version);
393 tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT);
394 uuid->time_hi_and_version = cpu_to_be16(tmp);
396 uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK;
397 uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
400 void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...)
407 uint8_t hash[SHA1_SUM_LEN];
410 /* Hash the namespace UUID as salt */
411 sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
412 va_start(args, guid);
414 while ((data = va_arg(args, const uint8_t *))) {
415 unsigned int len = va_arg(args, size_t);
417 sha1_update(&ctx, data, len);
421 sha1_finish(&ctx, hash);
423 /* Truncate the hash into output UUID, it is already big endian */
424 memcpy(guid, hash, sizeof(*guid));
426 configure_uuid((struct uuid *)guid, 5);
428 /* Make little endian */
429 tmp32 = (uint32_t *)&guid->b[0];
430 *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32));
431 tmp16 = (uint16_t *)&guid->b[4];
432 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
433 tmp16 = (uint16_t *)&guid->b[6];
434 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
438 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
439 void gen_rand_uuid(unsigned char *uuid_bin)
442 struct uuid *uuid = (struct uuid *)ptr;
444 struct udevice *devp;
447 if (CONFIG_IS_ENABLED(DM_RNG)) {
448 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
450 ret = dm_rng_read(devp, &randv, sizeof(randv));
458 srand(get_ticks() + rand());
460 /* Set all fields randomly */
461 for (i = 0; i < 4; i++)
464 configure_uuid(uuid, UUID_VERSION);
466 memcpy(uuid_bin, uuid, 16);
469 void gen_rand_uuid_str(char *uuid_str, int str_format)
471 unsigned char uuid_bin[UUID_BIN_LEN];
473 /* Generate UUID (big endian) */
474 gen_rand_uuid(uuid_bin);
476 /* Convert UUID bin to UUID or GUID formated STRING */
477 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
480 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
481 int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
483 char uuid[UUID_STR_LEN + 1];
486 if (!strcmp(argv[0], "uuid"))
487 str_format = UUID_STR_FORMAT_STD;
489 str_format = UUID_STR_FORMAT_GUID;
492 return CMD_RET_USAGE;
494 gen_rand_uuid_str(uuid, str_format);
497 printf("%s\n", uuid);
499 env_set(argv[1], uuid);
501 return CMD_RET_SUCCESS;
504 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
505 "UUID - generate random Universally Unique Identifier",
508 "varname: for set result in a environment variable\n"
512 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
513 "GUID - generate Globally Unique Identifier based on random UUID",
516 "varname: for set result in a environment variable\n"
519 #endif /* CONFIG_CMD_UUID */
520 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
521 #endif /* !USE_HOSTCC */