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