• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / libtpms / #2022

23 Sep 2025 01:11PM UTC coverage: 77.227% (+0.009%) from 77.218%
#2022

push

travis-ci

web-flow
Merge a45c293de into 4504f47c6

36116 of 46766 relevant lines covered (77.23%)

125180.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.29
/src/tpm2/PlatformACT.c
1
/********************************************************************************/
2
/*                                                                                */
3
/*                 Platform Authenticated Countdown Timer                                  */
4
/*                             Written by Ken Goldman                                */
5
/*                       IBM Thomas J. Watson Research Center                        */
6
/*            $Id: PlatformACT.c 1594 2020-03-26 22:15:48Z kgoldman $                */
7
/*                                                                                */
8
/*  Licenses and Notices                                                        */
9
/*                                                                                */
10
/*  1. Copyright Licenses:                                                        */
11
/*                                                                                */
12
/*  - Trusted Computing Group (TCG) grants to the user of the source code in        */
13
/*    this specification (the "Source Code") a worldwide, irrevocable,                 */
14
/*    nonexclusive, royalty free, copyright license to reproduce, create         */
15
/*    derivative works, distribute, display and perform the Source Code and        */
16
/*    derivative works thereof, and to grant others the rights granted herein.        */
17
/*                                                                                */
18
/*  - The TCG grants to the user of the other parts of the specification         */
19
/*    (other than the Source Code) the rights to reproduce, distribute,         */
20
/*    display, and perform the specification solely for the purpose of                 */
21
/*    developing products based on such documents.                                */
22
/*                                                                                */
23
/*  2. Source Code Distribution Conditions:                                        */
24
/*                                                                                */
25
/*  - Redistributions of Source Code must retain the above copyright licenses,         */
26
/*    this list of conditions and the following disclaimers.                        */
27
/*                                                                                */
28
/*  - Redistributions in binary form must reproduce the above copyright         */
29
/*    licenses, this list of conditions        and the following disclaimers in the         */
30
/*    documentation and/or other materials provided with the distribution.        */
31
/*                                                                                */
32
/*  3. Disclaimers:                                                                */
33
/*                                                                                */
34
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF        */
35
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH        */
36
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)        */
37
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.                */
38
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for                 */
39
/*  information on specification licensing rights available through TCG         */
40
/*  membership agreements.                                                        */
41
/*                                                                                */
42
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED         */
43
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR         */
44
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR                 */
45
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY                 */
46
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.                */
47
/*                                                                                */
48
/*  - Without limitation, TCG and its members and licensors disclaim all         */
49
/*    liability, including liability for infringement of any proprietary         */
50
/*    rights, relating to use of information in this specification and to the        */
51
/*    implementation of this specification, and TCG disclaims all liability for        */
52
/*    cost of procurement of substitute goods or services, lost profits, loss         */
53
/*    of use, loss of data or any incidental, consequential, direct, indirect,         */
54
/*    or special damages, whether under contract, tort, warranty or otherwise,         */
55
/*    arising in any way out of use or reliance upon this specification or any         */
56
/*    information herein.                                                        */
57
/*                                                                                */
58
/*  (c) Copyright IBM Corp. and others, 2019 - 2020                                */
59
/*                                                                                */
60
/********************************************************************************/
61

62
//** Includes
63
#include "Platform.h"
64

65
//** Functions
66

67
#if ACT_SUPPORT
68

