fsw_efi.c revision e99df4387f81aa7b9900504a6a6117e0d3226eb3
/* $Id$ */
/** @file
* fsw_efi.c - EFI host environment code.
*/
/*
* Copyright (C) 2010-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*-
* This code is based on:
*
* Copyright (c) 2006 Christoph Pfisterer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* distribution.
*
* * Neither the name of Christoph Pfisterer nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "fsw_efi.h"
#define DEBUG_LEVEL 0
#ifndef FSTYPE
#ifdef VBOX
#else
#endif
#endif
/** Helper macro for stringification. */
#define FSW_EFI_STRINGIFY(x) L#x
/** Expands to the EFI driver name given the file system type name. */
// function prototypes
/**
* Interface structure for the EFI Driver Binding protocol.
*/
0x10,
NULL,
};
/**
* Interface structure for the EFI Component Name protocol.
*/
"eng"
};
/**
* Dispatch table for our FSW host driver.
*/
struct fsw_host_table fsw_efi_host_table = {
};
/**
* Image entry point. Installs the Driver Binding and Component Name protocols
* on the image's handle. Actually mounting a file system is initiated through
* the Driver Binding protocol at the firmware's request.
*/
{
#ifndef VBOX
#endif
// complete Driver Binding protocol instance
// install Driver Binding protocol
return Status;
}
// install Component Name protocol
return Status;
}
return EFI_SUCCESS;
}
/**
* Driver Binding EFI protocol, Supported function. This function is called by EFI
* to test if this driver can handle a certain device. Our implementation only checks
* if the device is a disk (i.e. that it supports the Block I/O and Disk I/O protocols)
* and implicitly checks if the disk is already in use by another driver.
*/
{
// we check for both DiskIO and BlockIO protocols
// first, open DiskIO
{
return Status;
}
// we were just checking, close it again
// next, check BlockIO without actually opening it
NULL,
return Status;
}
{
// mount the filesystem
pVolume);
// register the SimpleFileSystem protocol
NULL);
}
return Status;
}
/**
* Driver Binding EFI protocol, Start function. This function is called by EFI
* to start driving the given device. It is still possible at this point to
* return EFI_UNSUPPORTED, and in fact we will do so if the file system driver
* cannot find the superblock signature (or equivalent) that it expects.
*
* This function allocates memory for a per-volume structure, opens the
* required protocols (just Disk I/O in our case, Block I/O is only looked
* at to get the MediaId field), and lets the FSW core mount the file system.
* If successful, an EFI Simple File System protocol is exported on the
* device handle.
*/
{
// open consumed protocols
EFI_OPEN_PROTOCOL_GET_PROTOCOL); // NOTE: we only want to look at the MediaId
return Status;
}
return Status;
}
// allocate volume structure
// on errors, close the opened protocols
#if 0
if (Status == EFI_MEDIA_CHANGED)
else
#endif
}
return Status;
}
/**
* Driver Binding EFI protocol, Stop function. This function is called by EFI
* to stop the driver on the given device. This translates to an unmount
* call for the FSW core.
*
* We assume that all file handles on the volume have been closed before
* the driver is stopped. At least with the EFI shell, that is actually the
* case; it closes all file handles between commands.
*/
{
#if DEBUG_LEVEL
Print(L"fsw_efi_DriverBinding_Stop\n");
#endif
// get the installed SimpleFileSystem interface
(VOID **) &FileSystem,
return EFI_UNSUPPORTED;
// get private data structure
// uninstall Simple File System protocol
NULL);
return Status;
}
#if DEBUG_LEVEL
Print(L"fsw_efi_DriverBinding_Stop: protocol uninstalled successfully\n");
#endif
// release private data structure
// close the consumed protocols
return Status;
}
/**
* Component Name EFI protocol, GetDriverName function. Used by the EFI
* environment to inquire the name of this driver. The name returned is
* based on the file system type actually used in compilation.
*/
{
return EFI_INVALID_PARAMETER;
#if 0
return EFI_SUCCESS;
}
#endif
return EFI_UNSUPPORTED;
}
/**
* Component Name EFI protocol, GetControllerName function. Not implemented
* because this is not a "bus" driver in the sense of the EFI Driver Model.
*/
{
return EFI_UNSUPPORTED;
}
/**
* FSW interface function for block size changes. This function is called by the FSW core
* when the file system driver changes the block sizes for the volume.
*/
{
// nothing to do
}
/**
* FSW interface function to read data blocks. This function is called by the FSW core
* to read a block of data from the device. The buffer is allocated by the core code.
*/
{
// read from disk
buffer);
return FSW_IO_ERROR;
return FSW_SUCCESS;
}
/**
* Map FSW status codes to EFI status codes. The FSW_IO_ERROR code is only produced
* by fsw_efi_read_block, so we map it back to the EFI status code remembered from
* the last I/O operation.
*/
{
switch (fsw_status) {
case FSW_SUCCESS:
return EFI_SUCCESS;
case FSW_OUT_OF_MEMORY:
return EFI_VOLUME_CORRUPTED;
case FSW_IO_ERROR:
return Volume->LastIOStatus;
case FSW_UNSUPPORTED:
return EFI_UNSUPPORTED;
case FSW_NOT_FOUND:
return EFI_NOT_FOUND;
case FSW_VOLUME_CORRUPTED:
return EFI_VOLUME_CORRUPTED;
default:
return EFI_DEVICE_ERROR;
}
}
/**
* File System EFI protocol, OpenVolume function. Creates a file handle for
* the root directory and returns it. Note that this function may be called
* multiple times and returns a new file handle each time. Each returned
* handle is closed by the client using it.
*/
{
#if DEBUG_LEVEL
Print(L"fsw_efi_FileSystem_OpenVolume\n");
#endif
return Status;
}
/**
* File Handle EFI protocol, Open function. Dispatches the call
* based on the kind of file handle.
*/
{
// not supported for regular files
return EFI_UNSUPPORTED;
}
/**
* File Handle EFI protocol, Close function. Closes the FSW shandle
* and frees the memory used for the structure.
*/
{
#if DEBUG_LEVEL
Print(L"fsw_efi_FileHandle_Close\n");
#endif
return EFI_SUCCESS;
}
/**
* File Handle EFI protocol, Delete function. Calls through to Close
* and returns a warning because this driver is read-only.
*/
{
if (Status == EFI_SUCCESS) {
// this driver is read-only
}
return Status;
}
/**
* File Handle EFI protocol, Read function. Dispatches the call
* based on the kind of file handle.
*/
{
return EFI_UNSUPPORTED;
}
/**
* File Handle EFI protocol, Write function. Returns unsupported status
* because this driver is read-only.
*/
{
// this driver is read-only
return EFI_WRITE_PROTECTED;
}
/**
* File Handle EFI protocol, GetPosition function. Dispatches the call
* based on the kind of file handle.
*/
{
// not defined for directories
return EFI_UNSUPPORTED;
}
/**
* File Handle EFI protocol, SetPosition function. Dispatches the call
* based on the kind of file handle.
*/
{
return EFI_UNSUPPORTED;
}
/**
* File Handle EFI protocol, GetInfo function. Dispatches to the common
* function implementing this.
*/
{
}
/**
* File Handle EFI protocol, SetInfo function. Returns unsupported status
* because this driver is read-only.
*/
{
// this driver is read-only
return EFI_WRITE_PROTECTED;
}
/**
* File Handle EFI protocol, Flush function. Returns unsupported status
* because this driver is read-only.
*/
{
// this driver is read-only
return EFI_WRITE_PROTECTED;
}
/**
* Set up a file handle for a dnode. This function allocates a data structure
* for a file handle, opens a FSW shandle and populates the EFI_FILE structure
* with the interface functions.
*/
{
// make sure the dnode has complete info
return Status;
// check type
return EFI_UNSUPPORTED;
// allocate file structure
// open shandle
return Status;
}
// populate the file handle
return EFI_SUCCESS;
}
/**
* Data read function for regular files. Calls through to fsw_shandle_read.
*/
{
#if DEBUG_LEVEL
#endif
if (buffer_size != *BufferSize)
buffer_size = ~(fsw_u32)0;
return Status;
}
/**
* Get file position for regular files.
*/
{
return EFI_SUCCESS;
}
/**
* Set file position for regular files. EFI specifies the all-ones value
* to be a special value for the end of the file.
*/
{
if (Position == 0xFFFFFFFFFFFFFFFFULL)
else
return EFI_SUCCESS;
}
/**
* Open function used to open new file handles relative to a directory.
* In EFI, the "open file" function is implemented by directory file handles
* and is passed a relative or volume-absolute path to the file or directory
* to open. We use fsw_dnode_lookup_path to find the node plus an additional
* call to fsw_dnode_resolve because EFI has no concept of symbolic links.
*/
{
struct fsw_dnode *target_dno;
struct fsw_string lookup_path;
#if DEBUG_LEVEL
#endif
if (OpenMode != EFI_FILE_MODE_READ)
return EFI_WRITE_PROTECTED;
// resolve the path (symlinks along the way are automatically resolved)
Volume);
return Status;
// if the final node is a symlink, also resolve it
Volume);
return Status;
dno = target_dno;
// make a new EFI handle for the target dnode
return Status;
}
/**
* Read function for directories. A file handle read on a directory retrieves
* the next directory entry.
*/
{
#if DEBUG_LEVEL
Print(L"fsw_efi_dir_read...\n");
#endif
// read the next entry
Volume);
if (Status == EFI_NOT_FOUND) {
// end of directory
*BufferSize = 0;
#if DEBUG_LEVEL
Print(L"...no more entries\n");
#endif
return EFI_SUCCESS;
}
return Status;
// get info into buffer
return Status;
}
/**
* Set file position for directories. The only allowed set position operation
* for directories is to rewind the directory completely by setting the
* position to zero.
*/
{
if (Position == 0) {
return EFI_SUCCESS;
} else {
// directories can only rewind to the start
return EFI_UNSUPPORTED;
}
}
/**
* Get file or volume information. This function implements the GetInfo call
* for all file handles. Control is dispatched according to the type of information
* requested by the caller.
*/
{
struct fsw_volume_stat vsb;
#if DEBUG_LEVEL
Print(L"fsw_efi_dnode_getinfo: FILE_INFO\n");
#endif
#if DEBUG_LEVEL
Print(L"fsw_efi_dnode_getinfo: FILE_SYSTEM_INFO\n");
#endif
// check buffer size
if (*BufferSize < RequiredSize) {
return EFI_BUFFER_TOO_SMALL;
}
// fill structure
// get the missing info from the fs driver
return Status;
// prepare for return
#if DEBUG_LEVEL
Print(L"fsw_efi_dnode_getinfo: FILE_SYSTEM_VOLUME_LABEL\n");
#endif
// check buffer size
if (*BufferSize < RequiredSize) {
return EFI_BUFFER_TOO_SMALL;
}
// copy volume label
// prepare for return
} else {
}
return Status;
}
/**
* Time mapping callback for the fsw_dnode_stat call. This function converts
* a Posix style timestamp into an EFI_TIME structure and writes it to the
* appropriate member of the EFI_FILE_INFO structure that we're filling.
*/
{
if (which == FSW_DNODE_STAT_CTIME)
else if (which == FSW_DNODE_STAT_MTIME)
else if (which == FSW_DNODE_STAT_ATIME)
}
/**
* Mode mapping callback for the fsw_dnode_stat call. This function looks at
* the Posix mode passed by the file system driver and makes appropriate
* adjustments to the EFI_FILE_INFO structure that we're filling.
*/
{
if ((posix_mode & S_IWUSR) == 0)
}
/**
* Common function to fill an EFI_FILE_INFO with information about a dnode.
*/
{
struct fsw_dnode_stat sb;
// make sure the dnode has complete info
return Status;
// check buffer size
if (*BufferSize < RequiredSize) {
// TODO: wind back the directory in this case
#if DEBUG_LEVEL
Print(L"...BUFFER TOO SMALL\n");
#endif
return EFI_BUFFER_TOO_SMALL;
}
// fill structure
// get the missing info from the fs driver
return Status;
// prepare for return
#if DEBUG_LEVEL
#endif
return EFI_SUCCESS;
}
// EOF