Add MonotonicCounter driver, which produces MonotonicCounter arch protocols
[mirror_edk2.git] / MdeModulePkg / Universal / BaseMemoryTestPei / BaseMemoryTest.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 BaseMemoryTest.c
15
16 Abstract:
17
18 The PEI memory test support
19
20 --*/
21
22 #include <BaseMemoryTest.h>
23
24 static PEI_BASE_MEMORY_TEST_PPI mPeiBaseMemoryTestPpi = { BaseMemoryTest };
25
26 static EFI_PEI_PPI_DESCRIPTOR PpiListPeiBaseMemoryTest = {
27 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
28 &gPeiBaseMemoryTestPpiGuid,
29 &mPeiBaseMemoryTestPpi
30 };
31
32 EFI_STATUS
33 EFIAPI
34 PeiBaseMemoryTestInit (
35 IN EFI_FFS_FILE_HEADER *FfsHeader,
36 IN EFI_PEI_SERVICES **PeiServices
37 )
38 /*++
39 Description:
40
41 Entry point function of BaseMemoryTestInit Peim.
42
43 Arguments:
44
45 PeiServices - General purpose services available to every PEIM.
46 FfsHeader - Ffs header pointer
47
48 Returns:
49
50 Status - Result of InstallPpi
51
52 --*/
53 {
54 EFI_STATUS Status;
55
56 Status = (**PeiServices).InstallPpi (PeiServices, &PpiListPeiBaseMemoryTest);
57
58 return Status;
59 }
60
61 EFI_STATUS
62 EFIAPI
63 BaseMemoryTest (
64 IN EFI_PEI_SERVICES **PeiServices,
65 IN PEI_BASE_MEMORY_TEST_PPI *This,
66 IN EFI_PHYSICAL_ADDRESS BeginAddress,
67 IN UINT64 MemoryLength,
68 IN PEI_MEMORY_TEST_OP Operation,
69 OUT EFI_PHYSICAL_ADDRESS *ErrorAddress
70 )
71 /*++
72 Description:
73
74 Test base memory.
75
76 Arguments:
77
78 PeiServices - General purpose services available to every PEIM.
79 This - Pei memory test PPI pointer.
80 BeginAddress - Beginning of the memory address to be checked.
81 MemoryLength - Bytes of memory range to be checked.
82 Operation - Type of memory check operation to be performed.
83 ErrorAddress - Return the address of the error memory address.
84 ErrorAddress - Address which has error when checked.
85
86 Returns:
87
88 Status - Result of InstallPpi
89
90 --*/
91 {
92 UINT32 TestPattern;
93 EFI_PHYSICAL_ADDRESS TempAddress;
94 UINT32 SpanSize;
95
96 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdStatusCodeValueMemoryTestStarted));
97
98 TestPattern = TEST_PATTERN;
99 SpanSize = 0;
100
101 //
102 // Make sure we don't try and test anything above the max physical address range
103 //
104 ASSERT (BeginAddress + MemoryLength < EFI_MAX_ADDRESS);
105
106 switch (Operation) {
107 case Extensive:
108 SpanSize = 0x4;
109 break;
110
111 case Sparse:
112 case Quick:
113 SpanSize = COVER_SPAN;
114 break;
115
116 case Ignore:
117 goto Done;
118 break;
119 }
120 //
121 // Write the test pattern into memory range
122 //
123 TempAddress = BeginAddress;
124 while (TempAddress < BeginAddress + MemoryLength) {
125 (*(UINT32 *) (UINTN) TempAddress) = TestPattern;
126 TempAddress += SpanSize;
127 }
128 //
129 // Read pattern from memory and compare it
130 //
131 TempAddress = BeginAddress;
132 while (TempAddress < BeginAddress + MemoryLength) {
133 if ((*(UINT32 *) (UINTN) TempAddress) != TestPattern) {
134 *ErrorAddress = TempAddress;
135 REPORT_STATUS_CODE (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED, PcdGet32 (PcdStatusCodeValueUncorrectableMemoryError));
136
137 return EFI_DEVICE_ERROR;
138 }
139
140 TempAddress += SpanSize;
141 }
142
143 Done:
144 return EFI_SUCCESS;
145 }