/** @file
  Perform the platform memory test

Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2021, American Megatrends International LLC.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "BoardBdsHook.h"
#include <Protocol/GenericMemoryTest.h>

/**
  Perform the memory test base on the memory test intensive level,
  and update the memory resource.

  @param  Level         The memory test intensive level.

  @retval EFI_STATUS    Success test all the system memory and update
                        the memory resource

**/
EFI_STATUS
MemoryTest (
  IN EXTENDMEM_COVERAGE_LEVEL Level
  )
{
  EFI_STATUS                        Status;
  BOOLEAN                           RequireSoftECCInit;
  EFI_GENERIC_MEMORY_TEST_PROTOCOL  *GenMemoryTest;
  UINT64                            TestedMemorySize;
  UINT64                            TotalMemorySize;
  BOOLEAN                           ErrorOut;
  BOOLEAN                           TestAbort;

  TestedMemorySize  = 0;
  TotalMemorySize   = 0;
  ErrorOut          = FALSE;
  TestAbort         = FALSE;

  RequireSoftECCInit = FALSE;

  Status = gBS->LocateProtocol (
                  &gEfiGenericMemTestProtocolGuid,
                  NULL,
                  (VOID **) &GenMemoryTest
                  );
  if (EFI_ERROR (Status)) {
    return EFI_SUCCESS;
  }

  Status = GenMemoryTest->MemoryTestInit (
                                GenMemoryTest,
                                Level,
                                &RequireSoftECCInit
                                );
  if (Status == EFI_NO_MEDIA) {
    //
    // The PEI codes also have the relevant memory test code to check the memory,
    // it can select to test some range of the memory or all of them. If PEI code
    // checks all the memory, this BDS memory test will has no not-test memory to
    // do the test, and then the status of EFI_NO_MEDIA will be returned by
    // "MemoryTestInit". So it does not need to test memory again, just return.
    //
    return EFI_SUCCESS;
  }

  if (PcdGetBool (PcdFastBoot) == FALSE) {
    do {
      Status = GenMemoryTest->PerformMemoryTest (
                                GenMemoryTest,
                                &TestedMemorySize,
                                &TotalMemorySize,
                                &ErrorOut,
                                TestAbort
                                );
      if (ErrorOut && (Status == EFI_DEVICE_ERROR)) {
        ASSERT (0);
      }
    } while (Status != EFI_NOT_FOUND);
  }

  Status = GenMemoryTest->Finished (GenMemoryTest);

  return EFI_SUCCESS;
}
