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