]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Application/CapsuleApp/AppSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Application / CapsuleApp / AppSupport.c
1 /** @file
2 A shell application that triggers capsule update process.
3
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "CapsuleApp.h"
10
11 UINTN Argc;
12 CHAR16 **Argv;
13 EFI_SHELL_PROTOCOL *mShellProtocol = NULL;
14
15 /**
16
17 This function parse application ARG.
18
19 @return Status
20 **/
21 EFI_STATUS
22 GetArg (
23 VOID
24 )
25 {
26 EFI_STATUS Status;
27 EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;
28
29 Status = gBS->HandleProtocol (
30 gImageHandle,
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
43 /**
44 Get shell protocol.
45
46 @return Pointer to shell protocol.
47 **/
48 EFI_SHELL_PROTOCOL *
49 GetShellProtocol (
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
69 /**
70 Read a file.
71
72 @param[in] FileName The file to be read.
73 @param[out] BufferSize The file buffer size
74 @param[out] Buffer The file buffer
75
76 @retval EFI_SUCCESS Read file successfully
77 @retval EFI_NOT_FOUND Shell protocol or file not found
78 @retval others Read file failed
79 **/
80 EFI_STATUS
81 ReadFileToBuffer (
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
147 /**
148 Write a file.
149
150 @param[in] FileName The file to be written.
151 @param[in] BufferSize The file buffer size
152 @param[in] Buffer The file buffer
153
154 @retval EFI_SUCCESS Write file successfully
155 @retval EFI_NOT_FOUND Shell protocol not found
156 @retval others Write file failed
157 **/
158 EFI_STATUS
159 WriteFileFromBuffer (
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;
168 EFI_FILE_INFO *FileInfo;
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)) {
207 FreePool (FileInfo);
208 ShellProtocol->CloseFile (Handle);
209 return Status;
210 }
211 }
212
213 FreePool (FileInfo);
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 }