]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/InstallConfigurationTable.c
57f31fa98fe5e81e81bb92b570b95a5551bed35b
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / InstallConfigurationTable.c
1 /** @file
2 System Management System Table Services SmmInstallConfigurationTable service
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "PiSmmCore.h"
10
11 #define CONFIG_TABLE_SIZE_INCREASED 0x10
12
13 UINTN mSmmSystemTableAllocateSize = 0;
14
15 /**
16 The SmmInstallConfigurationTable() function is used to maintain the list
17 of configuration tables that are stored in the System Management System
18 Table. The list is stored as an array of (GUID, Pointer) pairs. The list
19 must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
20
21 @param SystemTable A pointer to the SMM System Table (SMST).
22 @param Guid A pointer to the GUID for the entry to add, update, or remove.
23 @param Table A pointer to the buffer of the table to add.
24 @param TableSize The size of the table to install.
25
26 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
27 @retval EFI_INVALID_PARAMETER Guid is not valid.
28 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
29 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
30
31 **/
32 EFI_STATUS
33 EFIAPI
34 SmmInstallConfigurationTable (
35 IN CONST EFI_SMM_SYSTEM_TABLE2 *SystemTable,
36 IN CONST EFI_GUID *Guid,
37 IN VOID *Table,
38 IN UINTN TableSize
39 )
40 {
41 UINTN Index;
42 EFI_CONFIGURATION_TABLE *ConfigurationTable;
43 EFI_CONFIGURATION_TABLE *OldTable;
44
45 //
46 // If Guid is NULL, then this operation cannot be performed
47 //
48 if (Guid == NULL) {
49 return EFI_INVALID_PARAMETER;
50 }
51
52 ConfigurationTable = gSmmCoreSmst.SmmConfigurationTable;
53
54 //
55 // Search all the table for an entry that matches Guid
56 //
57 for (Index = 0; Index < gSmmCoreSmst.NumberOfTableEntries; Index++) {
58 if (CompareGuid (Guid, &(ConfigurationTable[Index].VendorGuid))) {
59 break;
60 }
61 }
62
63 if (Index < gSmmCoreSmst.NumberOfTableEntries) {
64 //
65 // A match was found, so this is either a modify or a delete operation
66 //
67 if (Table != NULL) {
68 //
69 // If Table is not NULL, then this is a modify operation.
70 // Modify the table entry and return.
71 //
72 ConfigurationTable[Index].VendorTable = Table;
73 return EFI_SUCCESS;
74 }
75
76 //
77 // A match was found and Table is NULL, so this is a delete operation.
78 //
79 gSmmCoreSmst.NumberOfTableEntries--;
80
81 //
82 // Copy over deleted entry
83 //
84 CopyMem (
85 &(ConfigurationTable[Index]),
86 &(ConfigurationTable[Index + 1]),
87 (gSmmCoreSmst.NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
88 );
89
90 } else {
91 //
92 // No matching GUIDs were found, so this is an add operation.
93 //
94 if (Table == NULL) {
95 //
96 // If Table is NULL on an add operation, then return an error.
97 //
98 return EFI_NOT_FOUND;
99 }
100
101 //
102 // Assume that Index == gSmmCoreSmst.NumberOfTableEntries
103 //
104 if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mSmmSystemTableAllocateSize) {
105 //
106 // Allocate a table with one additional entry.
107 //
108 mSmmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
109 ConfigurationTable = AllocatePool (mSmmSystemTableAllocateSize);
110 if (ConfigurationTable == NULL) {
111 //
112 // If a new table could not be allocated, then return an error.
113 //
114 return EFI_OUT_OF_RESOURCES;
115 }
116
117 if (gSmmCoreSmst.SmmConfigurationTable != NULL) {
118 //
119 // Copy the old table to the new table.
120 //
121 CopyMem (
122 ConfigurationTable,
123 gSmmCoreSmst.SmmConfigurationTable,
124 Index * sizeof (EFI_CONFIGURATION_TABLE)
125 );
126
127 //
128 // Record the old table pointer.
129 //
130 OldTable = gSmmCoreSmst.SmmConfigurationTable;
131
132 //
133 // As the SmmInstallConfigurationTable() may be re-entered by FreePool() in
134 // its calling stack, updating System table to the new table pointer must
135 // be done before calling FreePool() to free the old table.
136 // It can make sure the gSmmCoreSmst.SmmConfigurationTable point to the new
137 // table and avoid the errors of use-after-free to the old table by the
138 // reenter of SmmInstallConfigurationTable() in FreePool()'s calling stack.
139 //
140 gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;
141
142 //
143 // Free the old table after updating System Table to the new table pointer.
144 //
145 FreePool (OldTable);
146 } else {
147 //
148 // Update System Table
149 //
150 gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;
151 }
152 }
153
154 //
155 // Fill in the new entry
156 //
157 CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
158 ConfigurationTable[Index].VendorTable = Table;
159
160 //
161 // This is an add operation, so increment the number of table entries
162 //
163 gSmmCoreSmst.NumberOfTableEntries++;
164 }
165
166 //
167 // CRC-32 field is ignorable for SMM System Table and should be set to zero
168 //
169
170 return EFI_SUCCESS;
171 }