ArmPkg/SemihostFs: Implement SetInfo() and handle seeking past the end of a file

Implement the resizing of the file with SetInfo().
Implement the renaming of a file with SetInfo().
Allow to seek past the end of a file.

The support of file resizing implies a rework of the read, write
and close functions. So does the support of seeking past the end
of a file. That why those two changes are done in the same patch.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
Reviewed-By: Olivier Martin <olivier.martin@arm.com>



git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16244 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Ronald Cron 2014-10-27 10:42:51 +00:00 committed by oliviermartin
parent 11bbc25789
commit daefd574f9
2 changed files with 802 additions and 125 deletions

View File

@ -297,42 +297,166 @@ Error:
return Status; return Status;
} }
/**
Worker function that truncate a file specified by its name to a given size.
@param[in] FileName The Null-terminated string of the name of the file to be opened.
@param[in] Size The target size for the file.
@retval EFI_SUCCESS The file was truncated.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
**/
STATIC
EFI_STATUS EFI_STATUS
FileClose ( TruncateFile (
IN EFI_FILE *File IN CHAR8 *FileName,
IN UINTN Size
) )
{ {
SEMIHOST_FCB *Fcb = NULL; EFI_STATUS Status;
EFI_STATUS Status = EFI_SUCCESS; RETURN_STATUS Return;
UINTN FileHandle;
UINT8 *Buffer;
UINTN Remaining;
UINTN Read;
UINTN ToRead;
Fcb = SEMIHOST_FCB_FROM_THIS(File); Status = EFI_DEVICE_ERROR;
FileHandle = 0;
Buffer = NULL;
if (Fcb->IsRoot == TRUE) { Return = SemihostFileOpen (
FreeFCB (Fcb); FileName,
Status = EFI_SUCCESS; SEMIHOST_FILE_MODE_READ | SEMIHOST_FILE_MODE_BINARY,
} else { &FileHandle
Status = SemihostFileClose (Fcb->SemihostHandle); );
if (!EFI_ERROR(Status)) { if (RETURN_ERROR (Return)) {
FreePool (Fcb->FileName); goto Error;
FreeFCB (Fcb); }
Buffer = AllocatePool (Size);
if (Buffer == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Error;
}
Read = 0;
Remaining = Size;
while (Remaining > 0) {
ToRead = Remaining;
Return = SemihostFileRead (FileHandle, &ToRead, Buffer + Read);
if (RETURN_ERROR (Return)) {
goto Error;
}
Remaining -= ToRead;
Read += ToRead;
}
Return = SemihostFileClose (FileHandle);
FileHandle = 0;
if (RETURN_ERROR (Return)) {
goto Error;
}
Return = SemihostFileOpen (
FileName,
SEMIHOST_FILE_MODE_WRITE | SEMIHOST_FILE_MODE_BINARY,
&FileHandle
);
if (RETURN_ERROR (Return)) {
goto Error;
}
if (Size > 0) {
Return = SemihostFileWrite (FileHandle, &Size, Buffer);
if (RETURN_ERROR (Return)) {
goto Error;
} }
} }
return Status; Status = EFI_SUCCESS;
Error:
if (FileHandle != 0) {
SemihostFileClose (FileHandle);
}
if (Buffer != NULL) {
FreePool (Buffer);
}
return (Status);
} }
/**
Close a specified file handle.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file
handle to close.
@retval EFI_SUCCESS The file was closed.
@retval EFI_INVALID_PARAMETER The parameter "This" is NULL.
**/
EFI_STATUS EFI_STATUS
FileDelete ( FileClose (
IN EFI_FILE *File IN EFI_FILE *This
) )
{ {
SEMIHOST_FCB *Fcb = NULL; SEMIHOST_FCB *Fcb;
EFI_STATUS Status;
CHAR8 *FileName;
UINTN NameSize;
Fcb = SEMIHOST_FCB_FROM_THIS(File); if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
Fcb = SEMIHOST_FCB_FROM_THIS(This);
if (!Fcb->IsRoot) {
SemihostFileClose (Fcb->SemihostHandle);
//
// The file size might have been reduced from its actual
// size on the host file system with FileSetInfo(). In
// that case, the file has to be truncated.
//
if (Fcb->Info.FileSize < Fcb->Info.PhysicalSize) {
TruncateFile (Fcb->FileName, Fcb->Info.FileSize);
}
FreePool (Fcb->FileName);
}
FreeFCB (Fcb);
return EFI_SUCCESS;
}
/**
Close and delete a file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file
handle to delete.
@retval EFI_SUCCESS The file was closed and deleted.
@retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not deleted.
@retval EFI_INVALID_PARAMETER The parameter "This" is NULL.
**/
EFI_STATUS
FileDelete (
IN EFI_FILE *This
)
{
SEMIHOST_FCB *Fcb;
RETURN_STATUS Return;
CHAR8 *FileName;
UINTN NameSize;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
Fcb = SEMIHOST_FCB_FROM_THIS (This);
if (!Fcb->IsRoot) { if (!Fcb->IsRoot) {
// Get the filename from the Fcb // Get the filename from the Fcb
@ -343,57 +467,157 @@ FileDelete (
// Close the file if it's open. Disregard return status, // Close the file if it's open. Disregard return status,
// since it might give an error if the file isn't open. // since it might give an error if the file isn't open.
File->Close (File); This->Close (This);
// Call the semihost interface to delete the file. // Call the semihost interface to delete the file.
Status = SemihostFileRemove (FileName); Return = SemihostFileRemove (FileName);
if (EFI_ERROR(Status)) { if (RETURN_ERROR (Return)) {
Status = EFI_WARN_DELETE_FAILURE; return EFI_WARN_DELETE_FAILURE;
} }
return EFI_SUCCESS;
} else { } else {
Status = EFI_WARN_DELETE_FAILURE; return EFI_WARN_DELETE_FAILURE;
} }
return Status;
} }
/**
Read data from an open file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle to read data from.
@param[in out] BufferSize On input, the size of the Buffer. On output, the
amount of data returned in Buffer. In both cases,
the size is measured in bytes.
@param[out] Buffer The buffer into which the data is read.
@retval EFI_SUCCESS The data was read.
@retval EFI_DEVICE_ERROR On entry, the current file position is
beyond the end of the file, or the semi-hosting
interface reported an error while performing the
read operation.
@retval EFI_INVALID_PARAMETER At least one of the three input pointers is NULL.
**/
EFI_STATUS EFI_STATUS
FileRead ( FileRead (
IN EFI_FILE *File, IN EFI_FILE *This,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
) )
{ {
SEMIHOST_FCB *Fcb = NULL; SEMIHOST_FCB *Fcb;
EFI_STATUS Status; EFI_STATUS Status;
RETURN_STATUS Return;
Fcb = SEMIHOST_FCB_FROM_THIS(File); if ((This == NULL) || (BufferSize == NULL) || (Buffer == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (Fcb->IsRoot == TRUE) { Fcb = SEMIHOST_FCB_FROM_THIS (This);
// By design, the Semihosting feature does not allow to list files on the host machine.
if (Fcb->IsRoot) {
// The semi-hosting interface does not allow to list files on the host machine.
Status = EFI_UNSUPPORTED; Status = EFI_UNSUPPORTED;
} else { } else {
Status = SemihostFileRead (Fcb->SemihostHandle, BufferSize, Buffer); Status = EFI_SUCCESS;
if (!EFI_ERROR (Status)) { if (Fcb->Position >= Fcb->Info.FileSize) {
Fcb->Position += *BufferSize; *BufferSize = 0;
if (Fcb->Position > Fcb->Info.FileSize) {
Status = EFI_DEVICE_ERROR;
}
} else {
Return = SemihostFileRead (Fcb->SemihostHandle, BufferSize, Buffer);
if (RETURN_ERROR (Return)) {
Status = EFI_DEVICE_ERROR;
} else {
Fcb->Position += *BufferSize;
}
} }
} }
return Status; return Status;
} }
/**
Worker function that extends the size of an open file.
The extension is filled with zeros.
@param[in] Fcb Internal description of the opened file
@param[in] Size The number of bytes, the file has to be extended.
@retval EFI_SUCCESS The file was extended.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
**/
STATIC
EFI_STATUS
ExtendFile (
IN SEMIHOST_FCB *Fcb,
IN UINTN Size
)
{
RETURN_STATUS Return;
UINTN Remaining;
CHAR8 WriteBuffer[128];
UINTN WriteNb;
UINTN WriteSize;
Return = SemihostFileSeek (Fcb->SemihostHandle, Fcb->Info.FileSize);
if (RETURN_ERROR (Return)) {
return EFI_DEVICE_ERROR;
}
Remaining = Size;
SetMem (WriteBuffer, 0, sizeof(WriteBuffer));
while (Remaining > 0) {
WriteNb = MIN (Remaining, sizeof(WriteBuffer));
WriteSize = WriteNb;
Return = SemihostFileWrite (Fcb->SemihostHandle, &WriteSize, WriteBuffer);
if (RETURN_ERROR (Return)) {
return EFI_DEVICE_ERROR;
}
Remaining -= WriteNb;
}
return EFI_SUCCESS;
}
/**
Write data to an open file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle to write data to.
@param[in out] BufferSize On input, the size of the Buffer. On output, the
size of the data actually written. In both cases,
the size is measured in bytes.
@param[in] Buffer The buffer of data to write.
@retval EFI_SUCCESS The data was written.
@retval EFI_ACCESS_DENIED Attempt to write into a read only file or
in a file opened in read only mode.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
@retval EFI_INVALID_PARAMETER At least one of the three input pointers is NULL.
**/
EFI_STATUS EFI_STATUS
FileWrite ( FileWrite (
IN EFI_FILE *File, IN EFI_FILE *This,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
IN VOID *Buffer IN VOID *Buffer
) )
{ {
SEMIHOST_FCB *Fcb = NULL; SEMIHOST_FCB *Fcb;
EFI_STATUS Status; EFI_STATUS Status;
UINTN WriteSize = *BufferSize; UINTN WriteSize;
RETURN_STATUS Return;
UINTN Length;
Fcb = SEMIHOST_FCB_FROM_THIS(File); if ((This == NULL) || (BufferSize == NULL) || (Buffer == NULL)) {
return EFI_INVALID_PARAMETER;
}
Fcb = SEMIHOST_FCB_FROM_THIS (This);
// We cannot write a read-only file // We cannot write a read-only file
if ((Fcb->Info.Attribute & EFI_FILE_READ_ONLY) if ((Fcb->Info.Attribute & EFI_FILE_READ_ONLY)
@ -401,72 +625,142 @@ FileWrite (
return EFI_ACCESS_DENIED; return EFI_ACCESS_DENIED;
} }
Status = SemihostFileWrite (Fcb->SemihostHandle, &WriteSize, Buffer); //
// If the position has been set past the end of the file, first grow the
if (!EFI_ERROR(Status)) { // file from its current size "Fcb->Info.FileSize" to "Fcb->Position"
// Semihost write return the number of bytes *NOT* written. // size, filling the gap with zeros.
*BufferSize -= WriteSize; //
Fcb->Position += *BufferSize; if (Fcb->Position > Fcb->Info.FileSize) {
Status = ExtendFile (Fcb, Fcb->Position - Fcb->Info.FileSize);
if (EFI_ERROR (Status)) {
return Status;
}
Fcb->Info.FileSize = Fcb->Position;
} }
return Status; WriteSize = *BufferSize;
Return = SemihostFileWrite (Fcb->SemihostHandle, &WriteSize, Buffer);
if (RETURN_ERROR (Return)) {
return EFI_DEVICE_ERROR;
}
Fcb->Position += *BufferSize;
if (Fcb->Position > Fcb->Info.FileSize) {
Fcb->Info.FileSize = Fcb->Position;
}
Return = SemihostFileLength (Fcb->SemihostHandle, &Length);
if (RETURN_ERROR (Return)) {
return EFI_DEVICE_ERROR;
}
Fcb->Info.PhysicalSize = Length;
return EFI_SUCCESS;
} }
/**
Return a file's current position.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
the file handle to get the current position on.
@param[out] Position The address to return the file's current position value.
@retval EFI_SUCCESS The position was returned.
@retval EFI_INVALID_PARAMETER The parameter "This" or "Position" is NULL.
**/
EFI_STATUS EFI_STATUS
FileGetPosition ( FileGetPosition (
IN EFI_FILE *File, IN EFI_FILE *This,
OUT UINT64 *Position OUT UINT64 *Position
) )
{ {
SEMIHOST_FCB *Fcb = NULL; SEMIHOST_FCB *Fcb;
if (Position == NULL) { if ((This == NULL) || (Position == NULL)) {
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
Fcb = SEMIHOST_FCB_FROM_THIS(File); Fcb = SEMIHOST_FCB_FROM_THIS(This);
*Position = Fcb->Position; *Position = Fcb->Position;
return EFI_SUCCESS; return EFI_SUCCESS;
} }
/**
Set a file's current position.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
the file handle to set the requested position on.
@param[in] Position The byte position from the start of the file to set.
@retval EFI_SUCCESS The position was set.
@retval EFI_DEVICE_ERROR The semi-hosting positionning operation failed.
@retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open
directories.
@retval EFI_INVALID_PARAMETER The parameter "This" is NULL.
**/
EFI_STATUS EFI_STATUS
FileSetPosition ( FileSetPosition (
IN EFI_FILE *File, IN EFI_FILE *This,
IN UINT64 Position IN UINT64 Position
) )
{ {
SEMIHOST_FCB *Fcb = NULL; SEMIHOST_FCB *Fcb;
UINTN Length; RETURN_STATUS Return;
EFI_STATUS Status;
Fcb = SEMIHOST_FCB_FROM_THIS(File); if (This == NULL) {
return EFI_INVALID_PARAMETER;
if (!Fcb->IsRoot) {
Status = SemihostFileLength (Fcb->SemihostHandle, &Length);
if (!EFI_ERROR(Status) && (Length < Position)) {
Position = Length;
}
Status = SemihostFileSeek (Fcb->SemihostHandle, (UINT32)Position);
if (!EFI_ERROR(Status)) {
Fcb->Position = Position;
}
} else {
Fcb->Position = Position;
Status = EFI_SUCCESS;
} }
return Status; Fcb = SEMIHOST_FCB_FROM_THIS (This);
if (Fcb->IsRoot) {
if (Position != 0) {
return EFI_UNSUPPORTED;
}
}
else {
//
// UEFI Spec section 12.5:
// "Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to
// be set to the end of the file."
//
if (Position == 0xFFFFFFFFFFFFFFFF) {
Position = Fcb->Info.FileSize;
}
Return = SemihostFileSeek (Fcb->SemihostHandle, MIN (Position, Fcb->Info.FileSize));
if (RETURN_ERROR (Return)) {
return EFI_DEVICE_ERROR;
}
}
Fcb->Position = Position;
return EFI_SUCCESS;
} }
/**
Return information about a file.
@param[in] Fcb A pointer to the description of an open file.
@param[in out] BufferSize The size, in bytes, of Buffer.
@param[out] Buffer A pointer to the data buffer to return. Not NULL if
"*BufferSize" is greater than 0.
@retval EFI_SUCCESS The information was returned.
@retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to return the information.
BufferSize has been updated with the size needed to
complete the request.
**/
STATIC STATIC
EFI_STATUS EFI_STATUS
GetFileInfo ( GetFileInfo (
IN SEMIHOST_FCB *Fcb, IN SEMIHOST_FCB *Fcb,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
) )
{ {
EFI_FILE_INFO *Info = NULL; EFI_FILE_INFO *Info = NULL;
@ -507,6 +801,22 @@ GetFileInfo (
return EFI_SUCCESS; return EFI_SUCCESS;
} }
/**
Return information about a file system.
@param[in] Fcb A pointer to the description of an open file
which belongs to the file system, the information
is requested for.
@param[in out] BufferSize The size, in bytes, of Buffer.
@param[out] Buffer A pointer to the data buffer to return. Not NULL if
"*BufferSize" is greater than 0.
@retval EFI_SUCCESS The information was returned.
@retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to return the information.
BufferSize has been updated with the size needed to
complete the request.
**/
STATIC STATIC
EFI_STATUS EFI_STATUS
GetFilesystemInfo ( GetFilesystemInfo (
@ -515,9 +825,11 @@ GetFilesystemInfo (
OUT VOID *Buffer OUT VOID *Buffer
) )
{ {
EFI_FILE_SYSTEM_INFO *Info = NULL; EFI_FILE_SYSTEM_INFO *Info;
EFI_STATUS Status; EFI_STATUS Status;
UINTN ResultSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (mSemihostFsLabel); UINTN ResultSize;
ResultSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (mSemihostFsLabel);
if (*BufferSize >= ResultSize) { if (*BufferSize >= ResultSize) {
ZeroMem (Buffer, ResultSize); ZeroMem (Buffer, ResultSize);
@ -540,25 +852,54 @@ GetFilesystemInfo (
return Status; return Status;
} }
/**
Return information about a file or a file system.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle the requested information is for.
@param[in] InformationType The type identifier for the information being requested :
EFI_FILE_INFO_ID or EFI_FILE_SYSTEM_INFO_ID or
EFI_FILE_SYSTEM_VOLUME_LABEL_ID
@param[in out] BufferSize The size, in bytes, of Buffer.
@param[out] Buffer A pointer to the data buffer to return. The type of the
data inside the buffer is indicated by InformationType.
@retval EFI_SUCCESS The information was returned.
@retval EFI_UNSUPPORTED The InformationType is not known.
@retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to return the information.
BufferSize has been updated with the size needed to
complete the request.
@retval EFI_INVALID_PARAMETER The parameter "This" or "InformationType" or "BufferSize"
is NULL or "Buffer" is NULL and "*Buffersize" is greater
than 0.
**/
EFI_STATUS EFI_STATUS
FileGetInfo ( FileGetInfo (
IN EFI_FILE *File, IN EFI_FILE *This,
IN EFI_GUID *InformationType, IN EFI_GUID *InformationType,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
) )
{ {
SEMIHOST_FCB *Fcb; SEMIHOST_FCB *Fcb;
EFI_STATUS Status; EFI_STATUS Status;
UINTN ResultSize; UINTN ResultSize;
Fcb = SEMIHOST_FCB_FROM_THIS(File); if ((This == NULL) ||
(InformationType == NULL) ||
(BufferSize == NULL) ||
((Buffer == NULL) && (*BufferSize > 0)) ) {
return EFI_INVALID_PARAMETER;
}
if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid) != 0) { Fcb = SEMIHOST_FCB_FROM_THIS(This);
if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {
Status = GetFilesystemInfo (Fcb, BufferSize, Buffer); Status = GetFilesystemInfo (Fcb, BufferSize, Buffer);
} else if (CompareGuid (InformationType, &gEfiFileInfoGuid) != 0) { } else if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {
Status = GetFileInfo (Fcb, BufferSize, Buffer); Status = GetFileInfo (Fcb, BufferSize, Buffer);
} else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid) != 0) { } else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
ResultSize = StrSize (mSemihostFsLabel); ResultSize = StrSize (mSemihostFsLabel);
if (*BufferSize >= ResultSize) { if (*BufferSize >= ResultSize) {
@ -576,39 +917,241 @@ FileGetInfo (
return Status; return Status;
} }
/**
Set information about a file.
@param[in] Fcb A pointer to the description of the open file.
@param[in] Info A pointer to the file information to write.
@retval EFI_SUCCESS The information was set.
@retval EFI_ACCESS_DENIED An attempt is made to change the name of a file
to a file that is already present.
@retval EFI_ACCESS_DENIED An attempt is being made to change the
EFI_FILE_DIRECTORY Attribute.
@retval EFI_ACCESS_DENIED The file is a read-only file or has been
opened in read-only mode and an attempt is
being made to modify a field other than
Attribute.
@retval EFI_WRITE_PROTECTED An attempt is being made to modify a
read-only attribute.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
@retval EFI_OUT_OF_RESOURCES A allocation needed to process the request failed.
**/
STATIC
EFI_STATUS EFI_STATUS
FileSetInfo ( SetFileInfo (
IN EFI_FILE *File, IN SEMIHOST_FCB *Fcb,
IN EFI_GUID *InformationType, IN EFI_FILE_INFO *Info
IN UINTN BufferSize,
IN VOID *Buffer
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
RETURN_STATUS Return;
BOOLEAN FileSizeIsDifferent;
BOOLEAN FileNameIsDifferent;
BOOLEAN ReadOnlyIsDifferent;
CHAR8 *AsciiFileName;
UINTN FileSize;
UINTN Length;
UINTN SemihostHandle;
if (Buffer == NULL) { //
return EFI_INVALID_PARAMETER; // A directory can not be changed to a file and a file can
// not be changed to a directory.
//
if (((Info->Attribute & EFI_FILE_DIRECTORY) != 0) != Fcb->IsRoot) {
return EFI_ACCESS_DENIED;
} }
Status = EFI_UNSUPPORTED; AsciiFileName = AllocatePool (StrLen (Info->FileName) + 1);
if (AsciiFileName == NULL) {
return EFI_OUT_OF_RESOURCES;
}
UnicodeStrToAsciiStr (Info->FileName, AsciiFileName);
if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid) != 0) { FileSizeIsDifferent = (Info->FileSize != Fcb->Info.FileSize);
//Status = SetFilesystemInfo (Fcb, BufferSize, Buffer); FileNameIsDifferent = (AsciiStrCmp (AsciiFileName, Fcb->FileName) != 0);
} else if (CompareGuid (InformationType, &gEfiFileInfoGuid) != 0) { ReadOnlyIsDifferent = CompareMem (
// Semihosting does not give us access to setting file info, but &Info->CreateTime,
// if we fail here we cannot create new files. &Fcb->Info.CreateTime,
Status = EFI_SUCCESS; 3 * sizeof (EFI_TIME)
} else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid) != 0) { ) != 0;
if (StrSize (Buffer) > 0) {
FreePool (mSemihostFsLabel); //
mSemihostFsLabel = AllocateCopyPool (StrSize (Buffer), Buffer); // For a read-only file or a file opened in read-only mode, only
Status = EFI_SUCCESS; // the Attribute field can be modified. As the root directory is
// read-only (i.e. VolumeOpen()), this protects the root directory
// description.
//
if ((Fcb->OpenMode == EFI_FILE_MODE_READ) ||
(Fcb->Info.Attribute & EFI_FILE_READ_ONLY) ) {
if (FileSizeIsDifferent || FileNameIsDifferent || ReadOnlyIsDifferent) {
Status = EFI_ACCESS_DENIED;
goto Error;
} }
} }
if (ReadOnlyIsDifferent) {
Status = EFI_WRITE_PROTECTED;
goto Error;
}
Status = EFI_DEVICE_ERROR;
if (FileSizeIsDifferent) {
FileSize = Info->FileSize;
if (Fcb->Info.FileSize < FileSize) {
Status = ExtendFile (Fcb, FileSize - Fcb->Info.FileSize);
if (EFI_ERROR (Status)) {
goto Error;
}
//
// The read/write position from the host file system point of view
// is at the end of the file. If the position from this module
// point of view is smaller than the new file size, then
// ask the host file system to move to that position.
//
if (Fcb->Position < FileSize) {
FileSetPosition (&Fcb->File, Fcb->Position);
}
}
Fcb->Info.FileSize = FileSize;
Return = SemihostFileLength (Fcb->SemihostHandle, &Length);
if (RETURN_ERROR (Return)) {
goto Error;
}
Fcb->Info.PhysicalSize = Length;
}
//
// Note down in RAM the Attribute field but we can not ask
// for its modification to the host file system as the
// semi-host interface does not provide this feature.
//
Fcb->Info.Attribute = Info->Attribute;
if (FileNameIsDifferent) {
Return = SemihostFileOpen (
AsciiFileName,
SEMIHOST_FILE_MODE_READ | SEMIHOST_FILE_MODE_BINARY,
&SemihostHandle
);
if (!RETURN_ERROR (Return)) {
SemihostFileClose (SemihostHandle);
Status = EFI_ACCESS_DENIED;
goto Error;
}
Return = SemihostFileRename (Fcb->FileName, AsciiFileName);
if (RETURN_ERROR (Return)) {
goto Error;
}
FreePool (Fcb->FileName);
Fcb->FileName = AsciiFileName;
AsciiFileName = NULL;
}
Status = EFI_SUCCESS;
Error:
if (AsciiFileName != NULL) {
FreePool (AsciiFileName);
}
return Status; return Status;
} }
/**
Set information about a file or a file system.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle the information is for.
@param[in] InformationType The type identifier for the information being set :
EFI_FILE_INFO_ID or EFI_FILE_SYSTEM_INFO_ID or
EFI_FILE_SYSTEM_VOLUME_LABEL_ID
@param[in] BufferSize The size, in bytes, of Buffer.
@param[in] Buffer A pointer to the data buffer to write. The type of the
data inside the buffer is indicated by InformationType.
@retval EFI_SUCCESS The information was set.
@retval EFI_UNSUPPORTED The InformationType is not known.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
@retval EFI_ACCESS_DENIED An attempt is being made to change the
EFI_FILE_DIRECTORY Attribute.
@retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and
the file is a read-only file or has been
opened in read-only mode and an attempt is
being made to modify a field other than
Attribute.
@retval EFI_ACCESS_DENIED An attempt is made to change the name of a file
to a file that is already present.
@retval EFI_WRITE_PROTECTED An attempt is being made to modify a
read-only attribute.
@retval EFI_BAD_BUFFER_SIZE The size of the buffer is lower than that indicated by
the data inside the buffer.
@retval EFI_OUT_OF_RESOURCES An allocation needed to process the request failed.
@retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
**/
EFI_STATUS
FileSetInfo (
IN EFI_FILE *This,
IN EFI_GUID *InformationType,
IN UINTN BufferSize,
IN VOID *Buffer
)
{
SEMIHOST_FCB *Fcb;
EFI_FILE_INFO *Info;
EFI_FILE_SYSTEM_INFO *SystemInfo;
CHAR16 *VolumeLabel;
if ((This == NULL) || (InformationType == NULL) || (Buffer == NULL)) {
return EFI_INVALID_PARAMETER;
}
Fcb = SEMIHOST_FCB_FROM_THIS (This);
if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {
Info = Buffer;
if (Info->Size < (SIZE_OF_EFI_FILE_INFO + StrSize (Info->FileName))) {
return EFI_INVALID_PARAMETER;
}
if (BufferSize < Info->Size) {
return EFI_BAD_BUFFER_SIZE;
}
return SetFileInfo (Fcb, Info);
} else if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {
SystemInfo = Buffer;
if (SystemInfo->Size <
(SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (SystemInfo->VolumeLabel))) {
return EFI_INVALID_PARAMETER;
}
if (BufferSize < SystemInfo->Size) {
return EFI_BAD_BUFFER_SIZE;
}
Buffer = SystemInfo->VolumeLabel;
if (StrSize (Buffer) > 0) {
VolumeLabel = AllocateCopyPool (StrSize (Buffer), Buffer);
if (VolumeLabel != NULL) {
FreePool (mSemihostFsLabel);
mSemihostFsLabel = VolumeLabel;
return EFI_SUCCESS;
} else {
return EFI_OUT_OF_RESOURCES;
}
} else {
return EFI_INVALID_PARAMETER;
}
} else if (!CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
return EFI_UNSUPPORTED;
} else {
return EFI_UNSUPPORTED;
}
}
EFI_STATUS EFI_STATUS
FileFlush ( FileFlush (
IN EFI_FILE *File IN EFI_FILE *File

View File

@ -57,56 +57,190 @@ FileOpen (
IN UINT64 Attributes IN UINT64 Attributes
); );
/**
Close a specified file handle.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file
handle to close.
@retval EFI_SUCCESS The file was closed.
@retval EFI_INVALID_PARAMETER The parameter "This" is NULL.
**/
EFI_STATUS EFI_STATUS
FileClose ( FileClose (
IN EFI_FILE *File IN EFI_FILE *This
); );
/**
Close and delete a file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file
handle to delete.
@retval EFI_SUCCESS The file was closed and deleted.
@retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not deleted.
@retval EFI_INVALID_PARAMETER The parameter "This" is NULL.
**/
EFI_STATUS EFI_STATUS
FileDelete( FileDelete (
IN EFI_FILE *File IN EFI_FILE *This
); );
/**
Read data from an open file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle to read data from.
@param[in out] BufferSize On input, the size of the Buffer. On output, the
amount of data returned in Buffer. In both cases,
the size is measured in bytes.
@param[out] Buffer The buffer into which the data is read.
@retval EFI_SUCCESS The data was read.
@retval EFI_DEVICE_ERROR On entry, the current file position is
beyond the end of the file, or the semi-hosting
interface reported an error while performing the
read operation.
@retval EFI_INVALID_PARAMETER The parameter "This" or the parameter "Buffer"
is NULL.
**/
EFI_STATUS EFI_STATUS
FileRead ( FileRead (
IN EFI_FILE *File, IN EFI_FILE *This,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
); );
/**
Write data to an open file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle to write data to.
@param[in out] BufferSize On input, the size of the Buffer. On output, the
size of the data actually written. In both cases,
the size is measured in bytes.
@param[in] Buffer The buffer of data to write.
@retval EFI_SUCCESS The data was written.
@retval EFI_ACCESS_DENIED Attempt to write into a read only file or
in a file opened in read only mode.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
@retval EFI_INVALID_PARAMETER The parameter "This" or the parameter "Buffer"
is NULL.
**/
EFI_STATUS EFI_STATUS
FileWrite ( FileWrite (
IN EFI_FILE *File, IN EFI_FILE *This,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
IN VOID *Buffer IN VOID *Buffer
); );
/**
Return a file's current position.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
the file handle to get the current position on.
@param[out] Position The address to return the file's current position value.
@retval EFI_SUCCESS The position was returned.
@retval EFI_INVALID_PARAMETER Position is a NULL pointer.
**/
EFI_STATUS EFI_STATUS
FileGetPosition ( FileGetPosition (
IN EFI_FILE *File, IN EFI_FILE *File,
OUT UINT64 *Position OUT UINT64 *Position
); );
/**
Set a file's current position.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
the file handle to set the requested position on.
@param[in] Position The byte position from the start of the file to set.
@retval EFI_SUCCESS The position was set.
@retval EFI_DEVICE_ERROR The semi-hosting positionning operation failed.
@retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open
directories.
**/
EFI_STATUS EFI_STATUS
FileSetPosition ( FileSetPosition (
IN EFI_FILE *File, IN EFI_FILE *File,
IN UINT64 Position IN UINT64 Position
); );
/**
Return information about a file or a file system.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle the requested information is for.
@param[in] InformationType The type identifier for the information being requested :
EFI_FILE_INFO_ID or EFI_FILE_SYSTEM_INFO_ID or
EFI_FILE_SYSTEM_VOLUME_LABEL_ID
@param[in out] BufferSize The size, in bytes, of Buffer.
@param[out] Buffer A pointer to the data buffer to return. The type of the
data inside the buffer is indicated by InformationType.
@retval EFI_SUCCESS The information was returned.
@retval EFI_UNSUPPORTED The InformationType is not known.
@retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to return the information.
BufferSize has been updated with the size needed to
complete the request.
@retval EFI_INVALID_PARAMETER The parameter "This" or the parameter "Buffer"
is NULL.
**/
EFI_STATUS EFI_STATUS
FileGetInfo ( FileGetInfo (
IN EFI_FILE *File, IN EFI_FILE *This,
IN EFI_GUID *InformationType, IN EFI_GUID *InformationType,
IN OUT UINTN *BufferSize, IN OUT UINTN *BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
); );
/**
Set information about a file or a file system.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle the information is for.
@param[in] InformationType The type identifier for the information being set :
EFI_FILE_INFO_ID or EFI_FILE_SYSTEM_INFO_ID or
EFI_FILE_SYSTEM_VOLUME_LABEL_ID
@param[in] BufferSize The size, in bytes, of Buffer.
@param[in] Buffer A pointer to the data buffer to write. The type of the
data inside the buffer is indicated by InformationType.
@retval EFI_SUCCESS The information was set.
@retval EFI_UNSUPPORTED The InformationType is not known.
@retval EFI_DEVICE_ERROR The last issued semi-hosting operation failed.
@retval EFI_ACCESS_DENIED An attempt is being made to change the
EFI_FILE_DIRECTORY Attribute.
@retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and
the file is a read-only file or has been
opened in read-only mode and an attempt is
being made to modify a field other than
Attribute.
@retval EFI_ACCESS_DENIED An attempt is made to change the name of a file
to a file that is already present.
@retval EFI_WRITE_PROTECTED An attempt is being made to modify a
read-only attribute.
@retval EFI_BAD_BUFFER_SIZE The size of the buffer is lower than that indicated by
the data inside the buffer.
@retval EFI_OUT_OF_RESOURCES An allocation needed to process the request failed.
@retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
**/
EFI_STATUS EFI_STATUS
FileSetInfo ( FileSetInfo (
IN EFI_FILE *File, IN EFI_FILE *This,
IN EFI_GUID *InformationType, IN EFI_GUID *InformationType,
IN UINTN BufferSize, IN UINTN BufferSize,
IN VOID *Buffer IN VOID *Buffer
); );
EFI_STATUS EFI_STATUS