blob: a1c88b9a62299eabd53636849b9174cfae948eeb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs6e39cce2011-08-23 11:06:56 +00002/*
3 * Copyright 2011 Calxeda, Inc.
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +01004 * 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 Hobbs6e39cce2011-08-23 11:06:56 +00008 */
9
Caleb Connollyfb9e9482024-08-30 13:34:36 +010010#ifndef USE_HOSTCC
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Heinrich Schuchardta4628b42022-01-16 14:10:23 +010012#include <efi_api.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <rand.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070015#include <time.h>
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020016#include <asm/io.h>
17#include <part_efi.h>
18#include <malloc.h>
Matthias Bruggerd03560b2020-12-18 10:28:03 +010019#include <dm/uclass.h>
20#include <rng.h>
Caleb Connollyfb9e9482024-08-30 13:34:36 +010021#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 Connolly29cab7c2024-08-30 13:34:37 +010032#include <u-boot/uuid.h>
Caleb Connolly2f4f0be2024-08-30 13:34:32 +010033#include <u-boot/sha1.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000034
Caleb Connollyfb9e9482024-08-30 13:34:36 +010035#ifdef USE_HOSTCC
36/* polyfill hextoul to avoid pulling in strto.c */
37#define hextoul(cp, endp) strtoul(cp, endp, 16)
Heinrich Schuchardt80e81d82024-11-03 23:42:21 +010038#define hextoull(cp, endp) strtoull(cp, endp, 16)
Caleb Connollyfb9e9482024-08-30 13:34:36 +010039#endif
40
Jason Hobbs6e39cce2011-08-23 11:06:56 +000041int 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 Marczak0c813362014-04-02 10:20:03 +020059 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000060 return 0;
61
62 return 1;
63}
64
Patrick Delaunay5d2451d2025-06-16 13:27:47 +020065/*
66 * Array of string (short and long) for known GUID of GPT partition type
67 * at least one string must be present, @type or @description
68 *
69 * @type : short name for the parameter 'type' of gpt command (max size UUID_STR_LEN = 36,
70 * no space), also used as fallback description when the next field is absent
71 * @description : long description associated to type GUID, used for %pUs
72 * @guid : known type GUID value
73 */
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010074static const struct {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +020075 const char *type;
76 const char *description;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010077 efi_guid_t guid;
78} list_guid[] = {
Caleb Connollyfb9e9482024-08-30 13:34:36 +010079#ifndef USE_HOSTCC
Patrick Delaunay5d2451d2025-06-16 13:27:47 +020080#if CONFIG_IS_ENABLED(EFI_PARTITION)
81 {"mbr", NULL, LEGACY_MBR_PARTITION_GUID},
82 {"msft", NULL, PARTITION_MSFT_RESERVED_GUID},
83 {"data", NULL, PARTITION_BASIC_DATA_GUID},
84 {"linux", NULL, PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
85 {"raid", NULL, PARTITION_LINUX_RAID_GUID},
86 {"swap", NULL, PARTITION_LINUX_SWAP_GUID},
87 {"lvm", NULL, PARTITION_LINUX_LVM_GUID},
88 {"u-boot-env", NULL, PARTITION_U_BOOT_ENVIRONMENT},
89 {"cros-kern", NULL, PARTITION_CROS_KERNEL},
90 {"cros-root", NULL, PARTITION_CROS_ROOT},
91 {"cros-fw", NULL, PARTITION_CROS_FIRMWARE},
92 {"cros-rsrv", NULL, PARTITION_CROS_RESERVED},
93 {
94 "system", "EFI System Partition",
95 PARTITION_SYSTEM_GUID,
96 },
Simon Glassf7f55782023-03-20 08:30:12 +130097#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Heinrich Schuchardta4628b42022-01-16 14:10:23 +010098 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +020099 NULL, "Device Path",
100 PARTITION_SYSTEM_GUID,
101 },
102 {
103 NULL, "Device Path",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100104 EFI_DEVICE_PATH_PROTOCOL_GUID,
105 },
106 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200107 NULL, "Device Path To Text",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100108 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
109 },
110 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200111 NULL, "Device Path Utilities",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100112 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
113 },
114 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200115 NULL, "Unicode Collation 2",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100116 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
117 },
118 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200119 NULL, "Driver Binding",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100120 EFI_DRIVER_BINDING_PROTOCOL_GUID,
121 },
122 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200123 NULL, "Simple Text Input",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100124 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
125 },
126 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200127 NULL, "Simple Text Input Ex",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100128 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
129 },
130 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200131 NULL, "Simple Text Output",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100132 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
133 },
134 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200135 NULL, "Block IO",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100136 EFI_BLOCK_IO_PROTOCOL_GUID,
137 },
138 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200139 NULL, "Disk IO",
Vincent Stehlée3cdedc2025-01-17 18:13:26 +0100140 EFI_DISK_IO_PROTOCOL_GUID,
141 },
142 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200143 NULL, "Simple File System",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100144 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
145 },
146 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200147 NULL, "Loaded Image",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100148 EFI_LOADED_IMAGE_PROTOCOL_GUID,
149 },
150 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200151 NULL, "Loaded Image Device Path",
Vincent Stehlée3cdedc2025-01-17 18:13:26 +0100152 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID,
153 },
154 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200155 NULL, "Graphics Output",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100156 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
157 },
158 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200159 NULL, "HII String",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100160 EFI_HII_STRING_PROTOCOL_GUID,
161 },
162 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200163 NULL, "HII Database",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100164 EFI_HII_DATABASE_PROTOCOL_GUID,
165 },
166 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200167 NULL, "HII Config Access",
Vincent Stehlée3cdedc2025-01-17 18:13:26 +0100168 EFI_HII_CONFIG_ACCESS_PROTOCOL_GUID,
169 },
170 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200171 NULL, "HII Config Routing",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100172 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
173 },
174 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200175 NULL, "Load File",
Vincent Stehlée3cdedc2025-01-17 18:13:26 +0100176 EFI_LOAD_FILE_PROTOCOL_GUID,
177 },
178 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200179 NULL, "Load File2",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100180 EFI_LOAD_FILE2_PROTOCOL_GUID,
181 },
182 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200183 NULL, "Random Number Generator",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100184 EFI_RNG_PROTOCOL_GUID,
185 },
186 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200187 NULL, "Simple Network",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100188 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
189 },
190 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200191 NULL, "PXE Base Code",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100192 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
193 },
194 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200195 NULL, "Device-Tree Fixup",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100196 EFI_DT_FIXUP_PROTOCOL_GUID,
197 },
198 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200199 NULL, "TCG2",
Heinrich Schuchardtd2a7d122022-01-16 17:46:38 +0100200 EFI_TCG2_PROTOCOL_GUID,
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100201 },
202 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200203 NULL, "Firmware Management",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100204 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
205 },
Heinrich Schuchardt06649fe2024-12-04 00:05:27 -0300206#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
207 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200208 NULL, "HTTP",
Heinrich Schuchardt06649fe2024-12-04 00:05:27 -0300209 EFI_HTTP_PROTOCOL_GUID,
210 },
211 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200212 NULL, "HTTP Service Binding",
Heinrich Schuchardt06649fe2024-12-04 00:05:27 -0300213 EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID,
214 },
215 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200216 NULL, "IPv4 Config2",
Heinrich Schuchardt06649fe2024-12-04 00:05:27 -0300217 EFI_IP4_CONFIG2_PROTOCOL_GUID,
218 },
219#endif
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100220 /* Configuration table GUIDs */
221 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200222 NULL, "ACPI table",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100223 EFI_ACPI_TABLE_GUID,
224 },
225 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200226 NULL, "EFI System Resource Table",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100227 EFI_SYSTEM_RESOURCE_TABLE_GUID,
228 },
229 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200230 NULL, "device tree",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100231 EFI_FDT_GUID,
232 },
233 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200234 NULL, "SMBIOS table",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100235 SMBIOS_TABLE_GUID,
236 },
237 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200238 NULL, "SMBIOS3 table",
Heinrich Schuchardtb36c9a02024-01-22 14:04:35 +0100239 SMBIOS3_TABLE_GUID,
240 },
241 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200242 NULL, "Runtime properties",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100243 EFI_RT_PROPERTIES_TABLE_GUID,
244 },
245 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200246 NULL, "TCG2 Final Events Table",
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100247 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
248 },
Jose Marinhoff72cb32021-12-23 14:51:07 +0000249 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200250 NULL, "EFI Conformance Profiles Table",
Jose Marinhoff72cb32021-12-23 14:51:07 +0000251 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
252 },
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100253#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
254 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200255 NULL, "RISC-V Boot",
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100256 RISCV_EFI_BOOT_PROTOCOL_GUID,
257 },
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100258#endif
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100259#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100260#ifdef CONFIG_CMD_NVEDIT_EFI
261 /* signature database */
262 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200263 "EFI_GLOBAL_VARIABLE_GUID", NULL,
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100264 EFI_GLOBAL_VARIABLE_GUID,
265 },
266 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200267 "EFI_IMAGE_SECURITY_DATABASE_GUID", NULL,
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100268 EFI_IMAGE_SECURITY_DATABASE_GUID,
269 },
270 /* certificate types */
271 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200272 "EFI_CERT_SHA256_GUID", NULL,
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100273 EFI_CERT_SHA256_GUID,
274 },
275 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200276 "EFI_CERT_X509_GUID", NULL,
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100277 EFI_CERT_X509_GUID,
278 },
279 {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200280 "EFI_CERT_TYPE_PKCS7_GUID", NULL,
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100281 EFI_CERT_TYPE_PKCS7_GUID,
282 },
283#endif
Simon Glass54d26e72023-07-15 21:38:51 -0600284#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200285 { "EFI_LZMA_COMPRESSED", NULL, EFI_LZMA_COMPRESSED },
286 { "EFI_DXE_SERVICES", NULL, EFI_DXE_SERVICES },
287 { "EFI_HOB_LIST", NULL, EFI_HOB_LIST },
288 { "EFI_MEMORY_TYPE", NULL, EFI_MEMORY_TYPE },
289 { "EFI_MEM_STATUS_CODE_REC", NULL, EFI_MEM_STATUS_CODE_REC },
290 { "EFI_GUID_EFI_ACPI1", NULL, EFI_GUID_EFI_ACPI1 },
Simon Glassd134ad52023-03-20 08:30:11 +1300291#endif
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200292#endif /* EFI_PARTITION */
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100293#endif /* !USE_HOSTCC */
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100294};
295
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100296int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
297{
298 int i;
299
300 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200301 if (list_guid[i].type &&
302 !strcmp(list_guid[i].type, guid_str)) {
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100303 memcpy(guid_bin, &list_guid[i].guid, 16);
304 return 0;
305 }
306 }
307 return -ENODEV;
308}
309
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100310const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100311{
312 int i;
313
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100314 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
315 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200316 if (list_guid[i].description)
317 return list_guid[i].description;
318 return list_guid[i].type;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100319 }
320 }
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100321 return NULL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100322}
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100323
Simon Glass4acd4f02020-04-08 08:32:58 -0600324int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
325 int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000326{
327 uint16_t tmp16;
328 uint32_t tmp32;
329 uint64_t tmp64;
330
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100331 if (!uuid_str_valid(uuid_str)) {
Patrick Delaunay5d2451d2025-06-16 13:27:47 +0200332 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID) &&
333 !uuid_guid_get_bin(uuid_str, uuid_bin))
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100334 return 0;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200335 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100336 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200337
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200338 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600339 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200340 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000341
Simon Glass3ff49ec2021-07-24 09:03:29 -0600342 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200343 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000344
Simon Glass3ff49ec2021-07-24 09:03:29 -0600345 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200346 memcpy(uuid_bin + 6, &tmp16, 2);
347 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600348 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200349 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000350
Simon Glass3ff49ec2021-07-24 09:03:29 -0600351 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200352 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000353
Simon Glass3ff49ec2021-07-24 09:03:29 -0600354 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200355 memcpy(uuid_bin + 6, &tmp16, 2);
356 }
357
Simon Glass3ff49ec2021-07-24 09:03:29 -0600358 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200359 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000360
Heinrich Schuchardt5833b052024-11-03 23:42:22 +0100361 tmp64 = cpu_to_be64(hextoull(uuid_str + 24, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200362 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200363
364 return 0;
365}
366
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100367int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
368{
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100369 uint16_t tmp16;
370 uint32_t tmp32;
371 uint64_t tmp64;
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100372
373 if (!uuid_str_valid(uuid_str) || !uuid_bin)
374 return -EINVAL;
375
376 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
377 memcpy(uuid_bin, &tmp32, 4);
378
379 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
380 memcpy(uuid_bin + 4, &tmp16, 2);
381
382 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
383 memcpy(uuid_bin + 6, &tmp16, 2);
384
385 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
386 memcpy(uuid_bin + 8, &tmp16, 2);
387
Heinrich Schuchardt80e81d82024-11-03 23:42:21 +0100388 tmp64 = cpu_to_le64(hextoull(uuid_str + 24, NULL));
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100389 memcpy(uuid_bin + 10, &tmp64, 6);
390
391 return 0;
392}
393
Simon Glass4acd4f02020-04-08 08:32:58 -0600394void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
395 int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200396{
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100397 const uint8_t uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200398 9, 10, 11, 12, 13, 14, 15};
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100399 const uint8_t guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200400 9, 10, 11, 12, 13, 14, 15};
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100401 const uint8_t *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200402 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200403 int i;
404
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200405 /*
406 * UUID and GUID bin data - always in big endian:
407 * 4B-2B-2B-2B-6B
408 * be be be be be
409 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200410 if (str_format & UUID_STR_FORMAT_GUID)
411 char_order = guid_char_order;
412 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200413 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200414 if (str_format & UUID_STR_UPPER_CASE)
415 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200416 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200417 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200418
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200419 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200420 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200421 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200422 switch (i) {
423 case 3:
424 case 5:
425 case 7:
426 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200427 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200428 break;
429 }
430 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000431}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200432
Caleb Connolly2f4f0be2024-08-30 13:34:32 +0100433static void configure_uuid(struct uuid *uuid, unsigned char version)
434{
435 uint16_t tmp;
436
437 /* Configure variant/version bits */
438 tmp = be16_to_cpu(uuid->time_hi_and_version);
439 tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT);
440 uuid->time_hi_and_version = cpu_to_be16(tmp);
441
442 uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK;
443 uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
444}
445
446void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...)
447{
448 sha1_context ctx;
449 va_list args;
450 const uint8_t *data;
451 uint32_t *tmp32;
452 uint16_t *tmp16;
453 uint8_t hash[SHA1_SUM_LEN];
454
455 sha1_starts(&ctx);
456 /* Hash the namespace UUID as salt */
457 sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
458 va_start(args, guid);
459
460 while ((data = va_arg(args, const uint8_t *))) {
461 unsigned int len = va_arg(args, size_t);
462
463 sha1_update(&ctx, data, len);
464 }
465
466 va_end(args);
467 sha1_finish(&ctx, hash);
468
469 /* Truncate the hash into output UUID, it is already big endian */
470 memcpy(guid, hash, sizeof(*guid));
471
472 configure_uuid((struct uuid *)guid, 5);
473
474 /* Make little endian */
475 tmp32 = (uint32_t *)&guid->b[0];
476 *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32));
477 tmp16 = (uint16_t *)&guid->b[4];
478 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
479 tmp16 = (uint16_t *)&guid->b[6];
480 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
481}
482
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100483#ifndef USE_HOSTCC
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200484#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200485void gen_rand_uuid(unsigned char *uuid_bin)
486{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200487 u32 ptr[4];
488 struct uuid *uuid = (struct uuid *)ptr;
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100489 int i, ret;
490 struct udevice *devp;
491 u32 randv = 0;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200492
Marek Vasut0d871e72024-04-26 01:02:07 +0200493 if (CONFIG_IS_ENABLED(DM_RNG)) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100494 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunaya6064f72021-10-22 17:05:47 +0200495 if (!ret) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100496 ret = dm_rng_read(devp, &randv, sizeof(randv));
497 if (ret < 0)
498 randv = 0;
499 }
500 }
501 if (randv)
502 srand(randv);
503 else
504 srand(get_ticks() + rand());
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200505
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200506 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200507 for (i = 0; i < 4; i++)
508 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200509
Caleb Connolly2f4f0be2024-08-30 13:34:32 +0100510 configure_uuid(uuid, UUID_VERSION);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200511
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200512 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200513}
514
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200515void gen_rand_uuid_str(char *uuid_str, int str_format)
516{
517 unsigned char uuid_bin[UUID_BIN_LEN];
518
519 /* Generate UUID (big endian) */
520 gen_rand_uuid(uuid_bin);
521
522 /* Convert UUID bin to UUID or GUID formated STRING */
523 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
524}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200525
Simon Glass0e84d962024-09-29 19:49:50 -0600526#if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glassed38aef2020-05-10 11:40:03 -0600527int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200528{
529 char uuid[UUID_STR_LEN + 1];
530 int str_format;
531
532 if (!strcmp(argv[0], "uuid"))
533 str_format = UUID_STR_FORMAT_STD;
534 else
535 str_format = UUID_STR_FORMAT_GUID;
536
537 if (argc > 2)
538 return CMD_RET_USAGE;
539
540 gen_rand_uuid_str(uuid, str_format);
541
542 if (argc == 1)
543 printf("%s\n", uuid);
544 else
Simon Glass6a38e412017-08-03 12:22:09 -0600545 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200546
547 return CMD_RET_SUCCESS;
548}
549
550U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
551 "UUID - generate random Universally Unique Identifier",
552 "[<varname>]\n"
553 "Argument:\n"
554 "varname: for set result in a environment variable\n"
555 "e.g. uuid uuid_env"
556);
557
558U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
559 "GUID - generate Globally Unique Identifier based on random UUID",
560 "[<varname>]\n"
561 "Argument:\n"
562 "varname: for set result in a environment variable\n"
563 "e.g. guid guid_env"
564);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200565#endif /* CONFIG_CMD_UUID */
566#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100567#endif /* !USE_HOSTCC */