Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2005 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *	Seiji Munetoh <munetoh@jp.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	Stefan Berger <stefanb@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	Reiner Sailer <sailer@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	Nayna Jain <nayna@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Access to the event log extended by the TCG BIOS of PC platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/tpm_eventlog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "../tpm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) struct acpi_tcpa {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct acpi_table_header hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u16 platform_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		struct client_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 			u32 log_max_len __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 			u64 log_start_addr __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		} client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		struct server_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			u16 reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			u64 log_max_len __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			u64 log_start_addr __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		} server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /* Check that the given log is indeed a TPM2 log. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static bool tpm_is_tpm2_log(void *bios_event_log, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct tcg_efi_specid_event_head *efispecid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct tcg_pcr_event *event_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (len < sizeof(*event_header))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	len -= sizeof(*event_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	event_header = bios_event_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (len < sizeof(*efispecid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	efispecid = (struct tcg_efi_specid_event_head *)event_header->event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	n = memcmp(efispecid->signature, TCG_SPECID_SIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		   sizeof(TCG_SPECID_SIG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return n == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) /* read binary bios log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) int tpm_read_log_acpi(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct acpi_tcpa *buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	void __iomem *virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	u64 len, start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct tpm_bios_log *log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct acpi_table_tpm2 *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct acpi_tpm2_phy *tpm2_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	log = &chip->log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* Unfortuntely ACPI does not associate the event log with a specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * TPM, like PPI. Thus all ACPI TPMs will read the same log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!chip->acpi_dev_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		status = acpi_get_table("TPM2", 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 					(struct acpi_table_header **)&tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		if (tbl->header.length <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				sizeof(*tbl) + sizeof(struct acpi_tpm2_phy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		tpm2_phy = (void *)tbl + sizeof(*tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		len = tpm2_phy->log_area_minimum_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		start = tpm2_phy->log_area_start_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		if (!start || !len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		/* Find TCPA entry in RSDT (ACPI_LOGICAL_ADDRESSING) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		status = acpi_get_table(ACPI_SIG_TCPA, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 					(struct acpi_table_header **)&buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		switch (buff->platform_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		case BIOS_SERVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			len = buff->server.log_max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			start = buff->server.log_start_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		case BIOS_CLIENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			len = buff->client.log_max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			start = buff->client.log_start_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (!len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		dev_warn(&chip->dev, "%s: TCPA log area empty\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* malloc EventLog space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	log->bios_event_log = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (!log->bios_event_log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	log->bios_event_log_end = log->bios_event_log + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	virt = acpi_os_map_iomem(start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (!virt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	memcpy_fromio(log->bios_event_log, virt, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	acpi_os_unmap_iomem(virt, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (chip->flags & TPM_CHIP_FLAG_TPM2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	    !tpm_is_tpm2_log(log->bios_event_log, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		/* try EFI log next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	kfree(log->bios_event_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	log->bios_event_log = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }