<commit_before>/** @file
  EFI PEI Core PPI services
  
Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
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 "PeiMain.h"

/**

  Initialize PPI services.

  @param PrivateData     Pointer to the PEI Core data.
  @param OldCoreData     Pointer to old PEI Core data. 
                         NULL if being run in non-permament memory mode.

**/
VOID
InitializePpiServices (
  IN PEI_CORE_INSTANCE *PrivateData,
  IN PEI_CORE_INSTANCE *OldCoreData
  )
{
  if (OldCoreData == NULL) {
    PrivateData->PpiData.NotifyListEnd = PcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
    PrivateData->PpiData.DispatchListEnd = PcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
    PrivateData->PpiData.LastDispatchedNotify = PcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
  }
}

/**

  Migrate Single PPI Pointer from the temporary memory to PEI installed memory.

  @param PpiPointer      Pointer to Ppi
  @param TempBottom      Base of old temporary memory
  @param TempTop         Top of old temporary memory
  @param Offset          Offset of new memory to old temporary memory.
  @param OffsetPositive  Positive flag of Offset value. 

**/
VOID
ConverSinglePpiPointer (
  IN PEI_PPI_LIST_POINTERS *PpiPointer,
  IN UINTN                 TempBottom,
  IN UINTN                 TempTop,
  IN UINTN                 Offset,
  IN BOOLEAN               OffsetPositive
  )
{
  if (((UINTN)PpiPointer->Raw < TempTop) &&
      ((UINTN)PpiPointer->Raw >= TempBottom)) {
    //
    // Convert the pointer to the PPI descriptor from the old TempRam
    // to the relocated physical memory.
    //
    if (OffsetPositive) {
      PpiPointer->Raw = (VOID *) ((UINTN)PpiPointer->Raw + Offset);
    } else {
      PpiPointer->Raw = (VOID *) ((UINTN)PpiPointer->Raw - Offset);
    }

    //
    // Only when the PEIM descriptor is in the old TempRam should it be necessary
    // to try to convert the pointers in the PEIM descriptor
    //

    if (((UINTN)PpiPointer->Ppi->Guid < TempTop) &&
        ((UINTN)PpiPointer->Ppi->Guid >= TempBottom)) {
      //
      // Convert the pointer to the GUID in the PPI or NOTIFY descriptor
      // from the old TempRam to the relocated physical memory.
      //
      if (OffsetPositive) {
        PpiPointer->Ppi->Guid = (VOID *) ((UINTN)PpiPointer->Ppi->Guid + Offset);
      } else {
        PpiPointer->Ppi->Guid = (VOID *) ((UINTN)PpiPointer->Ppi->Guid - Offset);
      }
    }

    //
    // Convert the pointer to the PPI interface structure in the PPI descriptor
    // from the old TempRam to the relocated physical memory.
    //
    if ((UINTN)PpiPointer->Ppi->Ppi < TempTop &&
        (UINTN)PpiPointer->Ppi->Ppi >= TempBottom) {
      if (OffsetPositive) {
        PpiPointer->Ppi->Ppi = (VOID *) ((UINTN)PpiPointer->Ppi->Ppi + Offset);
      } else {
        PpiPointer->Ppi->Ppi = (VOID *) ((UINTN)PpiPointer->Ppi->Ppi - Offset);
      }
    }
  }
}

/**

  Migrate PPI Pointers from the temporary memory stack to PEI installed memory.

  @param SecCoreData     Points to a data structure containing SEC to PEI handoff data, such as the size 
                         and location of temporary RAM, the stack location and the BFV location.
  @param PrivateData     Pointer to PeiCore's private data structure.

**/
VOID
ConvertPpiPointers (
  IN CONST EFI_SEC_PEI_HAND_OFF  *SecCoreData,
  IN PEI_CORE_INSTANCE           *PrivateData
  )
{
  UINT8                 Index;
  UINT8                 IndexHole;

  for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxPpiSupported); Index++) {
    if (Index < PrivateData->PpiData.PpiListEnd || Index > PrivateData->PpiData.NotifyListEnd) {
      //
      // Convert PPI pointer in old Heap
      //
      ConverSinglePpiPointer (
        &PrivateData->PpiData.PpiListPtrs[Index],
        (UINTN)SecCoreData->PeiTemporaryRamBase,
        (UINTN)SecCoreData->PeiTemporaryRamBase + SecCoreData->PeiTemporaryRamSize,
        PrivateData->HeapOffset,
        PrivateData->HeapOffsetPositive
        );
        
      //
      // Convert PPI pointer in old Stack
      //
      ConverSinglePpiPointer (
        &PrivateData->PpiData.PpiListPtrs[Index],
        (UINTN)SecCoreData->StackBase,
        (UINTN)SecCoreData->StackBase + SecCoreData->StackSize,
        PrivateData->StackOffset,
        PrivateData->StackOffsetPositive
        );
        
      //
      // Convert PPI pointer in old TempRam Hole
      //
      for (IndexHole = 0; IndexHole < HOLE_MAX_NUMBER; IndexHole ++) {
        if (PrivateData->HoleData[IndexHole].Size == 0) {
          continue;
        }
        
        ConverSinglePpiPointer (
          &PrivateData->PpiData.PpiListPtrs[Index],
          (UINTN)PrivateData->HoleData[IndexHole].Base,
          (UINTN)PrivateData->HoleData[IndexHole].Base + PrivateData->HoleData[IndexHole].Size,
          PrivateData->HoleData[IndexHole].Offset,
          PrivateData->HoleData[IndexHole].OffsetPositive
          );
      }
    }
  }<commit_msg>MdeModulePkg/PeiCore: Fix ConverSinglePpiPointer () typo.<commit_after>/** @file
  EFI PEI Core PPI services
  
Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
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 "PeiMain.h"

/**

  Initialize PPI services.

  @param PrivateData     Pointer to the PEI Core data.
  @param OldCoreData     Pointer to old PEI Core data. 
                         NULL if being run in non-permament memory mode.

**/
VOID
InitializePpiServices (
  IN PEI_CORE_INSTANCE *PrivateData,
  IN PEI_CORE_INSTANCE *OldCoreData
  )
{
  if (OldCoreData == NULL) {
    PrivateData->PpiData.NotifyListEnd = PcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
    PrivateData->PpiData.DispatchListEnd = PcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
    PrivateData->PpiData.LastDispatchedNotify = PcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
  }
}

/**

  Migrate Single PPI Pointer from the temporary memory to PEI installed memory.

  @param PpiPointer      Pointer to Ppi
  @param TempBottom      Base of old temporary memory
  @param TempTop         Top of old temporary memory
  @param Offset          Offset of new memory to old temporary memory.
  @param OffsetPositive  Positive flag of Offset value. 

**/
VOID
ConvertSinglePpiPointer (
  IN PEI_PPI_LIST_POINTERS *PpiPointer,
  IN UINTN                 TempBottom,
  IN UINTN                 TempTop,
  IN UINTN                 Offset,
  IN BOOLEAN               OffsetPositive
  )
{
  if (((UINTN)PpiPointer->Raw < TempTop) &&
      ((UINTN)PpiPointer->Raw >= TempBottom)) {
    //
    // Convert the pointer to the PPI descriptor from the old TempRam
    // to the relocated physical memory.
    //
    if (OffsetPositive) {
      PpiPointer->Raw = (VOID *) ((UINTN)PpiPointer->Raw + Offset);
    } else {
      PpiPointer->Raw = (VOID *) ((UINTN)PpiPointer->Raw - Offset);
    }

    //
    // Only when the PEIM descriptor is in the old TempRam should it be necessary
    // to try to convert the pointers in the PEIM descriptor
    //

    if (((UINTN)PpiPointer->Ppi->Guid < TempTop) &&
        ((UINTN)PpiPointer->Ppi->Guid >= TempBottom)) {
      //
      // Convert the pointer to the GUID in the PPI or NOTIFY descriptor
      // from the old TempRam to the relocated physical memory.
      //
      if (OffsetPositive) {
        PpiPointer->Ppi->Guid = (VOID *) ((UINTN)PpiPointer->Ppi->Guid + Offset);
      } else {
        PpiPointer->Ppi->Guid = (VOID *) ((UINTN)PpiPointer->Ppi->Guid - Offset);
      }
    }

    //
    // Convert the pointer to the PPI interface structure in the PPI descriptor
    // from the old TempRam to the relocated physical memory.
    //
    if ((UINTN)PpiPointer->Ppi->Ppi < TempTop &&
        (UINTN)PpiPointer->Ppi->Ppi >= TempBottom) {
      if (OffsetPositive) {
        PpiPointer->Ppi->Ppi = (VOID *) ((UINTN)PpiPointer->Ppi->Ppi + Offset);
      } else {
        PpiPointer->Ppi->Ppi = (VOID *) ((UINTN)PpiPointer->Ppi->Ppi - Offset);
      }
    }
  }
}

/**

  Migrate PPI Pointers from the temporary memory stack to PEI installed memory.

  @param SecCoreData     Points to a data structure containing SEC to PEI handoff data, such as the size 
                         and location of temporary RAM, the stack location and the BFV location.
  @param PrivateData     Pointer to PeiCore's private data structure.

**/
VOID
ConvertPpiPointers (
  IN CONST EFI_SEC_PEI_HAND_OFF  *SecCoreData,
  IN PEI_CORE_INSTANCE           *PrivateData
  )
{
  UINT8                 Index;
  UINT8                 IndexHole;

  for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxPpiSupported); Index++) {
    if (Index < PrivateData->PpiData.PpiListEnd || Index > PrivateData->PpiData.NotifyListEnd) {
      //
      // Convert PPI pointer in old Heap
      //
      ConvertSinglePpiPointer (
        &PrivateData->PpiData.PpiListPtrs[Index],
        (UINTN)SecCoreData->PeiTemporaryRamBase,
        (UINTN)SecCoreData->PeiTemporaryRamBase + SecCoreData->PeiTemporaryRamSize,
        PrivateData->HeapOffset,
        PrivateData->HeapOffsetPositive
        );
        
      //
      // Convert PPI pointer in old Stack
      //
      ConvertSinglePpiPointer (
        &PrivateData->PpiData.PpiListPtrs[Index],
        (UINTN)SecCoreData->StackBase,
        (UINTN)SecCoreData->StackBase + SecCoreData->StackSize,
        PrivateData->StackOffset,
        PrivateData->StackOffsetPositive
        );
        
      //
      // Convert PPI pointer in old TempRam Hole
      //
      for (IndexHole = 0; IndexHole < HOLE_MAX_NUMBER; IndexHole ++) {
        if (PrivateData->HoleData[IndexHole].Size == 0) {
          continue;
        }
        
        ConvertSinglePpiPointer (
          &PrivateData->PpiData.PpiListPtrs[Index],
          (UINTN)PrivateData->HoleData[IndexHole].Base,
          (UINTN)PrivateData->HoleData[IndexHole].Base + PrivateData->HoleData[IndexHole].Size,
          PrivateData->HoleData[IndexHole].Offset,
          PrivateData->HoleData[IndexHole].OffsetPositive
          );
      }
    }
  }
{'fraction_non_alphanumeric': 0.09644717261904762, 'fraction_numerical': 0.004464285714285714, 'mean_word_length': 2.5060319530485815, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6622139', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3252, 'n_words': 889}