Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2011 Calxeda, Inc. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 8 | #include <efi_api.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 9 | #include <env.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <rand.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 11 | #include <time.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 12 | #include <uuid.h> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 13 | #include <linux/ctype.h> |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <common.h> |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <part_efi.h> |
| 18 | #include <malloc.h> |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 19 | #include <dm/uclass.h> |
| 20 | #include <rng.h> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 23 | * UUID - Universally Unique IDentifier - 128 bits unique number. |
| 24 | * There are 5 versions and one variant of UUID defined by RFC4122 |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 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 |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 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. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 55 | * |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 56 | * UUID is also used as GUID (Globally Unique Identifier) with the same binary |
| 57 | * format but it differs in string format like below. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 58 | * |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 59 | * GUID: |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 60 | * 0 9 14 19 24 |
| 61 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 62 | * le le le be be |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 63 | * |
| 64 | * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 65 | */ |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 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 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 84 | if (i != UUID_STR_LEN || !valid) |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | |
| 87 | return 1; |
| 88 | } |
| 89 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 90 | static const struct { |
| 91 | const char *string; |
| 92 | efi_guid_t guid; |
| 93 | } list_guid[] = { |
Heinrich Schuchardt | 0a13b79 | 2022-01-16 11:55:39 +0100 | [diff] [blame] | 94 | #ifdef CONFIG_PARTITION_TYPE_GUID |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 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}, |
Rasmus Villemoes | dad0b63 | 2020-11-20 11:45:36 +0100 | [diff] [blame] | 102 | {"lvm", PARTITION_LINUX_LVM_GUID}, |
| 103 | {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT}, |
Heinrich Schuchardt | 0a13b79 | 2022-01-16 11:55:39 +0100 | [diff] [blame] | 104 | #endif |
Simon Glass | f7f5578 | 2023-03-20 08:30:12 +1300 | [diff] [blame] | 105 | #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI) |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 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 | { |
Heinrich Schuchardt | d2a7d12 | 2022-01-16 17:46:38 +0100 | [diff] [blame] | 187 | "TCG2", |
| 188 | EFI_TCG2_PROTOCOL_GUID, |
| 189 | }, |
| 190 | { |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 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 | }, |
Jose Marinho | ff72cb3 | 2021-12-23 14:51:07 +0000 | [diff] [blame] | 223 | { |
| 224 | "EFI Conformance Profiles Table", |
| 225 | EFI_CONFORMANCE_PROFILES_TABLE_GUID, |
| 226 | }, |
Heinrich Schuchardt | f4c5bcb | 2022-01-28 19:29:47 +0100 | [diff] [blame] | 227 | #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL |
| 228 | { |
| 229 | "RISC-V Boot", |
| 230 | RISCV_EFI_BOOT_PROTOCOL_GUID, |
| 231 | }, |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 232 | #endif |
Heinrich Schuchardt | f4c5bcb | 2022-01-28 19:29:47 +0100 | [diff] [blame] | 233 | #endif /* CONFIG_CMD_EFIDEBUG */ |
Heinrich Schuchardt | 1bebcb6 | 2022-01-16 14:53:37 +0100 | [diff] [blame] | 234 | #ifdef CONFIG_CMD_NVEDIT_EFI |
| 235 | /* signature database */ |
| 236 | { |
| 237 | "EFI_GLOBAL_VARIABLE_GUID", |
| 238 | EFI_GLOBAL_VARIABLE_GUID, |
| 239 | }, |
| 240 | { |
| 241 | "EFI_IMAGE_SECURITY_DATABASE_GUID", |
| 242 | EFI_IMAGE_SECURITY_DATABASE_GUID, |
| 243 | }, |
| 244 | /* certificate types */ |
| 245 | { |
| 246 | "EFI_CERT_SHA256_GUID", |
| 247 | EFI_CERT_SHA256_GUID, |
| 248 | }, |
| 249 | { |
| 250 | "EFI_CERT_X509_GUID", |
| 251 | EFI_CERT_X509_GUID, |
| 252 | }, |
| 253 | { |
| 254 | "EFI_CERT_TYPE_PKCS7_GUID", |
| 255 | EFI_CERT_TYPE_PKCS7_GUID, |
| 256 | }, |
| 257 | #endif |
Simon Glass | d134ad5 | 2023-03-20 08:30:11 +1300 | [diff] [blame] | 258 | #ifdef CONFIG_EFI |
| 259 | { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED }, |
| 260 | { "EFI_DXE_SERVICES", EFI_DXE_SERVICES }, |
| 261 | { "EFI_HOB_LIST", EFI_HOB_LIST }, |
| 262 | { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE }, |
| 263 | { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC }, |
| 264 | { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 }, |
| 265 | #endif |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | /* |
| 269 | * uuid_guid_get_bin() - this function get GUID bin for string |
| 270 | * |
| 271 | * @param guid_str - pointer to partition type string |
| 272 | * @param guid_bin - pointer to allocated array for big endian output [16B] |
| 273 | */ |
| 274 | int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin) |
| 275 | { |
| 276 | int i; |
| 277 | |
| 278 | for (i = 0; i < ARRAY_SIZE(list_guid); i++) { |
| 279 | if (!strcmp(list_guid[i].string, guid_str)) { |
| 280 | memcpy(guid_bin, &list_guid[i].guid, 16); |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | return -ENODEV; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * uuid_guid_get_str() - this function get string for GUID. |
| 289 | * |
| 290 | * @param guid_bin - pointer to string with partition type guid [16B] |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 291 | * |
| 292 | * Returns NULL if the type GUID is not known. |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 293 | */ |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 294 | const char *uuid_guid_get_str(const unsigned char *guid_bin) |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 295 | { |
| 296 | int i; |
| 297 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 298 | for (i = 0; i < ARRAY_SIZE(list_guid); i++) { |
| 299 | if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) { |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 300 | return list_guid[i].string; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 301 | } |
| 302 | } |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 303 | return NULL; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 304 | } |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 305 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 306 | /* |
| 307 | * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data. |
| 308 | * |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 309 | * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 310 | * @param uuid_bin - pointer to allocated array for big endian output [16B] |
| 311 | * @str_format - UUID string format: 0 - UUID; 1 - GUID |
| 312 | */ |
Simon Glass | 4acd4f0 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 313 | int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, |
| 314 | int str_format) |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 315 | { |
| 316 | uint16_t tmp16; |
| 317 | uint32_t tmp32; |
| 318 | uint64_t tmp64; |
| 319 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 320 | if (!uuid_str_valid(uuid_str)) { |
| 321 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 322 | if (!uuid_guid_get_bin(uuid_str, uuid_bin)) |
| 323 | return 0; |
| 324 | #endif |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 325 | return -EINVAL; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 326 | } |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 327 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 328 | if (str_format == UUID_STR_FORMAT_STD) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 329 | tmp32 = cpu_to_be32(hextoul(uuid_str, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 330 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 331 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 332 | tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 333 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 334 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 335 | tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 336 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 337 | } else { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 338 | tmp32 = cpu_to_le32(hextoul(uuid_str, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 339 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 340 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 341 | tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 342 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 343 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 344 | tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 345 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 346 | } |
| 347 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 348 | tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 349 | memcpy(uuid_bin + 8, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 350 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 351 | tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16)); |
| 352 | memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6); |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 357 | /* |
| 358 | * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. |
| 359 | * |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 360 | * @param uuid_bin: pointer to binary data of UUID (big endian) [16B] |
| 361 | * @param uuid_str: pointer to allocated array for output string [37B] |
| 362 | * @str_format: bit 0: 0 - UUID; 1 - GUID |
| 363 | * bit 1: 0 - lower case; 2 - upper case |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 364 | */ |
Simon Glass | 4acd4f0 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 365 | void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, |
| 366 | int str_format) |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 367 | { |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 368 | const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, |
| 369 | 9, 10, 11, 12, 13, 14, 15}; |
| 370 | const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8, |
| 371 | 9, 10, 11, 12, 13, 14, 15}; |
| 372 | const u8 *char_order; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 373 | const char *format; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 374 | int i; |
| 375 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 376 | /* |
| 377 | * UUID and GUID bin data - always in big endian: |
| 378 | * 4B-2B-2B-2B-6B |
| 379 | * be be be be be |
| 380 | */ |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 381 | if (str_format & UUID_STR_FORMAT_GUID) |
| 382 | char_order = guid_char_order; |
| 383 | else |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 384 | char_order = uuid_char_order; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 385 | if (str_format & UUID_STR_UPPER_CASE) |
| 386 | format = "%02X"; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 387 | else |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 388 | format = "%02x"; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 389 | |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 390 | for (i = 0; i < 16; i++) { |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 391 | sprintf(uuid_str, format, uuid_bin[char_order[i]]); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 392 | uuid_str += 2; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 393 | switch (i) { |
| 394 | case 3: |
| 395 | case 5: |
| 396 | case 7: |
| 397 | case 9: |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 398 | *uuid_str++ = '-'; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | } |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 402 | } |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 403 | |
| 404 | /* |
| 405 | * gen_rand_uuid() - this function generates a random binary UUID version 4. |
| 406 | * In this version all fields beside 4 bits of version and |
| 407 | * 2 bits of variant are randomly generated. |
| 408 | * |
| 409 | * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian. |
| 410 | */ |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 411 | #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID) |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 412 | void gen_rand_uuid(unsigned char *uuid_bin) |
| 413 | { |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 414 | u32 ptr[4]; |
| 415 | struct uuid *uuid = (struct uuid *)ptr; |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 416 | int i, ret; |
| 417 | struct udevice *devp; |
| 418 | u32 randv = 0; |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 419 | |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 420 | if (IS_ENABLED(CONFIG_DM_RNG)) { |
| 421 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
Patrick Delaunay | a6064f7 | 2021-10-22 17:05:47 +0200 | [diff] [blame] | 422 | if (!ret) { |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 423 | ret = dm_rng_read(devp, &randv, sizeof(randv)); |
| 424 | if (ret < 0) |
| 425 | randv = 0; |
| 426 | } |
| 427 | } |
| 428 | if (randv) |
| 429 | srand(randv); |
| 430 | else |
| 431 | srand(get_ticks() + rand()); |
Eugeniu Rosca | 9d74ba6 | 2019-05-02 14:27:06 +0200 | [diff] [blame] | 432 | |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 433 | /* Set all fields randomly */ |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 434 | for (i = 0; i < 4; i++) |
| 435 | ptr[i] = rand(); |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 436 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 437 | clrsetbits_be16(&uuid->time_hi_and_version, |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 438 | UUID_VERSION_MASK, |
| 439 | UUID_VERSION << UUID_VERSION_SHIFT); |
| 440 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 441 | clrsetbits_8(&uuid->clock_seq_hi_and_reserved, |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 442 | UUID_VARIANT_MASK, |
| 443 | UUID_VARIANT << UUID_VARIANT_SHIFT); |
| 444 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 445 | memcpy(uuid_bin, uuid, 16); |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
| 449 | * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string |
| 450 | * formats UUID or GUID. |
| 451 | * |
| 452 | * @param uuid_str - pointer to allocated array [37B]. |
| 453 | * @param - uuid output type: UUID - 0, GUID - 1 |
| 454 | */ |
| 455 | void gen_rand_uuid_str(char *uuid_str, int str_format) |
| 456 | { |
| 457 | unsigned char uuid_bin[UUID_BIN_LEN]; |
| 458 | |
| 459 | /* Generate UUID (big endian) */ |
| 460 | gen_rand_uuid(uuid_bin); |
| 461 | |
| 462 | /* Convert UUID bin to UUID or GUID formated STRING */ |
| 463 | uuid_bin_to_str(uuid_bin, uuid_str, str_format); |
| 464 | } |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 465 | |
Marek Vasut | 51d85f6 | 2019-01-07 21:23:38 +0100 | [diff] [blame] | 466 | #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 467 | int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 468 | { |
| 469 | char uuid[UUID_STR_LEN + 1]; |
| 470 | int str_format; |
| 471 | |
| 472 | if (!strcmp(argv[0], "uuid")) |
| 473 | str_format = UUID_STR_FORMAT_STD; |
| 474 | else |
| 475 | str_format = UUID_STR_FORMAT_GUID; |
| 476 | |
| 477 | if (argc > 2) |
| 478 | return CMD_RET_USAGE; |
| 479 | |
| 480 | gen_rand_uuid_str(uuid, str_format); |
| 481 | |
| 482 | if (argc == 1) |
| 483 | printf("%s\n", uuid); |
| 484 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 485 | env_set(argv[1], uuid); |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 486 | |
| 487 | return CMD_RET_SUCCESS; |
| 488 | } |
| 489 | |
| 490 | U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 491 | "UUID - generate random Universally Unique Identifier", |
| 492 | "[<varname>]\n" |
| 493 | "Argument:\n" |
| 494 | "varname: for set result in a environment variable\n" |
| 495 | "e.g. uuid uuid_env" |
| 496 | ); |
| 497 | |
| 498 | U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 499 | "GUID - generate Globally Unique Identifier based on random UUID", |
| 500 | "[<varname>]\n" |
| 501 | "Argument:\n" |
| 502 | "varname: for set result in a environment variable\n" |
| 503 | "e.g. guid guid_env" |
| 504 | ); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 505 | #endif /* CONFIG_CMD_UUID */ |
| 506 | #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */ |