1
2
3
4
5
6#include <common.h>
7#include <command.h>
8#include <efi_api.h>
9#include <env.h>
10#include <rand.h>
11#include <time.h>
12#include <uuid.h>
13#include <linux/ctype.h>
14#include <errno.h>
15#include <common.h>
16#include <asm/io.h>
17#include <part_efi.h>
18#include <malloc.h>
19#include <dm/uclass.h>
20#include <rng.h>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66int 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
84 if (i != UUID_STR_LEN || !valid)
85 return 0;
86
87 return 1;
88}
89
90static const struct {
91 const char *string;
92 efi_guid_t guid;
93} list_guid[] = {
94#ifdef CONFIG_PARTITION_TYPE_GUID
95 {"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},
102 {"lvm", PARTITION_LINUX_LVM_GUID},
103 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
104#endif
105#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
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 {
187 "TCG2",
188 EFI_TCG2_PROTOCOL_GUID,
189 },
190 {
191 "System Partition",
192 PARTITION_SYSTEM_GUID
193 },
194 {
195 "Firmware Management",
196 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
197 },
198
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 },
223 {
224 "EFI Conformance Profiles Table",
225 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
226 },
227#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
228 {
229 "RISC-V Boot",
230 RISCV_EFI_BOOT_PROTOCOL_GUID,
231 },
232#endif
233#endif
234#ifdef CONFIG_CMD_NVEDIT_EFI
235
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
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
258#ifdef CONFIG_EFI
259 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
260 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
261 { "EFI_HOB_LIST", EFI_HOB_LIST },
262 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
263 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
264 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
265#endif
266};
267
268
269
270
271
272
273
274int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
275{
276 int i;
277
278 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
279 if (!strcmp(list_guid[i].string, guid_str)) {
280 memcpy(guid_bin, &list_guid[i].guid, 16);
281 return 0;
282 }
283 }
284 return -ENODEV;
285}
286
287
288
289
290
291
292
293
294const char *uuid_guid_get_str(const unsigned char *guid_bin)
295{
296 int i;
297
298 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
299 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
300 return list_guid[i].string;
301 }
302 }
303 return NULL;
304}
305
306
307
308
309
310
311
312
313int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
314 int str_format)
315{
316 uint16_t tmp16;
317 uint32_t tmp32;
318 uint64_t tmp64;
319
320 if (!uuid_str_valid(uuid_str)) {
321#ifdef CONFIG_PARTITION_TYPE_GUID
322 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
323 return 0;
324#endif
325 return -EINVAL;
326 }
327
328 if (str_format == UUID_STR_FORMAT_STD) {
329 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
330 memcpy(uuid_bin, &tmp32, 4);
331
332 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
333 memcpy(uuid_bin + 4, &tmp16, 2);
334
335 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
336 memcpy(uuid_bin + 6, &tmp16, 2);
337 } else {
338 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
339 memcpy(uuid_bin, &tmp32, 4);
340
341 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
342 memcpy(uuid_bin + 4, &tmp16, 2);
343
344 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
345 memcpy(uuid_bin + 6, &tmp16, 2);
346 }
347
348 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
349 memcpy(uuid_bin + 8, &tmp16, 2);
350
351 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
352 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
353
354 return 0;
355}
356
357
358
359
360
361
362
363
364
365void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
366 int str_format)
367{
368 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
369 9, 10, 11, 12, 13, 14, 15};
370 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
371 9, 10, 11, 12, 13, 14, 15};
372 const u8 *char_order;
373 const char *format;
374 int i;
375
376
377
378
379
380
381 if (str_format & UUID_STR_FORMAT_GUID)
382 char_order = guid_char_order;
383 else
384 char_order = uuid_char_order;
385 if (str_format & UUID_STR_UPPER_CASE)
386 format = "%02X";
387 else
388 format = "%02x";
389
390 for (i = 0; i < 16; i++) {
391 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
392 uuid_str += 2;
393 switch (i) {
394 case 3:
395 case 5:
396 case 7:
397 case 9:
398 *uuid_str++ = '-';
399 break;
400 }
401 }
402}
403
404
405
406
407
408
409
410
411#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
412void gen_rand_uuid(unsigned char *uuid_bin)
413{
414 u32 ptr[4];
415 struct uuid *uuid = (struct uuid *)ptr;
416 int i, ret;
417 struct udevice *devp;
418 u32 randv = 0;
419
420 if (IS_ENABLED(CONFIG_DM_RNG)) {
421 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
422 if (!ret) {
423 ret = dm_rng_read(devp, &randv, sizeof(randv));
424 if (ret < 0)
425 randv = 0;
426 }
427 }
428 if (randv)
429 srand(randv);
430 else
431 srand(get_ticks() + rand());
432
433
434 for (i = 0; i < 4; i++)
435 ptr[i] = rand();
436
437 clrsetbits_be16(&uuid->time_hi_and_version,
438 UUID_VERSION_MASK,
439 UUID_VERSION << UUID_VERSION_SHIFT);
440
441 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
442 UUID_VARIANT_MASK,
443 UUID_VARIANT << UUID_VARIANT_SHIFT);
444
445 memcpy(uuid_bin, uuid, 16);
446}
447
448
449
450
451
452
453
454
455void gen_rand_uuid_str(char *uuid_str, int str_format)
456{
457 unsigned char uuid_bin[UUID_BIN_LEN];
458
459
460 gen_rand_uuid(uuid_bin);
461
462
463 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
464}
465
466#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
467int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
468{
469 char uuid[UUID_STR_LEN + 1];
470 int str_format;
471
472 if (!strcmp(argv[0], "uuid"))
473 str_format = UUID_STR_FORMAT_STD;
474 else
475 str_format = UUID_STR_FORMAT_GUID;
476
477 if (argc > 2)
478 return CMD_RET_USAGE;
479
480 gen_rand_uuid_str(uuid, str_format);
481
482 if (argc == 1)
483 printf("%s\n", uuid);
484 else
485 env_set(argv[1], uuid);
486
487 return CMD_RET_SUCCESS;
488}
489
490U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
491 "UUID - generate random Universally Unique Identifier",
492 "[<varname>]\n"
493 "Argument:\n"
494 "varname: for set result in a environment variable\n"
495 "e.g. uuid uuid_env"
496);
497
498U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
499 "GUID - generate Globally Unique Identifier based on random UUID",
500 "[<varname>]\n"
501 "Argument:\n"
502 "varname: for set result in a environment variable\n"
503 "e.g. guid guid_env"
504);
505#endif
506#endif
507