blob: 465e1ac38f57df7295db61f8ed7e9d00a10a878b [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.
Jason Hobbs6e39cce2011-08-23 11:06:56 +00004 */
5
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +02006#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Heinrich Schuchardta4628b42022-01-16 14:10:23 +01008#include <efi_api.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06009#include <env.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <rand.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Simon Glass6f044982020-05-10 11:39:52 -060012#include <uuid.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000013#include <linux/ctype.h>
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020014#include <errno.h>
15#include <common.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>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000021
22/*
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020023 * UUID - Universally Unique IDentifier - 128 bits unique number.
24 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczakb4285142014-04-02 10:20:04 +020025 * specification. A UUID contains a set of fields. The set varies
26 * depending on the version of the UUID, as shown below:
27 * - time, MAC address(v1),
28 * - user ID(v2),
29 * - MD5 of name or URL(v3),
30 * - random data(v4),
31 * - SHA-1 of name or URL(v5),
32 *
33 * Layout of UUID:
34 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
35 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
36 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
37 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
38 * node - 48 bit
39 *
40 * source: https://www.ietf.org/rfc/rfc4122.txt
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020041 *
42 * UUID binary format (16 bytes):
43 *
44 * 4B-2B-2B-2B-6B (big endian - network byte order)
45 *
46 * UUID string is 36 length of characters (36 bytes):
47 *
48 * 0 9 14 19 24
49 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
50 * be be be be be
51 *
52 * where x is a hexadecimal character. Fields are separated by '-'s.
53 * When converting to a binary UUID, le means the field should be converted
54 * to little endian and be means it should be converted to big endian.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000055 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020056 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
57 * format but it differs in string format like below.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000058 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020059 * GUID:
Jason Hobbs6e39cce2011-08-23 11:06:56 +000060 * 0 9 14 19 24
61 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
62 * le le le be be
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020063 *
64 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000065 */
Jason Hobbs6e39cce2011-08-23 11:06:56 +000066int uuid_str_valid(const char *uuid)
67{
68 int i, valid;
69
70 if (uuid == NULL)
71 return 0;
72
73 for (i = 0, valid = 1; uuid[i] && valid; i++) {
74 switch (i) {
75 case 8: case 13: case 18: case 23:
76 valid = (uuid[i] == '-');
77 break;
78 default:
79 valid = isxdigit(uuid[i]);
80 break;
81 }
82 }
83
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020084 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000085 return 0;
86
87 return 1;
88}
89
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010090static const struct {
91 const char *string;
92 efi_guid_t guid;
93} list_guid[] = {
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +010094#ifdef CONFIG_PARTITION_TYPE_GUID
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010095 {"system", PARTITION_SYSTEM_GUID},
96 {"mbr", LEGACY_MBR_PARTITION_GUID},
97 {"msft", PARTITION_MSFT_RESERVED_GUID},
98 {"data", PARTITION_BASIC_DATA_GUID},
99 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
100 {"raid", PARTITION_LINUX_RAID_GUID},
101 {"swap", PARTITION_LINUX_SWAP_GUID},
Rasmus Villemoesdad0b632020-11-20 11:45:36 +0100102 {"lvm", PARTITION_LINUX_LVM_GUID},
103 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
Heinrich Schuchardt0a13b792022-01-16 11:55:39 +0100104#endif
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100105#ifdef CONFIG_CMD_EFIDEBUG
106 {
107 "Device Path",
108 EFI_DEVICE_PATH_PROTOCOL_GUID,
109 },
110 {
111 "Device Path To Text",
112 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
113 },
114 {
115 "Device Path Utilities",
116 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
117 },
118 {
119 "Unicode Collation 2",
120 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
121 },
122 {
123 "Driver Binding",
124 EFI_DRIVER_BINDING_PROTOCOL_GUID,
125 },
126 {
127 "Simple Text Input",
128 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
129 },
130 {
131 "Simple Text Input Ex",
132 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
133 },
134 {
135 "Simple Text Output",
136 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
137 },
138 {
139 "Block IO",
140 EFI_BLOCK_IO_PROTOCOL_GUID,
141 },
142 {
143 "Simple File System",
144 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
145 },
146 {
147 "Loaded Image",
148 EFI_LOADED_IMAGE_PROTOCOL_GUID,
149 },
150 {
151 "Graphics Output",
152 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
153 },
154 {
155 "HII String",
156 EFI_HII_STRING_PROTOCOL_GUID,
157 },
158 {
159 "HII Database",
160 EFI_HII_DATABASE_PROTOCOL_GUID,
161 },
162 {
163 "HII Config Routing",
164 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
165 },
166 {
167 "Load File2",
168 EFI_LOAD_FILE2_PROTOCOL_GUID,
169 },
170 {
171 "Random Number Generator",
172 EFI_RNG_PROTOCOL_GUID,
173 },
174 {
175 "Simple Network",
176 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
177 },
178 {
179 "PXE Base Code",
180 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
181 },
182 {
183 "Device-Tree Fixup",
184 EFI_DT_FIXUP_PROTOCOL_GUID,
185 },
186 {
Heinrich Schuchardtd2a7d122022-01-16 17:46:38 +0100187 "TCG2",
188 EFI_TCG2_PROTOCOL_GUID,
189 },
190 {
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100191 "System Partition",
192 PARTITION_SYSTEM_GUID
193 },
194 {
195 "Firmware Management",
196 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
197 },
198 /* Configuration table GUIDs */
199 {
200 "ACPI table",
201 EFI_ACPI_TABLE_GUID,
202 },
203 {
204 "EFI System Resource Table",
205 EFI_SYSTEM_RESOURCE_TABLE_GUID,
206 },
207 {
208 "device tree",
209 EFI_FDT_GUID,
210 },
211 {
212 "SMBIOS table",
213 SMBIOS_TABLE_GUID,
214 },
215 {
216 "Runtime properties",
217 EFI_RT_PROPERTIES_TABLE_GUID,
218 },
219 {
220 "TCG2 Final Events Table",
221 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
222 },
Jose Marinhoff72cb32021-12-23 14:51:07 +0000223 {
224 "EFI Conformance Profiles Table",
225 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
226 },
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100227#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
228 {
229 "RISC-V Boot",
230 RISCV_EFI_BOOT_PROTOCOL_GUID,
231 },
Heinrich Schuchardta4628b42022-01-16 14:10:23 +0100232#endif
Heinrich Schuchardtf4c5bcb2022-01-28 19:29:47 +0100233#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100234#ifdef CONFIG_CMD_NVEDIT_EFI
235 /* signature database */
236 {
237 "EFI_GLOBAL_VARIABLE_GUID",
238 EFI_GLOBAL_VARIABLE_GUID,
239 },
240 {
241 "EFI_IMAGE_SECURITY_DATABASE_GUID",
242 EFI_IMAGE_SECURITY_DATABASE_GUID,
243 },
244 /* certificate types */
245 {
246 "EFI_CERT_SHA256_GUID",
247 EFI_CERT_SHA256_GUID,
248 },
249 {
250 "EFI_CERT_X509_GUID",
251 EFI_CERT_X509_GUID,
252 },
253 {
254 "EFI_CERT_TYPE_PKCS7_GUID",
255 EFI_CERT_TYPE_PKCS7_GUID,
256 },
257#endif
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100258};
259
260/*
261 * uuid_guid_get_bin() - this function get GUID bin for string
262 *
263 * @param guid_str - pointer to partition type string
264 * @param guid_bin - pointer to allocated array for big endian output [16B]
265 */
266int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
267{
268 int i;
269
270 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
271 if (!strcmp(list_guid[i].string, guid_str)) {
272 memcpy(guid_bin, &list_guid[i].guid, 16);
273 return 0;
274 }
275 }
276 return -ENODEV;
277}
278
279/*
280 * uuid_guid_get_str() - this function get string for GUID.
281 *
282 * @param guid_bin - pointer to string with partition type guid [16B]
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100283 *
284 * Returns NULL if the type GUID is not known.
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100285 */
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100286const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100287{
288 int i;
289
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100290 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
291 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100292 return list_guid[i].string;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100293 }
294 }
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100295 return NULL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100296}
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100297
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200298/*
299 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
300 *
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100301 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200302 * @param uuid_bin - pointer to allocated array for big endian output [16B]
303 * @str_format - UUID string format: 0 - UUID; 1 - GUID
304 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600305int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
306 int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000307{
308 uint16_t tmp16;
309 uint32_t tmp32;
310 uint64_t tmp64;
311
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100312 if (!uuid_str_valid(uuid_str)) {
313#ifdef CONFIG_PARTITION_TYPE_GUID
314 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
315 return 0;
316#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200317 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100318 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200319
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200320 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600321 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200322 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000323
Simon Glass3ff49ec2021-07-24 09:03:29 -0600324 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200325 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000326
Simon Glass3ff49ec2021-07-24 09:03:29 -0600327 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200328 memcpy(uuid_bin + 6, &tmp16, 2);
329 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600330 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200331 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000332
Simon Glass3ff49ec2021-07-24 09:03:29 -0600333 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200334 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000335
Simon Glass3ff49ec2021-07-24 09:03:29 -0600336 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200337 memcpy(uuid_bin + 6, &tmp16, 2);
338 }
339
Simon Glass3ff49ec2021-07-24 09:03:29 -0600340 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200341 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000342
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200343 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
344 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200345
346 return 0;
347}
348
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200349/*
350 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
351 *
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200352 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
353 * @param uuid_str: pointer to allocated array for output string [37B]
354 * @str_format: bit 0: 0 - UUID; 1 - GUID
355 * bit 1: 0 - lower case; 2 - upper case
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200356 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600357void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
358 int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200359{
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200360 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
361 9, 10, 11, 12, 13, 14, 15};
362 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
363 9, 10, 11, 12, 13, 14, 15};
364 const u8 *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200365 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200366 int i;
367
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200368 /*
369 * UUID and GUID bin data - always in big endian:
370 * 4B-2B-2B-2B-6B
371 * be be be be be
372 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200373 if (str_format & UUID_STR_FORMAT_GUID)
374 char_order = guid_char_order;
375 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200376 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200377 if (str_format & UUID_STR_UPPER_CASE)
378 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200379 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200380 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200381
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200382 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200383 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200384 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200385 switch (i) {
386 case 3:
387 case 5:
388 case 7:
389 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200390 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200391 break;
392 }
393 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000394}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200395
396/*
397 * gen_rand_uuid() - this function generates a random binary UUID version 4.
398 * In this version all fields beside 4 bits of version and
399 * 2 bits of variant are randomly generated.
400 *
401 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
402*/
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200403#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200404void gen_rand_uuid(unsigned char *uuid_bin)
405{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200406 u32 ptr[4];
407 struct uuid *uuid = (struct uuid *)ptr;
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100408 int i, ret;
409 struct udevice *devp;
410 u32 randv = 0;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200411
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100412 if (IS_ENABLED(CONFIG_DM_RNG)) {
413 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunaya6064f72021-10-22 17:05:47 +0200414 if (!ret) {
Matthias Bruggerd03560b2020-12-18 10:28:03 +0100415 ret = dm_rng_read(devp, &randv, sizeof(randv));
416 if (ret < 0)
417 randv = 0;
418 }
419 }
420 if (randv)
421 srand(randv);
422 else
423 srand(get_ticks() + rand());
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200424
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200425 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200426 for (i = 0; i < 4; i++)
427 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200428
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200429 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200430 UUID_VERSION_MASK,
431 UUID_VERSION << UUID_VERSION_SHIFT);
432
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200433 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200434 UUID_VARIANT_MASK,
435 UUID_VARIANT << UUID_VARIANT_SHIFT);
436
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200437 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200438}
439
440/*
441 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
442 * formats UUID or GUID.
443 *
444 * @param uuid_str - pointer to allocated array [37B].
445 * @param - uuid output type: UUID - 0, GUID - 1
446 */
447void gen_rand_uuid_str(char *uuid_str, int str_format)
448{
449 unsigned char uuid_bin[UUID_BIN_LEN];
450
451 /* Generate UUID (big endian) */
452 gen_rand_uuid(uuid_bin);
453
454 /* Convert UUID bin to UUID or GUID formated STRING */
455 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
456}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200457
Marek Vasut51d85f62019-01-07 21:23:38 +0100458#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glassed38aef2020-05-10 11:40:03 -0600459int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200460{
461 char uuid[UUID_STR_LEN + 1];
462 int str_format;
463
464 if (!strcmp(argv[0], "uuid"))
465 str_format = UUID_STR_FORMAT_STD;
466 else
467 str_format = UUID_STR_FORMAT_GUID;
468
469 if (argc > 2)
470 return CMD_RET_USAGE;
471
472 gen_rand_uuid_str(uuid, str_format);
473
474 if (argc == 1)
475 printf("%s\n", uuid);
476 else
Simon Glass6a38e412017-08-03 12:22:09 -0600477 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200478
479 return CMD_RET_SUCCESS;
480}
481
482U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
483 "UUID - generate random Universally Unique Identifier",
484 "[<varname>]\n"
485 "Argument:\n"
486 "varname: for set result in a environment variable\n"
487 "e.g. uuid uuid_env"
488);
489
490U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
491 "GUID - generate Globally Unique Identifier based on random UUID",
492 "[<varname>]\n"
493 "Argument:\n"
494 "varname: for set result in a environment variable\n"
495 "e.g. guid guid_env"
496);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200497#endif /* CONFIG_CMD_UUID */
498#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */