]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Application/CapsuleApp/AppSupport.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Application / CapsuleApp / AppSupport.c
1 /** @file
2 A shell application that triggers capsule update process.
3
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
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>
19
20 UINTN Argc;
21 CHAR16 **Argv;
22 EFI_SHELL_PROTOCOL *mShellProtocol = NULL;
23
24 /**
25
26 This function parse application ARG.
27
28 @return Status
29 **/
30 EFI_STATUS
31 GetArg (
32 VOID
33 )
34 {
35 EFI_STATUS Status;
36 EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;
37
38 Status = gBS->HandleProtocol (
39 gImageHandle,
40 &gEfiShellParametersProtocolGuid,
41 (VOID**)&ShellParameters
42 );
43 if (EFI_ERROR(Status)) {
44 return Status;
45 }
46
47 Argc = ShellParameters->Argc;
48 Argv = ShellParameters->Argv;
49 return EFI_SUCCESS;
50 }
51
52 /**
53 Get shell protocol.
54
55 @return Pointer to shell protocol.
56 **/
57 EFI_SHELL_PROTOCOL *
58 GetShellProtocol (
59 VOID
60 )
61 {
62 EFI_STATUS Status;
63
64 if (mShellProtocol == NULL) {
65 Status = gBS->LocateProtocol (
66 &gEfiShellProtocolGuid,
67 NULL,
68 (VOID **) &mShellProtocol
69 );
70 if (EFI_ERROR (Status)) {
71 mShellProtocol = NULL;
72 }
73 }
74
75 return mShellProtocol;
76 }
77
78 /**
79 Read a file.
80
81 @param[in] FileName The file to be read.
82 @param[out] BufferSize The file buffer size
83 @param[out] Buffer The file buffer
84
85 @retval EFI_SUCCESS Read file successfully
86 @retval EFI_NOT_FOUND Shell protocol or file not found
87 @retval others Read file failed
88 **/
89 EFI_STATUS
90 ReadFileToBuffer (
91 IN CHAR16 *FileName,
92 OUT UINTN *BufferSize,
93 OUT VOID **Buffer
94 )
95 {
96 EFI_STATUS Status;
97 EFI_SHELL_PROTOCOL *ShellProtocol;
98 SHELL_FILE_HANDLE Handle;
99 UINT64 FileSize;
100 UINTN TempBufferSize;
101 VOID *TempBuffer;
102
103 ShellProtocol = GetShellProtocol();
104 if (ShellProtocol == NULL) {
105 return EFI_NOT_FOUND;
106 }
107
108 //
109 // Open file by FileName.
110 //
111 Status = ShellProtocol->OpenFileByName (
112 FileName,
113 &Handle,
114 EFI_FILE_MODE_READ
115 );
116 if (EFI_ERROR (Status)) {
117 return Status;
118 }
119
120 //
121 // Get the file size.
122 //
123 Status = ShellProtocol->GetFileSize (Handle, &FileSize);
124 if (EFI_ERROR (Status)) {
125 ShellProtocol->CloseFile (Handle);
126 return Status;
127 }
128
129 TempBufferSize = (UINTN) FileSize;
130 TempBuffer = AllocateZeroPool (TempBufferSize);
131 if (TempBuffer == NULL) {
132 ShellProtocol->CloseFile (Handle);
133 return EFI_OUT_OF_RESOURCES;
134 }
135
136 //
137 // Read the file data to the buffer
138 //
139 Status = ShellProtocol->ReadFile (
140 Handle,
141 &TempBufferSize,
142 TempBuffer
143 );
144 if (EFI_ERROR (Status)) {
145 ShellProtocol->CloseFile (Handle);
146 return Status;
147 }
148
149 ShellProtocol->CloseFile (Handle);
150
151 *BufferSize = TempBufferSize;
152 *Buffer = TempBuffer;
153 return EFI_SUCCESS;
154 }
155
156 /**
157 Write a file.
158
159 @param[in] FileName The file to be written.
160 @param[in] BufferSize The file buffer size
161 @param[in] Buffer The file buffer
162
163 @retval EFI_SUCCESS Write file successfully
164 @retval EFI_NOT_FOUND Shell protocol not found
165 @retval others Write file failed
166 **/
167 EFI_STATUS
168 WriteFileFromBuffer (
169 IN CHAR16 *FileName,
170 IN UINTN BufferSize,
171 IN VOID *Buffer
172 )
173 {
174 EFI_STATUS Status;
175 EFI_SHELL_PROTOCOL *ShellProtocol;
176 SHELL_FILE_HANDLE Handle;
177 EFI_FILE_INFO *FileInfo;
178 UINTN TempBufferSize;
179
180 ShellProtocol = GetShellProtocol();
181 if (ShellProtocol == NULL) {
182 return EFI_NOT_FOUND;
183 }
184
185 //
186 // Open file by FileName.
187 //
188 Status = ShellProtocol->OpenFileByName (
189 FileName,
190 &Handle,
191 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
192 );
193 if (EFI_ERROR (Status)) {
194 return Status;
195 }
196
197 //
198 // Empty the file contents.
199 //
200 FileInfo = ShellProtocol->GetFileInfo (Handle);
201 if (FileInfo == NULL) {
202 ShellProtocol->CloseFile (Handle);
203 return EFI_DEVICE_ERROR;
204 }
205
206 //
207 // If the file size is already 0, then it has been empty.
208 //
209 if (FileInfo->FileSize != 0) {
210 //
211 // Set the file size to 0.
212 //
213 FileInfo->FileSize = 0;
214 Status = ShellProtocol->SetFileInfo (Handle, FileInfo);
215 if (EFI_ERROR (Status)) {
216 FreePool (FileInfo);
217 ShellProtocol->CloseFile (Handle);
218 return Status;
219 }
220 }
221 FreePool (FileInfo);
222
223 //
224 // Write the file data from the buffer
225 //
226 TempBufferSize = BufferSize;
227 Status = ShellProtocol->WriteFile (
228 Handle,
229 &TempBufferSize,
230 Buffer
231 );
232 if (EFI_ERROR (Status)) {
233 ShellProtocol->CloseFile (Handle);
234 return Status;
235 }
236
237 ShellProtocol->CloseFile (Handle);
238
239 return EFI_SUCCESS;
240 }
241