Add BaseMemoryTestDxe in MdeModulePkg.dsc
[mirror_edk2.git] / MdeModulePkg / Universal / BaseMemoryTestPei / BaseMemoryTest.c
CommitLineData
ce2f5557 1/*++\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
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\r
14 BaseMemoryTest.c\r
15 \r
16Abstract:\r
17 \r
18 The PEI memory test support\r
19\r
20--*/\r
21\r
22//\r
23// The package level header files this module uses\r
24//\r
25#include <PiPei.h>\r
26#include <FrameworkPei.h>\r
27//\r
28// The protocols, PPI and GUID defintions for this module\r
29//\r
30#include <Ppi/BaseMemoryTest.h>\r
31//\r
32// The Library classes this module consumes\r
33//\r
34#include <Library/DebugLib.h>\r
35#include <Library/PeimEntryPoint.h>\r
36#include <Library/ReportStatusCodeLib.h>\r
37\r
38#include <BaseMemoryTest.h>\r
39\r
40static PEI_BASE_MEMORY_TEST_PPI mPeiBaseMemoryTestPpi = { BaseMemoryTest };\r
41\r
42static EFI_PEI_PPI_DESCRIPTOR PpiListPeiBaseMemoryTest = {\r
43 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
44 &gPeiBaseMemoryTestPpiGuid,\r
45 &mPeiBaseMemoryTestPpi\r
46};\r
47\r
48EFI_STATUS\r
49EFIAPI\r
50PeiBaseMemoryTestInit (\r
51 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
52 IN EFI_PEI_SERVICES **PeiServices\r
53 )\r
54/*++\r
55Description:\r
56\r
57 Entry point function of BaseMemoryTestInit Peim.\r
58\r
59Arguments:\r
60\r
61 PeiServices - General purpose services available to every PEIM.\r
62 FfsHeader - Ffs header pointer\r
63\r
64Returns:\r
65\r
66 Status - Result of InstallPpi\r
67\r
68--*/ \r
69{\r
70 EFI_STATUS Status;\r
71\r
72 Status = (**PeiServices).InstallPpi (PeiServices, &PpiListPeiBaseMemoryTest);\r
73\r
74 return Status;\r
75}\r
76\r
77EFI_STATUS\r
78EFIAPI\r
79BaseMemoryTest (\r
80 IN EFI_PEI_SERVICES **PeiServices,\r
81 IN PEI_BASE_MEMORY_TEST_PPI *This,\r
82 IN EFI_PHYSICAL_ADDRESS BeginAddress,\r
83 IN UINT64 MemoryLength,\r
84 IN PEI_MEMORY_TEST_OP Operation,\r
85 OUT EFI_PHYSICAL_ADDRESS *ErrorAddress\r
86 )\r
87/*++\r
88Description:\r
89\r
90 Test base memory.\r
91\r
92Arguments:\r
93\r
94 PeiServices - General purpose services available to every PEIM.\r
95 This - Pei memory test PPI pointer.\r
96 BeginAddress - Beginning of the memory address to be checked.\r
97 MemoryLength - Bytes of memory range to be checked.\r
98 Operation - Type of memory check operation to be performed.\r
99 ErrorAddress - Return the address of the error memory address.\r
100 ErrorAddress - Address which has error when checked.\r
101\r
102Returns:\r
103\r
104 Status - Result of InstallPpi\r
105\r
106--*/ \r
107{\r
108 UINT32 TestPattern;\r
109 EFI_PHYSICAL_ADDRESS TempAddress;\r
110 UINT32 SpanSize;\r
111\r
112 REPORT_STATUS_CODE (\r
113 EFI_PROGRESS_CODE,\r
114 EFI_COMPUTING_UNIT_MEMORY + EFI_CU_MEMORY_PC_TEST\r
115 );\r
116\r
117 TestPattern = TEST_PATTERN;\r
118 SpanSize = 0;\r
119\r
120 //\r
121 // Make sure we don't try and test anything above the max physical address range\r
122 //\r
123 ASSERT (BeginAddress + MemoryLength < EFI_MAX_ADDRESS);\r
124\r
125 switch (Operation) {\r
126 case Extensive:\r
127 SpanSize = 0x4;\r
128 break;\r
129\r
130 case Sparse:\r
131 case Quick:\r
132 SpanSize = COVER_SPAN;\r
133 break;\r
134\r
135 case Ignore:\r
136 goto Done;\r
137 break;\r
138 }\r
139 //\r
140 // Write the test pattern into memory range\r
141 //\r
142 TempAddress = BeginAddress;\r
143 while (TempAddress < BeginAddress + MemoryLength) {\r
144 (*(UINT32 *) (UINTN) TempAddress) = TestPattern;\r
145 TempAddress += SpanSize;\r
146 }\r
147 //\r
148 // Read pattern from memory and compare it\r
149 //\r
150 TempAddress = BeginAddress;\r
151 while (TempAddress < BeginAddress + MemoryLength) {\r
152 if ((*(UINT32 *) (UINTN) TempAddress) != TestPattern) {\r
153 *ErrorAddress = TempAddress;\r
154 REPORT_STATUS_CODE (\r
155 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r
156 EFI_COMPUTING_UNIT_MEMORY | EFI_CU_MEMORY_EC_UNCORRECTABLE\r
157 );\r
158\r
159 return EFI_DEVICE_ERROR;\r
160 }\r
161\r
162 TempAddress += SpanSize;\r
163 }\r
164\r
165Done:\r
166 return EFI_SUCCESS;\r
167}\r