Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jVkSC-0002Vz-Dh for mharc-grub-devel@gnu.org; Mon, 04 May 2020 19:24:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51388) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jVkSA-0002O1-CM for grub-devel@gnu.org; Mon, 04 May 2020 19:24:50 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:50872) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jVkS7-0007t3-5t for grub-devel@gnu.org; Mon, 04 May 2020 19:24:49 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 044NNaxM190740; Mon, 4 May 2020 23:24:39 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=U9MsE8YG2XA+6eWHIBqjagX72ut5XbC/DYIUDNw4JjM=; b=fRKv7lRijU3c08AnnVripNEEzP2/cn27a/3vJeN3SpHe8jFanH4PL8Sg76CJNv1TIk5G rfpQSpo5Ug3rf1rQoOMxZtmu4t/CsUL3wLLeZzSGgJKWgX16BxhfrixxaMsbooGQCJOa Tu5qCBBgnxG7oTXckryftjhGzA8e7Zebff5tmMtGZw3upwr+VNkU712SmZmCeEfvQ/3h jkXKtupfJjFqL5dCSdG6NCNlMlToGiaU+WASwV/PDpWPHplQ3U9eF0yFADDnRPqh/q17 Ot9AhKrcnLfI8s5qE5I8bLwDrejOOK48dJMJe8EegmCaoTMxScTrPdr2SqL3Y0Bzg/b1 RA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2130.oracle.com with ESMTP id 30s09r1v3x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 May 2020 23:24:39 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 044NLcPt139999; Mon, 4 May 2020 23:22:39 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userp3030.oracle.com with ESMTP id 30t1r3j0t0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 May 2020 23:22:39 +0000 Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 044NMbGG001531; Mon, 4 May 2020 23:22:37 GMT Received: from tomti.i.net-space.pl (/10.175.189.148) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 04 May 2020 16:22:36 -0700 From: Daniel Kiper To: grub-devel@gnu.org, linux-kernel@vger.kernel.org, trenchboot-devel@googlegroups.com, x86@kernel.org Cc: alexander.burmashev@oracle.com, andrew.cooper3@citrix.com, ard.biesheuvel@linaro.org, dpsmith@apertussolutions.com, eric.snowberg@oracle.com, javierm@redhat.com, kanth.ghatraju@oracle.com, konrad.wilk@oracle.com, krystian.hebel@3mdeb.com, lukasz.hawrylko@linux.intel.com, michal.zygowski@3mdeb.com, mjg59@google.com, phcoder@gmail.com, pirot.krol@3mdeb.com, pjones@redhat.com, ross.philipson@oracle.com Subject: [GRUB PATCH RFC 08/18] i386/tpm: Add TPM TIS and CRB driver Date: Tue, 5 May 2020 01:21:22 +0200 Message-Id: <20200504232132.23570-9-daniel.kiper@oracle.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200504232132.23570-1-daniel.kiper@oracle.com> References: <20200504232132.23570-1-daniel.kiper@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9611 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 suspectscore=2 spamscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005040182 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9611 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 lowpriorityscore=0 spamscore=0 adultscore=0 clxscore=1015 suspectscore=2 priorityscore=1501 malwarescore=0 mlxlogscore=999 phishscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005040182 Received-SPF: pass client-ip=156.151.31.86; envelope-from=daniel.kiper@oracle.com; helo=userp2130.oracle.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/04 19:22:12 X-ACL-Warn: Detected OS = Linux 3.x [generic] [fuzzy] X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 May 2020 23:24:50 -0000 It will be used by Intel TXT secure launcher introduced by subsequent patches. Signed-off-by: Daniel Kiper --- grub-core/commands/i386/tpm.c | 182 ++++++++++++++++++++++++++++++++++++++++++ include/grub/i386/tpm.h | 36 +++++++++ 2 files changed, 218 insertions(+) create mode 100644 grub-core/commands/i386/tpm.c create mode 100644 include/grub/i386/tpm.h diff --git a/grub-core/commands/i386/tpm.c b/grub-core/commands/i386/tpm.c new file mode 100644 index 000000000..ff29c2e85 --- /dev/null +++ b/grub-core/commands/i386/tpm.c @@ -0,0 +1,182 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2020 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + * + * TPM TIS and CRB driver. + * + * Note: It is suggested to not use this driver together with UEFI TPM driver. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +#define TPM_MMIO_BASE 0xfed40000 + +/* TIS registers. */ +#define TPM_ACCESS 0x0000 +#define TPM_INTF_CAPABILITY 0x0014 +#define TPM_INTERFACE_ID 0x0030 + +/* CRB registers. */ +#define TPM_LOC_CTRL 0x0008 + +#define TPM_12_TIS_INTF_12 0x0 +#define TPM_12_TIS_INTF_13 0x2 +#define TPM_20_TIS_INTF_13 0x3 + +#define TPM_CRB_INTF_ACTIVE 0x1 + +#define TIS_RELINQUISH_LCL 0x20 +#define CRB_RELINQUISH_LCL 0x0002 + +/* TODO: Do we need GRUB_PACKED for unions below??? */ + +union tpm_interface_id +{ + grub_uint32_t raw; + struct + { + grub_uint32_t interface_type:4; + grub_uint32_t interface_version:4; + grub_uint32_t cap_locality:1; + grub_uint32_t reserved_0:4; + grub_uint32_t cap_tis:1; + grub_uint32_t cap_crb:1; + grub_uint32_t cap_ifres:2; + grub_uint32_t interface_selector:2; + grub_uint32_t intf_sel_lock:1; + grub_uint32_t reserved_1:4; + grub_uint32_t reserved_2:8; + }; +} GRUB_PACKED; +typedef union tpm_interface_id tpm_interface_id_t; + +union tpm_intf_capability +{ + grub_uint32_t raw; + struct + { + grub_uint32_t data_avail_int_support:1; + grub_uint32_t sts_valid_int_support:1; + grub_uint32_t locality_change_int_support:1; + grub_uint32_t interrupt_level_high:1; + grub_uint32_t interrupt_level_low:1; + grub_uint32_t interrupt_edge_rising:1; + grub_uint32_t interrupt_edge_falling:1; + grub_uint32_t command_ready_int_support:1; + grub_uint32_t burst_count_static:1; + grub_uint32_t data_transfer_size_support:2; + grub_uint32_t reserved_0:17; + grub_uint32_t interface_version:3; + grub_uint32_t reserved_1:1; + }; +} GRUB_PACKED; +typedef union tpm_intf_capability tpm_intf_capability_t; + +typedef enum + { + TPM_INTF_NONE = 0, + TPM_INTF_TIS, + TPM_INTF_CRB + } +tpm_intf_t; + +static grub_tpm_ver_t tpm_ver = GRUB_TPM_NONE; +static tpm_intf_t tpm_intf = TPM_INTF_NONE; + +grub_tpm_ver_t +grub_get_tpm_ver (void) +{ + return tpm_ver; +} + +/* Localities 0-4 are supported only. */ +void +grub_tpm_relinquish_lcl (grub_uint8_t lcl) +{ + grub_addr_t addr = TPM_MMIO_BASE + lcl * GRUB_PAGE_SIZE; + + if (tpm_intf == TPM_INTF_TIS) + grub_writeb (TIS_RELINQUISH_LCL, (void *) (addr + TPM_ACCESS)); + else if (tpm_intf == TPM_INTF_CRB) + grub_writel (CRB_RELINQUISH_LCL, (void *) (addr + TPM_LOC_CTRL)); +} + +static grub_err_t +grub_cmd_tpm_type (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char *argv[] __attribute__ ((unused))) +{ + const char *tpm_ver_s = "NONE"; + const char *tpm_intf_s = "NONE"; + + if (tpm_ver == GRUB_TPM_12) + tpm_ver_s = "1.2"; + else if (tpm_ver == GRUB_TPM_20) + tpm_ver_s = "2.0"; + + if (tpm_intf == TPM_INTF_TIS) + tpm_intf_s = "TIS"; + else if (tpm_intf == TPM_INTF_CRB) + tpm_intf_s = "CRB"; + + grub_printf ("TPM VER: %s\nTPM INTF: %s\n", tpm_ver_s, tpm_intf_s); + + return GRUB_ERR_NONE; +} + +static grub_command_t cmd_tpm_type; + +GRUB_MOD_INIT (tpm) +{ + tpm_interface_id_t intf_id; + tpm_intf_capability_t intf_cap; + + cmd_tpm_type = grub_register_command ("tpm_type", grub_cmd_tpm_type, + NULL, N_("Show TPM version and interface type.")); + + intf_cap.raw = grub_readl ((void *)(grub_addr_t) (TPM_MMIO_BASE + TPM_INTF_CAPABILITY)); + + if (intf_cap.interface_version == TPM_12_TIS_INTF_12 || + intf_cap.interface_version == TPM_12_TIS_INTF_13) + { + tpm_ver = GRUB_TPM_12; + tpm_intf = TPM_INTF_TIS; + return; + } + + if (intf_cap.interface_version != TPM_20_TIS_INTF_13) + return; + + tpm_ver = GRUB_TPM_20; + + intf_id.raw = grub_readl ((void *)(grub_addr_t) (TPM_MMIO_BASE + TPM_INTERFACE_ID)); + + tpm_intf = (intf_id.interface_type == TPM_CRB_INTF_ACTIVE) ? TPM_INTF_CRB : TPM_INTF_TIS; +} + +GRUB_MOD_FINI (tpm) +{ + grub_unregister_command (cmd_tpm_type); +} diff --git a/include/grub/i386/tpm.h b/include/grub/i386/tpm.h new file mode 100644 index 000000000..ae8d4a27d --- /dev/null +++ b/include/grub/i386/tpm.h @@ -0,0 +1,36 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2020 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + * + */ + +#ifndef GRUB_I386_TPM_H +#define GRUB_I386_TPM_H 1 + +#include + +typedef enum + { + GRUB_TPM_NONE = 0, + GRUB_TPM_12, + GRUB_TPM_20 + } +grub_tpm_ver_t; + +extern grub_tpm_ver_t grub_get_tpm_ver (void); +extern void grub_tpm_relinquish_lcl (grub_uint8_t lcl); + +#endif /* GRUB_I386_TPM_H */ -- 2.11.0