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