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

stefanberger / libtpms / #2047

02 Jan 2026 05:02PM UTC coverage: 77.211% (-0.01%) from 77.225%
#2047

push

travis-ci

web-flow
Merge af9de2478 into 03ff2481e

1 of 1 new or added line in 1 file covered. (100.0%)

523 existing lines in 42 files now uncovered.

36060 of 46703 relevant lines covered (77.21%)

101760.53 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
/* C.16        PlatformACT.c */
62
/* C.16.1.        Includes */
63
#include "Platform.h"
64
#include "PlatformACT_fp.h"
65
/* C.16.2.        Functions */
66
/* C.16.2.1.        ActSignal() */
67
/* Function called when there is an ACT event to signal or unsignal */
68
#ifndef __ACT_DISABLED        // libtpms added
69
static void
70
ActSignal(
71
          P_ACT_DATA          actData,
72
          int                 on
73
          )
74
{
75
    if(actData == NULL)
76
        return;
77
    // If this is to turn a signal on, don't do anything if it is already on. If this
78
    // is to turn the signal off, do it anyway because this might be for
79
    // initialization.
80
    if(on && (actData->signaled == TRUE))
81
        return;
82
    actData->signaled = (uint8_t)on;
83
    
84
    // If there is an action, then replace the "Do something" with the correct action.
85
    // It should test 'on' to see if it is turning the signal on or off.
86
    switch(actData->number)
87
        {
88
#if RH_ACT_0
89
          case 0: // Do something
90
            return;
91
#endif
92
#if RH_ACT_1
93
          case 1: // Do something
94
            return;
95
#endif
96
#if RH_ACT_2
97
          case 2: // Do something
98
            return;
99
#endif
100
#if RH_ACT_3
101
          case 3: // Do something
102
            return;
103
#endif
104
#if RH_ACT_4
105
          case 4: // Do something
106
            return;
107
#endif
108
#if RH_ACT_5
109
          case 5: // Do something
110
            return;
111
#endif
112
#if RH_ACT_6
113
          case 6: // Do something
114
            return;
115
#endif
116
#if RH_ACT_7
117
          case 7: // Do something
118
            return;
119
#endif
120
#if RH_ACT_8
121
          case 8: // Do something
122
            return;
123
#endif
124
#if RH_ACT_9
125
          case 9: // Do something
126
            return;
127
#endif
128
#if RH_ACT_A
129
          case 0xA: // Do something
130
            return;
131
#endif
132
#if RH_ACT_B
133
          case 0xB:
134
            // Do something
135
            return;
136
#endif
137
#if RH_ACT_C
138
          case 0xC: // Do something
139
            return;
140
#endif
141
#if RH_ACT_D
142
          case 0xD: // Do something
143
            return;
144
#endif
145
#if RH_ACT_E
146
          case 0xE: // Do something
147
            return;
148
#endif
149
#if RH_ACT_F
150
          case 0xF: // Do something
151
            return;
152
#endif
153
          default:
154
            return;
155
        }
156
}
157
#endif                // libtpms added
158
/* C.16.2.2.        ActGetDataPointer() */
159
static P_ACT_DATA
160
ActGetDataPointer(
161
                  uint32_t            act __attribute__((unused))
162
                  )
163
{
164
    
165
#define RETURN_ACT_POINTER(N)  if(0x##N == act) return &ACT_##N;
166
    
167
    FOR_EACH_ACT(RETURN_ACT_POINTER)
168
        
169
        return (P_ACT_DATA)NULL;
170
}
171
/* C.16.2.3.        _plat__ACT_GetImplemented() */
172
/* This function tests to see if an ACT is implemented. It is a belt and suspenders function because
173
   the TPM should not be calling to manipulate an ACT that is not implemented. However, this
174
   could help the simulator code which doesn't necessarily know if an ACT is implemented or not. */
175
LIB_EXPORT int
176
_plat__ACT_GetImplemented(
×
177
                          uint32_t            act
178
                          )
179
{
UNCOV
180
    return (ActGetDataPointer(act) != NULL);
×
181
}
182
/* C.16.2.4.        _plat__ACT_GetRemaining() */
183
/* This function returns the remaining time. If an update is pending, newValue is
184
   returned. Otherwise, the current counter value is returned. Note that since the timers keep
185
   running, the returned value can get stale immediately. The actual count value will be no greater
186
   than the returned value. */
187
LIB_EXPORT uint32_t
UNCOV
188
_plat__ACT_GetRemaining(
×
189
                        uint32_t            act             //IN: the ACT selector
190
                        )
191
{
192
    P_ACT_DATA              actData = ActGetDataPointer(act);
×
193
    uint32_t                remain;
×
194
    //
UNCOV
195
    if(actData == NULL)
×
UNCOV
196
        return 0;
×
197
    remain = actData->remaining;
198
    if(actData->pending)
199
        remain = actData->newValue;
200
    return remain;
201
}
202
/* C.16.2.5.        _plat__ACT_GetSignaled() */
203
LIB_EXPORT int
204
_plat__ACT_GetSignaled(
×
205
                       uint32_t            act         //IN: number of ACT to check
206
                       )
207
{
UNCOV
208
    P_ACT_DATA              actData = ActGetDataPointer(act);
×
209
    //
UNCOV
210
    if(actData == NULL)
×
UNCOV
211
        return 0;
×
212
    return (int)actData->signaled;
213
}
214
/* C.16.2.6.        _plat__ACT_SetSignaled() */
215
#ifndef __ACT_DISABLED        // libtpms added
216
LIB_EXPORT void
217
_plat__ACT_SetSignaled(
218
                       uint32_t            act,
219
                       int                 on
220
                       )
