2 Support functions for managing debug image info table when loading and unloading
5 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader
= {
20 0, // volatile UINT32 UpdateStatus;
21 0, // UINT32 TableSize;
22 NULL
// EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
25 UINTN mMaxTableEntries
= 0;
27 EFI_SYSTEM_TABLE_POINTER
*mDebugTable
= NULL
;
29 #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))
32 Creates and initializes the DebugImageInfo Table. Also creates the configuration
33 table and registers it into the system table.
37 CoreInitializeDebugImageInfoTable (
43 EFI_PHYSICAL_ADDRESS Memory
;
50 // Allocate 4M aligned page for the structure and fill in the data.
51 // Ideally we would update the CRC now as well, but the service may not yet be available.
52 // See comments in the CoreUpdateDebugTableCrc32() function below for details.
54 Pages
= EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER
));
55 AlignmentMask
= SIZE_4MB
- 1;
56 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (SIZE_4MB
);
59 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress
60 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below
63 Memory
= PcdGet64 (PcdMaxEfiSystemTablePointerAddress
);
67 Status
= CoreAllocatePages (
73 if (EFI_ERROR (Status
)) {
74 if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress
) != 0) {
75 DEBUG ((EFI_D_INFO
, "Allocate memory for EFI_SYSTEM_TABLE_POINTER below PcdMaxEfiSystemTablePointerAddress failed. \
76 Retry to allocate memroy as close to the top of memory as feasible.\n"));
79 // If the initial memory allocation fails, then reattempt allocation
80 // as close to the top of memory as feasible.
82 Status
= CoreAllocatePages (
88 ASSERT_EFI_ERROR (Status
);
89 if (EFI_ERROR (Status
)) {
95 // Free overallocated pages
97 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
98 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
)Memory
);
99 if (UnalignedPages
> 0) {
101 // Free first unaligned page(s).
103 Status
= CoreFreePages (Memory
, UnalignedPages
);
104 ASSERT_EFI_ERROR (Status
);
106 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
107 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
108 if (UnalignedPages
> 0) {
110 // Free last unaligned page(s).
112 Status
= CoreFreePages (Memory
, UnalignedPages
);
113 ASSERT_EFI_ERROR (Status
);
117 // Set mDebugTable to the 4MB aligned allocated pages
119 mDebugTable
= (EFI_SYSTEM_TABLE_POINTER
*)(AlignedMemory
);
120 ASSERT (mDebugTable
!= NULL
);
123 // Initialize EFI_SYSTEM_TABLE_POINTER structure
125 mDebugTable
->Signature
= EFI_SYSTEM_TABLE_SIGNATURE
;
126 mDebugTable
->EfiSystemTableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) gDxeCoreST
;
127 mDebugTable
->Crc32
= 0;
130 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System
131 // Configuration Table
133 Status
= CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid
, &mDebugInfoTableHeader
);
134 ASSERT_EFI_ERROR (Status
);
139 Update the CRC32 in the Debug Table.
140 Since the CRC32 service is made available by the Runtime driver, we have to
141 wait for the Runtime Driver to be installed before the CRC32 can be computed.
142 This function is called elsewhere by the core when the runtime architectural
143 protocol is produced.
147 CoreUpdateDebugTableCrc32 (
151 ASSERT(mDebugTable
!= NULL
);
152 mDebugTable
->Crc32
= 0;
153 gBS
->CalculateCrc32 ((VOID
*)mDebugTable
, sizeof (EFI_SYSTEM_TABLE_POINTER
), &mDebugTable
->Crc32
);
158 Adds a new DebugImageInfo structure to the DebugImageInfo Table. Re-Allocates
159 the table if it's not large enough to accomidate another entry.
161 @param ImageInfoType type of debug image information
162 @param LoadedImage pointer to the loaded image protocol for the image being
164 @param ImageHandle image handle for the image being loaded
168 CoreNewDebugImageInfoEntry (
169 IN UINT32 ImageInfoType
,
170 IN EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
,
171 IN EFI_HANDLE ImageHandle
174 EFI_DEBUG_IMAGE_INFO
*Table
;
175 EFI_DEBUG_IMAGE_INFO
*NewTable
;
180 // Set the flag indicating that we're in the process of updating the table.
182 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
184 Table
= mDebugInfoTableHeader
.EfiDebugImageInfoTable
;
186 if (mDebugInfoTableHeader
.TableSize
< mMaxTableEntries
) {
188 // We still have empty entires in the Table, find the first empty entry.
191 while (Table
[Index
].NormalImage
!= NULL
) {
195 // There must be an empty entry in the in the table.
197 ASSERT (Index
< mMaxTableEntries
);
200 // Table is full, so re-allocate another page for a larger table...
202 TableSize
= mMaxTableEntries
* EFI_DEBUG_TABLE_ENTRY_SIZE
;
203 NewTable
= AllocateZeroPool (TableSize
+ EFI_PAGE_SIZE
);
204 if (NewTable
== NULL
) {
205 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
209 // Copy the old table into the new one
211 CopyMem (NewTable
, Table
, TableSize
);
213 // Free the old table
215 CoreFreePool (Table
);
217 // Update the table header
220 mDebugInfoTableHeader
.EfiDebugImageInfoTable
= NewTable
;
222 // Enlarge the max table entries and set the first empty entry index to
223 // be the original max table entries.
225 Index
= mMaxTableEntries
;
226 mMaxTableEntries
+= EFI_PAGE_SIZE
/ EFI_DEBUG_TABLE_ENTRY_SIZE
;
230 // Allocate data for new entry
232 Table
[Index
].NormalImage
= AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL
));
233 if (Table
[Index
].NormalImage
!= NULL
) {
237 Table
[Index
].NormalImage
->ImageInfoType
= (UINT32
) ImageInfoType
;
238 Table
[Index
].NormalImage
->LoadedImageProtocolInstance
= LoadedImage
;
239 Table
[Index
].NormalImage
->ImageHandle
= ImageHandle
;
241 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
243 mDebugInfoTableHeader
.TableSize
++;
244 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED
;
246 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
252 Removes and frees an entry from the DebugImageInfo Table.
254 @param ImageHandle image handle for the image being unloaded
258 CoreRemoveDebugImageInfoEntry (
259 EFI_HANDLE ImageHandle
262 EFI_DEBUG_IMAGE_INFO
*Table
;
265 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
267 Table
= mDebugInfoTableHeader
.EfiDebugImageInfoTable
;
269 for (Index
= 0; Index
< mMaxTableEntries
; Index
++) {
270 if (Table
[Index
].NormalImage
!= NULL
&& Table
[Index
].NormalImage
->ImageHandle
== ImageHandle
) {
272 // Found a match. Free up the record, then NULL the pointer to indicate the slot
275 CoreFreePool (Table
[Index
].NormalImage
);
276 Table
[Index
].NormalImage
= NULL
;
278 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
280 mDebugInfoTableHeader
.TableSize
--;
281 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED
;
285 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;