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