1 /*++ @file
2   Stub SEC that is called from the OS application 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   EFI_PEI_PPI_DESCRIPTOR    PpiArray[10];
79 
80   EMU_MAGIC_PAGE()->PpiList = PpiList;
81   ProcessLibraryConstructorList ();
82 
83   DEBUG ((EFI_D_ERROR, "SEC Has Started\n"));
84 
85   //
86   // Add Our PPIs to the list
87   //
88   SecReseveredMemorySize = sizeof (gPrivateDispatchTable);
89   for (Ppi = PpiList, Index = 1; ; Ppi++, Index++) {
90     SecReseveredMemorySize += sizeof (EFI_PEI_PPI_DESCRIPTOR);
91 
92     if ((Ppi->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) == EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {
93       // Since we are appending, need to clear out previous list terminator.
94       Ppi->Flags &= ~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
95       break;
96     }
97   }
98 
99   // Keep everything on a good alignment
100   SecReseveredMemorySize = ALIGN_VALUE (SecReseveredMemorySize, CPU_STACK_ALIGNMENT);
101 
102 #if 0
103   // Tell the PEI Core to not use our buffer in temp RAM
104   SecPpiList = (EFI_PEI_PPI_DESCRIPTOR *)SecCoreData->PeiTemporaryRamBase;
105   SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN)SecCoreData->PeiTemporaryRamBase + SecReseveredMemorySize);
106   SecCoreData->PeiTemporaryRamSize -= SecReseveredMemorySize;
107 #else
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 
113   SecPpiList = &PpiArray[0];
114   ASSERT (sizeof (PpiArray) >= SecReseveredMemorySize);
115 #endif
116   // Copy existing list, and append our entries.
117   CopyMem (SecPpiList, PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR) * Index);
118   CopyMem (&SecPpiList[Index], gPrivateDispatchTable, sizeof (gPrivateDispatchTable));
119 
120   // Find PEI Core and transfer control
121   VolumeHandle = (EFI_PEI_FV_HANDLE)(UINTN)SecCoreData->BootFirmwareVolumeBase;
122   FileHandle   = NULL;
123   Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_PEI_CORE, VolumeHandle, &FileHandle);
124   ASSERT_EFI_ERROR (Status);
125 
126   Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage);
127   ASSERT_EFI_ERROR (Status);
128 
129   Status = PeCoffLoaderGetEntryPoint (PeCoffImage, (VOID **)&EntryPoint);
130   ASSERT_EFI_ERROR (Status);
131 
132   // Transfer control to PEI Core
133   EntryPoint (SecCoreData, SecPpiList);
134 
135   // PEI Core never returns
136   ASSERT (FALSE);
137   return;
138 }
139 
140 
141