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