]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
MdeModulePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / DebugImageInfo.c
... / ...
CommitLineData
1/** @file\r
2 Support functions for managing debug image info table when loading and unloading\r
3 images.\r
4\r
5Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "DxeMain.h"\r
17\r
18\r
19EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader = {\r
20 0, // volatile UINT32 UpdateStatus;\r
21 0, // UINT32 TableSize;\r
22 NULL // EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;\r
23};\r
24\r
25UINTN mMaxTableEntries = 0;\r
26\r
27EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;\r
28\r
29#define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))\r
30\r
31/**\r
32 Creates and initializes the DebugImageInfo Table. Also creates the configuration\r
33 table and registers it into the system table.\r
34\r
35**/\r
36VOID\r
37CoreInitializeDebugImageInfoTable (\r
38 VOID\r
39 )\r
40{\r
41 EFI_STATUS Status;\r
42 UINTN Pages;\r
43 EFI_PHYSICAL_ADDRESS Memory;\r
44 UINTN AlignedMemory;\r
45 UINTN AlignmentMask;\r
46 UINTN UnalignedPages;\r
47 UINTN RealPages;\r
48\r
49 //\r
50 // Allocate 4M aligned page for the structure and fill in the data.\r
51 // Ideally we would update the CRC now as well, but the service may not yet be available.\r
52 // See comments in the CoreUpdateDebugTableCrc32() function below for details.\r
53 //\r
54 Pages = EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER));\r
55 AlignmentMask = SIZE_4MB - 1;\r
56 RealPages = Pages + EFI_SIZE_TO_PAGES (SIZE_4MB);\r
57\r
58 //\r
59 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress\r
60 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below\r
61 // MAX_ADDRESS\r
62 //\r
63 Memory = PcdGet64 (PcdMaxEfiSystemTablePointerAddress);\r
64 if (Memory == 0) {\r
65 Memory = MAX_ADDRESS;\r
66 }\r
67 Status = CoreAllocatePages (\r
68 AllocateMaxAddress, \r
69 EfiBootServicesData,\r
70 RealPages, \r
71 &Memory\r
72 );\r
73 if (EFI_ERROR (Status)) {\r
74 if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress) != 0) {\r
75 DEBUG ((EFI_D_INFO, "Allocate memory for EFI_SYSTEM_TABLE_POINTER below PcdMaxEfiSystemTablePointerAddress failed. \\r
76 Retry to allocate memroy as close to the top of memory as feasible.\n"));\r
77 }\r
78 //\r
79 // If the initial memory allocation fails, then reattempt allocation\r
80 // as close to the top of memory as feasible.\r
81 //\r
82 Status = CoreAllocatePages (\r
83 AllocateAnyPages, \r
84 EfiBootServicesData,\r
85 RealPages, \r
86 &Memory\r
87 );\r
88 ASSERT_EFI_ERROR (Status);\r
89 if (EFI_ERROR (Status)) {\r
90 return;\r
91 }\r
92 } \r
93\r
94 //\r
95 // Free overallocated pages\r
96 //\r
97 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
98 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
99 if (UnalignedPages > 0) {\r
100 //\r
101 // Free first unaligned page(s).\r
102 //\r
103 Status = CoreFreePages (Memory, UnalignedPages);\r
104 ASSERT_EFI_ERROR (Status);\r
105 }\r
106 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
107 UnalignedPages = RealPages - Pages - UnalignedPages;\r
108 if (UnalignedPages > 0) {\r
109 //\r
110 // Free last unaligned page(s).\r
111 //\r
112 Status = CoreFreePages (Memory, UnalignedPages);\r
113 ASSERT_EFI_ERROR (Status);\r
114 }\r
115\r
116 //\r
117 // Set mDebugTable to the 4MB aligned allocated pages\r
118 //\r
119 mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(AlignedMemory);\r
120 ASSERT (mDebugTable != NULL);\r
121\r
122 //\r
123 // Initialize EFI_SYSTEM_TABLE_POINTER structure\r
124 // \r
125 mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;\r
126 mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;\r
127 mDebugTable->Crc32 = 0;\r
128 \r
129 //\r
130 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System \r
131 // Configuration Table\r
132 //\r
133 Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);\r
134 ASSERT_EFI_ERROR (Status);\r
135}\r
136\r
137\r
138/**\r
139 Update the CRC32 in the Debug Table.\r
140 Since the CRC32 service is made available by the Runtime driver, we have to\r
141 wait for the Runtime Driver to be installed before the CRC32 can be computed.\r
142 This function is called elsewhere by the core when the runtime architectural\r
143 protocol is produced.\r
144\r
145**/\r
146VOID\r
147CoreUpdateDebugTableCrc32 (\r
148 VOID\r
149 )\r
150{\r
151 ASSERT(mDebugTable != NULL);\r
152 mDebugTable->Crc32 = 0;\r
153 gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);\r
154}\r
155\r
156\r
157/**\r
158 Adds a new DebugImageInfo structure to the DebugImageInfo Table. Re-Allocates\r
159 the table if it's not large enough to accomidate another entry.\r
160\r
161 @param ImageInfoType type of debug image information\r
162 @param LoadedImage pointer to the loaded image protocol for the image being\r
163 loaded\r
164 @param ImageHandle image handle for the image being loaded\r
165\r
166**/\r
167VOID\r
168CoreNewDebugImageInfoEntry (\r
169 IN UINT32 ImageInfoType,\r
170 IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,\r
171 IN EFI_HANDLE ImageHandle\r
172 )\r
173{\r
174 EFI_DEBUG_IMAGE_INFO *Table;\r
175 EFI_DEBUG_IMAGE_INFO *NewTable;\r
176 UINTN Index;\r
177 UINTN TableSize;\r
178\r
179 //\r
180 // Set the flag indicating that we're in the process of updating the table.\r
181 //\r
182 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
183\r
184 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;\r
185 \r
186 if (mDebugInfoTableHeader.TableSize < mMaxTableEntries) {\r
187 //\r
188 // We still have empty entires in the Table, find the first empty entry.\r
189 //\r
190 Index = 0;\r
191 while (Table[Index].NormalImage != NULL) {\r
192 Index++;\r
193 }\r
194 //\r
195 // There must be an empty entry in the in the table.\r
196 //\r
197 ASSERT (Index < mMaxTableEntries);\r
198 } else {\r
199 //\r
200 // Table is full, so re-allocate another page for a larger table...\r
201 //\r
202 TableSize = mMaxTableEntries * EFI_DEBUG_TABLE_ENTRY_SIZE;\r
203 NewTable = AllocateZeroPool (TableSize + EFI_PAGE_SIZE);\r
204 if (NewTable == NULL) {\r
205 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
206 return;\r
207 }\r
208 //\r
209 // Copy the old table into the new one\r
210 //\r
211 CopyMem (NewTable, Table, TableSize);\r
212 //\r
213 // Free the old table\r
214 //\r
215 CoreFreePool (Table);\r
216 //\r
217 // Update the table header\r
218 //\r
219 Table = NewTable;\r
220 mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;\r
221 //\r
222 // Enlarge the max table entries and set the first empty entry index to\r
223 // be the original max table entries.\r
224 //\r
225 Index = mMaxTableEntries;\r
226 mMaxTableEntries += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;\r
227 }\r
228\r
229 //\r
230 // Allocate data for new entry\r
231 //\r
232 Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));\r
233 if (Table[Index].NormalImage != NULL) {\r
234 //\r
235 // Update the entry\r
236 //\r
237 Table[Index].NormalImage->ImageInfoType = (UINT32) ImageInfoType;\r
238 Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;\r
239 Table[Index].NormalImage->ImageHandle = ImageHandle;\r
240 //\r
241 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.\r
242 //\r
243 mDebugInfoTableHeader.TableSize++;\r
244 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;\r
245 }\r
246 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
247}\r
248\r
249\r
250\r
251/**\r
252 Removes and frees an entry from the DebugImageInfo Table.\r
253\r
254 @param ImageHandle image handle for the image being unloaded\r
255\r
256**/\r
257VOID\r
258CoreRemoveDebugImageInfoEntry (\r
259 EFI_HANDLE ImageHandle\r
260 )\r
261{\r
262 EFI_DEBUG_IMAGE_INFO *Table;\r
263 UINTN Index;\r
264\r
265 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
266\r
267 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;\r
268\r
269 for (Index = 0; Index < mMaxTableEntries; Index++) {\r
270 if (Table[Index].NormalImage != NULL && Table[Index].NormalImage->ImageHandle == ImageHandle) {\r
271 //\r
272 // Found a match. Free up the record, then NULL the pointer to indicate the slot\r
273 // is free.\r
274 //\r
275 CoreFreePool (Table[Index].NormalImage);\r
276 Table[Index].NormalImage = NULL;\r
277 //\r
278 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.\r
279 //\r
280 mDebugInfoTableHeader.TableSize--;\r
281 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;\r
282 break;\r
283 }\r
284 }\r
285 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
286}\r
287\r
288\r