3 Copyright (c) 2014-2017, Linaro Limited. All rights reserved.
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/DebugLib.h>
17 #include <Library/PcdLib.h>
22 QemuVirtMemInfoPeiLibConstructor (
28 UINT64 NewBase
, CurBase
;
29 UINT64 NewSize
, CurSize
;
32 CONST UINT64
*RegProp
;
33 RETURN_STATUS PcdStatus
;
38 DeviceTreeBase
= (VOID
*)(UINTN
)PcdGet64 (PcdDeviceTreeInitialBaseAddress
);
39 ASSERT (DeviceTreeBase
!= NULL
);
42 // Make sure we have a valid device tree blob
44 ASSERT (fdt_check_header (DeviceTreeBase
) == 0);
47 // Look for the lowest memory node
49 for (Prev
= 0;; Prev
= Node
) {
50 Node
= fdt_next_node (DeviceTreeBase
, Prev
, NULL
);
56 // Check for memory node
58 Type
= fdt_getprop (DeviceTreeBase
, Node
, "device_type", &Len
);
59 if (Type
&& AsciiStrnCmp (Type
, "memory", Len
) == 0) {
61 // Get the 'reg' property of this node. For now, we will assume
62 // two 8 byte quantities for base and size, respectively.
64 RegProp
= fdt_getprop (DeviceTreeBase
, Node
, "reg", &Len
);
65 if (RegProp
!= 0 && Len
== (2 * sizeof (UINT64
))) {
67 CurBase
= fdt64_to_cpu (ReadUnaligned64 (RegProp
));
68 CurSize
= fdt64_to_cpu (ReadUnaligned64 (RegProp
+ 1));
70 DEBUG ((DEBUG_INFO
, "%a: System RAM @ 0x%lx - 0x%lx\n",
71 __FUNCTION__
, CurBase
, CurBase
+ CurSize
- 1));
73 if (NewBase
> CurBase
|| NewBase
== 0) {
78 DEBUG ((DEBUG_ERROR
, "%a: Failed to parse FDT memory node\n",
85 // Make sure the start of DRAM matches our expectation
87 ASSERT (FixedPcdGet64 (PcdSystemMemoryBase
) == NewBase
);
88 PcdStatus
= PcdSet64S (PcdSystemMemorySize
, NewSize
);
89 ASSERT_RETURN_ERROR (PcdStatus
);
92 // We need to make sure that the machine we are running on has at least
93 // 128 MB of memory configured, and is currently executing this binary from
94 // NOR flash. This prevents a device tree image in DRAM from getting
95 // clobbered when our caller installs permanent PEI RAM, before we have a
96 // chance of marking its location as reserved or copy it to a freshly
97 // allocated block in the permanent PEI RAM in the platform PEIM.
99 ASSERT (NewSize
>= SIZE_128MB
);
101 (((UINT64
)PcdGet64 (PcdFdBaseAddress
) +
102 (UINT64
)PcdGet32 (PcdFdSize
)) <= NewBase
) ||
103 ((UINT64
)PcdGet64 (PcdFdBaseAddress
) >= (NewBase
+ NewSize
)));
105 return RETURN_SUCCESS
;