| 1 |
/* Microsoft Reference Implementation for TPM 2.0 |
| 2 |
* |
| 3 |
* The copyright in this software is being made available under the BSD |
| 4 |
* License, included below. This software may be subject to other third party |
| 5 |
* and contributor rights, including patent rights, and no such rights are |
| 6 |
* granted under this license. |
| 7 |
* |
| 8 |
* Copyright (c) Microsoft Corporation |
| 9 |
* |
| 10 |
* All rights reserved. |
| 11 |
* |
| 12 |
* BSD License |
| 13 |
* |
| 14 |
* Redistribution and use in source and binary forms, with or without |
| 15 |
* modification, are permitted provided that the following conditions are met: |
| 16 |
* |
| 17 |
* Redistributions of source code must retain the above copyright notice, this |
| 18 |
* list of conditions and the following disclaimer. |
| 19 |
* |
| 20 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 21 |
* this list of conditions and the following disclaimer in the documentation |
| 22 |
* and/or other materials provided with the distribution. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS |
| 25 |
* IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 26 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 31 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 32 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 33 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 34 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
*/ |
| 36 |
// Platform functions used by libtpm |
| 37 |
|
| 38 |
#ifndef _PLATFORM_FP_H_ |
| 39 |
#define _PLATFORM_FP_H_ |
| 40 |
|
| 41 |
#include <stdbool.h> |
| 42 |
#include <stdint.h> |
| 43 |
|
| 44 |
//***_plat__IsCanceled() |
| 45 |
// We opt to not support cancellation, so always return false. |
| 46 |
// Return values: |
| 47 |
// true(1) if cancel flag is set |
| 48 |
// false(0) if cancel flag is not set |
| 49 |
static inline int _plat__IsCanceled() { return false; } |
| 50 |
|
| 51 |
//***_plat__TimerReset() |
| 52 |
// This function sets current system clock time as t0 for counting TPM time. |
| 53 |
// This function is called at a power on event to reset the clock. When the |
| 54 |
// clock is reset, the indication that the clock was stopped is also set. |
| 55 |
void _plat__TimerReset(); |
| 56 |
|
| 57 |
//***_plat__TimerRead() |
| 58 |
// This function provides access to the tick timer of the platform. The TPM code |
| 59 |
// uses this value to drive the TPM Clock. |
| 60 |
// |
| 61 |
// The tick timer is supposed to run when power is applied to the device. This |
| 62 |
// timer should not be reset by time events including _TPM_Init. It should only |
| 63 |
// be reset when TPM power is re-applied. |
| 64 |
// |
| 65 |
// If the TPM is run in a protected environment, that environment may provide |
| 66 |
// the tick time to the TPM as long as the time provided by the environment is |
| 67 |
// not allowed to go backwards. If the time provided by the system can go |
| 68 |
// backwards during a power discontinuity, then the _plat__Signal_PowerOn should |
| 69 |
// call _plat__TimerReset(). |
| 70 |
uint64_t _plat__TimerRead(); |
| 71 |
|
| 72 |
//*** _plat__TimerWasReset() |
| 73 |
// This function is used to interrogate the flag indicating if the tick timer |
| 74 |
// has been reset. |
| 75 |
// |
| 76 |
// If the resetFlag parameter is SET, then the flag will be CLEAR before the |
| 77 |
// function returns. |
| 78 |
bool _plat__TimerWasReset(); |
| 79 |
|
| 80 |
//*** _plat__TimerWasStopped() |
| 81 |
// As we have CLOCK_STOPS=NO, we will only stop our timer on resets. |
| 82 |
static inline bool _plat__TimerWasStopped() { return _plat__TimerWasReset(); } |
| 83 |
|
| 84 |
//***_plat__ClockAdjustRate() |
| 85 |
// Adjust the clock rate |
| 86 |
// IN: the adjust number. It could be positive or negative |
| 87 |
void _plat__ClockAdjustRate(int adjust); |
| 88 |
|
| 89 |
//*** _plat__GetEntropy() |
| 90 |
// This function is used to get available hardware entropy. In a hardware |
| 91 |
// implementation of this function, there would be no call to the system |
| 92 |
// to get entropy. |
| 93 |
// Return values: |
| 94 |
// < 0 hardware failure of the entropy generator, this is sticky |
| 95 |
// >= 0 the returned amount of entropy (bytes) |
| 96 |
int32_t _plat__GetEntropy(uint8_t *entropy, // output buffer |
| 97 |
uint32_t amount // amount requested |
| 98 |
); |
| 99 |
|
| 100 |
//***_plat__LocalityGet() |
| 101 |
// We do not support non-zero localities, so just always return 0. |
| 102 |
static inline uint8_t _plat__LocalityGet() { return 0; } |
| 103 |
|
| 104 |
//***_plat__NVEnable() |
| 105 |
// As we just hold the NV data in memory, always return success. |
| 106 |
// Return values: |
| 107 |
// 0 if success |
| 108 |
// > 0 if receive recoverable error |
| 109 |
// < 0 if unrecoverable error |
| 110 |
static inline int _plat__NVEnable(void *platParameter) { |
| 111 |
(void)(platParameter); |
| 112 |
return 0; |
| 113 |
}; |
| 114 |
|
| 115 |
//***_plat__IsNvAvailable() |
| 116 |
// Our NV Data is always available and has no write limits. |
| 117 |
// Return values: |
| 118 |
// 0 NV is available |
| 119 |
// 1 NV is not available due to write failure |
| 120 |
// 2 NV is not available due to rate limit |
| 121 |
static inline int _plat__IsNvAvailable() { return 0; } |
| 122 |
|
| 123 |
//***_plat__NvMemoryRead() |
| 124 |
// Function: Read a chunk of NV memory |
| 125 |
void _plat__NvMemoryRead(unsigned int startOffset, // IN: read start |
| 126 |
unsigned int size, // IN: size of bytes to read |
| 127 |
void *data // OUT: data buffer |
| 128 |
); |
| 129 |
|
| 130 |
//*** _plat__NvIsDifferent() |
| 131 |
// This function checks to see if the NV is different from the test value. This |
| 132 |
// is so that NV will not be written if it has not changed. |
| 133 |
// Return Type: int |
| 134 |
// TRUE(1) the NV location is different from the test value |
| 135 |
// FALSE(0) the NV location is the same as the test value |
| 136 |
int _plat__NvIsDifferent(unsigned int startOffset, // IN: read start |
| 137 |
unsigned int size, // IN: size of bytes to read |
| 138 |
void *data // IN: data buffer |
| 139 |
); |
| 140 |
|
| 141 |
//***_plat__NvMemoryWrite() |
| 142 |
// This function is used to update NV memory. The "write" is to a memory copy of |
| 143 |
// NV. At the end of the current command, any changes are written to |
| 144 |
// the actual NV memory. |
| 145 |
// NOTE: A useful optimization would be for this code to compare the current |
| 146 |
// contents of NV with the local copy and note the blocks that have changed. |
| 147 |
// Then only write those blocks when _plat__NvCommit() is called. |
| 148 |
bool _plat__NvMemoryWrite(unsigned int startOffset, // IN: write start |
| 149 |
unsigned int size, // IN: size of bytes to write |
| 150 |
void *data // OUT: data buffer |
| 151 |
); |
| 152 |
|
| 153 |
//***_plat__NvMemoryClear() |
| 154 |
// Function is used to set a range of NV memory bytes to an implementation- |
| 155 |
// dependent value. The value represents the erase state of the memory. |
| 156 |
void _plat__NvMemoryClear(unsigned int start, // IN: clear start |
| 157 |
unsigned int size // IN: number of bytes to clear |
| 158 |
); |
| 159 |
|
| 160 |
//***_plat__NvMemoryMove() |
| 161 |
// Function: Move a chunk of NV memory from source to destination |
| 162 |
// This function should ensure that if there overlap, the original data is |
| 163 |
// copied before it is written |
| 164 |
void _plat__NvMemoryMove(unsigned int sourceOffset, // IN: source offset |
| 165 |
unsigned int destOffset, // IN: destination offset |
| 166 |
unsigned int size // IN: size of data being moved |
| 167 |
); |
| 168 |
|
| 169 |
//***_plat__NvCommit() |
| 170 |
// Our NV Data is just in memory, so "committing" it is a no-op. |
| 171 |
// Return values: |
| 172 |
// 0 NV write success |
| 173 |
// != 0 NV write fail |
| 174 |
static inline int _plat__NvCommit() { return 0; } |
| 175 |
|
| 176 |
//*** _plat__WasPowerLost() |
| 177 |
// Test whether power was lost before a _TPM_Init. As we use in-memory NV Data, |
| 178 |
// there's no reason to to not do the power-loss activities on every _TPM_Init. |
| 179 |
// Return values: |
| 180 |
// true(1) power was lost |
| 181 |
// false(0) power was not lost |
| 182 |
static inline int _plat__WasPowerLost() { return true; } |
| 183 |
|
| 184 |
//** From PPPlat.c |
| 185 |
|
| 186 |
//***_plat__PhysicalPresenceAsserted() |
| 187 |
// Our vTPM has no way to assert physical presence, so we always return true. |
| 188 |
// Return values: |
| 189 |
// true(1) if physical presence is signaled |
| 190 |
// false(0) if physical presence is not signaled |
| 191 |
static inline int _plat__PhysicalPresenceAsserted() { return true; } |
| 192 |
|
| 193 |
//***_plat__Fail() |
| 194 |
// This is the platform depended failure exit for the TPM. |
| 195 |
_Noreturn void _plat__Fail(); |
| 196 |
|
| 197 |
#endif // _PLATFORM_FP_H_ |