69
//*** ActSignal()
70
// Function called when there is an ACT event to signal or unsignal
71
#ifndef __ACT_DISABLED        // libtpms added
72
static void ActSignal(P_ACT_DATA actData, int on)
73
{
74
    if(actData == NULL)
75
        return;
76
    // If this is to turn a signal on, don't do anything if it is already on. If this
77
    // is to turn the signal off, do it anyway because this might be for
78
    // initialization.
79
    if(on && (actData->signaled == TRUE))
80
        return;
81
    actData->signaled = (uint8_t)on;
82

83
    // If there is an action, then replace the "Do something" with the correct action.
84
    // It should test 'on' to see if it is turning the signal on or off.
85
    switch(actData->number)
86
    {
87
#  if RH_ACT_0
88
        case 0:  // Do something
89
            return;
90
#  endif
91
#  if RH_ACT_1
92
        case 1:  // Do something
93
            return;
94
#  endif
95
#  if RH_ACT_2
96
        case 2:  // Do something
97
            return;
98
#  endif
99
#  if RH_ACT_3
100
        case 3:  // Do something
101
            return;
102
#  endif
103
#  if RH_ACT_4
104
        case 4:  // Do something
105
            return;
106
#  endif
107
#  if RH_ACT_5
108
        case 5:  // Do something
109
            return;
110
#  endif
111
#  if RH_ACT_6
112
        case 6:  // Do something
113
            return;
114
#  endif
115
#  if RH_ACT_7
116
        case 7:  // Do something
117
            return;
118
#  endif
119
#  if RH_ACT_8
120
        case 8:  // Do something
121
            return;
122
#  endif
123
#  if RH_ACT_9
124
        case 9:  // Do something
125
            return;
126
#  endif
127
#  if RH_ACT_A
128
        case 0xA:  // Do something
129
            return;
130
#  endif
131
#  if RH_ACT_B
132
        case 0xB:
133
            // Do something
134
            return;
135
#  endif
136
#  if RH_ACT_C
137
        case 0xC:  // Do something
138
            return;
139
#  endif
140
#  if RH_ACT_D
141
        case 0xD:  // Do something
142
            return;
143
#  endif
144
#  if RH_ACT_E
145
        case 0xE:  // Do something
146
            return;
147
#  endif
148
#  if RH_ACT_F
149
        case 0xF:  // Do something
150
            return;
151
#  endif
152
        default:
153
            return;
154
    }
155
}
156
#endif                // libtpms added
157

