1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2011 Calxeda, Inc.
4 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
7 * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
10 #define LOG_CATEGOT LOGC_CORE
18 #include <linux/ctype.h>
23 #include <dm/uclass.h>
26 int uuid_str_valid(const char *uuid
)
33 for (i
= 0, valid
= 1; uuid
[i
] && valid
; i
++) {
35 case 8: case 13: case 18: case 23:
36 valid
= (uuid
[i
] == '-');
39 valid
= isxdigit(uuid
[i
]);
44 if (i
!= UUID_STR_LEN
|| !valid
)
54 #ifdef CONFIG_PARTITION_TYPE_GUID
55 {"system", PARTITION_SYSTEM_GUID
},
56 {"mbr", LEGACY_MBR_PARTITION_GUID
},
57 {"msft", PARTITION_MSFT_RESERVED_GUID
},
58 {"data", PARTITION_BASIC_DATA_GUID
},
59 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID
},
60 {"raid", PARTITION_LINUX_RAID_GUID
},
61 {"swap", PARTITION_LINUX_SWAP_GUID
},
62 {"lvm", PARTITION_LINUX_LVM_GUID
},
63 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT
},
64 {"cros-kern", PARTITION_CROS_KERNEL
},
65 {"cros-root", PARTITION_CROS_ROOT
},
66 {"cros-fw", PARTITION_CROS_FIRMWARE
},
67 {"cros-rsrv", PARTITION_CROS_RESERVED
},
69 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
72 EFI_DEVICE_PATH_PROTOCOL_GUID
,
75 "Device Path To Text",
76 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID
,
79 "Device Path Utilities",
80 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID
,
83 "Unicode Collation 2",
84 EFI_UNICODE_COLLATION_PROTOCOL2_GUID
,
88 EFI_DRIVER_BINDING_PROTOCOL_GUID
,
92 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID
,
95 "Simple Text Input Ex",
96 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID
,
100 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID
,
104 EFI_BLOCK_IO_PROTOCOL_GUID
,
107 "Simple File System",
108 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
,
112 EFI_LOADED_IMAGE_PROTOCOL_GUID
,
116 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
,
120 EFI_HII_STRING_PROTOCOL_GUID
,
124 EFI_HII_DATABASE_PROTOCOL_GUID
,
127 "HII Config Routing",
128 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID
,
132 EFI_LOAD_FILE2_PROTOCOL_GUID
,
135 "Random Number Generator",
136 EFI_RNG_PROTOCOL_GUID
,
140 EFI_SIMPLE_NETWORK_PROTOCOL_GUID
,
144 EFI_PXE_BASE_CODE_PROTOCOL_GUID
,
148 EFI_DT_FIXUP_PROTOCOL_GUID
,
152 EFI_TCG2_PROTOCOL_GUID
,
156 PARTITION_SYSTEM_GUID
159 "Firmware Management",
160 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
162 /* Configuration table GUIDs */
168 "EFI System Resource Table",
169 EFI_SYSTEM_RESOURCE_TABLE_GUID
,
180 "Runtime properties",
181 EFI_RT_PROPERTIES_TABLE_GUID
,
184 "TCG2 Final Events Table",
185 EFI_TCG2_FINAL_EVENTS_TABLE_GUID
,
188 "EFI Conformance Profiles Table",
189 EFI_CONFORMANCE_PROFILES_TABLE_GUID
,
191 #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
194 RISCV_EFI_BOOT_PROTOCOL_GUID
,
197 #endif /* CONFIG_CMD_EFIDEBUG */
198 #ifdef CONFIG_CMD_NVEDIT_EFI
199 /* signature database */
201 "EFI_GLOBAL_VARIABLE_GUID",
202 EFI_GLOBAL_VARIABLE_GUID
,
205 "EFI_IMAGE_SECURITY_DATABASE_GUID",
206 EFI_IMAGE_SECURITY_DATABASE_GUID
,
208 /* certificate types */
210 "EFI_CERT_SHA256_GUID",
211 EFI_CERT_SHA256_GUID
,
214 "EFI_CERT_X509_GUID",
218 "EFI_CERT_TYPE_PKCS7_GUID",
219 EFI_CERT_TYPE_PKCS7_GUID
,
222 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
223 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED
},
224 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES
},
225 { "EFI_HOB_LIST", EFI_HOB_LIST
},
226 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE
},
227 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC
},
228 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1
},
232 int uuid_guid_get_bin(const char *guid_str
, unsigned char *guid_bin
)
236 for (i
= 0; i
< ARRAY_SIZE(list_guid
); i
++) {
237 if (!strcmp(list_guid
[i
].string
, guid_str
)) {
238 memcpy(guid_bin
, &list_guid
[i
].guid
, 16);
245 const char *uuid_guid_get_str(const unsigned char *guid_bin
)
249 for (i
= 0; i
< ARRAY_SIZE(list_guid
); i
++) {
250 if (!memcmp(list_guid
[i
].guid
.b
, guid_bin
, 16)) {
251 return list_guid
[i
].string
;
257 int uuid_str_to_bin(const char *uuid_str
, unsigned char *uuid_bin
,
264 if (!uuid_str_valid(uuid_str
)) {
265 log_debug("not valid\n");
266 #ifdef CONFIG_PARTITION_TYPE_GUID
267 if (!uuid_guid_get_bin(uuid_str
, uuid_bin
))
273 if (str_format
== UUID_STR_FORMAT_STD
) {
274 tmp32
= cpu_to_be32(hextoul(uuid_str
, NULL
));
275 memcpy(uuid_bin
, &tmp32
, 4);
277 tmp16
= cpu_to_be16(hextoul(uuid_str
+ 9, NULL
));
278 memcpy(uuid_bin
+ 4, &tmp16
, 2);
280 tmp16
= cpu_to_be16(hextoul(uuid_str
+ 14, NULL
));
281 memcpy(uuid_bin
+ 6, &tmp16
, 2);
283 tmp32
= cpu_to_le32(hextoul(uuid_str
, NULL
));
284 memcpy(uuid_bin
, &tmp32
, 4);
286 tmp16
= cpu_to_le16(hextoul(uuid_str
+ 9, NULL
));
287 memcpy(uuid_bin
+ 4, &tmp16
, 2);
289 tmp16
= cpu_to_le16(hextoul(uuid_str
+ 14, NULL
));
290 memcpy(uuid_bin
+ 6, &tmp16
, 2);
293 tmp16
= cpu_to_be16(hextoul(uuid_str
+ 19, NULL
));
294 memcpy(uuid_bin
+ 8, &tmp16
, 2);
296 tmp64
= cpu_to_be64(simple_strtoull(uuid_str
+ 24, NULL
, 16));
297 memcpy(uuid_bin
+ 10, (char *)&tmp64
+ 2, 6);
302 int uuid_str_to_le_bin(const char *uuid_str
, unsigned char *uuid_bin
)
308 if (!uuid_str_valid(uuid_str
) || !uuid_bin
)
311 tmp32
= cpu_to_le32(hextoul(uuid_str
, NULL
));
312 memcpy(uuid_bin
, &tmp32
, 4);
314 tmp16
= cpu_to_le16(hextoul(uuid_str
+ 9, NULL
));
315 memcpy(uuid_bin
+ 4, &tmp16
, 2);
317 tmp16
= cpu_to_le16(hextoul(uuid_str
+ 14, NULL
));
318 memcpy(uuid_bin
+ 6, &tmp16
, 2);
320 tmp16
= cpu_to_le16(hextoul(uuid_str
+ 19, NULL
));
321 memcpy(uuid_bin
+ 8, &tmp16
, 2);
323 tmp64
= cpu_to_le64(simple_strtoull(uuid_str
+ 24, NULL
, 16));
324 memcpy(uuid_bin
+ 10, &tmp64
, 6);
329 void uuid_bin_to_str(const unsigned char *uuid_bin
, char *uuid_str
,
332 const u8 uuid_char_order
[UUID_BIN_LEN
] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
333 9, 10, 11, 12, 13, 14, 15};
334 const u8 guid_char_order
[UUID_BIN_LEN
] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
335 9, 10, 11, 12, 13, 14, 15};
336 const u8
*char_order
;
341 * UUID and GUID bin data - always in big endian:
345 if (str_format
& UUID_STR_FORMAT_GUID
)
346 char_order
= guid_char_order
;
348 char_order
= uuid_char_order
;
349 if (str_format
& UUID_STR_UPPER_CASE
)
354 for (i
= 0; i
< 16; i
++) {
355 sprintf(uuid_str
, format
, uuid_bin
[char_order
[i
]]);
368 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
369 void gen_rand_uuid(unsigned char *uuid_bin
)
372 struct uuid
*uuid
= (struct uuid
*)ptr
;
374 struct udevice
*devp
;
377 if (IS_ENABLED(CONFIG_DM_RNG
)) {
378 ret
= uclass_get_device(UCLASS_RNG
, 0, &devp
);
380 ret
= dm_rng_read(devp
, &randv
, sizeof(randv
));
388 srand(get_ticks() + rand());
390 /* Set all fields randomly */
391 for (i
= 0; i
< 4; i
++)
394 clrsetbits_be16(&uuid
->time_hi_and_version
,
396 UUID_VERSION
<< UUID_VERSION_SHIFT
);
398 clrsetbits_8(&uuid
->clock_seq_hi_and_reserved
,
400 UUID_VARIANT
<< UUID_VARIANT_SHIFT
);
402 memcpy(uuid_bin
, uuid
, 16);
405 void gen_rand_uuid_str(char *uuid_str
, int str_format
)
407 unsigned char uuid_bin
[UUID_BIN_LEN
];
409 /* Generate UUID (big endian) */
410 gen_rand_uuid(uuid_bin
);
412 /* Convert UUID bin to UUID or GUID formated STRING */
413 uuid_bin_to_str(uuid_bin
, uuid_str
, str_format
);
416 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
417 int do_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
419 char uuid
[UUID_STR_LEN
+ 1];
422 if (!strcmp(argv
[0], "uuid"))
423 str_format
= UUID_STR_FORMAT_STD
;
425 str_format
= UUID_STR_FORMAT_GUID
;
428 return CMD_RET_USAGE
;
430 gen_rand_uuid_str(uuid
, str_format
);
433 printf("%s\n", uuid
);
435 env_set(argv
[1], uuid
);
437 return CMD_RET_SUCCESS
;
440 U_BOOT_CMD(uuid
, CONFIG_SYS_MAXARGS
, 1, do_uuid
,
441 "UUID - generate random Universally Unique Identifier",
444 "varname: for set result in a environment variable\n"
448 U_BOOT_CMD(guid
, CONFIG_SYS_MAXARGS
, 1, do_uuid
,
449 "GUID - generate Globally Unique Identifier based on random UUID",
452 "varname: for set result in a environment variable\n"
455 #endif /* CONFIG_CMD_UUID */
456 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */