blob: 306e90329b085a8d56a3296a65499fb38c4b69d2 [file] [log] [blame]
Randall Spanglera3eac792013-01-23 13:04:05 -08001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Gaurav Shahce0cc302010-03-24 13:48:55 -07002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Functions for querying, manipulating and locking rollback indices
6 * stored in the TPM NVRAM.
7 */
8
Bill Richardson0c3ba242013-03-29 11:09:30 -07009#include "sysincludes.h"
10
Bill Richardsonfeac0772012-05-16 10:04:38 -070011#include "crc8.h"
Gaurav Shahce0cc302010-03-24 13:48:55 -070012#include "rollback_index.h"
Gaurav Shahce0cc302010-03-24 13:48:55 -070013#include "tlcl.h"
Gaurav Shah887e3d42010-04-27 16:26:48 -070014#include "tss_constants.h"
Randall Spanglerf3029052010-06-16 13:42:58 -070015#include "utility.h"
Randall Spanglere49e8af2011-07-08 13:03:32 -070016#include "vboot_api.h"
Gaurav Shahce0cc302010-03-24 13:48:55 -070017
Bill Richardsonfeac0772012-05-16 10:04:38 -070018#ifndef offsetof
19#define offsetof(A,B) __builtin_offsetof(A,B)
20#endif
21
Aaron Durbin612797e2013-02-26 09:40:55 -060022/*
23 * Provide protoypes for functions not in the header file. These prototypes
24 * fix -Wmissing-prototypes warnings.
25 */
26uint32_t ReadSpaceFirmware(RollbackSpaceFirmware *rsf);
27uint32_t WriteSpaceFirmware(RollbackSpaceFirmware *rsf);
28uint32_t ReadSpaceKernel(RollbackSpaceKernel *rsk);
29uint32_t WriteSpaceKernel(RollbackSpaceKernel *rsk);
30
Randall Spangler17f8d342013-01-11 10:55:11 -080031#ifdef FOR_TEST
Randall Spangler7993f252013-01-29 15:01:12 -080032/*
33 * Compiling for unit test, so we need the real implementations of
Randall Spangler46d37cf2011-08-29 12:37:15 -070034 * rollback functions. The unit test mocks the underlying tlcl
Randall Spangler7993f252013-01-29 15:01:12 -080035 * functions, so this is ok to run on the host.
36 */
Randall Spangler46d37cf2011-08-29 12:37:15 -070037#undef CHROMEOS_ENVIRONMENT
38#undef DISABLE_ROLLBACK_TPM
39#endif
40
Randall Spangler7993f252013-01-29 15:01:12 -080041#define RETURN_ON_FAILURE(tpm_command) do { \
Aaron Durbinab63d3c2013-02-26 09:51:58 -060042 uint32_t result_; \
43 if ((result_ = (tpm_command)) != TPM_SUCCESS) { \
Randall Spangler7993f252013-01-29 15:01:12 -080044 VBDEBUG(("Rollback: %08x returned by " #tpm_command \
Aaron Durbinab63d3c2013-02-26 09:51:58 -060045 "\n", (int)result_)); \
46 return result_; \
Randall Spangler7993f252013-01-29 15:01:12 -080047 } \
48 } while (0)
Luigi Semenzato59204c52010-06-09 13:37:15 -070049
Randall Spangler4abede32010-08-12 16:40:32 -070050
Randall Spanglera3eac792013-01-23 13:04:05 -080051uint32_t TPMClearAndReenable(void)
52{
53 VBDEBUG(("TPM: Clear and re-enable\n"));
54 RETURN_ON_FAILURE(TlclForceClear());
55 RETURN_ON_FAILURE(TlclSetEnable());
56 RETURN_ON_FAILURE(TlclSetDeactivated(0));
Randall Spanglerada3fa92010-07-20 15:35:49 -070057
Randall Spanglera3eac792013-01-23 13:04:05 -080058 return TPM_SUCCESS;
Luigi Semenzato361049c2010-06-22 13:37:53 -070059}
60
Randall Spanglera3eac792013-01-23 13:04:05 -080061uint32_t SafeWrite(uint32_t index, const void *data, uint32_t length)
62{
63 uint32_t result = TlclWrite(index, data, length);
64 if (result == TPM_E_MAXNVWRITES) {
65 RETURN_ON_FAILURE(TPMClearAndReenable());
66 return TlclWrite(index, data, length);
67 } else {
68 return result;
69 }
Luigi Semenzato361049c2010-06-22 13:37:53 -070070}
71
Randall Spanglera3eac792013-01-23 13:04:05 -080072uint32_t SafeDefineSpace(uint32_t index, uint32_t perm, uint32_t size)
73{
74 uint32_t result = TlclDefineSpace(index, perm, size);
75 if (result == TPM_E_MAXNVWRITES) {
76 RETURN_ON_FAILURE(TPMClearAndReenable());
77 return TlclDefineSpace(index, perm, size);
78 } else {
79 return result;
80 }
Luigi Semenzato8510d912010-07-08 15:40:30 -070081}
82
Randall Spangler4abede32010-08-12 16:40:32 -070083/* Functions to read and write firmware and kernel spaces. */
Randall Spanglera3eac792013-01-23 13:04:05 -080084uint32_t ReadSpaceFirmware(RollbackSpaceFirmware *rsf)
85{
86 uint32_t r;
87 int attempts = 3;
Bill Richardsonfeac0772012-05-16 10:04:38 -070088
Randall Spanglera3eac792013-01-23 13:04:05 -080089 while (attempts--) {
90 r = TlclRead(FIRMWARE_NV_INDEX, rsf,
91 sizeof(RollbackSpaceFirmware));
92 if (r != TPM_SUCCESS)
93 return r;
Bill Richardsonfeac0772012-05-16 10:04:38 -070094
Randall Spanglera3eac792013-01-23 13:04:05 -080095 /*
96 * No CRC in this version, so we'll create one when we write
97 * it. Note that we're marking this as version 2, not
98 * ROLLBACK_SPACE_FIRMWARE_VERSION, because version 2 just
99 * added the CRC. Later versions will need to set default
100 * values for any extra fields explicitly (probably here).
101 */
102 if (rsf->struct_version < 2) {
103 /* Danger Will Robinson! Danger! */
104 rsf->struct_version = 2;
105 return TPM_SUCCESS;
106 }
Bill Richardsonfeac0772012-05-16 10:04:38 -0700107
Randall Spanglera3eac792013-01-23 13:04:05 -0800108 /*
109 * If the CRC is good, we're done. If it's bad, try a couple
110 * more times to see if it gets better before we give up. It
111 * could just be noise.
112 */
113 if (rsf->crc8 == Crc8(rsf,
114 offsetof(RollbackSpaceFirmware, crc8)))
115 return TPM_SUCCESS;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700116
Randall Spanglera3eac792013-01-23 13:04:05 -0800117 VBDEBUG(("TPM: %s() - bad CRC\n", __func__));
118 }
Bill Richardsonfeac0772012-05-16 10:04:38 -0700119
Randall Spanglera3eac792013-01-23 13:04:05 -0800120 VBDEBUG(("TPM: %s() - too many bad CRCs, giving up\n", __func__));
121 return TPM_E_CORRUPTED_STATE;
Luigi Semenzato4f11c362010-06-10 11:01:04 -0700122}
123
Randall Spanglera3eac792013-01-23 13:04:05 -0800124uint32_t WriteSpaceFirmware(RollbackSpaceFirmware *rsf)
125{
126 RollbackSpaceFirmware rsf2;
127 uint32_t r;
128 int attempts = 3;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700129
Randall Spanglera3eac792013-01-23 13:04:05 -0800130 /* All writes should use struct_version 2 or greater. */
131 if (rsf->struct_version < 2)
132 rsf->struct_version = 2;
133 rsf->crc8 = Crc8(rsf, offsetof(RollbackSpaceFirmware, crc8));
Bill Richardsonfeac0772012-05-16 10:04:38 -0700134
Randall Spanglera3eac792013-01-23 13:04:05 -0800135 while (attempts--) {
136 r = SafeWrite(FIRMWARE_NV_INDEX, rsf,
137 sizeof(RollbackSpaceFirmware));
138 /* Can't write, not gonna try again */
139 if (r != TPM_SUCCESS)
140 return r;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700141
Randall Spanglera3eac792013-01-23 13:04:05 -0800142 /* Read it back to be sure it got the right values. */
143 r = ReadSpaceFirmware(&rsf2); /* This checks the CRC */
144 if (r == TPM_SUCCESS)
145 return r;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700146
Randall Spanglera3eac792013-01-23 13:04:05 -0800147 VBDEBUG(("TPM: %s() - bad CRC\n", __func__));
148 /* Try writing it again. Maybe it was garbled on the way out. */
149 }
Bill Richardsonfeac0772012-05-16 10:04:38 -0700150
Randall Spanglera3eac792013-01-23 13:04:05 -0800151 VBDEBUG(("TPM: %s() - too many bad CRCs, giving up\n", __func__));
152 return TPM_E_CORRUPTED_STATE;
Luigi Semenzato4f11c362010-06-10 11:01:04 -0700153}
154
Randall Spanglera3eac792013-01-23 13:04:05 -0800155uint32_t SetVirtualDevMode(int val)
156{
157 RollbackSpaceFirmware rsf;
Bill Richardsonec8df162012-06-07 04:21:14 -0700158
Randall Spanglera3eac792013-01-23 13:04:05 -0800159 VBDEBUG(("TPM: Entering %s()\n", __func__));
160 if (TPM_SUCCESS != ReadSpaceFirmware(&rsf))
161 return VBERROR_TPM_FIRMWARE_SETUP;
Bill Richardsonec8df162012-06-07 04:21:14 -0700162
Randall Spanglera3eac792013-01-23 13:04:05 -0800163 VBDEBUG(("TPM: flags were 0x%02x\n", rsf.flags));
164 if (val)
165 rsf.flags |= FLAG_VIRTUAL_DEV_MODE_ON;
166 else
167 rsf.flags &= ~FLAG_VIRTUAL_DEV_MODE_ON;
168 /*
169 * NOTE: This doesn't update the FLAG_LAST_BOOT_DEVELOPER bit. That
170 * will be done by SetupTPM() on the next boot.
171 */
172 VBDEBUG(("TPM: flags are now 0x%02x\n", rsf.flags));
Bill Richardsonec8df162012-06-07 04:21:14 -0700173
Randall Spanglera3eac792013-01-23 13:04:05 -0800174 if (TPM_SUCCESS != WriteSpaceFirmware(&rsf))
175 return VBERROR_TPM_SET_BOOT_MODE_STATE;
Bill Richardsonec8df162012-06-07 04:21:14 -0700176
Randall Spanglera3eac792013-01-23 13:04:05 -0800177 VBDEBUG(("TPM: Leaving %s()\n", __func__));
178 return VBERROR_SUCCESS;
Bill Richardsonec8df162012-06-07 04:21:14 -0700179}
180
Randall Spanglera3eac792013-01-23 13:04:05 -0800181uint32_t ReadSpaceKernel(RollbackSpaceKernel *rsk)
182{
183 uint32_t r;
184 int attempts = 3;
Randall Spangler4abede32010-08-12 16:40:32 -0700185
Randall Spanglera3eac792013-01-23 13:04:05 -0800186 while (attempts--) {
187 r = TlclRead(KERNEL_NV_INDEX, rsk, sizeof(RollbackSpaceKernel));
188 if (r != TPM_SUCCESS)
189 return r;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700190
Randall Spanglera3eac792013-01-23 13:04:05 -0800191 /*
192 * No CRC in this version, so we'll create one when we write
193 * it. Note that we're marking this as version 2, not
194 * ROLLBACK_SPACE_KERNEL_VERSION, because version 2 just added
195 * the CRC. Later versions will need to set default values for
196 * any extra fields explicitly (probably here).
197 */
198 if (rsk->struct_version < 2) {
199 /* Danger Will Robinson! Danger! */
200 rsk->struct_version = 2;
201 return TPM_SUCCESS;
202 }
Bill Richardsonfeac0772012-05-16 10:04:38 -0700203
Randall Spanglera3eac792013-01-23 13:04:05 -0800204 /*
205 * If the CRC is good, we're done. If it's bad, try a couple
206 * more times to see if it gets better before we give up. It
207 * could just be noise.
208 */
209 if (rsk->crc8 == Crc8(rsk, offsetof(RollbackSpaceKernel, crc8)))
210 return TPM_SUCCESS;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700211
Randall Spanglera3eac792013-01-23 13:04:05 -0800212 VBDEBUG(("TPM: %s() - bad CRC\n", __func__));
213 }
Bill Richardsonfeac0772012-05-16 10:04:38 -0700214
Randall Spanglera3eac792013-01-23 13:04:05 -0800215 VBDEBUG(("TPM: %s() - too many bad CRCs, giving up\n", __func__));
216 return TPM_E_CORRUPTED_STATE;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700217}
218
Randall Spanglera3eac792013-01-23 13:04:05 -0800219uint32_t WriteSpaceKernel(RollbackSpaceKernel *rsk)
220{
221 RollbackSpaceKernel rsk2;
222 uint32_t r;
223 int attempts = 3;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700224
Randall Spanglera3eac792013-01-23 13:04:05 -0800225 /* All writes should use struct_version 2 or greater. */
226 if (rsk->struct_version < 2)
227 rsk->struct_version = 2;
228 rsk->crc8 = Crc8(rsk, offsetof(RollbackSpaceKernel, crc8));
Bill Richardsonfeac0772012-05-16 10:04:38 -0700229
Randall Spanglera3eac792013-01-23 13:04:05 -0800230 while (attempts--) {
231 r = SafeWrite(KERNEL_NV_INDEX, rsk,
232 sizeof(RollbackSpaceKernel));
233 /* Can't write, not gonna try again */
234 if (r != TPM_SUCCESS)
235 return r;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700236
Randall Spanglera3eac792013-01-23 13:04:05 -0800237 /* Read it back to be sure it got the right values. */
238 r = ReadSpaceKernel(&rsk2); /* This checks the CRC */
239 if (r == TPM_SUCCESS)
240 return r;
Bill Richardsonfeac0772012-05-16 10:04:38 -0700241
Randall Spanglera3eac792013-01-23 13:04:05 -0800242 VBDEBUG(("TPM: %s() - bad CRC\n", __func__));
243 /* Try writing it again. Maybe it was garbled on the way out. */
244 }
Bill Richardsonfeac0772012-05-16 10:04:38 -0700245
Randall Spanglera3eac792013-01-23 13:04:05 -0800246 VBDEBUG(("TPM: %s() - too many bad CRCs, giving up\n", __func__));
247 return TPM_E_CORRUPTED_STATE;
248}
249
250uint32_t OneTimeInitializeTPM(RollbackSpaceFirmware *rsf,
251 RollbackSpaceKernel *rsk)
252{
253 static const RollbackSpaceFirmware rsf_init = {
254 .struct_version = ROLLBACK_SPACE_FIRMWARE_VERSION,
255 };
256 static const RollbackSpaceKernel rsk_init = {
257 .struct_version = ROLLBACK_SPACE_KERNEL_VERSION,
258 .uid = ROLLBACK_SPACE_KERNEL_UID,
259 };
260 TPM_PERMANENT_FLAGS pflags;
261 uint32_t result;
262
263 VBDEBUG(("TPM: One-time initialization\n"));
264
265 /*
266 * Do a full test. This only happens the first time the device is
267 * turned on in the factory, so performance is not an issue. This is
268 * almost certainly not necessary, but it gives us more confidence
269 * about some code paths below that are difficult to
270 * test---specifically the ones that set lifetime flags, and are only
271 * executed once per physical TPM.
272 */
273 result = TlclSelfTestFull();
274 if (result != TPM_SUCCESS)
275 return result;
276
277 result = TlclGetPermanentFlags(&pflags);
278 if (result != TPM_SUCCESS)
279 return result;
280
281 /*
282 * TPM may come from the factory without physical presence finalized.
283 * Fix if necessary.
284 */
285 VBDEBUG(("TPM: physicalPresenceLifetimeLock=%d\n",
286 pflags.physicalPresenceLifetimeLock));
287 if (!pflags.physicalPresenceLifetimeLock) {
288 VBDEBUG(("TPM: Finalizing physical presence\n"));
289 RETURN_ON_FAILURE(TlclFinalizePhysicalPresence());
290 }
291
292 /*
293 * The TPM will not enforce the NV authorization restrictions until the
294 * execution of a TPM_NV_DefineSpace with the handle of
295 * TPM_NV_INDEX_LOCK. Here we create that space if it doesn't already
296 * exist. */
297 VBDEBUG(("TPM: nvLocked=%d\n", pflags.nvLocked));
298 if (!pflags.nvLocked) {
299 VBDEBUG(("TPM: Enabling NV locking\n"));
300 RETURN_ON_FAILURE(TlclSetNvLocked());
301 }
302
303 /* Clear TPM owner, in case the TPM is already owned for some reason. */
304 VBDEBUG(("TPM: Clearing owner\n"));
305 RETURN_ON_FAILURE(TPMClearAndReenable());
306
307 /* Initializes the firmware and kernel spaces */
308 Memcpy(rsf, &rsf_init, sizeof(RollbackSpaceFirmware));
309 Memcpy(rsk, &rsk_init, sizeof(RollbackSpaceKernel));
310
Bill Richardsonb64f0972014-05-28 15:49:23 -0700311 /* Define the backup space. No need to initialize it, though. */
312 RETURN_ON_FAILURE(SafeDefineSpace(
313 BACKUP_NV_INDEX, TPM_NV_PER_PPWRITE, BACKUP_NV_SIZE));
314
315 /* Define and initialize the kernel space */
Randall Spanglera3eac792013-01-23 13:04:05 -0800316 RETURN_ON_FAILURE(SafeDefineSpace(KERNEL_NV_INDEX, TPM_NV_PER_PPWRITE,
317 sizeof(RollbackSpaceKernel)));
318 RETURN_ON_FAILURE(WriteSpaceKernel(rsk));
Bill Richardsonb64f0972014-05-28 15:49:23 -0700319
320 /* Do the firmware space last, so we retry if we don't get this far. */
Randall Spanglera3eac792013-01-23 13:04:05 -0800321 RETURN_ON_FAILURE(SafeDefineSpace(
322 FIRMWARE_NV_INDEX,
323 TPM_NV_PER_GLOBALLOCK | TPM_NV_PER_PPWRITE,
324 sizeof(RollbackSpaceFirmware)));
325 RETURN_ON_FAILURE(WriteSpaceFirmware(rsf));
Bill Richardsonb64f0972014-05-28 15:49:23 -0700326
Randall Spanglera3eac792013-01-23 13:04:05 -0800327 return TPM_SUCCESS;
Randall Spangler4abede32010-08-12 16:40:32 -0700328}
329
Randall Spanglercb3313e2011-08-26 12:53:16 -0700330
Randall Spanglera3eac792013-01-23 13:04:05 -0800331/*
332 * SetupTPM starts the TPM and establishes the root of trust for the
Luigi Semenzato2b9ddae2010-06-28 13:34:31 -0700333 * anti-rollback mechanism. SetupTPM can fail for three reasons. 1 A bug. 2 a
334 * TPM hardware failure. 3 An unexpected TPM state due to some attack. In
335 * general we cannot easily distinguish the kind of failure, so our strategy is
336 * to reboot in recovery mode in all cases. The recovery mode calls SetupTPM
337 * again, which executes (almost) the same sequence of operations. There is a
338 * good chance that, if recovery mode was entered because of a TPM failure, the
339 * failure will repeat itself. (In general this is impossible to guarantee
340 * because we have no way of creating the exact TPM initial state at the
341 * previous boot.) In recovery mode, we ignore the failure and continue, thus
342 * giving the recovery kernel a chance to fix things (that's why we don't set
343 * bGlobalLock). The choice is between a knowingly insecure device and a
344 * bricked device.
345 *
346 * As a side note, observe that we go through considerable hoops to avoid using
347 * the STCLEAR permissions for the index spaces. We do this to avoid writing
348 * to the TPM flashram at every reboot or wake-up, because of concerns about
349 * the durability of the NVRAM.
350 */
Shawn Nematbakhsh472d93c2013-07-22 16:19:13 -0700351uint32_t SetupTPM(int developer_mode, int disable_dev_request,
352 int clear_tpm_owner_request, RollbackSpaceFirmware* rsf)
Randall Spanglera3eac792013-01-23 13:04:05 -0800353{
354 uint8_t in_flags;
355 uint8_t disable;
356 uint8_t deactivated;
357 uint32_t result;
Gabe Black51d22d52013-03-02 18:19:41 -0800358 uint32_t versions;
Randall Spangler4abede32010-08-12 16:40:32 -0700359
Randall Spanglera3eac792013-01-23 13:04:05 -0800360 RETURN_ON_FAILURE(TlclLibInit());
Randall Spangler3e1081f2010-07-19 10:04:21 -0700361
Luigi Semenzato7b90cf32011-05-23 23:34:17 -0700362#ifdef TEGRA_SOFT_REBOOT_WORKAROUND
Randall Spanglera3eac792013-01-23 13:04:05 -0800363 result = TlclStartup();
364 if (result == TPM_E_INVALID_POSTINIT) {
365 /*
366 * Some prototype hardware doesn't reset the TPM on a CPU
367 * reset. We do a hard reset to get around this.
368 */
369 VBDEBUG(("TPM: soft reset detected\n", result));
370 return TPM_E_MUST_REBOOT;
371 } else if (result != TPM_SUCCESS) {
372 VBDEBUG(("TPM: TlclStartup returned %08x\n", result));
373 return result;
374 }
Luigi Semenzato7b90cf32011-05-23 23:34:17 -0700375#else
Randall Spanglera3eac792013-01-23 13:04:05 -0800376 RETURN_ON_FAILURE(TlclStartup());
Luigi Semenzato7b90cf32011-05-23 23:34:17 -0700377#endif
378
Randall Spanglera3eac792013-01-23 13:04:05 -0800379 /*
380 * Some TPMs start the self test automatically at power on. In that case we
Luigi Semenzato06fbb162011-03-16 17:33:23 -0700381 * don't need to call ContinueSelfTest. On some (other) TPMs,
382 * ContinueSelfTest may block. In that case, we definitely don't want to
383 * call it here. For TPMs in the intersection of these two sets, we're
384 * screwed. (In other words: TPMs that require manually starting the
385 * self-test AND block will have poor performance until we split
386 * TlclSendReceive() into Send() and Receive(), and have a state machine to
387 * control setup.)
388 *
389 * This comment is likely to become obsolete in the near future, so don't
390 * trust it. It may have not been updated.
391 */
392#ifdef TPM_MANUAL_SELFTEST
393#ifdef TPM_BLOCKING_CONTINUESELFTEST
394#warning "lousy TPM!"
395#endif
Randall Spanglera3eac792013-01-23 13:04:05 -0800396 RETURN_ON_FAILURE(TlclContinueSelfTest());
Luigi Semenzato06fbb162011-03-16 17:33:23 -0700397#endif
Randall Spanglera3eac792013-01-23 13:04:05 -0800398 result = TlclAssertPhysicalPresence();
399 if (result != TPM_SUCCESS) {
400 /*
401 * It is possible that the TPM was delivered with the physical
402 * presence command disabled. This tries enabling it, then
403 * tries asserting PP again.
404 */
405 RETURN_ON_FAILURE(TlclPhysicalPresenceCMDEnable());
406 RETURN_ON_FAILURE(TlclAssertPhysicalPresence());
407 }
Randall Spangler4abede32010-08-12 16:40:32 -0700408
Randall Spanglera3eac792013-01-23 13:04:05 -0800409 /* Check that the TPM is enabled and activated. */
410 RETURN_ON_FAILURE(TlclGetFlags(&disable, &deactivated, NULL));
411 if (disable || deactivated) {
412 VBDEBUG(("TPM: disabled (%d) or deactivated (%d). Fixing...\n",
413 disable, deactivated));
414 RETURN_ON_FAILURE(TlclSetEnable());
415 RETURN_ON_FAILURE(TlclSetDeactivated(0));
416 VBDEBUG(("TPM: Must reboot to re-enable\n"));
417 return TPM_E_MUST_REBOOT;
418 }
Randall Spangler4abede32010-08-12 16:40:32 -0700419
Randall Spanglera3eac792013-01-23 13:04:05 -0800420 /* Read the firmware space. */
421 result = ReadSpaceFirmware(rsf);
422 if (TPM_E_BADINDEX == result) {
423 RollbackSpaceKernel rsk;
Randall Spanglerbb5d9f12010-08-16 15:36:07 -0700424
Randall Spanglera3eac792013-01-23 13:04:05 -0800425 /*
426 * This is the first time we've run, and the TPM has not been
427 * initialized. Initialize it.
428 */
429 VBDEBUG(("TPM: Not initialized yet.\n"));
430 RETURN_ON_FAILURE(OneTimeInitializeTPM(rsf, &rsk));
431 } else if (TPM_SUCCESS != result) {
432 VBDEBUG(("TPM: Firmware space in a bad state; giving up.\n"));
433 return TPM_E_CORRUPTED_STATE;
434 }
Gabe Black51d22d52013-03-02 18:19:41 -0800435 Memcpy(&versions, &rsf->fw_versions, sizeof(versions));
Randall Spanglera3eac792013-01-23 13:04:05 -0800436 VBDEBUG(("TPM: Firmware space sv%d f%x v%x\n",
Gabe Black51d22d52013-03-02 18:19:41 -0800437 rsf->struct_version, rsf->flags, versions));
Randall Spanglera3eac792013-01-23 13:04:05 -0800438 in_flags = rsf->flags;
Bill Richardsonec8df162012-06-07 04:21:14 -0700439
Randall Spanglera3eac792013-01-23 13:04:05 -0800440 /* If we've been asked to clear the virtual dev-mode flag, do so now */
441 if (disable_dev_request) {
442 rsf->flags &= ~FLAG_VIRTUAL_DEV_MODE_ON;
443 VBDEBUG(("TPM: Clearing virt dev-switch: f%x\n", rsf->flags));
444 }
Randall Spangler4abede32010-08-12 16:40:32 -0700445
Randall Spanglera3eac792013-01-23 13:04:05 -0800446 /*
447 * The developer_mode value that's passed in is only set by a hardware
448 * dev-switch. We should OR it with the virtual switch, whether or not
449 * the virtual switch is used. If it's not used, it shouldn't change,
450 * so it doesn't matter.
451 */
452 if (rsf->flags & FLAG_VIRTUAL_DEV_MODE_ON)
453 developer_mode = 1;
Bill Richardsonb75d8ad2012-05-17 13:26:05 -0700454
Randall Spanglera3eac792013-01-23 13:04:05 -0800455 /*
456 * Clear ownership if developer flag has toggled, or if an owner-clear
457 * has been requested.
458 */
459 if ((developer_mode ? FLAG_LAST_BOOT_DEVELOPER : 0) !=
460 (in_flags & FLAG_LAST_BOOT_DEVELOPER)) {
461 VBDEBUG(("TPM: Developer flag changed; clearing owner.\n"));
462 RETURN_ON_FAILURE(TPMClearAndReenable());
463 } else if (clear_tpm_owner_request) {
464 VBDEBUG(("TPM: Clearing owner as specifically requested.\n"));
465 RETURN_ON_FAILURE(TPMClearAndReenable());
466 }
Randall Spangler4abede32010-08-12 16:40:32 -0700467
Randall Spanglera3eac792013-01-23 13:04:05 -0800468 if (developer_mode)
469 rsf->flags |= FLAG_LAST_BOOT_DEVELOPER;
470 else
471 rsf->flags &= ~FLAG_LAST_BOOT_DEVELOPER;
Bill Richardsonec8df162012-06-07 04:21:14 -0700472
Randall Spangler4abede32010-08-12 16:40:32 -0700473
Randall Spanglera3eac792013-01-23 13:04:05 -0800474 /* If firmware space is dirty, flush it back to the TPM */
475 if (rsf->flags != in_flags) {
476 VBDEBUG(("TPM: Updating firmware space.\n"));
477 RETURN_ON_FAILURE(WriteSpaceFirmware(rsf));
478 }
Randall Spangler4abede32010-08-12 16:40:32 -0700479
Randall Spanglera3eac792013-01-23 13:04:05 -0800480 VBDEBUG(("TPM: SetupTPM() succeeded\n"));
481 return TPM_SUCCESS;
Gaurav Shahce0cc302010-03-24 13:48:55 -0700482}
483
Randall Spangler39f66112010-07-14 09:10:23 -0700484
485#ifdef DISABLE_ROLLBACK_TPM
Randall Spanglerada3fa92010-07-20 15:35:49 -0700486/* Dummy implementations which don't support TPM rollback protection */
Randall Spangler39f66112010-07-14 09:10:23 -0700487
Randall Spanglera3eac792013-01-23 13:04:05 -0800488uint32_t RollbackS3Resume(void)
489{
Randall Spangler1fe16072010-09-02 11:37:51 -0700490#ifndef CHROMEOS_ENVIRONMENT
Randall Spanglera3eac792013-01-23 13:04:05 -0800491 /*
492 * Initialize the TPM, but ignore return codes. In ChromeOS
493 * environment, don't even talk to the TPM.
494 */
495 TlclLibInit();
496 TlclResume();
Randall Spangler1fe16072010-09-02 11:37:51 -0700497#endif
Randall Spanglera3eac792013-01-23 13:04:05 -0800498 return TPM_SUCCESS;
Randall Spangler1fe16072010-09-02 11:37:51 -0700499}
500
Shawn Nematbakhsh472d93c2013-07-22 16:19:13 -0700501uint32_t RollbackFirmwareSetup(int is_hw_dev,
Bill Richardsonec8df162012-06-07 04:21:14 -0700502 int disable_dev_request,
Randall Spangler29e88072012-06-19 10:03:53 -0700503 int clear_tpm_owner_request,
Randall Spanglera3eac792013-01-23 13:04:05 -0800504 int *is_virt_dev, uint32_t *version)
505{
Randall Spanglerada3fa92010-07-20 15:35:49 -0700506#ifndef CHROMEOS_ENVIRONMENT
Randall Spanglera3eac792013-01-23 13:04:05 -0800507 /*
508 * Initialize the TPM, but ignores return codes. In ChromeOS
509 * environment, don't even talk to the TPM.
510 */
511 TlclLibInit();
512 TlclStartup();
513 TlclContinueSelfTest();
Randall Spanglerada3fa92010-07-20 15:35:49 -0700514#endif
Randall Spanglera3eac792013-01-23 13:04:05 -0800515 *is_virt_dev = 0;
516 *version = 0;
517 return TPM_SUCCESS;
Randall Spangler39f66112010-07-14 09:10:23 -0700518}
519
Randall Spanglera3eac792013-01-23 13:04:05 -0800520uint32_t RollbackFirmwareWrite(uint32_t version)
521{
522 return TPM_SUCCESS;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700523}
524
Randall Spanglera3eac792013-01-23 13:04:05 -0800525uint32_t RollbackFirmwareLock(void)
526{
527 return TPM_SUCCESS;
Randall Spangler39f66112010-07-14 09:10:23 -0700528}
529
Randall Spanglera3eac792013-01-23 13:04:05 -0800530uint32_t RollbackKernelRead(uint32_t* version)
531{
532 *version = 0;
533 return TPM_SUCCESS;
Randall Spangler39f66112010-07-14 09:10:23 -0700534}
535
Randall Spanglera3eac792013-01-23 13:04:05 -0800536uint32_t RollbackKernelWrite(uint32_t version)
537{
538 return TPM_SUCCESS;
Randall Spangler39f66112010-07-14 09:10:23 -0700539}
540
Bill Richardsonb64f0972014-05-28 15:49:23 -0700541uint32_t RollbackBackupRead(uint8_t *raw)
542{
543 return TPM_SUCCESS;
544}
545
546uint32_t RollbackBackupWrite(uint8_t *raw)
547{
548 return TPM_SUCCESS;
549}
550
Shawn Nematbakhsh964144b2013-07-22 13:33:46 -0700551uint32_t RollbackKernelLock(int recovery_mode)
Randall Spanglera3eac792013-01-23 13:04:05 -0800552{
553 return TPM_SUCCESS;
Randall Spangler39f66112010-07-14 09:10:23 -0700554}
555
556#else
Randall Spangler1fe16072010-09-02 11:37:51 -0700557
Randall Spanglera3eac792013-01-23 13:04:05 -0800558uint32_t RollbackS3Resume(void)
559{
560 uint32_t result;
561 RETURN_ON_FAILURE(TlclLibInit());
562 result = TlclResume();
563 if (result == TPM_E_INVALID_POSTINIT) {
564 /*
565 * We're on a platform where the TPM maintains power in S3, so
566 * it's already initialized.
567 */
568 return TPM_SUCCESS;
569 }
570 return result;
Randall Spangler1fe16072010-09-02 11:37:51 -0700571}
572
Shawn Nematbakhsh472d93c2013-07-22 16:19:13 -0700573uint32_t RollbackFirmwareSetup(int is_hw_dev,
Bill Richardsonec8df162012-06-07 04:21:14 -0700574 int disable_dev_request,
Randall Spangler29e88072012-06-19 10:03:53 -0700575 int clear_tpm_owner_request,
Randall Spanglera3eac792013-01-23 13:04:05 -0800576 int *is_virt_dev, uint32_t *version)
577{
578 RollbackSpaceFirmware rsf;
Randall Spangler10788382010-06-23 15:35:31 -0700579
Randall Spanglera3eac792013-01-23 13:04:05 -0800580 /* Set version to 0 in case we fail */
581 *version = 0;
Randall Spangler22e7bb22011-07-22 14:06:51 -0700582
Shawn Nematbakhsh472d93c2013-07-22 16:19:13 -0700583 RETURN_ON_FAILURE(SetupTPM(is_hw_dev, disable_dev_request,
Randall Spanglera3eac792013-01-23 13:04:05 -0800584 clear_tpm_owner_request, &rsf));
Gabe Black51d22d52013-03-02 18:19:41 -0800585 Memcpy(version, &rsf.fw_versions, sizeof(*version));
Randall Spanglera3eac792013-01-23 13:04:05 -0800586 *is_virt_dev = (rsf.flags & FLAG_VIRTUAL_DEV_MODE_ON) ? 1 : 0;
Gabe Black51d22d52013-03-02 18:19:41 -0800587 VBDEBUG(("TPM: RollbackFirmwareSetup %x\n", (int)*version));
Randall Spanglera3eac792013-01-23 13:04:05 -0800588 return TPM_SUCCESS;
Randall Spangler10788382010-06-23 15:35:31 -0700589}
590
Randall Spanglera3eac792013-01-23 13:04:05 -0800591uint32_t RollbackFirmwareWrite(uint32_t version)
592{
593 RollbackSpaceFirmware rsf;
Gabe Black51d22d52013-03-02 18:19:41 -0800594 uint32_t old_version;
Randall Spangler4abede32010-08-12 16:40:32 -0700595
Randall Spanglera3eac792013-01-23 13:04:05 -0800596 RETURN_ON_FAILURE(ReadSpaceFirmware(&rsf));
Gabe Black51d22d52013-03-02 18:19:41 -0800597 Memcpy(&old_version, &rsf.fw_versions, sizeof(old_version));
598 VBDEBUG(("TPM: RollbackFirmwareWrite %x --> %x\n", (int)old_version,
Randall Spanglera3eac792013-01-23 13:04:05 -0800599 (int)version));
Gabe Black51d22d52013-03-02 18:19:41 -0800600 Memcpy(&rsf.fw_versions, &version, sizeof(version));
Randall Spanglera3eac792013-01-23 13:04:05 -0800601 return WriteSpaceFirmware(&rsf);
Randall Spangler10788382010-06-23 15:35:31 -0700602}
603
Randall Spanglera3eac792013-01-23 13:04:05 -0800604uint32_t RollbackFirmwareLock(void)
605{
606 return TlclSetGlobalLock();
Randall Spangler10788382010-06-23 15:35:31 -0700607}
608
Randall Spanglera3eac792013-01-23 13:04:05 -0800609uint32_t RollbackKernelRead(uint32_t* version)
610{
611 RollbackSpaceKernel rsk;
Gabe Black522da3e2013-03-02 21:00:03 -0800612 uint32_t perms, uid;
Randall Spanglerbb5d9f12010-08-16 15:36:07 -0700613
Randall Spanglera3eac792013-01-23 13:04:05 -0800614 /*
615 * Read the kernel space and verify its permissions. If the kernel
616 * space has the wrong permission, or it doesn't contain the right
617 * identifier, we give up. This will need to be fixed by the
618 * recovery kernel. We have to worry about this because at any time
619 * (even with PP turned off) the TPM owner can remove and redefine a
620 * PP-protected space (but not write to it).
621 */
622 RETURN_ON_FAILURE(ReadSpaceKernel(&rsk));
623 RETURN_ON_FAILURE(TlclGetPermissions(KERNEL_NV_INDEX, &perms));
Gabe Black522da3e2013-03-02 21:00:03 -0800624 Memcpy(&uid, &rsk.uid, sizeof(uid));
625 if (TPM_NV_PER_PPWRITE != perms || ROLLBACK_SPACE_KERNEL_UID != uid)
Randall Spanglera3eac792013-01-23 13:04:05 -0800626 return TPM_E_CORRUPTED_STATE;
Randall Spanglerbb5d9f12010-08-16 15:36:07 -0700627
Gabe Black522da3e2013-03-02 21:00:03 -0800628 Memcpy(version, &rsk.kernel_versions, sizeof(*version));
629 VBDEBUG(("TPM: RollbackKernelRead %x\n", (int)*version));
Randall Spanglera3eac792013-01-23 13:04:05 -0800630 return TPM_SUCCESS;
Randall Spangler10788382010-06-23 15:35:31 -0700631}
632
Randall Spanglera3eac792013-01-23 13:04:05 -0800633uint32_t RollbackKernelWrite(uint32_t version)
634{
635 RollbackSpaceKernel rsk;
Gabe Black522da3e2013-03-02 21:00:03 -0800636 uint32_t old_version;
Randall Spanglera3eac792013-01-23 13:04:05 -0800637 RETURN_ON_FAILURE(ReadSpaceKernel(&rsk));
Gabe Black522da3e2013-03-02 21:00:03 -0800638 Memcpy(&old_version, &rsk.kernel_versions, sizeof(old_version));
Randall Spanglera3eac792013-01-23 13:04:05 -0800639 VBDEBUG(("TPM: RollbackKernelWrite %x --> %x\n",
Gabe Black522da3e2013-03-02 21:00:03 -0800640 (int)old_version, (int)version));
641 Memcpy(&rsk.kernel_versions, &version, sizeof(version));
Randall Spanglera3eac792013-01-23 13:04:05 -0800642 return WriteSpaceKernel(&rsk);
Randall Spangler10788382010-06-23 15:35:31 -0700643}
644
Bill Richardsonb64f0972014-05-28 15:49:23 -0700645/*
646 * We don't really care whether the TPM owner has been messing with this or
647 * not. We lock it along with the Kernel space just to avoid problems, but it's
648 * only useful in dev-mode and only when the battery has been drained
649 * completely. There aren't any security issues. It's just in the TPM because
650 * we don't have any other place to keep it.
651 */
652uint32_t RollbackBackupRead(uint8_t *raw)
653{
654 uint32_t r;
655 r = TlclRead(BACKUP_NV_INDEX, raw, BACKUP_NV_SIZE);
656 VBDEBUG(("TPM: %s returning 0x%x\n", __func__, r));
657 return r;
658}
659
660uint32_t RollbackBackupWrite(uint8_t *raw)
661{
662 uint32_t r;
663 r = TlclWrite(BACKUP_NV_INDEX, raw, BACKUP_NV_SIZE);
664 VBDEBUG(("TPM: %s returning 0x%x\n", __func__, r));
665 return r;
666}
667
Shawn Nematbakhsh964144b2013-07-22 13:33:46 -0700668uint32_t RollbackKernelLock(int recovery_mode)
Randall Spanglera3eac792013-01-23 13:04:05 -0800669{
Shawn Nematbakhsh964144b2013-07-22 13:33:46 -0700670 if (recovery_mode)
Randall Spanglera3eac792013-01-23 13:04:05 -0800671 return TPM_SUCCESS;
672 else
673 return TlclLockPhysicalPresence();
Randall Spangler10788382010-06-23 15:35:31 -0700674}
Randall Spangler39f66112010-07-14 09:10:23 -0700675
Randall Spanglera3eac792013-01-23 13:04:05 -0800676#endif /* DISABLE_ROLLBACK_TPM */