158
//*** ActGetDataPointer()
159
static P_ACT_DATA ActGetDataPointer(uint32_t act)
160
{
161

162
#  define RETURN_ACT_POINTER(N) \
163
      if(0x##N == act)          \
164
          return &ACT_##N;
165

166
    FOR_EACH_ACT(RETURN_ACT_POINTER)
167

168
    return (P_ACT_DATA)NULL;
169
}
170

171
//*** _plat__ACT_GetImplemented()
172
// This function tests to see if an ACT is implemented. It is a belt and suspenders
173
// function because the TPM should not be calling to manipulate an ACT that is not
174
// implemented. However, this could help the simulator code which doesn't necessarily
175
// know if an ACT is implemented or not.
176
LIB_EXPORT int _plat__ACT_GetImplemented(uint32_t act)
×
177
{
178
    return (ActGetDataPointer(act) != NULL);
×
179
}
180

181
//*** _plat__ACT_GetRemaining()
182
// This function returns the remaining time. If an update is pending, 'newValue' is
183
// returned. Otherwise, the current counter value is returned. Note that since the
184
// timers keep running, the returned value can get stale immediately. The actual count
185
// value will be no greater than the returned value.
186
LIB_EXPORT uint32_t _plat__ACT_GetRemaining(uint32_t act  //IN: the ACT selector
×
187
)
188
{
189
    P_ACT_DATA actData = ActGetDataPointer(act);
×
190
    uint32_t   remain;
×
191
    //
192
    if(actData == NULL)
×
193
        return 0;
×
194
    remain = actData->remaining;
195
    if(actData->pending)
196
        remain = actData->newValue;
197
    return remain;
198
}
199

200
//*** _plat__ACT_GetSignaled()
201
LIB_EXPORT int _plat__ACT_GetSignaled(uint32_t act  //IN: number of ACT to check
×
202
)
203
{
204
    P_ACT_DATA actData = ActGetDataPointer(act);
×
205
    //
206
    if(actData == NULL)
×
207
        return 0;
×
208
    return (int)actData->signaled;
209
}
210

211
#ifndef __ACT_DISABLED        // libtpms added
212
//*** _plat__ACT_SetSignaled()
213
LIB_EXPORT void _plat__ACT_SetSignaled(uint32_t act, int on)
214
{
215
    ActSignal(ActGetDataPointer(act), on);
216
}
217

218
//*** _plat__ACT_GetPending()
219
LIB_EXPORT int _plat__ACT_GetPending(uint32_t act  //IN: number of ACT to check
220
)
221
{
222
    P_ACT_DATA actData = ActGetDataPointer(act);
223
    //
224
    if(actData == NULL)
225
        return 0;
226
    return (int)actData->pending;
227
}
228

229
//*** _plat__ACT_UpdateCounter()
230
// This function is used to write the newValue for the counter. If an update is
231
// pending, then no update occurs and the function returns FALSE. If 'setSignaled'
232
// is TRUE, then the ACT signaled state is SET and if 'newValue' is 0, nothing
233
// is posted.
234
LIB_EXPORT int _plat__ACT_UpdateCounter(uint32_t act,      // IN: ACT to update
235
                                        uint32_t newValue  // IN: the value to post
236
)
237
{
238
    P_ACT_DATA actData = ActGetDataPointer(act);
239
    //
240
    if(actData == NULL)
241
        // actData doesn't exist but pretend update is pending rather than indicate
242
        // that a retry is necessary.
243
        return TRUE;
244
    // if an update is pending then return FALSE so that there will be a retry
245
    if(actData->pending != 0)
246
        return FALSE;
247
    actData->newValue = newValue;
248
    actData->pending  = TRUE;
249

250
    return TRUE;
251
}
252
#endif                // libtpms added
253

254
//***_plat__ACT_EnableTicks()
255
// This enables and disables the processing of the once-per-second ticks. This should
256
// be turned off ('enable' = FALSE) by _TPM_Init and turned on ('enable' = TRUE) by
257
// TPM2_Startup() after all the initializations have completed.
258
LIB_EXPORT void _plat__ACT_EnableTicks(int enable)
20,954✔
259
{
260
    actTicksAllowed = enable;
20,954✔
261
}
20,954✔
262

263
#ifndef __ACT_DISABLED        // libtpms added
264
//*** ActDecrement()
265
// If 'newValue' is non-zero it is copied to 'remaining' and then 'newValue' is
266
// set to zero. Then 'remaining' is decremented by one if it is not already zero. If
267
// the value is decremented to zero, then the associated event is signaled. If setting
268
// 'remaining' causes it to be greater than 1, then the signal associated with the ACT
269
// is turned off.
270
static void ActDecrement(P_ACT_DATA actData)
271
{
272
    // Check to see if there is an update pending
273
    if(actData->pending)
274
    {
275
        // If this update will cause the count to go from non-zero to zero, set
276
        // the newValue to 1 so that it will timeout when decremented below.
277
        if((actData->newValue == 0) && (actData->remaining != 0))
278
            actData->newValue = 1;
279
        actData->remaining = actData->newValue;
280

281
        // Update processed
282
        actData->pending = 0;
283
    }
284
    // no update so countdown if the count is non-zero but not max
285
    if((actData->remaining != 0) && (actData->remaining != UINT32_MAX))
286
    {
287
        // If this countdown causes the count to go to zero, then turn the signal for
288
        // the ACT on.
289
        if((actData->remaining -= 1) == 0)
290
            ActSignal(actData, TRUE);
291
    }
292
    // If the current value of the counter is non-zero, then the signal should be
293
    // off.
294
    if(actData->signaled && (actData->remaining > 0))
295
        ActSignal(actData, FALSE);
296
}
297

298
//*** _plat__ACT_Tick()
299
// This processes the once-per-second clock tick from the hardware. This is set up
300
// for the simulator to use the control interface to send ticks to the TPM. These
301
// ticks do not have to be on a per second basis. They can be as slow or as fast as
302
// desired so that the simulation can be tested.
303
LIB_EXPORT void _plat__ACT_Tick(void)
304
{
305
    // Ticks processing is turned off at certain times just to make sure that nothing
306
    // strange is happening before pointers and things are
307
    if(actTicksAllowed)
308
    {
309
        // Handle the update for each counter.
310
#  define DECREMENT_COUNT(N) ActDecrement(&ACT_##N);
311

312
        FOR_EACH_ACT(DECREMENT_COUNT)
313
    }
314
}
315

316
//*** ActZero()
317
// This function initializes a single ACT
318
static void ActZero(uint32_t act, P_ACT_DATA actData)
319
{
320
    actData->remaining = 0;
321
    actData->newValue  = 0;
322
    actData->pending   = 0;
323
    actData->number    = (uint8_t)act;
324
    ActSignal(actData, FALSE);
325
}
326
#endif                        // libtpms added
327

328
//***_plat__ACT_Initialize()
329
// This function initializes the ACT hardware and data structures
330
LIB_EXPORT int _plat__ACT_Initialize(void)
4,251✔
331
{
332
    actTicksAllowed = 0;
4,251✔
333
#  define ZERO_ACT(N) ActZero(0x##N, &ACT_##N);
334
    FOR_EACH_ACT(ZERO_ACT)
335

336
    return TRUE;
4,251✔
337
}
338

339
#endif  // ACT_SUPPORT
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc