]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/Library/PlatformBootManagerLib/MemoryTest.c
UefiCpuPkg: Remove double \r
[mirror_edk2.git] / Nt32Pkg / Library / PlatformBootManagerLib / MemoryTest.c
1 /** @file
2 Perform the platform memory test
3
4 Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "PlatformBootManager.h"
10
11 EFI_HII_HANDLE gStringPackHandle = NULL;
12 EFI_GUID mPlatformBootManagerStringPackGuid = {
13 0x154dd51, 0x9079, 0x4a10, { 0x89, 0x5c, 0x9c, 0x7, 0x72, 0x81, 0x57, 0x88 }
14 };
15 // extern UINT8 BdsDxeStrings[];
16
17 //
18 // BDS Platform Functions
19 //
20
21 /**
22 Perform the memory test base on the memory test intensive level,
23 and update the memory resource.
24
25 @param Level The memory test intensive level.
26
27 @retval EFI_STATUS Success test all the system memory and update
28 the memory resource
29
30 **/
31 EFI_STATUS
32 PlatformBootManagerMemoryTest (
33 IN EXTENDMEM_COVERAGE_LEVEL Level
34 )
35 {
36 EFI_STATUS Status;
37 EFI_STATUS KeyStatus;
38 EFI_STATUS InitStatus;
39 EFI_STATUS ReturnStatus;
40 BOOLEAN RequireSoftECCInit;
41 EFI_GENERIC_MEMORY_TEST_PROTOCOL *GenMemoryTest;
42 UINT64 TestedMemorySize;
43 UINT64 TotalMemorySize;
44 UINTN TestPercent;
45 UINT64 PreviousValue;
46 BOOLEAN ErrorOut;
47 BOOLEAN TestAbort;
48 EFI_INPUT_KEY Key;
49 CHAR16 StrPercent[80];
50 CHAR16 *StrTotalMemory;
51 CHAR16 *Pos;
52 CHAR16 *TmpStr;
53 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Foreground;
54 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;
55 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color;
56 UINT32 TempData;
57 UINTN StrTotalMemorySize;
58
59 ReturnStatus = EFI_SUCCESS;
60 ZeroMem (&Key, sizeof (EFI_INPUT_KEY));
61
62 StrTotalMemorySize = 128;
63 Pos = AllocateZeroPool (StrTotalMemorySize);
64 ASSERT (Pos != NULL);
65
66 if (gStringPackHandle == NULL) {
67 gStringPackHandle = HiiAddPackages (
68 &mPlatformBootManagerStringPackGuid,
69 gImageHandle,
70 PlatformBootManagerLibStrings,
71 NULL
72 );
73 ASSERT (gStringPackHandle != NULL);
74 }
75
76 StrTotalMemory = Pos;
77
78 TestedMemorySize = 0;
79 TotalMemorySize = 0;
80 PreviousValue = 0;
81 ErrorOut = FALSE;
82 TestAbort = FALSE;
83
84 SetMem (&Foreground, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL), 0xff);
85 SetMem (&Background, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL), 0x0);
86 SetMem (&Color, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL), 0xff);
87
88 RequireSoftECCInit = FALSE;
89
90 Status = gBS->LocateProtocol (
91 &gEfiGenericMemTestProtocolGuid,
92 NULL,
93 (VOID **) &GenMemoryTest
94 );
95 if (EFI_ERROR (Status)) {
96 FreePool (Pos);
97 return EFI_SUCCESS;
98 }
99
100 InitStatus = GenMemoryTest->MemoryTestInit (
101 GenMemoryTest,
102 Level,
103 &RequireSoftECCInit
104 );
105 if (InitStatus == EFI_NO_MEDIA) {
106 //
107 // The PEI codes also have the relevant memory test code to check the memory,
108 // it can select to test some range of the memory or all of them. If PEI code
109 // checks all the memory, this BDS memory test will has no not-test memory to
110 // do the test, and then the status of EFI_NO_MEDIA will be returned by
111 // "MemoryTestInit". So it does not need to test memory again, just return.
112 //
113 FreePool (Pos);
114 return EFI_SUCCESS;
115 }
116
117 if (!FeaturePcdGet(PcdBootlogoOnlyEnable)) {
118 TmpStr = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_ESC_TO_SKIP_MEM_TEST), NULL);
119
120 if (TmpStr != NULL) {
121 PrintXY (10, 10, NULL, NULL, TmpStr);
122 FreePool (TmpStr);
123 }
124 } else {
125 DEBUG ((EFI_D_INFO, "Enter memory test.\n"));
126 }
127 do {
128 Status = GenMemoryTest->PerformMemoryTest (
129 GenMemoryTest,
130 &TestedMemorySize,
131 &TotalMemorySize,
132 &ErrorOut,
133 TestAbort
134 );
135 if (ErrorOut && (Status == EFI_DEVICE_ERROR)) {
136 TmpStr = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_SYSTEM_MEM_ERROR), NULL);
137 if (TmpStr != NULL) {
138 PrintXY (10, 10, NULL, NULL, TmpStr);
139 FreePool (TmpStr);
140 }
141
142 ASSERT (0);
143 }
144
145 if (!FeaturePcdGet(PcdBootlogoOnlyEnable)) {
146 TempData = (UINT32) DivU64x32 (TotalMemorySize, 16);
147 TestPercent = (UINTN) DivU64x32 (
148 DivU64x32 (MultU64x32 (TestedMemorySize, 100), 16),
149 TempData
150 );
151 if (TestPercent != PreviousValue) {
152 UnicodeValueToStringS (StrPercent, sizeof (StrPercent), 0, TestPercent, 0);
153 TmpStr = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_MEMORY_TEST_PERCENT), NULL);
154 if (TmpStr != NULL) {
155 //
156 // TmpStr size is 64, StrPercent is reserved to 16.
157 //
158 StrnCatS (
159 StrPercent,
160 sizeof (StrPercent) / sizeof (CHAR16),
161 TmpStr,
162 sizeof (StrPercent) / sizeof (CHAR16) - StrLen (StrPercent) - 1
163 );
164 PrintXY (10, 10, NULL, NULL, StrPercent);
165 FreePool (TmpStr);
166 }
167
168 TmpStr = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_PERFORM_MEM_TEST), NULL);
169 if (TmpStr != NULL) {
170 BootLogoUpdateProgress (
171 Foreground,
172 Background,
173 TmpStr,
174 Color,
175 TestPercent,
176 (UINTN) PreviousValue
177 );
178 FreePool (TmpStr);
179 }
180 }
181
182 PreviousValue = TestPercent;
183 } else {
184 DEBUG ((EFI_D_INFO, "Perform memory test (ESC to skip).\n"));
185 }
186
187 if (!PcdGetBool (PcdConInConnectOnDemand)) {
188 KeyStatus = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
189 if (!EFI_ERROR (KeyStatus) && (Key.ScanCode == SCAN_ESC)) {
190 if (!RequireSoftECCInit) {
191 if (!FeaturePcdGet(PcdBootlogoOnlyEnable)) {
192 TmpStr = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_PERFORM_MEM_TEST), NULL);
193 if (TmpStr != NULL) {
194 BootLogoUpdateProgress (
195 Foreground,
196 Background,
197 TmpStr,
198 Color,
199 100,
200 (UINTN) PreviousValue
201 );
202 FreePool (TmpStr);
203 }
204
205 PrintXY (10, 10, NULL, NULL, L"100");
206 }
207 Status = GenMemoryTest->Finished (GenMemoryTest);
208 goto Done;
209 }
210
211 TestAbort = TRUE;
212 }
213 }
214 } while (Status != EFI_NOT_FOUND);
215
216 Status = GenMemoryTest->Finished (GenMemoryTest);
217
218 Done:
219 if (!FeaturePcdGet(PcdBootlogoOnlyEnable)) {
220 UnicodeValueToStringS (StrTotalMemory, StrTotalMemorySize, COMMA_TYPE, TotalMemorySize, 0);
221 if (StrTotalMemory[0] == L',') {
222 StrTotalMemory++;
223 StrTotalMemorySize -= sizeof (CHAR16);
224 }
225
226 TmpStr = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_MEM_TEST_COMPLETED), NULL);
227 if (TmpStr != NULL) {
228 StrnCatS (
229 StrTotalMemory,
230 StrTotalMemorySize / sizeof (CHAR16),
231 TmpStr,
232 StrTotalMemorySize / sizeof (CHAR16) - StrLen (StrTotalMemory) - 1
233 );
234 FreePool (TmpStr);
235 }
236
237 PrintXY (10, 10, NULL, NULL, StrTotalMemory);
238 BootLogoUpdateProgress (
239 Foreground,
240 Background,
241 StrTotalMemory,
242 Color,
243 100,
244 (UINTN) PreviousValue
245 );
246
247 } else {
248 DEBUG ((EFI_D_INFO, "%d bytes of system memory tested OK\r\n", TotalMemorySize));
249 }
250
251 FreePool (Pos);
252 return ReturnStatus;
253 }