]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspWrapperPkg/Library/SecPeiFspPlatformSecLibSample/SecTempRamSupport.c
Add IntelFspWrapper to support boot EDKII on FSP bin.
[mirror_edk2.git] / IntelFspWrapperPkg / Library / SecPeiFspPlatformSecLibSample / SecTempRamSupport.c
CommitLineData
a33a2f62
JY
1/** @file\r
2 Sample to provide SecTemporaryRamSupport function.\r
3\r
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiPei.h>\r
16\r
17#include <Ppi/TemporaryRamSupport.h>\r
18\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22#include <Library/DebugAgentLib.h>\r
23\r
24/**\r
25 Switch the stack in the temporary memory to the one in the permanent memory.\r
26\r
27 This function must be invoked after the memory migration immediately. The relative\r
28 position of the stack in the temporary and permanent memory is same.\r
29\r
30 @param[in] TemporaryMemoryBase Base address of the temporary memory.\r
31 @param[in] PermenentMemoryBase Base address of the permanent memory.\r
32**/\r
33VOID\r
34EFIAPI\r
35SecSwitchStack (\r
36 IN UINT32 TemporaryMemoryBase,\r
37 IN UINT32 PermenentMemoryBase\r
38 );\r
39\r
40/**\r
41 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into\r
42 permanent memory.\r
43\r
44 @param[in] PeiServices Pointer to the PEI Services Table.\r
45 @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the\r
46 Temporary RAM contents.\r
47 @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the\r
48 Temporary RAM contents.\r
49 @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.\r
50\r
51 @retval EFI_SUCCESS The data was successfully returned.\r
52 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when\r
53 TemporaryMemoryBase > PermanentMemoryBase.\r
54\r
55**/\r
56EFI_STATUS\r
57EFIAPI\r
58SecTemporaryRamSupport (\r
59 IN CONST EFI_PEI_SERVICES **PeiServices,\r
60 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r
61 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r
62 IN UINTN CopySize\r
63 )\r
64{\r
65 IA32_DESCRIPTOR IdtDescriptor;\r
66 VOID* OldHeap;\r
67 VOID* NewHeap;\r
68 VOID* OldStack;\r
69 VOID* NewStack;\r
70 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext;\r
71 BOOLEAN OldStatus;\r
72 UINTN PeiStackSize;\r
73\r
74 PeiStackSize = (UINTN)PcdGet32 (PcdPeiTemporaryRamStackSize);\r
75 if (PeiStackSize == 0) {\r
76 PeiStackSize = (CopySize >> 1);\r
77 }\r
78\r
79 ASSERT (PeiStackSize < CopySize);\r
80\r
81 //\r
82 // |-------------------|---->\r
83 // | Stack | PeiStackSize\r
84 // |-------------------|---->\r
85 // | Heap | PeiTemporayRamSize\r
86 // |-------------------|----> TempRamBase\r
87 //\r
88 // |-------------------|---->\r
89 // | Heap | PeiTemporayRamSize\r
90 // |-------------------|---->\r
91 // | Stack | PeiStackSize\r
92 // |-------------------|----> PermanentMemoryBase\r
93 //\r
94\r
95 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;\r
96 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + PeiStackSize);\r
97\r
98 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize - PeiStackSize);\r
99 NewStack = (VOID*)(UINTN)PermanentMemoryBase;\r
100\r
101 DebugAgentContext.HeapMigrateOffset = (UINTN)NewHeap - (UINTN)OldHeap;\r
102 DebugAgentContext.StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;\r
103\r
104 OldStatus = SaveAndSetDebugTimerInterrupt (FALSE);\r
105 //\r
106 // Initialize Debug Agent to support source level debug in PEI phase after memory ready.\r
107 // It will build HOB and fix up the pointer in IDT table.\r
108 //\r
109 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);\r
110\r
111 //\r
112 // Migrate Heap\r
113 //\r
114 CopyMem (NewHeap, OldHeap, CopySize - PeiStackSize);\r
115\r
116 //\r
117 // Migrate Stack\r
118 //\r
119 CopyMem (NewStack, OldStack, PeiStackSize);\r
120\r
121\r
122 //\r
123 // We need *not* fix the return address because currently,\r
124 // The PeiCore is executed in flash.\r
125 //\r
126\r
127 //\r
128 // Rebase IDT table in permanent memory\r
129 //\r
130 AsmReadIdtr (&IdtDescriptor);\r
131 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;\r
132\r
133 AsmWriteIdtr (&IdtDescriptor);\r
134\r
135\r
136 //\r
137 // Program MTRR\r
138 //\r
139\r
140 //\r
141 // SecSwitchStack function must be invoked after the memory migration\r
142 // immediatly, also we need fixup the stack change caused by new call into\r
143 // permenent memory.\r
144 //\r
145 SecSwitchStack (\r
146 (UINT32) (UINTN) OldStack,\r
147 (UINT32) (UINTN) NewStack\r
148 );\r
149\r
150 SaveAndSetDebugTimerInterrupt (OldStatus);\r
151\r
152 return EFI_SUCCESS;\r
153}\r
154\r