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 #define MANUFACTURE_C
9 #include "InternalRoutines.h"
10 #include "Global.h"
11 //
12 //
13 //          Functions
14 //
15 //         TPM_Manufacture()
16 //
17 //     This function initializes the TPM values in preparation for the TPM's first use. This function will fail if
18 //     previously called. The TPM can be re-manufactured by calling TPM_Teardown() first and then calling this
19 //     function again.
20 //
21 //     Return Value                      Meaning
22 //
23 //     0                                 success
24 //     1                                 manufacturing process previously performed
25 //
26 LIB_EXPORT int
TPM_Manufacture(BOOL firstTime)27 TPM_Manufacture(
28    BOOL                 firstTime           // IN: indicates if this is the first call from
29                                             //     main()
30    )
31 {
32    TPM_SU              orderlyShutdown;
33    UINT64              totalResetCount = 0;
34    // If TPM has been manufactured, return indication.
35    if(!firstTime && g_manufactured)
36        return 1;
37    // initialize crypto units
38    //CryptInitUnits();
39    //
40    s_selfHealTimer = 0;
41    s_lockoutTimer = 0;
42    s_DAPendingOnNV = FALSE;
43    // initialize NV
44    NvInit();
45 #ifdef _DRBG_STATE_SAVE
46    // Initialize the drbg. This needs to come before the install
47    // of the hierarchies
48    if(!_cpri__Startup())               // Have to start the crypto units first
49        FAIL(FATAL_ERROR_INTERNAL);
50    _cpri__DrbgGetPutState(PUT_STATE, 0, NULL);
51 #endif
52    // default configuration for PCR
53    PCRSimStart();
54    // initialize pre-installed hierarchy data
55    // This should happen after NV is initialized because hierarchy data is
56    // stored in NV.
57    HierarchyPreInstall_Init();
58    // initialize dictionary attack parameters
59    DAPreInstall_Init();
60    // initialize PP list
61    PhysicalPresencePreInstall_Init();
62    // initialize command audit list
63    CommandAuditPreInstall_Init();
64    // first start up is required to be Startup(CLEAR)
65     orderlyShutdown = TPM_SU_CLEAR;
66     NvWriteReserved(NV_ORDERLY, &orderlyShutdown);
67    // initialize the firmware version
68    gp.firmwareV1 = FIRMWARE_V1;
69 #ifdef FIRMWARE_V2
70    gp.firmwareV2 = FIRMWARE_V2;
71 #else
72    gp.firmwareV2 = 0;
73 #endif
74    NvWriteReserved(NV_FIRMWARE_V1, &gp.firmwareV1);
75    NvWriteReserved(NV_FIRMWARE_V2, &gp.firmwareV2);
76     // initialize the total reset counter to 0
77     NvWriteReserved(NV_TOTAL_RESET_COUNT, &totalResetCount);
78     // initialize the clock stuff
79     go.clock = 0;
80     go.clockSafe = YES;
81 #ifdef _DRBG_STATE_SAVE
82    // initialize the current DRBG state in NV
83    _cpri__DrbgGetPutState(GET_STATE, sizeof(go.drbgState), (BYTE *)&go.drbgState);
84 #endif
85     NvWriteReserved(NV_ORDERLY_DATA, &go);
86     // Commit NV writes. Manufacture process is an artificial process existing
87     // only in simulator environment and it is not defined in the specification
88     // that what should be the expected behavior if the NV write fails at this
89     // point. Therefore, it is assumed the NV write here is always success and
90     // no return code of this function is checked.
91     NvCommit();
92     g_manufactured = TRUE;
93     return 0;
94 }
95 //
96 //
97 //          TPM_TearDown()
98 //
99 //      This function prepares the TPM for re-manufacture. It should not be implemented in anything other than a
100 //      simulated TPM.
101 //      In this implementation, all that is needs is to stop the cryptographic units and set a flag to indicate that the
102 //      TPM can be re-manufactured. This should be all that is necessary to start the manufacturing process
103 //      again.
104 //
105 //      Return Value                      Meaning
106 //
107 //      0                                 success
108 //      1                                 TPM not previously manufactured
109 //
110 LIB_EXPORT int
TPM_TearDown(void)111 TPM_TearDown(
112     void
113     )
114 {
115     // stop crypt units
116     CryptStopUnits();
117     g_manufactured = FALSE;
118       return 0;
119 }
120