blob: c9dfdf007a18b4943740d4ddd9f8a79cdaf139ea [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
Simon Glass09140112020-05-10 11:40:03 -060012#include <command.h>
Heinrich Schuchardt3adae642022-01-16 14:10:23 +010013#include <efi_api.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060014#include <env.h>
Simon Glass90526e92020-05-10 11:39:56 -060015#include <rand.h>
Simon Glass10453152019-11-14 12:57:30 -070016#include <time.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060017#include <uuid.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000018#include <linux/ctype.h>
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020019#include <errno.h>
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020020#include <asm/io.h>
21#include <part_efi.h>
22#include <malloc.h>
Matthias Brugger92fdad22020-12-18 10:28:03 +010023#include <dm/uclass.h>
24#include <rng.h>
Caleb Connolly4c5e1ff2024-08-30 13:34:32 +010025#include <u-boot/sha1.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000026
Jason Hobbse11938e2011-08-23 11:06:56 +000027int uuid_str_valid(const char *uuid)
28{
29 int i, valid;
30
31 if (uuid == NULL)
32 return 0;
33
34 for (i = 0, valid = 1; uuid[i] && valid; i++) {
35 switch (i) {
36 case 8: case 13: case 18: case 23:
37 valid = (uuid[i] == '-');
38 break;
39 default:
40 valid = isxdigit(uuid[i]);
41 break;
42 }
43 }
44
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020045 if (i != UUID_STR_LEN || !valid)
Jason Hobbse11938e2011-08-23 11:06:56 +000046 return 0;
47
48 return 1;
49}
50
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +010051static const struct {
52 const char *string;
53 efi_guid_t guid;
54} list_guid[] = {
Heinrich Schuchardtc1528f32022-01-16 11:55:39 +010055#ifdef CONFIG_PARTITION_TYPE_GUID
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +010056 {"system", PARTITION_SYSTEM_GUID},
57 {"mbr", LEGACY_MBR_PARTITION_GUID},
58 {"msft", PARTITION_MSFT_RESERVED_GUID},
59 {"data", PARTITION_BASIC_DATA_GUID},
60 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
61 {"raid", PARTITION_LINUX_RAID_GUID},
62 {"swap", PARTITION_LINUX_SWAP_GUID},
Rasmus Villemoesc0364ce2020-11-20 11:45:36 +010063 {"lvm", PARTITION_LINUX_LVM_GUID},
64 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
Simon Glass966b16c2023-08-24 13:55:44 -060065 {"cros-kern", PARTITION_CROS_KERNEL},
66 {"cros-root", PARTITION_CROS_ROOT},
67 {"cros-fw", PARTITION_CROS_FIRMWARE},
68 {"cros-rsrv", PARTITION_CROS_RESERVED},
Heinrich Schuchardtc1528f32022-01-16 11:55:39 +010069#endif
Simon Glass4e5e49a2023-03-20 08:30:12 +130070#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Heinrich Schuchardt3adae642022-01-16 14:10:23 +010071 {
72 "Device Path",
73 EFI_DEVICE_PATH_PROTOCOL_GUID,
74 },
75 {
76 "Device Path To Text",
77 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
78 },
79 {
80 "Device Path Utilities",
81 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
82 },
83 {
84 "Unicode Collation 2",
85 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
86 },
87 {
88 "Driver Binding",
89 EFI_DRIVER_BINDING_PROTOCOL_GUID,
90 },
91 {
92 "Simple Text Input",
93 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
94 },
95 {
96 "Simple Text Input Ex",
97 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
98 },
99 {
100 "Simple Text Output",
101 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
102 },
103 {
104 "Block IO",
105 EFI_BLOCK_IO_PROTOCOL_GUID,
106 },
107 {
108 "Simple File System",
109 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
110 },
111 {
112 "Loaded Image",
113 EFI_LOADED_IMAGE_PROTOCOL_GUID,
114 },
115 {
116 "Graphics Output",
117 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
118 },
119 {
120 "HII String",
121 EFI_HII_STRING_PROTOCOL_GUID,
122 },
123 {
124 "HII Database",
125 EFI_HII_DATABASE_PROTOCOL_GUID,
126 },
127 {
128 "HII Config Routing",
129 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
130 },
131 {
132 "Load File2",
133 EFI_LOAD_FILE2_PROTOCOL_GUID,
134 },
135 {
136 "Random Number Generator",
137 EFI_RNG_PROTOCOL_GUID,
138 },
139 {
140 "Simple Network",
141 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
142 },
143 {
144 "PXE Base Code",
145 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
146 },
147 {
148 "Device-Tree Fixup",
149 EFI_DT_FIXUP_PROTOCOL_GUID,
150 },
151 {
Heinrich Schuchardt38040a62022-01-16 17:46:38 +0100152 "TCG2",
153 EFI_TCG2_PROTOCOL_GUID,
154 },
155 {
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100156 "System Partition",
157 PARTITION_SYSTEM_GUID
158 },
159 {
160 "Firmware Management",
161 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
162 },
163 /* Configuration table GUIDs */
164 {
165 "ACPI table",
166 EFI_ACPI_TABLE_GUID,
167 },
168 {
169 "EFI System Resource Table",
170 EFI_SYSTEM_RESOURCE_TABLE_GUID,
171 },
172 {
173 "device tree",
174 EFI_FDT_GUID,
175 },
176 {
177 "SMBIOS table",
178 SMBIOS_TABLE_GUID,
179 },
180 {
Heinrich Schuchardt91cc06b2024-01-22 14:04:35 +0100181 "SMBIOS3 table",
182 SMBIOS3_TABLE_GUID,
183 },
184 {
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100185 "Runtime properties",
186 EFI_RT_PROPERTIES_TABLE_GUID,
187 },
188 {
189 "TCG2 Final Events Table",
190 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
191 },
Jose Marinho6b92c172021-12-23 14:51:07 +0000192 {
193 "EFI Conformance Profiles Table",
194 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
195 },
Heinrich Schuchardt8d4c4262022-01-28 19:29:47 +0100196#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
197 {
198 "RISC-V Boot",
199 RISCV_EFI_BOOT_PROTOCOL_GUID,
200 },
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100201#endif
Heinrich Schuchardt8d4c4262022-01-28 19:29:47 +0100202#endif /* CONFIG_CMD_EFIDEBUG */
Heinrich Schuchardt983a5a22022-01-16 14:53:37 +0100203#ifdef CONFIG_CMD_NVEDIT_EFI
204 /* signature database */
205 {
206 "EFI_GLOBAL_VARIABLE_GUID",
207 EFI_GLOBAL_VARIABLE_GUID,
208 },
209 {
210 "EFI_IMAGE_SECURITY_DATABASE_GUID",
211 EFI_IMAGE_SECURITY_DATABASE_GUID,
212 },
213 /* certificate types */
214 {
215 "EFI_CERT_SHA256_GUID",
216 EFI_CERT_SHA256_GUID,
217 },
218 {
219 "EFI_CERT_X509_GUID",
220 EFI_CERT_X509_GUID,
221 },
222 {
223 "EFI_CERT_TYPE_PKCS7_GUID",
224 EFI_CERT_TYPE_PKCS7_GUID,
225 },
226#endif
Simon Glass37108022023-07-15 21:38:51 -0600227#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
Simon Glassac932752023-03-20 08:30:11 +1300228 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
229 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
230 { "EFI_HOB_LIST", EFI_HOB_LIST },
231 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
232 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
233 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
234#endif
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100235};
236
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100237int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
238{
239 int i;
240
241 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
242 if (!strcmp(list_guid[i].string, guid_str)) {
243 memcpy(guid_bin, &list_guid[i].guid, 16);
244 return 0;
245 }
246 }
247 return -ENODEV;
248}
249
Rasmus Villemoes31ce3672020-11-20 11:45:35 +0100250const char *uuid_guid_get_str(const unsigned char *guid_bin)
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100251{
252 int i;
253
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100254 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
255 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
Rasmus Villemoes31ce3672020-11-20 11:45:35 +0100256 return list_guid[i].string;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100257 }
258 }
Rasmus Villemoes31ce3672020-11-20 11:45:35 +0100259 return NULL;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100260}
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100261
Simon Glass2c2ca202020-04-08 08:32:58 -0600262int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
263 int str_format)
Jason Hobbse11938e2011-08-23 11:06:56 +0000264{
265 uint16_t tmp16;
266 uint32_t tmp32;
267 uint64_t tmp64;
268
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100269 if (!uuid_str_valid(uuid_str)) {
Simon Glass966b16c2023-08-24 13:55:44 -0600270 log_debug("not valid\n");
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100271#ifdef CONFIG_PARTITION_TYPE_GUID
272 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
273 return 0;
274#endif
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200275 return -EINVAL;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100276 }
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200277
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200278 if (str_format == UUID_STR_FORMAT_STD) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600279 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200280 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000281
Simon Glass7e5f4602021-07-24 09:03:29 -0600282 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200283 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000284
Simon Glass7e5f4602021-07-24 09:03:29 -0600285 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200286 memcpy(uuid_bin + 6, &tmp16, 2);
287 } else {
Simon Glass7e5f4602021-07-24 09:03:29 -0600288 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200289 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000290
Simon Glass7e5f4602021-07-24 09:03:29 -0600291 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200292 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000293
Simon Glass7e5f4602021-07-24 09:03:29 -0600294 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200295 memcpy(uuid_bin + 6, &tmp16, 2);
296 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000297
Simon Glass7e5f4602021-07-24 09:03:29 -0600298 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200299 memcpy(uuid_bin + 8, &tmp16, 2);
300
301 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
302 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200303
304 return 0;
305}
306
Abdellatif El Khlifi7048f262023-08-04 14:33:38 +0100307int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
308{
309 u16 tmp16;
310 u32 tmp32;
311 u64 tmp64;
312
313 if (!uuid_str_valid(uuid_str) || !uuid_bin)
314 return -EINVAL;
315
316 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
317 memcpy(uuid_bin, &tmp32, 4);
318
319 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
320 memcpy(uuid_bin + 4, &tmp16, 2);
321
322 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
323 memcpy(uuid_bin + 6, &tmp16, 2);
324
325 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
326 memcpy(uuid_bin + 8, &tmp16, 2);
327
328 tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
329 memcpy(uuid_bin + 10, &tmp64, 6);
330
331 return 0;
332}
333
Simon Glass2c2ca202020-04-08 08:32:58 -0600334void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
335 int str_format)
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200336{
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200337 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
338 9, 10, 11, 12, 13, 14, 15};
339 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
340 9, 10, 11, 12, 13, 14, 15};
341 const u8 *char_order;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200342 const char *format;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200343 int i;
344
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200345 /*
346 * UUID and GUID bin data - always in big endian:
347 * 4B-2B-2B-2B-6B
348 * be be be be be
349 */
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200350 if (str_format & UUID_STR_FORMAT_GUID)
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200351 char_order = guid_char_order;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200352 else
353 char_order = uuid_char_order;
354 if (str_format & UUID_STR_UPPER_CASE)
355 format = "%02X";
356 else
357 format = "%02x";
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200358
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200359 for (i = 0; i < 16; i++) {
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200360 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200361 uuid_str += 2;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200362 switch (i) {
363 case 3:
364 case 5:
365 case 7:
366 case 9:
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200367 *uuid_str++ = '-';
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200368 break;
369 }
370 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000371}
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200372
Caleb Connolly4c5e1ff2024-08-30 13:34:32 +0100373static void configure_uuid(struct uuid *uuid, unsigned char version)
374{
375 uint16_t tmp;
376
377 /* Configure variant/version bits */
378 tmp = be16_to_cpu(uuid->time_hi_and_version);
379 tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT);
380 uuid->time_hi_and_version = cpu_to_be16(tmp);
381
382 uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK;
383 uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
384}
385
386void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...)
387{
388 sha1_context ctx;
389 va_list args;
390 const uint8_t *data;
391 uint32_t *tmp32;
392 uint16_t *tmp16;
393 uint8_t hash[SHA1_SUM_LEN];
394
395 sha1_starts(&ctx);
396 /* Hash the namespace UUID as salt */
397 sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
398 va_start(args, guid);
399
400 while ((data = va_arg(args, const uint8_t *))) {
401 unsigned int len = va_arg(args, size_t);
402
403 sha1_update(&ctx, data, len);
404 }
405
406 va_end(args);
407 sha1_finish(&ctx, hash);
408
409 /* Truncate the hash into output UUID, it is already big endian */
410 memcpy(guid, hash, sizeof(*guid));
411
412 configure_uuid((struct uuid *)guid, 5);
413
414 /* Make little endian */
415 tmp32 = (uint32_t *)&guid->b[0];
416 *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32));
417 tmp16 = (uint16_t *)&guid->b[4];
418 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
419 tmp16 = (uint16_t *)&guid->b[6];
420 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
421}
422
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200423#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200424void gen_rand_uuid(unsigned char *uuid_bin)
425{
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200426 u32 ptr[4];
427 struct uuid *uuid = (struct uuid *)ptr;
Matthias Brugger92fdad22020-12-18 10:28:03 +0100428 int i, ret;
429 struct udevice *devp;
430 u32 randv = 0;
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200431
Marek Vasut591257b2024-04-26 01:02:07 +0200432 if (CONFIG_IS_ENABLED(DM_RNG)) {
Matthias Brugger92fdad22020-12-18 10:28:03 +0100433 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
Patrick Delaunay70a9f4d2021-10-22 17:05:47 +0200434 if (!ret) {
Matthias Brugger92fdad22020-12-18 10:28:03 +0100435 ret = dm_rng_read(devp, &randv, sizeof(randv));
436 if (ret < 0)
437 randv = 0;
438 }
439 }
440 if (randv)
441 srand(randv);
442 else
443 srand(get_ticks() + rand());
Eugeniu Rosca4ccf6782019-05-02 14:27:06 +0200444
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200445 /* Set all fields randomly */
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200446 for (i = 0; i < 4; i++)
447 ptr[i] = rand();
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200448
Caleb Connolly4c5e1ff2024-08-30 13:34:32 +0100449 configure_uuid(uuid, UUID_VERSION);
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200450
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200451 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200452}
453
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200454void gen_rand_uuid_str(char *uuid_str, int str_format)
455{
456 unsigned char uuid_bin[UUID_BIN_LEN];
457
458 /* Generate UUID (big endian) */
459 gen_rand_uuid(uuid_bin);
460
461 /* Convert UUID bin to UUID or GUID formated STRING */
462 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
463}
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200464
Marek Vasut05f6da32019-01-07 21:23:38 +0100465#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Simon Glass09140112020-05-10 11:40:03 -0600466int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200467{
468 char uuid[UUID_STR_LEN + 1];
469 int str_format;
470
471 if (!strcmp(argv[0], "uuid"))
472 str_format = UUID_STR_FORMAT_STD;
473 else
474 str_format = UUID_STR_FORMAT_GUID;
475
476 if (argc > 2)
477 return CMD_RET_USAGE;
478
479 gen_rand_uuid_str(uuid, str_format);
480
481 if (argc == 1)
482 printf("%s\n", uuid);
483 else
Simon Glass382bee52017-08-03 12:22:09 -0600484 env_set(argv[1], uuid);
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200485
486 return CMD_RET_SUCCESS;
487}
488
489U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
490 "UUID - generate random Universally Unique Identifier",
491 "[<varname>]\n"
492 "Argument:\n"
493 "varname: for set result in a environment variable\n"
494 "e.g. uuid uuid_env"
495);
496
497U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
498 "GUID - generate Globally Unique Identifier based on random UUID",
499 "[<varname>]\n"
500 "Argument:\n"
501 "varname: for set result in a environment variable\n"
502 "e.g. guid guid_env"
503);
Przemyslaw Marczak39206382014-04-02 10:20:06 +0200504#endif /* CONFIG_CMD_UUID */
505#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */