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. |
Abdellatif El Khlifi | dc3cb5d | 2023-08-04 14:33:38 +0100 | [diff] [blame] | 4 | * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 5 | * |
| 6 | * Authors: |
| 7 | * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Simon Glass | 21fcfd8 | 2023-08-24 13:55:44 -0600 | [diff] [blame] | 10 | #define LOG_CATEGOT LOGC_CORE |
| 11 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 12 | #include <command.h> |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 13 | #include <efi_api.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 15 | #include <rand.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 16 | #include <time.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 17 | #include <uuid.h> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 18 | #include <linux/ctype.h> |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <part_efi.h> |
| 22 | #include <malloc.h> |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 23 | #include <dm/uclass.h> |
| 24 | #include <rng.h> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 25 | |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 26 | int uuid_str_valid(const char *uuid) |
| 27 | { |
| 28 | int i, valid; |
| 29 | |
| 30 | if (uuid == NULL) |
| 31 | return 0; |
| 32 | |
| 33 | for (i = 0, valid = 1; uuid[i] && valid; i++) { |
| 34 | switch (i) { |
| 35 | case 8: case 13: case 18: case 23: |
| 36 | valid = (uuid[i] == '-'); |
| 37 | break; |
| 38 | default: |
| 39 | valid = isxdigit(uuid[i]); |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 44 | if (i != UUID_STR_LEN || !valid) |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | |
| 47 | return 1; |
| 48 | } |
| 49 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 50 | static const struct { |
| 51 | const char *string; |
| 52 | efi_guid_t guid; |
| 53 | } list_guid[] = { |
Heinrich Schuchardt | 0a13b79 | 2022-01-16 11:55:39 +0100 | [diff] [blame] | 54 | #ifdef CONFIG_PARTITION_TYPE_GUID |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 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}, |
Rasmus Villemoes | dad0b63 | 2020-11-20 11:45:36 +0100 | [diff] [blame] | 62 | {"lvm", PARTITION_LINUX_LVM_GUID}, |
| 63 | {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT}, |
Simon Glass | 21fcfd8 | 2023-08-24 13:55:44 -0600 | [diff] [blame] | 64 | {"cros-kern", PARTITION_CROS_KERNEL}, |
| 65 | {"cros-root", PARTITION_CROS_ROOT}, |
| 66 | {"cros-fw", PARTITION_CROS_FIRMWARE}, |
| 67 | {"cros-rsrv", PARTITION_CROS_RESERVED}, |
Heinrich Schuchardt | 0a13b79 | 2022-01-16 11:55:39 +0100 | [diff] [blame] | 68 | #endif |
Simon Glass | f7f5578 | 2023-03-20 08:30:12 +1300 | [diff] [blame] | 69 | #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI) |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 70 | { |
| 71 | "Device Path", |
| 72 | EFI_DEVICE_PATH_PROTOCOL_GUID, |
| 73 | }, |
| 74 | { |
| 75 | "Device Path To Text", |
| 76 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, |
| 77 | }, |
| 78 | { |
| 79 | "Device Path Utilities", |
| 80 | EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID, |
| 81 | }, |
| 82 | { |
| 83 | "Unicode Collation 2", |
| 84 | EFI_UNICODE_COLLATION_PROTOCOL2_GUID, |
| 85 | }, |
| 86 | { |
| 87 | "Driver Binding", |
| 88 | EFI_DRIVER_BINDING_PROTOCOL_GUID, |
| 89 | }, |
| 90 | { |
| 91 | "Simple Text Input", |
| 92 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, |
| 93 | }, |
| 94 | { |
| 95 | "Simple Text Input Ex", |
| 96 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, |
| 97 | }, |
| 98 | { |
| 99 | "Simple Text Output", |
| 100 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, |
| 101 | }, |
| 102 | { |
| 103 | "Block IO", |
| 104 | EFI_BLOCK_IO_PROTOCOL_GUID, |
| 105 | }, |
| 106 | { |
| 107 | "Simple File System", |
| 108 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, |
| 109 | }, |
| 110 | { |
| 111 | "Loaded Image", |
| 112 | EFI_LOADED_IMAGE_PROTOCOL_GUID, |
| 113 | }, |
| 114 | { |
| 115 | "Graphics Output", |
| 116 | EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, |
| 117 | }, |
| 118 | { |
| 119 | "HII String", |
| 120 | EFI_HII_STRING_PROTOCOL_GUID, |
| 121 | }, |
| 122 | { |
| 123 | "HII Database", |
| 124 | EFI_HII_DATABASE_PROTOCOL_GUID, |
| 125 | }, |
| 126 | { |
| 127 | "HII Config Routing", |
| 128 | EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, |
| 129 | }, |
| 130 | { |
| 131 | "Load File2", |
| 132 | EFI_LOAD_FILE2_PROTOCOL_GUID, |
| 133 | }, |
| 134 | { |
| 135 | "Random Number Generator", |
| 136 | EFI_RNG_PROTOCOL_GUID, |
| 137 | }, |
| 138 | { |
| 139 | "Simple Network", |
| 140 | EFI_SIMPLE_NETWORK_PROTOCOL_GUID, |
| 141 | }, |
| 142 | { |
| 143 | "PXE Base Code", |
| 144 | EFI_PXE_BASE_CODE_PROTOCOL_GUID, |
| 145 | }, |
| 146 | { |
| 147 | "Device-Tree Fixup", |
| 148 | EFI_DT_FIXUP_PROTOCOL_GUID, |
| 149 | }, |
| 150 | { |
Heinrich Schuchardt | d2a7d12 | 2022-01-16 17:46:38 +0100 | [diff] [blame] | 151 | "TCG2", |
| 152 | EFI_TCG2_PROTOCOL_GUID, |
| 153 | }, |
| 154 | { |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 155 | "System Partition", |
| 156 | PARTITION_SYSTEM_GUID |
| 157 | }, |
| 158 | { |
| 159 | "Firmware Management", |
| 160 | EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID |
| 161 | }, |
| 162 | /* Configuration table GUIDs */ |
| 163 | { |
| 164 | "ACPI table", |
| 165 | EFI_ACPI_TABLE_GUID, |
| 166 | }, |
| 167 | { |
| 168 | "EFI System Resource Table", |
| 169 | EFI_SYSTEM_RESOURCE_TABLE_GUID, |
| 170 | }, |
| 171 | { |
| 172 | "device tree", |
| 173 | EFI_FDT_GUID, |
| 174 | }, |
| 175 | { |
| 176 | "SMBIOS table", |
| 177 | SMBIOS_TABLE_GUID, |
| 178 | }, |
| 179 | { |
| 180 | "Runtime properties", |
| 181 | EFI_RT_PROPERTIES_TABLE_GUID, |
| 182 | }, |
| 183 | { |
| 184 | "TCG2 Final Events Table", |
| 185 | EFI_TCG2_FINAL_EVENTS_TABLE_GUID, |
| 186 | }, |
Jose Marinho | ff72cb3 | 2021-12-23 14:51:07 +0000 | [diff] [blame] | 187 | { |
| 188 | "EFI Conformance Profiles Table", |
| 189 | EFI_CONFORMANCE_PROFILES_TABLE_GUID, |
| 190 | }, |
Heinrich Schuchardt | f4c5bcb | 2022-01-28 19:29:47 +0100 | [diff] [blame] | 191 | #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL |
| 192 | { |
| 193 | "RISC-V Boot", |
| 194 | RISCV_EFI_BOOT_PROTOCOL_GUID, |
| 195 | }, |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 196 | #endif |
Heinrich Schuchardt | f4c5bcb | 2022-01-28 19:29:47 +0100 | [diff] [blame] | 197 | #endif /* CONFIG_CMD_EFIDEBUG */ |
Heinrich Schuchardt | 1bebcb6 | 2022-01-16 14:53:37 +0100 | [diff] [blame] | 198 | #ifdef CONFIG_CMD_NVEDIT_EFI |
| 199 | /* signature database */ |
| 200 | { |
| 201 | "EFI_GLOBAL_VARIABLE_GUID", |
| 202 | EFI_GLOBAL_VARIABLE_GUID, |
| 203 | }, |
| 204 | { |
| 205 | "EFI_IMAGE_SECURITY_DATABASE_GUID", |
| 206 | EFI_IMAGE_SECURITY_DATABASE_GUID, |
| 207 | }, |
| 208 | /* certificate types */ |
| 209 | { |
| 210 | "EFI_CERT_SHA256_GUID", |
| 211 | EFI_CERT_SHA256_GUID, |
| 212 | }, |
| 213 | { |
| 214 | "EFI_CERT_X509_GUID", |
| 215 | EFI_CERT_X509_GUID, |
| 216 | }, |
| 217 | { |
| 218 | "EFI_CERT_TYPE_PKCS7_GUID", |
| 219 | EFI_CERT_TYPE_PKCS7_GUID, |
| 220 | }, |
| 221 | #endif |
Simon Glass | 54d26e7 | 2023-07-15 21:38:51 -0600 | [diff] [blame] | 222 | #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI) |
Simon Glass | d134ad5 | 2023-03-20 08:30:11 +1300 | [diff] [blame] | 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 }, |
| 229 | #endif |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 230 | }; |
| 231 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 232 | int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin) |
| 233 | { |
| 234 | int i; |
| 235 | |
| 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); |
| 239 | return 0; |
| 240 | } |
| 241 | } |
| 242 | return -ENODEV; |
| 243 | } |
| 244 | |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 245 | const char *uuid_guid_get_str(const unsigned char *guid_bin) |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 246 | { |
| 247 | int i; |
| 248 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 249 | for (i = 0; i < ARRAY_SIZE(list_guid); i++) { |
| 250 | if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) { |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 251 | return list_guid[i].string; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 254 | return NULL; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 255 | } |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 256 | |
Simon Glass | 4acd4f0 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 257 | int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, |
| 258 | int str_format) |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 259 | { |
| 260 | uint16_t tmp16; |
| 261 | uint32_t tmp32; |
| 262 | uint64_t tmp64; |
| 263 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 264 | if (!uuid_str_valid(uuid_str)) { |
Simon Glass | 21fcfd8 | 2023-08-24 13:55:44 -0600 | [diff] [blame] | 265 | log_debug("not valid\n"); |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 266 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 267 | if (!uuid_guid_get_bin(uuid_str, uuid_bin)) |
| 268 | return 0; |
| 269 | #endif |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 270 | return -EINVAL; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 271 | } |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 272 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 273 | if (str_format == UUID_STR_FORMAT_STD) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 274 | tmp32 = cpu_to_be32(hextoul(uuid_str, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 275 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 276 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 277 | tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 278 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 279 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 280 | tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 281 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 282 | } else { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 283 | tmp32 = cpu_to_le32(hextoul(uuid_str, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 284 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 285 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 286 | tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 287 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 288 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 289 | tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 290 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 291 | } |
| 292 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 293 | tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 294 | memcpy(uuid_bin + 8, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 295 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 296 | tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16)); |
| 297 | memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6); |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Abdellatif El Khlifi | dc3cb5d | 2023-08-04 14:33:38 +0100 | [diff] [blame] | 302 | int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin) |
| 303 | { |
| 304 | u16 tmp16; |
| 305 | u32 tmp32; |
| 306 | u64 tmp64; |
| 307 | |
| 308 | if (!uuid_str_valid(uuid_str) || !uuid_bin) |
| 309 | return -EINVAL; |
| 310 | |
| 311 | tmp32 = cpu_to_le32(hextoul(uuid_str, NULL)); |
| 312 | memcpy(uuid_bin, &tmp32, 4); |
| 313 | |
| 314 | tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL)); |
| 315 | memcpy(uuid_bin + 4, &tmp16, 2); |
| 316 | |
| 317 | tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL)); |
| 318 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 319 | |
| 320 | tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL)); |
| 321 | memcpy(uuid_bin + 8, &tmp16, 2); |
| 322 | |
| 323 | tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16)); |
| 324 | memcpy(uuid_bin + 10, &tmp64, 6); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Simon Glass | 4acd4f0 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 329 | void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, |
| 330 | int str_format) |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 331 | { |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 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; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 337 | const char *format; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 338 | int i; |
| 339 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 340 | /* |
| 341 | * UUID and GUID bin data - always in big endian: |
| 342 | * 4B-2B-2B-2B-6B |
| 343 | * be be be be be |
| 344 | */ |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 345 | if (str_format & UUID_STR_FORMAT_GUID) |
| 346 | char_order = guid_char_order; |
| 347 | else |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 348 | char_order = uuid_char_order; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 349 | if (str_format & UUID_STR_UPPER_CASE) |
| 350 | format = "%02X"; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 351 | else |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 352 | format = "%02x"; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 353 | |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 354 | for (i = 0; i < 16; i++) { |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 355 | sprintf(uuid_str, format, uuid_bin[char_order[i]]); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 356 | uuid_str += 2; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 357 | switch (i) { |
| 358 | case 3: |
| 359 | case 5: |
| 360 | case 7: |
| 361 | case 9: |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 362 | *uuid_str++ = '-'; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | } |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 366 | } |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 367 | |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 368 | #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID) |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 369 | void gen_rand_uuid(unsigned char *uuid_bin) |
| 370 | { |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 371 | u32 ptr[4]; |
| 372 | struct uuid *uuid = (struct uuid *)ptr; |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 373 | int i, ret; |
| 374 | struct udevice *devp; |
| 375 | u32 randv = 0; |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 376 | |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 377 | if (IS_ENABLED(CONFIG_DM_RNG)) { |
| 378 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
Patrick Delaunay | a6064f7 | 2021-10-22 17:05:47 +0200 | [diff] [blame] | 379 | if (!ret) { |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 380 | ret = dm_rng_read(devp, &randv, sizeof(randv)); |
| 381 | if (ret < 0) |
| 382 | randv = 0; |
| 383 | } |
| 384 | } |
| 385 | if (randv) |
| 386 | srand(randv); |
| 387 | else |
| 388 | srand(get_ticks() + rand()); |
Eugeniu Rosca | 9d74ba6 | 2019-05-02 14:27:06 +0200 | [diff] [blame] | 389 | |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 390 | /* Set all fields randomly */ |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 391 | for (i = 0; i < 4; i++) |
| 392 | ptr[i] = rand(); |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 393 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 394 | clrsetbits_be16(&uuid->time_hi_and_version, |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 395 | UUID_VERSION_MASK, |
| 396 | UUID_VERSION << UUID_VERSION_SHIFT); |
| 397 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 398 | clrsetbits_8(&uuid->clock_seq_hi_and_reserved, |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 399 | UUID_VARIANT_MASK, |
| 400 | UUID_VARIANT << UUID_VARIANT_SHIFT); |
| 401 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 402 | memcpy(uuid_bin, uuid, 16); |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 405 | void gen_rand_uuid_str(char *uuid_str, int str_format) |
| 406 | { |
| 407 | unsigned char uuid_bin[UUID_BIN_LEN]; |
| 408 | |
| 409 | /* Generate UUID (big endian) */ |
| 410 | gen_rand_uuid(uuid_bin); |
| 411 | |
| 412 | /* Convert UUID bin to UUID or GUID formated STRING */ |
| 413 | uuid_bin_to_str(uuid_bin, uuid_str, str_format); |
| 414 | } |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 415 | |
Marek Vasut | 51d85f6 | 2019-01-07 21:23:38 +0100 | [diff] [blame] | 416 | #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 417 | 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] | 418 | { |
| 419 | char uuid[UUID_STR_LEN + 1]; |
| 420 | int str_format; |
| 421 | |
| 422 | if (!strcmp(argv[0], "uuid")) |
| 423 | str_format = UUID_STR_FORMAT_STD; |
| 424 | else |
| 425 | str_format = UUID_STR_FORMAT_GUID; |
| 426 | |
| 427 | if (argc > 2) |
| 428 | return CMD_RET_USAGE; |
| 429 | |
| 430 | gen_rand_uuid_str(uuid, str_format); |
| 431 | |
| 432 | if (argc == 1) |
| 433 | printf("%s\n", uuid); |
| 434 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 435 | env_set(argv[1], uuid); |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 436 | |
| 437 | return CMD_RET_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 441 | "UUID - generate random Universally Unique Identifier", |
| 442 | "[<varname>]\n" |
| 443 | "Argument:\n" |
| 444 | "varname: for set result in a environment variable\n" |
| 445 | "e.g. uuid uuid_env" |
| 446 | ); |
| 447 | |
| 448 | U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 449 | "GUID - generate Globally Unique Identifier based on random UUID", |
| 450 | "[<varname>]\n" |
| 451 | "Argument:\n" |
| 452 | "varname: for set result in a environment variable\n" |
| 453 | "e.g. guid guid_env" |
| 454 | ); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 455 | #endif /* CONFIG_CMD_UUID */ |
| 456 | #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */ |