Skip to content

Commit c48833c

Browse files
stefanbergerfrozencemetery
authored andcommitted
ibmvtpm: Add support for trusted boot using a vTPM 2.0
Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275 PowerPC platform. With this patch grub now measures text and binary data into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform does. This patch requires Daniel Axtens's patches for claiming more memory. Note: The tpm_init() function cannot be called from GRUB_MOD_INIT() since it does not find the device nodes upon module initialization and therefore the call to tpm_init() must be deferred to grub_tpm_measure(). For vTPM support to work on PowerVM, system driver levels 1010.30 or 1020.00 are required. Note: Previous versions of firmware levels with the 2hash-ext-log API call have a bug that, once this API call is invoked, has the effect of disabling the vTPM driver under Linux causing an error message to be displayed in the Linux kernel log. Those users will have to update their machines to the firmware levels mentioned above. Cc: Eric Snowberg <[email protected]> Signed-off-by: Stefan Berger <[email protected]> Signed-off-by: Daniel Axtens <[email protected]> Reviewed-by: Daniel Kiper <[email protected]> (cherry picked from commit 2aa5ef83743dfea79377309ff4f5e9c9a55de355)
1 parent e6cd87b commit c48833c

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

docs/grub.texi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6221,7 +6221,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built
62216221
into @file{core.img} in order to avoid a potential gap in measurement between
62226222
@file{core.img} being loaded and the tpm module being loaded.
62236223

6224-
Measured boot is currently only supported on EFI platforms.
6224+
Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC
6225+
platforms.
62256226

62266227
@node Lockdown
62276228
@section Lockdown when booting on a secure setup

grub-core/Makefile.core.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,13 @@ module = {
11721172
enable = powerpc_ieee1275;
11731173
};
11741174

1175+
module = {
1176+
name = tpm;
1177+
common = commands/tpm.c;
1178+
ieee1275 = commands/ieee1275/ibmvtpm.c;
1179+
enable = powerpc_ieee1275;
1180+
};
1181+
11751182
module = {
11761183
name = terminal;
11771184
common = commands/terminal.c;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* GRUB -- GRand Unified Bootloader
3+
* Copyright (C) 2022 Free Software Foundation, Inc.
4+
* Copyright (C) 2022 IBM Corporation
5+
*
6+
* GRUB is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GRUB is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* IBM vTPM support code.
20+
*/
21+
22+
#include <grub/err.h>
23+
#include <grub/types.h>
24+
#include <grub/tpm.h>
25+
#include <grub/ieee1275/ieee1275.h>
26+
#include <grub/mm.h>
27+
#include <grub/misc.h>
28+
29+
static grub_ieee1275_ihandle_t tpm_ihandle;
30+
static grub_uint8_t tpm_version;
31+
32+
#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t) 0)
33+
34+
static void
35+
tpm_get_tpm_version (void)
36+
{
37+
grub_ieee1275_phandle_t vtpm;
38+
char buffer[20];
39+
40+
if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) &&
41+
!grub_ieee1275_get_property (vtpm, "compatible", buffer,
42+
sizeof (buffer), NULL) &&
43+
!grub_strcmp (buffer, "IBM,vtpm20"))
44+
tpm_version = 2;
45+
}
46+
47+
static grub_err_t
48+
tpm_init (void)
49+
{
50+
static int init_success = 0;
51+
52+
if (!init_success)
53+
{
54+
if (grub_ieee1275_open ("/vdevice/vtpm", &tpm_ihandle) < 0)
55+
{
56+
tpm_ihandle = IEEE1275_IHANDLE_INVALID;
57+
return GRUB_ERR_UNKNOWN_DEVICE;
58+
}
59+
60+
init_success = 1;
61+
62+
tpm_get_tpm_version ();
63+
}
64+
65+
return GRUB_ERR_NONE;
66+
}
67+
68+
static int
69+
ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex,
70+
grub_uint32_t eventtype,
71+
const char *description,
72+
grub_size_t description_size,
73+
void *buf, grub_size_t size)
74+
{
75+
struct tpm_2hash_ext_log
76+
{
77+
struct grub_ieee1275_common_hdr common;
78+
grub_ieee1275_cell_t method;
79+
grub_ieee1275_cell_t ihandle;
80+
grub_ieee1275_cell_t size;
81+
grub_ieee1275_cell_t buf;
82+
grub_ieee1275_cell_t description_size;
83+
grub_ieee1275_cell_t description;
84+
grub_ieee1275_cell_t eventtype;
85+
grub_ieee1275_cell_t pcrindex;
86+
grub_ieee1275_cell_t catch_result;
87+
grub_ieee1275_cell_t rc;
88+
};
89+
struct tpm_2hash_ext_log args;
90+
91+
INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2);
92+
args.method = (grub_ieee1275_cell_t) "2hash-ext-log";
93+
args.ihandle = tpm_ihandle;
94+
args.pcrindex = pcrindex;
95+
args.eventtype = eventtype;
96+
args.description = (grub_ieee1275_cell_t) description;
97+
args.description_size = description_size;
98+
args.buf = (grub_ieee1275_cell_t) buf;
99+
args.size = (grub_ieee1275_cell_t) size;
100+
101+
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
102+
return -1;
103+
104+
/*
105+
* catch_result is set if firmware does not support 2hash-ext-log
106+
* rc is GRUB_IEEE1275_CELL_FALSE (0) on failure
107+
*/
108+
if ((args.catch_result) || args.rc == GRUB_IEEE1275_CELL_FALSE)
109+
return -1;
110+
111+
return 0;
112+
}
113+
114+
static grub_err_t
115+
tpm2_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
116+
const char *description)
117+
{
118+
static int error_displayed = 0;
119+
int rc;
120+
121+
rc = ibmvtpm_2hash_ext_log (pcr, EV_IPL,
122+
description, grub_strlen(description) + 1,
123+
buf, size);
124+
if (rc && !error_displayed)
125+
{
126+
error_displayed++;
127+
return grub_error (GRUB_ERR_BAD_DEVICE,
128+
"2HASH-EXT-LOG failed: Firmware is likely too old.\n");
129+
}
130+
131+
return GRUB_ERR_NONE;
132+
}
133+
134+
grub_err_t
135+
grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
136+
const char *description)
137+
{
138+
/*
139+
* Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes
140+
* can be found.
141+
*/
142+
grub_err_t err = tpm_init ();
143+
144+
/* Absence of a TPM isn't a failure. */
145+
if (err != GRUB_ERR_NONE)
146+
return GRUB_ERR_NONE;
147+
148+
grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
149+
pcr, size, description);
150+
151+
if (tpm_version == 2)
152+
return tpm2_log_event (buf, size, pcr, description);
153+
154+
return GRUB_ERR_NONE;
155+
}

include/grub/ieee1275/ieee1275.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <grub/types.h>
2525
#include <grub/machine/ieee1275.h>
2626

27+
#define GRUB_IEEE1275_CELL_FALSE ((grub_ieee1275_cell_t) 0)
28+
#define GRUB_IEEE1275_CELL_TRUE ((grub_ieee1275_cell_t) -1)
29+
2730
struct grub_ieee1275_mem_region
2831
{
2932
unsigned int start;

0 commit comments

Comments
 (0)