]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/ArmPlatformGlobalVariableLib/PrePi/PrePiArmPlatformGlobalVariableLib.c
db69ba047dbdcae3805dbfb5284b347725731bc6
[mirror_edk2.git] / ArmPlatformPkg / Library / ArmPlatformGlobalVariableLib / PrePi / PrePiArmPlatformGlobalVariableLib.c
1 /** @file
2 *
3 * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
4 *
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
9 *
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.
12 *
13 **/
14
15 #include <Uefi.h>
16 #include <Library/ArmPlatformGlobalVariableLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/DebugLib.h>
21
22 extern UINT64 mSystemMemoryEnd;
23
24 #define IS_XIP() (((UINT32)PcdGet64 (PcdFdBaseAddress) > (UINT32)(mSystemMemoryEnd)) || \
25 ((PcdGet64 (PcdFdBaseAddress) + PcdGet32 (PcdFdSize)) < PcdGet64 (PcdSystemMemoryBase)))
26
27 // Declared by ArmPlatformPkg/PrePi Module
28 extern UINTN mGlobalVariableBase;
29
30 VOID
31 ArmPlatformGetGlobalVariable (
32 IN UINTN VariableOffset,
33 IN UINTN VariableSize,
34 OUT VOID* Variable
35 )
36 {
37 UINTN GlobalVariableBase;
38
39 // Ensure the Global Variable Size have been initialized
40 ASSERT (VariableOffset < PcdGet32 (PcdPeiGlobalVariableSize));
41
42 if (IS_XIP()) {
43 // In Case of XIP, we expect the Primary Stack at the top of the System Memory
44 // The size must be 64bit aligned to allow 64bit variable to be aligned
45 GlobalVariableBase = mSystemMemoryEnd + 1 - ALIGN_VALUE (PcdGet32 (PcdPeiGlobalVariableSize), 0x8);
46 } else {
47 GlobalVariableBase = mGlobalVariableBase;
48 }
49
50 if (VariableSize == 4) {
51 *(UINT32*)Variable = ReadUnaligned32 ((CONST UINT32*)(GlobalVariableBase + VariableOffset));
52 } else if (VariableSize == 8) {
53 *(UINT64*)Variable = ReadUnaligned64 ((CONST UINT64*)(GlobalVariableBase + VariableOffset));
54 } else {
55 CopyMem (Variable, (VOID*)(GlobalVariableBase + VariableOffset), VariableSize);
56 }
57 }
58
59 VOID
60 ArmPlatformSetGlobalVariable (
61 IN UINTN VariableOffset,
62 IN UINTN VariableSize,
63 OUT VOID* Variable
64 )
65 {
66 UINTN GlobalVariableBase;
67
68 // Ensure the Global Variable Size have been initialized
69 ASSERT (VariableOffset < PcdGet32 (PcdPeiGlobalVariableSize));
70
71 if (IS_XIP()) {
72 // In Case of XIP, we expect the Primary Stack at the top of the System Memory
73 // The size must be 64bit aligned to allow 64bit variable to be aligned
74 GlobalVariableBase = mSystemMemoryEnd + 1 - ALIGN_VALUE (PcdGet32 (PcdPeiGlobalVariableSize), 0x8);
75 } else {
76 GlobalVariableBase = mGlobalVariableBase;
77 }
78
79 if (VariableSize == 4) {
80 WriteUnaligned32 ((UINT32*)(GlobalVariableBase + VariableOffset), *(UINT32*)Variable);
81 } else if (VariableSize == 8) {
82 WriteUnaligned64 ((UINT64*)(GlobalVariableBase + VariableOffset), *(UINT64*)Variable);
83 } else {
84 CopyMem ((VOID*)(GlobalVariableBase + VariableOffset), Variable, VariableSize);
85 }
86 }
87
88 VOID*
89 ArmPlatformGetGlobalVariableAddress (
90 IN UINTN VariableOffset
91 )
92 {
93 UINTN GlobalVariableBase;
94
95 // Ensure the Global Variable Size have been initialized
96 ASSERT (VariableOffset < PcdGet32 (PcdPeiGlobalVariableSize));
97
98 if (IS_XIP()) {
99 // In Case of XIP, we expect the Primary Stack at the top of the System Memory
100 // The size must be 64bit aligned to allow 64bit variable to be aligned
101 GlobalVariableBase = mSystemMemoryEnd + 1 - ALIGN_VALUE (PcdGet32 (PcdPeiGlobalVariableSize), 0x8);
102 } else {
103 GlobalVariableBase = mGlobalVariableBase;
104 }
105
106 return (VOID*)(GlobalVariableBase + VariableOffset);
107 }