Vadim Bendebury | 5679752 | 2015-05-20 10:32:25 -0700 | [diff] [blame] | 1 | // This file was extracted from the TCG Published |
| 2 | // Trusted Platform Module Library |
| 3 | // Part 4: Supporting Routines |
| 4 | // Family "2.0" |
| 5 | // Level 00 Revision 01.16 |
| 6 | // October 30, 2014 |
| 7 | |
| 8 | #include <time.h> |
| 9 | #include "PlatformData.h" |
| 10 | #include "Platform.h" |
| 11 | // |
| 12 | // |
| 13 | // Functions |
| 14 | // |
| 15 | // _plat__ClockReset() |
| 16 | // |
| 17 | // Set the current clock time as initial time. This function is called at a power on event to reset the clock |
| 18 | // |
| 19 | LIB_EXPORT void |
| 20 | _plat__ClockReset( |
| 21 | void |
| 22 | ) |
| 23 | { |
| 24 | // Implementation specific: Microsoft C set CLOCKS_PER_SEC to be 1/1000, |
| 25 | // so here the measurement of clock() is in millisecond. |
| 26 | s_initClock = clock(); |
| 27 | s_adjustRate = CLOCK_NOMINAL; |
| 28 | return; |
| 29 | } |
| 30 | // |
| 31 | // |
| 32 | // _plat__ClockTimeFromStart() |
| 33 | // |
| 34 | // Function returns the compensated time from the start of the command when |
| 35 | // _plat__ClockTimeFromStart() was called. |
| 36 | // |
| 37 | unsigned long long |
| 38 | _plat__ClockTimeFromStart( |
| 39 | void |
| 40 | ) |
| 41 | { |
| 42 | unsigned long long currentClock = clock(); |
| 43 | return ((currentClock - s_initClock) * CLOCK_NOMINAL) / s_adjustRate; |
| 44 | } |
| 45 | // |
| 46 | // |
| 47 | // _plat__ClockTimeElapsed() |
| 48 | // |
| 49 | // Get the time elapsed from current to the last time the _plat__ClockTimeElapsed() is called. For the first |
| 50 | // _plat__ClockTimeElapsed() call after a power on event, this call report the elapsed time from power on to |
| 51 | // the current call |
| 52 | // |
| 53 | LIB_EXPORT unsigned long long |
| 54 | _plat__ClockTimeElapsed( |
| 55 | void |
| 56 | // |
| 57 | ) |
| 58 | { |
| 59 | unsigned long long elapsed; |
| 60 | unsigned long long currentClock = clock(); |
| 61 | elapsed = ((currentClock - s_initClock) * CLOCK_NOMINAL) / s_adjustRate; |
| 62 | s_initClock += (elapsed * s_adjustRate) / CLOCK_NOMINAL; |
| 63 | #ifdef DEBUGGING_TIME |
| 64 | // Put this in so that TPM time will pass much faster than real time when |
| 65 | // doing debug. |
| 66 | // A value of 1000 for DEBUG_TIME_MULTIPLER will make each ms into a second |
| 67 | // A good value might be 100 |
| 68 | elapsed *= DEBUG_TIME_MULTIPLIER |
| 69 | #endif |
| 70 | return elapsed; |
| 71 | } |
| 72 | // |
| 73 | // |
| 74 | // _plat__ClockAdjustRate() |
| 75 | // |
| 76 | // Adjust the clock rate |
| 77 | // |
| 78 | LIB_EXPORT void |
| 79 | _plat__ClockAdjustRate( |
| 80 | int adjust // IN: the adjust number. It could be positive |
| 81 | // or negative |
| 82 | ) |
| 83 | { |
| 84 | // We expect the caller should only use a fixed set of constant values to |
| 85 | // adjust the rate |
| 86 | switch(adjust) |
| 87 | { |
| 88 | case CLOCK_ADJUST_COARSE: |
| 89 | s_adjustRate += CLOCK_ADJUST_COARSE; |
| 90 | break; |
| 91 | case -CLOCK_ADJUST_COARSE: |
| 92 | s_adjustRate -= CLOCK_ADJUST_COARSE; |
| 93 | break; |
| 94 | case CLOCK_ADJUST_MEDIUM: |
| 95 | s_adjustRate += CLOCK_ADJUST_MEDIUM; |
| 96 | break; |
| 97 | case -CLOCK_ADJUST_MEDIUM: |
| 98 | s_adjustRate -= CLOCK_ADJUST_MEDIUM; |
| 99 | break; |
| 100 | case CLOCK_ADJUST_FINE: |
| 101 | s_adjustRate += CLOCK_ADJUST_FINE; |
| 102 | break; |
| 103 | case -CLOCK_ADJUST_FINE: |
| 104 | s_adjustRate -= CLOCK_ADJUST_FINE; |
| 105 | break; |
| 106 | default: |
| 107 | // ignore any other values; |
| 108 | break; |
| 109 | } |
| 110 | if(s_adjustRate > (CLOCK_NOMINAL + CLOCK_ADJUST_LIMIT)) |
| 111 | s_adjustRate = CLOCK_NOMINAL + CLOCK_ADJUST_LIMIT; |
| 112 | if(s_adjustRate < (CLOCK_NOMINAL - CLOCK_ADJUST_LIMIT)) |
| 113 | s_adjustRate = CLOCK_NOMINAL-CLOCK_ADJUST_LIMIT; |
| 114 | return; |
| 115 | } |