3 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #define WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE SIGNATURE_32 ('N', 'T', 'b', 'k')
20 EMU_IO_THUNK_PROTOCOL
*Thunk
;
29 EFI_BLOCK_IO_MEDIA
*Media
;
30 EMU_BLOCK_IO_PROTOCOL EmuBlockIo
;
31 } WIN_NT_BLOCK_IO_PRIVATE
;
33 #define WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS(a) \
34 CR(a, WIN_NT_BLOCK_IO_PRIVATE, EmuBlockIo, WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE)
39 IN EMU_BLOCK_IO_PROTOCOL
*This
,
40 IN BOOLEAN ExtendedVerification
48 IN WIN_NT_BLOCK_IO_PRIVATE
*Private
,
49 IN INT64 DistanceToMove
,
50 OUT UINT64
*NewFilePointer
,
55 This function extends the capability of SetFilePointer to accept 64 bit parameters
60 LARGE_INTEGER LargeInt
;
62 LargeInt
.QuadPart
= DistanceToMove
;
65 LargeInt
.LowPart
= SetFilePointer (
72 if (LargeInt
.LowPart
== -1 && GetLastError () != NO_ERROR
) {
73 Status
= EFI_INVALID_PARAMETER
;
76 if (NewFilePointer
!= NULL
) {
77 *NewFilePointer
= LargeInt
.QuadPart
;
86 WinNtBlockIoOpenDevice (
87 IN WIN_NT_BLOCK_IO_PRIVATE
*Private
,
88 IN EFI_BLOCK_IO_MEDIA
*Media
95 // If the device is already opened, close it
97 if (Private
->NtHandle
!= INVALID_HANDLE_VALUE
) {
98 WinNtBlockIoReset (&Private
->EmuBlockIo
, FALSE
);
104 Private
->NtHandle
= CreateFile (
106 GENERIC_READ
| (Private
->Readonly
? 0 : GENERIC_WRITE
),
107 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
109 OPEN_ALWAYS
, // Create if it doesn't exist
114 if (Private
->NtHandle
== INVALID_HANDLE_VALUE
) {
115 DEBUG ((EFI_D_INFO
, "OpenBlock: Could not open %S, %x\n", Private
->FileName
, GetLastError ()));
116 Media
->MediaPresent
= FALSE
;
117 Status
= EFI_NO_MEDIA
;
122 // get the size of the file
124 Status
= SetFilePointer64 (Private
, 0, &FileSize
, FILE_END
);
126 if (EFI_ERROR (Status
)) {
127 DEBUG ((EFI_D_ERROR
, "OpenBlock: Could not get filesize of %s\n", Private
->FileName
));
128 Status
= EFI_UNSUPPORTED
;
132 Media
->LastBlock
= DivU64x32 (FileSize
, (UINT32
)Private
->BlockSize
) - 1;
134 DEBUG ((EFI_D_INIT
, "OpenBlock: opened %S\n", Private
->FileName
));
135 Status
= EFI_SUCCESS
;
138 if (EFI_ERROR (Status
)) {
139 if (Private
->NtHandle
!= INVALID_HANDLE_VALUE
) {
140 WinNtBlockIoReset (&Private
->EmuBlockIo
, FALSE
);
150 WinNtBlockIoCreateMapping (
151 IN EMU_BLOCK_IO_PROTOCOL
*This
,
152 IN EFI_BLOCK_IO_MEDIA
*Media
155 WIN_NT_BLOCK_IO_PRIVATE
*Private
;
157 Private
= WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
160 Media
->RemovableMedia
= Private
->Removable
;
161 Media
->MediaPresent
= TRUE
;
162 Media
->LogicalPartition
= FALSE
;
163 Media
->ReadOnly
= Private
->Readonly
;
164 Media
->WriteCaching
= FALSE
;
166 Media
->LastBlock
= 0; // Filled in by OpenDevice
167 Media
->BlockSize
= Private
->BlockSize
;
169 // EFI_BLOCK_IO_PROTOCOL_REVISION2
170 Media
->LowestAlignedLba
= 0;
171 Media
->LogicalBlocksPerPhysicalBlock
= 0;
174 // EFI_BLOCK_IO_PROTOCOL_REVISION3
175 Media
->OptimalTransferLengthGranularity
= 0;
178 // Remember the Media pointer.
180 Private
->Media
= Media
;
181 return WinNtBlockIoOpenDevice (Private
, Media
);
188 IN WIN_NT_BLOCK_IO_PRIVATE
*Private
194 TODO: Add function description
198 Private - TODO: add argument description
202 TODO: add return values
206 EFI_BLOCK_IO_MEDIA
*Media
;
209 Media
= Private
->Media
;
211 switch (GetLastError ()) {
213 case ERROR_NOT_READY
:
214 Media
->ReadOnly
= FALSE
;
215 Media
->MediaPresent
= FALSE
;
216 Status
= EFI_NO_MEDIA
;
219 case ERROR_WRONG_DISK
:
220 Media
->ReadOnly
= FALSE
;
221 Media
->MediaPresent
= TRUE
;
223 Status
= EFI_MEDIA_CHANGED
;
226 case ERROR_WRITE_PROTECT
:
227 Media
->ReadOnly
= TRUE
;
228 Status
= EFI_WRITE_PROTECTED
;
232 Status
= EFI_DEVICE_ERROR
;
236 if (Status
== EFI_NO_MEDIA
|| Status
== EFI_MEDIA_CHANGED
) {
237 WinNtBlockIoReset (&Private
->EmuBlockIo
, FALSE
);
246 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
251 if (Token
->Event
!= NULL
) {
252 // Caller is responcible for signaling EFI Event
253 Token
->TransactionStatus
= Status
;
261 Read BufferSize bytes from Lba into Buffer.
263 This function reads the requested number of blocks from the device. All the
264 blocks are read, or an error is returned.
265 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
266 non-blocking I/O is being used, the Event associated with this request will
269 @param[in] This Indicates a pointer to the calling context.
270 @param[in] MediaId Id of the media, changes every time the media is
272 @param[in] Lba The starting Logical Block Address to read from.
273 @param[in, out] Token A pointer to the token associated with the transaction.
274 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
275 @param[out] Buffer A pointer to the destination buffer for the data. The
276 caller is responsible for either having implicit or
277 explicit ownership of the buffer.
279 @retval EFI_SUCCESS The read request was queued if Token->Event is
280 not NULL.The data was read correctly from the
281 device if the Token->Event is NULL.
282 @retval EFI_DEVICE_ERROR The device reported an error while performing
284 @retval EFI_NO_MEDIA There is no media in the device.
285 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
286 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
287 intrinsic block size of the device.
288 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
289 or the buffer is not on proper alignment.
290 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
294 WinNtBlockIoReadBlocks (
295 IN EMU_BLOCK_IO_PROTOCOL
*This
,
298 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
303 WIN_NT_BLOCK_IO_PRIVATE
*Private
;
307 UINT64 DistanceToMove
;
308 UINT64 DistanceMoved
;
310 Private
= WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
313 // Seek to proper position
315 DistanceToMove
= MultU64x32 (Lba
, (UINT32
)Private
->BlockSize
);
316 Status
= SetFilePointer64 (Private
, DistanceToMove
, &DistanceMoved
, FILE_BEGIN
);
318 if (EFI_ERROR (Status
) || (DistanceToMove
!= DistanceMoved
)) {
319 DEBUG ((EFI_D_INIT
, "ReadBlocks: SetFilePointer failed\n"));
320 return WinNtBlockIoError (Private
->Media
);
323 Flag
= ReadFile (Private
->NtHandle
, Buffer
, (DWORD
)BufferSize
, (LPDWORD
)&BytesRead
, NULL
);
324 if (!Flag
|| (BytesRead
!= BufferSize
)) {
325 return WinNtBlockIoError (Private
->Media
);
328 Private
->Media
->MediaPresent
= TRUE
;
329 return WinNtSignalToken (Token
, EFI_SUCCESS
);
334 Write BufferSize bytes from Lba into Buffer.
336 This function writes the requested number of blocks to the device. All blocks
337 are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
338 EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
339 being used, the Event associated with this request will not be signaled.
341 @param[in] This Indicates a pointer to the calling context.
342 @param[in] MediaId The media ID that the write request is for.
343 @param[in] Lba The starting logical block address to be written. The
344 caller is responsible for writing to only legitimate
346 @param[in, out] Token A pointer to the token associated with the transaction.
347 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
348 @param[in] Buffer A pointer to the source buffer for the data.
350 @retval EFI_SUCCESS The write request was queued if Event is not NULL.
351 The data was written correctly to the device if
353 @retval EFI_WRITE_PROTECTED The device can not be written to.
354 @retval EFI_NO_MEDIA There is no media in the device.
355 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
356 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
357 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
358 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
359 or the buffer is not on proper alignment.
360 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
365 WinNtBlockIoWriteBlocks (
366 IN EMU_BLOCK_IO_PROTOCOL
*This
,
369 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
374 WIN_NT_BLOCK_IO_PRIVATE
*Private
;
378 UINT64 DistanceToMove
;
379 UINT64 DistanceMoved
;
381 Private
= WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
384 // Seek to proper position
386 DistanceToMove
= MultU64x32 (Lba
, (UINT32
)Private
->BlockSize
);
387 Status
= SetFilePointer64 (Private
, DistanceToMove
, &DistanceMoved
, FILE_BEGIN
);
389 if (EFI_ERROR (Status
) || (DistanceToMove
!= DistanceMoved
)) {
390 DEBUG ((EFI_D_INIT
, "WriteBlocks: SetFilePointer failed\n"));
391 return WinNtBlockIoError (Private
->Media
);
394 Success
= WriteFile (Private
->NtHandle
, Buffer
, (DWORD
)BufferSize
, (LPDWORD
)&BytesWritten
, NULL
);
395 if (!Success
|| (BytesWritten
!= BufferSize
)) {
396 return WinNtBlockIoError (Private
->Media
);
400 // If the write succeeded, we are not write protected and media is present.
402 Private
->Media
->MediaPresent
= TRUE
;
403 Private
->Media
->ReadOnly
= FALSE
;
404 return WinNtSignalToken (Token
, EFI_SUCCESS
);
408 Flush the Block Device.
410 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
411 is returned and non-blocking I/O is being used, the Event associated with
412 this request will not be signaled.
414 @param[in] This Indicates a pointer to the calling context.
415 @param[in,out] Token A pointer to the token associated with the transaction
417 @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
418 All outstanding data was written correctly to the
419 device if the Event is NULL.
420 @retval EFI_DEVICE_ERROR The device reported an error while writting back
422 @retval EFI_WRITE_PROTECTED The device cannot be written to.
423 @retval EFI_NO_MEDIA There is no media in the device.
424 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
425 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
430 WinNtBlockIoFlushBlocks (
431 IN EMU_BLOCK_IO_PROTOCOL
*This
,
432 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
435 return WinNtSignalToken (Token
, EFI_SUCCESS
);
440 Reset the block device hardware.
442 @param[in] This Indicates a pointer to the calling context.
443 @param[in] ExtendedVerification Indicates that the driver may perform a more
444 exhausive verfication operation of the device
447 @retval EFI_SUCCESS The device was reset.
448 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
454 IN EMU_BLOCK_IO_PROTOCOL
*This
,
455 IN BOOLEAN ExtendedVerification
458 WIN_NT_BLOCK_IO_PRIVATE
*Private
;
460 Private
= WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
462 if (Private
->NtHandle
!= INVALID_HANDLE_VALUE
) {
463 CloseHandle (Private
->NtHandle
);
464 Private
->NtHandle
= INVALID_HANDLE_VALUE
;
470 EMU_BLOCK_IO_PROTOCOL gEmuBlockIoProtocol
= {
472 WinNtBlockIoReadBlocks
,
473 WinNtBlockIoWriteBlocks
,
474 WinNtBlockIoFlushBlocks
,
475 WinNtBlockIoCreateMapping
480 WinNtBlockIoThunkOpen (
481 IN EMU_IO_THUNK_PROTOCOL
*This
484 WIN_NT_BLOCK_IO_PRIVATE
*Private
;
487 Private
= AllocatePool (sizeof (*Private
));
488 if (Private
== NULL
) {
489 return EFI_OUT_OF_RESOURCES
;
492 Private
->Signature
= WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE
;
493 Private
->Thunk
= This
;
494 CopyMem (&Private
->EmuBlockIo
, &gEmuBlockIoProtocol
, sizeof (gEmuBlockIoProtocol
));
495 Private
->BlockSize
= 512;
496 Private
->NtHandle
= INVALID_HANDLE_VALUE
;
498 Private
->FileName
= AllocateCopyPool (StrSize (This
->ConfigString
), This
->ConfigString
);
499 if (Private
->FileName
== NULL
) {
500 return EFI_OUT_OF_RESOURCES
;
503 // Parse ConfigString
504 // <ConfigString> := <FileName> ':' [RF][OW] ':' <BlockSize>
506 Str
= StrStr (Private
->FileName
, L
":");
508 Private
->Removable
= FALSE
;
509 Private
->Readonly
= FALSE
;
511 for (*Str
++ = L
'\0'; *Str
!= L
'\0'; Str
++) {
512 if (*Str
== 'R' || *Str
== 'F') {
513 Private
->Removable
= (BOOLEAN
) (*Str
== L
'R');
515 if (*Str
== 'O' || *Str
== 'W') {
516 Private
->Readonly
= (BOOLEAN
) (*Str
== L
'O');
519 Private
->BlockSize
= wcstol (++Str
, NULL
, 0);
525 This
->Interface
= &Private
->EmuBlockIo
;
526 This
->Private
= Private
;
533 WinNtBlockIoThunkClose (
534 IN EMU_IO_THUNK_PROTOCOL
*This
537 WIN_NT_BLOCK_IO_PRIVATE
*Private
;
539 Private
= This
->Private
;
541 if (Private
!= NULL
) {
542 if (Private
->FileName
!= NULL
) {
543 FreePool (Private
->FileName
);
553 EMU_IO_THUNK_PROTOCOL mWinNtBlockIoThunkIo
= {
554 &gEmuBlockIoProtocolGuid
,
558 WinNtBlockIoThunkOpen
,
559 WinNtBlockIoThunkClose
,