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.
54 #ifdef __MAKEWITH_GNUEFI
55 #include "edk2/DriverBinding.h"
56 #include "edk2/ComponentName.h"
58 #include "../include/refit_call_wrapper.h"
63 #error FSTYPE must be defined!
69 #define DBG(x...) AsciiPrint(x)
71 #define DBG(x...) BootLog(x)
76 #ifdef __MAKEWITH_GNUEFI
78 #define EFI_DISK_IO_PROTOCOL_GUID \
80 0xce345171, 0xba0b, 0x11d2, {0x8e, 0x4f, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
83 #define EFI_BLOCK_IO_PROTOCOL_GUID \
85 0x964e5b21, 0x6459, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
88 EFI_GUID gEfiDriverBindingProtocolGuid
= EFI_DRIVER_BINDING_PROTOCOL_GUID
;
89 EFI_GUID gEfiComponentNameProtocolGuid
= EFI_COMPONENT_NAME_PROTOCOL_GUID
;
90 EFI_GUID gEfiDiskIoProtocolGuid
= EFI_DISK_IO_PROTOCOL_GUID
;
91 EFI_GUID gEfiBlockIoProtocolGuid
= EFI_BLOCK_IO_PROTOCOL_GUID
;
92 EFI_GUID gEfiFileInfoGuid
= EFI_FILE_INFO_ID
;
93 EFI_GUID gEfiFileSystemInfoGuid
= EFI_FILE_SYSTEM_INFO_ID
;
94 EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid
= EFI_FILE_SYSTEM_VOLUME_LABEL_INFO_ID
;
95 #define SimpleFileSystemProtocol FileSystemProtocol
98 /** Helper macro for stringification. */
99 #define FSW_EFI_STRINGIFY(x) #x
100 /** Expands to the EFI driver name given the file system type name. */
101 #define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.6.2.1 " FSW_EFI_STRINGIFY(t) L" File System Driver"
103 // function prototypes
105 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
106 IN EFI_HANDLE ControllerHandle
,
107 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
);
108 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
109 IN EFI_HANDLE ControllerHandle
,
110 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
);
111 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
112 IN EFI_HANDLE ControllerHandle
,
113 IN UINTN NumberOfChildren
,
114 IN EFI_HANDLE
*ChildHandleBuffer
);
116 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
118 OUT CHAR16
**DriverName
);
119 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
120 IN EFI_HANDLE ControllerHandle
,
121 IN EFI_HANDLE ChildHandle OPTIONAL
,
123 OUT CHAR16
**ControllerName
);
125 void fsw_efi_change_blocksize(struct fsw_volume
*vol
,
126 fsw_u32 old_phys_blocksize
, fsw_u32 old_log_blocksize
,
127 fsw_u32 new_phys_blocksize
, fsw_u32 new_log_blocksize
);
128 fsw_status_t
fsw_efi_read_block(struct fsw_volume
*vol
, fsw_u32 phys_bno
, void *buffer
);
130 EFI_STATUS
fsw_efi_map_status(fsw_status_t fsw_status
, FSW_VOLUME_DATA
*Volume
);
132 EFI_STATUS EFIAPI
fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE
*This
,
133 OUT EFI_FILE
**Root
);
134 EFI_STATUS
fsw_efi_dnode_to_FileHandle(IN
struct fsw_dnode
*dno
,
135 OUT EFI_FILE
**NewFileHandle
);
137 EFI_STATUS
fsw_efi_file_read(IN FSW_FILE_DATA
*File
,
138 IN OUT UINTN
*BufferSize
,
140 EFI_STATUS
fsw_efi_file_getpos(IN FSW_FILE_DATA
*File
,
141 OUT UINT64
*Position
);
142 EFI_STATUS
fsw_efi_file_setpos(IN FSW_FILE_DATA
*File
,
145 EFI_STATUS
fsw_efi_dir_open(IN FSW_FILE_DATA
*File
,
146 OUT EFI_FILE
**NewHandle
,
149 IN UINT64 Attributes
);
150 EFI_STATUS
fsw_efi_dir_read(IN FSW_FILE_DATA
*File
,
151 IN OUT UINTN
*BufferSize
,
153 EFI_STATUS
fsw_efi_dir_setpos(IN FSW_FILE_DATA
*File
,
156 EFI_STATUS
fsw_efi_dnode_getinfo(IN FSW_FILE_DATA
*File
,
157 IN EFI_GUID
*InformationType
,
158 IN OUT UINTN
*BufferSize
,
160 EFI_STATUS
fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA
*Volume
,
161 IN
struct fsw_dnode
*dno
,
162 IN OUT UINTN
*BufferSize
,
166 * Interface structure for the EFI Driver Binding protocol.
169 EFI_DRIVER_BINDING_PROTOCOL fsw_efi_DriverBinding_table
= {
170 fsw_efi_DriverBinding_Supported
,
171 fsw_efi_DriverBinding_Start
,
172 fsw_efi_DriverBinding_Stop
,
179 * Interface structure for the EFI Component Name protocol.
182 EFI_COMPONENT_NAME_PROTOCOL fsw_efi_ComponentName_table
= {
183 fsw_efi_ComponentName_GetDriverName
,
184 fsw_efi_ComponentName_GetControllerName
,
189 * Dispatch table for our FSW host driver.
192 struct fsw_host_table fsw_efi_host_table
= {
193 FSW_STRING_TYPE_UTF16
,
195 fsw_efi_change_blocksize
,
199 extern struct fsw_fstype_table
FSW_FSTYPE_TABLE_NAME(FSTYPE
);
201 //#include "OverrideFunctions-kabyl.edk2.c.include"
204 * Image entry point. Installs the Driver Binding and Component Name protocols
205 * on the image's handle. Actually mounting a file system is initiated through
206 * the Driver Binding protocol at the firmware's request.
208 EFI_STATUS EFIAPI
fsw_efi_main(IN EFI_HANDLE ImageHandle
,
209 IN EFI_SYSTEM_TABLE
*SystemTable
)
213 #ifndef HOST_EFI_EDK2
214 // Not available in EDK2 toolkit
215 InitializeLib(ImageHandle
, SystemTable
);
218 // complete Driver Binding protocol instance
219 fsw_efi_DriverBinding_table
.ImageHandle
= ImageHandle
;
220 fsw_efi_DriverBinding_table
.DriverBindingHandle
= ImageHandle
;
221 // install Driver Binding protocol
222 Status
= refit_call4_wrapper(BS
->InstallProtocolInterface
, &fsw_efi_DriverBinding_table
.DriverBindingHandle
,
223 &gEfiDriverBindingProtocolGuid
,
224 EFI_NATIVE_INTERFACE
,
225 &fsw_efi_DriverBinding_table
);
226 if (EFI_ERROR (Status
)) {
230 // install Component Name protocol
231 Status
= refit_call4_wrapper(BS
->InstallProtocolInterface
, &fsw_efi_DriverBinding_table
.DriverBindingHandle
,
232 &gEfiComponentNameProtocolGuid
,
233 EFI_NATIVE_INTERFACE
,
234 &fsw_efi_ComponentName_table
);
235 if (EFI_ERROR (Status
)) {
239 // OverrideFunctions();
242 // Status = BS->LocateProtocol(&gMsgLogProtocolGuid, NULL, (VOID **) &Msg);
243 // if (!EFI_ERROR(Status) && (Msg != NULL)) {
244 // msgCursor = Msg->Cursor;
245 // BootLog("MsgLog installed into VBoxFs\n");
251 #ifdef __MAKEWITH_GNUEFI
252 EFI_DRIVER_ENTRY_POINT(fsw_efi_main
)
256 * Driver Binding EFI protocol, Supported function. This function is called by EFI
257 * to test if this driver can handle a certain device. Our implementation only checks
258 * if the device is a disk (i.e. that it supports the Block I/O and Disk I/O protocols)
259 * and implicitly checks if the disk is already in use by another driver.
262 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
263 IN EFI_HANDLE ControllerHandle
,
264 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
)
269 // we check for both DiskIO and BlockIO protocols
271 // first, open DiskIO
272 Status
= refit_call6_wrapper(BS
->OpenProtocol
, ControllerHandle
,
273 &PROTO_NAME(DiskIoProtocol
),
275 This
->DriverBindingHandle
,
277 EFI_OPEN_PROTOCOL_BY_DRIVER
);
278 if (EFI_ERROR(Status
))
281 // we were just checking, close it again
282 refit_call4_wrapper(BS
->CloseProtocol
, ControllerHandle
,
283 &PROTO_NAME(DiskIoProtocol
),
284 This
->DriverBindingHandle
,
287 // next, check BlockIO without actually opening it
288 Status
= refit_call6_wrapper(BS
->OpenProtocol
, ControllerHandle
,
289 &PROTO_NAME(BlockIoProtocol
),
291 This
->DriverBindingHandle
,
293 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
);
298 * Driver Binding EFI protocol, Start function. This function is called by EFI
299 * to start driving the given device. It is still possible at this point to
300 * return EFI_UNSUPPORTED, and in fact we will do so if the file system driver
301 * cannot find the superblock signature (or equivalent) that it expects.
303 * This function allocates memory for a per-volume structure, opens the
304 * required protocols (just Disk I/O in our case, Block I/O is only looked
305 * at to get the MediaId field), and lets the FSW core mount the file system.
306 * If successful, an EFI Simple File System protocol is exported on the
310 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
311 IN EFI_HANDLE ControllerHandle
,
312 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
)
315 EFI_BLOCK_IO
*BlockIo
;
317 FSW_VOLUME_DATA
*Volume
;
320 Print(L
"fsw_efi_DriverBinding_Start\n");
323 // open consumed protocols
324 Status
= refit_call6_wrapper(BS
->OpenProtocol
, ControllerHandle
,
325 &PROTO_NAME(BlockIoProtocol
),
327 This
->DriverBindingHandle
,
329 EFI_OPEN_PROTOCOL_GET_PROTOCOL
); // NOTE: we only want to look at the MediaId
330 if (EFI_ERROR(Status
)) {
331 // Print(L"Fsw ERROR: OpenProtocol(BlockIo) returned %x\n", Status);
335 Status
= refit_call6_wrapper(BS
->OpenProtocol
, ControllerHandle
,
336 &PROTO_NAME(DiskIoProtocol
),
338 This
->DriverBindingHandle
,
340 EFI_OPEN_PROTOCOL_BY_DRIVER
);
341 if (EFI_ERROR(Status
)) {
342 Print(L
"Fsw ERROR: OpenProtocol(DiskIo) returned %r\n", Status
);
346 // allocate volume structure
347 Volume
= AllocateZeroPool(sizeof(FSW_VOLUME_DATA
));
348 Volume
->Signature
= FSW_VOLUME_DATA_SIGNATURE
;
349 Volume
->Handle
= ControllerHandle
;
350 Volume
->DiskIo
= DiskIo
;
351 Volume
->MediaId
= BlockIo
->Media
->MediaId
;
352 Volume
->LastIOStatus
= EFI_SUCCESS
;
354 // mount the filesystem
355 Status
= fsw_efi_map_status(fsw_mount(Volume
, &fsw_efi_host_table
,
356 &FSW_FSTYPE_TABLE_NAME(FSTYPE
), &Volume
->vol
),
359 if (!EFI_ERROR(Status
)) {
360 // register the SimpleFileSystem protocol
361 Volume
->FileSystem
.Revision
= EFI_FILE_IO_INTERFACE_REVISION
;
362 Volume
->FileSystem
.OpenVolume
= fsw_efi_FileSystem_OpenVolume
;
363 Status
= refit_call4_wrapper(BS
->InstallMultipleProtocolInterfaces
, &ControllerHandle
,
364 &PROTO_NAME(SimpleFileSystemProtocol
),
367 if (EFI_ERROR(Status
)) {
368 // Print(L"Fsw ERROR: InstallMultipleProtocolInterfaces returned %x\n", Status);
372 // on errors, close the opened protocols
373 if (EFI_ERROR(Status
)) {
374 if (Volume
->vol
!= NULL
)
375 fsw_unmount(Volume
->vol
);
378 refit_call4_wrapper(BS
->CloseProtocol
, ControllerHandle
,
379 &PROTO_NAME(DiskIoProtocol
),
380 This
->DriverBindingHandle
,
388 * Driver Binding EFI protocol, Stop function. This function is called by EFI
389 * to stop the driver on the given device. This translates to an unmount
390 * call for the FSW core.
392 * We assume that all file handles on the volume have been closed before
393 * the driver is stopped. At least with the EFI shell, that is actually the
394 * case; it closes all file handles between commands.
397 EFI_STATUS EFIAPI
fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
398 IN EFI_HANDLE ControllerHandle
,
399 IN UINTN NumberOfChildren
,
400 IN EFI_HANDLE
*ChildHandleBuffer
)
403 EFI_FILE_IO_INTERFACE
*FileSystem
;
404 FSW_VOLUME_DATA
*Volume
;
407 Print(L
"fsw_efi_DriverBinding_Stop\n");
410 // get the installed SimpleFileSystem interface
411 Status
= refit_call6_wrapper(BS
->OpenProtocol
, ControllerHandle
,
412 &PROTO_NAME(SimpleFileSystemProtocol
),
413 (VOID
**) &FileSystem
,
414 This
->DriverBindingHandle
,
416 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
417 if (EFI_ERROR(Status
))
418 return EFI_UNSUPPORTED
;
420 // get private data structure
421 Volume
= FSW_VOLUME_FROM_FILE_SYSTEM(FileSystem
);
423 // uninstall Simple File System protocol
424 Status
= refit_call4_wrapper(BS
->UninstallMultipleProtocolInterfaces
, ControllerHandle
,
425 &PROTO_NAME(SimpleFileSystemProtocol
), &Volume
->FileSystem
,
427 if (EFI_ERROR(Status
)) {
428 // Print(L"Fsw ERROR: UninstallMultipleProtocolInterfaces returned %x\n", Status);
432 Print(L
"fsw_efi_DriverBinding_Stop: protocol uninstalled successfully\n");
435 // release private data structure
436 if (Volume
->vol
!= NULL
)
437 fsw_unmount(Volume
->vol
);
440 // close the consumed protocols
441 Status
= refit_call4_wrapper(BS
->CloseProtocol
, ControllerHandle
,
442 &PROTO_NAME(DiskIoProtocol
),
443 This
->DriverBindingHandle
,
450 * Component Name EFI protocol, GetDriverName function. Used by the EFI
451 * environment to inquire the name of this driver. The name returned is
452 * based on the file system type actually used in compilation.
455 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
457 OUT CHAR16
**DriverName
)
459 if (Language
== NULL
|| DriverName
== NULL
)
460 return EFI_INVALID_PARAMETER
;
462 if (Language
[0] == 'e' && Language
[1] == 'n' && Language
[2] == 'g' && Language
[3] == 0) {
463 *DriverName
= FSW_EFI_DRIVER_NAME(FSTYPE
);
466 return EFI_UNSUPPORTED
;
470 * Component Name EFI protocol, GetControllerName function. Not implemented
471 * because this is not a "bus" driver in the sense of the EFI Driver Model.
474 EFI_STATUS EFIAPI
fsw_efi_ComponentName_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
475 IN EFI_HANDLE ControllerHandle
,
476 IN EFI_HANDLE ChildHandle OPTIONAL
,
478 OUT CHAR16
**ControllerName
)
480 return EFI_UNSUPPORTED
;
484 * FSW interface function for block size changes. This function is called by the FSW core
485 * when the file system driver changes the block sizes for the volume.
488 void fsw_efi_change_blocksize(struct fsw_volume
*vol
,
489 fsw_u32 old_phys_blocksize
, fsw_u32 old_log_blocksize
,
490 fsw_u32 new_phys_blocksize
, fsw_u32 new_log_blocksize
)
496 * FSW interface function to read data blocks. This function is called by the FSW core
497 * to read a block of data from the device. The buffer is allocated by the core code.
500 fsw_status_t
fsw_efi_read_block(struct fsw_volume
*vol
, fsw_u32 phys_bno
, void *buffer
)
503 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)vol
->host_data
;
505 // FSW_MSG_DEBUGV((FSW_MSGSTR("fsw_efi_read_block: %d (%d)\n"), phys_bno, vol->phys_blocksize));
508 Status
= refit_call5_wrapper(Volume
->DiskIo
->ReadDisk
, Volume
->DiskIo
, Volume
->MediaId
,
509 (UINT64
)phys_bno
* vol
->phys_blocksize
,
512 Volume
->LastIOStatus
= Status
;
513 if (EFI_ERROR(Status
))
519 * Map FSW status codes to EFI status codes. The FSW_IO_ERROR code is only produced
520 * by fsw_efi_read_block, so we map it back to the EFI status code remembered from
521 * the last I/O operation.
524 EFI_STATUS
fsw_efi_map_status(fsw_status_t fsw_status
, FSW_VOLUME_DATA
*Volume
)
526 switch (fsw_status
) {
529 case FSW_OUT_OF_MEMORY
:
530 return EFI_VOLUME_CORRUPTED
;
532 return Volume
->LastIOStatus
;
533 case FSW_UNSUPPORTED
:
534 return EFI_UNSUPPORTED
;
536 return EFI_NOT_FOUND
;
537 case FSW_VOLUME_CORRUPTED
:
538 return EFI_VOLUME_CORRUPTED
;
540 return EFI_DEVICE_ERROR
;
545 * File System EFI protocol, OpenVolume function. Creates a file handle for
546 * the root directory and returns it. Note that this function may be called
547 * multiple times and returns a new file handle each time. Each returned
548 * handle is closed by the client using it.
551 EFI_STATUS EFIAPI
fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE
*This
,
555 FSW_VOLUME_DATA
*Volume
= FSW_VOLUME_FROM_FILE_SYSTEM(This
);
558 Print(L
"fsw_efi_FileSystem_OpenVolume\n");
561 Status
= fsw_efi_dnode_to_FileHandle(Volume
->vol
->root
, Root
);
567 * File Handle EFI protocol, Open function. Dispatches the call
568 * based on the kind of file handle.
571 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Open(IN EFI_FILE
*This
,
572 OUT EFI_FILE
**NewHandle
,
575 IN UINT64 Attributes
)
577 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
579 if (File
->Type
== FSW_EFI_FILE_TYPE_DIR
)
580 return fsw_efi_dir_open(File
, NewHandle
, FileName
, OpenMode
, Attributes
);
581 // not supported for regular files
582 return EFI_UNSUPPORTED
;
586 * File Handle EFI protocol, Close function. Closes the FSW shandle
587 * and frees the memory used for the structure.
590 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Close(IN EFI_FILE
*This
)
592 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
595 Print(L
"fsw_efi_FileHandle_Close\n");
598 fsw_shandle_close(&File
->shand
);
605 * File Handle EFI protocol, Delete function. Calls through to Close
606 * and returns a warning because this driver is read-only.
609 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Delete(IN EFI_FILE
*This
)
613 Status
= refit_call1_wrapper(This
->Close
, This
);
614 if (Status
== EFI_SUCCESS
) {
615 // this driver is read-only
616 Status
= EFI_WARN_DELETE_FAILURE
;
623 * File Handle EFI protocol, Read function. Dispatches the call
624 * based on the kind of file handle.
627 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Read(IN EFI_FILE
*This
,
628 IN OUT UINTN
*BufferSize
,
631 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
633 if (File
->Type
== FSW_EFI_FILE_TYPE_FILE
)
634 return fsw_efi_file_read(File
, BufferSize
, Buffer
);
635 else if (File
->Type
== FSW_EFI_FILE_TYPE_DIR
)
636 return fsw_efi_dir_read(File
, BufferSize
, Buffer
);
637 return EFI_UNSUPPORTED
;
641 * File Handle EFI protocol, Write function. Returns unsupported status
642 * because this driver is read-only.
645 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Write(IN EFI_FILE
*This
,
646 IN OUT UINTN
*BufferSize
,
649 // this driver is read-only
650 return EFI_WRITE_PROTECTED
;
654 * File Handle EFI protocol, GetPosition function. Dispatches the call
655 * based on the kind of file handle.
658 EFI_STATUS EFIAPI
fsw_efi_FileHandle_GetPosition(IN EFI_FILE
*This
,
659 OUT UINT64
*Position
)
661 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
663 if (File
->Type
== FSW_EFI_FILE_TYPE_FILE
)
664 return fsw_efi_file_getpos(File
, Position
);
665 // not defined for directories
666 return EFI_UNSUPPORTED
;
670 * File Handle EFI protocol, SetPosition function. Dispatches the call
671 * based on the kind of file handle.
674 EFI_STATUS EFIAPI
fsw_efi_FileHandle_SetPosition(IN EFI_FILE
*This
,
677 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
679 if (File
->Type
== FSW_EFI_FILE_TYPE_FILE
)
680 return fsw_efi_file_setpos(File
, Position
);
681 else if (File
->Type
== FSW_EFI_FILE_TYPE_DIR
)
682 return fsw_efi_dir_setpos(File
, Position
);
683 return EFI_UNSUPPORTED
;
687 * File Handle EFI protocol, GetInfo function. Dispatches to the common
688 * function implementing this.
691 EFI_STATUS EFIAPI
fsw_efi_FileHandle_GetInfo(IN EFI_FILE
*This
,
692 IN EFI_GUID
*InformationType
,
693 IN OUT UINTN
*BufferSize
,
696 FSW_FILE_DATA
*File
= FSW_FILE_FROM_FILE_HANDLE(This
);
698 return fsw_efi_dnode_getinfo(File
, InformationType
, BufferSize
, Buffer
);
702 * File Handle EFI protocol, SetInfo function. Returns unsupported status
703 * because this driver is read-only.
706 EFI_STATUS EFIAPI
fsw_efi_FileHandle_SetInfo(IN EFI_FILE
*This
,
707 IN EFI_GUID
*InformationType
,
711 // this driver is read-only
712 return EFI_WRITE_PROTECTED
;
716 * File Handle EFI protocol, Flush function. Returns unsupported status
717 * because this driver is read-only.
720 EFI_STATUS EFIAPI
fsw_efi_FileHandle_Flush(IN EFI_FILE
*This
)
722 // this driver is read-only
723 return EFI_WRITE_PROTECTED
;
727 * Set up a file handle for a dnode. This function allocates a data structure
728 * for a file handle, opens a FSW shandle and populates the EFI_FILE structure
729 * with the interface functions.
732 EFI_STATUS
fsw_efi_dnode_to_FileHandle(IN
struct fsw_dnode
*dno
,
733 OUT EFI_FILE
**NewFileHandle
)
738 // make sure the dnode has complete info
739 Status
= fsw_efi_map_status(fsw_dnode_fill(dno
), (FSW_VOLUME_DATA
*)dno
->vol
->host_data
);
740 if (EFI_ERROR(Status
))
744 if (dno
->type
!= FSW_DNODE_TYPE_FILE
&& dno
->type
!= FSW_DNODE_TYPE_DIR
)
745 return EFI_UNSUPPORTED
;
747 // allocate file structure
748 File
= AllocateZeroPool(sizeof(FSW_FILE_DATA
));
749 File
->Signature
= FSW_FILE_DATA_SIGNATURE
;
750 if (dno
->type
== FSW_DNODE_TYPE_FILE
)
751 File
->Type
= FSW_EFI_FILE_TYPE_FILE
;
752 else if (dno
->type
== FSW_DNODE_TYPE_DIR
)
753 File
->Type
= FSW_EFI_FILE_TYPE_DIR
;
756 Status
= fsw_efi_map_status(fsw_shandle_open(dno
, &File
->shand
),
757 (FSW_VOLUME_DATA
*)dno
->vol
->host_data
);
758 if (EFI_ERROR(Status
)) {
763 // populate the file handle
764 File
->FileHandle
.Revision
= EFI_FILE_HANDLE_REVISION
;
765 File
->FileHandle
.Open
= fsw_efi_FileHandle_Open
;
766 File
->FileHandle
.Close
= fsw_efi_FileHandle_Close
;
767 File
->FileHandle
.Delete
= fsw_efi_FileHandle_Delete
;
768 File
->FileHandle
.Read
= fsw_efi_FileHandle_Read
;
769 File
->FileHandle
.Write
= fsw_efi_FileHandle_Write
;
770 File
->FileHandle
.GetPosition
= fsw_efi_FileHandle_GetPosition
;
771 File
->FileHandle
.SetPosition
= fsw_efi_FileHandle_SetPosition
;
772 File
->FileHandle
.GetInfo
= fsw_efi_FileHandle_GetInfo
;
773 File
->FileHandle
.SetInfo
= fsw_efi_FileHandle_SetInfo
;
774 File
->FileHandle
.Flush
= fsw_efi_FileHandle_Flush
;
776 *NewFileHandle
= &File
->FileHandle
;
781 * Data read function for regular files. Calls through to fsw_shandle_read.
784 EFI_STATUS
fsw_efi_file_read(IN FSW_FILE_DATA
*File
,
785 IN OUT UINTN
*BufferSize
,
792 Print(L
"fsw_efi_file_read %d bytes\n", *BufferSize
);
795 buffer_size
= (fsw_u32
)*BufferSize
;
796 Status
= fsw_efi_map_status(fsw_shandle_read(&File
->shand
, &buffer_size
, Buffer
),
797 (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
);
798 *BufferSize
= buffer_size
;
804 * Get file position for regular files.
807 EFI_STATUS
fsw_efi_file_getpos(IN FSW_FILE_DATA
*File
,
808 OUT UINT64
*Position
)
810 *Position
= File
->shand
.pos
;
815 * Set file position for regular files. EFI specifies the all-ones value
816 * to be a special value for the end of the file.
819 EFI_STATUS
fsw_efi_file_setpos(IN FSW_FILE_DATA
*File
, IN UINT64 Position
)
821 if (Position
== 0xFFFFFFFFFFFFFFFFULL
)
822 File
->shand
.pos
= File
->shand
.dnode
->size
;
824 File
->shand
.pos
= Position
;
829 * Open function used to open new file handles relative to a directory.
830 * In EFI, the "open file" function is implemented by directory file handles
831 * and is passed a relative or volume-absolute path to the file or directory
832 * to open. We use fsw_dnode_lookup_path to find the node plus an additional
833 * call to fsw_dnode_resolve because EFI has no concept of symbolic links.
836 EFI_STATUS
fsw_efi_dir_open(IN FSW_FILE_DATA
*File
,
837 OUT EFI_FILE
**NewHandle
,
840 IN UINT64 Attributes
)
843 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
;
844 struct fsw_dnode
*dno
;
845 struct fsw_dnode
*target_dno
;
846 struct fsw_string lookup_path
;
849 Print(L
"fsw_efi_dir_open: '%s'\n", FileName
);
852 if (OpenMode
!= EFI_FILE_MODE_READ
)
853 return EFI_WRITE_PROTECTED
;
855 lookup_path
.type
= FSW_STRING_TYPE_UTF16
;
856 lookup_path
.len
= (int)StrLen(FileName
);
857 lookup_path
.size
= lookup_path
.len
* sizeof(fsw_u16
);
858 lookup_path
.data
= FileName
;
860 // resolve the path (symlinks along the way are automatically resolved)
861 Status
= fsw_efi_map_status(fsw_dnode_lookup_path(File
->shand
.dnode
, &lookup_path
, '\\', &dno
), Volume
);
862 if (EFI_ERROR(Status
))
865 // if the final node is a symlink, also resolve it
866 Status
= fsw_efi_map_status(fsw_dnode_resolve(dno
, &target_dno
), Volume
);
867 fsw_dnode_release(dno
);
868 if (EFI_ERROR(Status
))
872 // make a new EFI handle for the target dnode
873 Status
= fsw_efi_dnode_to_FileHandle(dno
, NewHandle
);
874 fsw_dnode_release(dno
);
879 * Read function for directories. A file handle read on a directory retrieves
880 * the next directory entry.
883 EFI_STATUS
fsw_efi_dir_read(IN FSW_FILE_DATA
*File
,
884 IN OUT UINTN
*BufferSize
,
888 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
;
889 struct fsw_dnode
*dno
;
892 Print(L
"fsw_efi_dir_read...\n");
895 // read the next entry
896 Status
= fsw_efi_map_status(fsw_dnode_dir_read(&File
->shand
, &dno
), Volume
);
897 if (Status
== EFI_NOT_FOUND
) {
901 Print(L
"...no more entries\n");
905 if (EFI_ERROR(Status
))
908 // get info into buffer
909 Status
= fsw_efi_dnode_fill_FileInfo(Volume
, dno
, BufferSize
, Buffer
);
910 fsw_dnode_release(dno
);
915 * Set file position for directories. The only allowed set position operation
916 * for directories is to rewind the directory completely by setting the
920 EFI_STATUS
fsw_efi_dir_setpos(IN FSW_FILE_DATA
*File
, IN UINT64 Position
)
926 // directories can only rewind to the start
927 return EFI_UNSUPPORTED
;
932 * Get file or volume information. This function implements the GetInfo call
933 * for all file handles. Control is dispatched according to the type of information
934 * requested by the caller.
937 EFI_STATUS
fsw_efi_dnode_getinfo(IN FSW_FILE_DATA
*File
,
938 IN EFI_GUID
*InformationType
,
939 IN OUT UINTN
*BufferSize
,
943 FSW_VOLUME_DATA
*Volume
= (FSW_VOLUME_DATA
*)File
->shand
.dnode
->vol
->host_data
;
944 EFI_FILE_SYSTEM_INFO
*FSInfo
;
946 struct fsw_volume_stat vsb
;
949 if (CompareGuid(InformationType
, &gEfiFileInfoGuid
)) {
951 Print(L
"fsw_efi_dnode_getinfo: FILE_INFO\n");
954 Status
= fsw_efi_dnode_fill_FileInfo(Volume
, File
->shand
.dnode
, BufferSize
, Buffer
);
956 } else if (CompareGuid(InformationType
, &gEfiFileSystemInfoGuid
)) {
958 Print(L
"fsw_efi_dnode_getinfo: FILE_SYSTEM_INFO\n");
962 RequiredSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ fsw_efi_strsize(&Volume
->vol
->label
);
963 if (*BufferSize
< RequiredSize
) {
964 *BufferSize
= RequiredSize
;
965 return EFI_BUFFER_TOO_SMALL
;
969 FSInfo
= (EFI_FILE_SYSTEM_INFO
*)Buffer
;
970 FSInfo
->Size
= RequiredSize
;
971 FSInfo
->ReadOnly
= TRUE
;
972 FSInfo
->BlockSize
= Volume
->vol
->log_blocksize
;
973 fsw_efi_strcpy(FSInfo
->VolumeLabel
, &Volume
->vol
->label
);
975 // get the missing info from the fs driver
976 ZeroMem(&vsb
, sizeof(struct fsw_volume_stat
));
977 Status
= fsw_efi_map_status(fsw_volume_stat(Volume
->vol
, &vsb
), Volume
);
978 if (EFI_ERROR(Status
))
980 FSInfo
->VolumeSize
= vsb
.total_bytes
;
981 FSInfo
->FreeSpace
= vsb
.free_bytes
;
983 // prepare for return
984 *BufferSize
= RequiredSize
;
985 Status
= EFI_SUCCESS
;
987 } else if (CompareGuid(InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
989 Print(L
"fsw_efi_dnode_getinfo: FILE_SYSTEM_VOLUME_LABEL\n");
993 RequiredSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
+ fsw_efi_strsize(&Volume
->vol
->label
);
994 if (*BufferSize
< RequiredSize
) {
995 *BufferSize
= RequiredSize
;
996 return EFI_BUFFER_TOO_SMALL
;
1000 fsw_efi_strcpy(((EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*)Buffer
)->VolumeLabel
, &Volume
->vol
->label
);
1002 // prepare for return
1003 *BufferSize
= RequiredSize
;
1004 Status
= EFI_SUCCESS
;
1007 Status
= EFI_UNSUPPORTED
;
1014 * Time mapping callback for the fsw_dnode_stat call. This function converts
1015 * a Posix style timestamp into an EFI_TIME structure and writes it to the
1016 * appropriate member of the EFI_FILE_INFO structure that we're filling.
1019 static void fsw_efi_store_time_posix(struct fsw_dnode_stat
*sb
, int which
, fsw_u32 posix_time
)
1021 EFI_FILE_INFO
*FileInfo
= (EFI_FILE_INFO
*)sb
->host_data
;
1023 if (which
== FSW_DNODE_STAT_CTIME
)
1024 fsw_efi_decode_time(&FileInfo
->CreateTime
, posix_time
);
1025 else if (which
== FSW_DNODE_STAT_MTIME
)
1026 fsw_efi_decode_time(&FileInfo
->ModificationTime
, posix_time
);
1027 else if (which
== FSW_DNODE_STAT_ATIME
)
1028 fsw_efi_decode_time(&FileInfo
->LastAccessTime
, posix_time
);
1032 * Mode mapping callback for the fsw_dnode_stat call. This function looks at
1033 * the Posix mode passed by the file system driver and makes appropriate
1034 * adjustments to the EFI_FILE_INFO structure that we're filling.
1037 static void fsw_efi_store_attr_posix(struct fsw_dnode_stat
*sb
, fsw_u16 posix_mode
)
1039 EFI_FILE_INFO
*FileInfo
= (EFI_FILE_INFO
*)sb
->host_data
;
1041 if ((posix_mode
& S_IWUSR
) == 0)
1042 FileInfo
->Attribute
|= EFI_FILE_READ_ONLY
;
1046 * Common function to fill an EFI_FILE_INFO with information about a dnode.
1049 EFI_STATUS
fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA
*Volume
,
1050 IN
struct fsw_dnode
*dno
,
1051 IN OUT UINTN
*BufferSize
,
1055 EFI_FILE_INFO
*FileInfo
;
1057 struct fsw_dnode_stat sb
;
1059 // make sure the dnode has complete info
1060 Status
= fsw_efi_map_status(fsw_dnode_fill(dno
), Volume
);
1061 if (EFI_ERROR(Status
))
1064 // TODO: check/assert that the dno's name is in UTF16
1066 // check buffer size
1067 RequiredSize
= SIZE_OF_EFI_FILE_INFO
+ fsw_efi_strsize(&dno
->name
);
1068 if (*BufferSize
< RequiredSize
) {
1069 // TODO: wind back the directory in this case
1072 Print(L
"...BUFFER TOO SMALL\n");
1074 *BufferSize
= RequiredSize
;
1075 return EFI_BUFFER_TOO_SMALL
;
1079 ZeroMem(Buffer
, RequiredSize
);
1080 FileInfo
= (EFI_FILE_INFO
*)Buffer
;
1081 FileInfo
->Size
= RequiredSize
;
1082 FileInfo
->FileSize
= dno
->size
;
1083 FileInfo
->Attribute
= 0;
1084 if (dno
->type
== FSW_DNODE_TYPE_DIR
)
1085 FileInfo
->Attribute
|= EFI_FILE_DIRECTORY
;
1086 fsw_efi_strcpy(FileInfo
->FileName
, &dno
->name
);
1088 // get the missing info from the fs driver
1089 ZeroMem(&sb
, sizeof(struct fsw_dnode_stat
));
1090 sb
.store_time_posix
= fsw_efi_store_time_posix
;
1091 sb
.store_attr_posix
= fsw_efi_store_attr_posix
;
1092 sb
.host_data
= FileInfo
;
1093 Status
= fsw_efi_map_status(fsw_dnode_stat(dno
, &sb
), Volume
);
1094 if (EFI_ERROR(Status
))
1096 FileInfo
->PhysicalSize
= sb
.used_bytes
;
1098 // prepare for return
1099 *BufferSize
= RequiredSize
;
1101 Print(L
"...returning '%s'\n", FileInfo
->FileName
);