SecMain.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /** @file
  2. Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "SecMain.h"
  6. #include "SecFsp.h"
  7. EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI gSecTemporaryRamSupportPpi = {
  8. SecTemporaryRamSupport
  9. };
  10. EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {
  11. {
  12. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  13. &gEfiTemporaryRamSupportPpiGuid,
  14. &gSecTemporaryRamSupportPpi
  15. }
  16. };
  17. //
  18. // These are IDT entries pointing to 08:FFFFFFE4h.
  19. //
  20. UINT64 mIdtEntryTemplate = 0xffff8e000008ffe4ULL;
  21. /**
  22. Entry point to the C language phase of SEC. After the SEC assembly
  23. code has initialized some temporary memory and set up the stack,
  24. the control is transferred to this function.
  25. @param[in] SizeOfRam Size of the temporary memory available for use.
  26. @param[in] TempRamBase Base address of temporary ram
  27. @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.
  28. @param[in] PeiCore PeiCore entry point.
  29. @param[in] BootLoaderStack BootLoader stack.
  30. @param[in] ApiIdx the index of API.
  31. @return This function never returns.
  32. **/
  33. VOID
  34. EFIAPI
  35. SecStartup (
  36. IN UINT32 SizeOfRam,
  37. IN UINT32 TempRamBase,
  38. IN VOID *BootFirmwareVolume,
  39. IN PEI_CORE_ENTRY PeiCore,
  40. IN UINT32 BootLoaderStack,
  41. IN UINT32 ApiIdx
  42. )
  43. {
  44. EFI_SEC_PEI_HAND_OFF SecCoreData;
  45. IA32_DESCRIPTOR IdtDescriptor;
  46. SEC_IDT_TABLE IdtTableInStack;
  47. UINT32 Index;
  48. FSP_GLOBAL_DATA PeiFspData;
  49. UINT64 ExceptionHandler;
  50. //
  51. // Process all libraries constructor function linked to SecCore.
  52. //
  53. ProcessLibraryConstructorList ();
  54. //
  55. // Initialize floating point operating environment
  56. // to be compliant with UEFI spec.
  57. //
  58. InitializeFloatingPointUnits ();
  59. // |-------------------|---->
  60. // |Idt Table |
  61. // |-------------------|
  62. // |PeiService Pointer | PeiStackSize
  63. // |-------------------|
  64. // | |
  65. // | Stack |
  66. // |-------------------|---->
  67. // | |
  68. // | |
  69. // | Heap | PeiTemporayRamSize
  70. // | |
  71. // | |
  72. // |-------------------|----> TempRamBase
  73. IdtTableInStack.PeiService = NULL;
  74. ExceptionHandler = FspGetExceptionHandler(mIdtEntryTemplate);
  75. for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {
  76. CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&ExceptionHandler, sizeof (UINT64));
  77. }
  78. IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;
  79. IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);
  80. AsmWriteIdtr (&IdtDescriptor);
  81. //
  82. // Initialize the global FSP data region
  83. //
  84. FspGlobalDataInit (&PeiFspData, BootLoaderStack, (UINT8)ApiIdx);
  85. //
  86. // Update the base address and length of Pei temporary memory
  87. //
  88. SecCoreData.DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);
  89. SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;
  90. SecCoreData.BootFirmwareVolumeSize = (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)BootFirmwareVolume)->FvLength;
  91. SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;
  92. SecCoreData.TemporaryRamSize = SizeOfRam;
  93. SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
  94. SecCoreData.PeiTemporaryRamSize = SizeOfRam >> 1;
  95. SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);
  96. SecCoreData.StackSize = SizeOfRam >> 1;
  97. //
  98. // Call PeiCore Entry
  99. //
  100. PeiCore (&SecCoreData, mPeiSecPlatformInformationPpi);
  101. //
  102. // Should never be here
  103. //
  104. CpuDeadLoop ();
  105. }
  106. /**
  107. This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
  108. permanent memory.
  109. @param[in] PeiServices Pointer to the PEI Services Table.
  110. @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
  111. Temporary RAM contents.
  112. @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
  113. Temporary RAM contents.
  114. @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.
  115. @retval EFI_SUCCESS The data was successfully returned.
  116. @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
  117. TemporaryMemoryBase > PermanentMemoryBase.
  118. **/
  119. EFI_STATUS
  120. EFIAPI
  121. SecTemporaryRamSupport (
  122. IN CONST EFI_PEI_SERVICES **PeiServices,
  123. IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
  124. IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
  125. IN UINTN CopySize
  126. )
  127. {
  128. IA32_DESCRIPTOR IdtDescriptor;
  129. VOID* OldHeap;
  130. VOID* NewHeap;
  131. VOID* OldStack;
  132. VOID* NewStack;
  133. OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;
  134. NewHeap = (VOID*)((UINTN)PermanentMemoryBase + CopySize / 2);
  135. OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize / 2);
  136. NewStack = (VOID*)(UINTN)PermanentMemoryBase;
  137. //
  138. // Migrate Heap
  139. //
  140. CopyMem (NewHeap, OldHeap, CopySize / 2);
  141. //
  142. // Migrate Stack
  143. //
  144. CopyMem (NewStack, OldStack, CopySize / 2);
  145. //
  146. // We need *not* fix the return address because currently,
  147. // The PeiCore is executed in flash.
  148. //
  149. //
  150. // Rebase IDT table in permanent memory
  151. //
  152. AsmReadIdtr (&IdtDescriptor);
  153. IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
  154. AsmWriteIdtr (&IdtDescriptor);
  155. //
  156. // Fixed the FSP data pointer
  157. //
  158. FspDataPointerFixUp ((UINTN)NewStack - (UINTN)OldStack);
  159. //
  160. // SecSwitchStack function must be invoked after the memory migration
  161. // immediately, also we need fixup the stack change caused by new call into
  162. // permanent memory.
  163. //
  164. SecSwitchStack (
  165. (UINT32) (UINTN) OldStack,
  166. (UINT32) (UINTN) NewStack
  167. );
  168. return EFI_SUCCESS;
  169. }