2 A shell application that triggers capsule update process.
4 Copyright (c) 2016, 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 <Library/UefiRuntimeServicesTableLib.h>
22 #include <Library/UefiLib.h>
23 #include <Library/PrintLib.h>
24 #include <Protocol/LoadedImage.h>
25 #include <Protocol/SimpleFileSystem.h>
26 #include <Protocol/ShellParameters.h>
27 #include <Guid/FileInfo.h>
30 #define MAX_ARG_NUM 11
37 This function parse application ARG.
47 EFI_SHELL_PARAMETERS_PROTOCOL
*ShellParameters
;
49 Status
= gBS
->HandleProtocol (
51 &gEfiShellParametersProtocolGuid
,
52 (VOID
**)&ShellParameters
54 if (EFI_ERROR(Status
)) {
58 Argc
= ShellParameters
->Argc
;
59 Argv
= ShellParameters
->Argv
;
64 Return File System Volume containing this shell application.
66 @return File System Volume containing this shell application.
68 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*
74 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
75 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
77 Status
= gBS
->HandleProtocol (
79 &gEfiLoadedImageProtocolGuid
,
82 ASSERT_EFI_ERROR (Status
);
84 Status
= gBS
->HandleProtocol (
85 LoadedImage
->DeviceHandle
,
86 &gEfiSimpleFileSystemProtocolGuid
,
89 if (!EFI_ERROR (Status
)) {
97 Read a file from this volume.
99 @param[in] Vol File System Volume
100 @param[in] FileName The file to be read.
101 @param[out] BufferSize The file buffer size
102 @param[out] Buffer The file buffer
104 @retval EFI_SUCCESS Read file successfully
105 @retval EFI_NOT_FOUND File not found
109 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
,
111 OUT UINTN
*BufferSize
,
116 EFI_FILE_HANDLE RootDir
;
117 EFI_FILE_HANDLE Handle
;
119 EFI_FILE_INFO
*FileInfo
;
120 UINTN TempBufferSize
;
124 // Open the root directory
126 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
127 if (EFI_ERROR (Status
)) {
134 Status
= RootDir
->Open (
141 if (EFI_ERROR (Status
)) {
142 RootDir
->Close (RootDir
);
146 RootDir
->Close (RootDir
);
149 // Get the file information
151 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
153 FileInfo
= AllocateZeroPool (FileInfoSize
);
154 if (FileInfo
== NULL
) {
155 Handle
->Close (Handle
);
159 Status
= Handle
->GetInfo (
165 if (EFI_ERROR (Status
)) {
166 Handle
->Close (Handle
);
167 gBS
->FreePool (FileInfo
);
172 // Allocate buffer for the file data. The last CHAR16 is for L'\0'
174 TempBufferSize
= (UINTN
) FileInfo
->FileSize
+ sizeof(CHAR16
);
175 TempBuffer
= AllocateZeroPool (TempBufferSize
);
176 if (TempBuffer
== NULL
) {
177 Handle
->Close (Handle
);
178 gBS
->FreePool (FileInfo
);
182 gBS
->FreePool (FileInfo
);
185 // Read the file data to the buffer
187 Status
= Handle
->Read (
192 if (EFI_ERROR (Status
)) {
193 Handle
->Close (Handle
);
194 gBS
->FreePool (TempBuffer
);
198 Handle
->Close (Handle
);
200 *BufferSize
= TempBufferSize
;
201 *Buffer
= TempBuffer
;
207 If ScanFs is FLASE, it will use this Vol as default Fs.
208 If ScanFs is TRUE, it will scan all FS and check the file.
209 If there is only one file match the name, it will be read.
210 If there is more than one file match the name, it will return Error.
212 @param[in] ThisVol File System Volume
213 @param[in] FileName The file to be read.
214 @param[out] BufferSize The file buffer size
215 @param[out] Buffer The file buffer
216 @param[in] ScanFs Need Scan all FS
218 @retval EFI_SUCCESS Read file successfully
219 @retval EFI_NOT_FOUND File not found
220 @retval EFI_NO_MAPPING There is duplicated files found
224 IN OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
**ThisVol
,
226 OUT UINTN
*BufferSize
,
232 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
233 UINTN TempBufferSize
;
236 EFI_HANDLE
*HandleBuffer
;
242 if ((FileName
== NULL
) || (Buffer
== NULL
) || (ThisVol
== NULL
)) {
243 return EFI_INVALID_PARAMETER
;
250 if (*ThisVol
== NULL
) {
251 *ThisVol
= GetMyVol ();
252 if (*ThisVol
== NULL
) {
253 return EFI_INVALID_PARAMETER
;
257 // Read file directly from Vol
259 return ReadFileFromVol (*ThisVol
, FileName
, BufferSize
, Buffer
);
267 // Get all Vol handle
269 Status
= gBS
->LocateHandleBuffer (
271 &gEfiSimpleFileSystemProtocolGuid
,
276 if (EFI_ERROR (Status
) && (NoHandles
== 0)) {
277 return EFI_NOT_FOUND
;
281 // Walk through each Vol
286 for (Index
= 0; Index
< NoHandles
; Index
++) {
287 Status
= gBS
->HandleProtocol (
289 &gEfiSimpleFileSystemProtocolGuid
,
292 if (EFI_ERROR(Status
)) {
296 Status
= ReadFileFromVol (Vol
, FileName
, &TempBufferSize
, &TempBuffer
);
297 if (!EFI_ERROR (Status
)) {
299 // Read file OK, check duplication
301 if (*ThisVol
!= NULL
) {
303 // Find the duplicated file
305 gBS
->FreePool (TempBuffer
);
306 gBS
->FreePool (*Buffer
);
307 Print (L
"Duplicated FileName found!\n");
308 return EFI_NO_MAPPING
;
314 *BufferSize
= TempBufferSize
;
315 *Buffer
= TempBuffer
;
323 if (*ThisVol
== NULL
) {
324 return EFI_NOT_FOUND
;
336 @param[in] FileName The file to be read.
337 @param[out] BufferSize The file buffer size
338 @param[out] Buffer The file buffer
340 @retval EFI_SUCCESS Read file successfully
341 @retval EFI_NOT_FOUND File not found
346 OUT UINTN
*BufferSize
,
350 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
352 return ReadFileToBufferEx(&Vol
, FileName
, BufferSize
, Buffer
, FALSE
);
358 @param[in] FileName The file to be written.
359 @param[in] BufferSize The file buffer size
360 @param[in] Buffer The file buffer
362 @retval EFI_SUCCESS Write file successfully
365 WriteFileFromBuffer (
372 EFI_FILE_HANDLE RootDir
;
373 EFI_FILE_HANDLE Handle
;
374 UINTN TempBufferSize
;
375 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
379 return EFI_NOT_FOUND
;
383 // Open the root directory
385 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
386 if (EFI_ERROR (Status
)) {
393 Status
= RootDir
->Open (
397 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
400 if (EFI_ERROR (Status
)) {
401 RootDir
->Close (RootDir
);
408 Status
= Handle
->Delete(Handle
);
409 if (EFI_ERROR(Status
)) {
414 // Open the file again
416 Status
= RootDir
->Open (
420 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
423 if (EFI_ERROR (Status
)) {
424 RootDir
->Close (RootDir
);
428 RootDir
->Close (RootDir
);
431 // Write the file data from the buffer
433 TempBufferSize
= BufferSize
;
434 Status
= Handle
->Write (
439 if (EFI_ERROR (Status
)) {
440 Handle
->Close (Handle
);
444 Handle
->Close (Handle
);