1 /* Microsoft Reference Implementation for TPM 2.0
2  *
3  *  The copyright in this software is being made available under the BSD License,
4  *  included below. This software may be subject to other third party and
5  *  contributor rights, including patent rights, and no such rights are granted
6  *  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 modification,
15  *  are permitted provided that the following conditions are met:
16  *
17  *  Redistributions of source code must retain the above copyright notice, this list
18  *  of conditions and the following disclaimer.
19  *
20  *  Redistributions in binary form must reproduce the above copyright notice, this
21  *  list of conditions and the following disclaimer in the documentation and/or
22  *  other materials provided with the distribution.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "Tpm.h"
36 #include "_TPM_Init_fp.h"
37 
38  // This function is used to process a _TPM_Init indication.
39 LIB_EXPORT void
_TPM_Init(void)40 _TPM_Init(
41     void
42     )
43 {
44     g_powerWasLost = g_powerWasLost | _plat__WasPowerLost();
45 
46 #if SIMULATION && DEBUG
47     // If power was lost and this was a simulation, put canary in RAM used by NV
48     // so that uninitialized memory can be detected more easily
49     if(g_powerWasLost)
50     {
51         memset(&gc, 0xbb, sizeof(gc));
52         memset(&gr, 0xbb, sizeof(gr));
53         memset(&gp, 0xbb, sizeof(gp));
54         memset(&go, 0xbb, sizeof(go));
55     }
56 #endif
57 
58 #if SIMULATION
59     // Clear the flag that forces failure on self-test
60     g_forceFailureMode = FALSE;
61 #endif
62 
63     // Disable the tick processing
64     _plat__ACT_EnableTicks(FALSE);
65 
66     // Set initialization state
67     TPMInit();
68 
69     // Set g_DRTMHandle as unassigned
70     g_DRTMHandle = TPM_RH_UNASSIGNED;
71 
72     // No H-CRTM, yet.
73     g_DrtmPreStartup = FALSE;
74 
75     // Initialize the NvEnvironment.
76     g_nvOk = NvPowerOn();
77 
78     // Initialize cryptographic functions
79     g_inFailureMode = (CryptInit() == FALSE);
80     if(!g_inFailureMode)
81     {
82         // Load the persistent data
83         NvReadPersistent();
84 
85         // Load the orderly data (clock and DRBG state).
86         // If this is not done here, things break
87         NvRead(&go, NV_ORDERLY_DATA, sizeof(go));
88 
89         // Start clock. Need to do this after NV has been restored.
90         TimePowerOn();
91     }
92     return;
93 }