blob: 538a1ba6aa8e2991bf57042888b10bfea7dc715e [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 Delaunay65f94ed2015-10-27 11:00:28 +010065static const struct {
66 const char *string;
67 efi_guid_t guid;
68} list_guid[] = {
Caleb Connollyfb9e9482024-08-30 13:34:36 +010069#ifndef USE_HOSTCC
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +010070#ifdef CONFIG_PARTITION_TYPE_GUID
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010071 {"system", PARTITION_SYSTEM_GUID},
72 {"mbr", LEGACY_MBR_PARTITION_GUID},
73 {"msft", PARTITION_MSFT_RESERVED_GUID},
74 {"data", PARTITION_BASIC_DATA_GUID},
75 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
76 {"raid", PARTITION_LINUX_RAID_GUID},
77 {"swap", PARTITION_LINUX_SWAP_GUID},
Rasmus Villemoesdad0b632020-11-20 11:45:36 +010078 {"lvm", PARTITION_LINUX_LVM_GUID},
79 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
Simon Glass21fcfd82023-08-24 13:55:44 -060080 {"cros-kern", PARTITION_CROS_KERNEL},
81 {"cros-root", PARTITION_CROS_ROOT},
82 {"cros-fw", PARTITION_CROS_FIRMWARE},
83 {"cros-rsrv", PARTITION_CROS_RESERVED},
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +010084#endif
Simon Glassf7f55782023-03-20 08:30:12 +130085#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Heinrich Schuchardta4628b42022-01-16 14:10:23 +010086 {
87 "Device Path",
88 EFI_DEVICE_PATH_PROTOCOL_GUID,
89 },
90 {
91 "Device Path To Text",
92 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
93 },
94 {
95 "Device Path Utilities",
96 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
97 },
98 {
99 "Unicode Collation 2",
100 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
101 },
102 {
103 "Driver Binding",
104 EFI_DRIVER_BINDING_PROTOCOL_GUID,
105 },
106 {
107 "Simple Text Input",
108 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
109 },
110 {
111 "Simple Text Input Ex",
112 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
113 },
114 {
115 "Simple Text Output",
116 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
117 },
118 {
119 "Block IO",
120 EFI_BLOCK_IO_PROTOCOL_GUID,
121 },
122 {
123 "Simple File System",
124 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
125 },
126 {
127 "Loaded Image",
128 EFI_LOADED_IMAGE_PROTOCOL_GUID,
129 },
130 {
131 "Graphics Output",
132 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
133 },
134 {
135 "HII String",
136 EFI_HII_STRING_PROTOCOL_GUID,
137 },
138 {
139 "HII Database",
140 EFI_HII_DATABASE_PROTOCOL_GUID,
141 },
142 {
143 "HII Config Routing",
144 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
145 },
146 {
147 "Load File2",
148 EFI_LOAD_FILE2_PROTOCOL_GUID,
149 },
150 {
151 "Random Number Generator",
152 EFI_RNG_PROTOCOL_GUID,
153 },
154 {
155 "Simple Network",
156 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
157 },
158 {
159 "PXE Base Code",
160 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
161 },
162 {
163 "Device-Tree Fixup",
164 EFI_DT_FIXUP_PROTOCOL_GUID,
165 },
166 {
Heinrich Schuchardtd2a7d122022-01-16 17:46:38 +0100167 "TCG2",
168 EFI_TCG2_PROTOCOL_GUID,
169 },
170 {
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100171 "System Partition",
172 PARTITION_SYSTEM_GUID
173 },
174 {
175 "Firmware Management",
176 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
177 },
178 /* Configuration table GUIDs */
179 {
180 "ACPI table",
181 EFI_ACPI_TABLE_GUID,
182 },
183 {
184 "EFI System Resource Table",
185 EFI_SYSTEM_RESOURCE_TABLE_GUID,
186 },
187 {
188 "device tree",
189 EFI_FDT_GUID,
190 },
191 {
192 "SMBIOS table",
193 SMBIOS_TABLE_GUID,
194 },
195 {
Heinrich Schuchardtb36c9a02024-01-22 14:04:35 +0100196 "SMBIOS3 table",
197 SMBIOS3_TABLE_GUID,
198 },
199 {
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100200 "Runtime properties",
201 EFI_RT_PROPERTIES_TABLE_GUID,
202 },
203 {
204 "TCG2 Final Events Table",
205 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
206 },
Jose Marinhoff72cb32021-12-23 14:51:07 +0000207 {
208 "EFI Conformance Profiles Table",
209 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
210 },
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100211#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
212 {
213 "RISC-V Boot",
214 RISCV_EFI_BOOT_PROTOCOL_GUID,
215 },
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100216#endif
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100217#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100218#ifdef CONFIG_CMD_NVEDIT_EFI
219 /* signature database */
220 {
221 "EFI_GLOBAL_VARIABLE_GUID",
222 EFI_GLOBAL_VARIABLE_GUID,
223 },
224 {
225 "EFI_IMAGE_SECURITY_DATABASE_GUID",
226 EFI_IMAGE_SECURITY_DATABASE_GUID,
227 },
228 /* certificate types */
229 {
230 "EFI_CERT_SHA256_GUID",
231 EFI_CERT_SHA256_GUID,
232 },
233 {
234 "EFI_CERT_X509_GUID",
235 EFI_CERT_X509_GUID,
236 },
237 {
238 "EFI_CERT_TYPE_PKCS7_GUID",
239 EFI_CERT_TYPE_PKCS7_GUID,
240 },
241#endif
Simon Glass54d26e72023-07-15 21:38:51 -0600242#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Simon Glassd134ad52023-03-20 08:30:11 +1300243 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
244 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
245 { "EFI_HOB_LIST", EFI_HOB_LIST },
246 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
247 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
248 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
249#endif
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100250#endif /* !USE_HOSTCC */
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100251};
252
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100253int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
254{
255 int i;
256
257 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
258 if (!strcmp(list_guid[i].string, guid_str)) {
259 memcpy(guid_bin, &list_guid[i].guid, 16);
260 return 0;
261 }
262 }
263 return -ENODEV;
264}
265
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100266const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100267{
268 int i;
269
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100270 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
271 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100272 return list_guid[i].string;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100273 }
274 }
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100275 return NULL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100276}
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100277
Simon Glass4acd4f02020-04-08 08:32:58 -0600278int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
279 int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000280{
281 uint16_t tmp16;
282 uint32_t tmp32;
283 uint64_t tmp64;
284
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100285 if (!uuid_str_valid(uuid_str)) {
286#ifdef CONFIG_PARTITION_TYPE_GUID
287 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
288 return 0;
289#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200290 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100291 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200292
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200293 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600294 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200295 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000296
Simon Glass3ff49ec2021-07-24 09:03:29 -0600297 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200298 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000299
Simon Glass3ff49ec2021-07-24 09:03:29 -0600300 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200301 memcpy(uuid_bin + 6, &tmp16, 2);
302 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600303 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200304 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000305
Simon Glass3ff49ec2021-07-24 09:03:29 -0600306 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200307 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000308
Simon Glass3ff49ec2021-07-24 09:03:29 -0600309 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200310 memcpy(uuid_bin + 6, &tmp16, 2);
311 }
312
Simon Glass3ff49ec2021-07-24 09:03:29 -0600313 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200314 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000315
Heinrich Schuchardt5833b052024-11-03 23:42:22 +0100316 tmp64 = cpu_to_be64(hextoull(uuid_str + 24, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200317 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200318
319 return 0;
320}
321
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100322int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
323{
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100324 uint16_t tmp16;
325 uint32_t tmp32;
326 uint64_t tmp64;
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100327
328 if (!uuid_str_valid(uuid_str) || !uuid_bin)
329 return -EINVAL;
330
331 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
332 memcpy(uuid_bin, &tmp32, 4);
333
334 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
335 memcpy(uuid_bin + 4, &tmp16, 2);
336
337 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
338 memcpy(uuid_bin + 6, &tmp16, 2);
339
340 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
341 memcpy(uuid_bin + 8, &tmp16, 2);
342
Heinrich Schuchardt80e81d82024-11-03 23:42:21 +0100343 tmp64 = cpu_to_le64(hextoull(uuid_str + 24, NULL));
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100344 memcpy(uuid_bin + 10, &tmp64, 6);
345
346 return 0;
347}
348
Simon Glass4acd4f02020-04-08 08:32:58 -0600349void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
350 int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200351{
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100352 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 +0200353 9, 10, 11, 12, 13, 14, 15};
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100354 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 +0200355 9, 10, 11, 12, 13, 14, 15};
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100356 const uint8_t *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200357 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200358 int i;
359
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200360 /*
361 * UUID and GUID bin data - always in big endian:
362 * 4B-2B-2B-2B-6B
363 * be be be be be
364 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200365 if (str_format & UUID_STR_FORMAT_GUID)
366 char_order = guid_char_order;
367 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200368 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200369 if (str_format & UUID_STR_UPPER_CASE)
370 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200371 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200372 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200373
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200374 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200375 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200376 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200377 switch (i) {
378 case 3:
379 case 5:
380 case 7:
381 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200382 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200383 break;
384 }
385 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000386}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200387
Caleb Connolly2f4f0be2024-08-30 13:34:32 +0100388static void configure_uuid(struct uuid *uuid, unsigned char version)
389{
390 uint16_t tmp;
391
392 /* Configure variant/version bits */
393 tmp = be16_to_cpu(uuid->time_hi_and_version);
394 tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT);
395 uuid->time_hi_and_version = cpu_to_be16(tmp);
396
397 uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK;
398 uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
399}
400
401void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...)
402{
403 sha1_context ctx;
404 va_list args;
405 const uint8_t *data;
406 uint32_t *tmp32;
407 uint16_t *tmp16;
408 uint8_t hash[SHA1_SUM_LEN];
409
410 sha1_starts(&ctx);
411 /* Hash the namespace UUID as salt */
412 sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
413 va_start(args, guid);
414
415 while ((data = va_arg(args, const uint8_t *))) {
416 unsigned int len = va_arg(args, size_t);
417
418 sha1_update(&ctx, data, len);
419 }
420
421 va_end(args);
422 sha1_finish(&ctx, hash);
423
424 /* Truncate the hash into output UUID, it is already big endian */
425 memcpy(guid, hash, sizeof(*guid));
426
427 configure_uuid((struct uuid *)guid, 5);
428
429 /* Make little endian */
430 tmp32 = (uint32_t *)&guid->b[0];
431 *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32));
432 tmp16 = (uint16_t *)&guid->b[4];
433 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
434 tmp16 = (uint16_t *)&guid->b[6];
435 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
436}
437
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100438#ifndef USE_HOSTCC
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200439#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200440void gen_rand_uuid(unsigned char *uuid_bin)
441{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200442 u32 ptr[4];
443 struct uuid *uuid = (struct uuid *)ptr;
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100444 int i, ret;
445 struct udevice *devp;
446 u32 randv = 0;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200447
Marek Vasut0d871e72024-04-26 01:02:07 +0200448 if (CONFIG_IS_ENABLED(DM_RNG)) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100449 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunaya6064f72021-10-22 17:05:47 +0200450 if (!ret) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100451 ret = dm_rng_read(devp, &randv, sizeof(randv));
452 if (ret < 0)
453 randv = 0;
454 }
455 }
456 if (randv)
457 srand(randv);
458 else
459 srand(get_ticks() + rand());
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200460
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200461 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200462 for (i = 0; i < 4; i++)
463 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200464
Caleb Connolly2f4f0be2024-08-30 13:34:32 +0100465 configure_uuid(uuid, UUID_VERSION);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200466
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200467 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200468}
469
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200470void gen_rand_uuid_str(char *uuid_str, int str_format)
471{
472 unsigned char uuid_bin[UUID_BIN_LEN];
473
474 /* Generate UUID (big endian) */
475 gen_rand_uuid(uuid_bin);
476
477 /* Convert UUID bin to UUID or GUID formated STRING */
478 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
479}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200480
Simon Glass0e84d962024-09-29 19:49:50 -0600481#if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glassed38aef2020-05-10 11:40:03 -0600482int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200483{
484 char uuid[UUID_STR_LEN + 1];
485 int str_format;
486
487 if (!strcmp(argv[0], "uuid"))
488 str_format = UUID_STR_FORMAT_STD;
489 else
490 str_format = UUID_STR_FORMAT_GUID;
491
492 if (argc > 2)
493 return CMD_RET_USAGE;
494
495 gen_rand_uuid_str(uuid, str_format);
496
497 if (argc == 1)
498 printf("%s\n", uuid);
499 else
Simon Glass6a38e412017-08-03 12:22:09 -0600500 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200501
502 return CMD_RET_SUCCESS;
503}
504
505U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
506 "UUID - generate random Universally Unique Identifier",
507 "[<varname>]\n"
508 "Argument:\n"
509 "varname: for set result in a environment variable\n"
510 "e.g. uuid uuid_env"
511);
512
513U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
514 "GUID - generate Globally Unique Identifier based on random UUID",
515 "[<varname>]\n"
516 "Argument:\n"
517 "varname: for set result in a environment variable\n"
518 "e.g. guid guid_env"
519);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200520#endif /* CONFIG_CMD_UUID */
521#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
Caleb Connollyfb9e9482024-08-30 13:34:36 +0100522#endif /* !USE_HOSTCC */