]> Git Repo - J-u-boot.git/blame - lib/uuid.c
rockchip: rk3399-gru: Include pinctrl and regulators in SPL
[J-u-boot.git] / lib / uuid.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
e11938ea
JH
2/*
3 * Copyright 2011 Calxeda, Inc.
7048f26c
AEK
4 * Copyright 2022-2023 Arm Limited and/or its affiliates <[email protected]>
5 *
6 * Authors:
7 * Abdellatif El Khlifi <[email protected]>
e11938ea
JH
8 */
9
22c48a92 10#ifndef USE_HOSTCC
09140113 11#include <command.h>
3adae642 12#include <efi_api.h>
9fb625ce 13#include <env.h>
90526e9f 14#include <rand.h>
1045315d 15#include <time.h>
d718ded0
PM
16#include <asm/io.h>
17#include <part_efi.h>
18#include <malloc.h>
92fdad28
MB
19#include <dm/uclass.h>
20#include <rng.h>
22c48a92
CC
21#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>
58d825fb 32#include <u-boot/uuid.h>
4c5e1ff3 33#include <u-boot/sha1.h>
e11938ea 34
22c48a92
CC
35#ifdef USE_HOSTCC
36/* polyfill hextoul to avoid pulling in strto.c */
37#define hextoul(cp, endp) strtoul(cp, endp, 16)
e5a0cb3e 38#define hextoull(cp, endp) strtoull(cp, endp, 16)
22c48a92
CC
39#endif
40
e11938ea
JH
41int 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
d718ded0 59 if (i != UUID_STR_LEN || !valid)
e11938ea
JH
60 return 0;
61
62 return 1;
63}
64
bcb41dca
PD
65static const struct {
66 const char *string;
67 efi_guid_t guid;
68} list_guid[] = {
22c48a92 69#ifndef USE_HOSTCC
c1528f32 70#ifdef CONFIG_PARTITION_TYPE_GUID
bcb41dca
PD
71 {"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},
c0364ce1
RV
78 {"lvm", PARTITION_LINUX_LVM_GUID},
79 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
966b16c5
SG
80 {"cros-kern", PARTITION_CROS_KERNEL},
81 {"cros-root", PARTITION_CROS_ROOT},
82 {"cros-fw", PARTITION_CROS_FIRMWARE},
83 {"cros-rsrv", PARTITION_CROS_RESERVED},
c1528f32 84#endif
4e5e49a3 85#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
3adae642
HS
86 {
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 },
38040a63
HS
166 {
167 "TCG2",
168 EFI_TCG2_PROTOCOL_GUID,
169 },
3adae642
HS
170 {
171 "System Partition",
172 PARTITION_SYSTEM_GUID
173 },
174 {
175 "Firmware Management",
176 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
177 },
6df3af4f
HS
178#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
179 {
180 "HTTP",
181 EFI_HTTP_PROTOCOL_GUID,
182 },
183 {
184 "HTTP Service Binding",
185 EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID,
186 },
187 {
188 "IPv4 Config2",
189 EFI_IP4_CONFIG2_PROTOCOL_GUID,
190 },
191#endif
3adae642
HS
192 /* Configuration table GUIDs */
193 {
194 "ACPI table",
195 EFI_ACPI_TABLE_GUID,
196 },
197 {
198 "EFI System Resource Table",
199 EFI_SYSTEM_RESOURCE_TABLE_GUID,
200 },
201 {
202 "device tree",
203 EFI_FDT_GUID,
204 },
205 {
206 "SMBIOS table",
207 SMBIOS_TABLE_GUID,
208 },
91cc06bc
HS
209 {
210 "SMBIOS3 table",
211 SMBIOS3_TABLE_GUID,
212 },
3adae642
HS
213 {
214 "Runtime properties",
215 EFI_RT_PROPERTIES_TABLE_GUID,
216 },
217 {
218 "TCG2 Final Events Table",
219 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
220 },
6b92c173
JM
221 {
222 "EFI Conformance Profiles Table",
223 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
224 },
8d4c4265
HS
225#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
226 {
227 "RISC-V Boot",
228 RISCV_EFI_BOOT_PROTOCOL_GUID,
229 },
3adae642 230#endif
8d4c4265 231#endif /* CONFIG_CMD_EFIDEBUG */
983a5a2e
HS
232#ifdef CONFIG_CMD_NVEDIT_EFI
233 /* signature database */
234 {
235 "EFI_GLOBAL_VARIABLE_GUID",
236 EFI_GLOBAL_VARIABLE_GUID,
237 },
238 {
239 "EFI_IMAGE_SECURITY_DATABASE_GUID",
240 EFI_IMAGE_SECURITY_DATABASE_GUID,
241 },
242 /* certificate types */
243 {
244 "EFI_CERT_SHA256_GUID",
245 EFI_CERT_SHA256_GUID,
246 },
247 {
248 "EFI_CERT_X509_GUID",
249 EFI_CERT_X509_GUID,
250 },
251 {
252 "EFI_CERT_TYPE_PKCS7_GUID",
253 EFI_CERT_TYPE_PKCS7_GUID,
254 },
255#endif
3710802e 256#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
ac93275d
SG
257 { "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
258 { "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
259 { "EFI_HOB_LIST", EFI_HOB_LIST },
260 { "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
261 { "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
262 { "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
263#endif
22c48a92 264#endif /* !USE_HOSTCC */
bcb41dca
PD
265};
266
bcb41dca
PD
267int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
268{
269 int i;
270
271 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
272 if (!strcmp(list_guid[i].string, guid_str)) {
273 memcpy(guid_bin, &list_guid[i].guid, 16);
274 return 0;
275 }
276 }
277 return -ENODEV;
278}
279
31ce367c 280const char *uuid_guid_get_str(const unsigned char *guid_bin)
bcb41dca
PD
281{
282 int i;
283
bcb41dca
PD
284 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
285 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
31ce367c 286 return list_guid[i].string;
bcb41dca
PD
287 }
288 }
31ce367c 289 return NULL;
bcb41dca 290}
bcb41dca 291
2c2ca207
SG
292int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
293 int str_format)
e11938ea
JH
294{
295 uint16_t tmp16;
296 uint32_t tmp32;
297 uint64_t tmp64;
298
bcb41dca
PD
299 if (!uuid_str_valid(uuid_str)) {
300#ifdef CONFIG_PARTITION_TYPE_GUID
301 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
302 return 0;
303#endif
a96a0e61 304 return -EINVAL;
bcb41dca 305 }
a96a0e61 306
d718ded0 307 if (str_format == UUID_STR_FORMAT_STD) {
7e5f460e 308 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
d718ded0 309 memcpy(uuid_bin, &tmp32, 4);
e11938ea 310
7e5f460e 311 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
d718ded0 312 memcpy(uuid_bin + 4, &tmp16, 2);
e11938ea 313
7e5f460e 314 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
d718ded0
PM
315 memcpy(uuid_bin + 6, &tmp16, 2);
316 } else {
7e5f460e 317 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
d718ded0 318 memcpy(uuid_bin, &tmp32, 4);
e11938ea 319
7e5f460e 320 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
d718ded0 321 memcpy(uuid_bin + 4, &tmp16, 2);
e11938ea 322
7e5f460e 323 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
d718ded0
PM
324 memcpy(uuid_bin + 6, &tmp16, 2);
325 }
326
7e5f460e 327 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
d718ded0 328 memcpy(uuid_bin + 8, &tmp16, 2);
e11938ea 329
6a2664b1 330 tmp64 = cpu_to_be64(hextoull(uuid_str + 24, NULL));
d718ded0 331 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
a96a0e61
PM
332
333 return 0;
334}
335
7048f26c
AEK
336int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
337{
22c48a92
CC
338 uint16_t tmp16;
339 uint32_t tmp32;
340 uint64_t tmp64;
7048f26c
AEK
341
342 if (!uuid_str_valid(uuid_str) || !uuid_bin)
343 return -EINVAL;
344
345 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
346 memcpy(uuid_bin, &tmp32, 4);
347
348 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
349 memcpy(uuid_bin + 4, &tmp16, 2);
350
351 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
352 memcpy(uuid_bin + 6, &tmp16, 2);
353
354 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
355 memcpy(uuid_bin + 8, &tmp16, 2);
356
e5a0cb3e 357 tmp64 = cpu_to_le64(hextoull(uuid_str + 24, NULL));
7048f26c
AEK
358 memcpy(uuid_bin + 10, &tmp64, 6);
359
360 return 0;
361}
362
2c2ca207
SG
363void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
364 int str_format)
a96a0e61 365{
22c48a92 366 const uint8_t uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
d718ded0 367 9, 10, 11, 12, 13, 14, 15};
22c48a92 368 const uint8_t guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
d718ded0 369 9, 10, 11, 12, 13, 14, 15};
22c48a92 370 const uint8_t *char_order;
3bad256f 371 const char *format;
a96a0e61
PM
372 int i;
373
d718ded0
PM
374 /*
375 * UUID and GUID bin data - always in big endian:
376 * 4B-2B-2B-2B-6B
377 * be be be be be
378 */
3bad256f
HS
379 if (str_format & UUID_STR_FORMAT_GUID)
380 char_order = guid_char_order;
381 else
d718ded0 382 char_order = uuid_char_order;
3bad256f
HS
383 if (str_format & UUID_STR_UPPER_CASE)
384 format = "%02X";
d718ded0 385 else
3bad256f 386 format = "%02x";
d718ded0 387
a96a0e61 388 for (i = 0; i < 16; i++) {
3bad256f 389 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
d718ded0 390 uuid_str += 2;
a96a0e61
PM
391 switch (i) {
392 case 3:
393 case 5:
394 case 7:
395 case 9:
d718ded0 396 *uuid_str++ = '-';
a96a0e61
PM
397 break;
398 }
399 }
e11938ea 400}
4e4815fe 401
4c5e1ff3
CC
402static void configure_uuid(struct uuid *uuid, unsigned char version)
403{
404 uint16_t tmp;
405
406 /* Configure variant/version bits */
407 tmp = be16_to_cpu(uuid->time_hi_and_version);
408 tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT);
409 uuid->time_hi_and_version = cpu_to_be16(tmp);
410
411 uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK;
412 uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
413}
414
415void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...)
416{
417 sha1_context ctx;
418 va_list args;
419 const uint8_t *data;
420 uint32_t *tmp32;
421 uint16_t *tmp16;
422 uint8_t hash[SHA1_SUM_LEN];
423
424 sha1_starts(&ctx);
425 /* Hash the namespace UUID as salt */
426 sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
427 va_start(args, guid);
428
429 while ((data = va_arg(args, const uint8_t *))) {
430 unsigned int len = va_arg(args, size_t);
431
432 sha1_update(&ctx, data, len);
433 }
434
435 va_end(args);
436 sha1_finish(&ctx, hash);
437
438 /* Truncate the hash into output UUID, it is already big endian */
439 memcpy(guid, hash, sizeof(*guid));
440
441 configure_uuid((struct uuid *)guid, 5);
442
443 /* Make little endian */
444 tmp32 = (uint32_t *)&guid->b[0];
445 *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32));
446 tmp16 = (uint16_t *)&guid->b[4];
447 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
448 tmp16 = (uint16_t *)&guid->b[6];
449 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
450}
451
22c48a92 452#ifndef USE_HOSTCC
89c8230d 453#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
4e4815fe
PM
454void gen_rand_uuid(unsigned char *uuid_bin)
455{
a1b633df
HS
456 u32 ptr[4];
457 struct uuid *uuid = (struct uuid *)ptr;
92fdad28
MB
458 int i, ret;
459 struct udevice *devp;
460 u32 randv = 0;
461
591257b0 462 if (CONFIG_IS_ENABLED(DM_RNG)) {
92fdad28 463 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
70a9f4d2 464 if (!ret) {
92fdad28
MB
465 ret = dm_rng_read(devp, &randv, sizeof(randv));
466 if (ret < 0)
467 randv = 0;
468 }
469 }
470 if (randv)
471 srand(randv);
472 else
473 srand(get_ticks() + rand());
4ccf678f 474
4e4815fe 475 /* Set all fields randomly */
a1b633df
HS
476 for (i = 0; i < 4; i++)
477 ptr[i] = rand();
4e4815fe 478
4c5e1ff3 479 configure_uuid(uuid, UUID_VERSION);
4e4815fe 480
a1b633df 481 memcpy(uuid_bin, uuid, 16);
4e4815fe
PM
482}
483
4e4815fe
PM
484void gen_rand_uuid_str(char *uuid_str, int str_format)
485{
486 unsigned char uuid_bin[UUID_BIN_LEN];
487
488 /* Generate UUID (big endian) */
489 gen_rand_uuid(uuid_bin);
490
491 /* Convert UUID bin to UUID or GUID formated STRING */
492 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
493}
89c8230d 494
1d6132e2 495#if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_CMD_UUID)
09140113 496int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
89c8230d
PM
497{
498 char uuid[UUID_STR_LEN + 1];
499 int str_format;
500
501 if (!strcmp(argv[0], "uuid"))
502 str_format = UUID_STR_FORMAT_STD;
503 else
504 str_format = UUID_STR_FORMAT_GUID;
505
506 if (argc > 2)
507 return CMD_RET_USAGE;
508
509 gen_rand_uuid_str(uuid, str_format);
510
511 if (argc == 1)
512 printf("%s\n", uuid);
513 else
382bee57 514 env_set(argv[1], uuid);
89c8230d
PM
515
516 return CMD_RET_SUCCESS;
517}
518
519U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
520 "UUID - generate random Universally Unique Identifier",
521 "[<varname>]\n"
522 "Argument:\n"
523 "varname: for set result in a environment variable\n"
524 "e.g. uuid uuid_env"
525);
526
527U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
528 "GUID - generate Globally Unique Identifier based on random UUID",
529 "[<varname>]\n"
530 "Argument:\n"
531 "varname: for set result in a environment variable\n"
532 "e.g. guid guid_env"
533);
39206382
PM
534#endif /* CONFIG_CMD_UUID */
535#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
22c48a92 536#endif /* !USE_HOSTCC */
This page took 0.571039 seconds and 4 git commands to generate.