]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskProtocol.c
9fe888eb9b965587a086609b5fe97d35b3d1c33b
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / RamDiskDxe / RamDiskProtocol.c
1 /** @file
2 The realization of EFI_RAM_DISK_PROTOCOL.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this 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 "RamDiskImpl.h"
16
17 RAM_DISK_PRIVATE_DATA mRamDiskPrivateDataTemplate = {
18 RAM_DISK_PRIVATE_DATA_SIGNATURE,
19 NULL
20 };
21
22 MEDIA_RAM_DISK_DEVICE_PATH mRamDiskDeviceNodeTemplate = {
23 {
24 MEDIA_DEVICE_PATH,
25 MEDIA_RAM_DISK_DP,
26 {
27 (UINT8) (sizeof (MEDIA_RAM_DISK_DEVICE_PATH)),
28 (UINT8) ((sizeof (MEDIA_RAM_DISK_DEVICE_PATH)) >> 8)
29 }
30 }
31 };
32
33
34 /**
35 Initialize the RAM disk device node.
36
37 @param[in] PrivateData Points to RAM disk private data.
38 @param[in, out] RamDiskDevNode Points to the RAM disk device node.
39
40 **/
41 VOID
42 RamDiskInitDeviceNode (
43 IN RAM_DISK_PRIVATE_DATA *PrivateData,
44 IN OUT MEDIA_RAM_DISK_DEVICE_PATH *RamDiskDevNode
45 )
46 {
47 WriteUnaligned64 (
48 (UINT64 *) &(RamDiskDevNode->StartingAddr[0]),
49 (UINT64) PrivateData->StartingAddr
50 );
51 WriteUnaligned64 (
52 (UINT64 *) &(RamDiskDevNode->EndingAddr[0]),
53 (UINT64) PrivateData->StartingAddr + PrivateData->Size
54 );
55 CopyGuid (&RamDiskDevNode->TypeGuid, &PrivateData->TypeGuid);
56 RamDiskDevNode->Instance = PrivateData->InstanceNumber;
57 }
58
59
60 /**
61 Register a RAM disk with specified address, size and type.
62
63 @param[in] RamDiskBase The base address of registered RAM disk.
64 @param[in] RamDiskSize The size of registered RAM disk.
65 @param[in] RamDiskType The type of registered RAM disk. The GUID can be
66 any of the values defined in section 9.3.6.9, or a
67 vendor defined GUID.
68 @param[in] ParentDevicePath
69 Pointer to the parent device path. If there is no
70 parent device path then ParentDevicePath is NULL.
71 @param[out] DevicePath On return, points to a pointer to the device path
72 of the RAM disk device.
73 If ParentDevicePath is not NULL, the returned
74 DevicePath is created by appending a RAM disk node
75 to the parent device path. If ParentDevicePath is
76 NULL, the returned DevicePath is a RAM disk device
77 path without appending. This function is
78 responsible for allocating the buffer DevicePath
79 with the boot service AllocatePool().
80
81 @retval EFI_SUCCESS The RAM disk is registered successfully.
82 @retval EFI_INVALID_PARAMETER DevicePath or RamDiskType is NULL.
83 RamDiskSize is 0.
84 @retval EFI_ALREADY_STARTED A Device Path Protocol instance to be created
85 is already present in the handle database.
86 @retval EFI_OUT_OF_RESOURCES The RAM disk register operation fails due to
87 resource limitation.
88
89 **/
90 EFI_STATUS
91 EFIAPI
92 RamDiskRegister (
93 IN UINT64 RamDiskBase,
94 IN UINT64 RamDiskSize,
95 IN EFI_GUID *RamDiskType,
96 IN EFI_DEVICE_PATH *ParentDevicePath OPTIONAL,
97 OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
98 )
99 {
100 EFI_STATUS Status;
101 RAM_DISK_PRIVATE_DATA *PrivateData;
102 RAM_DISK_PRIVATE_DATA *RegisteredPrivateData;
103 MEDIA_RAM_DISK_DEVICE_PATH *RamDiskDevNode;
104 UINTN DevicePathSize;
105 LIST_ENTRY *Entry;
106 EFI_TPL OldTpl;
107
108 if ((0 == RamDiskSize) || (NULL == RamDiskType) || (NULL == DevicePath)) {
109 return EFI_INVALID_PARAMETER;
110 }
111
112 //
113 // Add check to prevent data read across the memory boundary
114 //
115 if (RamDiskBase + RamDiskSize > ((UINTN) -1) - RAM_DISK_BLOCK_SIZE + 1) {
116 return EFI_INVALID_PARAMETER;
117 }
118
119 RamDiskDevNode = NULL;
120
121 //
122 // Create a new RAM disk instance and initialize its private data
123 //
124 PrivateData = AllocateCopyPool (
125 sizeof (RAM_DISK_PRIVATE_DATA),
126 &mRamDiskPrivateDataTemplate
127 );
128 if (NULL == PrivateData) {
129 return EFI_OUT_OF_RESOURCES;
130 }
131
132 PrivateData->StartingAddr = RamDiskBase;
133 PrivateData->Size = RamDiskSize;
134 CopyGuid (&PrivateData->TypeGuid, RamDiskType);
135 InitializeListHead (&PrivateData->ThisInstance);
136
137 //
138 // Generate device path information for the registered RAM disk
139 //
140 RamDiskDevNode = AllocateCopyPool (
141 sizeof (MEDIA_RAM_DISK_DEVICE_PATH),
142 &mRamDiskDeviceNodeTemplate
143 );
144 if (NULL == RamDiskDevNode) {
145 Status = EFI_OUT_OF_RESOURCES;
146 goto ErrorExit;
147 }
148
149 RamDiskInitDeviceNode (PrivateData, RamDiskDevNode);
150
151 *DevicePath = AppendDevicePathNode (
152 ParentDevicePath,
153 (EFI_DEVICE_PATH_PROTOCOL *) RamDiskDevNode
154 );
155 if (NULL == *DevicePath) {
156 Status = EFI_OUT_OF_RESOURCES;
157 goto ErrorExit;
158 }
159
160 PrivateData->DevicePath = *DevicePath;
161
162 //
163 // Check whether the created device path is already present in the handle
164 // database
165 //
166 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
167 if (!IsListEmpty(&RegisteredRamDisks)) {
168 DevicePathSize = GetDevicePathSize (PrivateData->DevicePath);
169
170 EFI_LIST_FOR_EACH (Entry, &RegisteredRamDisks) {
171 RegisteredPrivateData = RAM_DISK_PRIVATE_FROM_THIS (Entry);
172 if (DevicePathSize == GetDevicePathSize (RegisteredPrivateData->DevicePath)) {
173 //
174 // Compare device path
175 //
176 if ((CompareMem (
177 PrivateData->DevicePath,
178 RegisteredPrivateData->DevicePath,
179 DevicePathSize)) == 0) {
180 *DevicePath = NULL;
181 Status = EFI_ALREADY_STARTED;
182 goto ErrorExit;
183 }
184 }
185 }
186 }
187 gBS->RestoreTPL (OldTpl);
188
189 //
190 // Fill Block IO protocol informations for the RAM disk
191 //
192 RamDiskInitBlockIo (PrivateData);
193
194 //
195 // Install EFI_DEVICE_PATH_PROTOCOL & EFI_BLOCK_IO_PROTOCOL on a new
196 // handle
197 //
198 Status = gBS->InstallMultipleProtocolInterfaces (
199 &PrivateData->Handle,
200 &gEfiBlockIoProtocolGuid,
201 &PrivateData->BlockIo,
202 &gEfiDevicePathProtocolGuid,
203 PrivateData->DevicePath,
204 NULL
205 );
206 if (EFI_ERROR (Status)) {
207 goto ErrorExit;
208 }
209
210 //
211 // Insert the newly created one to the registered RAM disk list
212 //
213 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
214 InsertTailList (&RegisteredRamDisks, &PrivateData->ThisInstance);
215 ListEntryNum++;
216 gBS->RestoreTPL (OldTpl);
217
218 gBS->ConnectController (PrivateData->Handle, NULL, NULL, TRUE);
219
220 FreePool (RamDiskDevNode);
221
222 return EFI_SUCCESS;
223
224 ErrorExit:
225 if (RamDiskDevNode != NULL) {
226 FreePool (RamDiskDevNode);
227 }
228
229 if (PrivateData != NULL) {
230 if (PrivateData->DevicePath) {
231 FreePool (PrivateData->DevicePath);
232 }
233
234 FreePool (PrivateData);
235 }
236
237 return Status;
238 }
239
240
241 /**
242 Unregister a RAM disk specified by DevicePath.
243
244 @param[in] DevicePath A pointer to the device path that describes a RAM
245 Disk device.
246
247 @retval EFI_SUCCESS The RAM disk is unregistered successfully.
248 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
249 @retval EFI_UNSUPPORTED The device specified by DevicePath is not a
250 valid ramdisk device path and not supported
251 by the driver.
252 @retval EFI_NOT_FOUND The RAM disk pointed by DevicePath doesn't
253 exist.
254
255 **/
256 EFI_STATUS
257 EFIAPI
258 RamDiskUnregister (
259 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
260 )
261 {
262 LIST_ENTRY *Entry;
263 LIST_ENTRY *NextEntry;
264 BOOLEAN Found;
265 UINT64 StartingAddr;
266 UINT64 EndingAddr;
267 EFI_DEVICE_PATH_PROTOCOL *Header;
268 MEDIA_RAM_DISK_DEVICE_PATH *RamDiskDevNode;
269 RAM_DISK_PRIVATE_DATA *PrivateData;
270 EFI_TPL OldTpl;
271
272 if (NULL == DevicePath) {
273 return EFI_INVALID_PARAMETER;
274 }
275
276 //
277 // Locate the RAM disk device node.
278 //
279 RamDiskDevNode = NULL;
280 Header = DevicePath;
281 do {
282 //
283 // Test if the current device node is a RAM disk.
284 //
285 if ((MEDIA_DEVICE_PATH == Header->Type) &&
286 (MEDIA_RAM_DISK_DP == Header->SubType)) {
287 RamDiskDevNode = (MEDIA_RAM_DISK_DEVICE_PATH *) Header;
288
289 break;
290 }
291
292 Header = NextDevicePathNode (Header);
293 } while ((Header->Type != END_DEVICE_PATH_TYPE));
294
295 if (NULL == RamDiskDevNode) {
296 return EFI_UNSUPPORTED;
297 }
298
299 Found = FALSE;
300 StartingAddr = ReadUnaligned64 ((UINT64 *) &(RamDiskDevNode->StartingAddr[0]));
301 EndingAddr = ReadUnaligned64 ((UINT64 *) &(RamDiskDevNode->EndingAddr[0]));
302
303 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
304 if (!IsListEmpty(&RegisteredRamDisks)) {
305 EFI_LIST_FOR_EACH_SAFE (Entry, NextEntry, &RegisteredRamDisks) {
306 PrivateData = RAM_DISK_PRIVATE_FROM_THIS (Entry);
307
308 //
309 // Unregister the RAM disk given by its starting address, ending address
310 // and type guid.
311 //
312 if ((StartingAddr == PrivateData->StartingAddr) &&
313 (EndingAddr == PrivateData->StartingAddr + PrivateData->Size) &&
314 (CompareGuid (&RamDiskDevNode->TypeGuid, &PrivateData->TypeGuid))) {
315 //
316 // Uninstall the EFI_DEVICE_PATH_PROTOCOL & EFI_BLOCK_IO_PROTOCOL
317 //
318 gBS->UninstallMultipleProtocolInterfaces (
319 PrivateData->Handle,
320 &gEfiBlockIoProtocolGuid,
321 &PrivateData->BlockIo,
322 &gEfiDevicePathProtocolGuid,
323 DevicePath,
324 NULL
325 );
326
327 RemoveEntryList (&PrivateData->ThisInstance);
328
329 if (RamDiskCreateHii == PrivateData->CreateMethod) {
330 //
331 // If a RAM disk is created within HII, then the RamDiskDxe driver
332 // driver is responsible for freeing the allocated memory for the
333 // RAM disk.
334 //
335 FreePool ((VOID *)(UINTN) PrivateData->StartingAddr);
336 }
337
338 gBS->DisconnectController (PrivateData->Handle, NULL, NULL);
339
340 FreePool (PrivateData->DevicePath);
341 FreePool (PrivateData);
342 ListEntryNum--;
343 Found = TRUE;
344
345 break;
346 }
347 }
348 }
349 gBS->RestoreTPL (OldTpl);
350
351 if (TRUE == Found) {
352 return EFI_SUCCESS;
353 } else {
354 return EFI_NOT_FOUND;
355 }
356 }