2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-08-23 11:06:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Calxeda, Inc.
|
2023-08-04 13:33:38 +00:00
|
|
|
* Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
|
2011-08-23 11:06:56 +00:00
|
|
|
*/
|
|
|
|
|
2023-08-24 19:55:44 +00:00
|
|
|
#define LOG_CATEGOT LOGC_CORE
|
|
|
|
|
2014-04-02 08:20:05 +00:00
|
|
|
#include <common.h>
|
2020-05-10 17:40:03 +00:00
|
|
|
#include <command.h>
|
2022-01-16 13:10:23 +00:00
|
|
|
#include <efi_api.h>
|
2019-08-01 15:46:51 +00:00
|
|
|
#include <env.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <rand.h>
|
2019-11-14 19:57:30 +00:00
|
|
|
#include <time.h>
|
2020-05-10 17:39:52 +00:00
|
|
|
#include <uuid.h>
|
2011-08-23 11:06:56 +00:00
|
|
|
#include <linux/ctype.h>
|
2014-04-02 08:20:02 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <common.h>
|
2014-04-02 08:20:03 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <part_efi.h>
|
|
|
|
#include <malloc.h>
|
2020-12-18 09:28:03 +00:00
|
|
|
#include <dm/uclass.h>
|
|
|
|
#include <rng.h>
|
2011-08-23 11:06:56 +00:00
|
|
|
|
|
|
|
int uuid_str_valid(const char *uuid)
|
|
|
|
{
|
|
|
|
int i, valid;
|
|
|
|
|
|
|
|
if (uuid == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0, valid = 1; uuid[i] && valid; i++) {
|
|
|
|
switch (i) {
|
|
|
|
case 8: case 13: case 18: case 23:
|
|
|
|
valid = (uuid[i] == '-');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
valid = isxdigit(uuid[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 08:20:03 +00:00
|
|
|
if (i != UUID_STR_LEN || !valid)
|
2011-08-23 11:06:56 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-27 10:00:28 +00:00
|
|
|
static const struct {
|
|
|
|
const char *string;
|
|
|
|
efi_guid_t guid;
|
|
|
|
} list_guid[] = {
|
2022-01-16 10:55:39 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
2015-10-27 10:00:28 +00:00
|
|
|
{"system", PARTITION_SYSTEM_GUID},
|
|
|
|
{"mbr", LEGACY_MBR_PARTITION_GUID},
|
|
|
|
{"msft", PARTITION_MSFT_RESERVED_GUID},
|
|
|
|
{"data", PARTITION_BASIC_DATA_GUID},
|
|
|
|
{"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
|
|
|
|
{"raid", PARTITION_LINUX_RAID_GUID},
|
|
|
|
{"swap", PARTITION_LINUX_SWAP_GUID},
|
2020-11-20 10:45:36 +00:00
|
|
|
{"lvm", PARTITION_LINUX_LVM_GUID},
|
|
|
|
{"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
|
2023-08-24 19:55:44 +00:00
|
|
|
{"cros-kern", PARTITION_CROS_KERNEL},
|
|
|
|
{"cros-root", PARTITION_CROS_ROOT},
|
|
|
|
{"cros-fw", PARTITION_CROS_FIRMWARE},
|
|
|
|
{"cros-rsrv", PARTITION_CROS_RESERVED},
|
2022-01-16 10:55:39 +00:00
|
|
|
#endif
|
2023-03-19 19:30:12 +00:00
|
|
|
#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
|
2022-01-16 13:10:23 +00:00
|
|
|
{
|
|
|
|
"Device Path",
|
|
|
|
EFI_DEVICE_PATH_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Device Path To Text",
|
|
|
|
EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Device Path Utilities",
|
|
|
|
EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Unicode Collation 2",
|
|
|
|
EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Driver Binding",
|
|
|
|
EFI_DRIVER_BINDING_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Simple Text Input",
|
|
|
|
EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Simple Text Input Ex",
|
|
|
|
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Simple Text Output",
|
|
|
|
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Block IO",
|
|
|
|
EFI_BLOCK_IO_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Simple File System",
|
|
|
|
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Loaded Image",
|
|
|
|
EFI_LOADED_IMAGE_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Graphics Output",
|
|
|
|
EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"HII String",
|
|
|
|
EFI_HII_STRING_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"HII Database",
|
|
|
|
EFI_HII_DATABASE_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"HII Config Routing",
|
|
|
|
EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Load File2",
|
|
|
|
EFI_LOAD_FILE2_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Random Number Generator",
|
|
|
|
EFI_RNG_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Simple Network",
|
|
|
|
EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"PXE Base Code",
|
|
|
|
EFI_PXE_BASE_CODE_PROTOCOL_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Device-Tree Fixup",
|
|
|
|
EFI_DT_FIXUP_PROTOCOL_GUID,
|
|
|
|
},
|
2022-01-16 16:46:38 +00:00
|
|
|
{
|
|
|
|
"TCG2",
|
|
|
|
EFI_TCG2_PROTOCOL_GUID,
|
|
|
|
},
|
2022-01-16 13:10:23 +00:00
|
|
|
{
|
|
|
|
"System Partition",
|
|
|
|
PARTITION_SYSTEM_GUID
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Firmware Management",
|
|
|
|
EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
|
|
|
|
},
|
|
|
|
/* Configuration table GUIDs */
|
|
|
|
{
|
|
|
|
"ACPI table",
|
|
|
|
EFI_ACPI_TABLE_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EFI System Resource Table",
|
|
|
|
EFI_SYSTEM_RESOURCE_TABLE_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"device tree",
|
|
|
|
EFI_FDT_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"SMBIOS table",
|
|
|
|
SMBIOS_TABLE_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Runtime properties",
|
|
|
|
EFI_RT_PROPERTIES_TABLE_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"TCG2 Final Events Table",
|
|
|
|
EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
|
|
|
|
},
|
2021-12-23 14:51:07 +00:00
|
|
|
{
|
|
|
|
"EFI Conformance Profiles Table",
|
|
|
|
EFI_CONFORMANCE_PROFILES_TABLE_GUID,
|
|
|
|
},
|
2022-01-28 18:29:47 +00:00
|
|
|
#ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
|
|
|
|
{
|
|
|
|
"RISC-V Boot",
|
|
|
|
RISCV_EFI_BOOT_PROTOCOL_GUID,
|
|
|
|
},
|
2022-01-16 13:10:23 +00:00
|
|
|
#endif
|
2022-01-28 18:29:47 +00:00
|
|
|
#endif /* CONFIG_CMD_EFIDEBUG */
|
2022-01-16 13:53:37 +00:00
|
|
|
#ifdef CONFIG_CMD_NVEDIT_EFI
|
|
|
|
/* signature database */
|
|
|
|
{
|
|
|
|
"EFI_GLOBAL_VARIABLE_GUID",
|
|
|
|
EFI_GLOBAL_VARIABLE_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EFI_IMAGE_SECURITY_DATABASE_GUID",
|
|
|
|
EFI_IMAGE_SECURITY_DATABASE_GUID,
|
|
|
|
},
|
|
|
|
/* certificate types */
|
|
|
|
{
|
|
|
|
"EFI_CERT_SHA256_GUID",
|
|
|
|
EFI_CERT_SHA256_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EFI_CERT_X509_GUID",
|
|
|
|
EFI_CERT_X509_GUID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EFI_CERT_TYPE_PKCS7_GUID",
|
|
|
|
EFI_CERT_TYPE_PKCS7_GUID,
|
|
|
|
},
|
|
|
|
#endif
|
2023-07-16 03:38:51 +00:00
|
|
|
#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI)
|
2023-03-19 19:30:11 +00:00
|
|
|
{ "EFI_LZMA_COMPRESSED", EFI_LZMA_COMPRESSED },
|
|
|
|
{ "EFI_DXE_SERVICES", EFI_DXE_SERVICES },
|
|
|
|
{ "EFI_HOB_LIST", EFI_HOB_LIST },
|
|
|
|
{ "EFI_MEMORY_TYPE", EFI_MEMORY_TYPE },
|
|
|
|
{ "EFI_MEM_STATUS_CODE_REC", EFI_MEM_STATUS_CODE_REC },
|
|
|
|
{ "EFI_GUID_EFI_ACPI1", EFI_GUID_EFI_ACPI1 },
|
|
|
|
#endif
|
2015-10-27 10:00:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
|
|
|
|
if (!strcmp(list_guid[i].string, guid_str)) {
|
|
|
|
memcpy(guid_bin, &list_guid[i].guid, 16);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2020-11-20 10:45:35 +00:00
|
|
|
const char *uuid_guid_get_str(const unsigned char *guid_bin)
|
2015-10-27 10:00:28 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
|
|
|
|
if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
|
2020-11-20 10:45:35 +00:00
|
|
|
return list_guid[i].string;
|
2015-10-27 10:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 10:45:35 +00:00
|
|
|
return NULL;
|
2015-10-27 10:00:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:32:58 +00:00
|
|
|
int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
|
|
|
|
int str_format)
|
2011-08-23 11:06:56 +00:00
|
|
|
{
|
|
|
|
uint16_t tmp16;
|
|
|
|
uint32_t tmp32;
|
|
|
|
uint64_t tmp64;
|
|
|
|
|
2015-10-27 10:00:28 +00:00
|
|
|
if (!uuid_str_valid(uuid_str)) {
|
2023-08-24 19:55:44 +00:00
|
|
|
log_debug("not valid\n");
|
2015-10-27 10:00:28 +00:00
|
|
|
#ifdef CONFIG_PARTITION_TYPE_GUID
|
|
|
|
if (!uuid_guid_get_bin(uuid_str, uuid_bin))
|
|
|
|
return 0;
|
|
|
|
#endif
|
2014-04-02 08:20:02 +00:00
|
|
|
return -EINVAL;
|
2015-10-27 10:00:28 +00:00
|
|
|
}
|
2014-04-02 08:20:02 +00:00
|
|
|
|
2014-04-02 08:20:03 +00:00
|
|
|
if (str_format == UUID_STR_FORMAT_STD) {
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin, &tmp32, 4);
|
2011-08-23 11:06:56 +00:00
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin + 4, &tmp16, 2);
|
2011-08-23 11:06:56 +00:00
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin + 6, &tmp16, 2);
|
|
|
|
} else {
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin, &tmp32, 4);
|
2011-08-23 11:06:56 +00:00
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin + 4, &tmp16, 2);
|
2011-08-23 11:06:56 +00:00
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin + 6, &tmp16, 2);
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
|
2014-04-02 08:20:03 +00:00
|
|
|
memcpy(uuid_bin + 8, &tmp16, 2);
|
2011-08-23 11:06:56 +00:00
|
|
|
|
2014-04-02 08:20:03 +00:00
|
|
|
tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
|
|
|
|
memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
|
2014-04-02 08:20:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-04 13:33:38 +00:00
|
|
|
int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
|
|
|
|
{
|
|
|
|
u16 tmp16;
|
|
|
|
u32 tmp32;
|
|
|
|
u64 tmp64;
|
|
|
|
|
|
|
|
if (!uuid_str_valid(uuid_str) || !uuid_bin)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
|
|
|
|
memcpy(uuid_bin, &tmp32, 4);
|
|
|
|
|
|
|
|
tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
|
|
|
|
memcpy(uuid_bin + 4, &tmp16, 2);
|
|
|
|
|
|
|
|
tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
|
|
|
|
memcpy(uuid_bin + 6, &tmp16, 2);
|
|
|
|
|
|
|
|
tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL));
|
|
|
|
memcpy(uuid_bin + 8, &tmp16, 2);
|
|
|
|
|
|
|
|
tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16));
|
|
|
|
memcpy(uuid_bin + 10, &tmp64, 6);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:32:58 +00:00
|
|
|
void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
|
|
|
|
int str_format)
|
2014-04-02 08:20:02 +00:00
|
|
|
{
|
2014-04-02 08:20:03 +00:00
|
|
|
const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12, 13, 14, 15};
|
|
|
|
const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
|
|
|
|
9, 10, 11, 12, 13, 14, 15};
|
|
|
|
const u8 *char_order;
|
2019-04-29 06:08:43 +00:00
|
|
|
const char *format;
|
2014-04-02 08:20:02 +00:00
|
|
|
int i;
|
|
|
|
|
2014-04-02 08:20:03 +00:00
|
|
|
/*
|
|
|
|
* UUID and GUID bin data - always in big endian:
|
|
|
|
* 4B-2B-2B-2B-6B
|
|
|
|
* be be be be be
|
|
|
|
*/
|
2019-04-29 06:08:43 +00:00
|
|
|
if (str_format & UUID_STR_FORMAT_GUID)
|
|
|
|
char_order = guid_char_order;
|
|
|
|
else
|
2014-04-02 08:20:03 +00:00
|
|
|
char_order = uuid_char_order;
|
2019-04-29 06:08:43 +00:00
|
|
|
if (str_format & UUID_STR_UPPER_CASE)
|
|
|
|
format = "%02X";
|
2014-04-02 08:20:03 +00:00
|
|
|
else
|
2019-04-29 06:08:43 +00:00
|
|
|
format = "%02x";
|
2014-04-02 08:20:03 +00:00
|
|
|
|
2014-04-02 08:20:02 +00:00
|
|
|
for (i = 0; i < 16; i++) {
|
2019-04-29 06:08:43 +00:00
|
|
|
sprintf(uuid_str, format, uuid_bin[char_order[i]]);
|
2014-04-02 08:20:03 +00:00
|
|
|
uuid_str += 2;
|
2014-04-02 08:20:02 +00:00
|
|
|
switch (i) {
|
|
|
|
case 3:
|
|
|
|
case 5:
|
|
|
|
case 7:
|
|
|
|
case 9:
|
2014-04-02 08:20:03 +00:00
|
|
|
*uuid_str++ = '-';
|
2014-04-02 08:20:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-23 11:06:56 +00:00
|
|
|
}
|
2014-04-02 08:20:04 +00:00
|
|
|
|
2014-04-02 08:20:05 +00:00
|
|
|
#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
|
2014-04-02 08:20:04 +00:00
|
|
|
void gen_rand_uuid(unsigned char *uuid_bin)
|
|
|
|
{
|
2019-07-14 21:31:50 +00:00
|
|
|
u32 ptr[4];
|
|
|
|
struct uuid *uuid = (struct uuid *)ptr;
|
2020-12-18 09:28:03 +00:00
|
|
|
int i, ret;
|
|
|
|
struct udevice *devp;
|
|
|
|
u32 randv = 0;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_DM_RNG)) {
|
|
|
|
ret = uclass_get_device(UCLASS_RNG, 0, &devp);
|
2021-10-22 15:05:47 +00:00
|
|
|
if (!ret) {
|
2020-12-18 09:28:03 +00:00
|
|
|
ret = dm_rng_read(devp, &randv, sizeof(randv));
|
|
|
|
if (ret < 0)
|
|
|
|
randv = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (randv)
|
|
|
|
srand(randv);
|
|
|
|
else
|
|
|
|
srand(get_ticks() + rand());
|
lib: uuid: Fix unseeded PRNG on RANDOM_UUID=y
The random uuid values (enabled via CONFIG_RANDOM_UUID=y) on our
platform are always the same. Below is consistent on each cold boot:
=> ### interrupt autoboot
=> env default -a; gpt write mmc 1 $partitions; print uuid_gpt_misc
...
uuid_gpt_misc=d117f98e-6f2c-d04b-a5b2-331a19f91cb2
=> env default -a; gpt write mmc 1 $partitions; print uuid_gpt_misc
...
uuid_gpt_misc=ad5ec4b6-2d9f-8544-9417-fe3bd1c9b1b3
=> env default -a; gpt write mmc 1 $partitions; print uuid_gpt_misc
...
uuid_gpt_misc=cceb0b18-39cb-d547-9db7-03b405fa77d4
=> env default -a; gpt write mmc 1 $partitions; print uuid_gpt_misc
...
uuid_gpt_misc=d4981a2b-0478-544e-9607-7fd3c651068d
=> env default -a; gpt write mmc 1 $partitions; print uuid_gpt_misc
...
uuid_gpt_misc=6d6c9a36-e919-264d-a9ee-bd00379686c7
While the uuids do change on every 'gpt write' command, the values
appear to be taken from the same pool, in the same order.
Assuming U-Boot with RANDOM_UUID=y is deployed on a large number of
devices, all those devices would essentially expose the same UUID,
breaking the assumption of system/RFS/application designers who rely
on UUID as being globally unique (e.g. a database using UUID as key
would alias/mix up entries/records due to duplicated UUID).
The root cause seems to be simply _not_ seeding PRNG before generating
a random value. It turns out this belongs to an established class of
PRNG-specific problems, commonly known as "unseeded randomness", for
which I am able to find below bugs/CVE/CWE:
- https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2015-0285
("CVE-2015-0285 openssl: handshake with unseeded PRNG")
- https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2015-9019
("CVE-2015-9019 libxslt: math.random() in xslt uses unseeded
randomness")
- https://cwe.mitre.org/data/definitions/336.html
("CWE-336: Same Seed in Pseudo-Random Number Generator (PRNG)")
The first revision [1] of this patch updated the seed based on the
output of get_timer(), similar to [4].
There are two problems with this approach:
- get_timer() has a poor _ms_ resolution
- when gen_rand_uuid() is called in a loop, get_timer() returns the
same result, leading to the same seed being passed to srand(),
leading to the same uuid being generated for several partitions
with different names
The above drawbacks have been addressed in the second version [2].
In its third revision (current), the patch reworded the description
and summary line to emphasize it is a *fix* rather than an improvement.
Testing [3] consisted of running 'gpt write mmc 1 $partitions' in a
loop on R-Car3 for several minutes, collecting 8844 randomly generated
UUIDS. Two consecutive cold boots are concatenated in the log.
As a result, all uuid values are unique (scripted check).
Thanks to Roman, who reported the issue and provided support in fixing.
[1] https://patchwork.ozlabs.org/patch/1091802/
[2] https://patchwork.ozlabs.org/patch/1092945/
[3] https://gist.github.com/erosca/2820be9d554f76b982edd48474d0e7ca
[4] commit da384a9d7628 ("net: rename and refactor eth_rand_ethaddr() function")
Reported-by: Roman Stratiienko <roman.stratiienko@globallogic.com>
Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2019-05-02 12:27:06 +00:00
|
|
|
|
2014-04-02 08:20:04 +00:00
|
|
|
/* Set all fields randomly */
|
2019-07-14 21:31:50 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
ptr[i] = rand();
|
2014-04-02 08:20:04 +00:00
|
|
|
|
2019-07-14 21:31:50 +00:00
|
|
|
clrsetbits_be16(&uuid->time_hi_and_version,
|
2014-04-02 08:20:04 +00:00
|
|
|
UUID_VERSION_MASK,
|
|
|
|
UUID_VERSION << UUID_VERSION_SHIFT);
|
|
|
|
|
2019-07-14 21:31:50 +00:00
|
|
|
clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
|
2014-04-02 08:20:04 +00:00
|
|
|
UUID_VARIANT_MASK,
|
|
|
|
UUID_VARIANT << UUID_VARIANT_SHIFT);
|
|
|
|
|
2019-07-14 21:31:50 +00:00
|
|
|
memcpy(uuid_bin, uuid, 16);
|
2014-04-02 08:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gen_rand_uuid_str(char *uuid_str, int str_format)
|
|
|
|
{
|
|
|
|
unsigned char uuid_bin[UUID_BIN_LEN];
|
|
|
|
|
|
|
|
/* Generate UUID (big endian) */
|
|
|
|
gen_rand_uuid(uuid_bin);
|
|
|
|
|
|
|
|
/* Convert UUID bin to UUID or GUID formated STRING */
|
|
|
|
uuid_bin_to_str(uuid_bin, uuid_str, str_format);
|
|
|
|
}
|
2014-04-02 08:20:05 +00:00
|
|
|
|
2019-01-07 20:23:38 +00:00
|
|
|
#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
|
2020-05-10 17:40:03 +00:00
|
|
|
int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
|
2014-04-02 08:20:05 +00:00
|
|
|
{
|
|
|
|
char uuid[UUID_STR_LEN + 1];
|
|
|
|
int str_format;
|
|
|
|
|
|
|
|
if (!strcmp(argv[0], "uuid"))
|
|
|
|
str_format = UUID_STR_FORMAT_STD;
|
|
|
|
else
|
|
|
|
str_format = UUID_STR_FORMAT_GUID;
|
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
gen_rand_uuid_str(uuid, str_format);
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
printf("%s\n", uuid);
|
|
|
|
else
|
2017-08-03 18:22:09 +00:00
|
|
|
env_set(argv[1], uuid);
|
2014-04-02 08:20:05 +00:00
|
|
|
|
|
|
|
return CMD_RET_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
|
|
|
|
"UUID - generate random Universally Unique Identifier",
|
|
|
|
"[<varname>]\n"
|
|
|
|
"Argument:\n"
|
|
|
|
"varname: for set result in a environment variable\n"
|
|
|
|
"e.g. uuid uuid_env"
|
|
|
|
);
|
|
|
|
|
|
|
|
U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
|
|
|
|
"GUID - generate Globally Unique Identifier based on random UUID",
|
|
|
|
"[<varname>]\n"
|
|
|
|
"Argument:\n"
|
|
|
|
"varname: for set result in a environment variable\n"
|
|
|
|
"e.g. guid guid_env"
|
|
|
|
);
|
2014-04-02 08:20:06 +00:00
|
|
|
#endif /* CONFIG_CMD_UUID */
|
|
|
|
#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
|