]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/InstallConfigurationTable.c
0bc51be12bf63977bb2d5701e59d1a47004e69c5
[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 } else {
90 //
91 // No matching GUIDs were found, so this is an add operation.
92 //
93 if (Table == NULL) {
94 //
95 // If Table is NULL on an add operation, then return an error.
96 //
97 return EFI_NOT_FOUND;
98 }
99
100 //
101 // Assume that Index == gSmmCoreSmst.NumberOfTableEntries
102 //
103 if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mSmmSystemTableAllocateSize) {
104 //
105 // Allocate a table with one additional entry.
106 //
107 mSmmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
108 ConfigurationTable = AllocatePool (mSmmSystemTableAllocateSize);
109 if (ConfigurationTable == NULL) {
110 //
111 // If a new table could not be allocated, then return an error.
112 //
113 return EFI_OUT_OF_RESOURCES;
114 }
115
116 if (gSmmCoreSmst.SmmConfigurationTable != NULL) {
117 //
118 // Copy the old table to the new table.
119 //
120 CopyMem (
121 ConfigurationTable,
122 gSmmCoreSmst.SmmConfigurationTable,
123 Index * sizeof (EFI_CONFIGURATION_TABLE)
124 );
125
126 //
127 // Record the old table pointer.
128 //
129 OldTable = gSmmCoreSmst.SmmConfigurationTable;
130
131 //
132 // As the SmmInstallConfigurationTable() may be re-entered by FreePool() in
133 // its calling stack, updating System table to the new table pointer must
134 // be done before calling FreePool() to free the old table.
135 // It can make sure the gSmmCoreSmst.SmmConfigurationTable point to the new
136 // table and avoid the errors of use-after-free to the old table by the
137 // reenter of SmmInstallConfigurationTable() in FreePool()'s calling stack.
138 //
139 gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;
140
141 //
142 // Free the old table after updating System Table to the new table pointer.
143 //
144 FreePool (OldTable);
145 } else {
146 //
147 // Update System Table
148 //
149 gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;
150 }
151 }
152
153 //
154 // Fill in the new entry
155 //
156 CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
157 ConfigurationTable[Index].VendorTable = Table;
158
159 //
160 // This is an add operation, so increment the number of table entries
161 //
162 gSmmCoreSmst.NumberOfTableEntries++;
163 }
164
165 //
166 // CRC-32 field is ignorable for SMM System Table and should be set to zero
167 //
168
169 return EFI_SUCCESS;
170 }