2 A shell application that triggers capsule update process.
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "CapsuleApp.h"
13 EFI_SHELL_PROTOCOL
*mShellProtocol
= NULL
;
17 This function parse application ARG.
27 EFI_SHELL_PARAMETERS_PROTOCOL
*ShellParameters
;
29 Status
= gBS
->HandleProtocol (
31 &gEfiShellParametersProtocolGuid
,
32 (VOID
**)&ShellParameters
34 if (EFI_ERROR (Status
)) {
38 Argc
= ShellParameters
->Argc
;
39 Argv
= ShellParameters
->Argv
;
46 @return Pointer to shell protocol.
55 if (mShellProtocol
== NULL
) {
56 Status
= gBS
->LocateProtocol (
57 &gEfiShellProtocolGuid
,
59 (VOID
**)&mShellProtocol
61 if (EFI_ERROR (Status
)) {
62 mShellProtocol
= NULL
;
66 return mShellProtocol
;
72 @param[in] FileName The file to be read.
73 @param[out] BufferSize The file buffer size
74 @param[out] Buffer The file buffer
76 @retval EFI_SUCCESS Read file successfully
77 @retval EFI_NOT_FOUND Shell protocol or file not found
78 @retval others Read file failed
83 OUT UINTN
*BufferSize
,
88 EFI_SHELL_PROTOCOL
*ShellProtocol
;
89 SHELL_FILE_HANDLE Handle
;
94 ShellProtocol
= GetShellProtocol ();
95 if (ShellProtocol
== NULL
) {
100 // Open file by FileName.
102 Status
= ShellProtocol
->OpenFileByName (
107 if (EFI_ERROR (Status
)) {
112 // Get the file size.
114 Status
= ShellProtocol
->GetFileSize (Handle
, &FileSize
);
115 if (EFI_ERROR (Status
)) {
116 ShellProtocol
->CloseFile (Handle
);
120 TempBufferSize
= (UINTN
)FileSize
;
121 TempBuffer
= AllocateZeroPool (TempBufferSize
);
122 if (TempBuffer
== NULL
) {
123 ShellProtocol
->CloseFile (Handle
);
124 return EFI_OUT_OF_RESOURCES
;
128 // Read the file data to the buffer
130 Status
= ShellProtocol
->ReadFile (
135 if (EFI_ERROR (Status
)) {
136 ShellProtocol
->CloseFile (Handle
);
140 ShellProtocol
->CloseFile (Handle
);
142 *BufferSize
= TempBufferSize
;
143 *Buffer
= TempBuffer
;
150 @param[in] FileName The file to be written.
151 @param[in] BufferSize The file buffer size
152 @param[in] Buffer The file buffer
154 @retval EFI_SUCCESS Write file successfully
155 @retval EFI_NOT_FOUND Shell protocol not found
156 @retval others Write file failed
159 WriteFileFromBuffer (
166 EFI_SHELL_PROTOCOL
*ShellProtocol
;
167 SHELL_FILE_HANDLE Handle
;
168 EFI_FILE_INFO
*FileInfo
;
169 UINTN TempBufferSize
;
171 ShellProtocol
= GetShellProtocol ();
172 if (ShellProtocol
== NULL
) {
173 return EFI_NOT_FOUND
;
177 // Open file by FileName.
179 Status
= ShellProtocol
->OpenFileByName (
182 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
184 if (EFI_ERROR (Status
)) {
189 // Empty the file contents.
191 FileInfo
= ShellProtocol
->GetFileInfo (Handle
);
192 if (FileInfo
== NULL
) {
193 ShellProtocol
->CloseFile (Handle
);
194 return EFI_DEVICE_ERROR
;
198 // If the file size is already 0, then it has been empty.
200 if (FileInfo
->FileSize
!= 0) {
202 // Set the file size to 0.
204 FileInfo
->FileSize
= 0;
205 Status
= ShellProtocol
->SetFileInfo (Handle
, FileInfo
);
206 if (EFI_ERROR (Status
)) {
208 ShellProtocol
->CloseFile (Handle
);
216 // Write the file data from the buffer
218 TempBufferSize
= BufferSize
;
219 Status
= ShellProtocol
->WriteFile (
224 if (EFI_ERROR (Status
)) {
225 ShellProtocol
->CloseFile (Handle
);
229 ShellProtocol
->CloseFile (Handle
);