blob: d0187007d0e89072781187fafa1e5da2eee50dc4 [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
26/*
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020027 * UUID - Universally Unique IDentifier - 128 bits unique number.
28 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczakb4285142014-04-02 10:20:04 +020029 * specification. A UUID contains a set of fields. The set varies
30 * depending on the version of the UUID, as shown below:
31 * - time, MAC address(v1),
32 * - user ID(v2),
33 * - MD5 of name or URL(v3),
34 * - random data(v4),
35 * - SHA-1 of name or URL(v5),
36 *
37 * Layout of UUID:
38 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
39 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
40 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
41 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
42 * node - 48 bit
43 *
44 * source: https://www.ietf.org/rfc/rfc4122.txt
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020045 *
46 * UUID binary format (16 bytes):
47 *
48 * 4B-2B-2B-2B-6B (big endian - network byte order)
49 *
50 * UUID string is 36 length of characters (36 bytes):
51 *
52 * 0 9 14 19 24
53 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
54 * be be be be be
55 *
56 * where x is a hexadecimal character. Fields are separated by '-'s.
57 * When converting to a binary UUID, le means the field should be converted
58 * to little endian and be means it should be converted to big endian.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000059 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020060 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
61 * format but it differs in string format like below.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000062 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020063 * GUID:
Jason Hobbs6e39cce2011-08-23 11:06:56 +000064 * 0 9 14 19 24
65 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
66 * le le le be be
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020067 *
68 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000069 */
Jason Hobbs6e39cce2011-08-23 11:06:56 +000070int uuid_str_valid(const char *uuid)
71{
72 int i, valid;
73
74 if (uuid == NULL)
75 return 0;
76
77 for (i = 0, valid = 1; uuid[i] && valid; i++) {
78 switch (i) {
79 case 8: case 13: case 18: case 23:
80 valid = (uuid[i] == '-');
81 break;
82 default:
83 valid = isxdigit(uuid[i]);
84 break;
85 }
86 }
87
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020088 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000089 return 0;
90
91 return 1;
92}
93
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010094static const struct {
95 const char *string;
96 efi_guid_t guid;
97} list_guid[] = {
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +010098#ifdef CONFIG_PARTITION_TYPE_GUID
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010099 {"system", PARTITION_SYSTEM_GUID},
100 {"mbr", LEGACY_MBR_PARTITION_GUID},
101 {"msft", PARTITION_MSFT_RESERVED_GUID},
102 {"data", PARTITION_BASIC_DATA_GUID},
103 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
104 {"raid", PARTITION_LINUX_RAID_GUID},
105 {"swap", PARTITION_LINUX_SWAP_GUID},
Rasmus Villemoesdad0b632020-11-20 11:45:36 +0100106 {"lvm", PARTITION_LINUX_LVM_GUID},
107 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +0100108#endif
Simon Glassf7f55782023-03-20 08:30:12 +1300109#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100110 {
111 "Device Path",
112 EFI_DEVICE_PATH_PROTOCOL_GUID,
113 },
114 {
115 "Device Path To Text",
116 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
117 },
118 {
119 "Device Path Utilities",
120 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
121 },
122 {
123 "Unicode Collation 2",
124 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
125 },
126 {
127 "Driver Binding",
128 EFI_DRIVER_BINDING_PROTOCOL_GUID,
129 },
130 {
131 "Simple Text Input",
132 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
133 },
134 {
135 "Simple Text Input Ex",
136 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
137 },
138 {
139 "Simple Text Output",
140 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
141 },
142 {
143 "Block IO",
144 EFI_BLOCK_IO_PROTOCOL_GUID,
145 },
146 {
147 "Simple File System",
148 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
149 },
150 {
151 "Loaded Image",
152 EFI_LOADED_IMAGE_PROTOCOL_GUID,
153 },
154 {
155 "Graphics Output",
156 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
157 },
158 {
159 "HII String",
160 EFI_HII_STRING_PROTOCOL_GUID,
161 },
162 {
163 "HII Database",
164 EFI_HII_DATABASE_PROTOCOL_GUID,
165 },
166 {
167 "HII Config Routing",
168 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
169 },
170 {
171 "Load File2",
172 EFI_LOAD_FILE2_PROTOCOL_GUID,
173 },
174 {
175 "Random Number Generator",
176 EFI_RNG_PROTOCOL_GUID,
177 },
178 {
179 "Simple Network",
180 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
181 },
182 {
183 "PXE Base Code",
184 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
185 },
186 {
187 "Device-Tree Fixup",
188 EFI_DT_FIXUP_PROTOCOL_GUID,
189 },
190 {
Heinrich Schuchardtd2a7d122022-01-16 17:46:38 +0100191 "TCG2",
192 EFI_TCG2_PROTOCOL_GUID,
193 },
194 {
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100195 "System Partition",
196 PARTITION_SYSTEM_GUID
197 },
198 {
199 "Firmware Management",
200 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
201 },
202 /* Configuration table GUIDs */
203 {
204 "ACPI table",
205 EFI_ACPI_TABLE_GUID,
206 },
207 {
208 "EFI System Resource Table",
209 EFI_SYSTEM_RESOURCE_TABLE_GUID,
210 },
211 {
212 "device tree",
213 EFI_FDT_GUID,
214 },
215 {
216 "SMBIOS table",
217 SMBIOS_TABLE_GUID,
218 },
219 {
220 "Runtime properties",
221 EFI_RT_PROPERTIES_TABLE_GUID,
222 },
223 {
224 "TCG2 Final Events Table",
225 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
226 },
Jose Marinhoff72cb32021-12-23 14:51:07 +0000227 {
228 "EFI Conformance Profiles Table",
229 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
230 },
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100231#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
232 {
233 "RISC-V Boot",
234 RISCV_EFI_BOOT_PROTOCOL_GUID,
235 },
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100236#endif
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100237#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100238#ifdef CONFIG_CMD_NVEDIT_EFI
239 /* signature database */
240 {
241 "EFI_GLOBAL_VARIABLE_GUID",
242 EFI_GLOBAL_VARIABLE_GUID,
243 },
244 {
245 "EFI_IMAGE_SECURITY_DATABASE_GUID",
246 EFI_IMAGE_SECURITY_DATABASE_GUID,
247 },
248 /* certificate types */
249 {
250 "EFI_CERT_SHA256_GUID",
251 EFI_CERT_SHA256_GUID,
252 },
253 {
254 "EFI_CERT_X509_GUID",
255 EFI_CERT_X509_GUID,
256 },
257 {
258 "EFI_CERT_TYPE_PKCS7_GUID",
259 EFI_CERT_TYPE_PKCS7_GUID,
260 },
261#endif
Simon Glass54d26e72023-07-15 21:38:51 -0600262#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Simon Glassd134ad52023-03-20 08:30:11 +1300263 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
264 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
265 { "EFI_HOB_LIST", EFI_HOB_LIST },
266 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
267 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
268 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
269#endif
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100270};
271
272/*
273 * uuid_guid_get_bin() - this function get GUID bin for string
274 *
275 * @param guid_str - pointer to partition type string
276 * @param guid_bin - pointer to allocated array for big endian output [16B]
277 */
278int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
279{
280 int i;
281
282 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
283 if (!strcmp(list_guid[i].string, guid_str)) {
284 memcpy(guid_bin, &list_guid[i].guid, 16);
285 return 0;
286 }
287 }
288 return -ENODEV;
289}
290
291/*
292 * uuid_guid_get_str() - this function get string for GUID.
293 *
294 * @param guid_bin - pointer to string with partition type guid [16B]
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100295 *
296 * Returns NULL if the type GUID is not known.
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100297 */
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100298const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100299{
300 int i;
301
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100302 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
303 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100304 return list_guid[i].string;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100305 }
306 }
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100307 return NULL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100308}
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100309
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200310/*
311 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
312 *
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100313 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200314 * @param uuid_bin - pointer to allocated array for big endian output [16B]
315 * @str_format - UUID string format: 0 - UUID; 1 - GUID
316 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600317int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
318 int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000319{
320 uint16_t tmp16;
321 uint32_t tmp32;
322 uint64_t tmp64;
323
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100324 if (!uuid_str_valid(uuid_str)) {
325#ifdef CONFIG_PARTITION_TYPE_GUID
326 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
327 return 0;
328#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200329 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100330 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200331
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200332 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600333 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200334 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000335
Simon Glass3ff49ec2021-07-24 09:03:29 -0600336 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200337 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000338
Simon Glass3ff49ec2021-07-24 09:03:29 -0600339 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200340 memcpy(uuid_bin + 6, &tmp16, 2);
341 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600342 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200343 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000344
Simon Glass3ff49ec2021-07-24 09:03:29 -0600345 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200346 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000347
Simon Glass3ff49ec2021-07-24 09:03:29 -0600348 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200349 memcpy(uuid_bin + 6, &tmp16, 2);
350 }
351
Simon Glass3ff49ec2021-07-24 09:03:29 -0600352 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200353 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000354
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200355 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
356 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200357
358 return 0;
359}
360
Abdellatif El Khlifidc3cb5d2023-08-04 14:33:38 +0100361/**
362 * uuid_str_to_le_bin() - Convert string UUID to little endian binary data.
363 * @uuid_str: pointer to UUID string
364 * @uuid_bin: pointer to allocated array for little endian output [16B]
365 *
366 * UUID string is 36 characters (36 bytes):
367 *
368 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
369 *
370 * where x is a hexadecimal character. Fields are separated by '-'s.
371 * When converting to a little endian binary UUID, the string fields are reversed.
372 *
373 * Return:
374 *
375 * uuid_bin filled with little endian UUID data
376 * On success 0 is returned. Otherwise, failure code.
377 */
378int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
379{
380 u16 tmp16;
381 u32 tmp32;
382 u64 tmp64;
383
384 if (!uuid_str_valid(uuid_str) || !uuid_bin)
385 return -EINVAL;
386
387 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
388 memcpy(uuid_bin, &tmp32, 4);
389
390 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
391 memcpy(uuid_bin + 4, &tmp16, 2);
392
393 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
394 memcpy(uuid_bin + 6, &tmp16, 2);
395
396 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
397 memcpy(uuid_bin + 8, &tmp16, 2);
398
399 tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
400 memcpy(uuid_bin + 10, &tmp64, 6);
401
402 return 0;
403}
404
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200405/*
406 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
407 *
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200408 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
409 * @param uuid_str: pointer to allocated array for output string [37B]
410 * @str_format: bit 0: 0 - UUID; 1 - GUID
411 * bit 1: 0 - lower case; 2 - upper case
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200412 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600413void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
414 int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200415{
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200416 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
417 9, 10, 11, 12, 13, 14, 15};
418 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
419 9, 10, 11, 12, 13, 14, 15};
420 const u8 *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200421 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200422 int i;
423
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200424 /*
425 * UUID and GUID bin data - always in big endian:
426 * 4B-2B-2B-2B-6B
427 * be be be be be
428 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200429 if (str_format & UUID_STR_FORMAT_GUID)
430 char_order = guid_char_order;
431 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200432 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200433 if (str_format & UUID_STR_UPPER_CASE)
434 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200435 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200436 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200437
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200438 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200439 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200440 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200441 switch (i) {
442 case 3:
443 case 5:
444 case 7:
445 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200446 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200447 break;
448 }
449 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000450}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200451
452/*
453 * gen_rand_uuid() - this function generates a random binary UUID version 4.
454 * In this version all fields beside 4 bits of version and
455 * 2 bits of variant are randomly generated.
456 *
457 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
458*/
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200459#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200460void gen_rand_uuid(unsigned char *uuid_bin)
461{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200462 u32 ptr[4];
463 struct uuid *uuid = (struct uuid *)ptr;
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100464 int i, ret;
465 struct udevice *devp;
466 u32 randv = 0;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200467
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100468 if (IS_ENABLED(CONFIG_DM_RNG)) {
469 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunaya6064f72021-10-22 17:05:47 +0200470 if (!ret) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100471 ret = dm_rng_read(devp, &randv, sizeof(randv));
472 if (ret < 0)
473 randv = 0;
474 }
475 }
476 if (randv)
477 srand(randv);
478 else
479 srand(get_ticks() + rand());
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200480
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200481 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200482 for (i = 0; i < 4; i++)
483 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200484
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200485 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200486 UUID_VERSION_MASK,
487 UUID_VERSION << UUID_VERSION_SHIFT);
488
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200489 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200490 UUID_VARIANT_MASK,
491 UUID_VARIANT << UUID_VARIANT_SHIFT);
492
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200493 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200494}
495
496/*
497 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
498 * formats UUID or GUID.
499 *
500 * @param uuid_str - pointer to allocated array [37B].
501 * @param - uuid output type: UUID - 0, GUID - 1
502 */
503void gen_rand_uuid_str(char *uuid_str, int str_format)
504{
505 unsigned char uuid_bin[UUID_BIN_LEN];
506
507 /* Generate UUID (big endian) */
508 gen_rand_uuid(uuid_bin);
509
510 /* Convert UUID bin to UUID or GUID formated STRING */
511 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
512}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200513
Marek Vasut51d85f62019-01-07 21:23:38 +0100514#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glassed38aef2020-05-10 11:40:03 -0600515int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200516{
517 char uuid[UUID_STR_LEN + 1];
518 int str_format;
519
520 if (!strcmp(argv[0], "uuid"))
521 str_format = UUID_STR_FORMAT_STD;
522 else
523 str_format = UUID_STR_FORMAT_GUID;
524
525 if (argc > 2)
526 return CMD_RET_USAGE;
527
528 gen_rand_uuid_str(uuid, str_format);
529
530 if (argc == 1)
531 printf("%s\n", uuid);
532 else
Simon Glass6a38e412017-08-03 12:22:09 -0600533 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200534
535 return CMD_RET_SUCCESS;
536}
537
538U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
539 "UUID - generate random Universally Unique Identifier",
540 "[<varname>]\n"
541 "Argument:\n"
542 "varname: for set result in a environment variable\n"
543 "e.g. uuid uuid_env"
544);
545
546U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
547 "GUID - generate Globally Unique Identifier based on random UUID",
548 "[<varname>]\n"
549 "Argument:\n"
550 "varname: for set result in a environment variable\n"
551 "e.g. guid guid_env"
552);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200553#endif /* CONFIG_CMD_UUID */
554#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */