]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskProtocol.c
MdeModulePkg/RamDiskDxe: Fix typo in HII message
[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
107 if ((0 == RamDiskSize) || (NULL == RamDiskType) || (NULL == DevicePath)) {
108 return EFI_INVALID_PARAMETER;
109 }
110
111 //
112 // Add check to prevent data read across the memory boundary
113 //
114 if (RamDiskBase + RamDiskSize > ((UINTN) -1) - RAM_DISK_BLOCK_SIZE + 1) {
115 return EFI_INVALID_PARAMETER;
116 }
117
118 RamDiskDevNode = NULL;
119
120 //
121 // Create a new RAM disk instance and initialize its private data
122 //
123 PrivateData = AllocateCopyPool (
124 sizeof (RAM_DISK_PRIVATE_DATA),
125 &mRamDiskPrivateDataTemplate
126 );
127 if (NULL == PrivateData) {
128 return EFI_OUT_OF_RESOURCES;
129 }
130
131 PrivateData->StartingAddr = RamDiskBase;
132 PrivateData->Size = RamDiskSize;
133 CopyGuid (&PrivateData->TypeGuid, RamDiskType);
134 InitializeListHead (&PrivateData->ThisInstance);
135
136 //
137 // Generate device path information for the registered RAM disk
138 //
139 RamDiskDevNode = AllocateCopyPool (
140 sizeof (MEDIA_RAM_DISK_DEVICE_PATH),
141 &mRamDiskDeviceNodeTemplate
142 );
143 if (NULL == RamDiskDevNode) {
144 Status = EFI_OUT_OF_RESOURCES;
145 goto ErrorExit;
146 }
147
148 RamDiskInitDeviceNode (PrivateData, RamDiskDevNode);
149
150 *DevicePath = AppendDevicePathNode (
151 ParentDevicePath,
152 (EFI_DEVICE_PATH_PROTOCOL *) RamDiskDevNode
153 );
154 if (NULL == *DevicePath) {
155 Status = EFI_OUT_OF_RESOURCES;
156 goto ErrorExit;
157 }
158
159 PrivateData->DevicePath = *DevicePath;
160
161 //
162 // Check whether the created device path is already present in the handle
163 // database
164 //
165 if (!IsListEmpty(&RegisteredRamDisks)) {
166 DevicePathSize = GetDevicePathSize (PrivateData->DevicePath);
167
168 EFI_LIST_FOR_EACH (Entry, &RegisteredRamDisks) {
169 RegisteredPrivateData = RAM_DISK_PRIVATE_FROM_THIS (Entry);
170 if (DevicePathSize == GetDevicePathSize (RegisteredPrivateData->DevicePath)) {
171 //
172 // Compare device path
173 //
174 if ((CompareMem (
175 PrivateData->DevicePath,
176 RegisteredPrivateData->DevicePath,
177 DevicePathSize)) == 0) {
178 *DevicePath = NULL;
179 Status = EFI_ALREADY_STARTED;
180 goto ErrorExit;
181 }
182 }
183 }
184 }
185
186 //
187 // Fill Block IO protocol informations for the RAM disk
188 //
189 RamDiskInitBlockIo (PrivateData);
190
191 //
192 // Install EFI_DEVICE_PATH_PROTOCOL & EFI_BLOCK_IO(2)_PROTOCOL on a new
193 // handle
194 //
195 Status = gBS->InstallMultipleProtocolInterfaces (
196 &PrivateData->Handle,
197 &gEfiBlockIoProtocolGuid,
198 &PrivateData->BlockIo,
199 &gEfiBlockIo2ProtocolGuid,
200 &PrivateData->BlockIo2,
201 &gEfiDevicePathProtocolGuid,
202 PrivateData->DevicePath,
203 NULL
204 );
205 if (EFI_ERROR (Status)) {
206 goto ErrorExit;
207 }
208
209 //
210 // Insert the newly created one to the registered RAM disk list
211 //
212 InsertTailList (&RegisteredRamDisks, &PrivateData->ThisInstance);
213 ListEntryNum++;
214
215 gBS->ConnectController (PrivateData->Handle, NULL, NULL, TRUE);
216
217 FreePool (RamDiskDevNode);
218
219 return EFI_SUCCESS;
220
221 ErrorExit:
222 if (RamDiskDevNode != NULL) {
223 FreePool (RamDiskDevNode);
224 }
225
226 if (PrivateData != NULL) {
227 if (PrivateData->DevicePath) {
228 FreePool (PrivateData->DevicePath);
229 }
230
231 FreePool (PrivateData);
232 }
233
234 return Status;
235 }
236
237
238 /**
239 Unregister a RAM disk specified by DevicePath.
240
241 @param[in] DevicePath A pointer to the device path that describes a RAM
242 Disk device.
243
244 @retval EFI_SUCCESS The RAM disk is unregistered successfully.
245 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
246 @retval EFI_UNSUPPORTED The device specified by DevicePath is not a
247 valid ramdisk device path and not supported
248 by the driver.
249 @retval EFI_NOT_FOUND The RAM disk pointed by DevicePath doesn't
250 exist.
251
252 **/
253 EFI_STATUS
254 EFIAPI
255 RamDiskUnregister (
256 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
257 )
258 {
259 LIST_ENTRY *Entry;
260 LIST_ENTRY *NextEntry;
261 BOOLEAN Found;
262 UINT64 StartingAddr;
263 UINT64 EndingAddr;
264 EFI_DEVICE_PATH_PROTOCOL *Header;
265 MEDIA_RAM_DISK_DEVICE_PATH *RamDiskDevNode;
266 RAM_DISK_PRIVATE_DATA *PrivateData;
267
268 if (NULL == DevicePath) {
269 return EFI_INVALID_PARAMETER;
270 }
271
272 //
273 // Locate the RAM disk device node.
274 //
275 RamDiskDevNode = NULL;
276 Header = DevicePath;
277 do {
278 //
279 // Test if the current device node is a RAM disk.
280 //
281 if ((MEDIA_DEVICE_PATH == Header->Type) &&
282 (MEDIA_RAM_DISK_DP == Header->SubType)) {
283 RamDiskDevNode = (MEDIA_RAM_DISK_DEVICE_PATH *) Header;
284
285 break;
286 }
287
288 Header = NextDevicePathNode (Header);
289 } while ((Header->Type != END_DEVICE_PATH_TYPE));
290
291 if (NULL == RamDiskDevNode) {
292 return EFI_UNSUPPORTED;
293 }
294
295 Found = FALSE;
296 StartingAddr = ReadUnaligned64 ((UINT64 *) &(RamDiskDevNode->StartingAddr[0]));
297 EndingAddr = ReadUnaligned64 ((UINT64 *) &(RamDiskDevNode->EndingAddr[0]));
298
299 if (!IsListEmpty(&RegisteredRamDisks)) {
300 EFI_LIST_FOR_EACH_SAFE (Entry, NextEntry, &RegisteredRamDisks) {
301 PrivateData = RAM_DISK_PRIVATE_FROM_THIS (Entry);
302
303 //
304 // Unregister the RAM disk given by its starting address, ending address
305 // and type guid.
306 //
307 if ((StartingAddr == PrivateData->StartingAddr) &&
308 (EndingAddr == PrivateData->StartingAddr + PrivateData->Size) &&
309 (CompareGuid (&RamDiskDevNode->TypeGuid, &PrivateData->TypeGuid))) {
310 //
311 // Uninstall the EFI_DEVICE_PATH_PROTOCOL & EFI_BLOCK_IO(2)_PROTOCOL
312 //
313 gBS->UninstallMultipleProtocolInterfaces (
314 PrivateData->Handle,
315 &gEfiBlockIoProtocolGuid,
316 &PrivateData->BlockIo,
317 &gEfiBlockIo2ProtocolGuid,
318 &PrivateData->BlockIo2,
319 &gEfiDevicePathProtocolGuid,
320 (EFI_DEVICE_PATH_PROTOCOL *) PrivateData->DevicePath,
321 NULL
322 );
323
324 RemoveEntryList (&PrivateData->ThisInstance);
325
326 if (RamDiskCreateHii == PrivateData->CreateMethod) {
327 //
328 // If a RAM disk is created within HII, then the RamDiskDxe driver
329 // driver is responsible for freeing the allocated memory for the
330 // RAM disk.
331 //
332 FreePool ((VOID *)(UINTN) PrivateData->StartingAddr);
333 }
334
335 FreePool (PrivateData->DevicePath);
336 FreePool (PrivateData);
337 ListEntryNum--;
338 Found = TRUE;
339
340 break;
341 }
342 }
343 }
344
345 if (TRUE == Found) {
346 return EFI_SUCCESS;
347 } else {
348 return EFI_NOT_FOUND;
349 }
350 }