]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinBlockIo.c
EmulatorPkg: formalize line endings
[mirror_edk2.git] / EmulatorPkg / Win / Host / WinBlockIo.c
CommitLineData
8f819697
RN
1/**@file\r
2\r
3Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12**/\r
13\r
14#include "WinHost.h"\r
15\r
16#define WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE SIGNATURE_32 ('N', 'T', 'b', 'k')\r
17typedef struct {\r
18 UINTN Signature;\r
19\r
20 EMU_IO_THUNK_PROTOCOL *Thunk;\r
21\r
22 CHAR16 *FileName;\r
23 BOOLEAN Removable;\r
24 BOOLEAN Readonly;\r
25\r
26 HANDLE NtHandle;\r
27 UINTN BlockSize;\r
28\r
29 EFI_BLOCK_IO_MEDIA *Media;\r
30 EMU_BLOCK_IO_PROTOCOL EmuBlockIo;\r
31} WIN_NT_BLOCK_IO_PRIVATE;\r
32\r
33#define WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS(a) \\r
34 CR(a, WIN_NT_BLOCK_IO_PRIVATE, EmuBlockIo, WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE)\r
35\r
36\r
37EFI_STATUS\r
38WinNtBlockIoReset (\r
39 IN EMU_BLOCK_IO_PROTOCOL *This,\r
40 IN BOOLEAN ExtendedVerification\r
41 );\r
42\r
43\r
44\r
45\r
46EFI_STATUS\r
47SetFilePointer64 (\r
48 IN WIN_NT_BLOCK_IO_PRIVATE *Private,\r
49 IN INT64 DistanceToMove,\r
50 OUT UINT64 *NewFilePointer,\r
51 IN DWORD MoveMethod\r
52)\r
53/*++\r
54\r
55This function extends the capability of SetFilePointer to accept 64 bit parameters\r
56\r
57--*/\r
58{\r
59 EFI_STATUS Status;\r
60 LARGE_INTEGER LargeInt;\r
61\r
62 LargeInt.QuadPart = DistanceToMove;\r
63 Status = EFI_SUCCESS;\r
64\r
65 LargeInt.LowPart = SetFilePointer (\r
66 Private->NtHandle,\r
67 LargeInt.LowPart,\r
68 &LargeInt.HighPart,\r
69 MoveMethod\r
70 );\r
71\r
72 if (LargeInt.LowPart == -1 && GetLastError () != NO_ERROR) {\r
73 Status = EFI_INVALID_PARAMETER;\r
74 }\r
75\r
76 if (NewFilePointer != NULL) {\r
77 *NewFilePointer = LargeInt.QuadPart;\r
78 }\r
79\r
80 return Status;\r
81}\r
82\r
83\r
84\r
85EFI_STATUS\r
86WinNtBlockIoOpenDevice (\r
87 IN WIN_NT_BLOCK_IO_PRIVATE *Private,\r
88 IN EFI_BLOCK_IO_MEDIA *Media\r
89 )\r
90{\r
91 EFI_STATUS Status;\r
92 UINT64 FileSize;\r
93\r
94 //\r
95 // If the device is already opened, close it\r
96 //\r
97 if (Private->NtHandle != INVALID_HANDLE_VALUE) {\r
98 WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);\r
99 }\r
100\r
101 //\r
102 // Open the device\r
103 //\r
104 Private->NtHandle = CreateFile (\r
105 Private->FileName,\r
106 GENERIC_READ | (Private->Readonly ? 0 : GENERIC_WRITE),\r
107 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
108 NULL,\r
109 OPEN_ALWAYS, // Create if it doesn't exist\r
110 0,\r
111 NULL\r
112 );\r
113\r
114 if (Private->NtHandle == INVALID_HANDLE_VALUE) {\r
115 DEBUG ((EFI_D_INFO, "OpenBlock: Could not open %S, %x\n", Private->FileName, GetLastError ()));\r
116 Media->MediaPresent = FALSE;\r
117 Status = EFI_NO_MEDIA;\r
118 goto Done;\r
119 }\r
120\r
121 //\r
122 // get the size of the file\r
123 //\r
124 Status = SetFilePointer64 (Private, 0, &FileSize, FILE_END);\r
125\r
126 if (EFI_ERROR (Status)) {\r
127 DEBUG ((EFI_D_ERROR, "OpenBlock: Could not get filesize of %s\n", Private->FileName));\r
128 Status = EFI_UNSUPPORTED;\r
129 goto Done;\r
130 }\r
131\r
132 Media->LastBlock = DivU64x32 (FileSize, (UINT32)Private->BlockSize) - 1;\r
133\r
134 DEBUG ((EFI_D_INIT, "OpenBlock: opened %S\n", Private->FileName));\r
135 Status = EFI_SUCCESS;\r
136\r
137Done:\r
138 if (EFI_ERROR (Status)) {\r
139 if (Private->NtHandle != INVALID_HANDLE_VALUE) {\r
140 WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);\r
141 }\r
142 }\r
143\r
144 return Status;\r
145}\r
146\r
147\r
148EFI_STATUS\r
149EFIAPI\r
150WinNtBlockIoCreateMapping (\r
151 IN EMU_BLOCK_IO_PROTOCOL *This,\r
152 IN EFI_BLOCK_IO_MEDIA *Media\r
153 )\r
154{\r
155 WIN_NT_BLOCK_IO_PRIVATE *Private;\r
156\r
157 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);\r
158\r
159 Media->MediaId = 0;\r
160 Media->RemovableMedia = Private->Removable;\r
161 Media->MediaPresent = TRUE;\r
162 Media->LogicalPartition = FALSE;\r
163 Media->ReadOnly = Private->Readonly;\r
164 Media->WriteCaching = FALSE;\r
165 Media->IoAlign = 1;\r
166 Media->LastBlock = 0; // Filled in by OpenDevice\r
167 Media->BlockSize = Private->BlockSize;\r
168\r
169 // EFI_BLOCK_IO_PROTOCOL_REVISION2\r
170 Media->LowestAlignedLba = 0;\r
171 Media->LogicalBlocksPerPhysicalBlock = 0;\r
172\r
173\r
174 // EFI_BLOCK_IO_PROTOCOL_REVISION3\r
175 Media->OptimalTransferLengthGranularity = 0;\r
176\r
177 //\r
178 // Remember the Media pointer.\r
179 //\r
180 Private->Media = Media;\r
181 return WinNtBlockIoOpenDevice (Private, Media);\r
182}\r
183\r
184\r
185\r
186EFI_STATUS\r
187WinNtBlockIoError (\r
188 IN WIN_NT_BLOCK_IO_PRIVATE *Private\r
189)\r
190/*++\r
191\r
192Routine Description:\r
193\r
194 TODO: Add function description\r
195\r
196Arguments:\r
197\r
198 Private - TODO: add argument description\r
199\r
200Returns:\r
201\r
202 TODO: add return values\r
203\r
204--*/\r
205{\r
206 EFI_BLOCK_IO_MEDIA *Media;\r
207 EFI_STATUS Status;\r
208\r
209 Media = Private->Media;\r
210\r
211 switch (GetLastError ()) {\r
212\r
213 case ERROR_NOT_READY:\r
214 Media->ReadOnly = FALSE;\r
215 Media->MediaPresent = FALSE;\r
216 Status = EFI_NO_MEDIA;\r
217 break;\r
218\r
219 case ERROR_WRONG_DISK:\r
220 Media->ReadOnly = FALSE;\r
221 Media->MediaPresent = TRUE;\r
222 Media->MediaId++;\r
223 Status = EFI_MEDIA_CHANGED;\r
224 break;\r
225\r
226 case ERROR_WRITE_PROTECT:\r
227 Media->ReadOnly = TRUE;\r
228 Status = EFI_WRITE_PROTECTED;\r
229 break;\r
230\r
231 default:\r
232 Status = EFI_DEVICE_ERROR;\r
233 break;\r
234 }\r
235\r
236 if (Status == EFI_NO_MEDIA || Status == EFI_MEDIA_CHANGED) {\r
237 WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);\r
238 }\r
239\r
240 return Status;\r
241}\r
242\r
243\r
244EFI_STATUS\r
245WinNtSignalToken (\r
246 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
247 IN EFI_STATUS Status\r
248)\r
249{\r
250 if (Token != NULL) {\r
251 if (Token->Event != NULL) {\r
252 // Caller is responcible for signaling EFI Event\r
253 Token->TransactionStatus = Status;\r
254 return EFI_SUCCESS;\r
255 }\r
256 }\r
257 return Status;\r
258}\r
259\r
260/**\r
261 Read BufferSize bytes from Lba into Buffer.\r
262\r
263 This function reads the requested number of blocks from the device. All the\r
264 blocks are read, or an error is returned.\r
265 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and\r
266 non-blocking I/O is being used, the Event associated with this request will\r
267 not be signaled.\r
268\r
269 @param[in] This Indicates a pointer to the calling context.\r
270 @param[in] MediaId Id of the media, changes every time the media is\r
271 replaced.\r
272 @param[in] Lba The starting Logical Block Address to read from.\r
79e4f2a5 273 @param[in, out] Token A pointer to the token associated with the transaction.\r
8f819697
RN
274 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
275 @param[out] Buffer A pointer to the destination buffer for the data. The\r
276 caller is responsible for either having implicit or\r
277 explicit ownership of the buffer.\r
278\r
279 @retval EFI_SUCCESS The read request was queued if Token->Event is\r
280 not NULL.The data was read correctly from the\r
281 device if the Token->Event is NULL.\r
282 @retval EFI_DEVICE_ERROR The device reported an error while performing\r
283 the read.\r
284 @retval EFI_NO_MEDIA There is no media in the device.\r
285 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
286 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the\r
287 intrinsic block size of the device.\r
288 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
289 or the buffer is not on proper alignment.\r
290 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
291 of resources.\r
292**/\r
293EFI_STATUS\r
294WinNtBlockIoReadBlocks (\r
295 IN EMU_BLOCK_IO_PROTOCOL *This,\r
296 IN UINT32 MediaId,\r
297 IN EFI_LBA Lba,\r
298 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
299 IN UINTN BufferSize,\r
300 OUT VOID *Buffer\r
301 )\r
302{\r
303 WIN_NT_BLOCK_IO_PRIVATE *Private;\r
304 BOOL Flag;\r
305 EFI_STATUS Status;\r
306 DWORD BytesRead;\r
307 UINT64 DistanceToMove;\r
308 UINT64 DistanceMoved;\r
309\r
310 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);\r
311\r
312 //\r
313 // Seek to proper position\r
314 //\r
315 DistanceToMove = MultU64x32 (Lba, (UINT32)Private->BlockSize);\r
316 Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, FILE_BEGIN);\r
317\r
318 if (EFI_ERROR (Status) || (DistanceToMove != DistanceMoved)) {\r
319 DEBUG ((EFI_D_INIT, "ReadBlocks: SetFilePointer failed\n"));\r
320 return WinNtBlockIoError (Private->Media);\r
321 }\r
322\r
323 Flag = ReadFile (Private->NtHandle, Buffer, (DWORD)BufferSize, (LPDWORD)&BytesRead, NULL);\r
324 if (!Flag || (BytesRead != BufferSize)) {\r
325 return WinNtBlockIoError (Private->Media);\r
326 }\r
327\r
328 Private->Media->MediaPresent = TRUE;\r
329 return WinNtSignalToken (Token, EFI_SUCCESS);\r
330}\r
331\r
332\r
333/**\r
334 Write BufferSize bytes from Lba into Buffer.\r
335\r
336 This function writes the requested number of blocks to the device. All blocks\r
337 are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,\r
338 EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is\r
339 being used, the Event associated with this request will not be signaled.\r
340\r
341 @param[in] This Indicates a pointer to the calling context.\r
342 @param[in] MediaId The media ID that the write request is for.\r
343 @param[in] Lba The starting logical block address to be written. The\r
344 caller is responsible for writing to only legitimate\r
345 locations.\r
346 @param[in, out] Token A pointer to the token associated with the transaction.\r
347 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
348 @param[in] Buffer A pointer to the source buffer for the data.\r
349\r
350 @retval EFI_SUCCESS The write request was queued if Event is not NULL.\r
351 The data was written correctly to the device if\r
352 the Event is NULL.\r
353 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
354 @retval EFI_NO_MEDIA There is no media in the device.\r
355 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
356 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
357 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
358 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
359 or the buffer is not on proper alignment.\r
360 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
361 of resources.\r
362\r
363**/\r
364EFI_STATUS\r
365WinNtBlockIoWriteBlocks (\r
366 IN EMU_BLOCK_IO_PROTOCOL *This,\r
367 IN UINT32 MediaId,\r
368 IN EFI_LBA Lba,\r
369 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
370 IN UINTN BufferSize,\r
371 IN VOID *Buffer\r
372 )\r
373{\r
374 WIN_NT_BLOCK_IO_PRIVATE *Private;\r
375 UINTN BytesWritten;\r
376 BOOL Success;\r
377 EFI_STATUS Status;\r
378 UINT64 DistanceToMove;\r
379 UINT64 DistanceMoved;\r
380\r
381 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);\r
382\r
383 //\r
384 // Seek to proper position\r
385 //\r
386 DistanceToMove = MultU64x32 (Lba, (UINT32)Private->BlockSize);\r
387 Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, FILE_BEGIN);\r
388\r
389 if (EFI_ERROR (Status) || (DistanceToMove != DistanceMoved)) {\r
390 DEBUG ((EFI_D_INIT, "WriteBlocks: SetFilePointer failed\n"));\r
391 return WinNtBlockIoError (Private->Media);\r
392 }\r
393\r
394 Success = WriteFile (Private->NtHandle, Buffer, (DWORD)BufferSize, (LPDWORD)&BytesWritten, NULL);\r
395 if (!Success || (BytesWritten != BufferSize)) {\r
396 return WinNtBlockIoError (Private->Media);\r
397 }\r
398\r
399 //\r
400 // If the write succeeded, we are not write protected and media is present.\r
401 //\r
402 Private->Media->MediaPresent = TRUE;\r
403 Private->Media->ReadOnly = FALSE;\r
404 return WinNtSignalToken (Token, EFI_SUCCESS);\r
405}\r
406\r
407/**\r
408 Flush the Block Device.\r
409\r
410 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED\r
411 is returned and non-blocking I/O is being used, the Event associated with\r
412 this request will not be signaled.\r
413\r
414 @param[in] This Indicates a pointer to the calling context.\r
415 @param[in,out] Token A pointer to the token associated with the transaction\r
416\r
417 @retval EFI_SUCCESS The flush request was queued if Event is not NULL.\r
418 All outstanding data was written correctly to the\r
419 device if the Event is NULL.\r
420 @retval EFI_DEVICE_ERROR The device reported an error while writting back\r
421 the data.\r
422 @retval EFI_WRITE_PROTECTED The device cannot be written to.\r
423 @retval EFI_NO_MEDIA There is no media in the device.\r
424 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
425 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
426 of resources.\r
427\r
428**/\r
429EFI_STATUS\r
430WinNtBlockIoFlushBlocks (\r
431 IN EMU_BLOCK_IO_PROTOCOL *This,\r
432 IN OUT EFI_BLOCK_IO2_TOKEN *Token\r
433 )\r
434{\r
435 return WinNtSignalToken (Token, EFI_SUCCESS);\r
436}\r
437\r
438\r
439/**\r
440 Reset the block device hardware.\r
441\r
442 @param[in] This Indicates a pointer to the calling context.\r
443 @param[in] ExtendedVerification Indicates that the driver may perform a more\r
444 exhausive verfication operation of the device\r
445 during reset.\r
446\r
447 @retval EFI_SUCCESS The device was reset.\r
448 @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r
449 not be reset.\r
450\r
451**/\r
452EFI_STATUS\r
453WinNtBlockIoReset (\r
454 IN EMU_BLOCK_IO_PROTOCOL *This,\r
455 IN BOOLEAN ExtendedVerification\r
456 )\r
457{\r
458 WIN_NT_BLOCK_IO_PRIVATE *Private;\r
459\r
460 Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);\r
461\r
462 if (Private->NtHandle != INVALID_HANDLE_VALUE) {\r
463 CloseHandle (Private->NtHandle);\r
464 Private->NtHandle = INVALID_HANDLE_VALUE;\r
465 }\r
466\r
467 return EFI_SUCCESS;\r
468}\r
469\r
470EMU_BLOCK_IO_PROTOCOL gEmuBlockIoProtocol = {\r
471 WinNtBlockIoReset,\r
472 WinNtBlockIoReadBlocks,\r
473 WinNtBlockIoWriteBlocks,\r
474 WinNtBlockIoFlushBlocks,\r
475 WinNtBlockIoCreateMapping\r
476};\r
477\r
478EFI_STATUS\r
479EFIAPI\r
480WinNtBlockIoThunkOpen (\r
481 IN EMU_IO_THUNK_PROTOCOL *This\r
482 )\r
483{\r
484 WIN_NT_BLOCK_IO_PRIVATE *Private;\r
485 CHAR16 *Str;\r
486\r
487 Private = AllocatePool (sizeof (*Private));\r
488 if (Private == NULL) {\r
489 return EFI_OUT_OF_RESOURCES;\r
490 }\r
491\r
492 Private->Signature = WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE;\r
493 Private->Thunk = This;\r
494 CopyMem (&Private->EmuBlockIo, &gEmuBlockIoProtocol, sizeof (gEmuBlockIoProtocol));\r
495 Private->BlockSize = 512;\r
496 Private->NtHandle = INVALID_HANDLE_VALUE;\r
497\r
498 Private->FileName = AllocateCopyPool (StrSize (This->ConfigString), This->ConfigString);\r
499 if (Private->FileName == NULL) {\r
500 return EFI_OUT_OF_RESOURCES;\r
501 }\r
502 //\r
503 // Parse ConfigString\r
504 // <ConfigString> := <FileName> ':' [RF][OW] ':' <BlockSize>\r
505 //\r
506 Str = StrStr (Private->FileName, L":");\r
507 if (Str == NULL) {\r
508 Private->Removable = FALSE;\r
509 Private->Readonly = FALSE;\r
510 } else {\r
511 for (*Str++ = L'\0'; *Str != L'\0'; Str++) {\r
512 if (*Str == 'R' || *Str == 'F') {\r
513 Private->Removable = (BOOLEAN) (*Str == L'R');\r
514 }\r
515 if (*Str == 'O' || *Str == 'W') {\r
516 Private->Readonly = (BOOLEAN) (*Str == L'O');\r
517 }\r
518 if (*Str == ':') {\r
519 Private->BlockSize = wcstol (++Str, NULL, 0);\r
520 break;\r
521 }\r
522 }\r
523 }\r
524\r
525 This->Interface = &Private->EmuBlockIo;\r
526 This->Private = Private;\r
527 return EFI_SUCCESS;\r
528}\r
529\r
530\r
531EFI_STATUS\r
532EFIAPI\r
533WinNtBlockIoThunkClose (\r
534 IN EMU_IO_THUNK_PROTOCOL *This\r
535 )\r
536{\r
537 WIN_NT_BLOCK_IO_PRIVATE *Private;\r
538\r
539 Private = This->Private;\r
540\r
541 if (Private != NULL) {\r
542 if (Private->FileName != NULL) {\r
543 FreePool (Private->FileName);\r
544 }\r
545 FreePool (Private);\r
546 }\r
547\r
548 return EFI_SUCCESS;\r
549}\r
550\r
551\r
552\r
553EMU_IO_THUNK_PROTOCOL mWinNtBlockIoThunkIo = {\r
554 &gEmuBlockIoProtocolGuid,\r
555 NULL,\r
556 NULL,\r
557 0,\r
558 WinNtBlockIoThunkOpen,\r
559 WinNtBlockIoThunkClose,\r
560 NULL\r
561};\r
562\r
563\r