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