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