blob: afb40bff507af4bb5adeed6be8934fc38b5e226f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbse11938e2011-08-23 11:06:56 +00002/*
3 * Copyright 2011 Calxeda, Inc.
Abdellatif El Khlifi7048f262023-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 Hobbse11938e2011-08-23 11:06:56 +00008 */
9
Simon Glass966b16c2023-08-24 13:55:44 -060010#define LOG_CATEGOT LOGC_CORE
11
Przemyslaw Marczak89c82302014-04-02 10:20:05 +020012#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
Heinrich Schuchardt3adae642022-01-16 14:10:23 +010014#include <efi_api.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060015#include <env.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <rand.h>
Simon Glass10453152019-11-14 12:57:30 -070017#include <time.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060018#include <uuid.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000019#include <linux/ctype.h>
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020020#include <errno.h>
21#include <common.h>
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020022#include <asm/io.h>
23#include <part_efi.h>
24#include <malloc.h>
Matthias Brugger92fdad22020-12-18 10:28:03 +010025#include <dm/uclass.h>
26#include <rng.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000027
Jason Hobbse11938e2011-08-23 11:06:56 +000028int uuid_str_valid(const char *uuid)
29{
30 int i, valid;
31
32 if (uuid == NULL)
33 return 0;
34
35 for (i = 0, valid = 1; uuid[i] && valid; i++) {
36 switch (i) {
37 case 8: case 13: case 18: case 23:
38 valid = (uuid[i] == '-');
39 break;
40 default:
41 valid = isxdigit(uuid[i]);
42 break;
43 }
44 }
45
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020046 if (i != UUID_STR_LEN || !valid)
Jason Hobbse11938e2011-08-23 11:06:56 +000047 return 0;
48
49 return 1;
50}
51
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +010052static const struct {
53 const char *string;
54 efi_guid_t guid;
55} list_guid[] = {
Heinrich Schuchardtc1528f32022-01-16 11:55:39 +010056#ifdef CONFIG_PARTITION_TYPE_GUID
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +010057 {"system", PARTITION_SYSTEM_GUID},
58 {"mbr", LEGACY_MBR_PARTITION_GUID},
59 {"msft", PARTITION_MSFT_RESERVED_GUID},
60 {"data", PARTITION_BASIC_DATA_GUID},
61 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
62 {"raid", PARTITION_LINUX_RAID_GUID},
63 {"swap", PARTITION_LINUX_SWAP_GUID},
Rasmus Villemoesc0364ce2020-11-20 11:45:36 +010064 {"lvm", PARTITION_LINUX_LVM_GUID},
65 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
Simon Glass966b16c2023-08-24 13:55:44 -060066 {"cros-kern", PARTITION_CROS_KERNEL},
67 {"cros-root", PARTITION_CROS_ROOT},
68 {"cros-fw", PARTITION_CROS_FIRMWARE},
69 {"cros-rsrv", PARTITION_CROS_RESERVED},
Heinrich Schuchardtc1528f32022-01-16 11:55:39 +010070#endif
Simon Glass4e5e49a2023-03-20 08:30:12 +130071#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Heinrich Schuchardt3adae642022-01-16 14:10:23 +010072 {
73 "Device Path",
74 EFI_DEVICE_PATH_PROTOCOL_GUID,
75 },
76 {
77 "Device Path To Text",
78 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
79 },
80 {
81 "Device Path Utilities",
82 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
83 },
84 {
85 "Unicode Collation 2",
86 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
87 },
88 {
89 "Driver Binding",
90 EFI_DRIVER_BINDING_PROTOCOL_GUID,
91 },
92 {
93 "Simple Text Input",
94 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
95 },
96 {
97 "Simple Text Input Ex",
98 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
99 },
100 {
101 "Simple Text Output",
102 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
103 },
104 {
105 "Block IO",
106 EFI_BLOCK_IO_PROTOCOL_GUID,
107 },
108 {
109 "Simple File System",
110 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
111 },
112 {
113 "Loaded Image",
114 EFI_LOADED_IMAGE_PROTOCOL_GUID,
115 },
116 {
117 "Graphics Output",
118 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
119 },
120 {
121 "HII String",
122 EFI_HII_STRING_PROTOCOL_GUID,
123 },
124 {
125 "HII Database",
126 EFI_HII_DATABASE_PROTOCOL_GUID,
127 },
128 {
129 "HII Config Routing",
130 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
131 },
132 {
133 "Load File2",
134 EFI_LOAD_FILE2_PROTOCOL_GUID,
135 },
136 {
137 "Random Number Generator",
138 EFI_RNG_PROTOCOL_GUID,
139 },
140 {
141 "Simple Network",
142 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
143 },
144 {
145 "PXE Base Code",
146 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
147 },
148 {
149 "Device-Tree Fixup",
150 EFI_DT_FIXUP_PROTOCOL_GUID,
151 },
152 {
Heinrich Schuchardt38040a62022-01-16 17:46:38 +0100153 "TCG2",
154 EFI_TCG2_PROTOCOL_GUID,
155 },
156 {
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100157 "System Partition",
158 PARTITION_SYSTEM_GUID
159 },
160 {
161 "Firmware Management",
162 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
163 },
164 /* Configuration table GUIDs */
165 {
166 "ACPI table",
167 EFI_ACPI_TABLE_GUID,
168 },
169 {
170 "EFI System Resource Table",
171 EFI_SYSTEM_RESOURCE_TABLE_GUID,
172 },
173 {
174 "device tree",
175 EFI_FDT_GUID,
176 },
177 {
178 "SMBIOS table",
179 SMBIOS_TABLE_GUID,
180 },
181 {
182 "Runtime properties",
183 EFI_RT_PROPERTIES_TABLE_GUID,
184 },
185 {
186 "TCG2 Final Events Table",
187 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
188 },
Jose Marinho6b92c172021-12-23 14:51:07 +0000189 {
190 "EFI Conformance Profiles Table",
191 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
192 },
Heinrich Schuchardt8d4c4262022-01-28 19:29:47 +0100193#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
194 {
195 "RISC-V Boot",
196 RISCV_EFI_BOOT_PROTOCOL_GUID,
197 },
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100198#endif
Heinrich Schuchardt8d4c4262022-01-28 19:29:47 +0100199#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt983a5a22022-01-16 14:53:37 +0100200#ifdef CONFIG_CMD_NVEDIT_EFI
201 /* signature database */
202 {
203 "EFI_GLOBAL_VARIABLE_GUID",
204 EFI_GLOBAL_VARIABLE_GUID,
205 },
206 {
207 "EFI_IMAGE_SECURITY_DATABASE_GUID",
208 EFI_IMAGE_SECURITY_DATABASE_GUID,
209 },
210 /* certificate types */
211 {
212 "EFI_CERT_SHA256_GUID",
213 EFI_CERT_SHA256_GUID,
214 },
215 {
216 "EFI_CERT_X509_GUID",
217 EFI_CERT_X509_GUID,
218 },
219 {
220 "EFI_CERT_TYPE_PKCS7_GUID",
221 EFI_CERT_TYPE_PKCS7_GUID,
222 },
223#endif
Simon Glass37108022023-07-15 21:38:51 -0600224#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Simon Glassac932752023-03-20 08:30:11 +1300225 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
226 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
227 { "EFI_HOB_LIST", EFI_HOB_LIST },
228 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
229 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
230 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
231#endif
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100232};
233
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100234int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
235{
236 int i;
237
238 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
239 if (!strcmp(list_guid[i].string, guid_str)) {
240 memcpy(guid_bin, &list_guid[i].guid, 16);
241 return 0;
242 }
243 }
244 return -ENODEV;
245}
246
Rasmus Villemoes31ce3672020-11-20 11:45:35 +0100247const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100248{
249 int i;
250
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100251 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
252 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Rasmus Villemoes31ce3672020-11-20 11:45:35 +0100253 return list_guid[i].string;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100254 }
255 }
Rasmus Villemoes31ce3672020-11-20 11:45:35 +0100256 return NULL;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100257}
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100258
Simon Glass2c2ca202020-04-08 08:32:58 -0600259int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
260 int str_format)
Jason Hobbse11938e2011-08-23 11:06:56 +0000261{
262 uint16_t tmp16;
263 uint32_t tmp32;
264 uint64_t tmp64;
265
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100266 if (!uuid_str_valid(uuid_str)) {
Simon Glass966b16c2023-08-24 13:55:44 -0600267 log_debug("not valid\n");
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100268#ifdef CONFIG_PARTITION_TYPE_GUID
269 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
270 return 0;
271#endif
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200272 return -EINVAL;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100273 }
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200274
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200275 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600276 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200277 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000278
Simon Glass7e5f4602021-07-24 09:03:29 -0600279 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200280 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000281
Simon Glass7e5f4602021-07-24 09:03:29 -0600282 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200283 memcpy(uuid_bin + 6, &tmp16, 2);
284 } else {
Simon Glass7e5f4602021-07-24 09:03:29 -0600285 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200286 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000287
Simon Glass7e5f4602021-07-24 09:03:29 -0600288 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200289 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000290
Simon Glass7e5f4602021-07-24 09:03:29 -0600291 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200292 memcpy(uuid_bin + 6, &tmp16, 2);
293 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000294
Simon Glass7e5f4602021-07-24 09:03:29 -0600295 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200296 memcpy(uuid_bin + 8, &tmp16, 2);
297
298 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
299 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200300
301 return 0;
302}
303
Abdellatif El Khlifi7048f262023-08-04 14:33:38 +0100304int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
305{
306 u16 tmp16;
307 u32 tmp32;
308 u64 tmp64;
309
310 if (!uuid_str_valid(uuid_str) || !uuid_bin)
311 return -EINVAL;
312
313 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
314 memcpy(uuid_bin, &tmp32, 4);
315
316 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
317 memcpy(uuid_bin + 4, &tmp16, 2);
318
319 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
320 memcpy(uuid_bin + 6, &tmp16, 2);
321
322 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
323 memcpy(uuid_bin + 8, &tmp16, 2);
324
325 tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
326 memcpy(uuid_bin + 10, &tmp64, 6);
327
328 return 0;
329}
330
Simon Glass2c2ca202020-04-08 08:32:58 -0600331void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
332 int str_format)
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200333{
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200334 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
335 9, 10, 11, 12, 13, 14, 15};
336 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
337 9, 10, 11, 12, 13, 14, 15};
338 const u8 *char_order;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200339 const char *format;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200340 int i;
341
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200342 /*
343 * UUID and GUID bin data - always in big endian:
344 * 4B-2B-2B-2B-6B
345 * be be be be be
346 */
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200347 if (str_format & UUID_STR_FORMAT_GUID)
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200348 char_order = guid_char_order;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200349 else
350 char_order = uuid_char_order;
351 if (str_format & UUID_STR_UPPER_CASE)
352 format = "%02X";
353 else
354 format = "%02x";
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200355
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200356 for (i = 0; i < 16; i++) {
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200357 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200358 uuid_str += 2;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200359 switch (i) {
360 case 3:
361 case 5:
362 case 7:
363 case 9:
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200364 *uuid_str++ = '-';
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200365 break;
366 }
367 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000368}
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200369
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200370#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200371void gen_rand_uuid(unsigned char *uuid_bin)
372{
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200373 u32 ptr[4];
374 struct uuid *uuid = (struct uuid *)ptr;
Matthias Brugger92fdad22020-12-18 10:28:03 +0100375 int i, ret;
376 struct udevice *devp;
377 u32 randv = 0;
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200378
Matthias Brugger92fdad22020-12-18 10:28:03 +0100379 if (IS_ENABLED(CONFIG_DM_RNG)) {
380 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunay70a9f4d2021-10-22 17:05:47 +0200381 if (!ret) {
Matthias Brugger92fdad22020-12-18 10:28:03 +0100382 ret = dm_rng_read(devp, &randv, sizeof(randv));
383 if (ret < 0)
384 randv = 0;
385 }
386 }
387 if (randv)
388 srand(randv);
389 else
390 srand(get_ticks() + rand());
Eugeniu Rosca4ccf6782019-05-02 14:27:06 +0200391
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200392 /* Set all fields randomly */
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200393 for (i = 0; i < 4; i++)
394 ptr[i] = rand();
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200395
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200396 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200397 UUID_VERSION_MASK,
398 UUID_VERSION << UUID_VERSION_SHIFT);
399
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200400 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200401 UUID_VARIANT_MASK,
402 UUID_VARIANT << UUID_VARIANT_SHIFT);
403
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200404 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200405}
406
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200407void gen_rand_uuid_str(char *uuid_str, int str_format)
408{
409 unsigned char uuid_bin[UUID_BIN_LEN];
410
411 /* Generate UUID (big endian) */
412 gen_rand_uuid(uuid_bin);
413
414 /* Convert UUID bin to UUID or GUID formated STRING */
415 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
416}
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200417
Marek Vasut05f6da32019-01-07 21:23:38 +0100418#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glass09140112020-05-10 11:40:03 -0600419int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200420{
421 char uuid[UUID_STR_LEN + 1];
422 int str_format;
423
424 if (!strcmp(argv[0], "uuid"))
425 str_format = UUID_STR_FORMAT_STD;
426 else
427 str_format = UUID_STR_FORMAT_GUID;
428
429 if (argc > 2)
430 return CMD_RET_USAGE;
431
432 gen_rand_uuid_str(uuid, str_format);
433
434 if (argc == 1)
435 printf("%s\n", uuid);
436 else
Simon Glass382bee52017-08-03 12:22:09 -0600437 env_set(argv[1], uuid);
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200438
439 return CMD_RET_SUCCESS;
440}
441
442U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
443 "UUID - generate random Universally Unique Identifier",
444 "[<varname>]\n"
445 "Argument:\n"
446 "varname: for set result in a environment variable\n"
447 "e.g. uuid uuid_env"
448);
449
450U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
451 "GUID - generate Globally Unique Identifier based on random UUID",
452 "[<varname>]\n"
453 "Argument:\n"
454 "varname: for set result in a environment variable\n"
455 "e.g. guid guid_env"
456);
Przemyslaw Marczak39206382014-04-02 10:20:06 +0200457#endif /* CONFIG_CMD_UUID */
458#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */