]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
Code Scrub for Dxe Core.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / DebugImageInfo.c
1 /** @file
2 Support functions for managing debug image info table when loading and unloading
3 images.
4
5 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
6 All rights reserved. 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
10
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.
13
14 **/
15
16 #include <DxeMain.h>
17
18
19 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader = {
20 0, // volatile UINT32 UpdateStatus;
21 0, // UINT32 TableSize;
22 NULL // EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
23 };
24
25 EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;
26
27
28
29 /**
30 Creates and initializes the DebugImageInfo Table. Also creates the configuration
31 table and registers it into the system table.
32
33 Note:
34 This function allocates memory, frees it, and then allocates memory at an
35 address within the initial allocation. Since this function is called early
36 in DXE core initialization (before drivers are dispatched), this should not
37 be a problem.
38
39 **/
40 VOID
41 CoreInitializeDebugImageInfoTable (
42 VOID
43 )
44 {
45 EFI_STATUS Status;
46 EFI_PHYSICAL_ADDRESS Mem;
47 UINTN NumberOfPages;
48
49 //
50 // Allocate boot services memory for the structure. It's required to be aligned on
51 // a 4M boundary, so allocate a 4M block (plus what we require), free it up, calculate
52 // a 4M aligned address within the memory we just freed, and then allocate memory at that
53 // address for our initial structure.
54 //
55 NumberOfPages = FOUR_MEG_PAGES + EFI_SIZE_TO_PAGES(sizeof (EFI_SYSTEM_TABLE_POINTER));
56
57 Status = CoreAllocatePages (AllocateAnyPages, EfiBootServicesData, NumberOfPages , &Mem);
58 ASSERT_EFI_ERROR (Status);
59 if (EFI_ERROR(Status)) {
60 return;
61 }
62 Status = CoreFreePages (Mem, NumberOfPages);
63 ASSERT_EFI_ERROR (Status);
64 if (EFI_ERROR(Status)) {
65 return;
66 }
67 //
68 // Now get a 4M aligned address within the memory range we were given.
69 // Then allocate memory at that address
70 //
71 Mem = (Mem + FOUR_MEG_MASK) & (~FOUR_MEG_MASK);
72
73 Status = CoreAllocatePages (AllocateAddress, EfiBootServicesData, NumberOfPages - FOUR_MEG_PAGES, &Mem);
74 ASSERT_EFI_ERROR (Status);
75 if (EFI_ERROR(Status)) {
76 return;
77 }
78 //
79 // We now have a 4M aligned page allocated, so fill in the data structure.
80 // Ideally we would update the CRC now as well, but the service may not yet be available.
81 // See comments in the CoreUpdateDebugTableCrc32() function below for details.
82 //
83 mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(UINTN)Mem;
84 mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;
85 mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;
86 mDebugTable->Crc32 = 0;
87 Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);
88 ASSERT_EFI_ERROR (Status);
89 }
90
91
92 /**
93 Update the CRC32 in the Debug Table.
94 Since the CRC32 service is made available by the Runtime driver, we have to
95 wait for the Runtime Driver to be installed before the CRC32 can be computed.
96 This function is called elsewhere by the core when the runtime architectural
97 protocol is produced.
98
99 **/
100 VOID
101 CoreUpdateDebugTableCrc32 (
102 VOID
103 )
104 {
105 ASSERT(mDebugTable != NULL);
106 mDebugTable->Crc32 = 0;
107 gDxeCoreBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);
108 }
109
110
111 /**
112 Adds a new DebugImageInfo structure to the DebugImageInfo Table. Re-Allocates
113 the table if it's not large enough to accomidate another entry.
114
115 @param ImageInfoType type of debug image information
116 @param LoadedImage pointer to the loaded image protocol for the image being
117 loaded
118 @param ImageHandle image handle for the image being loaded
119
120 **/
121 VOID
122 CoreNewDebugImageInfoEntry (
123 IN UINT32 ImageInfoType,
124 IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,
125 IN EFI_HANDLE ImageHandle
126 )
127 {
128 EFI_DEBUG_IMAGE_INFO *Table;
129 EFI_DEBUG_IMAGE_INFO *NewTable;
130 UINTN Index;
131 UINTN MaxTableIndex;
132 UINTN TableSize;
133
134 //
135 // Set the flag indicating that we're in the process of updating the table.
136 //
137 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
138
139 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
140 MaxTableIndex = mDebugInfoTableHeader.TableSize;
141
142 for (Index = 0; Index < MaxTableIndex; Index++) {
143 if (Table[Index].NormalImage == NULL) {
144 //
145 // We have found a free entry so exit the loop
146 //
147 break;
148 }
149 }
150 if (Index == MaxTableIndex) {
151 //
152 // Table is full, so re-allocate another page for a larger table...
153 //
154 TableSize = MaxTableIndex * EFI_DEBUG_TABLE_ENTRY_SIZE;
155 NewTable = CoreAllocateZeroBootServicesPool (TableSize + EFI_PAGE_SIZE);
156 if (NewTable == NULL) {
157 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
158 return;
159 }
160 //
161 // Copy the old table into the new one
162 //
163 CopyMem (NewTable, Table, TableSize);
164 //
165 // Free the old table
166 //
167 CoreFreePool (Table);
168 //
169 // Update the table header
170 //
171 Table = NewTable;
172 mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;
173 mDebugInfoTableHeader.TableSize += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
174 }
175 //
176 // Allocate data for new entry
177 //
178 Table[Index].NormalImage = CoreAllocateZeroBootServicesPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));
179 if (Table[Index].NormalImage != NULL) {
180 //
181 // Update the entry
182 //
183 Table[Index].NormalImage->ImageInfoType = (UINT32) ImageInfoType;
184 Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;
185 Table[Index].NormalImage->ImageHandle = ImageHandle;
186 }
187 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
188 }
189
190
191
192 /**
193 Removes and frees an entry from the DebugImageInfo Table.
194
195 @param ImageHandle image handle for the image being unloaded
196
197 **/
198 VOID
199 CoreRemoveDebugImageInfoEntry (
200 EFI_HANDLE ImageHandle
201 )
202 {
203 EFI_DEBUG_IMAGE_INFO *Table;
204 UINTN Index;
205
206 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
207
208 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
209
210 for (Index = 0; Index < mDebugInfoTableHeader.TableSize; Index++) {
211 if (Table[Index].NormalImage != NULL && Table[Index].NormalImage->ImageHandle == ImageHandle) {
212 //
213 // Found a match. Free up the record, then NULL the pointer to indicate the slot
214 // is free.
215 //
216 CoreFreePool (Table[Index].NormalImage);
217 Table[Index].NormalImage = NULL;
218 break;
219 }
220 }
221 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
222 }
223
224