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