1 /* $Id: fsw_efi.c 29125 2010-05-06 09:43:05Z vboxsync $ */
3 * fsw_efi.c - EFI host environment code.
7 * Copyright (C) 2010 Oracle Corporation
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 * This code is based on:
21 * Copyright (c) 2006 Christoph Pfisterer
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are
27 * * Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
30 * * Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the
35 * * Neither the name of Christoph Pfisterer nor the names of the
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
43 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #error FSTYPE must be defined!
66 // MESSAGE_LOG_PROTOCOL *Msg = NULL;
69 #define DBG(x...) AsciiPrint(x)
71 #define DBG(x...) BootLog(x)
77 /** Helper macro for stringification. */
78 #define FSW_EFI_STRINGIFY(x) #x
79 /** Expands to the EFI driver name given the file system type name. */
80 #define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.4.0 " FSW_EFI_STRINGIFY(t) L" File System Driver"
82 // function prototypes
84 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
85 IN EFI_HANDLE ControllerHandle
,
86 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
);
87 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
88 IN EFI_HANDLE ControllerHandle
,
89 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
);
90 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
91 IN EFI_HANDLE ControllerHandle
,
92 IN UINTN NumberOfChildren
,
93 IN EFI_HANDLE
*ChildHandleBuffer
);
95 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
97 OUT CHAR16
**DriverName
);
98 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
99 IN EFI_HANDLE ControllerHandle
,
100 IN EFI_HANDLE ChildHandle OPTIONAL
,
102 OUT CHAR16
**ControllerName
);
104 void fsw_efi_change_blocksize(struct fsw_volume
*vol
,
105 fsw_u32 old_phys_blocksize
, fsw_u32 old_log_blocksize
,
106 fsw_u32 new_phys_blocksize
, fsw_u32 new_log_blocksize
);
107 fsw_status_t
fsw_efi_read_block(struct fsw_volume
*vol
, fsw_u32 phys_bno
, void *buffer
);
109 EFI_STATUS
fsw_efi_map_status(fsw_status_t fsw_status
, FSW_VOLUME_DATA
*Volume
);
111 EFI_STATUS EFIAPI
fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE
*This
,
112 OUT EFI_FILE
**Root
);
113 EFI_STATUS
fsw_efi_dnode_to_FileHandle(IN
struct fsw_dnode
*dno
,
114 OUT EFI_FILE
**NewFileHandle
);
116 EFI_STATUS
fsw_efi_file_read(IN FSW_FILE_DATA
*File
,
117 IN OUT UINTN
*BufferSize
,
119 EFI_STATUS
fsw_efi_file_getpos(IN FSW_FILE_DATA
*File
,
120 OUT UINT64
*Position
);
121 EFI_STATUS
fsw_efi_file_setpos(IN FSW_FILE_DATA
*File
,
124 EFI_STATUS
fsw_efi_dir_open(IN FSW_FILE_DATA
*File
,
125 OUT EFI_FILE
**NewHandle
,
128 IN UINT64 Attributes
);
129 EFI_STATUS
fsw_efi_dir_read(IN FSW_FILE_DATA
*File
,
130 IN OUT UINTN
*BufferSize
,
132 EFI_STATUS
fsw_efi_dir_setpos(IN FSW_FILE_DATA
*File
,
135 EFI_STATUS
fsw_efi_dnode_getinfo(IN FSW_FILE_DATA
*File
,
136 IN EFI_GUID
*InformationType
,
137 IN OUT UINTN
*BufferSize
,
139 EFI_STATUS
fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA
*Volume
,
140 IN
struct fsw_dnode
*dno
,
141 IN OUT UINTN
*BufferSize
,
145 * Interface structure for the EFI Driver Binding protocol.
148 EFI_DRIVER_BINDING_PROTOCOL fsw_efi_DriverBinding_table
= {
149 fsw_efi_DriverBinding_Supported
,
150 fsw_efi_DriverBinding_Start
,
151 fsw_efi_DriverBinding_Stop
,
158 * Interface structure for the EFI Component Name protocol.
161 EFI_COMPONENT_NAME_PROTOCOL fsw_efi_ComponentName_table
= {
162 fsw_efi_ComponentName_GetDriverName
,
163 fsw_efi_ComponentName_GetControllerName
,
168 * Dispatch table for our FSW host driver.
171 struct fsw_host_table fsw_efi_host_table
= {
172 FSW_STRING_TYPE_UTF16
,
174 fsw_efi_change_blocksize
,
178 extern struct fsw_fstype_table
FSW_FSTYPE_TABLE_NAME(FSTYPE
);
180 //#include "OverrideFunctions-kabyl.edk2.c.include"
183 * Image entry point. Installs the Driver Binding and Component Name protocols
184 * on the image's handle. Actually mounting a file system is initiated through
185 * the Driver Binding protocol at the firmware's request.
187 EFI_STATUS EFIAPI
fsw_efi_main(IN EFI_HANDLE ImageHandle
,
188 IN EFI_SYSTEM_TABLE
*SystemTable
)
193 InitializeLib(ImageHandle
, SystemTable
);
196 // complete Driver Binding protocol instance
197 fsw_efi_DriverBinding_table
.ImageHandle
= ImageHandle
;
198 fsw_efi_DriverBinding_table
.DriverBindingHandle
= ImageHandle
;
199 // install Driver Binding protocol
200 Status
= BS
->InstallProtocolInterface(&fsw_efi_DriverBinding_table
.DriverBindingHandle
,
201 &PROTO_NAME(DriverBindingProtocol
),
202 EFI_NATIVE_INTERFACE
,
203 &fsw_efi_DriverBinding_table
);
204 if (EFI_ERROR (Status
)) {
208 // install Component Name protocol
209 Status
= BS
->InstallProtocolInterface(&fsw_efi_DriverBinding_table
.DriverBindingHandle
,
210 &PROTO_NAME(ComponentNameProtocol
),
211 EFI_NATIVE_INTERFACE
,
212 &fsw_efi_ComponentName_table
);
213 if (EFI_ERROR (Status
)) {
217 // OverrideFunctions();
220 // Status = BS->LocateProtocol(&gMsgLogProtocolGuid, NULL, (VOID **) &Msg);
221 // if (!EFI_ERROR(Status) && (Msg != NULL)) {
222 // msgCursor = Msg->Cursor;
223 // BootLog("MsgLog installed into VBoxFs\n");
230 * Driver Binding EFI protocol, Supported function. This function is called by EFI
231 * to test if this driver can handle a certain device. Our implementation only checks
232 * if the device is a disk (i.e. that it supports the Block I/O and Disk I/O protocols)
233 * and implicitly checks if the disk is already in use by another driver.
236 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
237 IN EFI_HANDLE ControllerHandle
,
238 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
)
243 // we check for both DiskIO and BlockIO protocols
245 // first, open DiskIO
246 Status
= BS
->OpenProtocol(ControllerHandle
,
247 &PROTO_NAME(DiskIoProtocol
),
249 This
->DriverBindingHandle
,
251 EFI_OPEN_PROTOCOL_BY_DRIVER
);
252 if (EFI_ERROR(Status
))
255 // we were just checking, close it again
256 BS
->CloseProtocol(ControllerHandle
,
257 &PROTO_NAME(DiskIoProtocol
),
258 This
->DriverBindingHandle
,
261 // next, check BlockIO without actually opening it
262 Status
= BS
->OpenProtocol(ControllerHandle
,
263 &PROTO_NAME(BlockIoProtocol
),
265 This
->DriverBindingHandle
,
267 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
);
272 * Driver Binding EFI protocol, Start function. This function is called by EFI
273 * to start driving the given device. It is still possible at this point to
274 * return EFI_UNSUPPORTED, and in fact we will do so if the file system driver
275 * cannot find the superblock signature (or equivalent) that it expects.
277 * This function allocates memory for a per-volume structure, opens the
278 * required protocols (just Disk I/O in our case, Block I/O is only looked
279 * at to get the MediaId field), and lets the FSW core mount the file system.
280 * If successful, an EFI Simple File System protocol is exported on the
284 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
285 IN EFI_HANDLE ControllerHandle
,
286 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
)
289 EFI_BLOCK_IO
*BlockIo
;
291 FSW_VOLUME_DATA
*Volume
;
294 Print(L
"fsw_efi_DriverBinding_Start\n");
297 // open consumed protocols
298 Status
= BS
->OpenProtocol(ControllerHandle
,
299 &PROTO_NAME(BlockIoProtocol
),
301 This
->DriverBindingHandle
,
303 EFI_OPEN_PROTOCOL_GET_PROTOCOL
); // NOTE: we only want to look at the MediaId
304 if (EFI_ERROR(Status
)) {
305 // Print(L"Fsw ERROR: OpenProtocol(BlockIo) returned %x\n", Status);
309 Status
= BS
->OpenProtocol(ControllerHandle
,
310 &PROTO_NAME(DiskIoProtocol
),
312 This
->DriverBindingHandle
,
314 EFI_OPEN_PROTOCOL_BY_DRIVER
);
315 if (EFI_ERROR(Status
)) {
316 Print(L
"Fsw ERROR: OpenProtocol(DiskIo) returned %r\n", Status
);
320 // allocate volume structure
321 Volume
= AllocateZeroPool(sizeof(FSW_VOLUME_DATA
));
322 Volume
->Signature
= FSW_VOLUME_DATA_SIGNATURE
;
323 Volume
->Handle
= ControllerHandle
;
324 Volume
->DiskIo
= DiskIo
;
325 Volume
->MediaId
= BlockIo
->Media
->MediaId
;
326 Volume
->LastIOStatus
= EFI_SUCCESS
;
328 // mount the filesystem
329 Status
= fsw_efi_map_status(fsw_mount(Volume
, &fsw_efi_host_table
,
330 &FSW_FSTYPE_TABLE_NAME(FSTYPE
), &Volume
->vol
),
333 if (!EFI_ERROR(Status
)) {
334 // register the SimpleFileSystem protocol
335 Volume
->FileSystem
.Revision
= EFI_FILE_IO_INTERFACE_REVISION
;
336 Volume
->FileSystem
.OpenVolume
= fsw_efi_FileSystem_OpenVolume
;
337 Status
= BS
->InstallMultipleProtocolInterfaces(&ControllerHandle
,
338 &PROTO_NAME(SimpleFileSystemProtocol
),
341 if (EFI_ERROR(Status
)) {
342 // Print(L"Fsw ERROR: InstallMultipleProtocolInterfaces returned %x\n", Status);
346 // on errors, close the opened protocols
347 if (EFI_ERROR(Status
)) {
348 if (Volume
->vol
!= NULL
)
349 fsw_unmount(Volume
->vol
);
352 BS
->CloseProtocol(ControllerHandle
,
353 &PROTO_NAME(DiskIoProtocol
),
354 This
->DriverBindingHandle
,
362 * Driver Binding EFI protocol, Stop function. This function is called by EFI
363 * to stop the driver on the given device. This translates to an unmount
364 * call for the FSW core.
366 * We assume that all file handles on the volume have been closed before
367 * the driver is stopped. At least with the EFI shell, that is actually the
368 * case; it closes all file handles between commands.
371 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
372 IN EFI_HANDLE ControllerHandle
,
373 IN UINTN NumberOfChildren
,
374 IN EFI_HANDLE
*ChildHandleBuffer
)
377 EFI_FILE_IO_INTERFACE
*FileSystem
;
378 FSW_VOLUME_DATA
*Volume
;
381 Print(L
"fsw_efi_DriverBinding_Stop\n");
384 // get the installed SimpleFileSystem interface
385 Status
= BS
->OpenProtocol(ControllerHandle
,
386 &PROTO_NAME(SimpleFileSystemProtocol
),
387 (VOID
**) &FileSystem
,
388 This
->DriverBindingHandle
,
390 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
391 if (EFI_ERROR(Status
))
392 return EFI_UNSUPPORTED
;
394 // get private data structure
395 Volume
= FSW_VOLUME_FROM_FILE_SYSTEM(FileSystem
);
397 // uninstall Simple File System protocol
398 Status
= BS
->UninstallMultipleProtocolInterfaces(ControllerHandle
,
399 &PROTO_NAME(SimpleFileSystemProtocol
), &Volume
->FileSystem
,
401 if (EFI_ERROR(Status
)) {
402 // Print(L"Fsw ERROR: UninstallMultipleProtocolInterfaces returned %x\n", Status);
406 Print(L
"fsw_efi_DriverBinding_Stop: protocol uninstalled successfully\n");
409 // release private data structure
410 if (Volume
->vol
!= NULL
)
411 fsw_unmount(Volume
->vol
);
414 // close the consumed protocols
415 Status
= BS
->CloseProtocol(ControllerHandle
,
416 &PROTO_NAME(DiskIoProtocol
),
417 This
->DriverBindingHandle
,
424 * Component Name EFI protocol, GetDriverName function. Used by the EFI
425 * environment to inquire the name of this driver. The name returned is
426 * based on the file system type actually used in compilation.
429 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
431 OUT CHAR16
**DriverName
)
433 if (Language
== NULL
|| DriverName
== NULL
)
434 return EFI_INVALID_PARAMETER
;
436 if (Language
[0] == 'e' && Language
[1] == 'n' && Language
[2] == 'g' && Language
[3] == 0) {
437 *DriverName
= FSW_EFI_DRIVER_NAME(FSTYPE
);
440 return EFI_UNSUPPORTED
;
444 * Component Name EFI protocol, GetControllerName function. Not implemented
445 * because this is not a "bus" driver in the sense of the EFI Driver Model.
448 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
449 IN EFI_HANDLE ControllerHandle
,
450 IN EFI_HANDLE ChildHandle OPTIONAL
,
452 OUT CHAR16
**ControllerName
)
454 return EFI_UNSUPPORTED
;
458 * FSW interface function for block size changes. This function is called by the FSW core
459 * when the file system driver changes the block sizes for the volume.
462 void fsw_efi_change_blocksize(struct fsw_volume
*vol
,
463 fsw_u32 old_phys_blocksize
, fsw_u32 old_log_blocksize
,
464 fsw_u32 new_phys_blocksize
, fsw_u32 new_log_blocksize
)
470 * FSW interface function to read data blocks. This function is called by the FSW core
471 * to read a block of data from the device. The buffer is allocated by the core code.
474 fsw_status_t
fsw_efi_read_block(struct fsw_volume
*vol
, fsw_u32 phys_bno
, void *buffer
)
477 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)vol
->host_data
;
479 // FSW_MSG_DEBUGV((FSW_MSGSTR("fsw_efi_read_block: %d (%d)\n"), phys_bno, vol->phys_blocksize));
482 Status
= Volume
->DiskIo
->ReadDisk(Volume
->DiskIo
, Volume
->MediaId
,
483 (UINT64
)phys_bno
* vol
->phys_blocksize
,
486 Volume
->LastIOStatus
= Status
;
487 if (EFI_ERROR(Status
))
493 * Map FSW status codes to EFI status codes. The FSW_IO_ERROR code is only produced
494 * by fsw_efi_read_block, so we map it back to the EFI status code remembered from
495 * the last I/O operation.
498 EFI_STATUS
fsw_efi_map_status(fsw_status_t fsw_status
, FSW_VOLUME_DATA
*Volume
)
500 switch (fsw_status
) {
503 case FSW_OUT_OF_MEMORY
:
504 return EFI_VOLUME_CORRUPTED
;
506 return Volume
->LastIOStatus
;
507 case FSW_UNSUPPORTED
:
508 return EFI_UNSUPPORTED
;
510 return EFI_NOT_FOUND
;
511 case FSW_VOLUME_CORRUPTED
:
512 return EFI_VOLUME_CORRUPTED
;
514 return EFI_DEVICE_ERROR
;
519 * File System EFI protocol, OpenVolume function. Creates a file handle for
520 * the root directory and returns it. Note that this function may be called
521 * multiple times and returns a new file handle each time. Each returned
522 * handle is closed by the client using it.
525 EFI_STATUS EFIAPI
fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE
*This
,
529 FSW_VOLUME_DATA
*Volume
= FSW_VOLUME_FROM_FILE_SYSTEM(This
);
532 Print(L
"fsw_efi_FileSystem_OpenVolume\n");
535 Status
= fsw_efi_dnode_to_FileHandle(Volume
->vol
->root
, Root
);
541 * File Handle EFI protocol, Open function. Dispatches the call
542 * based on the kind of file handle.
545 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Open(IN EFI_FILE
*This
,
546 OUT EFI_FILE
**NewHandle
,
549 IN UINT64 Attributes
)
551 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
553 if (File
->Type
== FSW_EFI_FILE_TYPE_DIR
)
554 return fsw_efi_dir_open(File
, NewHandle
, FileName
, OpenMode
, Attributes
);
555 // not supported for regular files
556 return EFI_UNSUPPORTED
;
560 * File Handle EFI protocol, Close function. Closes the FSW shandle
561 * and frees the memory used for the structure.
564 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Close(IN EFI_FILE
*This
)
566 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
569 Print(L
"fsw_efi_FileHandle_Close\n");
572 fsw_shandle_close(&File
->shand
);
579 * File Handle EFI protocol, Delete function. Calls through to Close
580 * and returns a warning because this driver is read-only.
583 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Delete(IN EFI_FILE
*This
)
587 Status
= This
->Close(This
);
588 if (Status
== EFI_SUCCESS
) {
589 // this driver is read-only
590 Status
= EFI_WARN_DELETE_FAILURE
;
597 * File Handle EFI protocol, Read function. Dispatches the call
598 * based on the kind of file handle.
601 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Read(IN EFI_FILE
*This
,
602 IN OUT UINTN
*BufferSize
,
605 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
607 if (File
->Type
== FSW_EFI_FILE_TYPE_FILE
)
608 return fsw_efi_file_read(File
, BufferSize
, Buffer
);
609 else if (File
->Type
== FSW_EFI_FILE_TYPE_DIR
)
610 return fsw_efi_dir_read(File
, BufferSize
, Buffer
);
611 return EFI_UNSUPPORTED
;
615 * File Handle EFI protocol, Write function. Returns unsupported status
616 * because this driver is read-only.
619 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Write(IN EFI_FILE
*This
,
620 IN OUT UINTN
*BufferSize
,
623 // this driver is read-only
624 return EFI_WRITE_PROTECTED
;
628 * File Handle EFI protocol, GetPosition function. Dispatches the call
629 * based on the kind of file handle.
632 EFI_STATUS EFIAPI
fsw_efi_FileHandle_GetPosition(IN EFI_FILE
*This
,
633 OUT UINT64
*Position
)
635 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
637 if (File
->Type
== FSW_EFI_FILE_TYPE_FILE
)
638 return fsw_efi_file_getpos(File
, Position
);
639 // not defined for directories
640 return EFI_UNSUPPORTED
;
644 * File Handle EFI protocol, SetPosition function. Dispatches the call
645 * based on the kind of file handle.
648 EFI_STATUS EFIAPI
fsw_efi_FileHandle_SetPosition(IN EFI_FILE
*This
,
651 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
653 if (File
->Type
== FSW_EFI_FILE_TYPE_FILE
)
654 return fsw_efi_file_setpos(File
, Position
);
655 else if (File
->Type
== FSW_EFI_FILE_TYPE_DIR
)
656 return fsw_efi_dir_setpos(File
, Position
);
657 return EFI_UNSUPPORTED
;
661 * File Handle EFI protocol, GetInfo function. Dispatches to the common
662 * function implementing this.
665 EFI_STATUS EFIAPI
fsw_efi_FileHandle_GetInfo(IN EFI_FILE
*This
,
666 IN EFI_GUID
*InformationType
,
667 IN OUT UINTN
*BufferSize
,
670 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
672 return fsw_efi_dnode_getinfo(File
, InformationType
, BufferSize
, Buffer
);
676 * File Handle EFI protocol, SetInfo function. Returns unsupported status
677 * because this driver is read-only.
680 EFI_STATUS EFIAPI
fsw_efi_FileHandle_SetInfo(IN EFI_FILE
*This
,
681 IN EFI_GUID
*InformationType
,
685 // this driver is read-only
686 return EFI_WRITE_PROTECTED
;
690 * File Handle EFI protocol, Flush function. Returns unsupported status
691 * because this driver is read-only.
694 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Flush(IN EFI_FILE
*This
)
696 // this driver is read-only
697 return EFI_WRITE_PROTECTED
;
701 * Set up a file handle for a dnode. This function allocates a data structure
702 * for a file handle, opens a FSW shandle and populates the EFI_FILE structure
703 * with the interface functions.
706 EFI_STATUS
fsw_efi_dnode_to_FileHandle(IN
struct fsw_dnode
*dno
,
707 OUT EFI_FILE
**NewFileHandle
)
712 // make sure the dnode has complete info
713 Status
= fsw_efi_map_status(fsw_dnode_fill(dno
), (FSW_VOLUME_DATA
*)dno
->vol
->host_data
);
714 if (EFI_ERROR(Status
))
718 if (dno
->type
!= FSW_DNODE_TYPE_FILE
&& dno
->type
!= FSW_DNODE_TYPE_DIR
)
719 return EFI_UNSUPPORTED
;
721 // allocate file structure
722 File
= AllocateZeroPool(sizeof(FSW_FILE_DATA
));
723 File
->Signature
= FSW_FILE_DATA_SIGNATURE
;
724 if (dno
->type
== FSW_DNODE_TYPE_FILE
)
725 File
->Type
= FSW_EFI_FILE_TYPE_FILE
;
726 else if (dno
->type
== FSW_DNODE_TYPE_DIR
)
727 File
->Type
= FSW_EFI_FILE_TYPE_DIR
;
730 Status
= fsw_efi_map_status(fsw_shandle_open(dno
, &File
->shand
),
731 (FSW_VOLUME_DATA
*)dno
->vol
->host_data
);
732 if (EFI_ERROR(Status
)) {
737 // populate the file handle
738 File
->FileHandle
.Revision
= EFI_FILE_HANDLE_REVISION
;
739 File
->FileHandle
.Open
= fsw_efi_FileHandle_Open
;
740 File
->FileHandle
.Close
= fsw_efi_FileHandle_Close
;
741 File
->FileHandle
.Delete
= fsw_efi_FileHandle_Delete
;
742 File
->FileHandle
.Read
= fsw_efi_FileHandle_Read
;
743 File
->FileHandle
.Write
= fsw_efi_FileHandle_Write
;
744 File
->FileHandle
.GetPosition
= fsw_efi_FileHandle_GetPosition
;
745 File
->FileHandle
.SetPosition
= fsw_efi_FileHandle_SetPosition
;
746 File
->FileHandle
.GetInfo
= fsw_efi_FileHandle_GetInfo
;
747 File
->FileHandle
.SetInfo
= fsw_efi_FileHandle_SetInfo
;
748 File
->FileHandle
.Flush
= fsw_efi_FileHandle_Flush
;
750 *NewFileHandle
= &File
->FileHandle
;
755 * Data read function for regular files. Calls through to fsw_shandle_read.
758 EFI_STATUS
fsw_efi_file_read(IN FSW_FILE_DATA
*File
,
759 IN OUT UINTN
*BufferSize
,
766 Print(L
"fsw_efi_file_read %d bytes\n", *BufferSize
);
769 buffer_size
= (fsw_u32
)*BufferSize
;
770 Status
= fsw_efi_map_status(fsw_shandle_read(&File
->shand
, &buffer_size
, Buffer
),
771 (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
);
772 *BufferSize
= buffer_size
;
778 * Get file position for regular files.
781 EFI_STATUS
fsw_efi_file_getpos(IN FSW_FILE_DATA
*File
,
782 OUT UINT64
*Position
)
784 *Position
= File
->shand
.pos
;
789 * Set file position for regular files. EFI specifies the all-ones value
790 * to be a special value for the end of the file.
793 EFI_STATUS
fsw_efi_file_setpos(IN FSW_FILE_DATA
*File
,
796 if (Position
== 0xFFFFFFFFFFFFFFFFULL
)
797 File
->shand
.pos
= File
->shand
.dnode
->size
;
799 File
->shand
.pos
= Position
;
804 * Open function used to open new file handles relative to a directory.
805 * In EFI, the "open file" function is implemented by directory file handles
806 * and is passed a relative or volume-absolute path to the file or directory
807 * to open. We use fsw_dnode_lookup_path to find the node plus an additional
808 * call to fsw_dnode_resolve because EFI has no concept of symbolic links.
811 EFI_STATUS
fsw_efi_dir_open(IN FSW_FILE_DATA
*File
,
812 OUT EFI_FILE
**NewHandle
,
815 IN UINT64 Attributes
)
818 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
;
819 struct fsw_dnode
*dno
;
820 struct fsw_dnode
*target_dno
;
821 struct fsw_string lookup_path
;
824 Print(L
"fsw_efi_dir_open: '%s'\n", FileName
);
827 if (OpenMode
!= EFI_FILE_MODE_READ
)
828 return EFI_WRITE_PROTECTED
;
830 lookup_path
.type
= FSW_STRING_TYPE_UTF16
;
831 lookup_path
.len
= (int)StrLen(FileName
);
832 lookup_path
.size
= lookup_path
.len
* sizeof(fsw_u16
);
833 lookup_path
.data
= FileName
;
835 // resolve the path (symlinks along the way are automatically resolved)
836 Status
= fsw_efi_map_status(fsw_dnode_lookup_path(File
->shand
.dnode
, &lookup_path
, '\\', &dno
),
838 if (EFI_ERROR(Status
))
841 // if the final node is a symlink, also resolve it
842 Status
= fsw_efi_map_status(fsw_dnode_resolve(dno
, &target_dno
),
844 fsw_dnode_release(dno
);
845 if (EFI_ERROR(Status
))
849 // make a new EFI handle for the target dnode
850 Status
= fsw_efi_dnode_to_FileHandle(dno
, NewHandle
);
851 fsw_dnode_release(dno
);
856 * Read function for directories. A file handle read on a directory retrieves
857 * the next directory entry.
860 EFI_STATUS
fsw_efi_dir_read(IN FSW_FILE_DATA
*File
,
861 IN OUT UINTN
*BufferSize
,
865 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
;
866 struct fsw_dnode
*dno
;
869 Print(L
"fsw_efi_dir_read...\n");
872 // read the next entry
873 Status
= fsw_efi_map_status(fsw_dnode_dir_read(&File
->shand
, &dno
),
875 if (Status
== EFI_NOT_FOUND
) {
879 Print(L
"...no more entries\n");
883 if (EFI_ERROR(Status
))
886 // get info into buffer
887 Status
= fsw_efi_dnode_fill_FileInfo(Volume
, dno
, BufferSize
, Buffer
);
888 fsw_dnode_release(dno
);
893 * Set file position for directories. The only allowed set position operation
894 * for directories is to rewind the directory completely by setting the
898 EFI_STATUS
fsw_efi_dir_setpos(IN FSW_FILE_DATA
*File
,
905 // directories can only rewind to the start
906 return EFI_UNSUPPORTED
;
911 * Get file or volume information. This function implements the GetInfo call
912 * for all file handles. Control is dispatched according to the type of information
913 * requested by the caller.
916 EFI_STATUS
fsw_efi_dnode_getinfo(IN FSW_FILE_DATA
*File
,
917 IN EFI_GUID
*InformationType
,
918 IN OUT UINTN
*BufferSize
,
922 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
;
923 EFI_FILE_SYSTEM_INFO
*FSInfo
;
925 struct fsw_volume_stat vsb
;
928 if (CompareGuid(InformationType
, &gEfiFileInfoGuid
)) {
930 Print(L
"fsw_efi_dnode_getinfo: FILE_INFO\n");
933 Status
= fsw_efi_dnode_fill_FileInfo(Volume
, File
->shand
.dnode
, BufferSize
, Buffer
);
935 } else if (CompareGuid(InformationType
, &gEfiFileSystemInfoGuid
)) {
937 Print(L
"fsw_efi_dnode_getinfo: FILE_SYSTEM_INFO\n");
941 RequiredSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ fsw_efi_strsize(&Volume
->vol
->label
);
942 if (*BufferSize
< RequiredSize
) {
943 *BufferSize
= RequiredSize
;
944 return EFI_BUFFER_TOO_SMALL
;
948 FSInfo
= (EFI_FILE_SYSTEM_INFO
*)Buffer
;
949 FSInfo
->Size
= RequiredSize
;
950 FSInfo
->ReadOnly
= TRUE
;
951 FSInfo
->BlockSize
= Volume
->vol
->log_blocksize
;
952 fsw_efi_strcpy(FSInfo
->VolumeLabel
, &Volume
->vol
->label
);
954 // get the missing info from the fs driver
955 ZeroMem(&vsb
, sizeof(struct fsw_volume_stat
));
956 Status
= fsw_efi_map_status(fsw_volume_stat(Volume
->vol
, &vsb
), Volume
);
957 if (EFI_ERROR(Status
))
959 FSInfo
->VolumeSize
= vsb
.total_bytes
;
960 FSInfo
->FreeSpace
= vsb
.free_bytes
;
962 // prepare for return
963 *BufferSize
= RequiredSize
;
964 Status
= EFI_SUCCESS
;
966 } else if (CompareGuid(InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
968 Print(L
"fsw_efi_dnode_getinfo: FILE_SYSTEM_VOLUME_LABEL\n");
972 RequiredSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
+ fsw_efi_strsize(&Volume
->vol
->label
);
973 if (*BufferSize
< RequiredSize
) {
974 *BufferSize
= RequiredSize
;
975 return EFI_BUFFER_TOO_SMALL
;
979 fsw_efi_strcpy(((EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*)Buffer
)->VolumeLabel
, &Volume
->vol
->label
);
981 // prepare for return
982 *BufferSize
= RequiredSize
;
983 Status
= EFI_SUCCESS
;
986 Status
= EFI_UNSUPPORTED
;
993 * Time mapping callback for the fsw_dnode_stat call. This function converts
994 * a Posix style timestamp into an EFI_TIME structure and writes it to the
995 * appropriate member of the EFI_FILE_INFO structure that we're filling.
998 static void fsw_efi_store_time_posix(struct fsw_dnode_stat
*sb
, int which
, fsw_u32 posix_time
)
1000 EFI_FILE_INFO
*FileInfo
= (EFI_FILE_INFO
*)sb
->host_data
;
1002 if (which
== FSW_DNODE_STAT_CTIME
)
1003 fsw_efi_decode_time(&FileInfo
->CreateTime
, posix_time
);
1004 else if (which
== FSW_DNODE_STAT_MTIME
)
1005 fsw_efi_decode_time(&FileInfo
->ModificationTime
, posix_time
);
1006 else if (which
== FSW_DNODE_STAT_ATIME
)
1007 fsw_efi_decode_time(&FileInfo
->LastAccessTime
, posix_time
);
1011 * Mode mapping callback for the fsw_dnode_stat call. This function looks at
1012 * the Posix mode passed by the file system driver and makes appropriate
1013 * adjustments to the EFI_FILE_INFO structure that we're filling.
1016 static void fsw_efi_store_attr_posix(struct fsw_dnode_stat
*sb
, fsw_u16 posix_mode
)
1018 EFI_FILE_INFO
*FileInfo
= (EFI_FILE_INFO
*)sb
->host_data
;
1020 if ((posix_mode
& S_IWUSR
) == 0)
1021 FileInfo
->Attribute
|= EFI_FILE_READ_ONLY
;
1025 * Common function to fill an EFI_FILE_INFO with information about a dnode.
1028 EFI_STATUS
fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA
*Volume
,
1029 IN
struct fsw_dnode
*dno
,
1030 IN OUT UINTN
*BufferSize
,
1034 EFI_FILE_INFO
*FileInfo
;
1036 struct fsw_dnode_stat sb
;
1038 // make sure the dnode has complete info
1039 Status
= fsw_efi_map_status(fsw_dnode_fill(dno
), Volume
);
1040 if (EFI_ERROR(Status
))
1043 // TODO: check/assert that the dno's name is in UTF16
1045 // check buffer size
1046 RequiredSize
= SIZE_OF_EFI_FILE_INFO
+ fsw_efi_strsize(&dno
->name
);
1047 if (*BufferSize
< RequiredSize
) {
1048 // TODO: wind back the directory in this case
1051 Print(L
"...BUFFER TOO SMALL\n");
1053 *BufferSize
= RequiredSize
;
1054 return EFI_BUFFER_TOO_SMALL
;
1058 ZeroMem(Buffer
, RequiredSize
);
1059 FileInfo
= (EFI_FILE_INFO
*)Buffer
;
1060 FileInfo
->Size
= RequiredSize
;
1061 FileInfo
->FileSize
= dno
->size
;
1062 FileInfo
->Attribute
= 0;
1063 if (dno
->type
== FSW_DNODE_TYPE_DIR
)
1064 FileInfo
->Attribute
|= EFI_FILE_DIRECTORY
;
1065 fsw_efi_strcpy(FileInfo
->FileName
, &dno
->name
);
1067 // get the missing info from the fs driver
1068 ZeroMem(&sb
, sizeof(struct fsw_dnode_stat
));
1069 sb
.store_time_posix
= fsw_efi_store_time_posix
;
1070 sb
.store_attr_posix
= fsw_efi_store_attr_posix
;
1071 sb
.host_data
= FileInfo
;
1072 Status
= fsw_efi_map_status(fsw_dnode_stat(dno
, &sb
), Volume
);
1073 if (EFI_ERROR(Status
))
1075 FileInfo
->PhysicalSize
= sb
.used_bytes
;
1077 // prepare for return
1078 *BufferSize
= RequiredSize
;
1080 Print(L
"...returning '%s'\n", FileInfo
->FileName
);