2 A shell application that triggers capsule update process.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15 #include <Protocol/SimpleFileSystem.h>
16 #include <Protocol/ShellParameters.h>
17 #include <Protocol/Shell.h>
18 #include <Guid/FileInfo.h>
22 EFI_SHELL_PROTOCOL
*mShellProtocol
= NULL
;
26 This function parse application ARG.
36 EFI_SHELL_PARAMETERS_PROTOCOL
*ShellParameters
;
38 Status
= gBS
->HandleProtocol (
40 &gEfiShellParametersProtocolGuid
,
41 (VOID
**)&ShellParameters
43 if (EFI_ERROR(Status
)) {
47 Argc
= ShellParameters
->Argc
;
48 Argv
= ShellParameters
->Argv
;
55 @return Pointer to shell protocol.
64 if (mShellProtocol
== NULL
) {
65 Status
= gBS
->LocateProtocol (
66 &gEfiShellProtocolGuid
,
68 (VOID
**) &mShellProtocol
70 if (EFI_ERROR (Status
)) {
71 mShellProtocol
= NULL
;
75 return mShellProtocol
;
81 @param[in] FileName The file to be read.
82 @param[out] BufferSize The file buffer size
83 @param[out] Buffer The file buffer
85 @retval EFI_SUCCESS Read file successfully
86 @retval EFI_NOT_FOUND Shell protocol or file not found
87 @retval others Read file failed
92 OUT UINTN
*BufferSize
,
97 EFI_SHELL_PROTOCOL
*ShellProtocol
;
98 SHELL_FILE_HANDLE Handle
;
100 UINTN TempBufferSize
;
103 ShellProtocol
= GetShellProtocol();
104 if (ShellProtocol
== NULL
) {
105 return EFI_NOT_FOUND
;
109 // Open file by FileName.
111 Status
= ShellProtocol
->OpenFileByName (
116 if (EFI_ERROR (Status
)) {
121 // Get the file size.
123 Status
= ShellProtocol
->GetFileSize (Handle
, &FileSize
);
124 if (EFI_ERROR (Status
)) {
125 ShellProtocol
->CloseFile (Handle
);
129 TempBufferSize
= (UINTN
) FileSize
;
130 TempBuffer
= AllocateZeroPool (TempBufferSize
);
131 if (TempBuffer
== NULL
) {
132 ShellProtocol
->CloseFile (Handle
);
133 return EFI_OUT_OF_RESOURCES
;
137 // Read the file data to the buffer
139 Status
= ShellProtocol
->ReadFile (
144 if (EFI_ERROR (Status
)) {
145 ShellProtocol
->CloseFile (Handle
);
149 ShellProtocol
->CloseFile (Handle
);
151 *BufferSize
= TempBufferSize
;
152 *Buffer
= TempBuffer
;
159 @param[in] FileName The file to be written.
160 @param[in] BufferSize The file buffer size
161 @param[in] Buffer The file buffer
163 @retval EFI_SUCCESS Write file successfully
164 @retval EFI_NOT_FOUND Shell protocol not found
165 @retval others Write file failed
168 WriteFileFromBuffer (
175 EFI_SHELL_PROTOCOL
*ShellProtocol
;
176 SHELL_FILE_HANDLE Handle
;
177 EFI_FILE_INFO
*FileInfo
;
178 UINTN TempBufferSize
;
180 ShellProtocol
= GetShellProtocol();
181 if (ShellProtocol
== NULL
) {
182 return EFI_NOT_FOUND
;
186 // Open file by FileName.
188 Status
= ShellProtocol
->OpenFileByName (
191 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
193 if (EFI_ERROR (Status
)) {
198 // Empty the file contents.
200 FileInfo
= ShellProtocol
->GetFileInfo (Handle
);
201 if (FileInfo
== NULL
) {
202 ShellProtocol
->CloseFile (Handle
);
203 return EFI_DEVICE_ERROR
;
207 // If the file size is already 0, then it has been empty.
209 if (FileInfo
->FileSize
!= 0) {
211 // Set the file size to 0.
213 FileInfo
->FileSize
= 0;
214 Status
= ShellProtocol
->SetFileInfo (Handle
, FileInfo
);
215 if (EFI_ERROR (Status
)) {
217 ShellProtocol
->CloseFile (Handle
);
224 // Write the file data from the buffer
226 TempBufferSize
= BufferSize
;
227 Status
= ShellProtocol
->WriteFile (
232 if (EFI_ERROR (Status
)) {
233 ShellProtocol
->CloseFile (Handle
);
237 ShellProtocol
->CloseFile (Handle
);