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