]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/EmuSimpleFileSystemDxe/EmuSimpleFileSystem.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmulatorPkg / EmuSimpleFileSystemDxe / EmuSimpleFileSystem.c
1 /*++ @file
2 Produce Simple File System abstractions for directories on your PC using Posix APIs.
3 The configuration of what devices to mount or emulate comes from UNIX
4 environment variables. The variables must be visible to the Microsoft*
5 Developer Studio for them to work.
6
7 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
8 Portions copyright (c) 2011, Apple Inc. All rights reserved.
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10
11 **/
12
13 #include "EmuSimpleFileSystem.h"
14
15 /**
16 Opens a new file relative to the source file's location.
17
18 @param This The protocol instance pointer.
19 @param NewHandle Returns File Handle for FileName.
20 @param FileName Null terminated string. "\", ".", and ".." are supported.
21 @param OpenMode Open mode for file.
22 @param Attributes Only used for EFI_FILE_MODE_CREATE.
23
24 @retval EFI_SUCCESS The device was opened.
25 @retval EFI_NOT_FOUND The specified file could not be found on the device.
26 @retval EFI_NO_MEDIA The device has no media.
27 @retval EFI_MEDIA_CHANGED The media has changed.
28 @retval EFI_DEVICE_ERROR The device reported an error.
29 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
30 @retval EFI_ACCESS_DENIED The service denied access to the file.
31 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
32 @retval EFI_VOLUME_FULL The volume is full.
33
34 **/
35 EFI_STATUS
36 EFIAPI
37 EmuSimpleFileSystemOpen (
38 IN EFI_FILE_PROTOCOL *This,
39 OUT EFI_FILE_PROTOCOL **NewHandle,
40 IN CHAR16 *FileName,
41 IN UINT64 OpenMode,
42 IN UINT64 Attributes
43 )
44 {
45 EFI_STATUS Status;
46 EFI_TPL OldTpl;
47 EMU_EFI_FILE_PRIVATE *PrivateFile;
48 EMU_EFI_FILE_PRIVATE *NewPrivateFile;
49
50 //
51 // Check for obvious invalid parameters.
52 //
53 if ((This == NULL) || (NewHandle == NULL) || (FileName == NULL)) {
54 return EFI_INVALID_PARAMETER;
55 }
56
57 switch (OpenMode) {
58 case EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:
59 if (Attributes &~EFI_FILE_VALID_ATTR) {
60 return EFI_INVALID_PARAMETER;
61 }
62
63 if (Attributes & EFI_FILE_READ_ONLY) {
64 return EFI_INVALID_PARAMETER;
65 }
66
67 //
68 // fall through
69 //
70 case EFI_FILE_MODE_READ:
71 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:
72 break;
73
74 default:
75 return EFI_INVALID_PARAMETER;
76 }
77
78 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
79
80 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
81
82 NewPrivateFile = AllocateCopyPool (sizeof (EMU_EFI_FILE_PRIVATE), PrivateFile);
83 if (NewPrivateFile == NULL) {
84 Status = EFI_OUT_OF_RESOURCES;
85 goto Done;
86 }
87
88 Status = PrivateFile->Io->Open (PrivateFile->Io, &NewPrivateFile->Io, FileName, OpenMode, Attributes);
89 if (!EFI_ERROR (Status)) {
90 *NewHandle = &NewPrivateFile->EfiFile;
91 } else {
92 *NewHandle = NULL;
93 FreePool (NewPrivateFile);
94 }
95
96 Done:
97 gBS->RestoreTPL (OldTpl);
98
99 return Status;
100 }
101
102 /**
103 Close the file handle
104
105 @param This Protocol instance pointer.
106
107 @retval EFI_SUCCESS The file was closed.
108
109 **/
110 EFI_STATUS
111 EFIAPI
112 EmuSimpleFileSystemClose (
113 IN EFI_FILE_PROTOCOL *This
114 )
115 {
116 EFI_STATUS Status;
117 EMU_EFI_FILE_PRIVATE *PrivateFile;
118 EFI_TPL OldTpl;
119
120 if (This == NULL) {
121 return EFI_INVALID_PARAMETER;
122 }
123
124 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
125
126 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
127
128 Status = PrivateFile->Io->Close (PrivateFile->Io);
129 if (!EFI_ERROR (Status)) {
130 gBS->FreePool (PrivateFile);
131 }
132
133 gBS->RestoreTPL (OldTpl);
134
135 return Status;
136 }
137
138 /**
139 Close and delete the file handle.
140
141 @param This Protocol instance pointer.
142
143 @retval EFI_SUCCESS The file was closed and deleted.
144 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
145
146 **/
147 EFI_STATUS
148 EFIAPI
149 EmuSimpleFileSystemDelete (
150 IN EFI_FILE_PROTOCOL *This
151 )
152 {
153 EFI_STATUS Status;
154 EMU_EFI_FILE_PRIVATE *PrivateFile;
155 EFI_TPL OldTpl;
156
157 if (This == NULL) {
158 return EFI_INVALID_PARAMETER;
159 }
160
161 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
162
163 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
164
165 Status = PrivateFile->Io->Delete (PrivateFile->Io);
166 if (!EFI_ERROR (Status)) {
167 gBS->FreePool (PrivateFile);
168 }
169
170 gBS->RestoreTPL (OldTpl);
171
172 return Status;
173 }
174
175 /**
176 Read data from the file.
177
178 @param This Protocol instance pointer.
179 @param BufferSize On input size of buffer, on output amount of data in buffer.
180 @param Buffer The buffer in which data is read.
181
182 @retval EFI_SUCCESS Data was read.
183 @retval EFI_NO_MEDIA The device has no media.
184 @retval EFI_DEVICE_ERROR The device reported an error.
185 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
186 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.
187
188 **/
189 EFI_STATUS
190 EFIAPI
191 EmuSimpleFileSystemRead (
192 IN EFI_FILE_PROTOCOL *This,
193 IN OUT UINTN *BufferSize,
194 OUT VOID *Buffer
195 )
196 {
197 EFI_STATUS Status;
198 EMU_EFI_FILE_PRIVATE *PrivateFile;
199 EFI_TPL OldTpl;
200
201 if ((This == NULL) || (BufferSize == NULL)) {
202 return EFI_INVALID_PARAMETER;
203 }
204
205 if ((*BufferSize != 0) && (Buffer == NULL)) {
206 // Buffer can be NULL if *BufferSize is zero
207 return EFI_INVALID_PARAMETER;
208 }
209
210 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
211
212 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
213
214 Status = PrivateFile->Io->Read (PrivateFile->Io, BufferSize, Buffer);
215
216 gBS->RestoreTPL (OldTpl);
217 return Status;
218 }
219
220 /**
221 Write data to a file.
222
223 @param This Protocol instance pointer.
224 @param BufferSize On input size of buffer, on output amount of data in buffer.
225 @param Buffer The buffer in which data to write.
226
227 @retval EFI_SUCCESS Data was written.
228 @retval EFI_UNSUPPORTED Writes to Open directory are not supported.
229 @retval EFI_NO_MEDIA The device has no media.
230 @retval EFI_DEVICE_ERROR The device reported an error.
231 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.
232 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
233 @retval EFI_WRITE_PROTECTED The device is write protected.
234 @retval EFI_ACCESS_DENIED The file was open for read only.
235 @retval EFI_VOLUME_FULL The volume is full.
236
237 **/
238 EFI_STATUS
239 EFIAPI
240 EmuSimpleFileSystemWrite (
241 IN EFI_FILE_PROTOCOL *This,
242 IN OUT UINTN *BufferSize,
243 IN VOID *Buffer
244 )
245 {
246 EFI_STATUS Status;
247 EMU_EFI_FILE_PRIVATE *PrivateFile;
248 EFI_TPL OldTpl;
249
250 if ((This == NULL) || (BufferSize == NULL) || (Buffer == NULL)) {
251 return EFI_INVALID_PARAMETER;
252 }
253
254 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
255
256 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
257
258 Status = PrivateFile->Io->Write (PrivateFile->Io, BufferSize, Buffer);
259
260 gBS->RestoreTPL (OldTpl);
261 return Status;
262 }
263
264 /**
265 Get a file's current position
266
267 @param This Protocol instance pointer.
268 @param Position Byte position from the start of the file.
269
270 @retval EFI_SUCCESS Position was updated.
271 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.
272
273 **/
274 EFI_STATUS
275 EFIAPI
276 EmuSimpleFileSystemGetPosition (
277 IN EFI_FILE_PROTOCOL *This,
278 OUT UINT64 *Position
279 )
280 {
281 EFI_STATUS Status;
282 EMU_EFI_FILE_PRIVATE *PrivateFile;
283 EFI_TPL OldTpl;
284
285 if ((This == NULL) || (Position == NULL)) {
286 return EFI_INVALID_PARAMETER;
287 }
288
289 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
290
291 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
292
293 Status = PrivateFile->Io->GetPosition (PrivateFile->Io, Position);
294
295 gBS->RestoreTPL (OldTpl);
296 return Status;
297 }
298
299 /**
300 Set file's current position
301
302 @param This Protocol instance pointer.
303 @param Position Byte position from the start of the file.
304
305 @retval EFI_SUCCESS Position was updated.
306 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..
307
308 **/
309 EFI_STATUS
310 EFIAPI
311 EmuSimpleFileSystemSetPosition (
312 IN EFI_FILE_PROTOCOL *This,
313 IN UINT64 Position
314 )
315 {
316 EFI_STATUS Status;
317 EMU_EFI_FILE_PRIVATE *PrivateFile;
318 EFI_TPL OldTpl;
319
320 if (This == NULL) {
321 return EFI_INVALID_PARAMETER;
322 }
323
324 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
325
326 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
327
328 Status = PrivateFile->Io->SetPosition (PrivateFile->Io, Position);
329
330 gBS->RestoreTPL (OldTpl);
331 return Status;
332 }
333
334 /**
335 Get information about a file.
336
337 @param This Protocol instance pointer.
338 @param InformationType Type of information to return in Buffer.
339 @param BufferSize On input size of buffer, on output amount of data in buffer.
340 @param Buffer The buffer to return data.
341
342 @retval EFI_SUCCESS Data was returned.
343 @retval EFI_UNSUPPORTED InformationType is not supported.
344 @retval EFI_NO_MEDIA The device has no media.
345 @retval EFI_DEVICE_ERROR The device reported an error.
346 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
347 @retval EFI_WRITE_PROTECTED The device is write protected.
348 @retval EFI_ACCESS_DENIED The file was open for read only.
349 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.
350
351 **/
352 EFI_STATUS
353 EFIAPI
354 EmuSimpleFileSystemGetInfo (
355 IN EFI_FILE_PROTOCOL *This,
356 IN EFI_GUID *InformationType,
357 IN OUT UINTN *BufferSize,
358 OUT VOID *Buffer
359 )
360 {
361 EFI_STATUS Status;
362 EMU_EFI_FILE_PRIVATE *PrivateFile;
363 EFI_TPL OldTpl;
364
365 if ((This == NULL) || (InformationType == NULL) || (BufferSize == NULL)) {
366 return EFI_INVALID_PARAMETER;
367 }
368
369 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
370
371 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
372
373 Status = PrivateFile->Io->GetInfo (PrivateFile->Io, InformationType, BufferSize, Buffer);
374
375 gBS->RestoreTPL (OldTpl);
376 return Status;
377 }
378
379 /**
380 Set information about a file
381
382 @param File Protocol instance pointer.
383 @param InformationType Type of information in Buffer.
384 @param BufferSize Size of buffer.
385 @param Buffer The data to write.
386
387 @retval EFI_SUCCESS Data was set.
388 @retval EFI_UNSUPPORTED InformationType is not supported.
389 @retval EFI_NO_MEDIA The device has no media.
390 @retval EFI_DEVICE_ERROR The device reported an error.
391 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
392 @retval EFI_WRITE_PROTECTED The device is write protected.
393 @retval EFI_ACCESS_DENIED The file was open for read only.
394
395 **/
396 EFI_STATUS
397 EFIAPI
398 EmuSimpleFileSystemSetInfo (
399 IN EFI_FILE_PROTOCOL *This,
400 IN EFI_GUID *InformationType,
401 IN UINTN BufferSize,
402 IN VOID *Buffer
403 )
404 {
405 EFI_STATUS Status;
406 EMU_EFI_FILE_PRIVATE *PrivateFile;
407 EFI_TPL OldTpl;
408
409 //
410 // Check for invalid parameters.
411 //
412 if ((This == NULL) || (InformationType == NULL) || (BufferSize == 0) || (Buffer == NULL)) {
413 return EFI_INVALID_PARAMETER;
414 }
415
416 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
417
418 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
419
420 Status = PrivateFile->Io->SetInfo (PrivateFile->Io, InformationType, BufferSize, Buffer);
421
422 gBS->RestoreTPL (OldTpl);
423 return Status;
424 }
425
426 /**
427 Flush data back for the file handle.
428
429 @param This Protocol instance pointer.
430
431 @retval EFI_SUCCESS Data was flushed.
432 @retval EFI_UNSUPPORTED Writes to Open directory are not supported.
433 @retval EFI_NO_MEDIA The device has no media.
434 @retval EFI_DEVICE_ERROR The device reported an error.
435 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
436 @retval EFI_WRITE_PROTECTED The device is write protected.
437 @retval EFI_ACCESS_DENIED The file was open for read only.
438 @retval EFI_VOLUME_FULL The volume is full.
439
440 **/
441 EFI_STATUS
442 EFIAPI
443 EmuSimpleFileSystemFlush (
444 IN EFI_FILE_PROTOCOL *This
445 )
446 {
447 EFI_STATUS Status;
448 EMU_EFI_FILE_PRIVATE *PrivateFile;
449 EFI_TPL OldTpl;
450
451 if (This == NULL) {
452 return EFI_INVALID_PARAMETER;
453 }
454
455 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
456
457 PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
458
459 Status = PrivateFile->Io->Flush (PrivateFile->Io);
460
461 gBS->RestoreTPL (OldTpl);
462 return Status;
463 }
464
465 /**
466 Open the root directory on a volume.
467
468 @param This Protocol instance pointer.
469 @param Root Returns an Open file handle for the root directory
470
471 @retval EFI_SUCCESS The device was opened.
472 @retval EFI_UNSUPPORTED This volume does not support the file system.
473 @retval EFI_NO_MEDIA The device has no media.
474 @retval EFI_DEVICE_ERROR The device reported an error.
475 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
476 @retval EFI_ACCESS_DENIED The service denied access to the file.
477 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
478
479 **/
480 EFI_STATUS
481 EFIAPI
482 EmuSimpleFileSystemOpenVolume (
483 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
484 OUT EFI_FILE_PROTOCOL **Root
485 )
486 {
487 EFI_STATUS Status;
488 EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
489 EMU_EFI_FILE_PRIVATE *PrivateFile;
490 EFI_TPL OldTpl;
491
492 Status = EFI_UNSUPPORTED;
493
494 if ((This == NULL) || (Root == NULL)) {
495 return EFI_INVALID_PARAMETER;
496 }
497
498 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
499
500 Private = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This);
501
502 PrivateFile = AllocatePool (sizeof (EMU_EFI_FILE_PRIVATE));
503 if (PrivateFile == NULL) {
504 Status = EFI_OUT_OF_RESOURCES;
505 goto Done;
506 }
507
508 PrivateFile->Signature = EMU_EFI_FILE_PRIVATE_SIGNATURE;
509 PrivateFile->IoThunk = Private->IoThunk;
510 PrivateFile->SimpleFileSystem = This;
511
512 ZeroMem (&PrivateFile->EfiFile, sizeof (PrivateFile->EfiFile));
513 PrivateFile->EfiFile.Revision = EFI_FILE_PROTOCOL_REVISION;
514 PrivateFile->EfiFile.Open = EmuSimpleFileSystemOpen;
515 PrivateFile->EfiFile.Close = EmuSimpleFileSystemClose;
516 PrivateFile->EfiFile.Delete = EmuSimpleFileSystemDelete;
517 PrivateFile->EfiFile.Read = EmuSimpleFileSystemRead;
518 PrivateFile->EfiFile.Write = EmuSimpleFileSystemWrite;
519 PrivateFile->EfiFile.GetPosition = EmuSimpleFileSystemGetPosition;
520 PrivateFile->EfiFile.SetPosition = EmuSimpleFileSystemSetPosition;
521 PrivateFile->EfiFile.GetInfo = EmuSimpleFileSystemGetInfo;
522 PrivateFile->EfiFile.SetInfo = EmuSimpleFileSystemSetInfo;
523 PrivateFile->EfiFile.Flush = EmuSimpleFileSystemFlush;
524
525 *Root = &PrivateFile->EfiFile;
526
527 Status = Private->Io->OpenVolume (Private->Io, &PrivateFile->Io);
528 if (EFI_ERROR (Status)) {
529 goto Done;
530 }
531
532 AddUnicodeString2 (
533 "eng",
534 gEmuSimpleFileSystemComponentName.SupportedLanguages,
535 &Private->ControllerNameTable,
536 Private->IoThunk->ConfigString,
537 TRUE
538 );
539
540 AddUnicodeString2 (
541 "en",
542 gEmuSimpleFileSystemComponentName.SupportedLanguages,
543 &Private->ControllerNameTable,
544 Private->IoThunk->ConfigString,
545 FALSE
546 );
547
548 Done:
549 if (EFI_ERROR (Status)) {
550 if (PrivateFile) {
551 gBS->FreePool (PrivateFile);
552 }
553
554 *Root = NULL;
555 }
556
557 gBS->RestoreTPL (OldTpl);
558
559 return Status;
560 }
561
562 /**
563 Tests to see if this driver supports a given controller. If a child device is provided,
564 it further tests to see if this driver supports creating a handle for the specified child device.
565
566 This function checks to see if the driver specified by This supports the device specified by
567 ControllerHandle. Drivers will typically use the device path attached to
568 ControllerHandle and/or the services from the bus I/O abstraction attached to
569 ControllerHandle to determine if the driver supports ControllerHandle. This function
570 may be called many times during platform initialization. In order to reduce boot times, the tests
571 performed by this function must be very small, and take as little time as possible to execute. This
572 function must not change the state of any hardware devices, and this function must be aware that the
573 device specified by ControllerHandle may already be managed by the same driver or a
574 different driver. This function must match its calls to AllocatePages() with FreePages(),
575 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
576 Because ControllerHandle may have been previously started by the same driver, if a protocol is
577 already in the opened state, then it must not be closed with CloseProtocol(). This is required
578 to guarantee the state of ControllerHandle is not modified by this function.
579
580 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
581 @param[in] ControllerHandle The handle of the controller to test. This handle
582 must support a protocol interface that supplies
583 an I/O abstraction to the driver.
584 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
585 parameter is ignored by device drivers, and is optional for bus
586 drivers. For bus drivers, if this parameter is not NULL, then
587 the bus driver must determine if the bus controller specified
588 by ControllerHandle and the child controller specified
589 by RemainingDevicePath are both supported by this
590 bus driver.
591
592 @retval EFI_SUCCESS The device specified by ControllerHandle and
593 RemainingDevicePath is supported by the driver specified by This.
594 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
595 RemainingDevicePath is already being managed by the driver
596 specified by This.
597 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
598 RemainingDevicePath is already being managed by a different
599 driver or an application that requires exclusive access.
600 Currently not implemented.
601 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
602 RemainingDevicePath is not supported by the driver specified by This.
603 **/
604 EFI_STATUS
605 EFIAPI
606 EmuSimpleFileSystemDriverBindingSupported (
607 IN EFI_DRIVER_BINDING_PROTOCOL *This,
608 IN EFI_HANDLE ControllerHandle,
609 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
610 )
611 {
612 EFI_STATUS Status;
613 EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
614
615 //
616 // Open the IO Abstraction(s) needed to perform the supported test
617 //
618 Status = gBS->OpenProtocol (
619 ControllerHandle,
620 &gEmuIoThunkProtocolGuid,
621 (VOID **)&EmuIoThunk,
622 This->DriverBindingHandle,
623 ControllerHandle,
624 EFI_OPEN_PROTOCOL_BY_DRIVER
625 );
626 if (EFI_ERROR (Status)) {
627 return Status;
628 }
629
630 //
631 // Make sure GUID is for a File System handle.
632 //
633 Status = EFI_UNSUPPORTED;
634 if (CompareGuid (EmuIoThunk->Protocol, &gEfiSimpleFileSystemProtocolGuid)) {
635 Status = EFI_SUCCESS;
636 }
637
638 //
639 // Close the I/O Abstraction(s) used to perform the supported test
640 //
641 gBS->CloseProtocol (
642 ControllerHandle,
643 &gEmuIoThunkProtocolGuid,
644 This->DriverBindingHandle,
645 ControllerHandle
646 );
647
648 return Status;
649 }
650
651 /**
652 Starts a device controller or a bus controller.
653
654 The Start() function is designed to be invoked from the EFI boot service ConnectController().
655 As a result, much of the error checking on the parameters to Start() has been moved into this
656 common boot service. It is legal to call Start() from other locations,
657 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
658 1. ControllerHandle must be a valid EFI_HANDLE.
659 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
660 EFI_DEVICE_PATH_PROTOCOL.
661 3. Prior to calling Start(), the Supported() function for the driver specified by This must
662 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
663
664 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
665 @param[in] ControllerHandle The handle of the controller to start. This handle
666 must support a protocol interface that supplies
667 an I/O abstraction to the driver.
668 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
669 parameter is ignored by device drivers, and is optional for bus
670 drivers. For a bus driver, if this parameter is NULL, then handles
671 for all the children of Controller are created by this driver.
672 If this parameter is not NULL and the first Device Path Node is
673 not the End of Device Path Node, then only the handle for the
674 child device specified by the first Device Path Node of
675 RemainingDevicePath is created by this driver.
676 If the first Device Path Node of RemainingDevicePath is
677 the End of Device Path Node, no child handle is created by this
678 driver.
679
680 @retval EFI_SUCCESS The device was started.
681 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
682 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
683 @retval Others The driver failded to start the device.
684
685 **/
686 EFI_STATUS
687 EFIAPI
688 EmuSimpleFileSystemDriverBindingStart (
689 IN EFI_DRIVER_BINDING_PROTOCOL *This,
690 IN EFI_HANDLE ControllerHandle,
691 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
692 )
693 {
694 EFI_STATUS Status;
695 EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
696 EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
697
698 Private = NULL;
699
700 //
701 // Open the IO Abstraction(s) needed
702 //
703 Status = gBS->OpenProtocol (
704 ControllerHandle,
705 &gEmuIoThunkProtocolGuid,
706 (VOID **)&EmuIoThunk,
707 This->DriverBindingHandle,
708 ControllerHandle,
709 EFI_OPEN_PROTOCOL_BY_DRIVER
710 );
711 if (EFI_ERROR (Status)) {
712 return Status;
713 }
714
715 //
716 // Validate GUID
717 //
718 if (!CompareGuid (EmuIoThunk->Protocol, &gEfiSimpleFileSystemProtocolGuid)) {
719 Status = EFI_UNSUPPORTED;
720 goto Done;
721 }
722
723 Private = AllocateZeroPool (sizeof (EMU_SIMPLE_FILE_SYSTEM_PRIVATE));
724 if (Private == NULL) {
725 Status = EFI_OUT_OF_RESOURCES;
726 goto Done;
727 }
728
729 Status = EmuIoThunk->Open (EmuIoThunk);
730 if (EFI_ERROR (Status)) {
731 goto Done;
732 }
733
734 Private->Signature = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE;
735 Private->IoThunk = EmuIoThunk;
736 Private->Io = EmuIoThunk->Interface;
737
738 Private->SimpleFileSystem.Revision = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
739 Private->SimpleFileSystem.OpenVolume = EmuSimpleFileSystemOpenVolume;
740
741 Private->ControllerNameTable = NULL;
742
743 AddUnicodeString2 (
744 "eng",
745 gEmuSimpleFileSystemComponentName.SupportedLanguages,
746 &Private->ControllerNameTable,
747 EmuIoThunk->ConfigString,
748 TRUE
749 );
750
751 AddUnicodeString2 (
752 "en",
753 gEmuSimpleFileSystemComponentName2.SupportedLanguages,
754 &Private->ControllerNameTable,
755 EmuIoThunk->ConfigString,
756 FALSE
757 );
758
759 Status = gBS->InstallMultipleProtocolInterfaces (
760 &ControllerHandle,
761 &gEfiSimpleFileSystemProtocolGuid,
762 &Private->SimpleFileSystem,
763 NULL
764 );
765
766 Done:
767 if (EFI_ERROR (Status)) {
768 if (Private != NULL) {
769 if (Private->ControllerNameTable != NULL) {
770 FreeUnicodeStringTable (Private->ControllerNameTable);
771 }
772
773 gBS->FreePool (Private);
774 }
775
776 gBS->CloseProtocol (
777 ControllerHandle,
778 &gEmuIoThunkProtocolGuid,
779 This->DriverBindingHandle,
780 ControllerHandle
781 );
782 }
783
784 return Status;
785 }
786
787 /**
788 Stops a device controller or a bus controller.
789
790 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
791 As a result, much of the error checking on the parameters to Stop() has been moved
792 into this common boot service. It is legal to call Stop() from other locations,
793 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
794 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
795 same driver's Start() function.
796 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
797 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
798 Start() function, and the Start() function must have called OpenProtocol() on
799 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
800
801 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
802 @param[in] ControllerHandle A handle to the device being stopped. The handle must
803 support a bus specific I/O protocol for the driver
804 to use to stop the device.
805 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
806 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
807 if NumberOfChildren is 0.
808
809 @retval EFI_SUCCESS The device was stopped.
810 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
811
812 **/
813 EFI_STATUS
814 EFIAPI
815 EmuSimpleFileSystemDriverBindingStop (
816 IN EFI_DRIVER_BINDING_PROTOCOL *This,
817 IN EFI_HANDLE ControllerHandle,
818 IN UINTN NumberOfChildren,
819 IN EFI_HANDLE *ChildHandleBuffer
820 )
821 {
822 EFI_STATUS Status;
823 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
824 EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
825
826 //
827 // Get our context back
828 //
829 Status = gBS->OpenProtocol (
830 ControllerHandle,
831 &gEfiSimpleFileSystemProtocolGuid,
832 (VOID **)&SimpleFileSystem,
833 This->DriverBindingHandle,
834 ControllerHandle,
835 EFI_OPEN_PROTOCOL_GET_PROTOCOL
836 );
837 if (EFI_ERROR (Status)) {
838 return EFI_UNSUPPORTED;
839 }
840
841 Private = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (SimpleFileSystem);
842
843 //
844 // Uninstall the Simple File System Protocol from ControllerHandle
845 //
846 Status = gBS->UninstallMultipleProtocolInterfaces (
847 ControllerHandle,
848 &gEfiSimpleFileSystemProtocolGuid,
849 &Private->SimpleFileSystem,
850 NULL
851 );
852 if (!EFI_ERROR (Status)) {
853 Status = gBS->CloseProtocol (
854 ControllerHandle,
855 &gEmuIoThunkProtocolGuid,
856 This->DriverBindingHandle,
857 ControllerHandle
858 );
859 ASSERT_EFI_ERROR (Status);
860 //
861 // Destroy the IO interface.
862 //
863 Status = Private->IoThunk->Close (Private->IoThunk);
864 ASSERT_EFI_ERROR (Status);
865 //
866 // Free our instance data
867 //
868 FreeUnicodeStringTable (Private->ControllerNameTable);
869 gBS->FreePool (Private);
870 }
871
872 return Status;
873 }
874
875 EFI_DRIVER_BINDING_PROTOCOL gEmuSimpleFileSystemDriverBinding = {
876 EmuSimpleFileSystemDriverBindingSupported,
877 EmuSimpleFileSystemDriverBindingStart,
878 EmuSimpleFileSystemDriverBindingStop,
879 0xa,
880 NULL,
881 NULL
882 };
883
884 /**
885 The user Entry Point for module EmuSimpleFileSystem. The user code starts with this function.
886
887 @param[in] ImageHandle The firmware allocated handle for the EFI image.
888 @param[in] SystemTable A pointer to the EFI System Table.
889
890 @retval EFI_SUCCESS The entry point is executed successfully.
891 @retval other Some error occurs when executing this entry point.
892
893 **/
894 EFI_STATUS
895 EFIAPI
896 InitializeEmuSimpleFileSystem (
897 IN EFI_HANDLE ImageHandle,
898 IN EFI_SYSTEM_TABLE *SystemTable
899 )
900 {
901 EFI_STATUS Status;
902
903 Status = EfiLibInstallDriverBindingComponentName2 (
904 ImageHandle,
905 SystemTable,
906 &gEmuSimpleFileSystemDriverBinding,
907 ImageHandle,
908 &gEmuSimpleFileSystemComponentName,
909 &gEmuSimpleFileSystemComponentName2
910 );
911 ASSERT_EFI_ERROR (Status);
912
913 return Status;
914 }