1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2011 Calxeda, Inc.
4 * 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>
8 */
9
10 #ifndef USE_HOSTCC
11 #include <command.h>
12 #include <efi_api.h>
13 #include <env.h>
14 #include <rand.h>
15 #include <time.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 #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>
32 #include <u-boot/uuid.h>
33 #include <u-boot/sha1.h>
34
35 #ifdef USE_HOSTCC
36 /* polyfill hextoul to avoid pulling in strto.c */
37 #define hextoul(cp, endp) strtoul(cp, endp, 16)
38 #define hextoull(cp, endp) strtoull(cp, endp, 16)
39 #endif
40
uuid_str_valid(const char * uuid)41 int 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
59 if (i != UUID_STR_LEN || !valid)
60 return 0;
61
62 return 1;
63 }
64
65 /*
66 * Array of string (short and long) for known GUID of GPT partition type
67 * at least one string must be present, @type or @description
68 *
69 * @type : short name for the parameter 'type' of gpt command (max size UUID_STR_LEN = 36,
70 * no space), also used as fallback description when the next field is absent
71 * @description : long description associated to type GUID, used for %pUs
72 * @guid : known type GUID value
73 */
74 static const struct {
75 const char *type;
76 const char *description;
77 efi_guid_t guid;
78 } list_guid[] = {
79 #ifndef USE_HOSTCC
80 #if CONFIG_IS_ENABLED(EFI_PARTITION)
81 {"mbr", NULL, LEGACY_MBR_PARTITION_GUID},
82 {"msft", NULL, PARTITION_MSFT_RESERVED_GUID},
83 {"data", NULL, PARTITION_BASIC_DATA_GUID},
84 {"linux", NULL, PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
85 {"raid", NULL, PARTITION_LINUX_RAID_GUID},
86 {"swap", NULL, PARTITION_LINUX_SWAP_GUID},
87 {"lvm", NULL, PARTITION_LINUX_LVM_GUID},
88 {"u-boot-env", NULL, PARTITION_U_BOOT_ENVIRONMENT},
89 {"cros-kern", NULL, PARTITION_CROS_KERNEL},
90 {"cros-root", NULL, PARTITION_CROS_ROOT},
91 {"cros-fw", NULL, PARTITION_CROS_FIRMWARE},
92 {"cros-rsrv", NULL, PARTITION_CROS_RESERVED},
93 {
94 "system", "EFI System Partition",
95 PARTITION_SYSTEM_GUID,
96 },
97 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI_CLIENT)
98 {
99 NULL, "Device Path",
100 PARTITION_SYSTEM_GUID,
101 },
102 {
103 NULL, "Device Path",
104 EFI_DEVICE_PATH_PROTOCOL_GUID,
105 },
106 {
107 NULL, "Device Path To Text",
108 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
109 },
110 {
111 NULL, "Device Path Utilities",
112 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
113 },
114 {
115 NULL, "Unicode Collation 2",
116 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
117 },
118 {
119 NULL, "Driver Binding",
120 EFI_DRIVER_BINDING_PROTOCOL_GUID,
121 },
122 {
123 NULL, "Simple Text Input",
124 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
125 },
126 {
127 NULL, "Simple Text Input Ex",
128 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
129 },
130 {
131 NULL, "Simple Text Output",
132 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
133 },
134 {
135 NULL, "Block IO",
136 EFI_BLOCK_IO_PROTOCOL_GUID,
137 },
138 {
139 NULL, "Disk IO",
140 EFI_DISK_IO_PROTOCOL_GUID,
141 },
142 {
143 NULL, "Simple File System",
144 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
145 },
146 {
147 NULL, "Loaded Image",
148 EFI_LOADED_IMAGE_PROTOCOL_GUID,
149 },
150 {
151 NULL, "Loaded Image Device Path",
152 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID,
153 },
154 {
155 NULL, "Graphics Output",
156 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
157 },
158 {
159 NULL, "HII String",
160 EFI_HII_STRING_PROTOCOL_GUID,
161 },
162 {
163 NULL, "HII Database",
164 EFI_HII_DATABASE_PROTOCOL_GUID,
165 },
166 {
167 NULL, "HII Config Access",
168 EFI_HII_CONFIG_ACCESS_PROTOCOL_GUID,
169 },
170 {
171 NULL, "HII Config Routing",
172 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
173 },
174 {
175 NULL, "Load File",
176 EFI_LOAD_FILE_PROTOCOL_GUID,
177 },
178 {
179 NULL, "Load File2",
180 EFI_LOAD_FILE2_PROTOCOL_GUID,
181 },
182 {
183 NULL, "Random Number Generator",
184 EFI_RNG_PROTOCOL_GUID,
185 },
186 {
187 NULL, "Simple Network",
188 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
189 },
190 {
191 NULL, "PXE Base Code",
192 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
193 },
194 {
195 NULL, "Device-Tree Fixup",
196 EFI_DT_FIXUP_PROTOCOL_GUID,
197 },
198 {
199 NULL, "TCG2",
200 EFI_TCG2_PROTOCOL_GUID,
201 },
202 {
203 NULL, "Firmware Management",
204 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
205 },
206 #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
207 {
208 NULL, "HTTP",
209 EFI_HTTP_PROTOCOL_GUID,
210 },
211 {
212 NULL, "HTTP Service Binding",
213 EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID,
214 },
215 {
216 NULL, "IPv4 Config2",
217 EFI_IP4_CONFIG2_PROTOCOL_GUID,
218 },
219 #endif
220 /* Configuration table GUIDs */
221 {
222 NULL, "ACPI table",
223 EFI_ACPI_TABLE_GUID,
224 },
225 {
226 NULL, "EFI System Resource Table",
227 EFI_SYSTEM_RESOURCE_TABLE_GUID,
228 },
229 {
230 NULL, "device tree",
231 EFI_FDT_GUID,
232 },
233 {
234 NULL, "SMBIOS table",
235 SMBIOS_TABLE_GUID,
236 },
237 {
238 NULL, "SMBIOS3 table",
239 SMBIOS3_TABLE_GUID,
240 },
241 {
242 NULL, "Runtime properties",
243 EFI_RT_PROPERTIES_TABLE_GUID,
244 },
245 {
246 NULL, "TCG2 Final Events Table",
247 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
248 },
249 {
250 NULL, "EFI Conformance Profiles Table",
251 EFI_CONFORMANCE_PROFILES_TABLE_GUID,
252 },
253 #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
254 {
255 NULL, "RISC-V Boot",
256 RISCV_EFI_BOOT_PROTOCOL_GUID,
257 },
258 #endif
259 #endif /* CONFIG_CMD_EFIDEBUG */
260 #ifdef CONFIG_CMD_NVEDIT_EFI
261 /* signature database */
262 {
263 "EFI_GLOBAL_VARIABLE_GUID", NULL,
264 EFI_GLOBAL_VARIABLE_GUID,
265 },
266 {
267 "EFI_IMAGE_SECURITY_DATABASE_GUID", NULL,
268 EFI_IMAGE_SECURITY_DATABASE_GUID,
269 },
270 /* certificate types */
271 {
272 "EFI_CERT_SHA256_GUID", NULL,
273 EFI_CERT_SHA256_GUID,
274 },
275 {
276 "EFI_CERT_X509_GUID", NULL,
277 EFI_CERT_X509_GUID,
278 },
279 {
280 "EFI_CERT_TYPE_PKCS7_GUID", NULL,
281 EFI_CERT_TYPE_PKCS7_GUID,
282 },
283 #endif
284 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI_CLIENT)
285 { "EFI_LZMA_COMPRESSED", NULL, EFI_LZMA_COMPRESSED },
286 { "EFI_DXE_SERVICES", NULL, EFI_DXE_SERVICES },
287 { "EFI_HOB_LIST", NULL, EFI_HOB_LIST },
288 { "EFI_MEMORY_TYPE", NULL, EFI_MEMORY_TYPE },
289 { "EFI_MEM_STATUS_CODE_REC", NULL, EFI_MEM_STATUS_CODE_REC },
290 { "EFI_GUID_EFI_ACPI1", NULL, EFI_GUID_EFI_ACPI1 },
291 #endif
292 #endif /* EFI_PARTITION */
293 #endif /* !USE_HOSTCC */
294 };
295
uuid_guid_get_bin(const char * guid_str,unsigned char * guid_bin)296 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
297 {
298 int i;
299
300 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
301 if (list_guid[i].type &&
302 !strcmp(list_guid[i].type, guid_str)) {
303 memcpy(guid_bin, &list_guid[i].guid, 16);
304 return 0;
305 }
306 }
307 return -ENODEV;
308 }
309
uuid_guid_get_str(const unsigned char * guid_bin)310 const char *uuid_guid_get_str(const unsigned char *guid_bin)
311 {
312 int i;
313
314 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
315 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
316 if (list_guid[i].description)
317 return list_guid[i].description;
318 return list_guid[i].type;
319 }
320 }
321 return NULL;
322 }
323
uuid_str_to_bin(const char * uuid_str,unsigned char * uuid_bin,int str_format)324 int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
325 int str_format)
326 {
327 uint16_t tmp16;
328 uint32_t tmp32;
329 uint64_t tmp64;
330
331 if (!uuid_str_valid(uuid_str)) {
332 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID) &&
333 !uuid_guid_get_bin(uuid_str, uuid_bin))
334 return 0;
335 return -EINVAL;
336 }
337
338 if (str_format == UUID_STR_FORMAT_STD) {
339 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
340 memcpy(uuid_bin, &tmp32, 4);
341
342 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
343 memcpy(uuid_bin + 4, &tmp16, 2);
344
345 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
346 memcpy(uuid_bin + 6, &tmp16, 2);
347 } else {
348 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
349 memcpy(uuid_bin, &tmp32, 4);
350
351 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
352 memcpy(uuid_bin + 4, &tmp16, 2);
353
354 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
355 memcpy(uuid_bin + 6, &tmp16, 2);
356 }
357
358 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
359 memcpy(uuid_bin + 8, &tmp16, 2);
360
361 tmp64 = cpu_to_be64(hextoull(uuid_str + 24, NULL));
362 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
363
364 return 0;
365 }
366
uuid_str_to_le_bin(const char * uuid_str,unsigned char * uuid_bin)367 int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
368 {
369 uint16_t tmp16;
370 uint32_t tmp32;
371 uint64_t tmp64;
372
373 if (!uuid_str_valid(uuid_str) || !uuid_bin)
374 return -EINVAL;
375
376 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
377 memcpy(uuid_bin, &tmp32, 4);
378
379 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
380 memcpy(uuid_bin + 4, &tmp16, 2);
381
382 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
383 memcpy(uuid_bin + 6, &tmp16, 2);
384
385 tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
386 memcpy(uuid_bin + 8, &tmp16, 2);
387
388 tmp64 = cpu_to_le64(hextoull(uuid_str + 24, NULL));
389 memcpy(uuid_bin + 10, &tmp64, 6);
390
391 return 0;
392 }
393
uuid_bin_to_str(const unsigned char * uuid_bin,char * uuid_str,int str_format)394 void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
395 int str_format)
396 {
397 const uint8_t uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
398 9, 10, 11, 12, 13, 14, 15};
399 const uint8_t guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
400 9, 10, 11, 12, 13, 14, 15};
401 const uint8_t *char_order;
402 const char *format;
403 int i;
404
405 /*
406 * UUID and GUID bin data - always in big endian:
407 * 4B-2B-2B-2B-6B
408 * be be be be be
409 */
410 if (str_format & UUID_STR_FORMAT_GUID)
411 char_order = guid_char_order;
412 else
413 char_order = uuid_char_order;
414 if (str_format & UUID_STR_UPPER_CASE)
415 format = "%02X";
416 else
417 format = "%02x";
418
419 for (i = 0; i < 16; i++) {
420 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
421 uuid_str += 2;
422 switch (i) {
423 case 3:
424 case 5:
425 case 7:
426 case 9:
427 *uuid_str++ = '-';
428 break;
429 }
430 }
431 }
432
configure_uuid(struct uuid * uuid,unsigned char version)433 static void configure_uuid(struct uuid *uuid, unsigned char version)
434 {
435 uint16_t tmp;
436
437 /* Configure variant/version bits */
438 tmp = be16_to_cpu(uuid->time_hi_and_version);
439 tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT);
440 uuid->time_hi_and_version = cpu_to_be16(tmp);
441
442 uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK;
443 uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT);
444 }
445
gen_v5_guid(const struct uuid * namespace,struct efi_guid * guid,...)446 void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...)
447 {
448 sha1_context ctx;
449 va_list args;
450 const uint8_t *data;
451 uint32_t *tmp32;
452 uint16_t *tmp16;
453 uint8_t hash[SHA1_SUM_LEN];
454
455 sha1_starts(&ctx);
456 /* Hash the namespace UUID as salt */
457 sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN);
458 va_start(args, guid);
459
460 while ((data = va_arg(args, const uint8_t *))) {
461 unsigned int len = va_arg(args, size_t);
462
463 sha1_update(&ctx, data, len);
464 }
465
466 va_end(args);
467 sha1_finish(&ctx, hash);
468
469 /* Truncate the hash into output UUID, it is already big endian */
470 memcpy(guid, hash, sizeof(*guid));
471
472 configure_uuid((struct uuid *)guid, 5);
473
474 /* Make little endian */
475 tmp32 = (uint32_t *)&guid->b[0];
476 *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32));
477 tmp16 = (uint16_t *)&guid->b[4];
478 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
479 tmp16 = (uint16_t *)&guid->b[6];
480 *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16));
481 }
482
483 #ifndef USE_HOSTCC
484 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
gen_rand_uuid(unsigned char * uuid_bin)485 void gen_rand_uuid(unsigned char *uuid_bin)
486 {
487 u32 ptr[4];
488 struct uuid *uuid = (struct uuid *)ptr;
489 int i, ret;
490 struct udevice *devp;
491 u32 randv = 0;
492
493 if (CONFIG_IS_ENABLED(DM_RNG)) {
494 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
495 if (!ret) {
496 ret = dm_rng_read(devp, &randv, sizeof(randv));
497 if (ret < 0)
498 randv = 0;
499 }
500 }
501 if (randv)
502 srand(randv);
503 else
504 srand(get_ticks() + rand());
505
506 /* Set all fields randomly */
507 for (i = 0; i < 4; i++)
508 ptr[i] = rand();
509
510 configure_uuid(uuid, UUID_VERSION);
511
512 memcpy(uuid_bin, uuid, 16);
513 }
514
gen_rand_uuid_str(char * uuid_str,int str_format)515 void gen_rand_uuid_str(char *uuid_str, int str_format)
516 {
517 unsigned char uuid_bin[UUID_BIN_LEN];
518
519 /* Generate UUID (big endian) */
520 gen_rand_uuid(uuid_bin);
521
522 /* Convert UUID bin to UUID or GUID formated STRING */
523 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
524 }
525
526 #if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_CMD_UUID)
do_uuid(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])527 int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
528 {
529 char uuid[UUID_STR_LEN + 1];
530 int str_format;
531
532 if (!strcmp(argv[0], "uuid"))
533 str_format = UUID_STR_FORMAT_STD;
534 else
535 str_format = UUID_STR_FORMAT_GUID;
536
537 if (argc > 2)
538 return CMD_RET_USAGE;
539
540 gen_rand_uuid_str(uuid, str_format);
541
542 if (argc == 1)
543 printf("%s\n", uuid);
544 else
545 env_set(argv[1], uuid);
546
547 return CMD_RET_SUCCESS;
548 }
549
550 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
551 "UUID - generate random Universally Unique Identifier",
552 "[<varname>]\n"
553 "Argument:\n"
554 "varname: for set result in a environment variable\n"
555 "e.g. uuid uuid_env"
556 );
557
558 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
559 "GUID - generate Globally Unique Identifier based on random UUID",
560 "[<varname>]\n"
561 "Argument:\n"
562 "varname: for set result in a environment variable\n"
563 "e.g. guid guid_env"
564 );
565 #endif /* CONFIG_CMD_UUID */
566 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
567 #endif /* !USE_HOSTCC */
568