memorystatuscode.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 511 行 · 第 1/2 页

C
511
字号
    mStatusCodeMemoryPpi.LastEntry  = LastEntry;
    mStatusCodeMemoryPpi.Address    = (EFI_PHYSICAL_ADDRESS) (UINTN) Buffer;
    mStatusCodeMemoryPpi.Length     = PEI_STATUS_CODE_RT_LENGTH;

    //
    // Reinstall the report status code function
    //
    //
    // Locate status code PPI
    //
    Status = (*PeiServices)->LocatePpi (
                              PeiServices,
                              &gPeiStatusCodePpiGuid,
                              0,
                              &ReportStatusCodeDescriptor,
                              &ReportStatusCodePpi
                              );
    if (EFI_ERROR (Status)) {
      return ;
    }
    //
    // Reinstall the ReportStatusCode interface using the memory-based
    // descriptor
    //
    Status = (*PeiServices)->ReInstallPpi (
                              PeiServices,
                              ReportStatusCodeDescriptor,
                              &mPpiListStatusCode
                              );
    if (EFI_ERROR (Status)) {
      EFI_BREAKPOINT ();
      return ;
    }
    //
    // Publish a GUIDed HOB that contains a pointer to the status code PPI
    // structure.  This is a bit of a short cut as I just used the PPI GUID to
    // identify the HOB.  This HOB is caught by the DXE status code memory
    // listener and used to find the journal.
    //
    StatusCodeMemoryPpi = &mStatusCodeMemoryPpi;
    Status = PeiBuildHobGuidData (
              PeiServices,
              &gPeiStatusCodeMemoryPpiGuid,
              &StatusCodeMemoryPpi,
              sizeof (VOID *)
              );
    if (EFI_ERROR (Status)) {
      EFI_BREAKPOINT ();
      return ;
    }
  }
}

EFI_STATUS
EFIAPI
MemoryReportStatusCode (
  IN EFI_PEI_SERVICES         **PeiServices,
  IN EFI_STATUS_CODE_TYPE     CodeType,
  IN EFI_STATUS_CODE_VALUE    Value,
  IN UINT32                   Instance,
  IN EFI_GUID                 * CallerId,
  IN EFI_STATUS_CODE_DATA     * Data OPTIONAL
  )
