Merge "QcomModulePkg: Add wrapper function for GetRamPartitions"
diff --git a/QcomModulePkg/Include/Library/Board.h b/QcomModulePkg/Include/Library/Board.h
index 6f82e63..1d3426d 100644
--- a/QcomModulePkg/Include/Library/Board.h
+++ b/QcomModulePkg/Include/Library/Board.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2018, 2020, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -47,6 +47,8 @@
 
 #define BIT(x) (1UL << x)
 
+extern RamPartitionEntry *RamPartitionEntries;
+
 typedef enum {
   EMMC = 0,
   UFS = 1,
@@ -93,7 +95,7 @@
 UfsGetSetBootLun (UINT32 *UfsBootlun, BOOLEAN IsGet);
 BOOLEAN BoardPlatformFusion (VOID);
 UINT32 BoardPlatformRawChipId (VOID);
-EFI_STATUS GetRamPartitions (RamPartitionEntry **RamPartitions,
+EFI_STATUS ReadRamPartitions (RamPartitionEntry **RamPartitions,
                   UINT32 *NumPartitions);
 EFI_STATUS GetGranuleSize (UINT32 *MinPasrGranuleSize);
 VOID GetPageSize (UINT32 *PageSize);
diff --git a/QcomModulePkg/Library/BootLib/Board.c b/QcomModulePkg/Library/BootLib/Board.c
index 7b8329a..358ec61 100644
--- a/QcomModulePkg/Library/BootLib/Board.c
+++ b/QcomModulePkg/Library/BootLib/Board.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2018, 2020, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -41,7 +41,9 @@
         [EMMC] = "EMMC", [UFS] = "UFS", [NAND] = "NAND", [UNKNOWN] = "Unknown",
 };
 
-EFI_STATUS
+RamPartitionEntry *RamPartitionEntries = NULL;
+
+STATIC EFI_STATUS
 GetRamPartitions (RamPartitionEntry **RamPartitions, UINT32 *NumPartitions)
 {
 
@@ -80,6 +82,31 @@
 }
 
 EFI_STATUS
+ReadRamPartitions (RamPartitionEntry **RamPartitions, UINT32 *NumPartitions)
+{
+  STATIC UINT32 NumPartitionEntries = 0;
+  EFI_STATUS Status = EFI_SUCCESS;
+
+  if (RamPartitionEntries == NULL) {
+    Status = GetRamPartitions (&RamPartitionEntries, &NumPartitionEntries);
+    if (EFI_ERROR (Status)) {
+      DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n",
+              Status));
+      return Status;
+    }
+    if (!RamPartitionEntries) {
+      DEBUG ((EFI_D_ERROR, "RamPartitions is NULL\n"));
+      return EFI_NOT_FOUND;
+    }
+  }
+
+  *RamPartitions = RamPartitionEntries;
+  *NumPartitions = NumPartitionEntries;
+
+  return Status;
+}
+
+EFI_STATUS
 GetGranuleSize (UINT32 *MinPasrGranuleSize)
 {
   EFI_STATUS Status = EFI_NOT_FOUND;
@@ -120,15 +147,11 @@
   UINT64 SmallestBase;
   UINT32 i = 0;
 
-  Status = GetRamPartitions (&RamPartitions, &NumPartitions);
+  Status = ReadRamPartitions (&RamPartitions, &NumPartitions);
   if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n", Status));
+    DEBUG ((EFI_D_ERROR, "Error returned from ReadRamPartitions %r\n", Status));
     return Status;
   }
-  if (!RamPartitions) {
-    DEBUG ((EFI_D_ERROR, "RamPartitions is NULL\n"));
-    return EFI_NOT_FOUND;
-  }
   SmallestBase = RamPartitions[0].Base;
   for (i = 0; i < NumPartitions; i++) {
     if (SmallestBase > RamPartitions[i].Base)
@@ -136,7 +159,6 @@
   }
   *BaseMemory = SmallestBase;
   DEBUG ((EFI_D_INFO, "Memory Base Address: 0x%x\n", *BaseMemory));
-  FreePool (RamPartitions);
 
   return Status;
 }
diff --git a/QcomModulePkg/Library/BootLib/Rtic.c b/QcomModulePkg/Library/BootLib/Rtic.c
index b928bed..62e943e 100644
--- a/QcomModulePkg/Library/BootLib/Rtic.c
+++ b/QcomModulePkg/Library/BootLib/Rtic.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2017-2018, 2020, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -80,7 +80,6 @@
 BOOLEAN
 GetRticDtb (VOID *Dtb)
 {
-  EFI_STATUS Status = EFI_SUCCESS;
   int RootOffset;
   const char *RticProp = NULL;
   const char *MpDataProp = NULL;
@@ -90,7 +89,6 @@
   UINT8 *MpData = NULL;
   UINT32 i;
   UINT64 *MpDataAddr;
-  UINT64 BaseMemory = 0;
 
   RootOffset = fdt_path_offset (Dtb, "/");
   if (RootOffset < 0)
@@ -133,13 +131,6 @@
   }
 
   MpDataAddr = (UINT64 *)MpData;
-  Status = BaseMem (&BaseMemory);
-  if (Status != EFI_SUCCESS) {
-    DEBUG ((EFI_D_ERROR, "Base memory not found!!! Status:%r\n", Status));
-    FreePool (MpData);
-    MpData = NULL;
-    return FALSE;
-  }
 
   /* Display the RTIC id and mpdata */
   DEBUG ((EFI_D_VERBOSE, "rtic-id (%x)\n", RticData.Id));
diff --git a/QcomModulePkg/Library/BootLib/UpdateDeviceTree.c b/QcomModulePkg/Library/BootLib/UpdateDeviceTree.c
index cfd6400..349d19d 100644
--- a/QcomModulePkg/Library/BootLib/UpdateDeviceTree.c
+++ b/QcomModulePkg/Library/BootLib/UpdateDeviceTree.c
@@ -347,15 +347,11 @@
     return Status;
   }
 
-  Status = GetRamPartitions (&RamPartitions, &NumPartitions);
+  Status = ReadRamPartitions (&RamPartitions, &NumPartitions);
   if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n", Status));
+    DEBUG ((EFI_D_ERROR, "Error returned from ReadRamPartitions %r\n", Status));
     return Status;
   }
-  if (!RamPartitions) {
-    DEBUG ((EFI_D_ERROR, "RamPartitions is NULL\n"));
-    return EFI_NOT_FOUND;
-  }
 
   DEBUG ((EFI_D_INFO, "RAM Partitions\r\n"));
   for (i = 0; i < NumPartitions; i++) {
@@ -379,6 +375,7 @@
 
   FreePool (RamPartitions);
   RamPartitions = NULL;
+  RamPartitionEntries = NULL;
 
   return EFI_SUCCESS;
 }