2016-09-21 04:43:03 +02:00
|
|
|
/** @file
|
|
|
|
A shell application that triggers capsule update process.
|
|
|
|
|
2017-02-21 10:04:29 +01:00
|
|
|
Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
|
2016-09-21 04:43:03 +02:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <Uefi.h>
|
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
#include <Library/DebugLib.h>
|
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
|
|
#include <Library/MemoryAllocationLib.h>
|
|
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
|
|
#include <Protocol/SimpleFileSystem.h>
|
|
|
|
#include <Protocol/ShellParameters.h>
|
2017-04-11 03:55:51 +02:00
|
|
|
#include <Protocol/Shell.h>
|
2016-09-21 04:43:03 +02:00
|
|
|
#include <Guid/FileInfo.h>
|
|
|
|
|
|
|
|
UINTN Argc;
|
|
|
|
CHAR16 **Argv;
|
2017-04-11 03:55:51 +02:00
|
|
|
EFI_SHELL_PROTOCOL *mShellProtocol = NULL;
|
2016-09-21 04:43:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
This function parse application ARG.
|
|
|
|
|
|
|
|
@return Status
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
GetArg (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;
|
|
|
|
|
|
|
|
Status = gBS->HandleProtocol (
|
|
|
|
gImageHandle,
|
|
|
|
&gEfiShellParametersProtocolGuid,
|
|
|
|
(VOID**)&ShellParameters
|
|
|
|
);
|
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Argc = ShellParameters->Argc;
|
|
|
|
Argv = ShellParameters->Argv;
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-11 03:55:51 +02:00
|
|
|
Get shell protocol.
|
2016-09-21 04:43:03 +02:00
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
@return Pointer to shell protocol.
|
2016-09-21 04:43:03 +02:00
|
|
|
**/
|
2017-04-11 03:55:51 +02:00
|
|
|
EFI_SHELL_PROTOCOL *
|
|
|
|
GetShellProtocol (
|
2016-09-21 04:43:03 +02:00
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2017-04-11 03:55:51 +02:00
|
|
|
EFI_STATUS Status;
|
2016-09-21 04:43:03 +02:00
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
if (mShellProtocol == NULL) {
|
|
|
|
Status = gBS->LocateProtocol (
|
|
|
|
&gEfiShellProtocolGuid,
|
|
|
|
NULL,
|
|
|
|
(VOID **) &mShellProtocol
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
mShellProtocol = NULL;
|
|
|
|
}
|
2016-09-21 04:43:03 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
return mShellProtocol;
|
2016-09-21 04:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-11 03:55:51 +02:00
|
|
|
Read a file.
|
2016-09-21 04:43:03 +02:00
|
|
|
|
|
|
|
@param[in] FileName The file to be read.
|
|
|
|
@param[out] BufferSize The file buffer size
|
|
|
|
@param[out] Buffer The file buffer
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Read file successfully
|
2017-04-11 03:55:51 +02:00
|
|
|
@retval EFI_NOT_FOUND Shell protocol or file not found
|
|
|
|
@retval others Read file failed
|
2016-09-21 04:43:03 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
2017-04-11 03:55:51 +02:00
|
|
|
ReadFileToBuffer (
|
|
|
|
IN CHAR16 *FileName,
|
|
|
|
OUT UINTN *BufferSize,
|
|
|
|
OUT VOID **Buffer
|
2016-09-21 04:43:03 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2017-04-11 03:55:51 +02:00
|
|
|
EFI_SHELL_PROTOCOL *ShellProtocol;
|
|
|
|
SHELL_FILE_HANDLE Handle;
|
|
|
|
UINT64 FileSize;
|
2016-09-21 04:43:03 +02:00
|
|
|
UINTN TempBufferSize;
|
|
|
|
VOID *TempBuffer;
|
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol = GetShellProtocol();
|
|
|
|
if (ShellProtocol == NULL) {
|
|
|
|
return EFI_NOT_FOUND;
|
2016-09-21 04:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
// Open file by FileName.
|
2016-09-21 04:43:03 +02:00
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
Status = ShellProtocol->OpenFileByName (
|
|
|
|
FileName,
|
|
|
|
&Handle,
|
|
|
|
EFI_FILE_MODE_READ
|
|
|
|
);
|
2016-09-21 04:43:03 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
// Get the file size.
|
2016-09-21 04:43:03 +02:00
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
Status = ShellProtocol->GetFileSize (Handle, &FileSize);
|
2017-05-11 06:57:47 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol->CloseFile (Handle);
|
2016-09-21 04:43:03 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
TempBufferSize = (UINTN) FileSize;
|
2016-09-21 04:43:03 +02:00
|
|
|
TempBuffer = AllocateZeroPool (TempBufferSize);
|
|
|
|
if (TempBuffer == NULL) {
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol->CloseFile (Handle);
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2016-09-21 04:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Read the file data to the buffer
|
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
Status = ShellProtocol->ReadFile (
|
|
|
|
Handle,
|
|
|
|
&TempBufferSize,
|
|
|
|
TempBuffer
|
|
|
|
);
|
2016-09-21 04:43:03 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol->CloseFile (Handle);
|
2016-09-21 04:43:03 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol->CloseFile (Handle);
|
2016-09-21 04:43:03 +02:00
|
|
|
|
|
|
|
*BufferSize = TempBufferSize;
|
|
|
|
*Buffer = TempBuffer;
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write a file.
|
|
|
|
|
|
|
|
@param[in] FileName The file to be written.
|
|
|
|
@param[in] BufferSize The file buffer size
|
|
|
|
@param[in] Buffer The file buffer
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Write file successfully
|
2017-04-11 03:55:51 +02:00
|
|
|
@retval EFI_NOT_FOUND Shell protocol not found
|
|
|
|
@retval others Write file failed
|
2016-09-21 04:43:03 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
WriteFileFromBuffer (
|
|
|
|
IN CHAR16 *FileName,
|
|
|
|
IN UINTN BufferSize,
|
|
|
|
IN VOID *Buffer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2017-04-11 03:55:51 +02:00
|
|
|
EFI_SHELL_PROTOCOL *ShellProtocol;
|
|
|
|
SHELL_FILE_HANDLE Handle;
|
|
|
|
EFI_FILE_INFO *FileInfo;
|
2016-09-21 04:43:03 +02:00
|
|
|
UINTN TempBufferSize;
|
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol = GetShellProtocol();
|
|
|
|
if (ShellProtocol == NULL) {
|
2016-09-21 04:43:03 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
// Open file by FileName.
|
2016-09-21 04:43:03 +02:00
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
Status = ShellProtocol->OpenFileByName (
|
|
|
|
FileName,
|
|
|
|
&Handle,
|
|
|
|
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
|
|
|
|
);
|
2016-09-21 04:43:03 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
// Empty the file contents.
|
2016-09-21 04:43:03 +02:00
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
FileInfo = ShellProtocol->GetFileInfo (Handle);
|
|
|
|
if (FileInfo == NULL) {
|
|
|
|
ShellProtocol->CloseFile (Handle);
|
|
|
|
return EFI_DEVICE_ERROR;
|
2016-09-21 04:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
// If the file size is already 0, then it has been empty.
|
2016-09-21 04:43:03 +02:00
|
|
|
//
|
2017-04-11 03:55:51 +02:00
|
|
|
if (FileInfo->FileSize != 0) {
|
|
|
|
//
|
|
|
|
// Set the file size to 0.
|
|
|
|
//
|
|
|
|
FileInfo->FileSize = 0;
|
|
|
|
Status = ShellProtocol->SetFileInfo (Handle, FileInfo);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
FreePool (FileInfo);
|
|
|
|
ShellProtocol->CloseFile (Handle);
|
|
|
|
return Status;
|
|
|
|
}
|
2016-09-21 04:43:03 +02:00
|
|
|
}
|
2017-04-11 03:55:51 +02:00
|
|
|
FreePool (FileInfo);
|
2016-09-21 04:43:03 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Write the file data from the buffer
|
|
|
|
//
|
|
|
|
TempBufferSize = BufferSize;
|
2017-04-11 03:55:51 +02:00
|
|
|
Status = ShellProtocol->WriteFile (
|
|
|
|
Handle,
|
|
|
|
&TempBufferSize,
|
|
|
|
Buffer
|
|
|
|
);
|
2016-09-21 04:43:03 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol->CloseFile (Handle);
|
2016-09-21 04:43:03 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-04-11 03:55:51 +02:00
|
|
|
ShellProtocol->CloseFile (Handle);
|
2016-09-21 04:43:03 +02:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|