From patchwork Thu Nov 18 06:17:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519234 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp518404imj; Wed, 17 Nov 2021 22:18:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzex07qARra8s6gd0aZZXJKY1oVkM9SMsv7ZeIgph+TvMH1cVZmBvYBD6o+Hdeqoz7oJ02H X-Received: by 2002:a05:6402:26c8:: with SMTP id x8mr7491479edd.156.1637216304060; Wed, 17 Nov 2021 22:18:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637216304; cv=none; d=google.com; s=arc-20160816; b=B1z25gJUczX+0HKs9CxHijo3UJA4Yn5cj4FU15KuVkX9DRTc/zLmznW8ne80jvA0bJ 24SGpcfAh42x+h04wtl4lw6lnAnm7wgStbvMH6kUgyLcxYU65xuBUE2Tm28A3kYe63qe chDN5bSvtZZDPwJ2ZW3rfJTjZ7fY5BhkxLF/mfLVZU9Utq1cMMzSoylFhinXv+qtFsN3 VVaBLyZ8UXerQDD5rpSD7a6wR+Jgfs3T8hfJDESZb/u5DCZjTpzK5kpcPa8NCG0NTlJT noGghRtkteGn/sYfcj+NDxSCsmBW+5YXNW0v2pZg32ghTs0tOdqvypgR6lP7XeOZH/Eg 8k6A== 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:message-id:date:subject:cc:to:from:dkim-signature; bh=mhlK85yz1krYfu2YU3OEcJ3yUue2wFQ/Gnh3lmanz2I=; b=d9rmK0UZvYym4pCkmETkjIE7r3BiW2J1HurGFzzkbCcwiLjkBEBGgifYJ7ao3q4Y9O XTJHjGyPedjEzppJb5G7YhgVbVGtBtF/s51kWxHu+HOUwypy9+l7nR/AAdBSW6fcar+H nltzU8hTrIAcca9UfDL35rxFjJ8xCUzLvdkR/qS95xrwEZfwDXxNSL/srTqMgUHU9DHU hoapnMmA0dh9fn+2jdItTNReCFpk26mevbne4aNkRNZITLlG12f0eJSAVh2OsdlhO3Tq WccXmSjmTycwLrNyLmn14FQLha+vjJ+dZ9SIyoATpE6xcSsVJmRTFgO/v78V+RO+s2jg idLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zjTWXMHd; 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 nd29si6383323ejc.75.2021.11.17.22.18.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 22:18:24 -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=zjTWXMHd; 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 63DD181EE5; Thu, 18 Nov 2021 07:18:20 +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="zjTWXMHd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BF34E81FD5; Thu, 18 Nov 2021 07:18:18 +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-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) (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 A409281C08 for ; Thu, 18 Nov 2021 07:18:13 +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-x431.google.com with SMTP id g19so4936344pfb.8 for ; Wed, 17 Nov 2021 22:18:13 -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:mime-version :content-transfer-encoding; bh=mhlK85yz1krYfu2YU3OEcJ3yUue2wFQ/Gnh3lmanz2I=; b=zjTWXMHdLsXw1izcSuXM7OetV6+LZ9Odpye+DHvView2/xhdvMyu4hWY4k5vjDo8DM 1u6MEg1t8sBanCmy2ylyZUiC27z2ZG4HR29mwhLBSmUhLTeumzvglVF552GX9Tzhu08m q3iRx5fXLGY0Gdb5Vxnf9CQUd223jCWio+GOUOWCreu1S9QNTVoyyfP6qiMRlXhERfJa 0mtqpvysVwYWe3AUgHT+MXD+z6c7XJ05mSekpO9Cm5AES4Jq6YRDtyevpmgRtXiq32g6 Mm9XZpTJ8oo8ag4HfA7Okh4jWUXrCzzboIk7SWHhb0UPxKdhx/qL7FA3UwZ2MMCaeOlF WKoA== 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:mime-version :content-transfer-encoding; bh=mhlK85yz1krYfu2YU3OEcJ3yUue2wFQ/Gnh3lmanz2I=; b=fSEM6mxqylAQvbEvPAIX9bHIpPpqBuGrZ4+sqCzfE8PC2B0ghLH60yek2rU6PXgXRa tbPFUu8Y6wLdKVCHRr9oWGpCr1qKkIa8MJWiqt4FgAItgMzb0xJ4LYNMK/uL98C8LjNU cM/12UsHCk1XJTd2yeax6MMthRXX94jCqOfJtrdIf1fdGM4E6mN6VU49PR3KIZ+jMTDZ s05lcOXysX9l8G+LFglleTEduU7kYURr6ULXDBlXuPB83KeNn32vo+cs390o17jF7/0i uJ3lINostgs6qXkYhDmnF8GXnejulNM2wPRmlbDcOvOLU7q4pczsYfmi4XRSEnPcP3O1 7V0w== X-Gm-Message-State: AOAM530s8aIW32qUPlckGxG/OWc/PH6jP/LQlmxtxr3QVGGK0vLEQUFS ZQt3v5SWEr6yzpQp1uZ/KKYHxTgxE1BnvA== X-Received: by 2002:a05:6a00:80c:b0:49f:de3e:e475 with SMTP id m12-20020a056a00080c00b0049fde3ee475mr12884491pfk.27.1637216291627; Wed, 17 Nov 2021 22:18:11 -0800 (PST) Received: from localhost.localdomain ([122.177.104.97]) by smtp.gmail.com with ESMTPSA id h3sm1745920pfc.204.2021.11.17.22.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 22:18:11 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de, ilias.apalodimas@linaro.org, xypron.glpk@gmx.de, agraf@csgraf.de Cc: Ruchika Gupta Subject: [PATCH 1/3] efi_loader: Add check for event log passed from firmware Date: Thu, 18 Nov 2021 11:47:48 +0530 Message-Id: <20211118061751.3334620-1-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 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 --- lib/efi_loader/efi_tcg2.c | 312 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 296 insertions(+), 16 deletions(-) diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index 74f0bef239..c97766eae3 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -276,6 +276,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 * @@ -1107,6 +1146,205 @@ 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; +} + /** * create_specid_event() - Create the first event in the eventlog * @@ -1241,16 +1479,19 @@ out: /** * efi_init_event_log() - initialize an eventlog */ -static efi_status_t efi_init_event_log(void) +static efi_status_t efi_init_event_log(struct udevice *dev) { /* * vendor_info_size is currently set to 0, we need to change the length * and allocate the flexible array member if this changes */ struct tcg_pcr_event *event_header = NULL; - struct udevice *dev; + struct tpml_digest_values digest_list; size_t spec_event_size; efi_status_t ret; + u32 pcr, pos; + u64 base; + u32 sz; ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) @@ -1266,26 +1507,65 @@ 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; 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 = platform_get_eventlog(dev, &base, &sz); + if (ret == EFI_SUCCESS && sz < TPM2_EVENT_LOG_SIZE) { + void *buffer = (void *)base; + + pos = 0; + /* Parse the eventlog to check for its validity */ + ret = parse_event_log_header(buffer, sz, &pos); + if (ret || pos > sz) { + ret = EFI_COMPROMISED_DATA; + goto free_pool; + } + + ret = parse_specid_event(dev, buffer, sz, &pos, &digest_list); + if (ret || pos > sz) { + log_err("Error parsing SPEC ID Event\n"); + ret = EFI_COMPROMISED_DATA; + goto free_pool; + } + + while (pos < sz) { + ret = tcg2_parse_event(dev, buffer, sz, &pos, + &digest_list, &pcr); + if (ret) { + log_err("Error parsing event\n"); + goto free_pool; + } + } + + memcpy(event_log.buffer, buffer, sz); + event_log.pos = sz; + } else { + 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 = create_final_event(); if (ret != EFI_SUCCESS) @@ -1664,7 +1944,7 @@ efi_status_t efi_tcg2_register(void) return EFI_SUCCESS; } - ret = efi_init_event_log(); + ret = efi_init_event_log(dev); if (ret != EFI_SUCCESS) goto fail; From patchwork Thu Nov 18 06:17: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: 519236 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp518745imj; Wed, 17 Nov 2021 22:18:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJxkEBmzzI0VVEqgX9PBBYVkHgHyKulDABMZslbUZQ3T36TZKnR6LMHD31OVLXf09yrdxXPn X-Received: by 2002:aa7:d997:: with SMTP id u23mr7561224eds.164.1637216327576; Wed, 17 Nov 2021 22:18:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637216327; cv=none; d=google.com; s=arc-20160816; b=zCkm137ApK61vyA5qK0aj0Pzb9J+TSFkarZgTexSlY73EasxO7UsZkkQ0koviwiZlA rRtKtJemVpTDPTO1+/FjSKryBcWydt9VDnVvrpfn1MFi3nEPfDmdrHmyqZZ6PDSIZiFo cwN3I9Bo863zvtmXr8XgDK4novvEMUYgPylO85ZPwEKUb0ju2YOdaBOaDaP1xZhgpfbt mmYsp7tiaYlH07C07MxYMfBy2p+db2GRXALUqQZX/PwgM8L93Kjo/NuQnqryoo8B0irL qmcNAYlTU2lEtoOqUJRA0U4Z+TgSAFAjmxuG98vY+cQHEIXfaA/rq4gbP1NMFdUVPNVx cy3g== 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=OZ8ShBxfCLS7gehzwlLpOyW+9taeYGcGDs5nlsQxF/o=; b=xPMNe1WEe7wBAxJPQ8Kg/vwUF0g3cfSt2z87u9+NjDpUsJMFRq1qWavMmp4fWzqDlr 4r88GIZA76T6/rRnG/KrUlnv6Gm3at3g6AxDnZaKnHDCtbOFcQ8LwjThbqnWhvCbIAUN lWr1Sr7J3DTIN3xyVNp8a6sD40Axgcqift0OeIIZlZxP3lPZ5VHG8XFzn8BzW6lx4Son hEsekGFgn4yd9bfJ08WWrpr9kSDHwrfFp1c+Inv+TXV2ntMjDXJpqDYm9radP1e8Q0oo h65sd04UpObaUVJ+4v3lXcucnmbwKLe7Bcv4HepX00R9Tz8qKYtNPV0KxbgkxOfcp8D/ zalA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="a4xPh/q7"; 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 go31si5558635ejc.244.2021.11.17.22.18.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 22:18:47 -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="a4xPh/q7"; 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 806B382F70; Thu, 18 Nov 2021 07:18:42 +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="a4xPh/q7"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6401A82F67; Thu, 18 Nov 2021 07:18:35 +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-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) (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 F29CF82F49 for ; Thu, 18 Nov 2021 07:18: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-pf1-x435.google.com with SMTP id x5so4996213pfr.0 for ; Wed, 17 Nov 2021 22:18: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=OZ8ShBxfCLS7gehzwlLpOyW+9taeYGcGDs5nlsQxF/o=; b=a4xPh/q7q9PdkVhVjUbAJngIfV1rvbVMZ9M6mzSMv/Wt4pc+6GCUQazPrKUtpPIJ6l M5uRzqRsbpKGpGKA078/XWLcTj8u5NFrIJOrO5X1PkjDaL/5Bh1T5sun/sAPT0fkX9TB 827kxELw8aNOipnGeb3Jx/v/8GERYKxicx+WRALBeGgFjP8TEPRlzZ0NgvzqCvfAvrDv BE9DZG1BFCTWmJYJ7uXRFi8uksylOWQvXSw38d9s6XLsXu7/+fn+vCuoPJ+uCbK5kmRL mJXEixUb6i9pfQLd6R0vu8OeGXyCFFdH8BDuHRue0CqAh7eCzVnuG2j7hbN4lMhMvnT9 tkVw== 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=OZ8ShBxfCLS7gehzwlLpOyW+9taeYGcGDs5nlsQxF/o=; b=f7j97q1NdHiTHYeBcNjz415XfyS3/vr4B4o9PWuSJvTL5zOAdz9kjscNamBxQI73vM zVSGAIW4Z6Etfj3H4o95yhzpKeruznaXt76tQmpG3rK3HlzIO7ClDor5/Cd5bu9Esapg OTfBwn+XCBbZQzQRv3ylWkXwx0WY9WdbR+xH0+sGsZ3Peb+TXa5jqPPmlRBAXSEBJr1L RpzU2AiJp95eb74v/Ok01Tynk2wsfjFlLyzeiOtOBzK9akJdyRUvBklvHJ1hm1nE1vHI Ee1xv6sTjok/z/qdzB94g7TDZ7JJjp8k7L0g0lw59wraC9r1Wc9ScgP4AR7B0OHiPxxU a4fA== X-Gm-Message-State: AOAM531hCIX5KRYI0WLdUZZ0FAn08R5BeSVF0rY4cVRca3Ne/2m9ziT9 v8KIrxJHt/pXbpjRqFyczOairN5L3Z22bQ== X-Received: by 2002:a63:804a:: with SMTP id j71mr9612309pgd.472.1637216307064; Wed, 17 Nov 2021 22:18:27 -0800 (PST) Received: from localhost.localdomain ([122.177.104.97]) by smtp.gmail.com with ESMTPSA id h3sm1745920pfc.204.2021.11.17.22.18.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 22:18:26 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de, ilias.apalodimas@linaro.org, xypron.glpk@gmx.de, agraf@csgraf.de Cc: Ruchika Gupta Subject: [PATCH 2/3] tpm: use more algorithms than sha256 on pcr_read Date: Thu, 18 Nov 2021 11:47:50 +0530 Message-Id: <20211118061751.3334620-3-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211118061751.3334620-1-ruchika.gupta@linaro.org> References: <20211118061751.3334620-1-ruchika.gupta@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 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 --- cmd/tpm-v2.c | 3 ++- include/tpm-v2.h | 3 ++- lib/tpm-v2.c | 12 ++++++++---- 3 files changed, 12 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 e6b68769f3..07dfaa64fb 100644 --- a/include/tpm-v2.h +++ b/include/tpm-v2.h @@ -518,7 +518,8 @@ u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data, * @return code of the operation */ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, - void *data, unsigned int *updates); + u32 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 235f8c20d4..9f86eab814 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) + u32 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 Thu Nov 18 06:17: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: 519237 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp518837imj; Wed, 17 Nov 2021 22:18:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJlmU59rfuB65w2esGq2iMsLjvgwvAsMPbgWALgeCnSmRrDOVcXpa1FqzhpzZ5JJTD65kf X-Received: by 2002:a17:906:2cd5:: with SMTP id r21mr30979176ejr.435.1637216335382; Wed, 17 Nov 2021 22:18:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637216335; cv=none; d=google.com; s=arc-20160816; b=fMQeHvVwVox9lq2wFZ2Np5I6bQlC9GwF+8wI3o/xZS/0UBUnWqAAUpe1Bjngtv62FQ HpMRVkPk/C6HdeL/ccejhXa+rWOmNbojK6cTtTX9XGGiHaUsmeTTffUz+cnO7aYHtdn6 bUEBMEdTA12CnADaZoWgUZ7PmlLeC58JVH3/5HueRBJK4AU6Vte52dmgGvYeHPJ6j0pU RisAHSfwkufTD5ZGV2k4WW5eAPhK8/a+0LoDpDEMfvS+H7synAJhzvhacpSfWI3ECUpy idbuI44sV+QQ34Pk+Tx4uV35rGJCWs4bQ4uavGUEswI8nJEHSqLSxJnkL7ldPiYnc1fV kmNw== 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=7o0EAogD3XYG2DtV2QJyqPn12wNNge8y6hWsQcDbVkA=; b=yPPBXRgsrjQreJGIIR8+vJReoIW9Dy/2KIhVxJwzfEIKneADeyR0JL6HD6cbgDFyZD hL0PaH6NzS5FT/F3QULuuDSbO73J9ffMuPk5c4A359thzw4qa7MeLRJiBqmN9HD2TREy kb6uHc5piaCoS6nY7s/tV+G4RIyY9ZSaNhd/Qzu+5Zu14cQcSUJBodSqld+B8+j+ehuw JTO7jMjpqchVwFhORrkZFo8/sUsYVqTIwlBstYJhzi+7Ibn9acCAm5KBu/N/HDm5eIRS dAygh/+rUJnK3Chj0Szk0LTFSGr+c1I02ud+aIlrlzFMCMw61zXptUedVZ1SuV0AB9+3 h+XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yvw0pown; 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 gk1si5877655ejc.699.2021.11.17.22.18.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 22:18:55 -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=Yvw0pown; 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 CA74982F78; Thu, 18 Nov 2021 07:18:48 +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="Yvw0pown"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 44C0582F65; Thu, 18 Nov 2021 07:18:40 +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-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) (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 936E182EC6 for ; Thu, 18 Nov 2021 07:18:33 +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-pj1-x1030.google.com with SMTP id v23so4270908pjr.5 for ; Wed, 17 Nov 2021 22:18:33 -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=7o0EAogD3XYG2DtV2QJyqPn12wNNge8y6hWsQcDbVkA=; b=Yvw0pown0jXEV5BWgCLy35LpqXlx4N6csVmVeVHo1xsxFht5rg7aLKJ8iuNF0UZh1l w+wnibDxFba3sTOP1vZFXNdTST6yhBOqZ5OEYAjQO3xHakoWw5SRZ2uc96R49z1r5J4J PWMhc9Kpv7sahxhRy8oorKZiaFOsT6RSCKbZ1WQG0U0P/gMvBkMVR2+8G37fgJSi68E4 3Uu4dhAjAtHwezaFJHBF654fQaBnNhgevWMQ7GkHny8Uc/BNSOPkAYs3gdLwV2EON++e Lto9V8fWpgUt8N+vpnQoH0r2QruGhAGUorwrF7+B3GEK9pXFd/f2jxs6F8Cf+eumUM0m 1jLg== 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=7o0EAogD3XYG2DtV2QJyqPn12wNNge8y6hWsQcDbVkA=; b=2D8FBelepZ096+lopbndyTttvB0qc+rrVo3ZWJik6dCkaxSiMlzJ2EnBU9toGuPeBY G8DkrWRZK5Vx1MSr6BLmY+hkmzO8OX95h4fZxTfixNNV8i3law5AXfce9UCTw8r8RfmR aoLCWDgX9xdWbBRdvOfEZfr83mUUhyUqvEJzHi2NOID4hmpXQpeRtes+PrASHq+bicl5 hOExThV8xO0WfPPzx8l3vXJ6eF2Osp4ggav/uzMpgS+skeE/lKIqXfICngMpnfS6cxWd IbXZW932BrBv22e+OagpicNYbNofz6KHdwCnlWzCBWlzgmbaRVq/o5sGxhblY65XFgtu /APw== X-Gm-Message-State: AOAM531+jZMaO4hcpgWql7NWKiHVQvf9GoygPYB1Y6HIK9mHUMweO9tj tDOF8iuNVM+yQoV1MbkouCY+iBBxJx+Fdg== X-Received: by 2002:a17:902:a714:b0:143:d007:412f with SMTP id w20-20020a170902a71400b00143d007412fmr22117194plq.18.1637216311814; Wed, 17 Nov 2021 22:18:31 -0800 (PST) Received: from localhost.localdomain ([122.177.104.97]) by smtp.gmail.com with ESMTPSA id h3sm1745920pfc.204.2021.11.17.22.18.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 22:18:31 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de, ilias.apalodimas@linaro.org, xypron.glpk@gmx.de, agraf@csgraf.de Cc: Ruchika Gupta Subject: [PATCH 3/3] efi_loader: Extend PCR's for firmware measurements Date: Thu, 18 Nov 2021 11:47:51 +0530 Message-Id: <20211118061751.3334620-4-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211118061751.3334620-1-ruchika.gupta@linaro.org> References: <20211118061751.3334620-1-ruchika.gupta@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 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. To cater to such platforms, read the PCR0 to determine if the previous firmwares have extended the PCR0. If not, then extend the PCR's as the eventlog is parsed. Signed-off-by: Ruchika Gupta --- lib/efi_loader/efi_tcg2.c | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index c97766eae3..cbd0c7d224 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -178,6 +178,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; +} + /* tcg2_agile_log_append - Append an agile event to out eventlog * * @pcr_index: PCR index @@ -1488,10 +1525,12 @@ static efi_status_t efi_init_event_log(struct udevice *dev) struct tcg_pcr_event *event_header = NULL; struct tpml_digest_values digest_list; size_t spec_event_size; + bool extend_pcr = false; efi_status_t ret; u32 pcr, pos; u64 base; u32 sz; + int i; ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) @@ -1541,6 +1580,26 @@ static efi_status_t efi_init_event_log(struct udevice *dev) goto free_pool; } + ret = tcg2_pcr_read(dev, 0, &digest_list); + if (ret) { + log_err("Error reading PCR 0\n"); + goto free_pool; + } + + /* + * 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); @@ -1548,6 +1607,33 @@ static efi_status_t efi_init_event_log(struct udevice *dev) log_err("Error parsing event\n"); goto free_pool; } + + if (pcr != 0) { + /* + * Eventlog passed by firmware should extend + * PCR0 only. + */ + log_err("Invalid PCR\n"); + goto free_pool; + } + + if (extend_pcr) { + ret = tcg2_pcr_extend(dev, pcr, &digest_list); + if (ret != EFI_SUCCESS) { + log_err("Error in extending PCR\n"); + goto free_pool; + } + + /* 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(event_log.buffer, buffer, sz);