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