2 A shell application that triggers capsule update process.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Protocol/SimpleFileSystem.h>
22 #include <Protocol/ShellParameters.h>
23 #include <Protocol/Shell.h>
24 #include <Guid/FileInfo.h>
28 EFI_SHELL_PROTOCOL
*mShellProtocol
= NULL
;
32 This function parse application ARG.
42 EFI_SHELL_PARAMETERS_PROTOCOL
*ShellParameters
;
44 Status
= gBS
->HandleProtocol (
46 &gEfiShellParametersProtocolGuid
,
47 (VOID
**)&ShellParameters
49 if (EFI_ERROR(Status
)) {
53 Argc
= ShellParameters
->Argc
;
54 Argv
= ShellParameters
->Argv
;
61 @return Pointer to shell protocol.
70 if (mShellProtocol
== NULL
) {
71 Status
= gBS
->LocateProtocol (
72 &gEfiShellProtocolGuid
,
74 (VOID
**) &mShellProtocol
76 if (EFI_ERROR (Status
)) {
77 mShellProtocol
= NULL
;
81 return mShellProtocol
;
87 @param[in] FileName The file to be read.
88 @param[out] BufferSize The file buffer size
89 @param[out] Buffer The file buffer
91 @retval EFI_SUCCESS Read file successfully
92 @retval EFI_NOT_FOUND Shell protocol or file not found
93 @retval others Read file failed
98 OUT UINTN
*BufferSize
,
103 EFI_SHELL_PROTOCOL
*ShellProtocol
;
104 SHELL_FILE_HANDLE Handle
;
106 UINTN TempBufferSize
;
109 ShellProtocol
= GetShellProtocol();
110 if (ShellProtocol
== NULL
) {
111 return EFI_NOT_FOUND
;
115 // Open file by FileName.
117 Status
= ShellProtocol
->OpenFileByName (
122 if (EFI_ERROR (Status
)) {
127 // Get the file size.
129 Status
= ShellProtocol
->GetFileSize (Handle
, &FileSize
);
130 if (EFI_ERROR (Status
)) {
131 ShellProtocol
->CloseFile (Handle
);
135 TempBufferSize
= (UINTN
) FileSize
;
136 TempBuffer
= AllocateZeroPool (TempBufferSize
);
137 if (TempBuffer
== NULL
) {
138 ShellProtocol
->CloseFile (Handle
);
139 return EFI_OUT_OF_RESOURCES
;
143 // Read the file data to the buffer
145 Status
= ShellProtocol
->ReadFile (
150 if (EFI_ERROR (Status
)) {
151 ShellProtocol
->CloseFile (Handle
);
155 ShellProtocol
->CloseFile (Handle
);
157 *BufferSize
= TempBufferSize
;
158 *Buffer
= TempBuffer
;
165 @param[in] FileName The file to be written.
166 @param[in] BufferSize The file buffer size
167 @param[in] Buffer The file buffer
169 @retval EFI_SUCCESS Write file successfully
170 @retval EFI_NOT_FOUND Shell protocol not found
171 @retval others Write file failed
174 WriteFileFromBuffer (
181 EFI_SHELL_PROTOCOL
*ShellProtocol
;
182 SHELL_FILE_HANDLE Handle
;
183 EFI_FILE_INFO
*FileInfo
;
184 UINTN TempBufferSize
;
186 ShellProtocol
= GetShellProtocol();
187 if (ShellProtocol
== NULL
) {
188 return EFI_NOT_FOUND
;
192 // Open file by FileName.
194 Status
= ShellProtocol
->OpenFileByName (
197 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
199 if (EFI_ERROR (Status
)) {
204 // Empty the file contents.
206 FileInfo
= ShellProtocol
->GetFileInfo (Handle
);
207 if (FileInfo
== NULL
) {
208 ShellProtocol
->CloseFile (Handle
);
209 return EFI_DEVICE_ERROR
;
213 // If the file size is already 0, then it has been empty.
215 if (FileInfo
->FileSize
!= 0) {
217 // Set the file size to 0.
219 FileInfo
->FileSize
= 0;
220 Status
= ShellProtocol
->SetFileInfo (Handle
, FileInfo
);
221 if (EFI_ERROR (Status
)) {
223 ShellProtocol
->CloseFile (Handle
);
230 // Write the file data from the buffer
232 TempBufferSize
= BufferSize
;
233 Status
= ShellProtocol
->WriteFile (
238 if (EFI_ERROR (Status
)) {
239 ShellProtocol
->CloseFile (Handle
);
243 ShellProtocol
->CloseFile (Handle
);