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