1 /*++ @file
2   Stub SEC that is called from the OS appliation that is the root of the emulator.
3 
4   The OS application will call the SEC with the PEI Entry Point API.
5 
6 Copyright (c) 2011, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution.  The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #include "Sec.h"
18 
19 
20 
21 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = {
22   SecTemporaryRamSupport
23 };
24 
25 
26 EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {
27   {
28     EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
29     &gEfiTemporaryRamSupportPpiGuid,
30     &mSecTemporaryRamSupportPpi
31   }
32 };
33 
34 
35 
36 /**
37   The entry point of PE/COFF Image for the PEI Core, that has been hijacked by this
38   SEC that sits on top of an OS application. So the entry and exit of this module
39   has the same API.
40 
41   This function is the entry point for the PEI Foundation, which allows the SEC phase
42   to pass information about the stack, temporary RAM and the Boot Firmware Volume.
43   In addition, it also allows the SEC phase to pass services and data forward for use
44   during the PEI phase in the form of one or more PPIs.
45   There is no limit to the number of additional PPIs that can be passed from SEC into
46   the PEI Foundation. As part of its initialization phase, the PEI Foundation will add
47   these SEC-hosted PPIs to its PPI database such that both the PEI Foundation and any
48   modules can leverage the associated service calls and/or code in these early PPIs.
49   This function is required to call ProcessModuleEntryPointList() with the Context
50   parameter set to NULL.  ProcessModuleEntryPoint() is never expected to return.
51   The PEI Core is responsible for calling ProcessLibraryConstructorList() as soon as
52   the PEI Services Table and the file handle for the PEI Core itself have been established.
53   If ProcessModuleEntryPointList() returns, then ASSERT() and halt the system.
54 
55   @param SecCoreData  Points to a data structure containing information about the PEI
56                       core's operating environment, such as the size and location of
57                       temporary RAM, the stack location and the BFV location.
58 
59   @param PpiList      Points to a list of one or more PPI descriptors to be installed
60                       initially by the PEI core. An empty PPI list consists of a single
61                       descriptor with the end-tag EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST.
62                       As part of its initialization phase, the PEI Foundation will add
63                       these SEC-hosted PPIs to its PPI database such that both the PEI
64                       Foundation and any modules can leverage the associated service calls
65                       and/or code in these early PPIs.
66 
67 **/
68 VOID
69 EFIAPI
_ModuleEntryPoint(IN EFI_SEC_PEI_HAND_OFF * SecCoreData,IN EFI_PEI_PPI_DESCRIPTOR * PpiList)70 _ModuleEntryPoint (
71   IN EFI_SEC_PEI_HAND_OFF   *SecCoreData,
72   IN EFI_PEI_PPI_DESCRIPTOR *PpiList
73   )
74 {
75   EFI_STATUS                Status;
76   EFI_PEI_FV_HANDLE         VolumeHandle;
77   EFI_PEI_FILE_HANDLE       FileHandle;
78   VOID                      *PeCoffImage;
79   EFI_PEI_CORE_ENTRY_POINT  EntryPoint;
80   EFI_PEI_PPI_DESCRIPTOR    *Ppi;
81   EFI_PEI_PPI_DESCRIPTOR    *SecPpiList;
82   UINTN                     SecReseveredMemorySize;
83   UINTN                     Index;
84 
85   EMU_MAGIC_PAGE()->PpiList = PpiList;
86   ProcessLibraryConstructorList ();
87 
88   DEBUG ((EFI_D_ERROR, "SEC Has Started\n"));
89 
90   //
91   // Add Our PPIs to the list
92   //
93   SecReseveredMemorySize = sizeof (gPrivateDispatchTable);
94   for (Ppi = PpiList, Index = 1; ; Ppi++, Index++) {
95     SecReseveredMemorySize += sizeof (EFI_PEI_PPI_DESCRIPTOR);
96 
97     if ((Ppi->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) == EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {
98       // Since we are appending, need to clear out privious list terminator.
99       Ppi->Flags &= ~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
100       break;
101     }
102   }
103 
104   // Keep everything on a good alignment
105   SecReseveredMemorySize = ALIGN_VALUE (SecReseveredMemorySize, CPU_STACK_ALIGNMENT);
106 
107 #if 0
108   // Tell the PEI Core to not use our buffer in temp RAM
109   SecPpiList = (EFI_PEI_PPI_DESCRIPTOR *)SecCoreData->PeiTemporaryRamBase;
110   SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN)SecCoreData->PeiTemporaryRamBase + SecReseveredMemorySize);
111   SecCoreData->PeiTemporaryRamSize -= SecReseveredMemorySize;
112 #else
113   {
114     //
115     // When I subtrack from SecCoreData->PeiTemporaryRamBase PEI Core crashes? Either there is a bug
116     // or I don't understand temp RAM correctly?
117     //
118     EFI_PEI_PPI_DESCRIPTOR    PpiArray[10];
119 
120     SecPpiList = &PpiArray[0];
121     ASSERT (sizeof (PpiArray) >= SecReseveredMemorySize);
122   }
123 #endif
124   // Copy existing list, and append our entries.
125   CopyMem (SecPpiList, PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR) * Index);
126   CopyMem (&SecPpiList[Index], gPrivateDispatchTable, sizeof (gPrivateDispatchTable));
127 
128   // Find PEI Core and transfer control
129   VolumeHandle = (EFI_PEI_FV_HANDLE)(UINTN)SecCoreData->BootFirmwareVolumeBase;
130   FileHandle   = NULL;
131   Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_PEI_CORE, VolumeHandle, &FileHandle);
132   ASSERT_EFI_ERROR (Status);
133 
134   Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage);
135   ASSERT_EFI_ERROR (Status);
136 
137   Status = PeCoffLoaderGetEntryPoint (PeCoffImage, (VOID **)&EntryPoint);
138   ASSERT_EFI_ERROR (Status);
139 
140   // Transfer control to PEI Core
141   EntryPoint (SecCoreData, SecPpiList);
142 
143   // PEI Core never returns
144   ASSERT (FALSE);
145   return;
146 }
147 
148 
149 
150