]>
Commit | Line | Data |
---|---|---|
23c98c94 | 1 | /** @file\r |
504214c4 LG |
2 | Support functions for managing debug image info table when loading and unloading\r |
3 | images.\r | |
4 | \r | |
6eea8eae | 5 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r |
cd5ebaa0 | 6 | This program and the accompanying materials\r |
23c98c94 | 7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
28a00297 | 13 | \r |
504214c4 | 14 | **/\r |
28a00297 | 15 | \r |
9c4ac31c | 16 | #include "DxeMain.h"\r |
28a00297 | 17 | \r |
18 | \r | |
e94a9ff7 | 19 | EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader = {\r |
28a00297 | 20 | 0, // volatile UINT32 UpdateStatus;\r |
21 | 0, // UINT32 TableSize;\r | |
22 | NULL // EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;\r | |
23 | };\r | |
24 | \r | |
2f2a277d | 25 | UINTN mMaxTableEntries = 0;\r |
26 | \r | |
6eea8eae | 27 | EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;\r |
022c6d45 | 28 | \r |
ec90508b | 29 | #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))\r |
28a00297 | 30 | \r |
162ed594 | 31 | /**\r |
28a00297 | 32 | Creates and initializes the DebugImageInfo Table. Also creates the configuration\r |
33 | table and registers it into the system table.\r | |
34 | \r | |
162ed594 | 35 | **/\r |
36 | VOID\r | |
37 | CoreInitializeDebugImageInfoTable (\r | |
38 | VOID\r | |
39 | )\r | |
022c6d45 | 40 | {\r |
6eea8eae LG |
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 | |
022c6d45 | 48 | \r |
28a00297 | 49 | //\r |
19e14fc9 | 50 | // Allocate 4M aligned page for the structure and fill in the data.\r |
28a00297 | 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 | |
6eea8eae LG |
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 = (EFI_PHYSICAL_ADDRESS)(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 | |
b364eeb0 | 120 | ASSERT (mDebugTable != NULL);\r |
6eea8eae LG |
121 | \r |
122 | //\r | |
123 | // Initialize EFI_SYSTEM_TABLE_POINTER structure\r | |
124 | // \r | |
125 | mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;\r | |
28a00297 | 126 | mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;\r |
6eea8eae LG |
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 | |
28a00297 | 133 | Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);\r |
134 | ASSERT_EFI_ERROR (Status);\r | |
135 | }\r | |
136 | \r | |
28a00297 | 137 | \r |
162ed594 | 138 | /**\r |
28a00297 | 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 | |
162ed594 | 145 | **/\r |
146 | VOID\r | |
147 | CoreUpdateDebugTableCrc32 (\r | |
148 | VOID\r | |
149 | )\r | |
28a00297 | 150 | {\r |
151 | ASSERT(mDebugTable != NULL);\r | |
152 | mDebugTable->Crc32 = 0;\r | |
0e9b156d | 153 | gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);\r |
28a00297 | 154 | }\r |
155 | \r | |
162ed594 | 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 | |
022c6d45 | 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 | |
162ed594 | 164 | @param ImageHandle image handle for the image being loaded\r |
165 | \r | |
166 | **/\r | |
28a00297 | 167 | VOID\r |
168 | CoreNewDebugImageInfoEntry (\r | |
169 | IN UINT32 ImageInfoType,\r | |
170 | IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,\r | |
171 | IN EFI_HANDLE ImageHandle\r | |
172 | )\r | |
022c6d45 | 173 | {\r |
28a00297 | 174 | EFI_DEBUG_IMAGE_INFO *Table;\r |
175 | EFI_DEBUG_IMAGE_INFO *NewTable;\r | |
176 | UINTN Index;\r | |
28a00297 | 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 | |
2f2a277d | 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 | |
28a00297 | 193 | }\r |
2f2a277d | 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 | |
28a00297 | 199 | //\r |
200 | // Table is full, so re-allocate another page for a larger table...\r | |
201 | //\r | |
2f2a277d | 202 | TableSize = mMaxTableEntries * EFI_DEBUG_TABLE_ENTRY_SIZE;\r |
9c4ac31c | 203 | NewTable = AllocateZeroPool (TableSize + EFI_PAGE_SIZE);\r |
28a00297 | 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 | |
2f2a277d | 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 | |
28a00297 | 227 | }\r |
2f2a277d | 228 | \r |
28a00297 | 229 | //\r |
230 | // Allocate data for new entry\r | |
231 | //\r | |
9c4ac31c | 232 | Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));\r |
28a00297 | 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 | |
2f2a277d | 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 | |
28a00297 | 245 | }\r |
246 | mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r | |
247 | }\r | |
248 | \r | |
249 | \r | |
28a00297 | 250 | \r |
162ed594 | 251 | /**\r |
28a00297 | 252 | Removes and frees an entry from the DebugImageInfo Table.\r |
253 | \r | |
162ed594 | 254 | @param ImageHandle image handle for the image being unloaded\r |
28a00297 | 255 | \r |
162ed594 | 256 | **/\r |
257 | VOID\r | |
258 | CoreRemoveDebugImageInfoEntry (\r | |
259 | EFI_HANDLE ImageHandle\r | |
260 | )\r | |
022c6d45 | 261 | {\r |
28a00297 | 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 | |
2f2a277d | 269 | for (Index = 0; Index < mMaxTableEntries; Index++) {\r |
28a00297 | 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 | |
2f2a277d | 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 | |
28a00297 | 282 | break;\r |
283 | }\r | |
284 | }\r | |
285 | mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r | |
286 | }\r | |
287 | \r | |
162ed594 | 288 | \r |