summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig1
-rw-r--r--lib/Makefile1
-rw-r--r--lib/display_options.c7
-rw-r--r--lib/ecdsa/Kconfig23
-rw-r--r--lib/ecdsa/Makefile1
-rw-r--r--lib/ecdsa/ecdsa-verify.c134
-rw-r--r--lib/efi_loader/efi_boottime.c25
-rw-r--r--lib/efi_loader/efi_device_path.c2
-rw-r--r--lib/efi_loader/efi_device_path_to_text.c15
-rw-r--r--lib/efi_loader/efi_memory.c5
-rw-r--r--lib/efi_loader/efi_tcg2.c356
-rw-r--r--lib/tiny-printf.c26
12 files changed, 572 insertions, 24 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index 7b445d01641..c535147aeaa 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -303,6 +303,7 @@ config AES
supported by the algorithm but only a 128-bit key is supported at
present.
+source lib/ecdsa/Kconfig
source lib/rsa/Kconfig
source lib/crypto/Kconfig
source lib/crypt/Kconfig
diff --git a/lib/Makefile b/lib/Makefile
index 07c2ccd7cfd..8ba745faa08 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -60,6 +60,7 @@ endif
obj-$(CONFIG_$(SPL_)ACPIGEN) += acpi/
obj-$(CONFIG_$(SPL_)MD5) += md5.o
+obj-$(CONFIG_ECDSA) += ecdsa/
obj-$(CONFIG_$(SPL_)RSA) += rsa/
obj-$(CONFIG_HASH) += hash-checksum.o
obj-$(CONFIG_SHA1) += sha1.o
diff --git a/lib/display_options.c b/lib/display_options.c
index c08a87e3162..4da1f5244f3 100644
--- a/lib/display_options.c
+++ b/lib/display_options.c
@@ -107,7 +107,12 @@ void print_size(uint64_t size, const char *s)
}
if (!c) {
- printf("%llu Bytes%s", size, s);
+ /*
+ * SPL tiny-printf is not capable for printing uint64_t.
+ * We have just checked that the size is small enought to fit
+ * unsigned int safely.
+ */
+ printf("%u Bytes%s", (unsigned int)size, s);
return;
}
diff --git a/lib/ecdsa/Kconfig b/lib/ecdsa/Kconfig
new file mode 100644
index 00000000000..a95c4ff581f
--- /dev/null
+++ b/lib/ecdsa/Kconfig
@@ -0,0 +1,23 @@
+config ECDSA
+ bool "Enable ECDSA support"
+ depends on DM
+ help
+ This enables the ECDSA (elliptic curve signature) algorithm for FIT
+ image verification in U-Boot. The ECDSA algorithm is implemented
+ using the driver model, so CONFIG_DM is required by this library.
+ See doc/uImage.FIT/signature.txt for more details.
+ ECDSA is enabled for mkimage regardless of this option.
+
+if ECDSA
+
+config ECDSA_VERIFY
+ bool "Enable ECDSA verification support in U-Boot."
+ help
+ Allow ECDSA signatures to be recognized and verified in U-Boot.
+
+config SPL_ECDSA_VERIFY
+ bool "Enable ECDSA verification support in SPL"
+ help
+ Allow ECDSA signatures to be recognized and verified in SPL.
+
+endif
diff --git a/lib/ecdsa/Makefile b/lib/ecdsa/Makefile
new file mode 100644
index 00000000000..771d6d3135b
--- /dev/null
+++ b/lib/ecdsa/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_$(SPL_)ECDSA_VERIFY) += ecdsa-verify.o
diff --git a/lib/ecdsa/ecdsa-verify.c b/lib/ecdsa/ecdsa-verify.c
new file mode 100644
index 00000000000..0601700c4fc
--- /dev/null
+++ b/lib/ecdsa/ecdsa-verify.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * ECDSA signature verification for u-boot
+ *
+ * This implements the firmware-side wrapper for ECDSA verification. It bridges
+ * the struct crypto_algo API to the ECDSA uclass implementations.
+ *
+ * Copyright (c) 2020, Alexandru Gagniuc <mr.nuke.me@gmail.com>
+ */
+
+#include <crypto/ecdsa-uclass.h>
+#include <dm/uclass.h>
+#include <u-boot/ecdsa.h>
+
+/*
+ * Derive size of an ECDSA key from the curve name
+ *
+ * While it's possible to extract the key size by using string manipulation,
+ * use a list of known curves for the time being.
+ */
+static int ecdsa_key_size(const char *curve_name)
+{
+ if (!strcmp(curve_name, "prime256v1"))
+ return 256;
+ else
+ return 0;
+}
+
+static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
+{
+ int x_len, y_len;
+
+ key->curve_name = fdt_getprop(fdt, node, "ecdsa,curve", NULL);
+ key->size_bits = ecdsa_key_size(key->curve_name);
+ if (key->size_bits == 0) {
+ debug("Unknown ECDSA curve '%s'", key->curve_name);
+ return -EINVAL;
+ }
+
+ key->x = fdt_getprop(fdt, node, "ecdsa,x-point", &x_len);
+ key->y = fdt_getprop(fdt, node, "ecdsa,y-point", &y_len);
+
+ if (!key->x || !key->y)
+ return -EINVAL;
+
+ if (x_len != (key->size_bits / 8) || y_len != (key->size_bits / 8)) {
+ printf("%s: node=%d, curve@%p x@%p+%i y@%p+%i\n", __func__,
+ node, key->curve_name, key->x, x_len, key->y, y_len);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int ecdsa_verify_hash(struct udevice *dev,
+ const struct image_sign_info *info,
+ const void *hash, const void *sig, uint sig_len)
+{
+ const struct ecdsa_ops *ops = device_get_ops(dev);
+ const struct checksum_algo *algo = info->checksum;
+ struct ecdsa_public_key key;
+ int sig_node, key_node, ret;
+
+ if (!ops || !ops->verify)
+ return -ENODEV;
+
+ if (info->required_keynode > 0) {
+ ret = fdt_get_key(&key, info->fdt_blob, info->required_keynode);
+ if (ret < 0)
+ return ret;
+
+ return ops->verify(dev, &key, hash, algo->checksum_len,
+ sig, sig_len);
+ }
+
+ sig_node = fdt_subnode_offset(info->fdt_blob, 0, FIT_SIG_NODENAME);
+ if (sig_node < 0)
+ return -ENOENT;
+
+ /* Try all possible keys under the "/signature" node */
+ fdt_for_each_subnode(key_node, info->fdt_blob, sig_node) {
+ ret = fdt_get_key(&key, info->fdt_blob, key_node);
+ if (ret < 0)
+ continue;
+
+ ret = ops->verify(dev, &key, hash, algo->checksum_len,
+ sig, sig_len);
+
+ /* On success, don't worry about remaining keys */
+ if (!ret)
+ return 0;
+ }
+
+ return -EPERM;
+}
+
+int ecdsa_verify(struct image_sign_info *info,
+ const struct image_region region[], int region_count,
+ uint8_t *sig, uint sig_len)
+{
+ const struct checksum_algo *algo = info->checksum;
+ uint8_t hash[algo->checksum_len];
+ struct udevice *dev;
+ int ret;
+
+ ret = uclass_first_device_err(UCLASS_ECDSA, &dev);
+ if (ret) {
+ debug("ECDSA: Could not find ECDSA implementation: %d\n", ret);
+ return ret;
+ }
+
+ ret = algo->calculate(algo->name, region, region_count, hash);
+ if (ret < 0)
+ return -EINVAL;
+
+ return ecdsa_verify_hash(dev, info, hash, sig, sig_len);
+}
+
+U_BOOT_CRYPTO_ALGO(ecdsa) = {
+ .name = "ecdsa256",
+ .key_len = ECDSA256_BYTES,
+ .verify = ecdsa_verify,
+};
+
+/*
+ * uclass definition for ECDSA API
+ *
+ * We don't implement any wrappers around ecdsa_ops->verify() because it's
+ * trivial to call ops->verify().
+ */
+UCLASS_DRIVER(ecdsa) = {
+ .id = UCLASS_ECDSA,
+ .name = "ecdsa_verifier",
+};
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 0b98e918137..f0283b539e4 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -2182,6 +2182,11 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
efi_set_watchdog(0);
WATCHDOG_RESET();
out:
+ if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
+ if (ret != EFI_SUCCESS)
+ efi_tcg2_notify_exit_boot_services_failed();
+ }
+
return EFI_EXIT(ret);
}
@@ -2994,6 +2999,16 @@ efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
image_obj->exit_status = &exit_status;
image_obj->exit_jmp = &exit_jmp;
+ if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
+ if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
+ ret = efi_tcg2_measure_efi_app_invocation();
+ if (ret != EFI_SUCCESS) {
+ log_warning("tcg2 measurement fails(0x%lx)\n",
+ ret);
+ }
+ }
+ }
+
/* call the image! */
if (setjmp(&exit_jmp)) {
/*
@@ -3252,6 +3267,16 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
exit_status != EFI_SUCCESS)
efi_delete_image(image_obj, loaded_image_protocol);
+ if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
+ if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
+ ret = efi_tcg2_measure_efi_app_exit();
+ if (ret != EFI_SUCCESS) {
+ log_warning("tcg2 measurement fails(0x%lx)\n",
+ ret);
+ }
+ }
+ }
+
/* Make sure entry/exit counts for EFI world cross-overs match */
EFI_EXIT(exit_status);
diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c
index 9c3ac712fef..cbdb466da41 100644
--- a/lib/efi_loader/efi_device_path.c
+++ b/lib/efi_loader/efi_device_path.c
@@ -68,7 +68,7 @@ static void *dp_alloc(size_t sz)
{
void *buf;
- if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
+ if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sz, &buf) !=
EFI_SUCCESS) {
debug("EFI: ERROR: out of memory in %s\n", __func__);
return NULL;
diff --git a/lib/efi_loader/efi_device_path_to_text.c b/lib/efi_loader/efi_device_path_to_text.c
index 675e80bcb8a..57fa9d97f71 100644
--- a/lib/efi_loader/efi_device_path_to_text.c
+++ b/lib/efi_loader/efi_device_path_to_text.c
@@ -34,7 +34,7 @@ static u16 *efi_str_to_u16(char *str)
efi_status_t ret;
len = sizeof(u16) * (utf8_utf16_strlen(str) + 1);
- ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len, (void **)&out);
+ ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, len, (void **)&out);
if (ret != EFI_SUCCESS)
return NULL;
dst = out;
@@ -190,6 +190,19 @@ static char *dp_msging(char *s, struct efi_device_path *dp)
break;
}
+ case DEVICE_PATH_SUB_TYPE_MSG_URI: {
+ struct efi_device_path_uri *udp =
+ (struct efi_device_path_uri *)dp;
+ int n;
+
+ n = (int)udp->dp.length - sizeof(struct efi_device_path_uri);
+
+ s += sprintf(s, "Uri(");
+ if (n > 0 && n < MAX_NODE_LEN - 6)
+ s += snprintf(s, n, "%s", (char *)udp->uri);
+ s += sprintf(s, ")");
+ break;
+ }
case DEVICE_PATH_SUB_TYPE_MSG_SD:
case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
const char *typename =
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index be2f655dffd..f4acbee4f9b 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -454,7 +454,8 @@ static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
* @memory allocated memory
* @return status code
*/
-efi_status_t efi_allocate_pages(int type, int memory_type,
+efi_status_t efi_allocate_pages(enum efi_allocate_type type,
+ enum efi_memory_type memory_type,
efi_uintn_t pages, uint64_t *memory)
{
u64 len = pages << EFI_PAGE_SHIFT;
@@ -556,7 +557,7 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
* @buffer: allocated memory
* Return: status code
*/
-efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
+efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
{
efi_status_t r;
u64 addr;
diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c
index 1319a8b3786..35e69b91129 100644
--- a/lib/efi_loader/efi_tcg2.c
+++ b/lib/efi_loader/efi_tcg2.c
@@ -35,6 +35,7 @@ struct event_log_buffer {
};
static struct event_log_buffer event_log;
+static bool tcg2_efi_app_invoked;
/*
* When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
* Since the current tpm2_get_capability() response buffers starts at
@@ -78,6 +79,19 @@ static const struct digest_info hash_algo_list[] = {
},
};
+struct variable_info {
+ u16 *name;
+ const efi_guid_t *guid;
+};
+
+static struct variable_info secure_variables[] = {
+ {L"SecureBoot", &efi_global_variable_guid},
+ {L"PK", &efi_global_variable_guid},
+ {L"KEK", &efi_global_variable_guid},
+ {L"db", &efi_guid_image_security_database},
+ {L"dbx", &efi_guid_image_security_database},
+};
+
#define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list)
/**
@@ -1265,6 +1279,39 @@ free_pool:
}
/**
+ * tcg2_measure_event() - common function to add event log and extend PCR
+ *
+ * @dev: TPM device
+ * @pcr_index: PCR index
+ * @event_type: type of event added
+ * @size: event size
+ * @event: event data
+ *
+ * Return: status code
+ */
+static efi_status_t
+tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type,
+ u32 size, u8 event[])
+{
+ struct tpml_digest_values digest_list;
+ efi_status_t ret;
+
+ ret = tcg2_create_digest(event, size, &digest_list);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
+ size, event);
+
+out:
+ return ret;
+}
+
+/**
* efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
* eventlog and extend the PCRs
*
@@ -1274,27 +1321,308 @@ free_pool:
*/
static efi_status_t efi_append_scrtm_version(struct udevice *dev)
{
- struct tpml_digest_values digest_list;
u8 ver[] = U_BOOT_VERSION_STRING;
- const int pcr_index = 0;
efi_status_t ret;
- ret = tcg2_create_digest(ver, sizeof(ver), &digest_list);
+ ret = tcg2_measure_event(dev, 0, EV_S_CRTM_VERSION, sizeof(ver), ver);
+
+ return ret;
+}
+
+/**
+ * tcg2_measure_variable() - add variable event log and extend PCR
+ *
+ * @dev: TPM device
+ * @pcr_index: PCR index
+ * @event_type: type of event added
+ * @var_name: variable name
+ * @guid: guid
+ * @data_size: variable data size
+ * @data: variable data
+ *
+ * Return: status code
+ */
+static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
+ u32 event_type, u16 *var_name,
+ const efi_guid_t *guid,
+ efi_uintn_t data_size, u8 *data)
+{
+ u32 event_size;
+ efi_status_t ret;
+ struct efi_tcg2_uefi_variable_data *event;
+
+ event_size = sizeof(event->variable_name) +
+ sizeof(event->unicode_name_length) +
+ sizeof(event->variable_data_length) +
+ (u16_strlen(var_name) * sizeof(u16)) + data_size;
+ event = malloc(event_size);
+ if (!event)
+ return EFI_OUT_OF_RESOURCES;
+
+ guidcpy(&event->variable_name, guid);
+ event->unicode_name_length = u16_strlen(var_name);
+ event->variable_data_length = data_size;
+ memcpy(event->unicode_name, var_name,
+ (event->unicode_name_length * sizeof(u16)));
+ if (data) {
+ memcpy((u16 *)event->unicode_name + event->unicode_name_length,
+ data, data_size);
+ }
+ ret = tcg2_measure_event(dev, pcr_index, event_type, event_size,
+ (u8 *)event);
+ free(event);
+ return ret;
+}
+
+/**
+ * tcg2_measure_boot_variable() - measure boot variables
+ *
+ * @dev: TPM device
+ *
+ * Return: status code
+ */
+static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
+{
+ u16 *boot_order;
+ u16 *boot_index;
+ u16 var_name[] = L"BootOrder";
+ u16 boot_name[] = L"Boot####";
+ u8 *bootvar;
+ efi_uintn_t var_data_size;
+ u32 count, i;
+ efi_status_t ret;
+
+ boot_order = efi_get_var(var_name, &efi_global_variable_guid,
+ &var_data_size);
+ if (!boot_order) {
+ ret = EFI_NOT_FOUND;
+ goto error;
+ }
+
+ ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
+ &efi_global_variable_guid, var_data_size,
+ (u8 *)boot_order);
+ if (ret != EFI_SUCCESS)
+ goto error;
+
+ count = var_data_size / sizeof(*boot_order);
+ boot_index = boot_order;
+ for (i = 0; i < count; i++) {
+ efi_create_indexed_name(boot_name, sizeof(boot_name),
+ "Boot", *boot_index++);
+
+ bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
+ &var_data_size);
+
+ if (!bootvar) {
+ log_info("%ls not found\n", boot_name);
+ continue;
+ }
+
+ ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
+ boot_name,
+ &efi_global_variable_guid,
+ var_data_size, bootvar);
+ free(bootvar);
+ if (ret != EFI_SUCCESS)
+ goto error;
+ }
+
+error:
+ free(boot_order);
+ return ret;
+}
+
+/**
+ * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
+ *
+ * Return: status code
+ */
+efi_status_t efi_tcg2_measure_efi_app_invocation(void)
+{
+ efi_status_t ret;
+ u32 pcr_index;
+ struct udevice *dev;
+ u32 event = 0;
+
+ if (tcg2_efi_app_invoked)
+ return EFI_SUCCESS;
+
+ ret = platform_get_tpm2_device(&dev);
+ if (ret != EFI_SUCCESS)
+ return ret;
+
+ ret = tcg2_measure_boot_variable(dev);
if (ret != EFI_SUCCESS)
goto out;
- ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
+ ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
+ strlen(EFI_CALLING_EFI_APPLICATION),
+ (u8 *)EFI_CALLING_EFI_APPLICATION);
if (ret != EFI_SUCCESS)
goto out;
- ret = tcg2_agile_log_append(pcr_index, EV_S_CRTM_VERSION, &digest_list,
- sizeof(ver), ver);
+ for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
+ ret = tcg2_measure_event(dev, pcr_index, EV_SEPARATOR,
+ sizeof(event), (u8 *)&event);
+ if (ret != EFI_SUCCESS)
+ goto out;
+ }
+ tcg2_efi_app_invoked = true;
out:
return ret;
}
/**
+ * efi_tcg2_measure_efi_app_exit() - measure efi app exit
+ *
+ * Return: status code
+ */
+efi_status_t efi_tcg2_measure_efi_app_exit(void)
+{
+ efi_status_t ret;
+ struct udevice *dev;
+
+ ret = platform_get_tpm2_device(&dev);
+ if (ret != EFI_SUCCESS)
+ return ret;
+
+ ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
+ strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
+ (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
+ return ret;
+}
+
+/**
+ * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
+ *
+ * @event: callback event
+ * @context: callback context
+ */
+static void EFIAPI
+efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
+{
+ efi_status_t ret;
+ struct udevice *dev;
+
+ EFI_ENTRY("%p, %p", event, context);
+
+ ret = platform_get_tpm2_device(&dev);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
+ strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
+ (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
+ strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
+ (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
+
+out:
+ EFI_EXIT(ret);
+}
+
+/**
+ * efi_tcg2_notify_exit_boot_services_failed()
+ * - notify ExitBootServices() is failed
+ *
+ * Return: status code
+ */
+efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
+{
+ struct udevice *dev;
+ efi_status_t ret;
+
+ ret = platform_get_tpm2_device(&dev);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
+ strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
+ (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
+ strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
+ (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
+
+out:
+ return ret;
+}
+
+/**
+ * tcg2_measure_secure_boot_variable() - measure secure boot variables
+ *
+ * @dev: TPM device
+ *
+ * Return: status code
+ */
+static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
+{
+ u8 *data;
+ efi_uintn_t data_size;
+ u32 count, i;
+ efi_status_t ret;
+
+ count = ARRAY_SIZE(secure_variables);
+ for (i = 0; i < count; i++) {
+ /*
+ * According to the TCG2 PC Client PFP spec, "SecureBoot",
+ * "PK", "KEK", "db" and "dbx" variables must be measured
+ * even if they are empty.
+ */
+ data = efi_get_var(secure_variables[i].name,
+ secure_variables[i].guid,
+ &data_size);
+
+ ret = tcg2_measure_variable(dev, 7,
+ EV_EFI_VARIABLE_DRIVER_CONFIG,
+ secure_variables[i].name,
+ secure_variables[i].guid,
+ data_size, data);
+ free(data);
+ if (ret != EFI_SUCCESS)
+ goto error;
+ }
+
+ /*
+ * TCG2 PC Client PFP spec says "dbt" and "dbr" are
+ * measured if present and not empty.
+ */
+ data = efi_get_var(L"dbt",
+ &efi_guid_image_security_database,
+ &data_size);
+ if (data) {
+ ret = tcg2_measure_variable(dev, 7,
+ EV_EFI_VARIABLE_DRIVER_CONFIG,
+ L"dbt",
+ &efi_guid_image_security_database,
+ data_size, data);
+ free(data);
+ }
+
+ data = efi_get_var(L"dbr",
+ &efi_guid_image_security_database,
+ &data_size);
+ if (data) {
+ ret = tcg2_measure_variable(dev, 7,
+ EV_EFI_VARIABLE_DRIVER_CONFIG,
+ L"dbr",
+ &efi_guid_image_security_database,
+ data_size, data);
+ free(data);
+ }
+
+error:
+ return ret;
+}
+
+/**
* efi_tcg2_register() - register EFI_TCG2_PROTOCOL
*
* If a TPM2 device is available, the TPM TCG2 Protocol is registered
@@ -1305,6 +1633,7 @@ efi_status_t efi_tcg2_register(void)
{
efi_status_t ret = EFI_SUCCESS;
struct udevice *dev;
+ struct efi_event *event;
ret = platform_get_tpm2_device(&dev);
if (ret != EFI_SUCCESS) {
@@ -1328,6 +1657,21 @@ efi_status_t efi_tcg2_register(void)
tcg2_uninit();
goto fail;
}
+
+ ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
+ efi_tcg2_notify_exit_boot_services, NULL,
+ NULL, &event);
+ if (ret != EFI_SUCCESS) {
+ tcg2_uninit();
+ goto fail;
+ }
+
+ ret = tcg2_measure_secure_boot_variable(dev);
+ if (ret != EFI_SUCCESS) {
+ tcg2_uninit();
+ goto fail;
+ }
+
return ret;
fail:
diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c
index 8fc7e48d994..89aaa854771 100644
--- a/lib/tiny-printf.c
+++ b/lib/tiny-printf.c
@@ -9,8 +9,9 @@
*/
#include <common.h>
-#include <stdarg.h>
+#include <log.h>
#include <serial.h>
+#include <stdarg.h>
#include <linux/ctype.h>
struct printf_info {
@@ -269,20 +270,19 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
}
break;
case 'p':
-#ifdef DEBUG
- pointer(info, fmt, va_arg(va, void *));
- /*
- * Skip this because it pulls in _ctype which is
- * 256 bytes, and we don't generally implement
- * pointer anyway
- */
- while (isalnum(fmt[0]))
- fmt++;
- break;
-#else
+ if (CONFIG_IS_ENABLED(NET_SUPPORT) || _DEBUG) {
+ pointer(info, fmt, va_arg(va, void *));
+ /*
+ * Skip this because it pulls in _ctype which is
+ * 256 bytes, and we don't generally implement
+ * pointer anyway
+ */
+ while (isalnum(fmt[0]))
+ fmt++;
+ break;
+ }
islong = true;
/* no break */
-#endif
case 'x':
if (islong) {
num = va_arg(va, unsigned long);