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