]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigFileExplorer.c
SecurityPkg/SecureBootConfigDxe: replace OpenFileByDevicePath() with UefiLib API
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigFileExplorer.c
1 /** @file
2 Internal file explorer functions for SecureBoot configuration module.
3
4 Copyright (c) 2012 - 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
9
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.
12
13 **/
14
15 #include "SecureBootConfigImpl.h"
16
17 VOID *mStartOpCodeHandle = NULL;
18 VOID *mEndOpCodeHandle = NULL;
19 EFI_IFR_GUID_LABEL *mStartLabel = NULL;
20 EFI_IFR_GUID_LABEL *mEndLabel = NULL;
21
22 /**
23 Refresh the global UpdateData structure.
24
25 **/
26 VOID
27 RefreshUpdateData (
28 VOID
29 )
30 {
31 //
32 // Free current updated date
33 //
34 if (mStartOpCodeHandle != NULL) {
35 HiiFreeOpCodeHandle (mStartOpCodeHandle);
36 }
37
38 //
39 // Create new OpCode Handle
40 //
41 mStartOpCodeHandle = HiiAllocateOpCodeHandle ();
42
43 //
44 // Create Hii Extend Label OpCode as the start opcode
45 //
46 mStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
47 mStartOpCodeHandle,
48 &gEfiIfrTianoGuid,
49 NULL,
50 sizeof (EFI_IFR_GUID_LABEL)
51 );
52 mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
53 }
54
55 /**
56 Clean up the dynamic opcode at label and form specified by both LabelId.
57
58 @param[in] LabelId It is both the Form ID and Label ID for opcode deletion.
59 @param[in] PrivateData Module private data.
60
61 **/
62 VOID
63 CleanUpPage (
64 IN UINT16 LabelId,
65 IN SECUREBOOT_CONFIG_PRIVATE_DATA *PrivateData
66 )
67 {
68 RefreshUpdateData ();
69
70 //
71 // Remove all op-codes from dynamic page
72 //
73 mStartLabel->Number = LabelId;
74 HiiUpdateForm (
75 PrivateData->HiiHandle,
76 &gSecureBootConfigFormSetGuid,
77 LabelId,
78 mStartOpCodeHandle, // Label LabelId
79 mEndOpCodeHandle // LABEL_END
80 );
81 }
82
83 /**
84 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
85 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
86 means not enough memory resource.
87
88 @param DevicePath Device path.
89
90 @retval NULL Not enough memory resourece for AllocateCopyPool.
91 @retval Other A new allocated string that represents the file name.
92
93 **/
94 CHAR16 *
95 ExtractFileNameFromDevicePath (
96 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
97 )
98 {
99 CHAR16 *String;
100 CHAR16 *MatchString;
101 CHAR16 *LastMatch;
102 CHAR16 *FileName;
103 UINTN Length;
104
105 ASSERT(DevicePath != NULL);
106
107 String = DevicePathToStr(DevicePath);
108 MatchString = String;
109 LastMatch = String;
110 FileName = NULL;
111
112 while(MatchString != NULL){
113 LastMatch = MatchString + 1;
114 MatchString = StrStr(LastMatch,L"\\");
115 }
116
117 Length = StrLen(LastMatch);
118 FileName = AllocateCopyPool ((Length + 1) * sizeof(CHAR16), LastMatch);
119 if (FileName != NULL) {
120 *(FileName + Length) = 0;
121 }
122
123 FreePool(String);
124
125 return FileName;
126 }
127
128
129 /**
130 Update the form base on the selected file.
131
132 @param FilePath Point to the file path.
133 @param FormId The form need to display.
134
135 @retval TRUE Exit caller function.
136 @retval FALSE Not exit caller function.
137
138 **/
139 BOOLEAN
140 UpdatePage(
141 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
142 IN EFI_FORM_ID FormId
143 )
144 {
145 CHAR16 *FileName;
146 EFI_STRING_ID StringToken;
147
148 FileName = NULL;
149
150 if (FilePath != NULL) {
151 FileName = ExtractFileNameFromDevicePath(FilePath);
152 }
153 if (FileName == NULL) {
154 //
155 // FileName = NULL has two case:
156 // 1. FilePath == NULL, not select file.
157 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
158 // In these two case, no need to update the form, and exit the caller function.
159 //
160 return TRUE;
161 }
162 StringToken = HiiSetString (gSecureBootPrivateData->HiiHandle, 0, FileName, NULL);
163
164 gSecureBootPrivateData->FileContext->FileName = FileName;
165
166 EfiOpenFileByDevicePath (
167 &FilePath,
168 &gSecureBootPrivateData->FileContext->FHandle,
169 EFI_FILE_MODE_READ,
170 0
171 );
172 //
173 // Create Subtitle op-code for the display string of the option.
174 //
175 RefreshUpdateData ();
176 mStartLabel->Number = FormId;
177
178 HiiCreateSubTitleOpCode (
179 mStartOpCodeHandle,
180 StringToken,
181 0,
182 0,
183 0
184 );
185
186 HiiUpdateForm (
187 gSecureBootPrivateData->HiiHandle,
188 &gSecureBootConfigFormSetGuid,
189 FormId,
190 mStartOpCodeHandle, // Label FormId
191 mEndOpCodeHandle // LABEL_END
192 );
193
194 return TRUE;
195 }
196
197 /**
198 Update the PK form base on the input file path info.
199
200 @param FilePath Point to the file path.
201
202 @retval TRUE Exit caller function.
203 @retval FALSE Not exit caller function.
204 **/
205 BOOLEAN
206 EFIAPI
207 UpdatePKFromFile (
208 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
209 )
210 {
211 return UpdatePage(FilePath, FORMID_ENROLL_PK_FORM);
212
213 }
214
215 /**
216 Update the KEK form base on the input file path info.
217
218 @param FilePath Point to the file path.
219
220 @retval TRUE Exit caller function.
221 @retval FALSE Not exit caller function.
222 **/
223 BOOLEAN
224 EFIAPI
225 UpdateKEKFromFile (
226 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
227 )
228 {
229 return UpdatePage(FilePath, FORMID_ENROLL_KEK_FORM);
230 }
231
232 /**
233 Update the DB form base on the input file path info.
234
235 @param FilePath Point to the file path.
236
237 @retval TRUE Exit caller function.
238 @retval FALSE Not exit caller function.
239 **/
240 BOOLEAN
241 EFIAPI
242 UpdateDBFromFile (
243 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
244 )
245 {
246 return UpdatePage(FilePath, SECUREBOOT_ENROLL_SIGNATURE_TO_DB);
247 }
248
249 /**
250 Update the DBX form base on the input file path info.
251
252 @param FilePath Point to the file path.
253
254 @retval TRUE Exit caller function.
255 @retval FALSE Not exit caller function.
256 **/
257 BOOLEAN
258 EFIAPI
259 UpdateDBXFromFile (
260 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
261 )
262 {
263 return UpdatePage(FilePath, SECUREBOOT_ENROLL_SIGNATURE_TO_DBX);
264 }
265
266 /**
267 Update the DBT form base on the input file path info.
268
269 @param FilePath Point to the file path.
270
271 @retval TRUE Exit caller function.
272 @retval FALSE Not exit caller function.
273 **/
274 BOOLEAN
275 EFIAPI
276 UpdateDBTFromFile (
277 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
278 )
279 {
280 return UpdatePage(FilePath, SECUREBOOT_ENROLL_SIGNATURE_TO_DBT);
281 }
282