blob: 3bdb20b0e5b6ac669cc501e77634fd692a3348ed [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
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +020010#include <common.h>
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>
Simon Glass6f044982020-05-10 11:39:52 -060016#include <uuid.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000017#include <linux/ctype.h>
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020018#include <errno.h>
19#include <common.h>
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020020#include <asm/io.h>
21#include <part_efi.h>
22#include <malloc.h>
Matthias Bruggerd03560b2020-12-18 10:28:03 +010023#include <dm/uclass.h>
24#include <rng.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000025
Jason Hobbs6e39cce2011-08-23 11:06:56 +000026int uuid_str_valid(const char *uuid)
27{
28 int i, valid;
29
30 if (uuid == NULL)
31 return 0;
32
33 for (i = 0, valid = 1; uuid[i] && valid; i++) {
34 switch (i) {
35 case 8: case 13: case 18: case 23:
36 valid = (uuid[i] == '-');
37 break;
38 default:
39 valid = isxdigit(uuid[i]);
40 break;
41 }
42 }
43
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020044 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000045 return 0;
46
47 return 1;
48}
49
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010050static const struct {
51 const char *string;
52 efi_guid_t guid;
53} list_guid[] = {
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +010054#ifdef CONFIG_PARTITION_TYPE_GUID
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010055 {"system", PARTITION_SYSTEM_GUID},
56 {"mbr", LEGACY_MBR_PARTITION_GUID},
57 {"msft", PARTITION_MSFT_RESERVED_GUID},
58 {"data", PARTITION_BASIC_DATA_GUID},
59 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
60 {"raid", PARTITION_LINUX_RAID_GUID},
61 {"swap", PARTITION_LINUX_SWAP_GUID},
Rasmus Villemoesdad0b632020-11-20 11:45:36 +010062 {"lvm", PARTITION_LINUX_LVM_GUID},
63 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +010064#endif
Simon Glassf7f55782023-03-20 08:30:12 +130065#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Heinrich Schuchardta4628b42022-01-16 14:10:23 +010066 {
67 "Device Path",
68 EFI_DEVICE_PATH_PROTOCOL_GUID,
69 },
70 {
71 "Device Path To Text",
72 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
73 },
74 {
75 "Device Path Utilities",
76 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
77 },
78 {
79 "Unicode Collation 2",
80 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
81 },
82 {
83 "Driver Binding",
84 EFI_DRIVER_BINDING_PROTOCOL_GUID,
85 },
86 {
87 "Simple Text Input",
88 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
89 },
90 {
91 "Simple Text Input Ex",
92 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
93 },
94 {
95 "Simple Text Output",
96 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
97 },
98 {
99 "Block IO",
100 EFI_BLOCK_IO_PROTOCOL_GUID,
101 },
102 {
103 "Simple File System",
104 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
105 },
106 {
107 "Loaded Image",
108 EFI_LOADED_IMAGE_PROTOCOL_GUID,
109 },
110 {
111 "Graphics Output",
112 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
113 },
114 {
115 "HII String",
116 EFI_HII_STRING_PROTOCOL_GUID,
117 },
118 {
119 "HII Database",
120 EFI_HII_DATABASE_PROTOCOL_GUID,
121 },
122 {
123 "HII Config Routing",
124 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
125 },
126 {
127 "Load File2",
128 EFI_LOAD_FILE2_PROTOCOL_GUID,
129 },
130 {
131 "Random Number Generator",
132 EFI_RNG_PROTOCOL_GUID,
133 },
134 {
135 "Simple Network",
136 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
137 },
138 {
139 "PXE Base Code",
140 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
141 },
142 {
143 "Device-Tree Fixup",
144 EFI_DT_FIXUP_PROTOCOL_GUID,
145 },
146 {
Heinrich Schuchardtd2a7d122022-01-16 17:46:38 +0100147 "TCG2",
148 EFI_TCG2_PROTOCOL_GUID,
149 },
150 {
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100151 "System Partition",
152 PARTITION_SYSTEM_GUID
153 },
154 {
155 "Firmware Management",
156 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
157 },
158 /* Configuration table GUIDs */
159 {
160 "ACPI table",
161 EFI_ACPI_TABLE_GUID,
162 },
163 {
164 "EFI System Resource Table",
165 EFI_SYSTEM_RESOURCE_TABLE_GUID,
166 },
167 {
168 "device tree",
169 EFI_FDT_GUID,
170 },
171 {
172 "SMBIOS table",
173 SMBIOS_TABLE_GUID,
174 },
175 {
176 "Runtime properties",
177 EFI_RT_PROPERTIES_TABLE_GUID,
178 },
179 {
180 "TCG2 Final Events Table",
181 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
182 },
Jose Marinhoff72cb32021-12-23 14:51:07 +0000183 {
184 "EFI Conformance Profiles Table",
185 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
186 },
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100187#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
188 {
189 "RISC-V Boot",
190 RISCV_EFI_BOOT_PROTOCOL_GUID,
191 },
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100192#endif
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100193#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100194#ifdef CONFIG_CMD_NVEDIT_EFI
195 /* signature database */
196 {
197 "EFI_GLOBAL_VARIABLE_GUID",
198 EFI_GLOBAL_VARIABLE_GUID,
199 },
200 {
201 "EFI_IMAGE_SECURITY_DATABASE_GUID",
202 EFI_IMAGE_SECURITY_DATABASE_GUID,
203 },
204 /* certificate types */
205 {
206 "EFI_CERT_SHA256_GUID",
207 EFI_CERT_SHA256_GUID,
208 },
209 {
210 "EFI_CERT_X509_GUID",
211 EFI_CERT_X509_GUID,
212 },
213 {
214 "EFI_CERT_TYPE_PKCS7_GUID",
215 EFI_CERT_TYPE_PKCS7_GUID,
216 },
217#endif
Simon Glass54d26e72023-07-15 21:38:51 -0600218#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Simon Glassd134ad52023-03-20 08:30:11 +1300219 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
220 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
221 { "EFI_HOB_LIST", EFI_HOB_LIST },
222 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
223 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
224 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
225#endif
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100226};
227
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100228int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
229{
230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
233 if (!strcmp(list_guid[i].string, guid_str)) {
234 memcpy(guid_bin, &list_guid[i].guid, 16);
235 return 0;
236 }
237 }
238 return -ENODEV;
239}
240
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100241const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100242{
243 int i;
244
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100245 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
246 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100247 return list_guid[i].string;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100248 }
249 }
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100250 return NULL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100251}
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100252
Simon Glass4acd4f02020-04-08 08:32:58 -0600253int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
254 int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000255{
256 uint16_t tmp16;
257 uint32_t tmp32;
258 uint64_t tmp64;
259
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100260 if (!uuid_str_valid(uuid_str)) {
261#ifdef CONFIG_PARTITION_TYPE_GUID
262 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
263 return 0;
264#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200265 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100266 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200267
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200268 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600269 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200270 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000271
Simon Glass3ff49ec2021-07-24 09:03:29 -0600272 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200273 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000274
Simon Glass3ff49ec2021-07-24 09:03:29 -0600275 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200276 memcpy(uuid_bin + 6, &tmp16, 2);
277 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600278 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200279 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000280
Simon Glass3ff49ec2021-07-24 09:03:29 -0600281 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200282 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000283
Simon Glass3ff49ec2021-07-24 09:03:29 -0600284 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200285 memcpy(uuid_bin + 6, &tmp16, 2);
286 }
287
Simon Glass3ff49ec2021-07-24 09:03:29 -0600288 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200289 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000290
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200291 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
292 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200293
294 return 0;
295}
296
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100297int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
298{
299 u16 tmp16;
300 u32 tmp32;
301 u64 tmp64;
302
303 if (!uuid_str_valid(uuid_str) || !uuid_bin)
304 return -EINVAL;
305
306 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
307 memcpy(uuid_bin, &tmp32, 4);
308
309 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
310 memcpy(uuid_bin + 4, &tmp16, 2);
311
312 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
313 memcpy(uuid_bin + 6, &tmp16, 2);
314
315 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
316 memcpy(uuid_bin + 8, &tmp16, 2);
317
318 tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
319 memcpy(uuid_bin + 10, &tmp64, 6);
320
321 return 0;
322}
323
Simon Glass4acd4f02020-04-08 08:32:58 -0600324void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
325 int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200326{
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200327 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
328 9, 10, 11, 12, 13, 14, 15};
329 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
330 9, 10, 11, 12, 13, 14, 15};
331 const u8 *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200332 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200333 int i;
334
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200335 /*
336 * UUID and GUID bin data - always in big endian:
337 * 4B-2B-2B-2B-6B
338 * be be be be be
339 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200340 if (str_format & UUID_STR_FORMAT_GUID)
341 char_order = guid_char_order;
342 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200343 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200344 if (str_format & UUID_STR_UPPER_CASE)
345 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200346 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200347 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200348
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200349 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200350 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200351 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200352 switch (i) {
353 case 3:
354 case 5:
355 case 7:
356 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200357 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200358 break;
359 }
360 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000361}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200362
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200363#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200364void gen_rand_uuid(unsigned char *uuid_bin)
365{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200366 u32 ptr[4];
367 struct uuid *uuid = (struct uuid *)ptr;
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100368 int i, ret;
369 struct udevice *devp;
370 u32 randv = 0;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200371
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100372 if (IS_ENABLED(CONFIG_DM_RNG)) {
373 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunaya6064f72021-10-22 17:05:47 +0200374 if (!ret) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100375 ret = dm_rng_read(devp, &randv, sizeof(randv));
376 if (ret < 0)
377 randv = 0;
378 }
379 }
380 if (randv)
381 srand(randv);
382 else
383 srand(get_ticks() + rand());
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200384
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200385 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200386 for (i = 0; i < 4; i++)
387 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200388
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200389 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200390 UUID_VERSION_MASK,
391 UUID_VERSION << UUID_VERSION_SHIFT);
392
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200393 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200394 UUID_VARIANT_MASK,
395 UUID_VARIANT << UUID_VARIANT_SHIFT);
396
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200397 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200398}
399
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200400void gen_rand_uuid_str(char *uuid_str, int str_format)
401{
402 unsigned char uuid_bin[UUID_BIN_LEN];
403
404 /* Generate UUID (big endian) */
405 gen_rand_uuid(uuid_bin);
406
407 /* Convert UUID bin to UUID or GUID formated STRING */
408 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
409}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200410
Marek Vasut51d85f62019-01-07 21:23:38 +0100411#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glassed38aef2020-05-10 11:40:03 -0600412int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200413{
414 char uuid[UUID_STR_LEN + 1];
415 int str_format;
416
417 if (!strcmp(argv[0], "uuid"))
418 str_format = UUID_STR_FORMAT_STD;
419 else
420 str_format = UUID_STR_FORMAT_GUID;
421
422 if (argc > 2)
423 return CMD_RET_USAGE;
424
425 gen_rand_uuid_str(uuid, str_format);
426
427 if (argc == 1)
428 printf("%s\n", uuid);
429 else
Simon Glass6a38e412017-08-03 12:22:09 -0600430 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200431
432 return CMD_RET_SUCCESS;
433}
434
435U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
436 "UUID - generate random Universally Unique Identifier",
437 "[<varname>]\n"
438 "Argument:\n"
439 "varname: for set result in a environment variable\n"
440 "e.g. uuid uuid_env"
441);
442
443U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
444 "GUID - generate Globally Unique Identifier based on random UUID",
445 "[<varname>]\n"
446 "Argument:\n"
447 "varname: for set result in a environment variable\n"
448 "e.g. guid guid_env"
449);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200450#endif /* CONFIG_CMD_UUID */
451#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */