]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
1. Use AllocateAlignedPages() to remove tricky logic in DebugImageInfo.c
[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 **/
34 VOID
35 CoreInitializeDebugImageInfoTable (
36 VOID
37 )
38 {
39 EFI_STATUS Status;
40
41 //
42 // Allocate 4M aligned page for the structure and fill in the data.
43 // Ideally we would update the CRC now as well, but the service may not yet be available.
44 // See comments in the CoreUpdateDebugTableCrc32() function below for details.
45 //
46 mDebugTable = AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER)), FOUR_MEG_ALIGNMENT);
47 mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;
48 mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;
49 mDebugTable->Crc32 = 0;
50 Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);
51 ASSERT_EFI_ERROR (Status);
52 }
53
54
55 /**
56 Update the CRC32 in the Debug Table.
57 Since the CRC32 service is made available by the Runtime driver, we have to
58 wait for the Runtime Driver to be installed before the CRC32 can be computed.
59 This function is called elsewhere by the core when the runtime architectural
60 protocol is produced.
61
62 **/
63 VOID
64 CoreUpdateDebugTableCrc32 (
65 VOID
66 )
67 {
68 ASSERT(mDebugTable != NULL);
69 mDebugTable->Crc32 = 0;
70 gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);
71 }
72
73
74 /**
75 Adds a new DebugImageInfo structure to the DebugImageInfo Table. Re-Allocates
76 the table if it's not large enough to accomidate another entry.
77
78 @param ImageInfoType type of debug image information
79 @param LoadedImage pointer to the loaded image protocol for the image being
80 loaded
81 @param ImageHandle image handle for the image being loaded
82
83 **/
84 VOID
85 CoreNewDebugImageInfoEntry (
86 IN UINT32 ImageInfoType,
87 IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,
88 IN EFI_HANDLE ImageHandle
89 )
90 {
91 EFI_DEBUG_IMAGE_INFO *Table;
92 EFI_DEBUG_IMAGE_INFO *NewTable;
93 UINTN Index;
94 UINTN MaxTableIndex;
95 UINTN TableSize;
96
97 //
98 // Set the flag indicating that we're in the process of updating the table.
99 //
100 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
101
102 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
103 MaxTableIndex = mDebugInfoTableHeader.TableSize;
104
105 for (Index = 0; Index < MaxTableIndex; Index++) {
106 if (Table[Index].NormalImage == NULL) {
107 //
108 // We have found a free entry so exit the loop
109 //
110 break;
111 }
112 }
113 if (Index == MaxTableIndex) {
114 //
115 // Table is full, so re-allocate another page for a larger table...
116 //
117 TableSize = MaxTableIndex * EFI_DEBUG_TABLE_ENTRY_SIZE;
118 NewTable = AllocateZeroPool (TableSize + EFI_PAGE_SIZE);
119 if (NewTable == NULL) {
120 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
121 return;
122 }
123 //
124 // Copy the old table into the new one
125 //
126 CopyMem (NewTable, Table, TableSize);
127 //
128 // Free the old table
129 //
130 CoreFreePool (Table);
131 //
132 // Update the table header
133 //
134 Table = NewTable;
135 mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;
136 mDebugInfoTableHeader.TableSize += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
137 }
138 //
139 // Allocate data for new entry
140 //
141 Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));
142 if (Table[Index].NormalImage != NULL) {
143 //
144 // Update the entry
145 //
146 Table[Index].NormalImage->ImageInfoType = (UINT32) ImageInfoType;
147 Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;
148 Table[Index].NormalImage->ImageHandle = ImageHandle;
149 }
150 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
151 }
152
153
154
155 /**
156 Removes and frees an entry from the DebugImageInfo Table.
157
158 @param ImageHandle image handle for the image being unloaded
159
160 **/
161 VOID
162 CoreRemoveDebugImageInfoEntry (
163 EFI_HANDLE ImageHandle
164 )
165 {
166 EFI_DEBUG_IMAGE_INFO *Table;
167 UINTN Index;
168
169 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
170
171 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
172
173 for (Index = 0; Index < mDebugInfoTableHeader.TableSize; Index++) {
174 if (Table[Index].NormalImage != NULL && Table[Index].NormalImage->ImageHandle == ImageHandle) {
175 //
176 // Found a match. Free up the record, then NULL the pointer to indicate the slot
177 // is free.
178 //
179 CoreFreePool (Table[Index].NormalImage);
180 Table[Index].NormalImage = NULL;
181 break;
182 }
183 }
184 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
185 }
186
187