221
{
222
    ActSignal(ActGetDataPointer(act), on);
223
}
224
/* C.16.2.7.        _plat__ACT_GetPending() */
225
LIB_EXPORT int
226
_plat__ACT_GetPending(
227
                      uint32_t            act         //IN: number of ACT to check
228
                      )
229
{
230
    P_ACT_DATA              actData = ActGetDataPointer(act);
231
    //
232
    if(actData == NULL)
233
        return 0;
234
    return (int)actData->pending;
235
}
236
/* C.16.2.8.        _plat__ACT_UpdateCounter() */
237
/* This function is used to write the newValue for the counter. If an update is pending, then no
238
   update occurs and the function returns FALSE. If setSignaled is TRUE, then the ACT signaled state
239
   is SET and if newValue is 0, nothing is posted. */
240
LIB_EXPORT int
241
_plat__ACT_UpdateCounter(
242
                         uint32_t            act,        // IN: ACT to update
243
                         uint32_t            newValue   // IN: the value to post
244
                         )
245
{
246
    P_ACT_DATA          actData = ActGetDataPointer(act);
247
    //
248
    if(actData == NULL)
249
        // actData doesn't exist but pretend update is pending rather than indicate
250
        // that a retry is necessary.
251
        return TRUE;
252
    // if an update is pending then return FALSE so that there will be a retry
253
    if(actData->pending != 0)
254
        return FALSE;
255
    actData->newValue = newValue;
256
    actData->pending = TRUE;
257
    
258
    return TRUE;
259
}
260
#endif                // libtpms added
261
/* C.16.2.9.        _plat__ACT_EnableTicks() */
262
/* This enables and disables the processing of the once-per-second ticks. This should be turned off
263
   (enable = FALSE) by _TPM_Init() and turned on (enable = TRUE) by TPM2_Startup() after all the
264
   initializations have completed. */
265
LIB_EXPORT void
266
_plat__ACT_EnableTicks(
20,911✔
267
                       int         enable
268
                       )
269
{
270
    actTicksAllowed = enable;
20,911✔
271
}
20,911✔
272
/* C.16.2.10.        ActDecrement() */
273
/* If newValue is non-zero it is copied to remaining and then newValue is set to zero. Then
274
   remaining is decremented by one if it is not already zero. If the value is decremented to zero,
275
   then the associated event is signaled. If setting remaining causes it to be greater than 1, then
276
   the signal associated with the ACT is turned off. */
277
#ifndef __ACT_DISABLED        // libtpms added
278
static void
279
ActDecrement(
280
             P_ACT_DATA            actData
281
             )
282
{
283
    // Check to see if there is an update pending
284
    if(actData->pending)
285
        {
286
            // If this update will cause the count to go from non-zero to zero, set
287
            // the newValue to 1 so that it will timeout when decremented below.
288
            if((actData->newValue == 0) && (actData->remaining != 0))
289
                actData->newValue = 1;
290
            actData->remaining = actData->newValue;
291
            
292
            // Update processed
293
            actData->pending = 0;
294
        }
295
    // no update so countdown if the count is non-zero but not max
296
    if((actData->remaining != 0) && (actData->remaining != UINT32_MAX))
297
        {
298
            // If this countdown causes the count to go to zero, then turn the signal for
299
            // the ACT on.
300
            if((actData->remaining -= 1) == 0)
301
                ActSignal(actData, TRUE);
302
        }
303
    // If the current value of the counter is non-zero, then the signal should be
304
    // off.
305
    if(actData->signaled && (actData->remaining > 0))
306
        ActSignal(actData, FALSE);
307
}
308
/* C.16.2.11.        _plat__ACT_Tick() */
309
/* This processes the once-per-second clock tick from the hardware. This is set up for the simulator to use the control interface to send ticks to the TPM. These ticks do not have to be on a per second basis. They can be as slow or as fast as desired so that the simulation can be tested. */
310
LIB_EXPORT void
311
_plat__ACT_Tick(
312
                void
313
                )
314
{
315
    // Ticks processing is turned off at certain times just to make sure that nothing
316
    // strange is happening before pointers and things are
317
    if(actTicksAllowed)
318
        {
319
            // Handle the update for each counter.
320
#define DECREMENT_COUNT(N)   ActDecrement(&ACT_##N);
321
            
322
            FOR_EACH_ACT(DECREMENT_COUNT)
323
                }
324
}
325
/* C.16.2.12.        ActZero() */
326
/* This function initializes a single ACT */
327
static void
328
ActZero(
329
        uint32_t        act,
330
        P_ACT_DATA      actData
331
        )
332
{
333
    actData->remaining = 0;
334
    actData->newValue = 0;
335
    actData->pending = 0;
336
    actData->number = (uint8_t)act;
337
    ActSignal(actData, FALSE);
338
}
339
#endif                        // libtpms added
340
/* C.16.2.13.        _plat__ACT_Initialize() */
341
/* This function initializes the ACT hardware and data structures */
342
LIB_EXPORT int
343
_plat__ACT_Initialize(
4,238✔
344
                      void
345
                      )
346
{
347
    actTicksAllowed = 0;
4,238✔
348
#define ZERO_ACT(N)  ActZero(0x##N, &ACT_##N);
349
    FOR_EACH_ACT(ZERO_ACT)
350
        
351
        return TRUE;
4,238✔
352
}
353

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