]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/WinNtAutoScanPei/WinNtAutoScan.c
Nt32Pkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Nt32Pkg / WinNtAutoScanPei / WinNtAutoScan.c
CommitLineData
6ae81428 1/**@file\r
6c25c60e 2\r
8f2a5f80 3Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>\r
4e502412 4(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
9d2eedba 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6c25c60e 6\r
7Module Name:\r
8 WinNtAutoscan.c\r
9\r
10Abstract:\r
11 This PEIM to abstract memory auto-scan in a Windows NT environment.\r
12\r
13Revision History\r
14\r
6ae81428 15**/\r
6c25c60e 16\r
17//\r
18// The package level header files this module uses\r
19//\r
20#include <PiPei.h>\r
21#include <WinNtPeim.h>\r
22//\r
23// The protocols, PPI and GUID defintions for this module\r
24//\r
6c25c60e 25#include <Ppi/NtAutoscan.h>\r
57b31029 26#include <Ppi/ReadOnlyVariable2.h>\r
27\r
28#include <Guid/MemoryTypeInformation.h>\r
29\r
6c25c60e 30//\r
31// The Library classes this module consumes\r
32//\r
33#include <Library/DebugLib.h>\r
34#include <Library/PeimEntryPoint.h>\r
35#include <Library/HobLib.h>\r
68443c61 36#include <Library/PeiServicesLib.h>\r
6c25c60e 37\r
57b31029 38EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {\r
39 { EfiReservedMemoryType, 0x0004 },\r
40 { EfiRuntimeServicesCode, 0x0040 },\r
41 { EfiRuntimeServicesData, 0x0040 },\r
42 { EfiBootServicesCode, 0x0300 },\r
43 { EfiBootServicesData, 0x1000 },\r
44 { EfiMaxMemoryType, 0 }\r
45};\r
46\r
a95b6045
ED
47/**\r
48 Validate variable data for the MemoryTypeInformation. \r
49\r
50 @param MemoryData Variable data.\r
51 @param MemoryDataSize Variable data length.\r
52 \r
53 @return TRUE The variable data is valid.\r
54 @return FALSE The variable data is invalid.\r
55\r
56**/\r
57BOOLEAN\r
58ValidateMemoryTypeInfoVariable (\r
59 IN EFI_MEMORY_TYPE_INFORMATION *MemoryData,\r
60 IN UINTN MemoryDataSize\r
61 )\r
62{\r
63 UINTN Count;\r
64 UINTN Index;\r
65\r
66 // Check the input parameter.\r
67 if (MemoryData == NULL) {\r
68 return FALSE;\r
69 }\r
70\r
71 // Get Count\r
72 Count = MemoryDataSize / sizeof (*MemoryData);\r
73\r
74 // Check Size\r
75 if (Count * sizeof(*MemoryData) != MemoryDataSize) {\r
76 return FALSE;\r
77 }\r
78\r
79 // Check last entry type filed.\r
80 if (MemoryData[Count - 1].Type != EfiMaxMemoryType) {\r
81 return FALSE;\r
82 }\r
83\r
84 // Check the type filed.\r
85 for (Index = 0; Index < Count - 1; Index++) {\r
86 if (MemoryData[Index].Type >= EfiMaxMemoryType) {\r
87 return FALSE;\r
88 }\r
89 }\r
90\r
91 return TRUE;\r
92}\r
93\r
6c25c60e 94EFI_STATUS\r
95EFIAPI\r
96PeimInitializeWinNtAutoScan (\r
68443c61 97 IN EFI_PEI_FILE_HANDLE FileHandle,\r
98 IN CONST EFI_PEI_SERVICES **PeiServices\r
6c25c60e 99 )\r
100/*++\r
101\r
102Routine Description:\r
103 Perform a call-back into the SEC simulator to get a memory value\r
104\r
105Arguments:\r
106 FfsHeader - General purpose data available to every PEIM\r
107 PeiServices - General purpose services available to every PEIM.\r
108 \r
109Returns:\r
110 None\r
111\r
112--*/\r
113{\r
114 EFI_STATUS Status;\r
115 EFI_PEI_PPI_DESCRIPTOR *PpiDescriptor;\r
116 PEI_NT_AUTOSCAN_PPI *PeiNtService;\r
117 UINT64 MemorySize;\r
118 EFI_PHYSICAL_ADDRESS MemoryBase;\r
6c25c60e 119 UINTN Index;\r
120 EFI_RESOURCE_ATTRIBUTE_TYPE Attributes;\r
57b31029 121 EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable;\r
122 UINTN DataSize;\r
123 EFI_MEMORY_TYPE_INFORMATION MemoryData [EfiMaxMemoryType + 1];\r
6c25c60e 124\r
125\r
126 DEBUG ((EFI_D_ERROR, "NT 32 Autoscan PEIM Loaded\n"));\r
127\r
128 //\r
129 // Get the PEI NT Autoscan PPI\r
130 //\r
68443c61 131 Status = PeiServicesLocatePpi (\r
132 &gPeiNtAutoScanPpiGuid, // GUID\r
133 0, // INSTANCE\r
134 &PpiDescriptor, // EFI_PEI_PPI_DESCRIPTOR\r
135 (VOID**)&PeiNtService // PPI\r
136 );\r
6c25c60e 137 ASSERT_EFI_ERROR (Status);\r
138\r
6c25c60e 139 Index = 0;\r
140 do {\r
141 Status = PeiNtService->NtAutoScan (Index, &MemoryBase, &MemorySize);\r
142 if (!EFI_ERROR (Status)) {\r
143 Attributes =\r
144 (\r
145 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
146 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
147 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
148 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
149 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
150 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
151 );\r
152\r
153 if (Index == 0) {\r
154 //\r
d73be1d1 155 // Register the memory with the PEI Core\r
6c25c60e 156 //\r
68443c61 157 Status = PeiServicesInstallPeiMemory (MemoryBase, MemorySize);\r
6c25c60e 158 ASSERT_EFI_ERROR (Status);\r
159\r
160 Attributes |= EFI_RESOURCE_ATTRIBUTE_TESTED;\r
161 }\r
162 \r
163 BuildResourceDescriptorHob (\r
164 EFI_RESOURCE_SYSTEM_MEMORY,\r
165 Attributes,\r
166 MemoryBase,\r
167 MemorySize\r
168 );\r
169 }\r
170 Index++;\r
171 } while (!EFI_ERROR (Status));\r
172\r
173 //\r
4e502412 174 // Build the CPU hob with 52-bit addressing and 16-bits of IO space.\r
6c25c60e 175 //\r
4e502412 176 BuildCpuHob (52, 16);\r
57b31029 177\r
178 //\r
179 // Build GUIDed Hob that contains the Memory Type Information array\r
180 //\r
181 Status = PeiServicesLocatePpi (\r
182 &gEfiPeiReadOnlyVariable2PpiGuid,\r
183 0,\r
184 NULL,\r
185 (VOID **)&Variable\r
186 );\r
187 ASSERT_EFI_ERROR (Status);\r
188\r
189 DataSize = sizeof (MemoryData);\r
190 Status = Variable->GetVariable (\r
191 Variable,\r
192 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
193 &gEfiMemoryTypeInformationGuid,\r
194 NULL,\r
195 &DataSize,\r
196 &MemoryData\r
197 );\r
a95b6045 198 if (EFI_ERROR (Status) || !ValidateMemoryTypeInfoVariable(MemoryData, DataSize)) {\r
57b31029 199 //\r
200 // Create Memory Type Information HOB\r
201 //\r
202 BuildGuidDataHob (\r
203 &gEfiMemoryTypeInformationGuid,\r
204 mDefaultMemoryTypeInformation,\r
205 sizeof(mDefaultMemoryTypeInformation)\r
206 );\r
207 } else {\r
208 //\r
209 // Create Memory Type Information HOB\r
210 //\r
211 BuildGuidDataHob (\r
212 &gEfiMemoryTypeInformationGuid,\r
213 MemoryData,\r
214 DataSize\r
215 );\r
216 }\r
217\r
6c25c60e 218 return Status;\r
219}\r