Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp80230ybj; Mon, 4 May 2020 16:29:31 -0700 (PDT) X-Google-Smtp-Source: APiQypKsaD8QhgC8asZd7UGjN0XhTfwshY7cQJuMfM/aUePB9z4A5nFc1lTP9A5GIe6bso/lDddZ X-Received: by 2002:a05:6402:88a:: with SMTP id e10mr395126edy.252.1588634971730; Mon, 04 May 2020 16:29:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588634971; cv=none; d=google.com; s=arc-20160816; b=b4bI7jLoxqxe4LXpihscEqtcWcQgq/HpODyCQznHMWIwuoxD0i9wy9c4JezpWa32JM X/nZID+DGbuhOXz99B3lE8It+O3jzA1PUtDumchukGh+ufufeRZhhTc/XZkfAfJwTMXX h74aK6EAwH0cj9ORg2g3Ip/NNL7wOuJHOjV7ffu7cYZ75VPR2Q06p5vcLma13mLWin3p fDmBmKgpj84ltiR3POIIONzkIKd38em84UzXKElnMWjQHKKs8FDXWXdD2eywiIsR6w+0 KUhpsc9jdMbVhKm8mXGjIINzJZ5lDDVUmMxjXwYuc6QQegqNuGOEKs1l1HYZTAMBxnid ZmUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=U9MsE8YG2XA+6eWHIBqjagX72ut5XbC/DYIUDNw4JjM=; b=SWF5dIE72lwmqSHNNcwFBf7231dmqJxqVgQiSqgHmHxlPeTXtDFd3U9GNMVLxBCSfZ jmUkIrz/lkeqMksoC98k1cQe9VUMef+xdovfMEHTPx6v0cYU5Fpmz/PR3gTLFXHuzvF0 z25yX0+gE65aSVQLbhzxZStnTzVx+SIGKcQAY6eCqQu/HXNw8Qmqb1a2QDYdAK2EMlUz zlGZdilra3reO76zY4Qsig5lxm9Ag/RDF4PlQj9epWHzLctNznWfT4LUfXd9FWJQYLJw d07RUJdLCTnOEJRuF2l+ySqY9y5PG6cEuETtJWL5cLMbip8V9gNTAsiRlgAJNXxbfSbf CGiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=fRKv7lRi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c4si34403ejm.465.2020.05.04.16.29.08; Mon, 04 May 2020 16:29:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=fRKv7lRi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728535AbgEDXY6 (ORCPT + 99 others); Mon, 4 May 2020 19:24:58 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:40192 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727982AbgEDXY5 (ORCPT ); Mon, 4 May 2020 19:24:57 -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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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