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