/*++

Routine Description:

  Provide a memory status code

Arguments:

  Same as ReportStatusCode PPI
    
Returns:

  EFI_SUCCESS   This function always returns success

--*/
{
  EFI_STATUS                  Status;
  PEI_STATUS_CODE_MEMORY_PPI  *StatusCodeMemoryPpi;
  EFI_STATUS_CODE_ENTRY       *CurrentEntry;
  UINTN                       LastEntry;
  MEMORY_STATUS_CODE_INSTANCE *PrivateData;
  EFI_PEI_PPI_DESCRIPTOR      *StatusCodeMemoryDescriptor;

  //
  // We don't care to log debug codes.
  //
  if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {
    return EFI_SUCCESS;
  }

  if (!mRunningFromMemory) {
    //
    // If we are called from DXE and have not been reinstalled into memory, we
    // can no longer locate the journal, so we can no longer log status codes.
    //
    if (!PeiServices) {
      return EFI_SUCCESS;
    }
    //
    // Locate Journal
    //
    Status = (*PeiServices)->LocatePpi (
                              PeiServices,
                              &gPeiStatusCodeMemoryPpiGuid,
                              0,
                              &StatusCodeMemoryDescriptor,
                              &StatusCodeMemoryPpi
                              );
    if (EFI_ERROR (Status)) {
      return EFI_SUCCESS;
    }
    //
    // Determine the last entry in the journal.
    // This is needed to properly implement the rolling queue.
    //
    LastEntry = PEI_STATUS_CODE_MAX_HEAP_ENTRY;

    //
    // Get private data
    //
    PrivateData = MEMORY_STATUS_CODE_FROM_DESCRIPTOR_THIS (StatusCodeMemoryDescriptor);

    //
    // Once memory gets installed, heap gets moved to real memory.
    // We need to fix up the pointers to match the move.
    //
    PrivateData->PpiDescriptor.Ppi  = &PrivateData->StatusCodeMemoryPpi;
    PrivateData->PpiDescriptor.Guid = &gPeiStatusCodeMemoryPpiGuid;
    PrivateData->StatusCodeMemoryPpi.Address = PrivateData->StatusCodeMemoryPpi.Address +
      (UINTN) PrivateData - (UINTN) PrivateData->This;
    PrivateData->NotifyDescriptor.Guid    = &gPeiFvFileLoaderPpiGuid;
    PrivateData->NotifyDescriptor.Notify  = LoadImageCallback;
    PrivateData->This                     = PrivateData;

    StatusCodeMemoryPpi                   = PrivateData->PpiDescriptor.Ppi;
  } else {
    //
    // Use global/memory copy of the PPI
    //
    StatusCodeMemoryPpi = &mStatusCodeMemoryPpi;

    //
    // Determine the last entry in the journal.
    // This is needed to properly implement the rolling queue.
    //
    LastEntry = PEI_STATUS_CODE_MAX_RT_ENTRY;
  }
  //
  // Return if we are using a cleared PPI somehow
  //
  if (!StatusCodeMemoryPpi->Address || !StatusCodeMemoryPpi->Length) {
    return EFI_SUCCESS;
  }
  //
  // Update the latest entry in the journal (may actually be first due to rolling
  // queue).
  //
  CurrentEntry = (EFI_STATUS_CODE_ENTRY *) (UINTN) (StatusCodeMemoryPpi->Address + (StatusCodeMemoryPpi->LastEntry * sizeof (EFI_STATUS_CODE_ENTRY)));

  StatusCodeMemoryPpi->LastEntry = (StatusCodeMemoryPpi->LastEntry + 1) % LastEntry;
  if (StatusCodeMemoryPpi->LastEntry == StatusCodeMemoryPpi->FirstEntry) {
    StatusCodeMemoryPpi->FirstEntry = (StatusCodeMemoryPpi->FirstEntry + 1) % LastEntry;
  }

  CurrentEntry->Type      = CodeType;
  CurrentEntry->Value     = Value;
  CurrentEntry->Instance  = Instance;

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
LoadImageCallback (
  IN EFI_PEI_SERVICES           **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Ppi
  )
/*++

Routine Description:

  Relocate the PEIM into memory.

  Once load protocol becomes available, relocate our PEIM into memory.
  The primary benefit is to eliminate the blackout window that we would have in
  the memory log between the end of PEI and the status code DXE driver taking
  control.  If we don't do this, we cannot determine where our memory journal
  is located and cannot function.

  A second benefit is speed optimization throughout DXE.

Arguments:

  PeiServices      - General purpose services available to every PEIM.
  NotifyDescriptor - Information about the notify event.
  Ppi              - Context
    
Returns:

  EFI_SUCCESS   This function always returns success.

--*/
{
  EFI_STATUS                  Status;
  EFI_PHYSICAL_ADDRESS        ImageAddress;
  EFI_PHYSICAL_ADDRESS        EntryPoint;
  UINT64                      ImageSize;
  MEMORY_STATUS_CODE_INSTANCE *PrivateData;

  //
  // Relocate to memory
  //
  if (!mRunningFromMemory) {
    //
    // Use the callback descriptor to get the FfsHeader
    //
    PrivateData = MEMORY_STATUS_CODE_FROM_NOTIFY_THIS (NotifyDescriptor);

    Status = ((EFI_PEI_FV_FILE_LOADER_PPI *) Ppi)->FvLoadFile (
                                                    Ppi,
                                                    PrivateData->FfsHeader,
                                                    &ImageAddress,
                                                    &ImageSize,
                                                    &EntryPoint
                                                    );
    if (EFI_ERROR (Status)) {
      return EFI_SUCCESS;
    }
    //
    // Set the flag in the loaded image that indicates the PEIM is executing
    // from memory.
    //
#ifdef EFI_NT_EMULATOR
    //
    // For NT32, we should also relocate image here, because if the DLL
    // is already load, we will NOT load it twice. This feature is added to
    // prevent loading driver twice in DXE phase cause system crash.
    //
    * (BOOLEAN *) ((UINTN) &mRunningFromMemory + (UINTN) EntryPoint - (UINTN) InstallMonoStatusCode) = TRUE;
#else
    * (BOOLEAN *) ((UINTN) &mRunningFromMemory + (UINTN) EntryPoint - (UINTN) InstallMonoStatusCode) = TRUE;
#endif
    Status = ((EFI_PEIM_ENTRY_POINT )(UINTN) EntryPoint) (PrivateData->FfsHeader, PeiServices);
    if (EFI_ERROR (Status)) {
      return EFI_SUCCESS;
    }
  }

  return EFI_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?