TianoCore EDK2 master
Loading...
Searching...
No Matches
AppSupport.c
Go to the documentation of this file.
1
9#include "CapsuleApp.h"
10
11UINTN Argc;
12CHAR16 **Argv;
13EFI_SHELL_PROTOCOL *mShellProtocol = NULL;
14
23 VOID
24 )
25{
26 EFI_STATUS Status;
27 EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;
28
29 Status = gBS->HandleProtocol (
31 &gEfiShellParametersProtocolGuid,
32 (VOID **)&ShellParameters
33 );
34 if (EFI_ERROR (Status)) {
35 return Status;
36 }
37
38 Argc = ShellParameters->Argc;
39 Argv = ShellParameters->Argv;
40 return EFI_SUCCESS;
41}
42
50 VOID
51 )
52{
53 EFI_STATUS Status;
54
55 if (mShellProtocol == NULL) {
56 Status = gBS->LocateProtocol (
57 &gEfiShellProtocolGuid,
58 NULL,
59 (VOID **)&mShellProtocol
60 );
61 if (EFI_ERROR (Status)) {
62 mShellProtocol = NULL;
63 }
64 }
65
66 return mShellProtocol;
67}
68
82 IN CHAR16 *FileName,
83 OUT UINTN *BufferSize,
84 OUT VOID **Buffer
85 )
86{
87 EFI_STATUS Status;
88 EFI_SHELL_PROTOCOL *ShellProtocol;
89 SHELL_FILE_HANDLE Handle;
90 UINT64 FileSize;
91 UINTN TempBufferSize;
92 VOID *TempBuffer;
93
94 ShellProtocol = GetShellProtocol ();
95 if (ShellProtocol == NULL) {
96 return EFI_NOT_FOUND;
97 }
98
99 //
100 // Open file by FileName.
101 //
102 Status = ShellProtocol->OpenFileByName (
103 FileName,
104 &Handle,
105 EFI_FILE_MODE_READ
106 );
107 if (EFI_ERROR (Status)) {
108 return Status;
109 }
110
111 //
112 // Get the file size.
113 //
114 Status = ShellProtocol->GetFileSize (Handle, &FileSize);
115 if (EFI_ERROR (Status)) {
116 ShellProtocol->CloseFile (Handle);
117 return Status;
118 }
119
120 TempBufferSize = (UINTN)FileSize;
121 TempBuffer = AllocateZeroPool (TempBufferSize);
122 if (TempBuffer == NULL) {
123 ShellProtocol->CloseFile (Handle);
124 return EFI_OUT_OF_RESOURCES;
125 }
126
127 //
128 // Read the file data to the buffer
129 //
130 Status = ShellProtocol->ReadFile (
131 Handle,
132 &TempBufferSize,
133 TempBuffer
134 );
135 if (EFI_ERROR (Status)) {
136 ShellProtocol->CloseFile (Handle);
137 return Status;
138 }
139
140 ShellProtocol->CloseFile (Handle);
141
142 *BufferSize = TempBufferSize;
143 *Buffer = TempBuffer;
144 return EFI_SUCCESS;
145}
146
160 IN CHAR16 *FileName,
161 IN UINTN BufferSize,
162 IN VOID *Buffer
163 )
164{
165 EFI_STATUS Status;
166 EFI_SHELL_PROTOCOL *ShellProtocol;
167 SHELL_FILE_HANDLE Handle;
169 UINTN TempBufferSize;
170
171 ShellProtocol = GetShellProtocol ();
172 if (ShellProtocol == NULL) {
173 return EFI_NOT_FOUND;
174 }
175
176 //
177 // Open file by FileName.
178 //
179 Status = ShellProtocol->OpenFileByName (
180 FileName,
181 &Handle,
182 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
183 );
184 if (EFI_ERROR (Status)) {
185 return Status;
186 }
187
188 //
189 // Empty the file contents.
190 //
191 FileInfo = ShellProtocol->GetFileInfo (Handle);
192 if (FileInfo == NULL) {
193 ShellProtocol->CloseFile (Handle);
194 return EFI_DEVICE_ERROR;
195 }
196
197 //
198 // If the file size is already 0, then it has been empty.
199 //
200 if (FileInfo->FileSize != 0) {
201 //
202 // Set the file size to 0.
203 //
204 FileInfo->FileSize = 0;
205 Status = ShellProtocol->SetFileInfo (Handle, FileInfo);
206 if (EFI_ERROR (Status)) {
208 ShellProtocol->CloseFile (Handle);
209 return Status;
210 }
211 }
212
214
215 //
216 // Write the file data from the buffer
217 //
218 TempBufferSize = BufferSize;
219 Status = ShellProtocol->WriteFile (
220 Handle,
221 &TempBufferSize,
222 Buffer
223 );
224 if (EFI_ERROR (Status)) {
225 ShellProtocol->CloseFile (Handle);
226 return Status;
227 }
228
229 ShellProtocol->CloseFile (Handle);
230
231 return EFI_SUCCESS;
232}
UINT64 UINTN
EFI_STATUS ReadFileToBuffer(IN CHAR16 *FileName, OUT UINTN *BufferSize, OUT VOID **Buffer)
Definition: AppSupport.c:81
EFI_STATUS WriteFileFromBuffer(IN CHAR16 *FileName, IN UINTN BufferSize, IN VOID *Buffer)
Definition: AppSupport.c:159
EFI_STATUS GetArg(VOID)
Definition: AppSupport.c:22
EFI_SHELL_PROTOCOL * GetShellProtocol(VOID)
Definition: AppSupport.c:49
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_FILE_INFO * FileInfo(IN EFI_FILE_HANDLE FHand)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
UINT64 FileSize
Definition: FileInfo.h:27