1/******************************************************************************
2 * Copyright (c) 2015-2020 IBM Corporation
3 * All rights reserved.
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
8 *
9 * Contributors:
10 *     IBM Corporation - initial implementation
11 *****************************************************************************/
12/*
13 * libtpm bindings for SLOF - implementation
14 */
15
16#include <tcgbios.h>
17#include <stdbool.h>
18
19/************************************************/
20/* Startup TPM code                             */
21/* SLOF:   tpm-start  ( -- errcode )            */
22/* LIBTPM: tpm_start(void)                      */
23/************************************************/
24PRIM(tpm_X2d_start)
25	PUSH;
26	TOS.n = tpm_start();
27MIRP
28
29/************************************************/
30/* Shutdown TPM layer before OS takes over      */
31/* SLOF:   tpm-finalize  ( -- )                 */
32/* LIBTPM: tpm_finalize(void)                   */
33/************************************************/
34PRIM(tpm_X2d_finalize)
35	tpm_finalize();
36MIRP
37
38/***************************************************************/
39/* Prepare TPM state for bootloader                            */
40/* SLOF:   tpm-leave-firwmare  ( -- errcode )                  */
41/* LIBTPM: tpm_leave_firmware(void)                            */
42/***************************************************************/
43PRIM(tpm_X2d_leave_X2d_firmware)
44	PUSH;
45	TOS.n = tpm_leave_firmware();
46MIRP
47
48/*************************************************************/
49/* Convey log address and size                               */
50/* SLOF:   tpm-set-log-parameters  ( addr size -- )          */
51/* LIBTPM: tpm_set_log_parameters(void *addr, uint64_t size) */
52/*************************************************************/
53PRIM(tpm_X2d_set_X2d_log_X2d_parameters)
54	int size = TOS.u; POP;
55	void *addr = TOS.a; POP;
56	tpm_set_log_parameters(addr, size);
57MIRP
58
59/*********************************************************/
60/* Firmware API                                          */
61/* SLOF:   tpm-driver-get_failure-reason ( -- errcode)   */
62/* LIBTPM: errcode = tpm_driver_get_failure_reason(void) */
63/*********************************************************/
64PRIM(tpm_X2d_driver_X2d_get_X2d_failure_X2d_reason)
65	PUSH;
66	TOS.n = tpm_driver_get_failure_reason();
67MIRP
68
69/********************************************************/
70/* Firmware API                                         */
71/* SLOF:   tpm-driver-set-failure_reason ( errcode -- ) */
72/* LIBTPM: tpm_driver_set_failure_reason(errcode)       */
73/********************************************************/
74PRIM(tpm_X2d_driver_X2d_set_X2d_failure_X2d_reason)
75	int errcode = TOS.u; POP;
76	tpm_driver_set_failure_reason(errcode);
77MIRP
78
79/************************************************/
80/* Get the size of the log                      */
81/* SLOF:   tpm-get-logsize         ( -- size )  */
82/* LIBTPM: logsize = tpm_get_logsize(void)      */
83/************************************************/
84PRIM(tpm_X2d_get_X2d_logsize)
85	PUSH;
86	TOS.n = tpm_get_logsize();
87MIRP
88
89/**********************************************************************/
90/* Measure and log event separators                                   */
91/* SLOF:   tpm-add-event-separators  ( start-pcr end-pcr -- errcode)  */
92/* LIBTPM: errcode = tpm_add_event_separators(start_pcr, end_pcr)     */
93/**********************************************************************/
94PRIM(tpm_X2d_add_X2d_event_X2d_separators)
95	int end_pcr = TOS.u; POP;
96	int start_pcr = TOS.u;
97	TOS.n = tpm_add_event_separators(start_pcr, end_pcr);
98MIRP
99
100/*************************************************************************/
101/* Measure and log boot connect vector (bcv) device's master boot record */
102/* SLOF:   tpm-measure-bcv-mbr  ( bootdrv addr length -- errcode )       */
103/* LIBTPM: errcode = tpm_measure_bcv_mbr(bbotdrv, addr, length)          */
104/*************************************************************************/
105PRIM(tpm_X2d_measure_X2d_bcv_X2d_mbr)
106	int length = TOS.u; POP;
107	void *addr = TOS.a; POP;
108	int bootdrv = TOS.u;
109	TOS.n = tpm_measure_bcv_mbr(bootdrv, addr, length);
110MIRP
111
112/************************************************/
113/* Check whether the TPM is working             */
114/* SLOF:   tpm-is-working  ( -- true | false )  */
115/* LIBTPM: bool = tpm_is_working()              */
116/************************************************/
117PRIM(tpm_X2d_is_X2d_working)
118	PUSH;
119	TOS.n = tpm_is_working();
120MIRP
121
122/************************************************/
123/* Have the S-CRTM measured                     */
124/* SLOF:   tpm-measure-scrtm  ( -- errcode )    */
125/* LIBTPM: errcode = tpm_measure_scrtm          */
126/************************************************/
127PRIM(tpm_X2d_measure_X2d_scrtm)
128	PUSH;
129	TOS.n = tpm_measure_scrtm();
130MIRP
131
132/*******************************************************************/
133/* Firmware API                                                    */
134/* SLOF:   tpm20-menu ( -- tpm-version )                           */
135/* LIBTPM: tpm20_menu()                                            */
136/*******************************************************************/
137PRIM(tpm20_X2d_menu)
138	tpm20_menu();
139MIRP
140
141/*************************************************************************/
142/* Set the LBA1 of the GPT                                               */
143/* SLOF:   tpm-gpt-set-lba1 ( addr length -- )                       */
144/* LIBTPM: tpm_gpt_set_lba1(addr, length)                            */
145/*************************************************************************/
146PRIM(tpm_X2d_gpt_X2d_set_X2d_lba1)
147	int length = TOS.u; POP;
148	void *addr = TOS.a; POP;
149	tpm_gpt_set_lba1(addr, length);
150MIRP
151
152/*************************************************************************/
153/* Add a GPT table entry                                                 */
154/* SLOF:   tpm-gpt-add-entry  ( addr length -- )                         */
155/* LIBTPM: tpm_gpt_add_entry(addr, length)                               */
156/*************************************************************************/
157PRIM(tpm_X2d_gpt_X2d_add_X2d_entry)
158	int length = TOS.u; POP;
159	void *addr = TOS.a; POP;
160	tpm_gpt_add_entry(addr, length);
161MIRP
162
163/*************************************************************************/
164/* Measure and log GPT EVENT                                             */
165/* SLOF:   tpm-measure-gpt  ( -- errcode )                               */
166/* LIBTPM: errcode = tpm_measure_gpt()                                   */
167/*************************************************************************/
168PRIM(tpm_X2d_measure_X2d_gpt)
169	PUSH;
170	TOS.n = tpm_measure_gpt();
171MIRP
172
173/***********************************************************************************************************/
174/* Firmware API                                                                                            */
175/* SLOF:   tpm-hash-log-extend-event-buffer ( pcr evt data-ptr data-len desc-ptr desclen is_elf -- errcode ) */
176/* LIBTPM: errcode = tpm-hash-log-extend-event-buffer                                                        */
177/***********************************************************************************************************/
178PRIM(tpm_X2d_hash_X2d_log_X2d_extend_X2d_event_X2d_buffer)
179	uint32_t is_elf  = TOS.u; POP;
180	uint32_t desclen = TOS.u; POP;
181	const char *desc = TOS.a; POP;
182	uint64_t datalen = TOS.u; POP;
183	const void *data = TOS.a; POP;
184	uint32_t eventtype = TOS.u; POP;
185	uint32_t pcrindex = TOS.u;
186
187	TOS.n = tpm_hash_log_extend_event_buffer(pcrindex, eventtype,
188					         data, datalen,
189					         desc, desclen, is_elf);
190MIRP
191