]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Win/Host/WinBlockIo.c
bae907c7052170b5e768f6107469a74172ef29a6
[mirror_edk2.git] / EmulatorPkg / Win / Host / WinBlockIo.c
1 /**@file
2
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
8
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.
11
12 **/
13
14 #include "WinHost.h"
15
16 #define WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE SIGNATURE_32 ('N', 'T', 'b', 'k')
17 typedef struct {
18 UINTN Signature;
19
20 EMU_IO_THUNK_PROTOCOL *Thunk;
21
22 CHAR16 *FileName;
23 BOOLEAN Removable;
24 BOOLEAN Readonly;
25
26 HANDLE NtHandle;
27 UINT32 BlockSize;
28
29 EFI_BLOCK_IO_MEDIA *Media;
30 EMU_BLOCK_IO_PROTOCOL EmuBlockIo;
31 } WIN_NT_BLOCK_IO_PRIVATE;
32
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)
35
36
37 EFI_STATUS
38 WinNtBlockIoReset (
39 IN EMU_BLOCK_IO_PROTOCOL *This,
40 IN BOOLEAN ExtendedVerification
41 );
42
43
44
45
46 EFI_STATUS
47 SetFilePointer64 (
48 IN WIN_NT_BLOCK_IO_PRIVATE *Private,
49 IN INT64 DistanceToMove,
50 OUT UINT64 *NewFilePointer,
51 IN DWORD MoveMethod
52 )
53 /*++
54
55 This function extends the capability of SetFilePointer to accept 64 bit parameters
56
57 --*/
58 {
59 EFI_STATUS Status;
60 LARGE_INTEGER LargeInt;
61
62 LargeInt.QuadPart = DistanceToMove;
63 Status = EFI_SUCCESS;
64
65 LargeInt.LowPart = SetFilePointer (
66 Private->NtHandle,
67 LargeInt.LowPart,
68 &LargeInt.HighPart,
69 MoveMethod
70 );
71
72 if (LargeInt.LowPart == -1 && GetLastError () != NO_ERROR) {
73 Status = EFI_INVALID_PARAMETER;
74 }
75
76 if (NewFilePointer != NULL) {
77 *NewFilePointer = LargeInt.QuadPart;
78 }
79
80 return Status;
81 }
82
83
84
85 EFI_STATUS
86 WinNtBlockIoOpenDevice (
87 IN WIN_NT_BLOCK_IO_PRIVATE *Private,
88 IN EFI_BLOCK_IO_MEDIA *Media
89 )
90 {
91 EFI_STATUS Status;
92 UINT64 FileSize;
93
94 //
95 // If the device is already opened, close it
96 //
97 if (Private->NtHandle != INVALID_HANDLE_VALUE) {
98 WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);
99 }
100
101 //
102 // Open the device
103 //
104 Private->NtHandle = CreateFile (
105 Private->FileName,
106 GENERIC_READ | (Private->Readonly ? 0 : GENERIC_WRITE),
107 FILE_SHARE_READ | FILE_SHARE_WRITE,
108 NULL,
109 OPEN_ALWAYS, // Create if it doesn't exist
110 0,
111 NULL
112 );
113
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;
118 goto Done;
119 }
120
121 //
122 // get the size of the file
123 //
124 Status = SetFilePointer64 (Private, 0, &FileSize, FILE_END);
125
126 if (EFI_ERROR (Status)) {
127 DEBUG ((EFI_D_ERROR, "OpenBlock: Could not get filesize of %s\n", Private->FileName));
128 Status = EFI_UNSUPPORTED;
129 goto Done;
130 }
131
132 Media->LastBlock = DivU64x32 (FileSize, (UINT32)Private->BlockSize) - 1;
133
134 DEBUG ((EFI_D_INIT, "OpenBlock: opened %S\n", Private->FileName));
135 Status = EFI_SUCCESS;
136
137 Done:
138 if (EFI_ERROR (Status)) {
139 if (Private->NtHandle != INVALID_HANDLE_VALUE) {
140 WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);
141 }
142 }
143
144 return Status;
145 }
146
147
148 EFI_STATUS
149 EFIAPI
150 WinNtBlockIoCreateMapping (
151 IN EMU_BLOCK_IO_PROTOCOL *This,
152 IN EFI_BLOCK_IO_MEDIA *Media
153 )
154 {
155 WIN_NT_BLOCK_IO_PRIVATE *Private;
156
157 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
158
159 Media->MediaId = 0;
160 Media->RemovableMedia = Private->Removable;
161 Media->MediaPresent = TRUE;
162 Media->LogicalPartition = FALSE;
163 Media->ReadOnly = Private->Readonly;
164 Media->WriteCaching = FALSE;
165 Media->IoAlign = 1;
166 Media->LastBlock = 0; // Filled in by OpenDevice
167 Media->BlockSize = Private->BlockSize;
168
169 // EFI_BLOCK_IO_PROTOCOL_REVISION2
170 Media->LowestAlignedLba = 0;
171 Media->LogicalBlocksPerPhysicalBlock = 0;
172
173
174 // EFI_BLOCK_IO_PROTOCOL_REVISION3
175 Media->OptimalTransferLengthGranularity = 0;
176
177 //
178 // Remember the Media pointer.
179 //
180 Private->Media = Media;
181 return WinNtBlockIoOpenDevice (Private, Media);
182 }
183
184
185
186 EFI_STATUS
187 WinNtBlockIoError (
188 IN WIN_NT_BLOCK_IO_PRIVATE *Private
189 )
190 /*++
191
192 Routine Description:
193
194 TODO: Add function description
195
196 Arguments:
197
198 Private - TODO: add argument description
199
200 Returns:
201
202 TODO: add return values
203
204 --*/
205 {
206 EFI_BLOCK_IO_MEDIA *Media;
207 EFI_STATUS Status;
208
209 Media = Private->Media;
210
211 switch (GetLastError ()) {
212
213 case ERROR_NOT_READY:
214 Media->ReadOnly = FALSE;
215 Media->MediaPresent = FALSE;
216 Status = EFI_NO_MEDIA;
217 break;
218
219 case ERROR_WRONG_DISK:
220 Media->ReadOnly = FALSE;
221 Media->MediaPresent = TRUE;
222 Media->MediaId++;
223 Status = EFI_MEDIA_CHANGED;
224 break;
225
226 case ERROR_WRITE_PROTECT:
227 Media->ReadOnly = TRUE;
228 Status = EFI_WRITE_PROTECTED;
229 break;
230
231 default:
232 Status = EFI_DEVICE_ERROR;
233 break;
234 }
235
236 if (Status == EFI_NO_MEDIA || Status == EFI_MEDIA_CHANGED) {
237 WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);
238 }
239
240 return Status;
241 }
242
243
244 EFI_STATUS
245 WinNtSignalToken (
246 IN OUT EFI_BLOCK_IO2_TOKEN *Token,
247 IN EFI_STATUS Status
248 )
249 {
250 if (Token != NULL) {
251 if (Token->Event != NULL) {
252 // Caller is responcible for signaling EFI Event
253 Token->TransactionStatus = Status;
254 return EFI_SUCCESS;
255 }
256 }
257 return Status;
258 }
259
260 /**
261 Read BufferSize bytes from Lba into Buffer.
262
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
267 not be signaled.
268
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
271 replaced.
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.
278
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
283 the read.
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
291 of resources.
292 **/
293 EFI_STATUS
294 WinNtBlockIoReadBlocks (
295 IN EMU_BLOCK_IO_PROTOCOL *This,
296 IN UINT32 MediaId,
297 IN EFI_LBA Lba,
298 IN OUT EFI_BLOCK_IO2_TOKEN *Token,
299 IN UINTN BufferSize,
300 OUT VOID *Buffer
301 )
302 {
303 WIN_NT_BLOCK_IO_PRIVATE *Private;
304 BOOL Flag;
305 EFI_STATUS Status;
306 DWORD BytesRead;
307 UINT64 DistanceToMove;
308 UINT64 DistanceMoved;
309
310 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
311
312 //
313 // Seek to proper position
314 //
315 DistanceToMove = MultU64x32 (Lba, (UINT32)Private->BlockSize);
316 Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, FILE_BEGIN);
317
318 if (EFI_ERROR (Status) || (DistanceToMove != DistanceMoved)) {
319 DEBUG ((EFI_D_INIT, "ReadBlocks: SetFilePointer failed\n"));
320 return WinNtBlockIoError (Private->Media);
321 }
322
323 Flag = ReadFile (Private->NtHandle, Buffer, (DWORD)BufferSize, (LPDWORD)&BytesRead, NULL);
324 if (!Flag || (BytesRead != BufferSize)) {
325 return WinNtBlockIoError (Private->Media);
326 }
327
328 Private->Media->MediaPresent = TRUE;
329 return WinNtSignalToken (Token, EFI_SUCCESS);
330 }
331
332
333 /**
334 Write BufferSize bytes from Lba into Buffer.
335
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.
340
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
345 locations.
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.
349
350 @retval EFI_SUCCESS The write request was queued if Event is not NULL.
351 The data was written correctly to the device if
352 the Event is NULL.
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
361 of resources.
362
363 **/
364 EFI_STATUS
365 WinNtBlockIoWriteBlocks (
366 IN EMU_BLOCK_IO_PROTOCOL *This,
367 IN UINT32 MediaId,
368 IN EFI_LBA Lba,
369 IN OUT EFI_BLOCK_IO2_TOKEN *Token,
370 IN UINTN BufferSize,
371 IN VOID *Buffer
372 )
373 {
374 WIN_NT_BLOCK_IO_PRIVATE *Private;
375 UINTN BytesWritten;
376 BOOL Success;
377 EFI_STATUS Status;
378 UINT64 DistanceToMove;
379 UINT64 DistanceMoved;
380
381 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
382
383 //
384 // Seek to proper position
385 //
386 DistanceToMove = MultU64x32 (Lba, (UINT32)Private->BlockSize);
387 Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, FILE_BEGIN);
388
389 if (EFI_ERROR (Status) || (DistanceToMove != DistanceMoved)) {
390 DEBUG ((EFI_D_INIT, "WriteBlocks: SetFilePointer failed\n"));
391 return WinNtBlockIoError (Private->Media);
392 }
393
394 Success = WriteFile (Private->NtHandle, Buffer, (DWORD)BufferSize, (LPDWORD)&BytesWritten, NULL);
395 if (!Success || (BytesWritten != BufferSize)) {
396 return WinNtBlockIoError (Private->Media);
397 }
398
399 //
400 // If the write succeeded, we are not write protected and media is present.
401 //
402 Private->Media->MediaPresent = TRUE;
403 Private->Media->ReadOnly = FALSE;
404 return WinNtSignalToken (Token, EFI_SUCCESS);
405 }
406
407 /**
408 Flush the Block Device.
409
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.
413
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
416
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
421 the data.
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
426 of resources.
427
428 **/
429 EFI_STATUS
430 WinNtBlockIoFlushBlocks (
431 IN EMU_BLOCK_IO_PROTOCOL *This,
432 IN OUT EFI_BLOCK_IO2_TOKEN *Token
433 )
434 {
435 return WinNtSignalToken (Token, EFI_SUCCESS);
436 }
437
438
439 /**
440 Reset the block device hardware.
441
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
445 during reset.
446
447 @retval EFI_SUCCESS The device was reset.
448 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
449 not be reset.
450
451 **/
452 EFI_STATUS
453 WinNtBlockIoReset (
454 IN EMU_BLOCK_IO_PROTOCOL *This,
455 IN BOOLEAN ExtendedVerification
456 )
457 {
458 WIN_NT_BLOCK_IO_PRIVATE *Private;
459
460 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
461
462 if (Private->NtHandle != INVALID_HANDLE_VALUE) {
463 CloseHandle (Private->NtHandle);
464 Private->NtHandle = INVALID_HANDLE_VALUE;
465 }
466
467 return EFI_SUCCESS;
468 }
469
470 EMU_BLOCK_IO_PROTOCOL gEmuBlockIoProtocol = {
471 WinNtBlockIoReset,
472 WinNtBlockIoReadBlocks,
473 WinNtBlockIoWriteBlocks,
474 WinNtBlockIoFlushBlocks,
475 WinNtBlockIoCreateMapping
476 };
477
478 EFI_STATUS
479 EFIAPI
480 WinNtBlockIoThunkOpen (
481 IN EMU_IO_THUNK_PROTOCOL *This
482 )
483 {
484 WIN_NT_BLOCK_IO_PRIVATE *Private;
485 CHAR16 *Str;
486
487 Private = AllocatePool (sizeof (*Private));
488 if (Private == NULL) {
489 return EFI_OUT_OF_RESOURCES;
490 }
491
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;
497
498 Private->FileName = AllocateCopyPool (StrSize (This->ConfigString), This->ConfigString);
499 if (Private->FileName == NULL) {
500 return EFI_OUT_OF_RESOURCES;
501 }
502 //
503 // Parse ConfigString
504 // <ConfigString> := <FileName> ':' [RF][OW] ':' <BlockSize>
505 //
506 Str = StrStr (Private->FileName, L":");
507 if (Str == NULL) {
508 Private->Removable = FALSE;
509 Private->Readonly = FALSE;
510 } else {
511 for (*Str++ = L'\0'; *Str != L'\0'; Str++) {
512 if (*Str == 'R' || *Str == 'F') {
513 Private->Removable = (BOOLEAN) (*Str == L'R');
514 }
515 if (*Str == 'O' || *Str == 'W') {
516 Private->Readonly = (BOOLEAN) (*Str == L'O');
517 }
518 if (*Str == ':') {
519 Private->BlockSize = wcstol (++Str, NULL, 0);
520 break;
521 }
522 }
523 }
524
525 This->Interface = &Private->EmuBlockIo;
526 This->Private = Private;
527 return EFI_SUCCESS;
528 }
529
530
531 EFI_STATUS
532 EFIAPI
533 WinNtBlockIoThunkClose (
534 IN EMU_IO_THUNK_PROTOCOL *This
535 )
536 {
537 WIN_NT_BLOCK_IO_PRIVATE *Private;
538
539 Private = This->Private;
540
541 if (Private != NULL) {
542 if (Private->FileName != NULL) {
543 FreePool (Private->FileName);
544 }
545 FreePool (Private);
546 }
547
548 return EFI_SUCCESS;
549 }
550
551
552
553 EMU_IO_THUNK_PROTOCOL mWinNtBlockIoThunkIo = {
554 &gEmuBlockIoProtocolGuid,
555 NULL,
556 NULL,
557 0,
558 WinNtBlockIoThunkOpen,
559 WinNtBlockIoThunkClose,
560 NULL
561 };
562
563