From patchwork Tue Nov 23 11:50:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519310 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp8502390imj; Tue, 23 Nov 2021 03:51:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJyLxKl/UqwaQeFQ6gbEn7BYuug6iIOsTw+8AcRE3W9Ukt1dewrTfot5ZTM5gc/BdB6ofk3n X-Received: by 2002:a17:907:3e1d:: with SMTP id hp29mr6984723ejc.70.1637668294432; Tue, 23 Nov 2021 03:51:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637668294; cv=none; d=google.com; s=arc-20160816; b=EYpYPsLKuydi4p0SpCBvp7b7W7pbsqAHe+jfGKwbyjqBh1eC+IxFry6Lsf8g26flKn Ukm0KyFIHa1h4vNTKxKRVbas5NXAr2XKsH+p3YCs8tsktQHf71JOhth6RuB3+ndMIFMy 9yDklf1sdqdP78lSWT9c8N09l9kZVUOtfQ0ggFH/sgStqY51cGpeBnE3weSDglp7p4P+ EEm4bTifmxHL0uzd2bmBUZsES4mTwJ+Dol7eOaHYN2ogrINk7Z6Fq/NVl3GHw264WZ84 3dNp9Ip2rxXwlUbspiKqWfJ7QjJ6B6clXEN+eejmLwBG+3erNq5algw7wnu3BNH2I+ZX Qogg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1fVb8mXPnv36SouLZavCsOwRAwFIWjsDYBLmdQtl2vQ=; b=UxrIQnzkDR1X4wfKbynfpKs77rRtqvmnwZsNUv/dk+k0D993yKcawc+U12snRbOn4y 2Kg/PmJAKwHRpQLd9S+evI8nSqf4S0y+cSMFQIHGEiDhAas+KPWyeetNDXqDfX6zB46+ LxvN4ihmqQIZkzXXo+w44C1udHIS77RDMT4yKlTwq13XzjnQZJ0cOU4n5GjWiNRLoWRw /RVeet3MmrabSv3nh5vvQPe/cknqkcXoPB6nr/Pz6+iiIx7cbBUVgOOsh1q+ZJGf8s1v zxL2nC11aedA7957VUS5KMLS8/g73m7Nb72Xqo/gs5VA6QT8J6AVf9F/y6Sg56qEp9rW EPrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Cts0Y1HK; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id g13si41352763ejt.663.2021.11.23.03.51.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 03:51:34 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Cts0Y1HK; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1EC9D82C88; Tue, 23 Nov 2021 12:51:31 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="Cts0Y1HK"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7639882A53; Tue, 23 Nov 2021 12:51:29 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 53CCF82A53 for ; Tue, 23 Nov 2021 12:51:25 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ruchika.gupta@linaro.org Received: by mail-pf1-x430.google.com with SMTP id i12so19220821pfd.6 for ; Tue, 23 Nov 2021 03:51:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1fVb8mXPnv36SouLZavCsOwRAwFIWjsDYBLmdQtl2vQ=; b=Cts0Y1HKeYhQLMpBbizp0xPOmDQiQlbcbmde4b/G/W6ICE53AafmOuTnBZhR5etBj+ 9IGOxuKOQbziDnBzCRINswkR8OceNBn9eA7RwlTs1qNvMK2OdfWMgMgDiYmvP/cGzZhA Veeh01nM4/MKRuaVI+lnI+rWFxtsH2FAzlisQsdhJgeT4ku74uaxQmzz3J0Sjvqu3eeG 7quJFsi6heeRBrkrTxfynYPPqJ+ATyAiJPTuyQV4iGx2xwXmIrZZOM15vcfkSU/iEro3 4aTna8/cgz5S9XqlVpNL2Dqidcx9Z04dmtnR/GpH1/TqhinPfP7XFgUvHp5Hk1bhBD3+ 8Haw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1fVb8mXPnv36SouLZavCsOwRAwFIWjsDYBLmdQtl2vQ=; b=XRQhpUp8VJONSR6GoqlBCLbszDyXaT6kT80hzBEUw0i8qJks9uUvTdUslj+XPoNlsB E931CNik55YvZ5XQoBY4e5AN4U4YY6zI/baBeo0Ts22rqokn7hKoftFHKvycf5w4n3Do E5IBKqeOBE7EtDRM6m/cOY8KRaI3yzU4EfmL//T7rJvG9WtRh7JoAozq61HV4RM2o/p6 XIPYqlmmUtyQ+Lbj24J9FP40Uf56ntU+yxs1g0/2oISDEZmRcAVyHWbLo9m0t4acBlOn 1gLmNElfEigfmcNcPpms/vT4Bm7ESrHjdGLHj9ixep90K6kNHWNWCfUKKVVKewb4YUWV xYFA== X-Gm-Message-State: AOAM533pUFHydeiro3dQLWUhJP5BKUnk4AphxNYVP/VUYYVcSZSpmcfC ogs7zUk7k+/5S4TXTOUFkOyUb7W5Ma6lJQ== X-Received: by 2002:a63:517:: with SMTP id 23mr1031559pgf.58.1637668282937; Tue, 23 Nov 2021 03:51:22 -0800 (PST) Received: from localhost.localdomain ([106.215.91.146]) by smtp.gmail.com with ESMTPSA id p66sm9159468pga.31.2021.11.23.03.51.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 03:51:22 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de Cc: Ruchika Gupta Subject: [v2][PATCH 1/3] efi_loader: Add check for event log passed from firmware Date: Tue, 23 Nov 2021 17:20:50 +0530 Message-Id: <20211123115052.124941-1-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Platforms may have support to measure their initial firmware components and pass the event log to u-boot. The event log address can be passed in property tpm_event_log_addr and tpm_event_log_size of the tpm node. Platforms may choose their own specific mechanism to do so. A weak function is added to check if even log has been passed to u-boot from earlier firmware components. If available, the eventlog is parsed to check for its correctness and further event logs are appended to the passed log. Signed-off-by: Ruchika Gupta --- v2: Moved firmware eventlog code parsing to tcg2_get_fw_eventlog() lib/efi_loader/efi_tcg2.c | 322 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 308 insertions(+), 14 deletions(-) diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index 8c1f22e337..c3ebdf92f5 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -324,6 +324,45 @@ __weak efi_status_t platform_get_tpm2_device(struct udevice **dev) return EFI_NOT_FOUND; } +/** + * platform_get_eventlog() - retrieve the eventlog address and size + * + * This function retrieves the eventlog address and size if the underlying + * firmware has done some measurements and passed them. + * + * This function may be overridden based on platform specific method of + * passing the eventlog address and size. + * + * @dev: udevice + * @addr: eventlog address + * @sz: eventlog size + * Return: status code + */ +__weak efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, + u32 *sz) +{ + const u64 *basep; + const u32 *sizep; + + basep = dev_read_prop(dev, "tpm_event_log_addr", NULL); + if (!basep) + return EFI_NOT_FOUND; + + *addr = be64_to_cpup((__force __be64 *)basep); + + sizep = dev_read_prop(dev, "tpm_event_log_size", NULL); + if (!sizep) + return EFI_NOT_FOUND; + + *sz = be32_to_cpup((__force __be32 *)sizep); + if (*sz == 0) { + log_debug("event log empty\n"); + return EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + /** * tpm2_get_max_command_size() - get the supported max command size * @@ -1181,6 +1220,249 @@ static const struct efi_tcg2_protocol efi_tcg2_protocol = { .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks, }; +/** + * parse_event_log_header() - Parse and verify the event log header fields + * + * @buffer: Pointer to the event header + * @size: Size of the eventlog + * @pos: Position in buffer after event log header + * + * Return: status code + */ +efi_status_t parse_event_log_header(void *buffer, u32 size, u32 *pos) +{ + struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer; + int i = 0; + + if (size < sizeof(*event_header)) + return EFI_COMPROMISED_DATA; + + if (get_unaligned_le32(&event_header->pcr_index) != 0 || + get_unaligned_le32(&event_header->event_type) != EV_NO_ACTION) + return EFI_COMPROMISED_DATA; + + for (i = 0; i < sizeof(event_header->digest); i++) { + if (event_header->digest[i] != 0) + return EFI_COMPROMISED_DATA; + } + + *pos += sizeof(*event_header); + + return EFI_SUCCESS; +} + +/** + * parse_specid_event() - Parse and verify the specID Event in the eventlog + * + * @dev: udevice + * @buffer: Pointer to the start of the eventlog + * @log_size: Size of the eventlog + * @pos: Offset in the evenlog where specID event starts + * + * Return: status code + * @pos Offset in the eventlog where the specID event ends + * @digest_list: list of digests in the event + */ +efi_status_t parse_specid_event(struct udevice *dev, void *buffer, u32 log_size, + u32 *pos, + struct tpml_digest_values *digest_list) +{ + struct tcg_efi_spec_id_event *spec_event; + struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer; + size_t spec_event_size; + u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0; + u32 spec_active = 0; + u16 hash_alg, hash_sz; + u8 vendor_sz; + int err, i; + + /* Check specID event data */ + spec_event = (struct tcg_efi_spec_id_event *)((uintptr_t)buffer + *pos); + /* Check for signature */ + if (memcmp(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, + sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03))) { + log_err("specID Event: Signature mismatch\n"); + return EFI_COMPROMISED_DATA; + } + + if (spec_event->spec_version_minor != + TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 || + spec_event->spec_version_major != + TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2) + return EFI_COMPROMISED_DATA; + + if (spec_event->number_of_algorithms > MAX_HASH_COUNT || + spec_event->number_of_algorithms < 1) { + log_err("specID Event: Number of algorithms incorrect\n"); + return EFI_COMPROMISED_DATA; + } + + alg_count = spec_event->number_of_algorithms; + + err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count); + if (err) + return EFI_DEVICE_ERROR; + + digest_list->count = 0; + /* + * We may need to worry about the order of algs in this structure as + * subsequent entries in event should be in same order + */ + for (i = 0; i < alg_count; i++) { + hash_alg = + get_unaligned_le16(&spec_event->digest_sizes[i].algorithm_id); + hash_sz = + get_unaligned_le16(&spec_event->digest_sizes[i].digest_size); + + if (!(supported & alg_to_mask(hash_alg))) { + log_err("specID Event: Unsupported algorithm\n"); + return EFI_COMPROMISED_DATA; + } + digest_list->digests[digest_list->count++].hash_alg = hash_alg; + + spec_active |= alg_to_mask(hash_alg); + } + + /* TCG spec expects the event log to have hashes for all active PCR's */ + if (spec_active != active) { + /* + * Previous stage bootloader should know all the active PCR's + * and use them in the Eventlog. + */ + log_err("specID Event: All active hash alg not present\n"); + return EFI_COMPROMISED_DATA; + } + + /* + * the size of the spec event and placement of vendor_info_size + * depends on supported algoriths + */ + spec_event_size = + offsetof(struct tcg_efi_spec_id_event, digest_sizes) + + alg_count * sizeof(spec_event->digest_sizes[0]); + + vendor_sz = *(uint8_t *)((uintptr_t)buffer + *pos + spec_event_size); + + spec_event_size += sizeof(vendor_sz) + vendor_sz; + *pos += spec_event_size; + + if (get_unaligned_le32(&event_header->event_size) != spec_event_size) { + log_err("specID event: header event size mismatch\n"); + /* Right way to handle this can be to call SetActive PCR's */ + return EFI_COMPROMISED_DATA; + } + + return EFI_SUCCESS; +} + +efi_status_t tcg2_parse_event(struct udevice *dev, void *buffer, u32 log_size, + u32 *offset, struct tpml_digest_values *digest_list, + u32 *pcr) +{ + struct tcg_pcr_event2 *event = NULL; + u32 event_type, count, size, event_size; + size_t pos; + + if (*offset > log_size) + return EFI_COMPROMISED_DATA; + + event = (struct tcg_pcr_event2 *)((uintptr_t)buffer + *offset); + + *pcr = get_unaligned_le32(&event->pcr_index); + + event_size = tcg_event_final_size(digest_list); + + if (*offset + event_size > log_size) { + log_err("Event exceeds log size\n"); + return EFI_COMPROMISED_DATA; + } + + event_type = get_unaligned_le32(&event->event_type); + + /* get the count */ + count = get_unaligned_le32(&event->digests.count); + if (count != digest_list->count) + return EFI_COMPROMISED_DATA; + + pos = offsetof(struct tcg_pcr_event2, digests); + pos += offsetof(struct tpml_digest_values, digests); + + for (int i = 0; i < digest_list->count; i++) { + u16 alg; + u16 hash_alg = digest_list->digests[i].hash_alg; + u8 *digest = (u8 *)&digest_list->digests[i].digest; + + alg = get_unaligned_le16((void *)((uintptr_t)event + pos)); + + if (alg != hash_alg) + return EFI_COMPROMISED_DATA; + + pos += offsetof(struct tpmt_ha, digest); + memcpy(digest, (void *)((uintptr_t)event + pos), alg_to_len(hash_alg)); + pos += alg_to_len(hash_alg); + } + + size = get_unaligned_le32((void *)((uintptr_t)event + pos)); + event_size += size; + pos += sizeof(u32); /* tcg_pcr_event2 event_size*/ + pos += size; + + /* make sure the calculated buffer is what we checked against */ + if (pos != event_size) + return EFI_COMPROMISED_DATA; + + if (pos > log_size) + return EFI_COMPROMISED_DATA; + + *offset += pos; + + return EFI_SUCCESS; +} + +efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, + size_t *log_sz) +{ + struct tpml_digest_values digest_list; + efi_status_t ret; + u32 pcr, pos; + u64 base; + u32 sz; + + ret = platform_get_eventlog(dev, &base, &sz); + if (ret == EFI_SUCCESS) { + void *buffer = (void *)base; + + if (sz > TPM2_EVENT_LOG_SIZE) + return EFI_VOLUME_FULL; + + pos = 0; + /* Parse the eventlog to check for its validity */ + ret = parse_event_log_header(buffer, sz, &pos); + if (ret || pos > sz) + return EFI_COMPROMISED_DATA; + + ret = parse_specid_event(dev, buffer, sz, &pos, &digest_list); + if (ret || pos > sz) { + log_err("Error parsing SPEC ID Event\n"); + return EFI_COMPROMISED_DATA; + } + + while (pos < sz) { + ret = tcg2_parse_event(dev, buffer, sz, &pos, + &digest_list, &pcr); + if (ret) { + log_err("Error parsing event\n"); + return ret; + } + } + + memcpy(log_buffer, buffer, sz); + *log_sz = sz; + } + + return ret; +} + /** * create_specid_event() - Create the first event in the eventlog * @@ -1340,6 +1622,12 @@ static efi_status_t efi_init_event_log(void) * last log entry */ memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); + + /* + * The log header is defined to be in SHA1 event log entry format. + * Setup event header + */ + event_header = (struct tcg_pcr_event *)event_log.buffer; event_log.pos = 0; event_log.last_event_size = 0; event_log.get_event_called = false; @@ -1347,22 +1635,28 @@ static efi_status_t efi_init_event_log(void) event_log.truncated = false; /* - * The log header is defined to be in SHA1 event log entry format. - * Setup event header + * Check if earlier firmware have passed any eventlog. Different + * platforms can use different ways to do so */ - event_header = (struct tcg_pcr_event *)event_log.buffer; - put_unaligned_le32(0, &event_header->pcr_index); - put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); - memset(&event_header->digest, 0, sizeof(event_header->digest)); - ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)), - &spec_event_size); - if (ret != EFI_SUCCESS) - goto free_pool; - put_unaligned_le32(spec_event_size, &event_header->event_size); - event_log.pos = spec_event_size + sizeof(*event_header); - event_log.last_event_size = event_log.pos; + ret = tcg2_get_fw_eventlog(dev, event_log.buffer, &event_log.pos); + if (ret == EFI_NOT_FOUND) { + put_unaligned_le32(0, &event_header->pcr_index); + put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); + memset(&event_header->digest, 0, sizeof(event_header->digest)); + ret = create_specid_event(dev, + (void *)((uintptr_t)event_log.buffer + + sizeof(*event_header)), + &spec_event_size); + if (ret != EFI_SUCCESS) + goto free_pool; + put_unaligned_le32(spec_event_size, &event_header->event_size); + event_log.pos = spec_event_size + sizeof(*event_header); + event_log.last_event_size = event_log.pos; + } + + if (ret == EFI_SUCCESS) + ret = create_final_event(); - ret = create_final_event(); if (ret != EFI_SUCCESS) goto free_pool; From patchwork Tue Nov 23 11:50:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519311 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp8502620imj; Tue, 23 Nov 2021 03:51:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwYU4j297fFXCpIFkTNQMU1u2qGz43AmnUAVWHhzUWbhlTzq8ZXcGIoGFz/AzKS62yfg6qg X-Received: by 2002:a05:6402:1849:: with SMTP id v9mr8139665edy.335.1637668304368; Tue, 23 Nov 2021 03:51:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637668304; cv=none; d=google.com; s=arc-20160816; b=pl+VnwIlWjy//lI5yx+VE/GavT9QrCOsI46SaT6UUBnFIPg9UW/cilGEw6Dh8qHTIt 9LwQUCeHicu88D/ZyHaXUrd8yjZ/fVMpp0p6znJLi9PMBUBTGzF153cK9TBwF1n6884y x3cfGjJ3vKr+c+HGiDdU7/M2Yrup5TyyK8i2ALUmsb5YXKPGuCH4LZzYgoRrbhcGGUjo tKLbTQgonpWuV4QKf0bQRhkx3EzI0q7gT9+7k8S+759yiPjPO0XWGrUvKZyY6X5+wNiL O0uChhVM8itQPIy9QyBoVNzTOOfxLbHsZx0QIDabhqt0jOeomk/lw3h25d02qwc3txjm I58w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Bff2wf29QQNRVoQJkjSfdwfzFPR/ux7Xoyrtoc3m6uo=; b=GdGgZF5QGb3+1eHXyLhLXUohgHxMWGoRjD+LxgVUHOrtRb07dzXuLFe7brLKBzMw2/ FokyMpAb0AuQx/lClk1hudh1Tn43EHLyjsdowsi0BqfwbyT+R/LS3+GVgYEDP10EyrKT lyWaYCqD3Y7C1c2eEBIiSFyt4HLeSdMWJDubRJ1IUTbBML4FbwCFw5CrIxnWIfJ/0j03 rQo27qLFWVPjjh5zNWfTtqPqd1ylXz8PKvmy2qJi0d8t0v5PCHv+O+kn1k+j4Y3ebFC1 L+goq/ht7nacjtQkHK/OwVU9D9vihRyPZ1M5CmWfdrO+YF9Dn5Nksi0mTDNCaBku8ER3 2VRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rD+m68yJ; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ji1si34028245ejc.121.2021.11.23.03.51.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 03:51:44 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rD+m68yJ; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 73065834D5; Tue, 23 Nov 2021 12:51:35 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="rD+m68yJ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3DB3482C88; Tue, 23 Nov 2021 12:51:30 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id CA16282DF6 for ; Tue, 23 Nov 2021 12:51:26 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ruchika.gupta@linaro.org Received: by mail-pf1-x42f.google.com with SMTP id n85so19185497pfd.10 for ; Tue, 23 Nov 2021 03:51:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Bff2wf29QQNRVoQJkjSfdwfzFPR/ux7Xoyrtoc3m6uo=; b=rD+m68yJ6UkjULa7IWUeIaUdjX/JQPJCZn8fWPmrYC2k1ABwukiKanlmvgwPHOSy4Q tnWq15S8tR6QieMTtcNTIhqtjVW8K3ID/8xx2stRLbHXUSbHRqmEn9BGp11Or7R2hk5r NPVT7RvayZhHmOi8VRB+S6ADQWcfM/3pNLR1ZAWDr0WvkD/XUZsqnSWcro83JZSzbzk7 reO6t9H2zWbnRVFGE9NvwjyIR/sci0Ou30pRh/GT6ducgTIFVA351tKN/DTNdRz/7ZSu UF/r2iy4FrUW6vdTCD5EUGLG64R1/7whhlE8qq4RKJrUwJT3kK9eu92L5sQm47hgSEKi CGkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Bff2wf29QQNRVoQJkjSfdwfzFPR/ux7Xoyrtoc3m6uo=; b=VFEqfJtd0K2MZUiRWQmqa9GJRwy68U6Ceud0Mwm6Hkh24d2CE5Jm3NGbBXMGLOiH82 54s5yYLrEMpvlNDVGLaxP+U8Mb+/FR44WKtR0vremm2eXA9KDACFE7cgTSnol9ZhkvYZ vjwd4VaWKYNW+fbHVePFmuNeUPowCGypm/UiUntyIWceqXVCowhFO+yJV2ZgRvHcoydl UNrpRCACZDWn1i5p4pWuHpEKFvVZwICnKIzCTLsTckbAL28UIwnqraLjLYkZo7QH5E12 kI6nLQqc9dPLTw4FDZ9zdr8mvsvoVrdpQbWN5yvQdIQKB3QrItLWMhSP2e6FjICNR0ma FRxw== X-Gm-Message-State: AOAM531cOYAFndzCokBphGIWOnvxhhH8iNECw3NVDAFaEgI+KSTHixD2 JnJaQQKL3sIGwJO0Df8eEg9v6XRHjV7iuw== X-Received: by 2002:a65:6702:: with SMTP id u2mr2776636pgf.24.1637668284908; Tue, 23 Nov 2021 03:51:24 -0800 (PST) Received: from localhost.localdomain ([106.215.91.146]) by smtp.gmail.com with ESMTPSA id p66sm9159468pga.31.2021.11.23.03.51.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 03:51:24 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de Cc: Ruchika Gupta Subject: [v2] [PATCH 2/3] tpm: use more algorithms than sha256 on pcr_read Date: Tue, 23 Nov 2021 17:20:51 +0530 Message-Id: <20211123115052.124941-2-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211123115052.124941-1-ruchika.gupta@linaro.org> References: <20211123115052.124941-1-ruchika.gupta@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean The current tpm2_pcr_read is hardcoded using SHA256. Make the actual command to TPM configurable to use wider range of algorithms. The current command line is kept as is i.e limited to SHA-256 only. Signed-off-by: Ruchika Gupta --- v2: Change algorithm from u32 to u16 Add parameter description in function declaration cmd/tpm-v2.c | 3 ++- include/tpm-v2.h | 5 ++++- lib/tpm-v2.c | 12 ++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/tpm-v2.c b/cmd/tpm-v2.c index daae91100a..4ea5f9f094 100644 --- a/cmd/tpm-v2.c +++ b/cmd/tpm-v2.c @@ -151,7 +151,8 @@ static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc, data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0); - rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, data, &updates); + rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, TPM2_ALG_SHA256, + data, TPM2_DIGEST_LEN, &updates); if (!rc) { printf("PCR #%u content (%u known updates):\n", index, updates); print_byte_string(data, TPM2_DIGEST_LEN); diff --git a/include/tpm-v2.h b/include/tpm-v2.h index ceff7d245e..4e9dd52cb6 100644 --- a/include/tpm-v2.h +++ b/include/tpm-v2.h @@ -512,13 +512,16 @@ u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data, * @dev TPM device * @idx Index of the PCR * @idx_min_sz Minimum size in bytes of the pcrSelect array + * @algorithm Algorithm used, defined in 'enum tpm2_algorithms' * @data Output buffer for contents of the named PCR + * @digest_len len of the data * @updates Optional out parameter: number of updates for this PCR * * @return code of the operation */ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, - void *data, unsigned int *updates); + u16 algorithm, void *data, u32 digest_len, + unsigned int *updates); /** * Issue a TPM2_GetCapability command. This implementation is limited diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c index 2e7b27bd6b..1bf627853a 100644 --- a/lib/tpm-v2.c +++ b/lib/tpm-v2.c @@ -254,7 +254,8 @@ u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data, } u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, - void *data, unsigned int *updates) + u16 algorithm, void *data, u32 digest_len, + unsigned int *updates) { u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8)); u8 command_v2[COMMAND_BUFFER_SIZE] = { @@ -264,7 +265,7 @@ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, /* TPML_PCR_SELECTION */ tpm_u32(1), /* Number of selections */ - tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */ + tpm_u16(algorithm), /* Algorithm of the hash */ idx_array_sz, /* Array size for selection */ /* bitmap(idx) Selected PCR bitmap */ }; @@ -283,10 +284,13 @@ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, if (ret) return ret; + if (digest_len > response_len) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_len, "ds", 10, &counter, - response_len - TPM2_DIGEST_LEN, data, - TPM2_DIGEST_LEN)) + response_len - digest_len, data, + digest_len)) return TPM_LIB_ERROR; if (updates) From patchwork Tue Nov 23 11:50:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519312 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp8502824imj; Tue, 23 Nov 2021 03:51:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJxKf6hjKgrhgu+QjYQheYDUMBl4RqzFlXDkzDoYkHvOITqJtW4ymQM7ZchNi+j7V+OroN6T X-Received: by 2002:a17:907:1c0a:: with SMTP id nc10mr6918979ejc.211.1637668313403; Tue, 23 Nov 2021 03:51:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637668313; cv=none; d=google.com; s=arc-20160816; b=Co06iBDVLxFCvxRkYSzbVSsUqUsr3z/X2Fq0rMRnLR1eQhwxojnaivzdgAC2f3ZXFJ a4V6CkwxNg2MAbKRxDH/DSWR4fqm67Xl5ohGCCAEK1wt7ybS4xLfLDZoxYhoMw5W1qk2 XheAq4lo0ABmkUsLEIIW8EcdyXufGBL5FjaGwZwpR+tFUWcWfQtoZKo4lRC6FEm4+nr2 14CbeAj3yUIxD6K+zYJvWsr/K0bgQcVO2EGTAiEO9hMiVc82LSN04QRmS3AW5Ed8TqZM TZm6hpwwXfz8n7eavZVTUZ8o4Bx7zrI6Vmpu+lkYz4EkCBiCNTmFvEJmMEimuYdHzBDl B2uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=THJOraiVv0uCtQt1waG8A1icDvTUH8J71myKCCEdGS4=; b=F9JmwhihLXhRWs9UQ8dGytbcRuMmL0qnzm4grANYgkjNnO5dTyJNDvHrShc3mf+H0K srYoTyLCOOsbc/2IWM/4jPRaitKJR217mTfJYSjfDpOYd3Kq3FbhNxz2/CaYq0zMTcsY 7l8W+hRWtTJpB/p/I+tusZzcCWfISnF5ZngNSmTA+lEVmd3Zq5j3hcm4IIOMgWnDzfhR ZoidpKNRmQqw3HFbpaS0/Q+CGOdGv8iq0cPs4dOd+9Lca1u+LdYrkAJ5ZniQP3eiPUTJ Evo1O1IiJFQlIxIjs3tdgdiq3GYJXT1ICXRccgBYgQoTRxjzbm1yORzGoEFnOpzKaL5T mJJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rFXD5+lS; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id dz10si40722817edb.316.2021.11.23.03.51.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 03:51:53 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rFXD5+lS; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 02523836BD; Tue, 23 Nov 2021 12:51:38 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="rFXD5+lS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 487C48352B; Tue, 23 Nov 2021 12:51:33 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D5A9882F70 for ; Tue, 23 Nov 2021 12:51:28 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ruchika.gupta@linaro.org Received: by mail-pl1-x630.google.com with SMTP id q17so16826692plr.11 for ; Tue, 23 Nov 2021 03:51:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=THJOraiVv0uCtQt1waG8A1icDvTUH8J71myKCCEdGS4=; b=rFXD5+lSiawT7tVMLfMjmSvbDtTvW+ioxJEdNKH6Gj3Kwo2k9bunbGFQKQ6aZ2MxHd o/ZiwJMPEKZtWWs+QoB2/xI4Ww5oW+l+zhDmbK7M+hw1vK2FTsFLuXPYWrYVan583xak EOez3sGfT2PVjYfNHERxkZnObDWXkZ1aNU0M8MAT7c4Y+uWxmjGM35YRJikiHhf8ZNoh qtT7AHD5lCqkjF2AvBivo0xwxMqsrKAa67B2FtK9E1PTui30+fIVdyq7S5FmlUdL9X3C UrsSgADkIpyZcRZsA3S1E2sZ/okgPfI2rBh765bTl3/l0v22/uDhaUqBfIO2TJ3f/qXs c8Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=THJOraiVv0uCtQt1waG8A1icDvTUH8J71myKCCEdGS4=; b=uyHuAZkpJFx7qJhdJpuBuATldlrbkqFFc7EVNeC86rjPEvdiHrS0JUAvInQEnxJuSy CH/3UOCHOz/PnLMtUy6jjApCcfFBwvd70wuyS8TM0gzFSUumNce5sPX3fQiCxw4PH2w4 EWCoHuRlxiSPXtHVDKi/x6dprIKmqGNPpXE09FtgCpSdUYS2nRVWa3K2LIZuIm/Qg8uf oMd5RRNoo7GtmGWdOOVZNx2nBNvAWQqPjP/yUfi+RlmrySy7T9GIjHGFtxlIgs/MQYyP z9vVUQWQwk7L4wlifUiwshR0Y2rKchS3SGhi27JN5tv+McXsMx0eHikNoT5xeuoqFrK6 gtEg== X-Gm-Message-State: AOAM533DUq7FLRQ9IwBWLvFs0BC2ysQauWvo2Ou16EloyN2RFsf1hpmr FJWqQmbhEejyhnm/yQ+Lu2sfcMuqfWMVvw== X-Received: by 2002:a17:90b:1c87:: with SMTP id oo7mr2048928pjb.159.1637668286838; Tue, 23 Nov 2021 03:51:26 -0800 (PST) Received: from localhost.localdomain ([106.215.91.146]) by smtp.gmail.com with ESMTPSA id p66sm9159468pga.31.2021.11.23.03.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 03:51:26 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de Cc: Ruchika Gupta Subject: [v2] [PATCH 3/3] efi_loader: Extend PCR's for firmware measurements Date: Tue, 23 Nov 2021 17:20:52 +0530 Message-Id: <20211123115052.124941-3-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211123115052.124941-1-ruchika.gupta@linaro.org> References: <20211123115052.124941-1-ruchika.gupta@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Firmwares before U-Boot may be capable of doing tpm measurements and passing them to U-Boot in the form of eventlog. However there may be scenarios where the firmwares don't have TPM driver and are not capable of extending the measurements in the PCRs. Based on TCG spec, if previous firnware has extended PCR's, PCR0 would not be 0. So, read the PCR0 to determine if the PCR's need to be extended as eventlog is parsed or not. Signed-off-by: Ruchika Gupta --- v2 : Removed check for PCR0 in eventlog lib/efi_loader/efi_tcg2.c | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index c3ebdf92f5..133fe8291a 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -199,6 +199,43 @@ static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index, return EFI_SUCCESS; } +/* tcg2_pcr_read - Read PCRs for a TPM2 device for a given tpml_digest_values + * + * @dev: device + * @digest_list: list of digest algorithms to extend + * + * @Return: status code + */ +static efi_status_t tcg2_pcr_read(struct udevice *dev, u32 pcr_index, + struct tpml_digest_values *digest_list) +{ + struct tpm_chip_priv *priv; + unsigned int updates, pcr_select_min; + u32 rc; + size_t i; + + priv = dev_get_uclass_priv(dev); + if (!priv) + return EFI_DEVICE_ERROR; + + pcr_select_min = priv->pcr_select_min; + + for (i = 0; i < digest_list->count; i++) { + u16 hash_alg = digest_list->digests[i].hash_alg; + u8 *digest = (u8 *)&digest_list->digests[i].digest; + + rc = tpm2_pcr_read(dev, pcr_index, pcr_select_min, + hash_alg, digest, alg_to_len(hash_alg), + &updates); + if (rc) { + EFI_PRINT("Failed to read PCR\n"); + return EFI_DEVICE_ERROR; + } + } + + return EFI_SUCCESS; +} + /* put_event - Append an agile event to an eventlog * * @pcr_index: PCR index @@ -1427,6 +1464,8 @@ efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, u32 pcr, pos; u64 base; u32 sz; + bool extend_pcr = false; + int i; ret = platform_get_eventlog(dev, &base, &sz); if (ret == EFI_SUCCESS) { @@ -1447,6 +1486,26 @@ efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, return EFI_COMPROMISED_DATA; } + ret = tcg2_pcr_read(dev, 0, &digest_list); + if (ret) { + log_err("Error reading PCR 0\n"); + return ret; + } + + /* + * If PCR0 is 0, previous firmware didn't have the capability + * to extend the PCR. In this scenario, extend the PCR as + * the eventlog is parsed. + */ + for (i = 0; i < digest_list.count; i++) { + u8 buffer[TPM2_DIGEST_LEN] = { 0 }; + u16 hash_alg = digest_list.digests[i].hash_alg; + + if (!memcmp((u8 *)&digest_list.digests[i].digest, + buffer, alg_to_len(hash_alg))) + extend_pcr = true; + } + while (pos < sz) { ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list, &pcr); @@ -1454,6 +1513,24 @@ efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, log_err("Error parsing event\n"); return ret; } + + if (extend_pcr) { + ret = tcg2_pcr_extend(dev, pcr, &digest_list); + if (ret != EFI_SUCCESS) { + log_err("Error in extending PCR\n"); + return ret; + } + + /* Clear the digest for next event */ + for (i = 0; i < digest_list.count; i++) { + u16 hash_alg = + digest_list.digests[i].hash_alg; + u8 *digest = + (u8 *)&digest_list.digests[i].digest; + + memset(digest, 0, alg_to_len(hash_alg)); + } + } } memcpy(log_buffer, buffer, sz);