]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskDriver.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / RamDiskDxe / RamDiskDriver.c
1 /** @file
2 The driver entry point for RamDiskDxe driver.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) Microsoft Corporation.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "RamDiskImpl.h"
11
12 //
13 // Handle for the EFI_RAM_DISK_PROTOCOL instance
14 //
15 EFI_HANDLE mRamDiskHandle = NULL;
16
17 //
18 // The EFI_RAM_DISK_PROTOCOL instances that is installed onto the driver
19 // handle
20 //
21 EFI_RAM_DISK_PROTOCOL mRamDiskProtocol = {
22 RamDiskRegister,
23 RamDiskUnregister
24 };
25
26 //
27 // RamDiskDxe driver maintains a list of registered RAM disks.
28 //
29 LIST_ENTRY RegisteredRamDisks;
30
31 //
32 // Pointers to the EFI_ACPI_TABLE_PROTOCOL and EFI_ACPI_SDT_PROTOCOL.
33 //
34 EFI_ACPI_TABLE_PROTOCOL *mAcpiTableProtocol = NULL;
35 EFI_ACPI_SDT_PROTOCOL *mAcpiSdtProtocol = NULL;
36
37 /**
38 Check whether EFI_ACPI_TABLE_PROTOCOL and EFI_ACPI_SDT_PROTOCOL are produced.
39 If both protocols are produced, publish all the reserved memory type RAM
40 disks to the NVDIMM Firmware Interface Table (NFIT).
41
42 @param[in] Event Event whose notification function is being invoked.
43 @param[in] Context The pointer to the notification function's context,
44 which is implementation-dependent.
45
46 **/
47 VOID
48 EFIAPI
49 RamDiskAcpiCheck (
50 IN EFI_EVENT Event,
51 IN VOID *Context
52 )
53 {
54 EFI_STATUS Status;
55 LIST_ENTRY *Entry;
56 RAM_DISK_PRIVATE_DATA *PrivateData;
57
58 gBS->CloseEvent (Event);
59
60 //
61 // Locate the EFI_ACPI_TABLE_PROTOCOL.
62 //
63 Status = gBS->LocateProtocol (
64 &gEfiAcpiTableProtocolGuid,
65 NULL,
66 (VOID **)&mAcpiTableProtocol
67 );
68 if (EFI_ERROR (Status)) {
69 DEBUG ((
70 DEBUG_INFO,
71 "RamDiskAcpiCheck: Cannot locate the EFI ACPI Table Protocol, "
72 "unable to publish RAM disks to NFIT.\n"
73 ));
74 return;
75 }
76
77 //
78 // Locate the EFI_ACPI_SDT_PROTOCOL.
79 //
80 Status = gBS->LocateProtocol (
81 &gEfiAcpiSdtProtocolGuid,
82 NULL,
83 (VOID **)&mAcpiSdtProtocol
84 );
85 if (EFI_ERROR (Status)) {
86 DEBUG ((
87 DEBUG_INFO,
88 "RamDiskAcpiCheck: Cannot locate the EFI ACPI Sdt Protocol, "
89 "unable to publish RAM disks to NFIT.\n"
90 ));
91 mAcpiTableProtocol = NULL;
92 return;
93 }
94
95 BASE_LIST_FOR_EACH (Entry, &RegisteredRamDisks) {
96 PrivateData = RAM_DISK_PRIVATE_FROM_THIS (Entry);
97 RamDiskPublishNfit (PrivateData);
98 }
99 }
100
101 /**
102 The entry point for RamDiskDxe driver.
103
104 @param[in] ImageHandle The image handle of the driver.
105 @param[in] SystemTable The system table.
106
107 @retval EFI_ALREADY_STARTED The driver already exists in system.
108 @retval EFI_OUT_OF_RESOURCES Fail to execute entry point due to lack of
109 resources.
110 @retval EFI_SUCCES All the related protocols are installed on
111 the driver.
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 RamDiskDxeEntryPoint (
117 IN EFI_HANDLE ImageHandle,
118 IN EFI_SYSTEM_TABLE *SystemTable
119 )
120 {
121 EFI_STATUS Status;
122 RAM_DISK_CONFIG_PRIVATE_DATA *ConfigPrivate;
123 VOID *DummyInterface;
124 EFI_EVENT Event;
125
126 //
127 // If already started, return.
128 //
129 Status = gBS->LocateProtocol (
130 &gEfiRamDiskProtocolGuid,
131 NULL,
132 &DummyInterface
133 );
134 if (!EFI_ERROR (Status)) {
135 DEBUG ((DEBUG_INFO, "Driver already started!\n"));
136 return EFI_ALREADY_STARTED;
137 }
138
139 //
140 // Create a private data structure.
141 //
142 ConfigPrivate = AllocateCopyPool (sizeof (RAM_DISK_CONFIG_PRIVATE_DATA), &mRamDiskConfigPrivateDataTemplate);
143 if (ConfigPrivate == NULL) {
144 return EFI_OUT_OF_RESOURCES;
145 }
146
147 //
148 // Install RAM disk configuration form
149 //
150 Status = InstallRamDiskConfigForm (ConfigPrivate);
151 if (EFI_ERROR (Status)) {
152 goto ErrorExit;
153 }
154
155 //
156 // Initialize the list of registered RAM disks maintained by the driver
157 // before installing the protocol
158 //
159 InitializeListHead (&RegisteredRamDisks);
160
161 //
162 // Install the EFI_RAM_DISK_PROTOCOL and RAM disk private data onto a
163 // new handle
164 //
165 Status = gBS->InstallMultipleProtocolInterfaces (
166 &mRamDiskHandle,
167 &gEfiRamDiskProtocolGuid,
168 &mRamDiskProtocol,
169 &gEfiCallerIdGuid,
170 ConfigPrivate,
171 NULL
172 );
173 if (EFI_ERROR (Status)) {
174 goto ErrorExit;
175 }
176
177 Status = EfiCreateEventReadyToBootEx (
178 TPL_CALLBACK,
179 RamDiskAcpiCheck,
180 NULL,
181 &Event
182 );
183 ASSERT_EFI_ERROR (Status);
184
185 return EFI_SUCCESS;
186
187 ErrorExit:
188 if (ConfigPrivate != NULL) {
189 UninstallRamDiskConfigForm (ConfigPrivate);
190 }
191
192 return Status;
193 }
194
195 /**
196 Unload the RamDiskDxe driver and its configuration form.
197
198 @param[in] ImageHandle The driver's image handle.
199
200 @retval EFI_SUCCESS The RamDiskDxe driver and its configuration
201 form is unloaded.
202 @retval Others Failed to unload the form.
203
204 **/
205 EFI_STATUS
206 EFIAPI
207 RamDiskDxeUnload (
208 IN EFI_HANDLE ImageHandle
209 )
210 {
211 EFI_STATUS Status;
212 RAM_DISK_CONFIG_PRIVATE_DATA *ConfigPrivate;
213
214 Status = gBS->HandleProtocol (
215 mRamDiskHandle,
216 &gEfiCallerIdGuid,
217 (VOID **)&ConfigPrivate
218 );
219 if (EFI_ERROR (Status)) {
220 return Status;
221 }
222
223 ASSERT (ConfigPrivate->Signature == RAM_DISK_CONFIG_PRIVATE_DATA_SIGNATURE);
224
225 //
226 // Unregister all registered RAM disks
227 //
228 UnregisterAllRamDisks ();
229
230 gBS->UninstallMultipleProtocolInterfaces (
231 mRamDiskHandle,
232 &gEfiRamDiskProtocolGuid,
233 &mRamDiskProtocol,
234 &gEfiCallerIdGuid,
235 ConfigPrivate,
236 NULL
237 );
238
239 UninstallRamDiskConfigForm (ConfigPrivate);
240
241 return EFI_SUCCESS;
242 }