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