168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * Use of this source code is governed by a BSD-style license that can be
3ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * found in the LICENSE file.
4ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah *
5ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * Functions for querying, manipulating and locking rollback indices
6ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * stored in the TPM NVRAM.
7ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */
8ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
9ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#ifndef VBOOT_REFERENCE_ROLLBACK_INDEX_H_
10ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define VBOOT_REFERENCE_ROLLBACK_INDEX_H_
11ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
12f302905224a346718910e56f5f1593d4b19253f1Randall Spangler#include "sysincludes.h"
137a786b73e769ba9be6823adc4980d7c9a992f55eRandall Spangler#include "tss_constants.h"
14ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
15ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* TPM NVRAM location indices. */
164abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler#define FIRMWARE_NV_INDEX               0x1007
174abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler#define KERNEL_NV_INDEX                 0x1008
18b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson/* This is just an opaque space for backup purposes */
19b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson#define BACKUP_NV_INDEX                 0x1009
20b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson#define BACKUP_NV_SIZE 16
21b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson
224abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler
234abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler/* Structure definitions for TPM spaces */
244abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler
254abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler/* Kernel space - KERNEL_NV_INDEX, locked with physical presence. */
26feac077c1d96d81f9c1c0b5253d0223b0a2d9448Bill Richardson#define ROLLBACK_SPACE_KERNEL_VERSION 2
274abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler#define ROLLBACK_SPACE_KERNEL_UID 0x4752574C  /* 'GRWL' */
2868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler
294abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spanglertypedef struct RollbackSpaceKernel {
3068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Struct version, for backwards compatibility */
3168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t struct_version;
3268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Unique ID to detect space redefinition */
3368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint32_t uid;
3468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Kernel versions */
3568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint32_t kernel_versions;
3668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Reserved for future expansion */
3768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t reserved[3];
3868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Checksum (v2 and later only) */
3968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t crc8;
404abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler} __attribute__((packed)) RollbackSpaceKernel;
414abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler
424abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler/* Flags for firmware space */
4368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/*
4468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Last boot was developer mode.  TPM ownership is cleared when transitioning
4568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * to/from developer mode.
4668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
474abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler#define FLAG_LAST_BOOT_DEVELOPER 0x01
4868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/*
4968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Some systems may not have a dedicated dev-mode switch, but enter and leave
5068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * dev-mode through some recovery-mode magic keypresses. For those systems, the
5168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * dev-mode "switch" state is in this bit (0=normal, 1=dev). To make it work, a
5268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * new flag is passed to VbInit(), indicating that the system lacks a physical
5368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * dev-mode switch. If a physical switch is present, this bit is ignored.
5468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
55b75d8adcc01f08cf5a6d87b78aeb1d7cdfcd22afBill Richardson#define FLAG_VIRTUAL_DEV_MODE_ON 0x02
564abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler
574abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler/* Firmware space - FIRMWARE_NV_INDEX, locked with global lock. */
5868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler#define ROLLBACK_SPACE_FIRMWARE_VERSION 2
5968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler
604abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spanglertypedef struct RollbackSpaceFirmware {
6168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Struct version, for backwards compatibility */
6268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t struct_version;
6368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Flags (see FLAG_* above) */
6468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t flags;
6568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Firmware versions */
6668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint32_t fw_versions;
6768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Reserved for future expansion */
6868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t reserved[3];
6968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	/* Checksum (v2 and later only) */
7068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler	uint8_t crc8;
714abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler} __attribute__((packed)) RollbackSpaceFirmware;
724abede35afc8b5ecc8165d5d79f77c203bce51fcRandall Spangler
73b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson
742666f10dec63de910696bfb84de2ea5d474b75e6Luigi Semenzato/* All functions return TPM_SUCCESS (zero) if successful, non-zero if error */
752666f10dec63de910696bfb84de2ea5d474b75e6Luigi Semenzato
7668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/*
7768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * These functions are called from VbInit().  They cannot use global
7868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * variables.
7968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
8068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler
811fe1607679a17a37ab6be390e2f04155e5c37e8eRandall Spangleruint32_t RollbackS3Resume(void);
821fe1607679a17a37ab6be390e2f04155e5c37e8eRandall Spangler
8368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/*
8468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * These functions are callable from VbSelectFirmware().  They cannot use
8568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * global variables.
8668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
872b9ddae52ba564dddcfc8bdcbed04dc07a52a7c6Luigi Semenzato
8868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
8968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * This must be called.
9068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
91472d93c146403c0f95b656e182b972e15ff11835Shawn Nematbakhshuint32_t RollbackFirmwareSetup(int is_hw_dev,
92ec8df1628cd9cf236bf912dee7d4365d7977e697Bill Richardson                               int disable_dev_request,
9329e8807ea045e119e3adeaec40c5f8421901b6fbRandall Spangler                               int clear_tpm_owner_request,
94ec8df1628cd9cf236bf912dee7d4365d7977e697Bill Richardson                               /* two outputs on success */
95ec8df1628cd9cf236bf912dee7d4365d7977e697Bill Richardson                               int *is_virt_dev, uint32_t *tpm_version);
965ac39bfff0d9e2ad2c3e1fe9b3fd3f314b50a472Randall Spangler
9768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
9868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Write may be called if the versions change.
9968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
100666802822b771c05e435973b767e494a4fff2747Randall Spangleruint32_t RollbackFirmwareWrite(uint32_t version);
1012b9ddae52ba564dddcfc8bdcbed04dc07a52a7c6Luigi Semenzato
10268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
10368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Lock must be called.
10468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
1051078838fff3554c0c4bcae771984cb2164bd7359Randall Spangleruint32_t RollbackFirmwareLock(void);
1061078838fff3554c0c4bcae771984cb2164bd7359Randall Spangler
10768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/*
10868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * These functions are callable from VbSelectAndLoadKernel().  They may use
10968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * global variables.
11068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
11168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler
11268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
11368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Read stored kernel version.
11468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
11568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangleruint32_t RollbackKernelRead(uint32_t *version);
1162b9ddae52ba564dddcfc8bdcbed04dc07a52a7c6Luigi Semenzato
11768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
11868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Write stored kernel version.
11968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
120666802822b771c05e435973b767e494a4fff2747Randall Spangleruint32_t RollbackKernelWrite(uint32_t version);
1212b9ddae52ba564dddcfc8bdcbed04dc07a52a7c6Luigi Semenzato
12268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
123b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson * Read backup data.
124b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson */
125b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardsonuint32_t RollbackBackupRead(uint8_t *raw);
126b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson
127b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson/**
128b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson * Write backup data.
129b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson */
130b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardsonuint32_t RollbackBackupWrite(uint8_t *raw);
131b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson
132b64f097891e697eaf3b2794baae934f8b4d82d14Bill Richardson/**
13368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Lock must be called.  Internally, it's ignored in recovery mode.
13468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
135964144bf2f3befe8c8a010000439cb5e5dccf00dShawn Nematbakhshuint32_t RollbackKernelLock(int recovery_mode);
1361078838fff3554c0c4bcae771984cb2164bd7359Randall Spangler
137cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler/****************************************************************************/
138416f681882d8a35fa4c7ad9245a9e544c3115670Luigi Semenzato
13968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/*
14068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * The following functions are internal apis, listed here for use by unit tests
14168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * only.
14268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
14368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler
14468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
14568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Issue a TPM_Clear and reenable/reactivate the TPM.
14668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
147416f681882d8a35fa4c7ad9245a9e544c3115670Luigi Semenzatouint32_t TPMClearAndReenable(void);
148416f681882d8a35fa4c7ad9245a9e544c3115670Luigi Semenzato
14968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
15068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Like TlclWrite(), but checks for write errors due to hitting the 64-write
151cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler * limit and clears the TPM when that happens.  This can only happen when the
152cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler * TPM is unowned, so it is OK to clear it (and we really have no choice).
15368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * This is not expected to happen frequently, but it could happen.
15468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
15568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangleruint32_t SafeWrite(uint32_t index, const void *data, uint32_t length);
156cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler
15768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
15868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Similarly to SafeWrite(), this ensures we don't fail a DefineSpace because
159cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler * we hit the TPM write limit.  This is even less likely to happen than with
160cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler * writes because we only define spaces once at initialization, but we'd rather
16168f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * be paranoid about this.
16268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
163cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangleruint32_t SafeDefineSpace(uint32_t index, uint32_t perm, uint32_t size);
164cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler
16568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
16668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Perform one-time initializations.
16768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler *
16868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Create the NVRAM spaces, and set their initial values as needed.  Sets the
16968f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * nvLocked bit and ensures the physical presence command is enabled and
17068f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * locked.
171cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler */
17268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangleruint32_t OneTimeInitializeTPM(RollbackSpaceFirmware *rsf,
17368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler                              RollbackSpaceKernel *rsk);
174cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler
17568f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
17668f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Start the TPM and establish the root of trust for the anti-rollback
17768f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * mechanism.
17868f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
179472d93c146403c0f95b656e182b972e15ff11835Shawn Nematbakhshuint32_t SetupTPM(int developer_mode, int disable_dev_request,
180472d93c146403c0f95b656e182b972e15ff11835Shawn Nematbakhsh                  int clear_tpm_owner_request, RollbackSpaceFirmware *rsf);
181ec8df1628cd9cf236bf912dee7d4365d7977e697Bill Richardson
18268f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler/**
18368f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler * Utility function to turn the virtual dev-mode flag on or off. 0=off, 1=on.
18468f54d44756e8f0a777808b710a4ccc5d2ce353dRandall Spangler */
185ec8df1628cd9cf236bf912dee7d4365d7977e697Bill Richardsonuint32_t SetVirtualDevMode(int val);
186cb3313e8cb6a95e5ad02860222fed18db82b37afRandall Spangler
187ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#endif  /* VBOOT_REFERENCE_ROLLBACK_INDEX_H_ */
188