1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
#include <Library/BaseMemoryLib.h>
#include <Library/DevicePathLib.h>
#include <Library/HiiLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiHiiServicesLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Protocol/HiiConfigAccess.h>
#include <Protocol/Hash2.h>
#include <Protocol/ServiceBinding.h>
#include "Data.h"
extern UINT8 FormBin[];
#pragma pack(1)
typedef struct {
VENDOR_DEVICE_PATH VendorDevicePath;
EFI_DEVICE_PATH_PROTOCOL End;
} HII_VENDOR_DEVICE_PATH;
#pragma pack()
HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath = {
{
{
HARDWARE_DEVICE_PATH,
HW_VENDOR_DP,
{
(UINT8) (sizeof (VENDOR_DEVICE_PATH)),
(UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
}
},
DATAPATH_GUID
},
{
END_DEVICE_PATH_TYPE,
END_ENTIRE_DEVICE_PATH_SUBTYPE,
{
(UINT8) (END_DEVICE_PATH_LENGTH),
(UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
}
}
};
EFI_HII_HANDLE mHiiHandle = NULL;
EFI_HANDLE mDriverHandle = NULL;
EFI_HII_CONFIG_ACCESS_PROTOCOL mConfigAccess;
EFI_GUID StorageGuid = STORAGE_GUID;
EFI_STRING StorageName = L"FormData";
VARIABLE_STRUCTURE FormStorage;
EFI_SERVICE_BINDING_PROTOCOL* hash2ServiceBinding;
EFI_HASH2_PROTOCOL* hash2Protocol;
EFI_HANDLE hash2ChildHandle = NULL;
STATIC
EFI_STATUS
EFIAPI
ExtractConfig (
IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
IN CONST EFI_STRING Request,
OUT EFI_STRING *Progress,
OUT EFI_STRING *Results
)
{
BOOLEAN AllocatedRequest = FALSE;
if (Progress == NULL || Results == NULL) {
return EFI_INVALID_PARAMETER;
}
if ((Request != NULL) && !HiiIsConfigHdrMatch(Request, &StorageGuid, StorageName)) {
return EFI_NOT_FOUND;
}
EFI_STRING ConfigRequest = Request;
if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {
EFI_STRING ConfigRequestHdr = HiiConstructConfigHdr(&StorageGuid, StorageName, mDriverHandle);
UINTN Size = (StrLen(ConfigRequestHdr) + StrLen(L"&OFFSET=0&WIDTH=") + sizeof(UINTN)*2 + 1) * sizeof(CHAR16);
ConfigRequest = AllocateZeroPool(Size);
AllocatedRequest = TRUE;
UnicodeSPrint(ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, sizeof(VARIABLE_STRUCTURE));
FreePool(ConfigRequestHdr);
}
EFI_STATUS Status = gHiiConfigRouting->BlockToConfig(gHiiConfigRouting,
ConfigRequest,
(UINT8*)&FormStorage,
sizeof(VARIABLE_STRUCTURE),
Results,
Progress);
if (AllocatedRequest) {
FreePool(ConfigRequest);
if (Request == NULL) {
*Progress = NULL;
} else if (StrStr(Request, L"OFFSET") == NULL) {
*Progress = Request + StrLen(Request);
}
}
return Status;
}
STATIC
EFI_STATUS
EFIAPI
RouteConfig (
IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
IN CONST EFI_STRING Configuration,
OUT EFI_STRING *Progress
)
{
if (Configuration == NULL || Progress == NULL) {
return EFI_INVALID_PARAMETER;
}
UINTN BlockSize = sizeof(VARIABLE_STRUCTURE);
EFI_STATUS Status = gHiiConfigRouting->ConfigToBlock(gHiiConfigRouting,
Configuration,
(UINT8*)&FormStorage,
&BlockSize,
Progress);
return Status;
}
BOOLEAN OldPasswordVerified = FALSE;
EFI_STATUS ComputeStringHash(EFI_STRING Password, UINT8* HashedPassword)
{
EFI_GUID HashGuid = EFI_HASH_ALGORITHM_SHA512_GUID;
EFI_HASH2_OUTPUT Hash;
EFI_STATUS Status = hash2Protocol->Hash(hash2Protocol,
&HashGuid,
(UINT8*)Password,
StrLen(Password)*sizeof(CHAR16),
&Hash);
if (EFI_ERROR(Status)) {
return Status;
}
CopyMem(HashedPassword, Hash.Sha512Hash, HASHED_PASSWORD_SIZE);
return EFI_SUCCESS;
}
EFI_STATUS HandlePasswordInput(EFI_STRING Password)
{
EFI_STATUS Status;
if (Password[0] == 0) {
// Form Browser checks if password exists
if (FormStorage.Password[0] != 0) {
return EFI_ALREADY_STARTED;
} else {
return EFI_SUCCESS;
}
} else {
// Form Browser sends password value
// It can be old password to check or initial/updated password to set
if (FormStorage.Password[0] == 0) {
// Set initial password
ComputeStringHash(Password, FormStorage.Password);
if (EFI_ERROR(Status)) {
return Status;
}
return EFI_SUCCESS;
}
if (!OldPasswordVerified) {
// Check old password
UINT8 TempHash[HASHED_PASSWORD_SIZE];
ComputeStringHash(Password, TempHash);
if (CompareMem(TempHash, FormStorage.Password, HASHED_PASSWORD_SIZE))
return EFI_NOT_READY;
OldPasswordVerified = TRUE;
return EFI_SUCCESS;
}
// Update password
Status = ComputeStringHash(Password, FormStorage.Password);
if (EFI_ERROR(Status)) {
return Status;
}
OldPasswordVerified = FALSE;
return EFI_SUCCESS;
}
}
STATIC
EFI_STATUS
EFIAPI
Callback (
IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
IN EFI_BROWSER_ACTION Action,
IN EFI_QUESTION_ID QuestionId,
IN UINT8 Type,
IN OUT EFI_IFR_TYPE_VALUE *Value,
OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
)
{
EFI_STATUS Status;
if ((QuestionId == KEY_PASSWORD) && (Action == EFI_BROWSER_ACTION_CHANGING)) {
if (Value->string == 0) {
return EFI_UNSUPPORTED;
}
EFI_STRING Password = HiiGetString(mHiiHandle, Value->string, "en-US");
Status = HandlePasswordInput(Password);
FreePool(Password);
return Status;
}
return EFI_UNSUPPORTED;
}
EFI_STATUS
EFIAPI
PasswordFormWithHashUnload (
EFI_HANDLE ImageHandle
)
{
hash2ServiceBinding->DestroyChild(hash2ServiceBinding, hash2ChildHandle);
if (mHiiHandle != NULL)
HiiRemovePackages(mHiiHandle);
EFI_STATUS Status = gBS->UninstallMultipleProtocolInterfaces(
mDriverHandle,
&gEfiDevicePathProtocolGuid,
&mHiiVendorDevicePath,
&gEfiHiiConfigAccessProtocolGuid,
&mConfigAccess,
NULL);
return Status;
}
EFI_STATUS
EFIAPI
PasswordFormWithHashEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
Status = gBS->LocateProtocol(&gEfiHash2ServiceBindingProtocolGuid,
NULL,
(VOID **)&hash2ServiceBinding);
if (EFI_ERROR(Status)) {
Print(L"Error! Can't locate gEfiHash2ServiceBindingProtocolGuid: %r\n", Status);
return Status;
}
Status = hash2ServiceBinding->CreateChild(hash2ServiceBinding,
&hash2ChildHandle);
if (EFI_ERROR(Status)) {
Print(L"Error! Can't create child on gEfiHash2ServiceBindingProtocolGuid: %r\n", Status);
return Status;
}
Status = gBS->OpenProtocol(hash2ChildHandle,
&gEfiHash2ProtocolGuid,
(VOID **)&hash2Protocol,
NULL,
hash2ChildHandle,
EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
if (EFI_ERROR(Status)) {
Print(L"Error! Can't open gEfiHashProtocolGuid: %r\n", Status);
return Status;
}
mConfigAccess.ExtractConfig = &ExtractConfig;
mConfigAccess.RouteConfig = &RouteConfig;
mConfigAccess.Callback = &Callback;
Status = gBS->InstallMultipleProtocolInterfaces(
&mDriverHandle,
&gEfiDevicePathProtocolGuid,
&mHiiVendorDevicePath,
&gEfiHiiConfigAccessProtocolGuid,
&mConfigAccess,
NULL
);
if (EFI_ERROR (Status)) {
return Status;
}
mHiiHandle = HiiAddPackages(
&gEfiCallerIdGuid,
mDriverHandle,
PasswordFormWithHashStrings,
FormBin,
NULL
);
if (mHiiHandle == NULL) {
gBS->UninstallMultipleProtocolInterfaces(
mDriverHandle,
&gEfiDevicePathProtocolGuid,
&mHiiVendorDevicePath,
&gEfiHiiConfigAccessProtocolGuid,
&mConfigAccess,
NULL);
return EFI_OUT_OF_RESOURCES;
}
EFI_STRING ConfigStr = HiiConstructConfigHdr(&StorageGuid, StorageName, mDriverHandle);
UINT16 DefaultId = 0;
if (!HiiSetToDefaults(ConfigStr, DefaultId)) {
Print(L"Error! Can't set default configuration #%d\n", DefaultId);
}
return EFI_SUCCESS;
}
|