1 /** @file
2   Sample to provide SaveSecContext function.
3 
4   Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php.
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 
16 #include <PiPei.h>
17 #include <Library/DebugLib.h>
18 
19 #include <Ppi/TopOfTemporaryRam.h>
20 #include <Ppi/SecPlatformInformation.h>
21 
22 /**
23   Save BIST value before call FspInit.
24 
25   @param[in] Bist   BIST value.
26 **/
27 VOID
28 AsmSaveBistValue (
29   IN UINT32  Bist
30   );
31 
32 /**
33   Save Ticker value before call FspInit.
34 
35   @param[in] Ticker   Ticker value.
36 **/
37 VOID
38 AsmSaveTickerValue (
39   IN UINT64  Ticker
40   );
41 
42 /**
43   Save SEC context before call FspInit.
44 
45   @param[in] PeiServices  Pointer to PEI Services Table.
46 **/
47 VOID
48 EFIAPI
SaveSecContext(IN CONST EFI_PEI_SERVICES ** PeiServices)49 SaveSecContext (
50   IN CONST EFI_PEI_SERVICES                     **PeiServices
51   )
52 {
53   UINT32      *Bist;
54   UINT64      *Ticker;
55   UINT32      Size;
56   UINT32      Count;
57   UINT32      TopOfTemporaryRam;
58   VOID        *TopOfTemporaryRamPpi;
59   EFI_STATUS  Status;
60 
61   DEBUG ((DEBUG_INFO, "SaveSecContext - 0x%x\n", PeiServices));
62 
63   Status = (*PeiServices)->LocatePpi (
64                              PeiServices,
65                              &gTopOfTemporaryRamPpiGuid,
66                              0,
67                              NULL,
68                              (VOID **) &TopOfTemporaryRamPpi
69                              );
70   if (EFI_ERROR (Status)) {
71     return ;
72   }
73 
74   DEBUG ((DEBUG_INFO, "TopOfTemporaryRamPpi - 0x%x\n", TopOfTemporaryRamPpi));
75 
76   //
77   // The entries of BIST information, together with the number of them,
78   // reside in the bottom of stack, left untouched by normal stack operation.
79   // This routine copies the BIST information to the buffer pointed by
80   // PlatformInformationRecord for output.
81   //
82   // |--------------| <- TopOfTemporaryRam
83   // |Number of BSPs|
84   // |--------------|
85   // |     BIST     |
86   // |--------------|
87   // |     ....     |
88   // |--------------|
89   // |  TSC[63:32]  |
90   // |--------------|
91   // |  TSC[31:00]  |
92   // |--------------|
93   //
94 
95   TopOfTemporaryRam = (UINT32)(UINTN)TopOfTemporaryRamPpi - sizeof(UINT32);
96   TopOfTemporaryRam -= sizeof(UINT32) * 2;
97   DEBUG ((DEBUG_INFO, "TopOfTemporaryRam - 0x%x\n", TopOfTemporaryRam));
98   Count             = *(UINT32 *)(UINTN)(TopOfTemporaryRam - sizeof(UINT32));
99   DEBUG ((DEBUG_INFO, "Count - 0x%x\n", Count));
100   Size              = Count * sizeof (IA32_HANDOFF_STATUS);
101   DEBUG ((DEBUG_INFO, "Size - 0x%x\n", Size));
102 
103   Bist   = (UINT32 *)(UINTN)(TopOfTemporaryRam - sizeof(UINT32) - Size);
104   DEBUG ((DEBUG_INFO, "Bist - 0x%x\n", *Bist));
105   Ticker = (UINT64 *)(UINTN)(TopOfTemporaryRam - sizeof(UINT32) - Size - sizeof(UINT64));
106   DEBUG ((DEBUG_INFO, "Ticker - 0x%lx\n", *Ticker));
107 
108   // Just need record BSP
109   AsmSaveBistValue (*Bist);
110   AsmSaveTickerValue (*Ticker);
111 }
112