summaryrefslogtreecommitdiff
path: root/OvmfPkg/Sec/SecMain.c
blob: d6c6f427227288c5f464b95d9dc14c8a36e4710d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/** @file
  Main SEC phase code.  Transitions to PEI.

  Copyright (c) 2008 - 2009, Intel Corporation

  All rights reserved. This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <PiPei.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/PeiServicesLib.h>
#include <Ppi/TemporaryRamSupport.h>
#include <Library/PcdLib.h>
#include <Library/UefiCpuLib.h>

#include "SecMain.h"

EFI_STATUS
EFIAPI
TemporaryRamMigration (
  IN CONST EFI_PEI_SERVICES   **PeiServices,
  IN EFI_PHYSICAL_ADDRESS     TemporaryMemoryBase,
  IN EFI_PHYSICAL_ADDRESS     PermanentMemoryBase,
  IN UINTN                    CopySize
  );

STATIC TEMPORARY_RAM_SUPPORT_PPI mTempRamSupportPpi = {
  (TEMPORARY_RAM_MIGRATION) TemporaryRamMigration
};

STATIC EFI_PEI_PPI_DESCRIPTOR mPrivateDispatchTable[] = {
  {
    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gEfiTemporaryRamSupportPpiGuid,
    &mTempRamSupportPpi
  },
};


VOID
InitializeIdtPtr (
  IN VOID* IdtPtr
  )
{
  IA32_DESCRIPTOR             IdtDescriptor;

  IdtDescriptor.Base  = (UINTN)IdtPtr;
  IdtDescriptor.Limit = (UINT16) 0;
  AsmWriteIdtr (&IdtDescriptor);
}

VOID
EFIAPI
SecCoreStartupWithStack (
  IN EFI_FIRMWARE_VOLUME_HEADER       *BootFv,
  IN VOID                             *TopOfCurrentStack
  )
{
  EFI_SEC_PEI_HAND_OFF        *SecCoreData;
  UINT8                       *BottomOfTempRam;
  UINT8                       *TopOfTempRam;
  UINTN                       SizeOfTempRam;
  VOID                        *IdtPtr;
  VOID                        *PeiCoreEntryPoint;

  DEBUG ((EFI_D_INFO,
    "SecCoreStartupWithStack(0x%x, 0x%x)\n",
    (UINT32)(UINTN)BootFv,
    (UINT32)(UINTN)TopOfCurrentStack
    ));

  ProcessLibraryConstructorList (NULL, NULL);

  //
  // Initialize floating point operating environment
  // to be compliant with UEFI spec.
  //
  InitializeFloatingPointUnits ();

  BottomOfTempRam = (UINT8*)(UINTN) INITIAL_TOP_OF_STACK;
  SizeOfTempRam = (UINTN) SIZE_64KB;
  TopOfTempRam = BottomOfTempRam + SizeOfTempRam;

  //
  // |-------------|
  // | SecCoreData | 4k
  // |-------------|
  // |    Heap     | 28k
  // |-------------|
  // |   Stack     | 32k
  // |-------------| <---- INITIAL_TOP_OF_STACK
  //

  //
  // Bind this information into the SEC hand-off state
  //
  SecCoreData = (EFI_SEC_PEI_HAND_OFF*)((UINTN) TopOfTempRam - SIZE_4KB);
  SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF);

  SecCoreData->TemporaryRamBase       = (VOID*) BottomOfTempRam;
  SecCoreData->TemporaryRamSize       = SizeOfTempRam;

  SecCoreData->PeiTemporaryRamSize    = 28 * SIZE_1KB;
  SecCoreData->PeiTemporaryRamBase    = (VOID*)((UINTN)SecCoreData - SecCoreData->PeiTemporaryRamSize);

  SecCoreData->StackBase              = SecCoreData->TemporaryRamBase;
  SecCoreData->StackSize              = (UINTN)SecCoreData->PeiTemporaryRamBase - (UINTN)SecCoreData->TemporaryRamBase;

  //
  // Initialize the IDT Pointer, since IA32 & X64 architectures
  // use it to store the PEI Services pointer.
  //
  IdtPtr = (VOID*)((UINT8*)SecCoreData + sizeof (*SecCoreData) + sizeof (UINTN));
  IdtPtr = ALIGN_POINTER(IdtPtr, 16);
  InitializeIdtPtr (IdtPtr);

  FindPeiCoreEntryPoint (&BootFv, &PeiCoreEntryPoint);

  SecCoreData->BootFirmwareVolumeBase = BootFv;
  SecCoreData->BootFirmwareVolumeSize = (UINTN) BootFv->FvLength;

  if (PeiCoreEntryPoint != NULL) {
    DEBUG ((EFI_D_INFO,
      "Calling PEI Core entry point at 0x%x\n",
      PeiCoreEntryPoint
      ));
    //
    // Transfer control to the PEI Core
    //
    PeiSwitchStacks (
      (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,
      SecCoreData,
      (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &mPrivateDispatchTable),
      NULL,
      TopOfCurrentStack,
      (VOID *)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)
      );
  }

  //
  // If we get here, then either we couldn't locate the PEI Core, or
  // the PEI Core returned.
  //
  // Both of these errors are unrecoverable.
  //
  ASSERT (FALSE);
  CpuDeadLoop ();
}

EFI_STATUS
EFIAPI
TemporaryRamMigration (
  IN CONST EFI_PEI_SERVICES   **PeiServices,
  IN EFI_PHYSICAL_ADDRESS     TemporaryMemoryBase,
  IN EFI_PHYSICAL_ADDRESS     PermanentMemoryBase,
  IN UINTN                    CopySize
  )
{
  DEBUG ((EFI_D_ERROR, "TemporaryRamMigration(0x%x, 0x%x, 0x%x)\n", (UINTN)TemporaryMemoryBase, (UINTN)PermanentMemoryBase, CopySize));

  //
  // Migrate the whole temporary memory to permenent memory.
  // 
  CopyMem((VOID*)(UINTN)PermanentMemoryBase, (VOID*)(UINTN)TemporaryMemoryBase, CopySize);

  //
  // SecSwitchStack function must be invoked after the memory migration
  // immediatly, also we need fixup the stack change caused by new call into 
  // permenent memory.
  // 
  SecSwitchStack (
    (UINTN) TemporaryMemoryBase,
    (UINTN) PermanentMemoryBase,
    CopySize
    );

  return EFI_SUCCESS;
}