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