]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/HddPassword/HddPasswordDxe.c
SecurityPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / HddPassword / HddPasswordDxe.c
CommitLineData
e8959f81
HW
1/** @file\r
2 HDD password driver which is used to support HDD security feature.\r
3\r
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
5\r
289b714b 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e8959f81
HW
7\r
8**/\r
9\r
10#include "HddPasswordDxe.h"\r
11\r
12EFI_GUID mHddPasswordVendorGuid = HDD_PASSWORD_CONFIG_GUID;\r
13CHAR16 mHddPasswordVendorStorageName[] = L"HDD_PASSWORD_CONFIG";\r
14LIST_ENTRY mHddPasswordConfigFormList;\r
15UINT32 mNumberOfHddDevices = 0;\r
16\r
17EFI_GUID mHddPasswordDeviceInfoGuid = HDD_PASSWORD_DEVICE_INFO_GUID;\r
18BOOLEAN mHddPasswordEndOfDxe = FALSE;\r
19HDD_PASSWORD_REQUEST_VARIABLE *mHddPasswordRequestVariable = NULL;\r
20UINTN mHddPasswordRequestVariableSize = 0;\r
21\r
22HII_VENDOR_DEVICE_PATH mHddPasswordHiiVendorDevicePath = {\r
23 {\r
24 {\r
25 HARDWARE_DEVICE_PATH,\r
26 HW_VENDOR_DP,\r
27 {\r
28 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),\r
29 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)\r
30 }\r
31 },\r
32 HDD_PASSWORD_CONFIG_GUID\r
33 },\r
34 {\r
35 END_DEVICE_PATH_TYPE,\r
36 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
37 {\r
38 (UINT8) (END_DEVICE_PATH_LENGTH),\r
39 (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)\r
40 }\r
41 }\r
42};\r
43\r
44\r
45/**\r
46 Check if the password is full zero.\r
47\r
48 @param[in] Password Points to the data buffer\r
49\r
50 @retval TRUE This password string is full zero.\r
51 @retval FALSE This password string is not full zero.\r
52\r
53**/\r
54BOOLEAN\r
55PasswordIsFullZero (\r
56 IN CHAR8 *Password\r
57 )\r
58{\r
59 UINTN Index;\r
60\r
61 for (Index = 0; Index < HDD_PASSWORD_MAX_LENGTH; Index++) {\r
62 if (Password[Index] != 0) {\r
63 return FALSE;\r
64 }\r
65 }\r
66\r
67 return TRUE;\r
68}\r
69\r
70/**\r
71 Save device info.\r
72\r
73 @param[in] ConfigFormEntry Points to HDD_PASSWORD_CONFIG_FORM_ENTRY buffer\r
74 @param[in,out] TempDevInfo Points to HDD_PASSWORD_DEVICE_INFO buffer\r
75\r
76**/\r
77VOID\r
78SaveDeviceInfo (\r
79 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry,\r
80 IN OUT HDD_PASSWORD_DEVICE_INFO *TempDevInfo\r
81 )\r
82{\r
83 TempDevInfo->Device.Bus = (UINT8) ConfigFormEntry->Bus;\r
84 TempDevInfo->Device.Device = (UINT8) ConfigFormEntry->Device;\r
85 TempDevInfo->Device.Function = (UINT8) ConfigFormEntry->Function;\r
86 TempDevInfo->Device.Port = ConfigFormEntry->Port;\r
87 TempDevInfo->Device.PortMultiplierPort = ConfigFormEntry->PortMultiplierPort;\r
88 CopyMem (TempDevInfo->Password, ConfigFormEntry->Password, HDD_PASSWORD_MAX_LENGTH);\r
89 TempDevInfo->DevicePathLength = (UINT32) GetDevicePathSize (ConfigFormEntry->DevicePath);\r
90 CopyMem (TempDevInfo->DevicePath, ConfigFormEntry->DevicePath, TempDevInfo->DevicePathLength);\r
91}\r
92\r
93/**\r
94 Build HDD password device info and save them to LockBox.\r
95\r
96 **/\r
97VOID\r
98BuildHddPasswordDeviceInfo (\r
99 VOID\r
100 )\r
101{\r
102 EFI_STATUS Status;\r
103 LIST_ENTRY *Entry;\r
104 HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry;\r
105 HDD_PASSWORD_DEVICE_INFO *DevInfo;\r
106 HDD_PASSWORD_DEVICE_INFO *TempDevInfo;\r
107 UINTN DevInfoLength;\r
108 UINT8 DummyData;\r
109 BOOLEAN S3InitDevicesExist;\r
110 UINTN S3InitDevicesLength;\r
111 EFI_DEVICE_PATH_PROTOCOL *S3InitDevices;\r
112 EFI_DEVICE_PATH_PROTOCOL *S3InitDevicesBak;\r
113\r
114 //\r
115 // Build HDD password device info and save them to LockBox.\r
116 //\r
117 DevInfoLength = 0;\r
118 EFI_LIST_FOR_EACH (Entry, &mHddPasswordConfigFormList) {\r
119 ConfigFormEntry = BASE_CR (Entry, HDD_PASSWORD_CONFIG_FORM_ENTRY, Link);\r
120\r
121 //\r
122 // 1. Handle device which already set password.\r
123 // 2. When request to send freeze comamnd, driver also needs to handle device\r
124 // which support security feature.\r
125 //\r
126 if ((!PasswordIsFullZero (ConfigFormEntry->Password)) ||\r
127 ((ConfigFormEntry->IfrData.SecurityStatus.Supported != 0) &&\r
128 (ConfigFormEntry->IfrData.SecurityStatus.Enabled == 0))) {\r
129 DevInfoLength += sizeof (HDD_PASSWORD_DEVICE_INFO) +\r
130 GetDevicePathSize (ConfigFormEntry->DevicePath);\r
131 }\r
132 }\r
133\r
134 if (DevInfoLength == 0) {\r
135 return;\r
136 }\r
137\r
138 S3InitDevicesLength = sizeof (DummyData);\r
139 Status = RestoreLockBox (\r
140 &gS3StorageDeviceInitListGuid,\r
141 &DummyData,\r
142 &S3InitDevicesLength\r
143 );\r
144 ASSERT ((Status == EFI_NOT_FOUND) || (Status == EFI_BUFFER_TOO_SMALL));\r
145 if (Status == EFI_NOT_FOUND) {\r
146 S3InitDevices = NULL;\r
147 S3InitDevicesExist = FALSE;\r
148 } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
149 S3InitDevices = AllocatePool (S3InitDevicesLength);\r
150 ASSERT (S3InitDevices != NULL);\r
151\r
152 Status = RestoreLockBox (\r
153 &gS3StorageDeviceInitListGuid,\r
154 S3InitDevices,\r
155 &S3InitDevicesLength\r
156 );\r
157 ASSERT_EFI_ERROR (Status);\r
158 S3InitDevicesExist = TRUE;\r
159 } else {\r
160 return;\r
161 }\r
162\r
163 DevInfo = AllocateZeroPool (DevInfoLength);\r
164 ASSERT (DevInfo != NULL);\r
165\r
166 TempDevInfo = DevInfo;\r
167 EFI_LIST_FOR_EACH (Entry, &mHddPasswordConfigFormList) {\r
168 ConfigFormEntry = BASE_CR (Entry, HDD_PASSWORD_CONFIG_FORM_ENTRY, Link);\r
169\r
170 if ((!PasswordIsFullZero (ConfigFormEntry->Password)) ||\r
171 ((ConfigFormEntry->IfrData.SecurityStatus.Supported != 0) &&\r
172 (ConfigFormEntry->IfrData.SecurityStatus.Enabled == 0))) {\r
173 SaveDeviceInfo (ConfigFormEntry, TempDevInfo);\r
174\r
175 S3InitDevicesBak = S3InitDevices;\r
176 S3InitDevices = AppendDevicePathInstance (\r
177 S3InitDevicesBak,\r
178 ConfigFormEntry->DevicePath\r
179 );\r
180 if (S3InitDevicesBak != NULL) {\r
181 FreePool (S3InitDevicesBak);\r
182 }\r
183 ASSERT (S3InitDevices != NULL);\r
184\r
185 TempDevInfo = (HDD_PASSWORD_DEVICE_INFO *) ((UINTN)TempDevInfo +\r
186 sizeof (HDD_PASSWORD_DEVICE_INFO) +\r
187 TempDevInfo->DevicePathLength);\r
188 }\r
189 }\r
190\r
191 Status = SaveLockBox (\r
192 &mHddPasswordDeviceInfoGuid,\r
193 DevInfo,\r
194 DevInfoLength\r
195 );\r
196 ASSERT_EFI_ERROR (Status);\r
197\r
198 Status = SetLockBoxAttributes (\r
199 &mHddPasswordDeviceInfoGuid,\r
200 LOCK_BOX_ATTRIBUTE_RESTORE_IN_S3_ONLY\r
201 );\r
202 ASSERT_EFI_ERROR (Status);\r
203\r
204 S3InitDevicesLength = GetDevicePathSize (S3InitDevices);\r
205 if (S3InitDevicesExist) {\r
206 Status = UpdateLockBox (\r
207 &gS3StorageDeviceInitListGuid,\r
208 0,\r
209 S3InitDevices,\r
210 S3InitDevicesLength\r
211 );\r
212 ASSERT_EFI_ERROR (Status);\r
213 } else {\r
214 Status = SaveLockBox (\r
215 &gS3StorageDeviceInitListGuid,\r
216 S3InitDevices,\r
217 S3InitDevicesLength\r
218 );\r
219 ASSERT_EFI_ERROR (Status);\r
220\r
221 Status = SetLockBoxAttributes (\r
222 &gS3StorageDeviceInitListGuid,\r
223 LOCK_BOX_ATTRIBUTE_RESTORE_IN_S3_ONLY\r
224 );\r
225 ASSERT_EFI_ERROR (Status);\r
226 }\r
227\r
228 ZeroMem (DevInfo, DevInfoLength);\r
229 FreePool (DevInfo);\r
230 FreePool (S3InitDevices);\r
231}\r
232\r
233/**\r
234 Send freeze lock cmd through Ata Pass Thru Protocol.\r
235\r
236 @param[in] AtaPassThru The pointer to the ATA_PASS_THRU protocol.\r
237 @param[in] Port The port number of the ATA device to send the command.\r
238 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
239 If there is no port multiplier, then specify 0xFFFF.\r
240\r
241 @retval EFI_SUCCESS Successful to send freeze lock cmd.\r
242 @retval EFI_INVALID_PARAMETER The parameter passed-in is invalid.\r
243 @retval EFI_OUT_OF_RESOURCES Not enough memory to send freeze lock cmd.\r
244 @retval EFI_DEVICE_ERROR Can not send freeze lock cmd.\r
245\r
246**/\r
247EFI_STATUS\r
248FreezeLockDevice (\r
249 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
250 IN UINT16 Port,\r
251 IN UINT16 PortMultiplierPort\r
252 )\r
253{\r
254 EFI_STATUS Status;\r
255 EFI_ATA_COMMAND_BLOCK Acb;\r
256 EFI_ATA_STATUS_BLOCK *Asb;\r
257 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet;\r
258\r
259 if (AtaPassThru == NULL) {\r
260 return EFI_INVALID_PARAMETER;\r
261 }\r
262\r
263 //\r
264 // The 'Asb' field (a pointer to the EFI_ATA_STATUS_BLOCK structure) in\r
265 // EFI_ATA_PASS_THRU_COMMAND_PACKET is required to be aligned specified by\r
266 // the 'IoAlign' field in the EFI_ATA_PASS_THRU_MODE structure. Meanwhile,\r
267 // the structure EFI_ATA_STATUS_BLOCK is composed of only UINT8 fields, so it\r
268 // may not be aligned when allocated on stack for some compilers. Hence, we\r
269 // use the API AllocateAlignedPages to ensure this structure is properly\r
270 // aligned.\r
271 //\r
272 Asb = AllocateAlignedPages (\r
273 EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)),\r
274 AtaPassThru->Mode->IoAlign\r
275 );\r
276 if (Asb == NULL) {\r
277 return EFI_OUT_OF_RESOURCES;\r
278 }\r
279\r
280 //\r
281 // Prepare for ATA command block.\r
282 //\r
283 ZeroMem (&Acb, sizeof (Acb));\r
284 ZeroMem (Asb, sizeof (EFI_ATA_STATUS_BLOCK));\r
285 Acb.AtaCommand = ATA_SECURITY_FREEZE_LOCK_CMD;\r
286 Acb.AtaDeviceHead = (UINT8) (PortMultiplierPort == 0xFFFF ? 0 : (PortMultiplierPort << 4));\r
287\r
288 //\r
289 // Prepare for ATA pass through packet.\r
290 //\r
291 ZeroMem (&Packet, sizeof (Packet));\r
292 Packet.Protocol = EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA;\r
293 Packet.Length = EFI_ATA_PASS_THRU_LENGTH_NO_DATA_TRANSFER;\r
294 Packet.Asb = Asb;\r
295 Packet.Acb = &Acb;\r
296 Packet.Timeout = ATA_TIMEOUT;\r
297\r
298 Status = AtaPassThru->PassThru (\r
299 AtaPassThru,\r
300 Port,\r
301 PortMultiplierPort,\r
302 &Packet,\r
303 NULL\r
304 );\r
305 if (!EFI_ERROR (Status) &&\r
306 ((Asb->AtaStatus & ATA_STSREG_ERR) != 0) &&\r
307 ((Asb->AtaError & ATA_ERRREG_ABRT) != 0)) {\r
308 Status = EFI_DEVICE_ERROR;\r
309 }\r
310\r
311 FreeAlignedPages (Asb, EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)));\r
312\r
313 DEBUG ((DEBUG_INFO, "%a() - %r\n", __FUNCTION__, Status));\r
314 return Status;\r
315}\r
316\r
317/**\r
318 Get attached harddisk identify data through Ata Pass Thru Protocol.\r
319\r
320 @param[in] AtaPassThru The pointer to the ATA_PASS_THRU protocol.\r
321 @param[in] Port The port number of the ATA device to send the command.\r
322 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
323 If there is no port multiplier, then specify 0xFFFF.\r
324 @param[in] IdentifyData The buffer to store identify data.\r
325\r
326 @retval EFI_SUCCESS Successful to get identify data.\r
327 @retval EFI_INVALID_PARAMETER The parameter passed-in is invalid.\r
328 @retval EFI_OUT_OF_RESOURCES Not enough memory to get identify data.\r
329 @retval EFI_DEVICE_ERROR Can not get identify data.\r
330\r
331**/\r
332EFI_STATUS\r
333GetHddDeviceIdentifyData (\r
334 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
335 IN UINT16 Port,\r
336 IN UINT16 PortMultiplierPort,\r
337 IN ATA_IDENTIFY_DATA *IdentifyData\r
338 )\r
339{\r
340 EFI_STATUS Status;\r
341 EFI_ATA_COMMAND_BLOCK Acb;\r
342 EFI_ATA_STATUS_BLOCK *Asb;\r
343 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet;\r
344\r
345 if ((AtaPassThru == NULL) || (IdentifyData == NULL)) {\r
346 return EFI_INVALID_PARAMETER;\r
347 }\r
348\r
349 //\r
350 // The 'Asb' field (a pointer to the EFI_ATA_STATUS_BLOCK structure) in\r
351 // EFI_ATA_PASS_THRU_COMMAND_PACKET is required to be aligned specified by\r
352 // the 'IoAlign' field in the EFI_ATA_PASS_THRU_MODE structure. Meanwhile,\r
353 // the structure EFI_ATA_STATUS_BLOCK is composed of only UINT8 fields, so it\r
354 // may not be aligned when allocated on stack for some compilers. Hence, we\r
355 // use the API AllocateAlignedPages to ensure this structure is properly\r
356 // aligned.\r
357 //\r
358 Asb = AllocateAlignedPages (\r
359 EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)),\r
360 AtaPassThru->Mode->IoAlign\r
361 );\r
362 if (Asb == NULL) {\r
363 return EFI_OUT_OF_RESOURCES;\r
364 }\r
365\r
366 //\r
367 // Prepare for ATA command block.\r
368 //\r
369 ZeroMem (&Acb, sizeof (Acb));\r
370 ZeroMem (Asb, sizeof (EFI_ATA_STATUS_BLOCK));\r
371 Acb.AtaCommand = ATA_CMD_IDENTIFY_DRIVE;\r
372 Acb.AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (PortMultiplierPort == 0xFFFF ? 0 : (PortMultiplierPort << 4)));\r
373\r
374 //\r
375 // Prepare for ATA pass through packet.\r
376 //\r
377 ZeroMem (&Packet, sizeof (Packet));\r
378 Packet.Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN;\r
379 Packet.Length = EFI_ATA_PASS_THRU_LENGTH_BYTES | EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;\r
380 Packet.Asb = Asb;\r
381 Packet.Acb = &Acb;\r
382 Packet.InDataBuffer = IdentifyData;\r
383 Packet.InTransferLength = sizeof (ATA_IDENTIFY_DATA);\r
384 Packet.Timeout = ATA_TIMEOUT;\r
385\r
386 Status = AtaPassThru->PassThru (\r
387 AtaPassThru,\r
388 Port,\r
389 PortMultiplierPort,\r
390 &Packet,\r
391 NULL\r
392 );\r
393\r
394 FreeAlignedPages (Asb, EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)));\r
395\r
396 return Status;\r
397}\r
398\r
399/**\r
400 Parse security status according to identify data.\r
401\r
402 @param[in] IdentifyData The buffer to store identify data.\r
403 @param[in, out] IfrData IFR data to hold security status.\r
404\r
405**/\r
406VOID\r
407GetHddPasswordSecurityStatus (\r
408 IN ATA_IDENTIFY_DATA *IdentifyData,\r
409 IN OUT HDD_PASSWORD_CONFIG *IfrData\r
410 )\r
411{\r
412 IfrData->SecurityStatus.Supported = (IdentifyData->command_set_supported_82 & BIT1) ? 1 : 0;\r
413 IfrData->SecurityStatus.Enabled = (IdentifyData->security_status & BIT1) ? 1 : 0;\r
414 IfrData->SecurityStatus.Locked = (IdentifyData->security_status & BIT2) ? 1 : 0;\r
415 IfrData->SecurityStatus.Frozen = (IdentifyData->security_status & BIT3) ? 1 : 0;\r
416 IfrData->SecurityStatus.UserPasswordStatus = IfrData->SecurityStatus.Enabled;\r
417 IfrData->SecurityStatus.MasterPasswordStatus = IfrData->SecurityStatus.Supported;\r
418\r
419 DEBUG ((DEBUG_INFO, "IfrData->SecurityStatus.Supported = %x\n", IfrData->SecurityStatus.Supported));\r
420 DEBUG ((DEBUG_INFO, "IfrData->SecurityStatus.Enabled = %x\n", IfrData->SecurityStatus.Enabled));\r
421 DEBUG ((DEBUG_INFO, "IfrData->SecurityStatus.Locked = %x\n", IfrData->SecurityStatus.Locked));\r
422 DEBUG ((DEBUG_INFO, "IfrData->SecurityStatus.Frozen = %x\n", IfrData->SecurityStatus.Frozen));\r
423 DEBUG ((DEBUG_INFO, "IfrData->SecurityStatus.UserPasswordStatus = %x\n", IfrData->SecurityStatus.UserPasswordStatus));\r
424 DEBUG ((DEBUG_INFO, "IfrData->SecurityStatus.MasterPasswordStatus = %x\n", IfrData->SecurityStatus.MasterPasswordStatus));\r
425}\r
426\r
427/**\r
428 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
429\r
430 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
431\r
432 @param Event Event whose notification function is being invoked.\r
433 @param Context Pointer to the notification function's context.\r
434\r
435**/\r
436VOID\r
437EFIAPI\r
438HddPasswordEndOfDxeEventNotify (\r
439 EFI_EVENT Event,\r
440 VOID *Context\r
441 )\r
442{\r
443 LIST_ENTRY *Entry;\r
444 HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry;\r
445 EFI_STATUS Status;\r
446 ATA_IDENTIFY_DATA IdentifyData;\r
447\r
448 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
449\r
450 mHddPasswordEndOfDxe = TRUE;\r
451\r
452 if (mHddPasswordRequestVariable != NULL) {\r
453 //\r
454 // Free the HDD password request variable buffer here\r
455 // as the HDD password requests should have been processed.\r
456 //\r
457 FreePool (mHddPasswordRequestVariable);\r
458 mHddPasswordRequestVariable = NULL;\r
459 mHddPasswordRequestVariableSize = 0;\r
460 }\r
461\r
462 //\r
463 // If no any device, return directly.\r
464 //\r
465 if (IsListEmpty (&mHddPasswordConfigFormList)) {\r
466 gBS->CloseEvent (Event);\r
467 return;\r
468 }\r
469\r
470 BuildHddPasswordDeviceInfo ();\r
471\r
472 //\r
473 // Zero passsword and freeze lock device.\r
474 //\r
475 EFI_LIST_FOR_EACH (Entry, &mHddPasswordConfigFormList) {\r
476 ConfigFormEntry = BASE_CR (Entry, HDD_PASSWORD_CONFIG_FORM_ENTRY, Link);\r
477\r
478 ZeroMem (ConfigFormEntry->Password, HDD_PASSWORD_MAX_LENGTH);\r
479\r
480 //\r
481 // Check whether need send freeze lock command.\r
482 // Below device will be froze:\r
483 // 1. Device not enable password.\r
484 // 2. Device enable password and unlocked.\r
485 //\r
486 if ((ConfigFormEntry->IfrData.SecurityStatus.Supported != 0) &&\r
487 (ConfigFormEntry->IfrData.SecurityStatus.Locked == 0) &&\r
488 (ConfigFormEntry->IfrData.SecurityStatus.Frozen == 0)) {\r
489 Status = FreezeLockDevice (ConfigFormEntry->AtaPassThru, ConfigFormEntry->Port, ConfigFormEntry->PortMultiplierPort);\r
490 DEBUG ((DEBUG_INFO, "FreezeLockDevice return %r!\n", Status));\r
491 Status = GetHddDeviceIdentifyData (\r
492 ConfigFormEntry->AtaPassThru,\r
493 ConfigFormEntry->Port,\r
494 ConfigFormEntry->PortMultiplierPort,\r
495 &IdentifyData\r
496 );\r
497 GetHddPasswordSecurityStatus (&IdentifyData, &ConfigFormEntry->IfrData);\r
498 }\r
499 }\r
500\r
501 DEBUG ((DEBUG_INFO, "%a() - exit\n", __FUNCTION__));\r
502\r
503 gBS->CloseEvent (Event);\r
504}\r
505\r
506/**\r
507 Generate Salt value.\r
508\r
509 @param[in, out] SaltValue Points to the salt buffer, 32 bytes\r
510\r
511**/\r
512VOID\r
513GenSalt (\r
514 IN OUT UINT8 *SaltValue\r
515 )\r
516{\r
517 RandomSeed (NULL, 0);\r
518 RandomBytes (SaltValue, PASSWORD_SALT_SIZE);\r
519}\r
520\r
521/**\r
522 Hash the data to get credential.\r
523\r
524 @param[in] Buffer Points to the data buffer\r
525 @param[in] BufferSize Buffer size\r
526 @param[in] SaltValue Points to the salt buffer, 32 bytes\r
527 @param[out] Credential Points to the hashed result\r
528\r
529 @retval TRUE Hash the data successfully.\r
530 @retval FALSE Failed to hash the data.\r
531\r
532**/\r
533BOOLEAN\r
534GenerateCredential (\r
535 IN UINT8 *Buffer,\r
536 IN UINTN BufferSize,\r
537 IN UINT8 *SaltValue,\r
538 OUT UINT8 *Credential\r
539 )\r
540{\r
541 BOOLEAN Status;\r
542 UINTN HashSize;\r
543 VOID *Hash;\r
544 VOID *HashData;\r
545\r
546 Hash = NULL;\r
547 HashData = NULL;\r
548 Status = FALSE;\r
549\r
550 HashSize = Sha256GetContextSize ();\r
551 Hash = AllocateZeroPool (HashSize);\r
552 ASSERT (Hash != NULL);\r
553 if (Hash == NULL) {\r
554 goto Done;\r
555 }\r
556\r
557 Status = Sha256Init (Hash);\r
558 if (!Status) {\r
559 goto Done;\r
560 }\r
561\r
562 HashData = AllocateZeroPool (PASSWORD_SALT_SIZE + BufferSize);\r
563 ASSERT (HashData != NULL);\r
564 if (HashData == NULL) {\r
565 goto Done;\r
566 }\r
567\r
568 CopyMem (HashData, SaltValue, PASSWORD_SALT_SIZE);\r
569 CopyMem ((UINT8 *) HashData + PASSWORD_SALT_SIZE, Buffer, BufferSize);\r
570\r
571 Status = Sha256Update (Hash, HashData, PASSWORD_SALT_SIZE + BufferSize);\r
572 if (!Status) {\r
573 goto Done;\r
574 }\r
575\r
576 Status = Sha256Final (Hash, Credential);\r
577\r
578Done:\r
579 if (Hash != NULL) {\r
580 FreePool (Hash);\r
581 }\r
582 if (HashData != NULL) {\r
583 ZeroMem (HashData, PASSWORD_SALT_SIZE + BufferSize);\r
584 FreePool (HashData);\r
585 }\r
586 return Status;\r
587}\r
588\r
589/**\r
590 Save HDD password variable that will be used to validate HDD password\r
591 when the device is at frozen state.\r
592\r
593 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
594 @param[in] Password The hdd password of attached ATA device.\r
595\r
596**/\r
597VOID\r
598SaveHddPasswordVariable (\r
599 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry,\r
600 IN CHAR8 *Password\r
601 )\r
602{\r
603 EFI_STATUS Status;\r
604 HDD_PASSWORD_VARIABLE *TempVariable;\r
605 UINTN TempVariableSize;\r
606 HDD_PASSWORD_VARIABLE *NextNode;\r
607 HDD_PASSWORD_VARIABLE *Variable;\r
608 UINTN VariableSize;\r
609 HDD_PASSWORD_VARIABLE *NewVariable;\r
610 UINTN NewVariableSize;\r
611 BOOLEAN Delete;\r
612 BOOLEAN HashOk;\r
613 UINT8 HashData[SHA256_DIGEST_SIZE];\r
614 UINT8 SaltData[PASSWORD_SALT_SIZE];\r
615\r
616 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
617\r
618 Delete = FALSE;\r
619 if (!PasswordIsFullZero (Password)) {\r
620 //\r
621 // It is Set/Update HDD Password.\r
622 //\r
623 ZeroMem (HashData, sizeof (HashData));\r
624 ZeroMem (SaltData, sizeof (SaltData));\r
625 GenSalt (SaltData);\r
626 HashOk = GenerateCredential ((UINT8 *) Password, HDD_PASSWORD_MAX_LENGTH, SaltData, HashData);\r
627 if (!HashOk) {\r
628 DEBUG ((DEBUG_INFO, "GenerateCredential failed\n"));\r
629 return;\r
630 }\r
631 } else {\r
632 //\r
633 // It is Disable HDD Password.\r
634 // Go to delete the variable node for the HDD password device.\r
635 //\r
636 Delete = TRUE;\r
637 }\r
638\r
639 Variable = NULL;\r
640 VariableSize = 0;\r
641 NewVariable = NULL;\r
642 NewVariableSize = 0;\r
643\r
644 Status = GetVariable2 (\r
645 HDD_PASSWORD_VARIABLE_NAME,\r
646 &mHddPasswordVendorGuid,\r
647 (VOID **) &Variable,\r
648 &VariableSize\r
649 );\r
650 if (Delete) {\r
651 if (!EFI_ERROR (Status) && (Variable != NULL)) {\r
652 TempVariable = Variable;\r
653 TempVariableSize = VariableSize;\r
654 while (TempVariableSize >= sizeof (HDD_PASSWORD_VARIABLE)) {\r
655 if ((TempVariable->Device.Bus == ConfigFormEntry->Bus) &&\r
656 (TempVariable->Device.Device == ConfigFormEntry->Device) &&\r
657 (TempVariable->Device.Function == ConfigFormEntry->Function) &&\r
658 (TempVariable->Device.Port == ConfigFormEntry->Port) &&\r
659 (TempVariable->Device.PortMultiplierPort == ConfigFormEntry->PortMultiplierPort)) {\r
660 //\r
661 // Found the node for the HDD password device.\r
662 // Delete the node.\r
663 //\r
664 NextNode = TempVariable + 1;\r
665 CopyMem (TempVariable, NextNode, (UINTN) Variable + VariableSize - (UINTN) NextNode);\r
666 NewVariable = Variable;\r
667 NewVariableSize = VariableSize - sizeof (HDD_PASSWORD_VARIABLE);\r
668 break;\r
669 }\r
670 TempVariableSize -= sizeof (HDD_PASSWORD_VARIABLE);\r
671 TempVariable += 1;\r
672 }\r
673 if (NewVariable == NULL) {\r
674 DEBUG ((DEBUG_INFO, "The variable node for the HDD password device is not found\n"));\r
675 }\r
676 } else {\r
677 DEBUG ((DEBUG_INFO, "HddPassword variable get failed (%r)\n", Status));\r
678 }\r
679 } else {\r
680 if (!EFI_ERROR (Status) && (Variable != NULL)) {\r
681 TempVariable = Variable;\r
682 TempVariableSize = VariableSize;\r
683 while (TempVariableSize >= sizeof (HDD_PASSWORD_VARIABLE)) {\r
684 if ((TempVariable->Device.Bus == ConfigFormEntry->Bus) &&\r
685 (TempVariable->Device.Device == ConfigFormEntry->Device) &&\r
686 (TempVariable->Device.Function == ConfigFormEntry->Function) &&\r
687 (TempVariable->Device.Port == ConfigFormEntry->Port) &&\r
688 (TempVariable->Device.PortMultiplierPort == ConfigFormEntry->PortMultiplierPort)) {\r
689 //\r
690 // Found the node for the HDD password device.\r
691 // Update the node.\r
692 //\r
693 CopyMem (TempVariable->PasswordHash, HashData, sizeof (HashData));\r
694 CopyMem (TempVariable->PasswordSalt, SaltData, sizeof (SaltData));\r
695 NewVariable = Variable;\r
696 NewVariableSize = VariableSize;\r
697 break;\r
698 }\r
699 TempVariableSize -= sizeof (HDD_PASSWORD_VARIABLE);\r
700 TempVariable += 1;\r
701 }\r
702 if (NewVariable == NULL) {\r
703 //\r
704 // The node for the HDD password device is not found.\r
705 // Create node for the HDD password device.\r
706 //\r
707 NewVariableSize = VariableSize + sizeof (HDD_PASSWORD_VARIABLE);\r
708 NewVariable = AllocateZeroPool (NewVariableSize);\r
709 ASSERT (NewVariable != NULL);\r
710 CopyMem (NewVariable, Variable, VariableSize);\r
711 TempVariable = (HDD_PASSWORD_VARIABLE *) ((UINTN) NewVariable + VariableSize);\r
712 TempVariable->Device.Bus = (UINT8) ConfigFormEntry->Bus;\r
713 TempVariable->Device.Device = (UINT8) ConfigFormEntry->Device;\r
714 TempVariable->Device.Function = (UINT8) ConfigFormEntry->Function;\r
715 TempVariable->Device.Port = ConfigFormEntry->Port;\r
716 TempVariable->Device.PortMultiplierPort = ConfigFormEntry->PortMultiplierPort;\r
717 CopyMem (TempVariable->PasswordHash, HashData, sizeof (HashData));\r
718 CopyMem (TempVariable->PasswordSalt, SaltData, sizeof (SaltData));\r
719 }\r
720 } else {\r
721 NewVariableSize = sizeof (HDD_PASSWORD_VARIABLE);\r
722 NewVariable = AllocateZeroPool (NewVariableSize);\r
723 ASSERT (NewVariable != NULL);\r
724 NewVariable->Device.Bus = (UINT8) ConfigFormEntry->Bus;\r
725 NewVariable->Device.Device = (UINT8) ConfigFormEntry->Device;\r
726 NewVariable->Device.Function = (UINT8) ConfigFormEntry->Function;\r
727 NewVariable->Device.Port = ConfigFormEntry->Port;\r
728 NewVariable->Device.PortMultiplierPort = ConfigFormEntry->PortMultiplierPort;\r
729 CopyMem (NewVariable->PasswordHash, HashData, sizeof (HashData));\r
730 CopyMem (NewVariable->PasswordSalt, SaltData, sizeof (SaltData));\r
731 }\r
732 }\r
733\r
734 if (NewVariable != NULL) {\r
735 Status = gRT->SetVariable (\r
736 HDD_PASSWORD_VARIABLE_NAME,\r
737 &mHddPasswordVendorGuid,\r
738 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
739 NewVariableSize,\r
740 NewVariable\r
741 );\r
742 if (EFI_ERROR (Status)) {\r
743 DEBUG ((DEBUG_INFO, "HddPassword variable set failed (%r)\n", Status));\r
744 }\r
745 }\r
746\r
747 if (NewVariable != Variable) {\r
748 FreePool (NewVariable);\r
749 }\r
750 if (Variable != NULL) {\r
751 FreePool (Variable);\r
752 }\r
753\r
754 DEBUG ((DEBUG_INFO, "%a() - exit\n", __FUNCTION__));\r
755}\r
756\r
757/**\r
758 Get saved HDD password variable that will be used to validate HDD password\r
759 when the device is at frozen state.\r
760\r
761 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
762 @param[out] HddPasswordVariable The variable node for the HDD password device.\r
763\r
764 @retval TRUE The variable node for the HDD password device is found and returned.\r
765 @retval FALSE The variable node for the HDD password device is not found.\r
766\r
767**/\r
768BOOLEAN\r
769GetSavedHddPasswordVariable (\r
770 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry,\r
771 OUT HDD_PASSWORD_VARIABLE *HddPasswordVariable\r
772 )\r
773{\r
774 EFI_STATUS Status;\r
775 HDD_PASSWORD_VARIABLE *TempVariable;\r
776 HDD_PASSWORD_VARIABLE *Variable;\r
777 UINTN VariableSize;\r
778 BOOLEAN Found;\r
779\r
780 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
781\r
782 Variable = NULL;\r
783 VariableSize = 0;\r
784\r
785 Status = GetVariable2 (\r
786 HDD_PASSWORD_VARIABLE_NAME,\r
787 &mHddPasswordVendorGuid,\r
788 (VOID **) &Variable,\r
789 &VariableSize\r
790 );\r
791 if (EFI_ERROR (Status) || (Variable == NULL)) {\r
792 DEBUG ((DEBUG_INFO, "HddPassword variable get failed (%r)\n", Status));\r
793 return FALSE;\r
794 }\r
795\r
796 Found = FALSE;\r
797 TempVariable = Variable;\r
798 while (VariableSize >= sizeof (HDD_PASSWORD_VARIABLE)) {\r
799 if ((TempVariable->Device.Bus == ConfigFormEntry->Bus) &&\r
800 (TempVariable->Device.Device == ConfigFormEntry->Device) &&\r
801 (TempVariable->Device.Function == ConfigFormEntry->Function) &&\r
802 (TempVariable->Device.Port == ConfigFormEntry->Port) &&\r
803 (TempVariable->Device.PortMultiplierPort == ConfigFormEntry->PortMultiplierPort)) {\r
804 //\r
805 // Found the node for the HDD password device.\r
806 // Get the node.\r
807 //\r
808 CopyMem (HddPasswordVariable, TempVariable, sizeof (HDD_PASSWORD_VARIABLE));\r
809 Found = TRUE;\r
810 break;\r
811 }\r
812 VariableSize -= sizeof (HDD_PASSWORD_VARIABLE);\r
813 TempVariable += 1;\r
814 }\r
815\r
816 FreePool (Variable);\r
817\r
818 if (!Found) {\r
819 DEBUG ((DEBUG_INFO, "The variable node for the HDD password device is not found\n"));\r
820 }\r
821\r
822 DEBUG ((DEBUG_INFO, "%a() - exit\n", __FUNCTION__));\r
823\r
824 return Found;\r
825}\r
826\r
827/**\r
828 Use saved HDD password variable to validate HDD password\r
829 when the device is at frozen state.\r
830\r
831 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
832 @param[in] Password The hdd password of attached ATA device.\r
833\r
834 @retval EFI_SUCCESS Pass to validate the HDD password.\r
835 @retval EFI_NOT_FOUND The variable node for the HDD password device is not found.\r
836 @retval EFI_DEVICE_ERROR Failed to generate credential for the HDD password.\r
837 @retval EFI_INVALID_PARAMETER Failed to validate the HDD password.\r
838\r
839**/\r
840EFI_STATUS\r
841ValidateHddPassword (\r
842 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry,\r
843 IN CHAR8 *Password\r
844 )\r
845{\r
846 EFI_STATUS Status;\r
847 HDD_PASSWORD_VARIABLE HddPasswordVariable;\r
848 BOOLEAN HashOk;\r
849 UINT8 HashData[SHA256_DIGEST_SIZE];\r
850\r
851 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
852\r
853 if (!GetSavedHddPasswordVariable (ConfigFormEntry, &HddPasswordVariable)) {\r
854 DEBUG ((DEBUG_INFO, "GetSavedHddPasswordVariable failed\n"));\r
855 return EFI_NOT_FOUND;\r
856 }\r
857\r
858 ZeroMem (HashData, sizeof (HashData));\r
859 HashOk = GenerateCredential ((UINT8 *) Password, HDD_PASSWORD_MAX_LENGTH, HddPasswordVariable.PasswordSalt, HashData);\r
860 if (!HashOk) {\r
861 DEBUG ((DEBUG_INFO, "GenerateCredential failed\n"));\r
862 return EFI_DEVICE_ERROR;\r
863 }\r
864\r
865 if (CompareMem (HddPasswordVariable.PasswordHash, HashData, sizeof (HashData)) != 0) {\r
866 Status = EFI_INVALID_PARAMETER;\r
867 } else {\r
868 Status = EFI_SUCCESS;\r
869 }\r
870\r
871 DEBUG ((DEBUG_INFO, "%a() - exit (%r)\n", __FUNCTION__, Status));\r
872 return Status;\r
873}\r
874\r
875/**\r
876 Send unlock hdd password cmd through Ata Pass Thru Protocol.\r
877\r
878 @param[in] AtaPassThru The pointer to the ATA_PASS_THRU protocol.\r
879 @param[in] Port The port number of the ATA device to send the command.\r
880 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
881 If there is no port multiplier, then specify 0xFFFF.\r
882 @param[in] Identifier The identifier to set user or master password.\r
883 @param[in] Password The hdd password of attached ATA device.\r
884\r
885 @retval EFI_SUCCESS Successful to send unlock hdd password cmd.\r
886 @retval EFI_INVALID_PARAMETER The parameter passed-in is invalid.\r
887 @retval EFI_OUT_OF_RESOURCES Not enough memory to send unlock hdd password cmd.\r
888 @retval EFI_DEVICE_ERROR Can not send unlock hdd password cmd.\r
889\r
890**/\r
891EFI_STATUS\r
892UnlockHddPassword (\r
893 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
894 IN UINT16 Port,\r
895 IN UINT16 PortMultiplierPort,\r
896 IN CHAR8 Identifier,\r
897 IN CHAR8 *Password\r
898 )\r
899{\r
900 EFI_STATUS Status;\r
901 EFI_ATA_COMMAND_BLOCK Acb;\r
902 EFI_ATA_STATUS_BLOCK *Asb;\r
903 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet;\r
904 UINT8 Buffer[HDD_PAYLOAD];\r
905\r
906 if ((AtaPassThru == NULL) || (Password == NULL)) {\r
907 return EFI_INVALID_PARAMETER;\r
908 }\r
909\r
910 //\r
911 // The 'Asb' field (a pointer to the EFI_ATA_STATUS_BLOCK structure) in\r
912 // EFI_ATA_PASS_THRU_COMMAND_PACKET is required to be aligned specified by\r
913 // the 'IoAlign' field in the EFI_ATA_PASS_THRU_MODE structure. Meanwhile,\r
914 // the structure EFI_ATA_STATUS_BLOCK is composed of only UINT8 fields, so it\r
915 // may not be aligned when allocated on stack for some compilers. Hence, we\r
916 // use the API AllocateAlignedPages to ensure this structure is properly\r
917 // aligned.\r
918 //\r
919 Asb = AllocateAlignedPages (\r
920 EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)),\r
921 AtaPassThru->Mode->IoAlign\r
922 );\r
923 if (Asb == NULL) {\r
924 return EFI_OUT_OF_RESOURCES;\r
925 }\r
926\r
927 //\r
928 // Prepare for ATA command block.\r
929 //\r
930 ZeroMem (&Acb, sizeof (Acb));\r
931 ZeroMem (Asb, sizeof (EFI_ATA_STATUS_BLOCK));\r
932 Acb.AtaCommand = ATA_SECURITY_UNLOCK_CMD;\r
933 Acb.AtaDeviceHead = (UINT8) (PortMultiplierPort == 0xFFFF ? 0 : (PortMultiplierPort << 4));\r
934\r
935 //\r
936 // Prepare for ATA pass through packet.\r
937 //\r
938 ZeroMem (&Packet, sizeof (Packet));\r
939 Packet.Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT;\r
940 Packet.Length = EFI_ATA_PASS_THRU_LENGTH_BYTES;\r
941 Packet.Asb = Asb;\r
942 Packet.Acb = &Acb;\r
943\r
944 ((CHAR16 *) Buffer)[0] = Identifier & BIT0;\r
945 CopyMem (&((CHAR16 *) Buffer)[1], Password, HDD_PASSWORD_MAX_LENGTH);\r
946\r
947 Packet.OutDataBuffer = Buffer;\r
948 Packet.OutTransferLength = sizeof (Buffer);\r
949 Packet.Timeout = ATA_TIMEOUT;\r
950\r
951 Status = AtaPassThru->PassThru (\r
952 AtaPassThru,\r
953 Port,\r
954 PortMultiplierPort,\r
955 &Packet,\r
956 NULL\r
957 );\r
958 if (!EFI_ERROR (Status) &&\r
959 ((Asb->AtaStatus & ATA_STSREG_ERR) != 0) &&\r
960 ((Asb->AtaError & ATA_ERRREG_ABRT) != 0)) {\r
961 Status = EFI_DEVICE_ERROR;\r
962 }\r
963\r
964 FreeAlignedPages (Asb, EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)));\r
965\r
966 ZeroMem (Buffer, sizeof (Buffer));\r
967\r
968 DEBUG ((DEBUG_INFO, "%a() - %r\n", __FUNCTION__, Status));\r
969 return Status;\r
970}\r
971\r
972/**\r
973 Send disable hdd password cmd through Ata Pass Thru Protocol.\r
974\r
975 @param[in] AtaPassThru The pointer to the ATA_PASS_THRU protocol.\r
976 @param[in] Port The port number of the ATA device to send the command.\r
977 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
978 If there is no port multiplier, then specify 0xFFFF.\r
979 @param[in] Identifier The identifier to set user or master password.\r
980 @param[in] Password The hdd password of attached ATA device.\r
981\r
982 @retval EFI_SUCCESS Successful to disable hdd password cmd.\r
983 @retval EFI_INVALID_PARAMETER The parameter passed-in is invalid.\r
984 @retval EFI_OUT_OF_RESOURCES Not enough memory to disable hdd password cmd.\r
985 @retval EFI_DEVICE_ERROR Can not disable hdd password cmd.\r
986\r
987**/\r
988EFI_STATUS\r
989DisableHddPassword (\r
990 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
991 IN UINT16 Port,\r
992 IN UINT16 PortMultiplierPort,\r
993 IN CHAR8 Identifier,\r
994 IN CHAR8 *Password\r
995 )\r
996{\r
997 EFI_STATUS Status;\r
998 EFI_ATA_COMMAND_BLOCK Acb;\r
999 EFI_ATA_STATUS_BLOCK *Asb;\r
1000 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet;\r
1001 UINT8 Buffer[HDD_PAYLOAD];\r
1002\r
1003 if ((AtaPassThru == NULL) || (Password == NULL)) {\r
1004 return EFI_INVALID_PARAMETER;\r
1005 }\r
1006\r
1007 //\r
1008 // The 'Asb' field (a pointer to the EFI_ATA_STATUS_BLOCK structure) in\r
1009 // EFI_ATA_PASS_THRU_COMMAND_PACKET is required to be aligned specified by\r
1010 // the 'IoAlign' field in the EFI_ATA_PASS_THRU_MODE structure. Meanwhile,\r
1011 // the structure EFI_ATA_STATUS_BLOCK is composed of only UINT8 fields, so it\r
1012 // may not be aligned when allocated on stack for some compilers. Hence, we\r
1013 // use the API AllocateAlignedPages to ensure this structure is properly\r
1014 // aligned.\r
1015 //\r
1016 Asb = AllocateAlignedPages (\r
1017 EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)),\r
1018 AtaPassThru->Mode->IoAlign\r
1019 );\r
1020 if (Asb == NULL) {\r
1021 return EFI_OUT_OF_RESOURCES;\r
1022 }\r
1023\r
1024 //\r
1025 // Prepare for ATA command block.\r
1026 //\r
1027 ZeroMem (&Acb, sizeof (Acb));\r
1028 ZeroMem (Asb, sizeof (EFI_ATA_STATUS_BLOCK));\r
1029 Acb.AtaCommand = ATA_SECURITY_DIS_PASSWORD_CMD;\r
1030 Acb.AtaDeviceHead = (UINT8) (PortMultiplierPort == 0xFFFF ? 0 : (PortMultiplierPort << 4));\r
1031\r
1032 //\r
1033 // Prepare for ATA pass through packet.\r
1034 //\r
1035 ZeroMem (&Packet, sizeof (Packet));\r
1036 Packet.Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT;\r
1037 Packet.Length = EFI_ATA_PASS_THRU_LENGTH_BYTES;\r
1038 Packet.Asb = Asb;\r
1039 Packet.Acb = &Acb;\r
1040\r
1041 ((CHAR16 *) Buffer)[0] = Identifier & BIT0;\r
1042 CopyMem (&((CHAR16 *) Buffer)[1], Password, HDD_PASSWORD_MAX_LENGTH);\r
1043\r
1044 Packet.OutDataBuffer = Buffer;\r
1045 Packet.OutTransferLength = sizeof (Buffer);\r
1046 Packet.Timeout = ATA_TIMEOUT;\r
1047\r
1048 Status = AtaPassThru->PassThru (\r
1049 AtaPassThru,\r
1050 Port,\r
1051 PortMultiplierPort,\r
1052 &Packet,\r
1053 NULL\r
1054 );\r
1055 if (!EFI_ERROR (Status) &&\r
1056 ((Asb->AtaStatus & ATA_STSREG_ERR) != 0) &&\r
1057 ((Asb->AtaError & ATA_ERRREG_ABRT) != 0)) {\r
1058 Status = EFI_DEVICE_ERROR;\r
1059 }\r
1060\r
1061 FreeAlignedPages (Asb, EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)));\r
1062\r
1063 ZeroMem (Buffer, sizeof (Buffer));\r
1064\r
1065 DEBUG ((DEBUG_INFO, "%a() - %r\n", __FUNCTION__, Status));\r
1066 return Status;\r
1067}\r
1068\r
1069/**\r
1070 Send set hdd password cmd through Ata Pass Thru Protocol.\r
1071\r
1072 @param[in] AtaPassThru The pointer to the ATA_PASS_THRU protocol.\r
1073 @param[in] Port The port number of the ATA device to send the command.\r
1074 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
1075 If there is no port multiplier, then specify 0xFFFF.\r
1076 @param[in] Identifier The identifier to set user or master password.\r
1077 @param[in] SecurityLevel The security level to be set to device.\r
1078 @param[in] MasterPasswordIdentifier The master password identifier to be set to device.\r
1079 @param[in] Password The hdd password of attached ATA device.\r
1080\r
1081 @retval EFI_SUCCESS Successful to set hdd password cmd.\r
1082 @retval EFI_INVALID_PARAMETER The parameter passed-in is invalid.\r
1083 @retval EFI_OUT_OF_RESOURCES Not enough memory to set hdd password cmd.\r
1084 @retval EFI_DEVICE_ERROR Can not set hdd password cmd.\r
1085\r
1086**/\r
1087EFI_STATUS\r
1088SetHddPassword (\r
1089 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
1090 IN UINT16 Port,\r
1091 IN UINT16 PortMultiplierPort,\r
1092 IN CHAR8 Identifier,\r
1093 IN CHAR8 SecurityLevel,\r
1094 IN CHAR16 MasterPasswordIdentifier,\r
1095 IN CHAR8 *Password\r
1096 )\r
1097{\r
1098 EFI_STATUS Status;\r
1099 EFI_ATA_COMMAND_BLOCK Acb;\r
1100 EFI_ATA_STATUS_BLOCK *Asb;\r
1101 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet;\r
1102 UINT8 Buffer[HDD_PAYLOAD];\r
1103\r
1104 if ((AtaPassThru == NULL) || (Password == NULL)) {\r
1105 return EFI_INVALID_PARAMETER;\r
1106 }\r
1107\r
1108 //\r
1109 // The 'Asb' field (a pointer to the EFI_ATA_STATUS_BLOCK structure) in\r
1110 // EFI_ATA_PASS_THRU_COMMAND_PACKET is required to be aligned specified by\r
1111 // the 'IoAlign' field in the EFI_ATA_PASS_THRU_MODE structure. Meanwhile,\r
1112 // the structure EFI_ATA_STATUS_BLOCK is composed of only UINT8 fields, so it\r
1113 // may not be aligned when allocated on stack for some compilers. Hence, we\r
1114 // use the API AllocateAlignedPages to ensure this structure is properly\r
1115 // aligned.\r
1116 //\r
1117 Asb = AllocateAlignedPages (\r
1118 EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)),\r
1119 AtaPassThru->Mode->IoAlign\r
1120 );\r
1121 if (Asb == NULL) {\r
1122 return EFI_OUT_OF_RESOURCES;\r
1123 }\r
1124\r
1125 //\r
1126 // Prepare for ATA command block.\r
1127 //\r
1128 ZeroMem (&Acb, sizeof (Acb));\r
1129 ZeroMem (Asb, sizeof (EFI_ATA_STATUS_BLOCK));\r
1130 Acb.AtaCommand = ATA_SECURITY_SET_PASSWORD_CMD;\r
1131 Acb.AtaDeviceHead = (UINT8) (PortMultiplierPort == 0xFFFF ? 0 : (PortMultiplierPort << 4));\r
1132\r
1133 //\r
1134 // Prepare for ATA pass through packet.\r
1135 //\r
1136 ZeroMem (&Packet, sizeof (Packet));\r
1137 Packet.Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT;\r
1138 Packet.Length = EFI_ATA_PASS_THRU_LENGTH_BYTES;\r
1139 Packet.Asb = Asb;\r
1140 Packet.Acb = &Acb;\r
1141\r
1142 ((CHAR16 *) Buffer)[0] = (Identifier | (UINT16)(SecurityLevel << 8)) & (BIT0 | BIT8);\r
1143 CopyMem (&((CHAR16 *) Buffer)[1], Password, HDD_PASSWORD_MAX_LENGTH);\r
1144 if ((Identifier & BIT0) != 0) {\r
1145 ((CHAR16 *) Buffer)[17] = MasterPasswordIdentifier;\r
1146 }\r
1147\r
1148 Packet.OutDataBuffer = Buffer;\r
1149 Packet.OutTransferLength = sizeof (Buffer);\r
1150 Packet.Timeout = ATA_TIMEOUT;\r
1151\r
1152 Status = AtaPassThru->PassThru (\r
1153 AtaPassThru,\r
1154 Port,\r
1155 PortMultiplierPort,\r
1156 &Packet,\r
1157 NULL\r
1158 );\r
1159 if (!EFI_ERROR (Status) &&\r
1160 ((Asb->AtaStatus & ATA_STSREG_ERR) != 0) &&\r
1161 ((Asb->AtaError & ATA_ERRREG_ABRT) != 0)) {\r
1162 Status = EFI_DEVICE_ERROR;\r
1163 }\r
1164\r
1165 FreeAlignedPages (Asb, EFI_SIZE_TO_PAGES (sizeof (EFI_ATA_STATUS_BLOCK)));\r
1166\r
1167 ZeroMem (Buffer, sizeof (Buffer));\r
1168\r
1169 DEBUG ((DEBUG_INFO, "%a() - %r\n", __FUNCTION__, Status));\r
1170 return Status;\r
1171}\r
1172\r
1173/**\r
1174 Get attached harddisk model number from identify data buffer.\r
1175\r
1176 @param[in] IdentifyData Pointer to identify data buffer.\r
1177 @param[in, out] String The buffer to store harddisk model number.\r
1178\r
1179**/\r
1180VOID\r
1181GetHddDeviceModelNumber (\r
1182 IN ATA_IDENTIFY_DATA *IdentifyData,\r
1183 IN OUT CHAR16 *String\r
1184 )\r
1185{\r
1186 UINTN Index;\r
1187\r
1188 //\r
1189 // Swap the byte order in the original module name.\r
1190 // From Ata spec, the maximum length is 40 bytes.\r
1191 //\r
1192 for (Index = 0; Index < 40; Index += 2) {\r
1193 String[Index] = IdentifyData->ModelName[Index + 1];\r
1194 String[Index + 1] = IdentifyData->ModelName[Index];\r
1195 }\r
1196\r
1197 //\r
1198 // Chap it off after 20 characters\r
1199 //\r
1200 String[20] = L'\0';\r
1201\r
1202 return ;\r
1203}\r
1204\r
1205/**\r
1206 Get password input from the popup windows.\r
1207\r
1208 @param[in] PopUpString1 Pop up string 1.\r
1209 @param[in] PopUpString2 Pop up string 2.\r
1210 @param[in, out] Password The buffer to hold the input password.\r
1211\r
1212 @retval EFI_ABORTED It is given up by pressing 'ESC' key.\r
1213 @retval EFI_SUCCESS Get password input successfully.\r
1214\r
1215**/\r
1216EFI_STATUS\r
1217PopupHddPasswordInputWindows (\r
1218 IN CHAR16 *PopUpString1,\r
1219 IN CHAR16 *PopUpString2,\r
1220 IN OUT CHAR8 *Password\r
1221 )\r
1222{\r
1223 EFI_INPUT_KEY Key;\r
1224 UINTN Length;\r
1225 CHAR16 Mask[HDD_PASSWORD_MAX_LENGTH + 1];\r
1226 CHAR16 Unicode[HDD_PASSWORD_MAX_LENGTH + 1];\r
1227 CHAR8 Ascii[HDD_PASSWORD_MAX_LENGTH + 1];\r
1228\r
1229 ZeroMem (Unicode, sizeof (Unicode));\r
1230 ZeroMem (Ascii, sizeof (Ascii));\r
1231 ZeroMem (Mask, sizeof (Mask));\r
1232\r
1233 gST->ConOut->ClearScreen(gST->ConOut);\r
1234\r
1235 Length = 0;\r
1236 while (TRUE) {\r
1237 Mask[Length] = L'_';\r
1238 if (PopUpString2 == NULL) {\r
1239 CreatePopUp (\r
1240 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1241 &Key,\r
1242 PopUpString1,\r
1243 L"---------------------",\r
1244 Mask,\r
1245 NULL\r
1246 );\r
1247 } else {\r
1248 CreatePopUp (\r
1249 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1250 &Key,\r
1251 PopUpString1,\r
1252 PopUpString2,\r
1253 L"---------------------",\r
1254 Mask,\r
1255 NULL\r
1256 );\r
1257 }\r
1258 //\r
1259 // Check key.\r
1260 //\r
1261 if (Key.ScanCode == SCAN_NULL) {\r
1262 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
1263 //\r
1264 // Add the null terminator.\r
1265 //\r
1266 Unicode[Length] = 0;\r
1267 break;\r
1268 } else if ((Key.UnicodeChar == CHAR_NULL) ||\r
1269 (Key.UnicodeChar == CHAR_TAB) ||\r
1270 (Key.UnicodeChar == CHAR_LINEFEED)\r
1271 ) {\r
1272 continue;\r
1273 } else {\r
1274 if (Key.UnicodeChar == CHAR_BACKSPACE) {\r
1275 if (Length > 0) {\r
1276 Unicode[Length] = 0;\r
1277 Mask[Length] = 0;\r
1278 Length--;\r
1279 }\r
1280 } else {\r
1281 Unicode[Length] = Key.UnicodeChar;\r
1282 Mask[Length] = L'*';\r
1283 Length++;\r
1284 if (Length == HDD_PASSWORD_MAX_LENGTH) {\r
1285 //\r
1286 // Add the null terminator.\r
1287 //\r
1288 Unicode[Length] = 0;\r
1289 Mask[Length] = 0;\r
1290 break;\r
1291 }\r
1292 }\r
1293 }\r
1294 }\r
1295\r
1296 if (Key.ScanCode == SCAN_ESC) {\r
1297 ZeroMem (Unicode, sizeof (Unicode));\r
1298 ZeroMem (Ascii, sizeof (Ascii));\r
1299 gST->ConOut->ClearScreen(gST->ConOut);\r
1300 return EFI_ABORTED;\r
1301 }\r
1302 }\r
1303\r
1304 UnicodeStrToAsciiStrS (Unicode, Ascii, sizeof (Ascii));\r
1305 CopyMem (Password, Ascii, HDD_PASSWORD_MAX_LENGTH);\r
1306 ZeroMem (Unicode, sizeof (Unicode));\r
1307 ZeroMem (Ascii, sizeof (Ascii));\r
1308\r
1309 gST->ConOut->ClearScreen(gST->ConOut);\r
1310 return EFI_SUCCESS;\r
1311}\r
1312\r
1313/**\r
1314 Check if disk is locked, show popup window and ask for password if it is.\r
1315\r
1316 @param[in] AtaPassThru Pointer to ATA_PASSTHRU instance.\r
1317 @param[in] Port The port number of attached ATA device.\r
1318 @param[in] PortMultiplierPort The port number of port multiplier of attached ATA device.\r
1319 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
1320\r
1321**/\r
1322VOID\r
1323HddPasswordRequestPassword (\r
1324 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
1325 IN UINT16 Port,\r
1326 IN UINT16 PortMultiplierPort,\r
1327 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry\r
1328 )\r
1329{\r
1330 EFI_STATUS Status;\r
1331 CHAR16 PopUpString[100];\r
1332 ATA_IDENTIFY_DATA IdentifyData;\r
1333 EFI_INPUT_KEY Key;\r
1334 UINT16 RetryCount;\r
1335 CHAR8 Password[HDD_PASSWORD_MAX_LENGTH];\r
1336\r
1337 RetryCount = 0;\r
1338\r
1339 DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));\r
1340\r
1341 UnicodeSPrint (PopUpString, sizeof (PopUpString), L"Unlock: %s", ConfigFormEntry->HddString);\r
1342\r
1343 //\r
1344 // Check the device security status.\r
1345 //\r
1346 if ((ConfigFormEntry->IfrData.SecurityStatus.Supported) &&\r
1347 (ConfigFormEntry->IfrData.SecurityStatus.Enabled)) {\r
1348 //\r
1349 // As soon as the HDD password is in enabled state, we pop up a window to unlock hdd\r
1350 // no matter it's really in locked or unlocked state.\r
1351 // This way forces user to enter password every time to provide best safety.\r
1352 //\r
1353 while (TRUE) {\r
1354 Status = PopupHddPasswordInputWindows (PopUpString, NULL, Password);\r
1355 if (!EFI_ERROR (Status)) {\r
1356 //\r
1357 // The HDD is in locked state, unlock it by user input.\r
1358 //\r
1359 if (!PasswordIsFullZero (Password)) {\r
1360 if (!ConfigFormEntry->IfrData.SecurityStatus.Frozen) {\r
1361 Status = UnlockHddPassword (AtaPassThru, Port, PortMultiplierPort, 0, Password);\r
1362 } else {\r
1363 //\r
1364 // Use saved HDD password variable to validate HDD password\r
1365 // when the device is at frozen state.\r
1366 //\r
1367 Status = ValidateHddPassword (ConfigFormEntry, Password);\r
1368 }\r
1369 } else {\r
1370 Status = EFI_INVALID_PARAMETER;\r
1371 }\r
1372 if (!EFI_ERROR (Status)) {\r
1373 CopyMem (ConfigFormEntry->Password, Password, HDD_PASSWORD_MAX_LENGTH);\r
1374 if (!ConfigFormEntry->IfrData.SecurityStatus.Frozen) {\r
1375 SaveHddPasswordVariable (ConfigFormEntry, Password);\r
1376 }\r
1377 ZeroMem (Password, HDD_PASSWORD_MAX_LENGTH);\r
1378 Status = GetHddDeviceIdentifyData (AtaPassThru, Port, PortMultiplierPort, &IdentifyData);\r
1379 ASSERT_EFI_ERROR (Status);\r
1380\r
1381 //\r
1382 // Check the device security status again.\r
1383 //\r
1384 GetHddPasswordSecurityStatus (&IdentifyData, &ConfigFormEntry->IfrData);\r
1385 return;\r
1386 }\r
1387\r
1388 ZeroMem (Password, HDD_PASSWORD_MAX_LENGTH);\r
1389\r
1390 if (EFI_ERROR (Status)) {\r
1391 RetryCount ++;\r
1392 if (RetryCount < MAX_HDD_PASSWORD_RETRY_COUNT) {\r
1393 do {\r
1394 CreatePopUp (\r
1395 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1396 &Key,\r
1397 L"Invalid password.",\r
1398 L"Press ENTER to retry",\r
1399 NULL\r
1400 );\r
1401 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1402 continue;\r
1403 } else {\r
1404 do {\r
1405 CreatePopUp (\r
1406 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1407 &Key,\r
1408 L"Hdd password retry count is expired. Please shutdown the machine.",\r
1409 L"Press ENTER to shutdown",\r
1410 NULL\r
1411 );\r
1412 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1413 gRT->ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r
1414 break;\r
1415 }\r
1416 }\r
1417 } else if (Status == EFI_ABORTED) {\r
1418 if (ConfigFormEntry->IfrData.SecurityStatus.Locked) {\r
1419 //\r
1420 // Current device in the lock status and\r
1421 // User not input password and press ESC,\r
1422 // keep device in lock status and continue boot.\r
1423 //\r
1424 do {\r
1425 CreatePopUp (\r
1426 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1427 &Key,\r
1428 L"Press ENTER to skip the request and continue boot,",\r
1429 L"Press ESC to input password again",\r
1430 NULL\r
1431 );\r
1432 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
1433\r
1434 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
1435 gST->ConOut->ClearScreen(gST->ConOut);\r
1436 //\r
1437 // Keep lock and continue boot.\r
1438 //\r
1439 return;\r
1440 } else {\r
1441 //\r
1442 // Let user input password again.\r
1443 //\r
1444 continue;\r
1445 }\r
1446 } else {\r
1447 //\r
1448 // Current device in the unlock status and\r
1449 // User not input password and press ESC,\r
1450 // Shutdown the device.\r
1451 //\r
1452 do {\r
1453 CreatePopUp (\r
1454 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1455 &Key,\r
1456 L"Press ENTER to shutdown, Press ESC to input password again",\r
1457 NULL\r
1458 );\r
1459 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
1460\r
1461 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
1462 gRT->ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r
1463 } else {\r
1464 //\r
1465 // Let user input password again.\r
1466 //\r
1467 continue;\r
1468 }\r
1469 }\r
1470 }\r
1471 }\r
1472 }\r
1473}\r
1474\r
1475/**\r
1476 Process Set User Pwd HDD password request.\r
1477\r
1478 @param[in] AtaPassThru Pointer to ATA_PASSTHRU instance.\r
1479 @param[in] Port The port number of attached ATA device.\r
1480 @param[in] PortMultiplierPort The port number of port multiplier of attached ATA device.\r
1481 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
1482\r
1483**/\r
1484VOID\r
1485ProcessHddPasswordRequestSetUserPwd (\r
1486 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
1487 IN UINT16 Port,\r
1488 IN UINT16 PortMultiplierPort,\r
1489 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry\r
1490 )\r
1491{\r
1492 EFI_STATUS Status;\r
1493 CHAR16 PopUpString[100];\r
1494 ATA_IDENTIFY_DATA IdentifyData;\r
1495 EFI_INPUT_KEY Key;\r
1496 UINT16 RetryCount;\r
1497 CHAR8 Password[HDD_PASSWORD_MAX_LENGTH];\r
1498 CHAR8 PasswordConfirm[HDD_PASSWORD_MAX_LENGTH];\r
1499\r
1500 RetryCount = 0;\r
1501\r
1502 DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));\r
1503\r
1504 if (ConfigFormEntry->IfrData.SecurityStatus.Frozen) {\r
1505 DEBUG ((DEBUG_INFO, "%s is frozen, do nothing\n", ConfigFormEntry->HddString));\r
1506 return;\r
1507 }\r
1508\r
1509 if (ConfigFormEntry->IfrData.SecurityStatus.Locked) {\r
1510 DEBUG ((DEBUG_INFO, "%s is locked, do nothing\n", ConfigFormEntry->HddString));\r
1511 return;\r
1512 }\r
1513\r
1514 UnicodeSPrint (PopUpString, sizeof (PopUpString), L"Set User Pwd: %s", ConfigFormEntry->HddString);\r
1515\r
1516 //\r
1517 // Check the device security status.\r
1518 //\r
1519 if (ConfigFormEntry->IfrData.SecurityStatus.Supported) {\r
1520 while (TRUE) {\r
1521 Status = PopupHddPasswordInputWindows (PopUpString, L"Please type in your new password", Password);\r
1522 if (!EFI_ERROR (Status)) {\r
1523 Status = PopupHddPasswordInputWindows (PopUpString, L"Please confirm your new password", PasswordConfirm);\r
1524 if (!EFI_ERROR (Status)) {\r
1525 if (CompareMem (Password, PasswordConfirm, HDD_PASSWORD_MAX_LENGTH) == 0) {\r
1526 if (!PasswordIsFullZero (Password)) {\r
1527 Status = SetHddPassword (AtaPassThru, Port, PortMultiplierPort, 0, 1, 0, Password);\r
1528 } else {\r
1529 if (ConfigFormEntry->IfrData.SecurityStatus.Enabled) {\r
1530 Status = DisableHddPassword (AtaPassThru, Port, PortMultiplierPort, 0, ConfigFormEntry->Password);\r
1531 } else {\r
1532 Status = EFI_INVALID_PARAMETER;\r
1533 }\r
1534 }\r
1535 if (!EFI_ERROR (Status)) {\r
1536 CopyMem (ConfigFormEntry->Password, Password, HDD_PASSWORD_MAX_LENGTH);\r
1537 SaveHddPasswordVariable (ConfigFormEntry, Password);\r
1538 ZeroMem (Password, HDD_PASSWORD_MAX_LENGTH);\r
1539 ZeroMem (PasswordConfirm, HDD_PASSWORD_MAX_LENGTH);\r
1540 Status = GetHddDeviceIdentifyData (AtaPassThru, Port, PortMultiplierPort, &IdentifyData);\r
1541 ASSERT_EFI_ERROR (Status);\r
1542\r
1543 //\r
1544 // Check the device security status again.\r
1545 //\r
1546 GetHddPasswordSecurityStatus (&IdentifyData, &ConfigFormEntry->IfrData);\r
1547 return;\r
1548 } else {\r
1549 do {\r
1550 CreatePopUp (\r
1551 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1552 &Key,\r
1553 L"Set/Disable User Pwd failed or invalid password.",\r
1554 L"Press ENTER to retry",\r
1555 NULL\r
1556 );\r
1557 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1558 }\r
1559 } else {\r
1560 do {\r
1561 CreatePopUp (\r
1562 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1563 &Key,\r
1564 L"Passwords are not the same.",\r
1565 L"Press ENTER to retry",\r
1566 NULL\r
1567 );\r
1568 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1569 Status = EFI_INVALID_PARAMETER;\r
1570 }\r
1571 }\r
1572\r
1573 ZeroMem (Password, HDD_PASSWORD_MAX_LENGTH);\r
1574 ZeroMem (PasswordConfirm, HDD_PASSWORD_MAX_LENGTH);\r
1575\r
1576 if (EFI_ERROR (Status)) {\r
1577 RetryCount ++;\r
1578 if (RetryCount >= MAX_HDD_PASSWORD_RETRY_COUNT) {\r
1579 do {\r
1580 CreatePopUp (\r
1581 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1582 &Key,\r
1583 L"Hdd password retry count is expired.",\r
1584 L"Press ENTER to skip the request and continue boot",\r
1585 NULL\r
1586 );\r
1587 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1588 gST->ConOut->ClearScreen(gST->ConOut);\r
1589 return;\r
1590 }\r
1591 }\r
1592 } else if (Status == EFI_ABORTED) {\r
1593 do {\r
1594 CreatePopUp (\r
1595 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1596 &Key,\r
1597 L"Press ENTER to skip the request and continue boot,",\r
1598 L"Press ESC to input password again",\r
1599 NULL\r
1600 );\r
1601 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
1602\r
1603 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
1604 gST->ConOut->ClearScreen(gST->ConOut);\r
1605 return;\r
1606 } else {\r
1607 //\r
1608 // Let user input password again.\r
1609 //\r
1610 continue;\r
1611 }\r
1612 }\r
1613 }\r
1614 }\r
1615}\r
1616\r
1617/**\r
1618 Process Set Master Pwd HDD password request.\r
1619\r
1620 @param[in] AtaPassThru Pointer to ATA_PASSTHRU instance.\r
1621 @param[in] Port The port number of attached ATA device.\r
1622 @param[in] PortMultiplierPort The port number of port multiplier of attached ATA device.\r
1623 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
1624\r
1625**/\r
1626VOID\r
1627ProcessHddPasswordRequestSetMasterPwd (\r
1628 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
1629 IN UINT16 Port,\r
1630 IN UINT16 PortMultiplierPort,\r
1631 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry\r
1632 )\r
1633{\r
1634 EFI_STATUS Status;\r
1635 CHAR16 PopUpString[100];\r
1636 EFI_INPUT_KEY Key;\r
1637 UINT16 RetryCount;\r
1638 CHAR8 Password[HDD_PASSWORD_MAX_LENGTH];\r
1639 CHAR8 PasswordConfirm[HDD_PASSWORD_MAX_LENGTH];\r
1640\r
1641 RetryCount = 0;\r
1642\r
1643 DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));\r
1644\r
1645 if (ConfigFormEntry->IfrData.SecurityStatus.Frozen) {\r
1646 DEBUG ((DEBUG_INFO, "%s is frozen, do nothing\n", ConfigFormEntry->HddString));\r
1647 return;\r
1648 }\r
1649\r
1650 if (ConfigFormEntry->IfrData.SecurityStatus.Locked) {\r
1651 DEBUG ((DEBUG_INFO, "%s is locked, do nothing\n", ConfigFormEntry->HddString));\r
1652 return;\r
1653 }\r
1654\r
1655 UnicodeSPrint (PopUpString, sizeof (PopUpString), L"Set Master Pwd: %s", ConfigFormEntry->HddString);\r
1656\r
1657 //\r
1658 // Check the device security status.\r
1659 //\r
1660 if (ConfigFormEntry->IfrData.SecurityStatus.Supported) {\r
1661 while (TRUE) {\r
1662 Status = PopupHddPasswordInputWindows (PopUpString, L"Please type in your new password", Password);\r
1663 if (!EFI_ERROR (Status)) {\r
1664 Status = PopupHddPasswordInputWindows (PopUpString, L"Please confirm your new password", PasswordConfirm);\r
1665 if (!EFI_ERROR (Status)) {\r
1666 if (CompareMem (Password, PasswordConfirm, HDD_PASSWORD_MAX_LENGTH) == 0) {\r
1667 if (!PasswordIsFullZero (Password)) {\r
1668 Status = SetHddPassword (AtaPassThru, Port, PortMultiplierPort, 1, 1, 1, Password);\r
1669 } else {\r
1670 Status = EFI_INVALID_PARAMETER;\r
1671 }\r
1672 if (!EFI_ERROR (Status)) {\r
1673 ZeroMem (Password, HDD_PASSWORD_MAX_LENGTH);\r
1674 ZeroMem (PasswordConfirm, HDD_PASSWORD_MAX_LENGTH);\r
1675 return;\r
1676 } else {\r
1677 do {\r
1678 CreatePopUp (\r
1679 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1680 &Key,\r
1681 L"Set Master Pwd failed or invalid password.",\r
1682 L"Press ENTER to retry",\r
1683 NULL\r
1684 );\r
1685 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1686 }\r
1687 } else {\r
1688 do {\r
1689 CreatePopUp (\r
1690 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1691 &Key,\r
1692 L"Passwords are not the same.",\r
1693 L"Press ENTER to retry",\r
1694 NULL\r
1695 );\r
1696 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1697 Status = EFI_INVALID_PARAMETER;\r
1698 }\r
1699 }\r
1700\r
1701 ZeroMem (Password, HDD_PASSWORD_MAX_LENGTH);\r
1702 ZeroMem (PasswordConfirm, HDD_PASSWORD_MAX_LENGTH);\r
1703\r
1704 if (EFI_ERROR (Status)) {\r
1705 RetryCount ++;\r
1706 if (RetryCount >= MAX_HDD_PASSWORD_RETRY_COUNT) {\r
1707 do {\r
1708 CreatePopUp (\r
1709 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1710 &Key,\r
1711 L"Hdd password retry count is expired.",\r
1712 L"Press ENTER to skip the request and continue boot",\r
1713 NULL\r
1714 );\r
1715 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
1716 gST->ConOut->ClearScreen(gST->ConOut);\r
1717 return;\r
1718 }\r
1719 }\r
1720 } else if (Status == EFI_ABORTED) {\r
1721 do {\r
1722 CreatePopUp (\r
1723 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
1724 &Key,\r
1725 L"Press ENTER to skip the request and continue boot,",\r
1726 L"Press ESC to input password again",\r
1727 NULL\r
1728 );\r
1729 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
1730\r
1731 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
1732 gST->ConOut->ClearScreen(gST->ConOut);\r
1733 return;\r
1734 } else {\r
1735 //\r
1736 // Let user input password again.\r
1737 //\r
1738 continue;\r
1739 }\r
1740 }\r
1741 }\r
1742 }\r
1743}\r
1744\r
1745/**\r
1746 Process HDD password request.\r
1747\r
1748 @param[in] AtaPassThru Pointer to ATA_PASSTHRU instance.\r
1749 @param[in] Port The port number of attached ATA device.\r
1750 @param[in] PortMultiplierPort The port number of port multiplier of attached ATA device.\r
1751 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
1752\r
1753**/\r
1754VOID\r
1755ProcessHddPasswordRequest (\r
1756 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
1757 IN UINT16 Port,\r
1758 IN UINT16 PortMultiplierPort,\r
1759 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry\r
1760 )\r
1761{\r
1762 EFI_STATUS Status;\r
1763 HDD_PASSWORD_REQUEST_VARIABLE *TempVariable;\r
1764 HDD_PASSWORD_REQUEST_VARIABLE *Variable;\r
1765 UINTN VariableSize;\r
1766\r
1767 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
1768\r
1769 if (mHddPasswordRequestVariable == NULL) {\r
1770 Status = GetVariable2 (\r
1771 HDD_PASSWORD_REQUEST_VARIABLE_NAME,\r
1772 &mHddPasswordVendorGuid,\r
1773 (VOID **) &Variable,\r
1774 &VariableSize\r
1775 );\r
1776 if (EFI_ERROR (Status) || (Variable == NULL)) {\r
1777 return;\r
1778 }\r
1779 mHddPasswordRequestVariable = Variable;\r
1780 mHddPasswordRequestVariableSize = VariableSize;\r
1781\r
1782 //\r
1783 // Delete the HDD password request variable.\r
1784 //\r
1785 Status = gRT->SetVariable (\r
1786 HDD_PASSWORD_REQUEST_VARIABLE_NAME,\r
1787 &mHddPasswordVendorGuid,\r
1788 0,\r
1789 0,\r
1790 NULL\r
1791 );\r
1792 ASSERT_EFI_ERROR (Status);\r
1793 } else {\r
1794 Variable = mHddPasswordRequestVariable;\r
1795 VariableSize = mHddPasswordRequestVariableSize;\r
1796 }\r
1797\r
1798 //\r
1799 // Process the HDD password requests.\r
1800 //\r
1801 TempVariable = Variable;\r
1802 while (VariableSize >= sizeof (HDD_PASSWORD_REQUEST_VARIABLE)) {\r
1803 if ((TempVariable->Device.Bus == ConfigFormEntry->Bus) &&\r
1804 (TempVariable->Device.Device == ConfigFormEntry->Device) &&\r
1805 (TempVariable->Device.Function == ConfigFormEntry->Function) &&\r
1806 (TempVariable->Device.Port == ConfigFormEntry->Port) &&\r
1807 (TempVariable->Device.PortMultiplierPort == ConfigFormEntry->PortMultiplierPort)) {\r
1808 //\r
1809 // Found the node for the HDD password device.\r
1810 //\r
1811 if (TempVariable->Request.UserPassword != 0) {\r
1812 ProcessHddPasswordRequestSetUserPwd (AtaPassThru, Port, PortMultiplierPort, ConfigFormEntry);\r
1813 }\r
1814 if (TempVariable->Request.MasterPassword != 0) {\r
1815 ProcessHddPasswordRequestSetMasterPwd (AtaPassThru, Port, PortMultiplierPort, ConfigFormEntry);\r
1816 }\r
1817\r
1818 break;\r
1819 }\r
1820\r
1821 VariableSize -= sizeof (HDD_PASSWORD_REQUEST_VARIABLE);\r
1822 TempVariable += 1;\r
1823 }\r
1824\r
1825 DEBUG ((DEBUG_INFO, "%a() - exit\n", __FUNCTION__));\r
1826}\r
1827\r
1828/**\r
1829 Get saved HDD password request.\r
1830\r
1831 @param[in, out] ConfigFormEntry The HDD Password configuration form entry.\r
1832\r
1833**/\r
1834VOID\r
1835GetSavedHddPasswordRequest (\r
1836 IN OUT HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry\r
1837 )\r
1838{\r
1839 EFI_STATUS Status;\r
1840 HDD_PASSWORD_REQUEST_VARIABLE *TempVariable;\r
1841 HDD_PASSWORD_REQUEST_VARIABLE *Variable;\r
1842 UINTN VariableSize;\r
1843\r
1844 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
1845\r
1846 Variable = NULL;\r
1847 VariableSize = 0;\r
1848\r
1849 Status = GetVariable2 (\r
1850 HDD_PASSWORD_REQUEST_VARIABLE_NAME,\r
1851 &mHddPasswordVendorGuid,\r
1852 (VOID **) &Variable,\r
1853 &VariableSize\r
1854 );\r
1855 if (EFI_ERROR (Status) || (Variable == NULL)) {\r
1856 return;\r
1857 }\r
1858\r
1859 TempVariable = Variable;\r
1860 while (VariableSize >= sizeof (HDD_PASSWORD_REQUEST_VARIABLE)) {\r
1861 if ((TempVariable->Device.Bus == ConfigFormEntry->Bus) &&\r
1862 (TempVariable->Device.Device == ConfigFormEntry->Device) &&\r
1863 (TempVariable->Device.Function == ConfigFormEntry->Function) &&\r
1864 (TempVariable->Device.Port == ConfigFormEntry->Port) &&\r
1865 (TempVariable->Device.PortMultiplierPort == ConfigFormEntry->PortMultiplierPort)) {\r
1866 //\r
1867 // Found the node for the HDD password device.\r
1868 // Get the HDD password request.\r
1869 //\r
1870 CopyMem (&ConfigFormEntry->IfrData.Request, &TempVariable->Request, sizeof (HDD_PASSWORD_REQUEST));\r
1871 DEBUG ((\r
1872 DEBUG_INFO,\r
1873 "HddPasswordRequest got: 0x%x\n",\r
1874 ConfigFormEntry->IfrData.Request\r
1875 ));\r
1876 break;\r
1877 }\r
1878 VariableSize -= sizeof (HDD_PASSWORD_REQUEST_VARIABLE);\r
1879 TempVariable += 1;\r
1880 }\r
1881\r
1882 FreePool (Variable);\r
1883\r
1884 DEBUG ((DEBUG_INFO, "%a() - exit\n", __FUNCTION__));\r
1885}\r
1886\r
1887/**\r
1888 Save HDD password request.\r
1889\r
1890 @param[in] ConfigFormEntry The HDD Password configuration form entry.\r
1891\r
1892**/\r
1893VOID\r
1894SaveHddPasswordRequest (\r
1895 IN HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry\r
1896 )\r
1897{\r
1898 EFI_STATUS Status;\r
1899 HDD_PASSWORD_REQUEST_VARIABLE *TempVariable;\r
1900 UINTN TempVariableSize;\r
1901 HDD_PASSWORD_REQUEST_VARIABLE *Variable;\r
1902 UINTN VariableSize;\r
1903 HDD_PASSWORD_REQUEST_VARIABLE *NewVariable;\r
1904 UINTN NewVariableSize;\r
1905\r
1906 DEBUG ((DEBUG_INFO, "%a() - enter\n", __FUNCTION__));\r
1907\r
1908 DEBUG ((\r
1909 DEBUG_INFO,\r
1910 "HddPasswordRequest to save: 0x%x\n",\r
1911 ConfigFormEntry->IfrData.Request\r
1912 ));\r
1913\r
1914 Variable = NULL;\r
1915 VariableSize = 0;\r
1916 NewVariable = NULL;\r
1917 NewVariableSize = 0;\r
1918\r
1919 Status = GetVariable2 (\r
1920 HDD_PASSWORD_REQUEST_VARIABLE_NAME,\r
1921 &mHddPasswordVendorGuid,\r
1922 (VOID **) &Variable,\r
1923 &VariableSize\r
1924 );\r
1925 if (!EFI_ERROR (Status) && (Variable != NULL)) {\r
1926 TempVariable = Variable;\r
1927 TempVariableSize = VariableSize;\r
1928 while (TempVariableSize >= sizeof (HDD_PASSWORD_REQUEST_VARIABLE)) {\r
1929 if ((TempVariable->Device.Bus == ConfigFormEntry->Bus) &&\r
1930 (TempVariable->Device.Device == ConfigFormEntry->Device) &&\r
1931 (TempVariable->Device.Function == ConfigFormEntry->Function) &&\r
1932 (TempVariable->Device.Port == ConfigFormEntry->Port) &&\r
1933 (TempVariable->Device.PortMultiplierPort == ConfigFormEntry->PortMultiplierPort)) {\r
1934 //\r
1935 // Found the node for the HDD password device.\r
1936 // Update the HDD password request.\r
1937 //\r
1938 CopyMem (&TempVariable->Request, &ConfigFormEntry->IfrData.Request, sizeof (HDD_PASSWORD_REQUEST));\r
1939 NewVariable = Variable;\r
1940 NewVariableSize = VariableSize;\r
1941 break;\r
1942 }\r
1943 TempVariableSize -= sizeof (HDD_PASSWORD_REQUEST_VARIABLE);\r
1944 TempVariable += 1;\r
1945 }\r
1946 if (NewVariable == NULL) {\r
1947 //\r
1948 // The node for the HDD password device is not found.\r
1949 // Create node for the HDD password device.\r
1950 //\r
1951 NewVariableSize = VariableSize + sizeof (HDD_PASSWORD_REQUEST_VARIABLE);\r
1952 NewVariable = AllocateZeroPool (NewVariableSize);\r
1953 ASSERT (NewVariable != NULL);\r
1954 CopyMem (NewVariable, Variable, VariableSize);\r
1955 TempVariable = (HDD_PASSWORD_REQUEST_VARIABLE *) ((UINTN) NewVariable + VariableSize);\r
1956 TempVariable->Device.Bus = (UINT8) ConfigFormEntry->Bus;\r
1957 TempVariable->Device.Device = (UINT8) ConfigFormEntry->Device;\r
1958 TempVariable->Device.Function = (UINT8) ConfigFormEntry->Function;\r
1959 TempVariable->Device.Port = ConfigFormEntry->Port;\r
1960 TempVariable->Device.PortMultiplierPort = ConfigFormEntry->PortMultiplierPort;\r
1961 CopyMem (&TempVariable->Request, &ConfigFormEntry->IfrData.Request, sizeof (HDD_PASSWORD_REQUEST));\r
1962 }\r
1963 } else {\r
1964 NewVariableSize = sizeof (HDD_PASSWORD_REQUEST_VARIABLE);\r
1965 NewVariable = AllocateZeroPool (NewVariableSize);\r
1966 ASSERT (NewVariable != NULL);\r
1967 NewVariable->Device.Bus = (UINT8) ConfigFormEntry->Bus;\r
1968 NewVariable->Device.Device = (UINT8) ConfigFormEntry->Device;\r
1969 NewVariable->Device.Function = (UINT8) ConfigFormEntry->Function;\r
1970 NewVariable->Device.Port = ConfigFormEntry->Port;\r
1971 NewVariable->Device.PortMultiplierPort = ConfigFormEntry->PortMultiplierPort;\r
1972 CopyMem (&NewVariable->Request, &ConfigFormEntry->IfrData.Request, sizeof (HDD_PASSWORD_REQUEST));\r
1973 }\r
1974 Status = gRT->SetVariable (\r
1975 HDD_PASSWORD_REQUEST_VARIABLE_NAME,\r
1976 &mHddPasswordVendorGuid,\r
1977 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
1978 NewVariableSize,\r
1979 NewVariable\r
1980 );\r
1981 if (EFI_ERROR (Status)) {\r
1982 DEBUG ((DEBUG_INFO, "HddPasswordRequest variable set failed (%r)\n", Status));\r
1983 }\r
1984 if (NewVariable != Variable) {\r
1985 FreePool (NewVariable);\r
1986 }\r
1987 if (Variable != NULL) {\r
1988 FreePool (Variable);\r
1989 }\r
1990\r
1991 DEBUG ((DEBUG_INFO, "%a() - exit\n", __FUNCTION__));\r
1992}\r
1993\r
1994/**\r
1995 Get the HDD Password configuration form entry by the index of the goto opcode actived.\r
1996\r
1997 @param[in] Index The 0-based index of the goto opcode actived.\r
1998\r
1999 @return The HDD Password configuration form entry found.\r
2000**/\r
2001HDD_PASSWORD_CONFIG_FORM_ENTRY *\r
2002HddPasswordGetConfigFormEntryByIndex (\r
2003 IN UINT32 Index\r
2004 )\r
2005{\r
2006 LIST_ENTRY *Entry;\r
2007 UINT32 CurrentIndex;\r
2008 HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry;\r
2009\r
2010 CurrentIndex = 0;\r
2011 ConfigFormEntry = NULL;\r
2012\r
2013 EFI_LIST_FOR_EACH (Entry, &mHddPasswordConfigFormList) {\r
2014 if (CurrentIndex == Index) {\r
2015 ConfigFormEntry = BASE_CR (Entry, HDD_PASSWORD_CONFIG_FORM_ENTRY, Link);\r
2016 break;\r
2017 }\r
2018\r
2019 CurrentIndex++;\r
2020 }\r
2021\r
2022 return ConfigFormEntry;\r
2023}\r
2024\r
2025/**\r
2026 This function allows the caller to request the current\r
2027 configuration for one or more named elements. The resulting\r
2028 string is in <ConfigAltResp> format. Any and all alternative\r
2029 configuration strings shall also be appended to the end of the\r
2030 current configuration string. If they are, they must appear\r
2031 after the current configuration. They must contain the same\r
2032 routing (GUID, NAME, PATH) as the current configuration string.\r
2033 They must have an additional description indicating the type of\r
2034 alternative configuration the string represents,\r
2035 "ALTCFG=<StringToken>". That <StringToken> (when\r
2036 converted from Hex UNICODE to binary) is a reference to a\r
2037 string in the associated string pack.\r
2038\r
2039 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
2040 @param[in] Request A null-terminated Unicode string in\r
2041 <ConfigRequest> format. Note that this\r
2042 includes the routing information as well as\r
2043 the configurable name / value pairs. It is\r
2044 invalid for this string to be in\r
2045 <MultiConfigRequest> format.\r
2046 @param[out] Progress On return, points to a character in the\r
2047 Request string. Points to the string's null\r
2048 terminator if request was successful. Points\r
2049 to the most recent "&" before the first\r
2050 failing name / value pair (or the beginning\r
2051 of the string if the failure is in the first\r
2052 name / value pair) if the request was not\r
2053 successful.\r
2054 @param[out] Results A null-terminated Unicode string in\r
2055 <ConfigAltResp> format which has all values\r
2056 filled in for the names in the Request string.\r
2057 String to be allocated by the called function.\r
2058\r
2059 @retval EFI_SUCCESS The Results string is filled with the\r
2060 values corresponding to all requested\r
2061 names.\r
2062 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the\r
2063 parts of the results that must be\r
2064 stored awaiting possible future\r
2065 protocols.\r
2066 @retval EFI_INVALID_PARAMETER For example, passing in a NULL\r
2067 for the Request parameter\r
2068 would result in this type of\r
2069 error. In this case, the\r
2070 Progress parameter would be\r
2071 set to NULL.\r
2072 @retval EFI_NOT_FOUND Routing data doesn't match any\r
2073 known driver. Progress set to the\r
2074 first character in the routing header.\r
2075 Note: There is no requirement that the\r
2076 driver validate the routing data. It\r
2077 must skip the <ConfigHdr> in order to\r
2078 process the names.\r
2079 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set\r
2080 to most recent & before the\r
2081 error or the beginning of the\r
2082 string.\r
2083 @retval EFI_INVALID_PARAMETER Unknown name. Progress points\r
2084 to the & before the name in\r
2085 question.Currently not implemented.\r
2086**/\r
2087EFI_STATUS\r
2088EFIAPI\r
2089HddPasswordFormExtractConfig (\r
2090 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
2091 IN CONST EFI_STRING Request,\r
2092 OUT EFI_STRING *Progress,\r
2093 OUT EFI_STRING *Results\r
2094 )\r
2095{\r
2096 EFI_STATUS Status;\r
2097 UINTN BufferSize;\r
2098 HDD_PASSWORD_CONFIG *IfrData;\r
2099 HDD_PASSWORD_DXE_PRIVATE_DATA *Private;\r
2100 EFI_STRING ConfigRequestHdr;\r
2101 EFI_STRING ConfigRequest;\r
2102 BOOLEAN AllocatedRequest;\r
2103 UINTN Size;\r
2104\r
2105 if (Progress == NULL || Results == NULL) {\r
2106 return EFI_INVALID_PARAMETER;\r
2107 }\r
2108\r
2109 *Progress = Request;\r
2110 if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &mHddPasswordVendorGuid, mHddPasswordVendorStorageName)) {\r
2111 return EFI_NOT_FOUND;\r
2112 }\r
2113\r
2114 ConfigRequestHdr = NULL;\r
2115 ConfigRequest = NULL;\r
2116 AllocatedRequest = FALSE;\r
2117 Size = 0;\r
2118\r
2119 Private = HDD_PASSWORD_DXE_PRIVATE_FROM_THIS (This);\r
2120 IfrData = AllocateZeroPool (sizeof (HDD_PASSWORD_CONFIG));\r
2121 ASSERT (IfrData != NULL);\r
2122 if (Private->Current != NULL) {\r
2123 CopyMem (IfrData, &Private->Current->IfrData, sizeof (HDD_PASSWORD_CONFIG));\r
2124 }\r
2125\r
2126 //\r
2127 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()\r
2128 //\r
2129 BufferSize = sizeof (HDD_PASSWORD_CONFIG);\r
2130 ConfigRequest = Request;\r
2131 if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {\r
2132 //\r
2133 // Request has no request element, construct full request string.\r
2134 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template\r
2135 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator\r
2136 //\r
2137 ConfigRequestHdr = HiiConstructConfigHdr (&mHddPasswordVendorGuid, mHddPasswordVendorStorageName, Private->DriverHandle);\r
2138 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);\r
2139 ConfigRequest = AllocateZeroPool (Size);\r
2140 ASSERT (ConfigRequest != NULL);\r
2141 AllocatedRequest = TRUE;\r
2142 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);\r
2143 FreePool (ConfigRequestHdr);\r
2144 }\r
2145 Status = gHiiConfigRouting->BlockToConfig (\r
2146 gHiiConfigRouting,\r
2147 ConfigRequest,\r
2148 (UINT8 *) IfrData,\r
2149 BufferSize,\r
2150 Results,\r
2151 Progress\r
2152 );\r
2153 FreePool (IfrData);\r
2154 //\r
2155 // Free the allocated config request string.\r
2156 //\r
2157 if (AllocatedRequest) {\r
2158 FreePool (ConfigRequest);\r
2159 ConfigRequest = NULL;\r
2160 }\r
2161\r
2162 //\r
2163 // Set Progress string to the original request string.\r
2164 //\r
2165 if (Request == NULL) {\r
2166 *Progress = NULL;\r
2167 } else if (StrStr (Request, L"OFFSET") == NULL) {\r
2168 *Progress = Request + StrLen (Request);\r
2169 }\r
2170\r
2171 return Status;\r
2172}\r
2173\r
2174/**\r
2175 This function applies changes in a driver's configuration.\r
2176 Input is a Configuration, which has the routing data for this\r
2177 driver followed by name / value configuration pairs. The driver\r
2178 must apply those pairs to its configurable storage. If the\r
2179 driver's configuration is stored in a linear block of data\r
2180 and the driver's name / value pairs are in <BlockConfig>\r
2181 format, it may use the ConfigToBlock helper function (above) to\r
2182 simplify the job. Currently not implemented.\r
2183\r
2184 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
2185 @param[in] Configuration A null-terminated Unicode string in\r
2186 <ConfigString> format.\r
2187 @param[out] Progress A pointer to a string filled in with the\r
2188 offset of the most recent '&' before the\r
2189 first failing name / value pair (or the\r
2190 beginn ing of the string if the failure\r
2191 is in the first name / value pair) or\r
2192 the terminating NULL if all was\r
2193 successful.\r
2194\r
2195 @retval EFI_SUCCESS The results have been distributed or are\r
2196 awaiting distribution.\r
2197 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the\r
2198 parts of the results that must be\r
2199 stored awaiting possible future\r
2200 protocols.\r
2201 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the\r
2202 Results parameter would result\r
2203 in this type of error.\r
2204 @retval EFI_NOT_FOUND Target for the specified routing data\r
2205 was not found.\r
2206**/\r
2207EFI_STATUS\r
2208EFIAPI\r
2209HddPasswordFormRouteConfig (\r
2210 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
2211 IN CONST EFI_STRING Configuration,\r
2212 OUT EFI_STRING *Progress\r
2213 )\r
2214{\r
2215 if (Configuration == NULL || Progress == NULL) {\r
2216 return EFI_INVALID_PARAMETER;\r
2217 }\r
2218\r
2219 //\r
2220 // Check routing data in <ConfigHdr>.\r
2221 // Note: if only one Storage is used, then this checking could be skipped.\r
2222 //\r
2223 if (!HiiIsConfigHdrMatch (Configuration, &mHddPasswordVendorGuid, mHddPasswordVendorStorageName)) {\r
2224 *Progress = Configuration;\r
2225 return EFI_NOT_FOUND;\r
2226 }\r
2227\r
2228 *Progress = Configuration + StrLen (Configuration);\r
2229 return EFI_SUCCESS;\r
2230}\r
2231\r
2232/**\r
2233 This function is called to provide results data to the driver.\r
2234 This data consists of a unique key that is used to identify\r
2235 which data is either being passed back or being asked for.\r
2236\r
2237 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
2238 @param[in] Action Specifies the type of action taken by the browser.\r
2239 @param[in] QuestionId A unique value which is sent to the original\r
2240 exporting driver so that it can identify the type\r
2241 of data to expect. The format of the data tends to\r
2242 vary based on the opcode that enerated the callback.\r
2243 @param[in] Type The type of value for the question.\r
2244 @param[in] Value A pointer to the data being sent to the original\r
2245 exporting driver.\r
2246 @param[out] ActionRequest On return, points to the action requested by the\r
2247 callback function.\r
2248\r
2249 @retval EFI_SUCCESS The callback successfully handled the action.\r
2250 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the\r
2251 variable and its data.\r
2252 @retval EFI_DEVICE_ERROR The variable could not be saved.\r
2253 @retval EFI_UNSUPPORTED The specified Action is not supported by the\r
2254 callback.Currently not implemented.\r
2255 @retval EFI_INVALID_PARAMETERS Passing in wrong parameter.\r
2256 @retval Others Other errors as indicated.\r
2257**/\r
2258EFI_STATUS\r
2259EFIAPI\r
2260HddPasswordFormCallback (\r
2261 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
2262 IN EFI_BROWSER_ACTION Action,\r
2263 IN EFI_QUESTION_ID QuestionId,\r
2264 IN UINT8 Type,\r
2265 IN EFI_IFR_TYPE_VALUE *Value,\r
2266 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r
2267 )\r
2268{\r
2269 HDD_PASSWORD_DXE_PRIVATE_DATA *Private;\r
2270 EFI_STRING_ID DeviceFormTitleToken;\r
2271 HDD_PASSWORD_CONFIG *IfrData;\r
2272 HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry;\r
2273\r
2274 if (ActionRequest != NULL) {\r
2275 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_NONE;\r
2276 } else {\r
2277 return EFI_INVALID_PARAMETER;\r
2278 }\r
2279\r
2280 if ((Action != EFI_BROWSER_ACTION_CHANGING) && (Action != EFI_BROWSER_ACTION_CHANGED)) {\r
2281 //\r
2282 // Do nothing for other UEFI Action. Only do call back when data is changing or changed.\r
2283 //\r
2284 return EFI_UNSUPPORTED;\r
2285 }\r
2286\r
2287 Private = HDD_PASSWORD_DXE_PRIVATE_FROM_THIS (This);\r
2288\r
2289 //\r
2290 // Retrive data from Browser\r
2291 //\r
2292 IfrData = AllocateZeroPool (sizeof (HDD_PASSWORD_CONFIG));\r
2293 ASSERT (IfrData != NULL);\r
2294 if (!HiiGetBrowserData (&mHddPasswordVendorGuid, mHddPasswordVendorStorageName, sizeof (HDD_PASSWORD_CONFIG), (UINT8 *) IfrData)) {\r
2295 FreePool (IfrData);\r
2296 return EFI_NOT_FOUND;\r
2297 }\r
2298\r
2299 switch (QuestionId) {\r
2300 case KEY_HDD_USER_PASSWORD:\r
2301 if (Action == EFI_BROWSER_ACTION_CHANGED) {\r
2302 DEBUG ((DEBUG_INFO, "KEY_HDD_USER_PASSWORD\n"));\r
2303 ConfigFormEntry = Private->Current;\r
2304 ConfigFormEntry->IfrData.Request.UserPassword = Value->b;\r
2305 SaveHddPasswordRequest (ConfigFormEntry);\r
2306 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;\r
2307 }\r
2308 break;\r
2309 case KEY_HDD_MASTER_PASSWORD:\r
2310 if (Action == EFI_BROWSER_ACTION_CHANGED) {\r
2311 DEBUG ((DEBUG_INFO, "KEY_HDD_MASTER_PASSWORD\n"));\r
2312 ConfigFormEntry = Private->Current;\r
2313 ConfigFormEntry->IfrData.Request.MasterPassword = Value->b;\r
2314 SaveHddPasswordRequest (ConfigFormEntry);\r
2315 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;\r
2316 }\r
2317 break;\r
2318\r
2319 default:\r
2320 if ((QuestionId >= KEY_HDD_DEVICE_ENTRY_BASE) && (QuestionId < (mNumberOfHddDevices + KEY_HDD_DEVICE_ENTRY_BASE))) {\r
2321 if (Action == EFI_BROWSER_ACTION_CHANGING) {\r
2322 //\r
2323 // In case goto the device configuration form, update the device form title.\r
2324 //\r
2325 ConfigFormEntry = HddPasswordGetConfigFormEntryByIndex ((UINT32) (QuestionId - KEY_HDD_DEVICE_ENTRY_BASE));\r
2326 ASSERT (ConfigFormEntry != NULL);\r
2327\r
2328 DeviceFormTitleToken = (EFI_STRING_ID) STR_HDD_SECURITY_HD;\r
2329 HiiSetString (Private->HiiHandle, DeviceFormTitleToken, ConfigFormEntry->HddString, NULL);\r
2330\r
2331 Private->Current = ConfigFormEntry;\r
2332 CopyMem (IfrData, &ConfigFormEntry->IfrData, sizeof (HDD_PASSWORD_CONFIG));\r
2333 }\r
2334 }\r
2335\r
2336 break;\r
2337 }\r
2338\r
2339 //\r
2340 // Pass changed uncommitted data back to Form Browser\r
2341 //\r
2342 HiiSetBrowserData (&mHddPasswordVendorGuid, mHddPasswordVendorStorageName, sizeof (HDD_PASSWORD_CONFIG), (UINT8 *) IfrData, NULL);\r
2343\r
2344 FreePool (IfrData);\r
2345 return EFI_SUCCESS;\r
2346}\r
2347\r
2348/**\r
2349 Updates the HDD Password configuration form to add an entry for the attached\r
2350 ata harddisk device specified by the Controller.\r
2351\r
2352 @param[in] HiiHandle The HII Handle associated with the registered package list.\r
2353 @param[in] AtaPassThru Pointer to ATA_PASSTHRU instance.\r
2354 @param[in] PciIo Pointer to PCI_IO instance.\r
2355 @param[in] Controller The controller handle of the attached ata controller.\r
2356 @param[in] Bus The bus number of ATA controller.\r
2357 @param[in] Device The device number of ATA controller.\r
2358 @param[in] Function The function number of ATA controller.\r
2359 @param[in] Port The port number of attached ATA device.\r
2360 @param[in] PortMultiplierPort The port number of port multiplier of attached ATA device.\r
2361\r
2362 @retval EFI_SUCCESS The Hdd Password configuration form is updated.\r
2363 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
2364 @retval Others Other errors as indicated.\r
2365\r
2366**/\r
2367EFI_STATUS\r
2368HddPasswordConfigUpdateForm (\r
2369 IN EFI_HII_HANDLE HiiHandle,\r
2370 IN EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru,\r
2371 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
2372 IN EFI_HANDLE Controller,\r
2373 IN UINTN Bus,\r
2374 IN UINTN Device,\r
2375 IN UINTN Function,\r
2376 IN UINT16 Port,\r
2377 IN UINT16 PortMultiplierPort\r
2378 )\r
2379{\r
2380 LIST_ENTRY *Entry;\r
2381 HDD_PASSWORD_CONFIG_FORM_ENTRY *ConfigFormEntry;\r
2382 BOOLEAN EntryExisted;\r
2383 EFI_STATUS Status;\r
2384 VOID *StartOpCodeHandle;\r
2385 VOID *EndOpCodeHandle;\r
2386 EFI_IFR_GUID_LABEL *StartLabel;\r
2387 EFI_IFR_GUID_LABEL *EndLabel;\r
2388 CHAR16 HddString[40];\r
2389 ATA_IDENTIFY_DATA IdentifyData;\r
2390 EFI_DEVICE_PATH_PROTOCOL *AtaDeviceNode;\r
2391\r
2392 ConfigFormEntry = NULL;\r
2393 EntryExisted = FALSE;\r
2394\r
2395 EFI_LIST_FOR_EACH (Entry, &mHddPasswordConfigFormList) {\r
2396 ConfigFormEntry = BASE_CR (Entry, HDD_PASSWORD_CONFIG_FORM_ENTRY, Link);\r
2397\r
2398 if ((ConfigFormEntry->Bus == Bus) &&\r
2399 (ConfigFormEntry->Device == Device) &&\r
2400 (ConfigFormEntry->Function == Function) &&\r
2401 (ConfigFormEntry->Port == Port) &&\r
2402 (ConfigFormEntry->PortMultiplierPort == PortMultiplierPort)) {\r
2403 EntryExisted = TRUE;\r
2404 break;\r
2405 }\r
2406 }\r
2407\r
2408 if (!EntryExisted) {\r
2409 //\r
2410 // Add a new form.\r
2411 //\r
2412 ConfigFormEntry = AllocateZeroPool (sizeof (HDD_PASSWORD_CONFIG_FORM_ENTRY));\r
2413 if (ConfigFormEntry == NULL) {\r
2414 return EFI_OUT_OF_RESOURCES;\r
2415 }\r
2416\r
2417 InitializeListHead (&ConfigFormEntry->Link);\r
2418 ConfigFormEntry->Controller = Controller;\r
2419 ConfigFormEntry->Bus = Bus;\r
2420 ConfigFormEntry->Device = Device;\r
2421 ConfigFormEntry->Function = Function;\r
2422 ConfigFormEntry->Port = Port;\r
2423 ConfigFormEntry->PortMultiplierPort = PortMultiplierPort;\r
2424 ConfigFormEntry->AtaPassThru = AtaPassThru;\r
2425\r
2426 DEBUG ((DEBUG_INFO, "HddPasswordDxe: Create new form for device[%d][%d] at Bus 0x%x Dev 0x%x Func 0x%x\n", Port, PortMultiplierPort, Bus, Device, Function));\r
2427\r
2428 //\r
2429 // Construct the device path for the HDD password device\r
2430 //\r
2431 Status = AtaPassThru->BuildDevicePath (\r
2432 AtaPassThru,\r
2433 Port,\r
2434 PortMultiplierPort,\r
2435 &AtaDeviceNode\r
2436 );\r
2437 if (EFI_ERROR (Status)) {\r
2438 return Status;\r
2439 }\r
2440 ConfigFormEntry->DevicePath = AppendDevicePathNode (DevicePathFromHandle (Controller), AtaDeviceNode);\r
2441 FreePool (AtaDeviceNode);\r
2442 if (ConfigFormEntry->DevicePath == NULL) {\r
2443 return EFI_OUT_OF_RESOURCES;\r
2444 }\r
2445\r
2446 //\r
2447 // Get attached harddisk model number\r
2448 //\r
2449 Status = GetHddDeviceIdentifyData (AtaPassThru, Port, PortMultiplierPort, &IdentifyData);\r
2450 ASSERT_EFI_ERROR (Status);\r
2451 if (EFI_ERROR (Status)) {\r
2452 return Status;\r
2453 }\r
2454 GetHddDeviceModelNumber (&IdentifyData, HddString);\r
2455 //\r
2456 // Compose the HDD title string and help string of this port and create a new EFI_STRING_ID.\r
2457 //\r
2458 UnicodeSPrint (ConfigFormEntry->HddString, sizeof (ConfigFormEntry->HddString), L"HDD %d:%s", mNumberOfHddDevices, HddString);\r
2459 ConfigFormEntry->TitleToken = HiiSetString (HiiHandle, 0, ConfigFormEntry->HddString, NULL);\r
2460 ConfigFormEntry->TitleHelpToken = HiiSetString (HiiHandle, 0, L"Request to set HDD Password", NULL);\r
2461\r
2462 GetHddPasswordSecurityStatus (&IdentifyData, &ConfigFormEntry->IfrData);\r
2463\r
2464 InsertTailList (&mHddPasswordConfigFormList, &ConfigFormEntry->Link);\r
2465\r
2466 //\r
2467 // Init OpCode Handle\r
2468 //\r
2469 StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
2470 ASSERT (StartOpCodeHandle != NULL);\r
2471\r
2472 EndOpCodeHandle = HiiAllocateOpCodeHandle ();\r
2473 ASSERT (EndOpCodeHandle != NULL);\r
2474\r
2475 //\r
2476 // Create Hii Extend Label OpCode as the start opcode\r
2477 //\r
2478 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
2479 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
2480 StartLabel->Number = HDD_DEVICE_ENTRY_LABEL;\r
2481\r
2482 //\r
2483 // Create Hii Extend Label OpCode as the end opcode\r
2484 //\r
2485 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
2486 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
2487 EndLabel->Number = HDD_DEVICE_LABEL_END;\r
2488\r
2489 mNumberOfHddDevices = 0;\r
2490 EFI_LIST_FOR_EACH (Entry, &mHddPasswordConfigFormList) {\r
2491 ConfigFormEntry = BASE_CR (Entry, HDD_PASSWORD_CONFIG_FORM_ENTRY, Link);\r
2492\r
2493 HiiCreateGotoOpCode (\r
2494 StartOpCodeHandle, // Container for dynamic created opcodes\r
2495 FORMID_HDD_DEVICE_FORM, // Target Form ID\r
2496 ConfigFormEntry->TitleToken, // Prompt text\r
2497 ConfigFormEntry->TitleHelpToken, // Help text\r
2498 EFI_IFR_FLAG_CALLBACK, // Question flag\r
2499 (UINT16) (KEY_HDD_DEVICE_ENTRY_BASE + mNumberOfHddDevices) // Question ID\r
2500 );\r
2501\r
2502 mNumberOfHddDevices++;\r
2503 }\r
2504\r
2505 HiiUpdateForm (\r
2506 HiiHandle,\r
2507 &mHddPasswordVendorGuid,\r
2508 FORMID_HDD_MAIN_FORM,\r
2509 StartOpCodeHandle,\r
2510 EndOpCodeHandle\r
2511 );\r
2512\r
2513 HiiFreeOpCodeHandle (StartOpCodeHandle);\r
2514 HiiFreeOpCodeHandle (EndOpCodeHandle);\r
2515\r
2516 //\r
2517 // Check if device is locked and prompt for password.\r
2518 //\r
2519 HddPasswordRequestPassword (AtaPassThru, Port, PortMultiplierPort, ConfigFormEntry);\r
2520\r
2521 //\r
2522 // Process HDD password request from last boot.\r
2523 //\r
2524 ProcessHddPasswordRequest (AtaPassThru, Port, PortMultiplierPort, ConfigFormEntry);\r
2525 }\r
2526\r
2527 return EFI_SUCCESS;\r
2528}\r
2529\r
2530/**\r
2531 Ata Pass Thru Protocol notification event handler.\r
2532\r
2533 Check attached harddisk status to see if it's locked. If yes, then pop up a password windows to require user input.\r
2534 It also registers a form for user configuration on Hdd password configuration.\r
2535\r
2536 @param[in] Event Event whose notification function is being invoked.\r
2537 @param[in] Context Pointer to the notification function's context.\r
2538\r
2539**/\r
2540VOID\r
2541EFIAPI\r
2542HddPasswordNotificationEvent (\r
2543 IN EFI_EVENT Event,\r
2544 IN VOID *Context\r
2545 )\r
2546{\r
2547 EFI_STATUS Status;\r
2548 HDD_PASSWORD_DXE_PRIVATE_DATA *Private;\r
2549 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;\r
2550 UINT16 Port;\r
2551 UINT16 PortMultiplierPort;\r
2552 EFI_HANDLE Controller;\r
2553 EFI_HANDLE *HandleBuffer;\r
2554 UINTN HandleCount;\r
2555 UINTN Index;\r
2556 EFI_PCI_IO_PROTOCOL *PciIo;\r
2557 UINTN SegNum;\r
2558 UINTN BusNum;\r
2559 UINTN DevNum;\r
2560 UINTN FuncNum;\r
2561\r
2562 if (mHddPasswordEndOfDxe) {\r
2563 gBS->CloseEvent (Event);\r
2564 return;\r
2565 }\r
2566\r
2567 Private = (HDD_PASSWORD_DXE_PRIVATE_DATA *)Context;\r
2568\r
2569 //\r
2570 // Locate all handles of AtaPassThru protocol\r
2571 //\r
2572 Status = gBS->LocateHandleBuffer (\r
2573 ByProtocol,\r
2574 &gEfiAtaPassThruProtocolGuid,\r
2575 NULL,\r
2576 &HandleCount,\r
2577 &HandleBuffer\r
2578 );\r
2579 if (EFI_ERROR (Status)) {\r
2580 return ;\r
2581 }\r
2582\r
2583 //\r
2584 // Check attached hard disk status to see if it's locked\r
2585 //\r
2586 for (Index = 0; Index < HandleCount; Index += 1) {\r
2587 Controller = HandleBuffer[Index];\r
2588 Status = gBS->HandleProtocol (\r
2589 Controller,\r
2590 &gEfiAtaPassThruProtocolGuid,\r
2591 (VOID **) &AtaPassThru\r
2592 );\r
2593 if (EFI_ERROR (Status)) {\r
2594 break;\r
2595 }\r
2596\r
2597 //\r
2598 // Ignore those logical ATA_PASS_THRU instance.\r
2599 //\r
2600 if ((AtaPassThru->Mode->Attributes & EFI_ATA_PASS_THRU_ATTRIBUTES_PHYSICAL) == 0) {\r
2601 continue;\r
2602 }\r
2603\r
2604 Status = gBS->HandleProtocol (\r
2605 Controller,\r
2606 &gEfiPciIoProtocolGuid,\r
2607 (VOID **) &PciIo\r
2608 );\r
2609 ASSERT_EFI_ERROR (Status);\r
2610 if (EFI_ERROR (Status)) {\r
2611 break;\r
2612 }\r
2613\r
2614 Status = PciIo->GetLocation (\r
2615 PciIo,\r
2616 &SegNum,\r
2617 &BusNum,\r
2618 &DevNum,\r
2619 &FuncNum\r
2620 );\r
2621 ASSERT_EFI_ERROR (Status);\r
2622 if (EFI_ERROR (Status)) {\r
2623 break;\r
2624 }\r
2625\r
2626 //\r
2627 // Assume and only support Segment == 0.\r
2628 //\r
2629 ASSERT (SegNum == 0);\r
2630\r
2631 //\r
2632 // traverse all attached harddisk devices to update form and unlock it\r
2633 //\r
2634 Port = 0xFFFF;\r
2635\r
2636 while (TRUE) {\r
2637 Status = AtaPassThru->GetNextPort (AtaPassThru, &Port);\r
2638 if (EFI_ERROR (Status)) {\r
2639 //\r
2640 // We cannot find more legal port then we are done.\r
2641 //\r
2642 break;\r
2643 }\r
2644\r
2645 PortMultiplierPort = 0xFFFF;\r
2646 while (TRUE) {\r
2647 Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);\r
2648 if (EFI_ERROR (Status)) {\r
2649 //\r
2650 // We cannot find more legal port multiplier port number for ATA device\r
2651 // on the port, then we are done.\r
2652 //\r
2653 break;\r
2654 }\r
2655 //\r
2656 // Find out the attached harddisk devices.\r
2657 // Try to add a HDD Password configuration page for the attached devices.\r
2658 //\r
2659 gBS->RestoreTPL (TPL_APPLICATION);\r
2660 Status = HddPasswordConfigUpdateForm (Private->HiiHandle, AtaPassThru, PciIo, Controller, BusNum, DevNum, FuncNum, Port, PortMultiplierPort);\r
2661 gBS->RaiseTPL (TPL_CALLBACK);\r
2662 if (EFI_ERROR (Status)) {\r
2663 break;\r
2664 }\r
2665 }\r
2666 }\r
2667 }\r
2668\r
2669 FreePool (HandleBuffer);\r
2670 return ;\r
2671}\r
2672\r
2673/**\r
2674 Initialize the HDD Password configuration form.\r
2675\r
2676 @param[out] Instance Pointer to private instance.\r
2677\r
2678 @retval EFI_SUCCESS The HDD Password configuration form is initialized.\r
2679 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
2680 @retval Others Other errors as indicated.\r
2681**/\r
2682EFI_STATUS\r
2683HddPasswordConfigFormInit (\r
2684 OUT HDD_PASSWORD_DXE_PRIVATE_DATA **Instance\r
2685 )\r
2686{\r
2687 EFI_STATUS Status;\r
2688 HDD_PASSWORD_DXE_PRIVATE_DATA *Private;\r
2689\r
2690 InitializeListHead (&mHddPasswordConfigFormList);\r
2691\r
2692 Private = AllocateZeroPool (sizeof (HDD_PASSWORD_DXE_PRIVATE_DATA));\r
2693 if (Private == NULL) {\r
2694 return EFI_OUT_OF_RESOURCES;\r
2695 }\r
2696\r
2697 Private->Signature = HDD_PASSWORD_DXE_PRIVATE_SIGNATURE;\r
2698\r
2699 Private->ConfigAccess.ExtractConfig = HddPasswordFormExtractConfig;\r
2700 Private->ConfigAccess.RouteConfig = HddPasswordFormRouteConfig;\r
2701 Private->ConfigAccess.Callback = HddPasswordFormCallback;\r
2702\r
2703 //\r
2704 // Install Device Path Protocol and Config Access protocol to driver handle\r
2705 //\r
2706 Status = gBS->InstallMultipleProtocolInterfaces (\r
2707 &Private->DriverHandle,\r
2708 &gEfiDevicePathProtocolGuid,\r
2709 &mHddPasswordHiiVendorDevicePath,\r
2710 &gEfiHiiConfigAccessProtocolGuid,\r
2711 &Private->ConfigAccess,\r
2712 NULL\r
2713 );\r
2714 ASSERT_EFI_ERROR (Status);\r
2715 if (EFI_ERROR (Status)) {\r
2716 FreePool(Private);\r
2717 return Status;\r
2718 }\r
2719\r
2720 //\r
2721 // Publish our HII data\r
2722 //\r
2723 Private->HiiHandle = HiiAddPackages (\r
2724 &mHddPasswordVendorGuid,\r
2725 Private->DriverHandle,\r
2726 HddPasswordDxeStrings,\r
2727 HddPasswordBin,\r
2728 NULL\r
2729 );\r
2730 if (Private->HiiHandle == NULL) {\r
2731 FreePool(Private);\r
2732 return EFI_OUT_OF_RESOURCES;\r
2733 }\r
2734\r
2735 *Instance = Private;\r
2736 return Status;\r
2737}\r
2738\r
2739/**\r
2740 Main entry for this driver.\r
2741\r
2742 @param ImageHandle Image handle this driver.\r
2743 @param SystemTable Pointer to SystemTable.\r
2744\r
2745 @retval EFI_SUCESS This function always complete successfully.\r
2746\r
2747**/\r
2748EFI_STATUS\r
2749EFIAPI\r
2750HddPasswordDxeInit (\r
2751 IN EFI_HANDLE ImageHandle,\r
2752 IN EFI_SYSTEM_TABLE *SystemTable\r
2753 )\r
2754{\r
2755 EFI_STATUS Status;\r
2756 HDD_PASSWORD_DXE_PRIVATE_DATA *Private;\r
2757 EFI_EVENT Registration;\r
2758 EFI_EVENT EndOfDxeEvent;\r
2759 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;\r
2760\r
2761 Private = NULL;\r
2762\r
2763 //\r
2764 // Initialize the configuration form of HDD Password.\r
2765 //\r
2766 Status = HddPasswordConfigFormInit (&Private);\r
2767 if (EFI_ERROR (Status)) {\r
2768 return Status;\r
2769 }\r
2770\r
2771 //\r
2772 // Register HddPasswordNotificationEvent() notify function.\r
2773 //\r
2774 EfiCreateProtocolNotifyEvent (\r
2775 &gEfiAtaPassThruProtocolGuid,\r
2776 TPL_CALLBACK,\r
2777 HddPasswordNotificationEvent,\r
2778 (VOID *)Private,\r
2779 &Registration\r
2780 );\r
2781\r
2782 Status = gBS->CreateEventEx (\r
2783 EVT_NOTIFY_SIGNAL,\r
2784 TPL_CALLBACK,\r
2785 HddPasswordEndOfDxeEventNotify,\r
2786 NULL,\r
2787 &gEfiEndOfDxeEventGroupGuid,\r
2788 &EndOfDxeEvent\r
2789 );\r
2790 ASSERT_EFI_ERROR (Status);\r
2791\r
2792 //\r
2793 // Make HDD_PASSWORD_VARIABLE_NAME varible read-only.\r
2794 //\r
2795 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);\r
2796 if (!EFI_ERROR (Status)) {\r
2797 Status = VariableLock->RequestToLock (\r
2798 VariableLock,\r
2799 HDD_PASSWORD_VARIABLE_NAME,\r
2800 &mHddPasswordVendorGuid\r
2801 );\r
2802 DEBUG ((DEBUG_INFO, "%a(): Lock %s variable (%r)\n", __FUNCTION__, HDD_PASSWORD_VARIABLE_NAME, Status));\r
2803 ASSERT_EFI_ERROR (Status);\r
2804 }\r
2805\r
2806 return Status;\r
2807}\r