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