]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Sample/Platform/Generic/RuntimeDxe/StatusCode/Lib/RtMemoryStatusCode/RtMemoryStatusCode.c
Add more check to make sure code run safely.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Platform / Generic / RuntimeDxe / StatusCode / Lib / RtMemoryStatusCode / RtMemoryStatusCode.c
CommitLineData
b38907a6 1/*++\r
2\r
4ac4deb7 3Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
4b1e1121 4This program and the accompanying materials \r
b38907a6 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 RtMemoryStatusCode.c\r
15 \r
16Abstract:\r
17\r
18 EFI lib to provide memory journal status code reporting routines.\r
19\r
20--*/\r
21\r
22#include "RtMemoryStatusCode.h"\r
23\r
24//\r
25// Global variables\r
26//\r
27PEI_STATUS_CODE_MEMORY_PPI mStatusCodeMemoryPpi = { 0, 0, 0, 0 };\r
28\r
29//\r
30// Function implementations\r
31//\r
32EFI_STATUS\r
33EFIAPI\r
34RtMemoryReportStatusCode (\r
35 IN EFI_STATUS_CODE_TYPE CodeType,\r
36 IN EFI_STATUS_CODE_VALUE Value,\r
37 IN UINT32 Instance,\r
38 IN EFI_GUID * CallerId,\r
39 IN EFI_STATUS_CODE_DATA * Data OPTIONAL\r
40 )\r
41/*++\r
42\r
43Routine Description:\r
44\r
45 Log a status code to a memory journal. If no memory journal exists, \r
46 we will just return.\r
47\r
48Arguments:\r
49\r
50 Same as ReportStatusCode AP\r
51 \r
52Returns:\r
53\r
54 EFI_SUCCESS This function always returns success\r
55\r
56--*/\r
57{\r
58 EFI_STATUS_CODE_ENTRY *CurrentEntry;\r
59 UINTN MaxEntry;\r
60\r
61 //\r
62 // We don't care to log debug codes.\r
63 //\r
64 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {\r
65 return EFI_SUCCESS;\r
66 }\r
67 //\r
68 // Update the latest entry in the journal.\r
69 //\r
70 MaxEntry = mStatusCodeMemoryPpi.Length / sizeof (EFI_STATUS_CODE_ENTRY);\r
71 if (!MaxEntry) {\r
72 //\r
73 // If we don't have any entries, then we can return.\r
74 // This effectively means that no memory buffer was passed forward from PEI.\r
75 //\r
76 return EFI_SUCCESS;\r
77 }\r
78\r
79 CurrentEntry = (EFI_STATUS_CODE_ENTRY *) (UINTN) (mStatusCodeMemoryPpi.Address + (mStatusCodeMemoryPpi.LastEntry * sizeof (EFI_STATUS_CODE_ENTRY)));\r
80\r
81 mStatusCodeMemoryPpi.LastEntry = (mStatusCodeMemoryPpi.LastEntry + 1) % MaxEntry;\r
82 if (mStatusCodeMemoryPpi.LastEntry == mStatusCodeMemoryPpi.FirstEntry) {\r
83 mStatusCodeMemoryPpi.FirstEntry = (mStatusCodeMemoryPpi.FirstEntry + 1) % MaxEntry;\r
84 }\r
85\r
86 CurrentEntry->Type = CodeType;\r
87 CurrentEntry->Value = Value;\r
88 CurrentEntry->Instance = Instance;\r
89\r
90 return EFI_SUCCESS;\r
91}\r
92\r
93VOID\r
94EFIAPI\r
95RtMemoryInitializeStatusCode (\r
96 IN EFI_HANDLE ImageHandle,\r
97 IN EFI_SYSTEM_TABLE *SystemTable\r
98 )\r
99/*++\r
100\r
101Routine Description:\r
102\r
103 Initialization routine.\r
104 Allocates heap space for storing Status Codes.\r
105 Installs a PPI to point to that heap space.\r
106 Installs a callback to switch to memory.\r
107 Installs a callback to \r
108\r
109Arguments: \r
110\r
111 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
112\r
113Returns: \r
114\r
115 None\r
116\r
117--*/\r
118{\r
119 EFI_STATUS Status;\r
120 VOID *HobList;\r
121 PEI_STATUS_CODE_MEMORY_PPI **StatusCodeMemoryPpi;\r
122\r
123 //\r
124 // Locate the HOB that contains the PPI structure for the memory journal\r
125 // We don't check for more than one.\r
126 //\r
4ac4deb7 127 StatusCodeMemoryPpi = NULL;\r
b38907a6 128 EfiLibGetSystemConfigurationTable (\r
129 &gEfiHobListGuid,\r
130 &HobList\r
131 );\r
132 Status = GetNextGuidHob (\r
133 &HobList,\r
134 &gPeiStatusCodeMemoryPpiGuid,\r
135 (VOID **) &StatusCodeMemoryPpi,\r
136 NULL\r
137 );\r
4ac4deb7 138 if (EFI_ERROR (Status) || (StatusCodeMemoryPpi == NULL)) {\r
b38907a6 139 return ;\r
140 }\r
141 //\r
142 // Copy data to our structure since the HOB will go away at runtime\r
143 //\r
144 // BUGBUG: Virtualize for RT\r
145 //\r
146 mStatusCodeMemoryPpi.FirstEntry = (*StatusCodeMemoryPpi)->FirstEntry;\r
147 mStatusCodeMemoryPpi.LastEntry = (*StatusCodeMemoryPpi)->LastEntry;\r
148 mStatusCodeMemoryPpi.Address = (*StatusCodeMemoryPpi)->Address;\r
149 mStatusCodeMemoryPpi.Length = (*StatusCodeMemoryPpi)->Length;\r
150}\r
151\r
152VOID\r
153EFIAPI\r
154PlaybackStatusCodes (\r
155 IN EFI_REPORT_STATUS_CODE ReportStatusCode\r
156 )\r
157/*++\r
158\r
159Routine Description:\r
160\r
161 Call the input ReportStatusCode function with every status code recorded in\r
162 the journal.\r
163\r
164Arguments: \r
165\r
166 ReportStatusCode ReportStatusCode function to call.\r
167\r
168Returns: \r
169\r
170 None\r
171\r
172--*/\r
173{\r
174 UINTN MaxEntry;\r
175 EFI_STATUS_CODE_ENTRY *CurrentEntry;\r
176 UINTN Counter;\r
177\r
178 if (ReportStatusCode == RtMemoryReportStatusCode) {\r
179 return ;\r
180 }\r
181 //\r
182 // Playback prior status codes to current listeners\r
183 //\r
184 MaxEntry = mStatusCodeMemoryPpi.Length / sizeof (EFI_STATUS_CODE_ENTRY);\r
185 for (Counter = mStatusCodeMemoryPpi.FirstEntry; Counter != mStatusCodeMemoryPpi.LastEntry; Counter++) {\r
186 //\r
187 // Check if we have to roll back to beginning of queue buffer\r
188 //\r
189 if (Counter == MaxEntry) {\r
190 Counter = 0;\r
191 }\r
192 //\r
193 // Play current entry\r
194 //\r
195 CurrentEntry = (EFI_STATUS_CODE_ENTRY *) (UINTN) (mStatusCodeMemoryPpi.Address + (Counter * sizeof (EFI_STATUS_CODE_ENTRY)));\r
196 ReportStatusCode (\r
197 CurrentEntry->Type,\r
198 CurrentEntry->Value,\r
199 CurrentEntry->Instance,\r
200 NULL,\r
201 NULL\r
202 );\r
203 }\r
204}\r