1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/** @file
A shell application that triggers capsule update process.
Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "CapsuleApp.h"
UINTN Argc;
CHAR16 **Argv;
EFI_SHELL_PROTOCOL *mShellProtocol = NULL;
/**
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;
}
/**
Get shell protocol.
@return Pointer to shell protocol.
**/
EFI_SHELL_PROTOCOL *
GetShellProtocol (
VOID
)
{
EFI_STATUS Status;
if (mShellProtocol == NULL) {
Status = gBS->LocateProtocol (
&gEfiShellProtocolGuid,
NULL,
(VOID **)&mShellProtocol
);
if (EFI_ERROR (Status)) {
mShellProtocol = NULL;
}
}
return mShellProtocol;
}
/**
Read a file.
@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
@retval EFI_NOT_FOUND Shell protocol or file not found
@retval others Read file failed
**/
EFI_STATUS
ReadFileToBuffer (
IN CHAR16 *FileName,
OUT UINTN *BufferSize,
OUT VOID **Buffer
)
{
EFI_STATUS Status;
EFI_SHELL_PROTOCOL *ShellProtocol;
SHELL_FILE_HANDLE Handle;
UINT64 FileSize;
UINTN TempBufferSize;
VOID *TempBuffer;
ShellProtocol = GetShellProtocol ();
if (ShellProtocol == NULL) {
return EFI_NOT_FOUND;
}
//
// Open file by FileName.
//
Status = ShellProtocol->OpenFileByName (
FileName,
&Handle,
EFI_FILE_MODE_READ
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Get the file size.
//
Status = ShellProtocol->GetFileSize (Handle, &FileSize);
if (EFI_ERROR (Status)) {
ShellProtocol->CloseFile (Handle);
return Status;
}
TempBufferSize = (UINTN)FileSize;
TempBuffer = AllocateZeroPool (TempBufferSize);
if (TempBuffer == NULL) {
ShellProtocol->CloseFile (Handle);
return EFI_OUT_OF_RESOURCES;
}
//
// Read the file data to the buffer
//
Status = ShellProtocol->ReadFile (
Handle,
&TempBufferSize,
TempBuffer
);
if (EFI_ERROR (Status)) {
ShellProtocol->CloseFile (Handle);
return Status;
}
ShellProtocol->CloseFile (Handle);
*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
@retval EFI_NOT_FOUND Shell protocol not found
@retval others Write file failed
**/
EFI_STATUS
WriteFileFromBuffer (
IN CHAR16 *FileName,
IN UINTN BufferSize,
IN VOID *Buffer
)
{
EFI_STATUS Status;
EFI_SHELL_PROTOCOL *ShellProtocol;
SHELL_FILE_HANDLE Handle;
EFI_FILE_INFO *FileInfo;
UINTN TempBufferSize;
ShellProtocol = GetShellProtocol ();
if (ShellProtocol == NULL) {
return EFI_NOT_FOUND;
}
//
// Open file by FileName.
//
Status = ShellProtocol->OpenFileByName (
FileName,
&Handle,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Empty the file contents.
//
FileInfo = ShellProtocol->GetFileInfo (Handle);
if (FileInfo == NULL) {
ShellProtocol->CloseFile (Handle);
return EFI_DEVICE_ERROR;
}
//
// If the file size is already 0, then it has been empty.
//
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;
}
}
FreePool (FileInfo);
//
// Write the file data from the buffer
//
TempBufferSize = BufferSize;
Status = ShellProtocol->WriteFile (
Handle,
&TempBufferSize,
Buffer
);
if (EFI_ERROR (Status)) {
ShellProtocol->CloseFile (Handle);
return Status;
}
ShellProtocol->CloseFile (Handle);
return EFI_SUCCESS;
}
|