TianoCore EDK2 master
Loading...
Searching...
No Matches
RamDiskBlockIo.c
Go to the documentation of this file.
1
9#include "RamDiskImpl.h"
10
11//
12// The EFI_BLOCK_IO_PROTOCOL instances that is installed onto the handle
13// for newly registered RAM disks
14//
15EFI_BLOCK_IO_PROTOCOL mRamDiskBlockIoTemplate = {
16 EFI_BLOCK_IO_PROTOCOL_REVISION,
22};
23
24//
25// The EFI_BLOCK_IO_PROTOCOL2 instances that is installed onto the handle
26// for newly registered RAM disks
27//
28EFI_BLOCK_IO2_PROTOCOL mRamDiskBlockIo2Template = {
34};
35
42VOID
44 IN RAM_DISK_PRIVATE_DATA *PrivateData
45 )
46{
47 EFI_BLOCK_IO_PROTOCOL *BlockIo;
48 EFI_BLOCK_IO2_PROTOCOL *BlockIo2;
49 EFI_BLOCK_IO_MEDIA *Media;
50 UINT32 Remainder;
51
52 BlockIo = &PrivateData->BlockIo;
53 BlockIo2 = &PrivateData->BlockIo2;
54 Media = &PrivateData->Media;
55
56 CopyMem (BlockIo, &mRamDiskBlockIoTemplate, sizeof (EFI_BLOCK_IO_PROTOCOL));
57 CopyMem (BlockIo2, &mRamDiskBlockIo2Template, sizeof (EFI_BLOCK_IO2_PROTOCOL));
58
59 BlockIo->Media = Media;
60 BlockIo2->Media = Media;
61 Media->RemovableMedia = FALSE;
62 Media->MediaPresent = TRUE;
63 Media->LogicalPartition = FALSE;
64 Media->ReadOnly = FALSE;
65 Media->WriteCaching = FALSE;
66
68 Media->BlockSize >= 1;
69 Media->BlockSize = Media->BlockSize >> 1)
70 {
71 Media->LastBlock = DivU64x32Remainder (PrivateData->Size, Media->BlockSize, &Remainder) - 1;
72 if (Remainder == 0) {
73 break;
74 }
75 }
76
77 ASSERT (Media->BlockSize != 0);
78
79 return;
80}
81
94EFIAPI
97 IN BOOLEAN ExtendedVerification
98 )
99{
100 return EFI_SUCCESS;
101}
102
129EFIAPI
132 IN UINT32 MediaId,
133 IN EFI_LBA Lba,
134 IN UINTN BufferSize,
135 OUT VOID *Buffer
136 )
137{
138 RAM_DISK_PRIVATE_DATA *PrivateData;
139 UINTN NumberOfBlocks;
140
141 PrivateData = RAM_DISK_PRIVATE_FROM_BLKIO (This);
142
143 if (MediaId != PrivateData->Media.MediaId) {
144 return EFI_MEDIA_CHANGED;
145 }
146
147 if (Buffer == NULL) {
148 return EFI_INVALID_PARAMETER;
149 }
150
151 if (BufferSize == 0) {
152 return EFI_SUCCESS;
153 }
154
155 if ((BufferSize % PrivateData->Media.BlockSize) != 0) {
156 return EFI_BAD_BUFFER_SIZE;
157 }
158
159 if (Lba > PrivateData->Media.LastBlock) {
160 return EFI_INVALID_PARAMETER;
161 }
162
163 NumberOfBlocks = BufferSize / PrivateData->Media.BlockSize;
164 if ((Lba + NumberOfBlocks - 1) > PrivateData->Media.LastBlock) {
165 return EFI_INVALID_PARAMETER;
166 }
167
168 CopyMem (
169 Buffer,
170 (VOID *)(UINTN)(PrivateData->StartingAddr + MultU64x32 (Lba, PrivateData->Media.BlockSize)),
171 BufferSize
172 );
173
174 return EFI_SUCCESS;
175}
176
203EFIAPI
206 IN UINT32 MediaId,
207 IN EFI_LBA Lba,
208 IN UINTN BufferSize,
209 IN VOID *Buffer
210 )
211{
212 RAM_DISK_PRIVATE_DATA *PrivateData;
213 UINTN NumberOfBlocks;
214
215 PrivateData = RAM_DISK_PRIVATE_FROM_BLKIO (This);
216
217 if (MediaId != PrivateData->Media.MediaId) {
218 return EFI_MEDIA_CHANGED;
219 }
220
221 if (TRUE == PrivateData->Media.ReadOnly) {
222 return EFI_WRITE_PROTECTED;
223 }
224
225 if (Buffer == NULL) {
226 return EFI_INVALID_PARAMETER;
227 }
228
229 if (BufferSize == 0) {
230 return EFI_SUCCESS;
231 }
232
233 if ((BufferSize % PrivateData->Media.BlockSize) != 0) {
234 return EFI_BAD_BUFFER_SIZE;
235 }
236
237 if (Lba > PrivateData->Media.LastBlock) {
238 return EFI_INVALID_PARAMETER;
239 }
240
241 NumberOfBlocks = BufferSize / PrivateData->Media.BlockSize;
242 if ((Lba + NumberOfBlocks - 1) > PrivateData->Media.LastBlock) {
243 return EFI_INVALID_PARAMETER;
244 }
245
246 CopyMem (
247 (VOID *)(UINTN)(PrivateData->StartingAddr + MultU64x32 (Lba, PrivateData->Media.BlockSize)),
248 Buffer,
249 BufferSize
250 );
251
252 return EFI_SUCCESS;
253}
254
267EFIAPI
270 )
271{
272 return EFI_SUCCESS;
273}
274
287EFIAPI
290 IN BOOLEAN ExtendedVerification
291 )
292{
293 return EFI_SUCCESS;
294}
295
330EFIAPI
333 IN UINT32 MediaId,
334 IN EFI_LBA Lba,
336 IN UINTN BufferSize,
337 OUT VOID *Buffer
338 )
339{
340 RAM_DISK_PRIVATE_DATA *PrivateData;
341 EFI_STATUS Status;
342
343 PrivateData = RAM_DISK_PRIVATE_FROM_BLKIO2 (This);
344
345 Status = RamDiskBlkIoReadBlocks (
346 &PrivateData->BlockIo,
347 MediaId,
348 Lba,
349 BufferSize,
350 Buffer
351 );
352 if (EFI_ERROR (Status)) {
353 return Status;
354 }
355
356 //
357 // If caller's event is given, signal it after the memory read completes.
358 //
359 if ((Token != NULL) && (Token->Event != NULL)) {
360 Token->TransactionStatus = EFI_SUCCESS;
361 gBS->SignalEvent (Token->Event);
362 }
363
364 return EFI_SUCCESS;
365}
366
400EFIAPI
403 IN UINT32 MediaId,
404 IN EFI_LBA Lba,
406 IN UINTN BufferSize,
407 IN VOID *Buffer
408 )
409{
410 RAM_DISK_PRIVATE_DATA *PrivateData;
411 EFI_STATUS Status;
412
413 PrivateData = RAM_DISK_PRIVATE_FROM_BLKIO2 (This);
414
415 Status = RamDiskBlkIoWriteBlocks (
416 &PrivateData->BlockIo,
417 MediaId,
418 Lba,
419 BufferSize,
420 Buffer
421 );
422 if (EFI_ERROR (Status)) {
423 return Status;
424 }
425
426 //
427 // If caller's event is given, signal it after the memory write completes.
428 //
429 if ((Token != NULL) && (Token->Event != NULL)) {
430 Token->TransactionStatus = EFI_SUCCESS;
431 gBS->SignalEvent (Token->Event);
432 }
433
434 return EFI_SUCCESS;
435}
436
457EFIAPI
461 )
462{
463 RAM_DISK_PRIVATE_DATA *PrivateData;
464
465 PrivateData = RAM_DISK_PRIVATE_FROM_BLKIO2 (This);
466
467 if (TRUE == PrivateData->Media.ReadOnly) {
468 return EFI_WRITE_PROTECTED;
469 }
470
471 //
472 // If caller's event is given, signal it directly.
473 //
474 if ((Token != NULL) && (Token->Event != NULL)) {
475 Token->TransactionStatus = EFI_SUCCESS;
476 gBS->SignalEvent (Token->Event);
477 }
478
479 return EFI_SUCCESS;
480}
UINT64 UINTN
UINT64 EFIAPI MultU64x32(IN UINT64 Multiplicand, IN UINT32 Multiplier)
Definition: MultU64x32.c:27
UINT64 EFIAPI DivU64x32Remainder(IN UINT64 Dividend, IN UINT32 Divisor, OUT UINT32 *Remainder OPTIONAL)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_STATUS EFIAPI RamDiskBlkIo2ReadBlocksEx(IN EFI_BLOCK_IO2_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN OUT EFI_BLOCK_IO2_TOKEN *Token, IN UINTN BufferSize, OUT VOID *Buffer)
EFI_STATUS EFIAPI RamDiskBlkIoFlushBlocks(IN EFI_BLOCK_IO_PROTOCOL *This)
EFI_STATUS EFIAPI RamDiskBlkIoWriteBlocks(IN EFI_BLOCK_IO_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN UINTN BufferSize, IN VOID *Buffer)
EFI_STATUS EFIAPI RamDiskBlkIoReadBlocks(IN EFI_BLOCK_IO_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer)
VOID RamDiskInitBlockIo(IN RAM_DISK_PRIVATE_DATA *PrivateData)
EFI_STATUS EFIAPI RamDiskBlkIo2WriteBlocksEx(IN EFI_BLOCK_IO2_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN OUT EFI_BLOCK_IO2_TOKEN *Token, IN UINTN BufferSize, IN VOID *Buffer)
EFI_STATUS EFIAPI RamDiskBlkIo2FlushBlocksEx(IN EFI_BLOCK_IO2_PROTOCOL *This, IN OUT EFI_BLOCK_IO2_TOKEN *Token)
EFI_STATUS EFIAPI RamDiskBlkIoReset(IN EFI_BLOCK_IO_PROTOCOL *This, IN BOOLEAN ExtendedVerification)
EFI_STATUS EFIAPI RamDiskBlkIo2Reset(IN EFI_BLOCK_IO2_PROTOCOL *This, IN BOOLEAN ExtendedVerification)
#define RAM_DISK_DEFAULT_BLOCK_SIZE
Definition: RamDiskImpl.h:49
UINT64 EFI_LBA
Definition: UefiBaseType.h:45
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_BLOCK_IO_MEDIA * Media
Definition: BlockIo2.h:187
EFI_BLOCK_IO_MEDIA * Media
Definition: BlockIo.h:224
BOOLEAN RemovableMedia
Definition: BlockIo.h:137
BOOLEAN LogicalPartition
Definition: BlockIo.h:150
UINT32 BlockSize
Definition: BlockIo.h:167
EFI_LBA LastBlock
Definition: BlockIo.h:178
BOOLEAN WriteCaching
Definition: BlockIo.h:161
BOOLEAN MediaPresent
Definition: BlockIo.h:144
BOOLEAN ReadOnly
Definition: BlockIo.h:156