]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
Old driver sample code does not return an EFI_UNSUPPORTED status code if a callback...
[mirror_edk2.git] / MdeModulePkg / Universal / DriverSampleDxe / DriverSample.c
1 /** @file
2 This is an example of how a driver might export data to the HII protocol to be
3 later utilized by the Setup Protocol
4
5 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16
17 #include "DriverSample.h"
18
19 #define DISPLAY_ONLY_MY_ITEM 0x0002
20
21 EFI_GUID mFormSetGuid = FORMSET_GUID;
22 EFI_GUID mInventoryGuid = INVENTORY_GUID;
23
24 CHAR16 VariableName[] = L"MyIfrNVData";
25 EFI_HANDLE DriverHandle[2] = {NULL, NULL};
26 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData = NULL;
27
28 HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath0 = {
29 {
30 {
31 HARDWARE_DEVICE_PATH,
32 HW_VENDOR_DP,
33 {
34 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
35 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
36 }
37 },
38 //
39 // {C153B68D-EBFC-488e-B110-662867745B87}
40 //
41 { 0xc153b68d, 0xebfc, 0x488e, { 0xb1, 0x10, 0x66, 0x28, 0x67, 0x74, 0x5b, 0x87 } }
42 },
43 {
44 END_DEVICE_PATH_TYPE,
45 END_ENTIRE_DEVICE_PATH_SUBTYPE,
46 {
47 (UINT8) (END_DEVICE_PATH_LENGTH),
48 (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
49 }
50 }
51 };
52
53 HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath1 = {
54 {
55 {
56 HARDWARE_DEVICE_PATH,
57 HW_VENDOR_DP,
58 {
59 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
60 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
61 }
62 },
63 //
64 // {06F37F07-0C48-40e9-8436-0A08A0BB76B0}
65 //
66 { 0x6f37f07, 0xc48, 0x40e9, { 0x84, 0x36, 0xa, 0x8, 0xa0, 0xbb, 0x76, 0xb0 } }
67 },
68 {
69 END_DEVICE_PATH_TYPE,
70 END_ENTIRE_DEVICE_PATH_SUBTYPE,
71 {
72 (UINT8) (END_DEVICE_PATH_LENGTH),
73 (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
74 }
75 }
76 };
77
78 /**
79 Encode the password using a simple algorithm.
80
81 @param Password The string to be encoded.
82 @param MaxSize The size of the string.
83
84 **/
85 VOID
86 EncodePassword (
87 IN CHAR16 *Password,
88 IN UINTN MaxSize
89 )
90 {
91 UINTN Index;
92 UINTN Loop;
93 CHAR16 *Buffer;
94 CHAR16 *Key;
95
96 Key = L"MAR10648567";
97 Buffer = AllocateZeroPool (MaxSize);
98 ASSERT (Buffer != NULL);
99
100 for (Index = 0; Key[Index] != 0; Index++) {
101 for (Loop = 0; Loop < (UINT8) (MaxSize / 2); Loop++) {
102 Buffer[Loop] = (CHAR16) (Password[Loop] ^ Key[Index]);
103 }
104 }
105
106 CopyMem (Password, Buffer, MaxSize);
107
108 FreePool (Buffer);
109 return ;
110 }
111
112 /**
113 Validate the user's password.
114
115 @param PrivateData This driver's private context data.
116 @param StringId The user's input.
117
118 @retval EFI_SUCCESS The user's input matches the password.
119 @retval EFI_NOT_READY The user's input does not match the password.
120 **/
121 EFI_STATUS
122 ValidatePassword (
123 IN DRIVER_SAMPLE_PRIVATE_DATA *PrivateData,
124 IN EFI_STRING_ID StringId
125 )
126 {
127 EFI_STATUS Status;
128 UINTN Index;
129 UINTN BufferSize;
130 UINTN PasswordMaxSize;
131 CHAR16 *Password;
132 CHAR16 *EncodedPassword;
133 BOOLEAN OldPassword;
134
135 //
136 // Get encoded password first
137 //
138 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
139 Status = gRT->GetVariable (
140 VariableName,
141 &mFormSetGuid,
142 NULL,
143 &BufferSize,
144 &PrivateData->Configuration
145 );
146 if (EFI_ERROR (Status)) {
147 //
148 // Old password not exist, prompt for new password
149 //
150 return EFI_SUCCESS;
151 }
152
153 OldPassword = FALSE;
154 PasswordMaxSize = sizeof (PrivateData->Configuration.WhatIsThePassword2);
155 //
156 // Check whether we have any old password set
157 //
158 for (Index = 0; Index < PasswordMaxSize / sizeof (UINT16); Index++) {
159 if (PrivateData->Configuration.WhatIsThePassword2[Index] != 0) {
160 OldPassword = TRUE;
161 break;
162 }
163 }
164 if (!OldPassword) {
165 //
166 // Old password not exist, return EFI_SUCCESS to prompt for new password
167 //
168 return EFI_SUCCESS;
169 }
170
171 //
172 // Get user input password
173 //
174 Password = HiiGetString (PrivateData->HiiHandle[0], StringId, NULL);
175 if (Password == NULL) {
176 return EFI_NOT_READY;
177 }
178 if (StrSize (Password) > PasswordMaxSize) {
179 FreePool (Password);
180 return EFI_NOT_READY;
181 }
182
183 //
184 // Validate old password
185 //
186 EncodedPassword = AllocateZeroPool (PasswordMaxSize);
187 ASSERT (EncodedPassword != NULL);
188 StrnCpy (EncodedPassword, Password, StrLen (Password));
189 EncodePassword (EncodedPassword, StrLen (EncodedPassword) * sizeof (CHAR16));
190 if (CompareMem (EncodedPassword, PrivateData->Configuration.WhatIsThePassword2, PasswordMaxSize) != 0) {
191 //
192 // Old password mismatch, return EFI_NOT_READY to prompt for error message
193 //
194 Status = EFI_NOT_READY;
195 } else {
196 Status = EFI_SUCCESS;
197 }
198
199 FreePool (Password);
200 FreePool (EncodedPassword);
201
202 return Status;
203 }
204
205 /**
206 Encode the password using a simple algorithm.
207
208 @param PrivateData This driver's private context data.
209 @param StringId The password from User.
210
211 @retval EFI_SUCESS The operation is successful.
212 @return Other value if gRT->SetVariable () fails.
213
214 **/
215 EFI_STATUS
216 SetPassword (
217 IN DRIVER_SAMPLE_PRIVATE_DATA *PrivateData,
218 IN EFI_STRING_ID StringId
219 )
220 {
221 EFI_STATUS Status;
222 CHAR16 *Password;
223 CHAR16 *TempPassword;
224 UINTN PasswordSize;
225 DRIVER_SAMPLE_CONFIGURATION *Configuration;
226 UINTN BufferSize;
227
228 //
229 // Get Buffer Storage data from EFI variable
230 //
231 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
232 Status = gRT->GetVariable (
233 VariableName,
234 &mFormSetGuid,
235 NULL,
236 &BufferSize,
237 &PrivateData->Configuration
238 );
239 if (EFI_ERROR (Status)) {
240 return Status;
241 }
242
243 //
244 // Get user input password
245 //
246 Password = &PrivateData->Configuration.WhatIsThePassword2[0];
247 PasswordSize = sizeof (PrivateData->Configuration.WhatIsThePassword2);
248 ZeroMem (Password, PasswordSize);
249
250 TempPassword = HiiGetString (PrivateData->HiiHandle[0], StringId, NULL);
251 if (TempPassword == NULL) {
252 return EFI_NOT_READY;
253 }
254 if (StrSize (TempPassword) > PasswordSize) {
255 FreePool (TempPassword);
256 return EFI_NOT_READY;
257 }
258 StrnCpy (Password, TempPassword, StrLen (TempPassword));
259 FreePool (TempPassword);
260
261 //
262 // Retrive uncommitted data from Browser
263 //
264 Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION));
265 ASSERT (Configuration != NULL);
266 if (HiiGetBrowserData (&mFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) {
267 //
268 // Update password's clear text in the screen
269 //
270 CopyMem (Configuration->PasswordClearText, Password, StrSize (Password));
271
272 //
273 // Update uncommitted data of Browser
274 //
275 HiiSetBrowserData (
276 &mFormSetGuid,
277 VariableName,
278 sizeof (DRIVER_SAMPLE_CONFIGURATION),
279 (UINT8 *) Configuration,
280 NULL
281 );
282 }
283
284 //
285 // Free Configuration Buffer
286 //
287 FreePool (Configuration);
288
289
290 //
291 // Set password
292 //
293 EncodePassword (Password, StrLen (Password) * 2);
294 Status = gRT->SetVariable(
295 VariableName,
296 &mFormSetGuid,
297 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
298 sizeof (DRIVER_SAMPLE_CONFIGURATION),
299 &PrivateData->Configuration
300 );
301 return Status;
302 }
303
304 /**
305 Update names of Name/Value storage to current language.
306
307 @param PrivateData Points to the driver private data.
308
309 @retval EFI_SUCCESS All names are successfully updated.
310 @retval EFI_NOT_FOUND Failed to get Name from HII database.
311
312 **/
313 EFI_STATUS
314 LoadNameValueNames (
315 IN DRIVER_SAMPLE_PRIVATE_DATA *PrivateData
316 )
317 {
318 UINTN Index;
319
320 //
321 // Get Name/Value name string of current language
322 //
323 for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) {
324 PrivateData->NameValueName[Index] = HiiGetString (
325 PrivateData->HiiHandle[0],
326 PrivateData->NameStringId[Index],
327 NULL
328 );
329 if (PrivateData->NameValueName[Index] == NULL) {
330 return EFI_NOT_FOUND;
331 }
332 }
333
334 return EFI_SUCCESS;
335 }
336
337 /**
338 This function allows a caller to extract the current configuration for one
339 or more named elements from the target driver.
340
341 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
342 @param Request A null-terminated Unicode string in
343 <ConfigRequest> format.
344 @param Progress On return, points to a character in the Request
345 string. Points to the string's null terminator if
346 request was successful. Points to the most recent
347 '&' before the first failing name/value pair (or
348 the beginning of the string if the failure is in
349 the first name/value pair) if the request was not
350 successful.
351 @param Results A null-terminated Unicode string in
352 <ConfigAltResp> format which has all values filled
353 in for the names in the Request string. String to
354 be allocated by the called function.
355
356 @retval EFI_SUCCESS The Results is filled with the requested values.
357 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
358 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
359 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
360 driver.
361
362 **/
363 EFI_STATUS
364 EFIAPI
365 ExtractConfig (
366 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
367 IN CONST EFI_STRING Request,
368 OUT EFI_STRING *Progress,
369 OUT EFI_STRING *Results
370 )
371 {
372 EFI_STATUS Status;
373 UINTN BufferSize;
374 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData;
375 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
376 EFI_STRING ConfigRequest;
377 EFI_STRING ConfigRequestHdr;
378 UINTN Size;
379 EFI_STRING Value;
380 UINTN ValueStrLen;
381 CHAR16 BackupChar;
382 CHAR16 *StrPointer;
383 BOOLEAN AllocatedRequest;
384
385 if (Progress == NULL || Results == NULL) {
386 return EFI_INVALID_PARAMETER;
387 }
388 //
389 // Initialize the local variables.
390 //
391 ConfigRequestHdr = NULL;
392 ConfigRequest = NULL;
393 Size = 0;
394 *Progress = Request;
395 AllocatedRequest = FALSE;
396
397 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
398 HiiConfigRouting = PrivateData->HiiConfigRouting;
399
400 //
401 // Get Buffer Storage data from EFI variable.
402 // Try to get the current setting from variable.
403 //
404 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
405 Status = gRT->GetVariable (
406 VariableName,
407 &mFormSetGuid,
408 NULL,
409 &BufferSize,
410 &PrivateData->Configuration
411 );
412 if (EFI_ERROR (Status)) {
413 return EFI_NOT_FOUND;
414 }
415
416 if (Request == NULL) {
417 //
418 // Request is set to NULL, construct full request string.
419 //
420
421 //
422 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
423 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
424 //
425 ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, PrivateData->DriverHandle[0]);
426 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
427 ConfigRequest = AllocateZeroPool (Size);
428 ASSERT (ConfigRequest != NULL);
429 AllocatedRequest = TRUE;
430 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);
431 FreePool (ConfigRequestHdr);
432 } else {
433 //
434 // Check routing data in <ConfigHdr>.
435 // Note: if only one Storage is used, then this checking could be skipped.
436 //
437 if (!HiiIsConfigHdrMatch (Request, &mFormSetGuid, NULL)) {
438 return EFI_NOT_FOUND;
439 }
440 //
441 // Set Request to the unified request string.
442 //
443 ConfigRequest = Request;
444 //
445 // Check whether Request includes Request Element.
446 //
447 if (StrStr (Request, L"OFFSET") == NULL) {
448 //
449 // Check Request Element does exist in Reques String
450 //
451 StrPointer = StrStr (Request, L"PATH");
452 if (StrPointer == NULL) {
453 return EFI_INVALID_PARAMETER;
454 }
455 if (StrStr (StrPointer, L"&") == NULL) {
456 Size = (StrLen (Request) + 32 + 1) * sizeof (CHAR16);
457 ConfigRequest = AllocateZeroPool (Size);
458 ASSERT (ConfigRequest != NULL);
459 AllocatedRequest = TRUE;
460 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", Request, (UINT64)BufferSize);
461 }
462 }
463 }
464
465 //
466 // Check if requesting Name/Value storage
467 //
468 if (StrStr (ConfigRequest, L"OFFSET") == NULL) {
469 //
470 // Update Name/Value storage Names
471 //
472 Status = LoadNameValueNames (PrivateData);
473 if (EFI_ERROR (Status)) {
474 return Status;
475 }
476
477 //
478 // Allocate memory for <ConfigResp>, e.g. Name0=0x11, Name1=0x1234, Name2="ABCD"
479 // <Request> ::=<ConfigHdr>&Name0&Name1&Name2
480 // <ConfigResp>::=<ConfigHdr>&Name0=11&Name1=1234&Name2=0041004200430044
481 //
482 BufferSize = (StrLen (ConfigRequest) +
483 1 + sizeof (PrivateData->Configuration.NameValueVar0) * 2 +
484 1 + sizeof (PrivateData->Configuration.NameValueVar1) * 2 +
485 1 + sizeof (PrivateData->Configuration.NameValueVar2) * 2 + 1) * sizeof (CHAR16);
486 *Results = AllocateZeroPool (BufferSize);
487 ASSERT (*Results != NULL);
488 StrCpy (*Results, ConfigRequest);
489 Value = *Results;
490
491 //
492 // Append value of NameValueVar0, type is UINT8
493 //
494 if ((Value = StrStr (*Results, PrivateData->NameValueName[0])) != NULL) {
495 Value += StrLen (PrivateData->NameValueName[0]);
496 ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar0) * 2) + 1);
497 CopyMem (Value + ValueStrLen, Value, StrSize (Value));
498
499 BackupChar = Value[ValueStrLen];
500 *Value++ = L'=';
501 Value += UnicodeValueToString (
502 Value,
503 PREFIX_ZERO | RADIX_HEX,
504 PrivateData->Configuration.NameValueVar0,
505 sizeof (PrivateData->Configuration.NameValueVar0) * 2
506 );
507 *Value = BackupChar;
508 }
509
510 //
511 // Append value of NameValueVar1, type is UINT16
512 //
513 if ((Value = StrStr (*Results, PrivateData->NameValueName[1])) != NULL) {
514 Value += StrLen (PrivateData->NameValueName[1]);
515 ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar1) * 2) + 1);
516 CopyMem (Value + ValueStrLen, Value, StrSize (Value));
517
518 BackupChar = Value[ValueStrLen];
519 *Value++ = L'=';
520 Value += UnicodeValueToString (
521 Value,
522 PREFIX_ZERO | RADIX_HEX,
523 PrivateData->Configuration.NameValueVar1,
524 sizeof (PrivateData->Configuration.NameValueVar1) * 2
525 );
526 *Value = BackupChar;
527 }
528
529 //
530 // Append value of NameValueVar2, type is CHAR16 *
531 //
532 if ((Value = StrStr (*Results, PrivateData->NameValueName[2])) != NULL) {
533 Value += StrLen (PrivateData->NameValueName[2]);
534 ValueStrLen = StrLen (PrivateData->Configuration.NameValueVar2) * 4 + 1;
535 CopyMem (Value + ValueStrLen, Value, StrSize (Value));
536
537 *Value++ = L'=';
538 //
539 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
540 //
541 StrPointer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;
542 for (; *StrPointer != L'\0'; StrPointer++) {
543 Value += UnicodeValueToString (Value, PREFIX_ZERO | RADIX_HEX, *StrPointer, 4);
544 }
545 }
546
547 Status = EFI_SUCCESS;
548 } else {
549 //
550 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
551 //
552 Status = HiiConfigRouting->BlockToConfig (
553 HiiConfigRouting,
554 ConfigRequest,
555 (UINT8 *) &PrivateData->Configuration,
556 BufferSize,
557 Results,
558 Progress
559 );
560 }
561
562 //
563 // Free the allocated config request string.
564 //
565 if (AllocatedRequest) {
566 FreePool (ConfigRequest);
567 }
568 //
569 // Set Progress string to the original request string.
570 //
571 if (Request == NULL) {
572 *Progress = NULL;
573 } else if (StrStr (Request, L"OFFSET") == NULL) {
574 *Progress = Request + StrLen (Request);
575 }
576
577 return Status;
578 }
579
580
581 /**
582 This function processes the results of changes in configuration.
583
584 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
585 @param Configuration A null-terminated Unicode string in <ConfigResp>
586 format.
587 @param Progress A pointer to a string filled in with the offset of
588 the most recent '&' before the first failing
589 name/value pair (or the beginning of the string if
590 the failure is in the first name/value pair) or
591 the terminating NULL if all was successful.
592
593 @retval EFI_SUCCESS The Results is processed successfully.
594 @retval EFI_INVALID_PARAMETER Configuration is NULL.
595 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
596 driver.
597
598 **/
599 EFI_STATUS
600 EFIAPI
601 RouteConfig (
602 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
603 IN CONST EFI_STRING Configuration,
604 OUT EFI_STRING *Progress
605 )
606 {
607 EFI_STATUS Status;
608 UINTN BufferSize;
609 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData;
610 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
611 CHAR16 *Value;
612 CHAR16 *StrPtr;
613 CHAR16 TemStr[5];
614 UINT8 *DataBuffer;
615 UINT8 DigitUint8;
616 UINTN Index;
617 CHAR16 *StrBuffer;
618
619 if (Configuration == NULL || Progress == NULL) {
620 return EFI_INVALID_PARAMETER;
621 }
622
623 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
624 HiiConfigRouting = PrivateData->HiiConfigRouting;
625 *Progress = Configuration;
626
627 //
628 // Check routing data in <ConfigHdr>.
629 // Note: if only one Storage is used, then this checking could be skipped.
630 //
631 if (!HiiIsConfigHdrMatch (Configuration, &mFormSetGuid, NULL)) {
632 return EFI_NOT_FOUND;
633 }
634
635 //
636 // Get Buffer Storage data from EFI variable
637 //
638 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
639 Status = gRT->GetVariable (
640 VariableName,
641 &mFormSetGuid,
642 NULL,
643 &BufferSize,
644 &PrivateData->Configuration
645 );
646 if (EFI_ERROR (Status)) {
647 return Status;
648 }
649
650 //
651 // Check if configuring Name/Value storage
652 //
653 if (StrStr (Configuration, L"OFFSET") == NULL) {
654 //
655 // Update Name/Value storage Names
656 //
657 Status = LoadNameValueNames (PrivateData);
658 if (EFI_ERROR (Status)) {
659 return Status;
660 }
661
662 //
663 // Convert value for NameValueVar0
664 //
665 if ((Value = StrStr (Configuration, PrivateData->NameValueName[0])) != NULL) {
666 //
667 // Skip "Name="
668 //
669 Value += StrLen (PrivateData->NameValueName[0]);
670 Value++;
671 //
672 // Get Value String
673 //
674 StrPtr = StrStr (Value, L"&");
675 if (StrPtr == NULL) {
676 StrPtr = Value + StrLen (Value);
677 }
678 //
679 // Convert Value to Buffer data
680 //
681 DataBuffer = (UINT8 *) &PrivateData->Configuration.NameValueVar0;
682 ZeroMem (TemStr, sizeof (TemStr));
683 for (Index = 0, StrPtr --; StrPtr >= Value; StrPtr --, Index ++) {
684 TemStr[0] = *StrPtr;
685 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
686 if ((Index & 1) == 0) {
687 DataBuffer [Index/2] = DigitUint8;
688 } else {
689 DataBuffer [Index/2] = (UINT8) ((UINT8) (DigitUint8 << 4) + DataBuffer [Index/2]);
690 }
691 }
692 }
693
694 //
695 // Convert value for NameValueVar1
696 //
697 if ((Value = StrStr (Configuration, PrivateData->NameValueName[1])) != NULL) {
698 //
699 // Skip "Name="
700 //
701 Value += StrLen (PrivateData->NameValueName[1]);
702 Value++;
703 //
704 // Get Value String
705 //
706 StrPtr = StrStr (Value, L"&");
707 if (StrPtr == NULL) {
708 StrPtr = Value + StrLen (Value);
709 }
710 //
711 // Convert Value to Buffer data
712 //
713 DataBuffer = (UINT8 *) &PrivateData->Configuration.NameValueVar1;
714 ZeroMem (TemStr, sizeof (TemStr));
715 for (Index = 0, StrPtr --; StrPtr >= Value; StrPtr --, Index ++) {
716 TemStr[0] = *StrPtr;
717 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
718 if ((Index & 1) == 0) {
719 DataBuffer [Index/2] = DigitUint8;
720 } else {
721 DataBuffer [Index/2] = (UINT8) ((UINT8) (DigitUint8 << 4) + DataBuffer [Index/2]);
722 }
723 }
724 }
725
726 //
727 // Convert value for NameValueVar2
728 //
729 if ((Value = StrStr (Configuration, PrivateData->NameValueName[2])) != NULL) {
730 //
731 // Skip "Name="
732 //
733 Value += StrLen (PrivateData->NameValueName[2]);
734 Value++;
735 //
736 // Get Value String
737 //
738 StrPtr = StrStr (Value, L"&");
739 if (StrPtr == NULL) {
740 StrPtr = Value + StrLen (Value);
741 }
742 //
743 // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"
744 //
745 StrBuffer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;
746 ZeroMem (TemStr, sizeof (TemStr));
747 while (Value < StrPtr) {
748 StrnCpy (TemStr, Value, 4);
749 *(StrBuffer++) = (CHAR16) StrHexToUint64 (TemStr);
750 Value += 4;
751 }
752 *StrBuffer = L'\0';
753 }
754
755 //
756 // Store Buffer Storage back to EFI variable
757 //
758 Status = gRT->SetVariable(
759 VariableName,
760 &mFormSetGuid,
761 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
762 sizeof (DRIVER_SAMPLE_CONFIGURATION),
763 &PrivateData->Configuration
764 );
765
766 return Status;
767 }
768
769 //
770 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()
771 //
772 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
773 Status = HiiConfigRouting->ConfigToBlock (
774 HiiConfigRouting,
775 Configuration,
776 (UINT8 *) &PrivateData->Configuration,
777 &BufferSize,
778 Progress
779 );
780 if (EFI_ERROR (Status)) {
781 return Status;
782 }
783
784 //
785 // Store Buffer Storage back to EFI variable
786 //
787 Status = gRT->SetVariable(
788 VariableName,
789 &mFormSetGuid,
790 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
791 sizeof (DRIVER_SAMPLE_CONFIGURATION),
792 &PrivateData->Configuration
793 );
794
795 return Status;
796 }
797
798
799 /**
800 This function processes the results of changes in configuration.
801
802 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
803 @param Action Specifies the type of action taken by the browser.
804 @param QuestionId A unique value which is sent to the original
805 exporting driver so that it can identify the type
806 of data to expect.
807 @param Type The type of value for the question.
808 @param Value A pointer to the data being sent to the original
809 exporting driver.
810 @param ActionRequest On return, points to the action requested by the
811 callback function.
812
813 @retval EFI_SUCCESS The callback successfully handled the action.
814 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
815 variable and its data.
816 @retval EFI_DEVICE_ERROR The variable could not be saved.
817 @retval EFI_UNSUPPORTED The specified Action is not supported by the
818 callback.
819
820 **/
821 EFI_STATUS
822 EFIAPI
823 DriverCallback (
824 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
825 IN EFI_BROWSER_ACTION Action,
826 IN EFI_QUESTION_ID QuestionId,
827 IN UINT8 Type,
828 IN EFI_IFR_TYPE_VALUE *Value,
829 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
830 )
831 {
832 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData;
833 EFI_STATUS Status;
834 UINT8 MyVar;
835 VOID *StartOpCodeHandle;
836 VOID *OptionsOpCodeHandle;
837 EFI_IFR_GUID_LABEL *StartLabel;
838 VOID *EndOpCodeHandle;
839 EFI_IFR_GUID_LABEL *EndLabel;
840 EFI_INPUT_KEY Key;
841 DRIVER_SAMPLE_CONFIGURATION *Configuration;
842 UINTN MyVarSize;
843
844 if (((Value == NULL) && (Action != EFI_BROWSER_ACTION_FORM_OPEN) && (Action != EFI_BROWSER_ACTION_FORM_CLOSE))||
845 (ActionRequest == NULL)) {
846 return EFI_INVALID_PARAMETER;
847 }
848
849
850 Status = EFI_SUCCESS;
851 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
852
853 switch (Action) {
854 case EFI_BROWSER_ACTION_FORM_OPEN:
855 {
856 if (QuestionId == 0x1234) {
857 //
858 // Sample CallBack for UEFI FORM_OPEN action:
859 // Add Save action into Form 3 when Form 1 is opened.
860 // This will be done only in FORM_OPEN CallBack of question with ID 0x1234 from Form 1.
861 //
862 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
863
864 //
865 // Initialize the container for dynamic opcodes
866 //
867 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
868 ASSERT (StartOpCodeHandle != NULL);
869
870 //
871 // Create Hii Extend Label OpCode as the start opcode
872 //
873 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
874 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
875 StartLabel->Number = LABEL_UPDATE2;
876
877 HiiCreateActionOpCode (
878 StartOpCodeHandle, // Container for dynamic created opcodes
879 0x1238, // Question ID
880 STRING_TOKEN(STR_SAVE_TEXT), // Prompt text
881 STRING_TOKEN(STR_SAVE_TEXT), // Help text
882 EFI_IFR_FLAG_CALLBACK, // Question flag
883 0 // Action String ID
884 );
885
886 HiiUpdateForm (
887 PrivateData->HiiHandle[0], // HII handle
888 &mFormSetGuid, // Formset GUID
889 0x3, // Form ID
890 StartOpCodeHandle, // Label for where to insert opcodes
891 NULL // Insert data
892 );
893
894 HiiFreeOpCodeHandle (StartOpCodeHandle);
895 }
896 }
897 break;
898
899 case EFI_BROWSER_ACTION_FORM_CLOSE:
900 {
901 if (QuestionId == 0x5678) {
902 //
903 // Sample CallBack for UEFI FORM_CLOSE action:
904 // Show up a pop-up to specify Form 3 will be closed when exit Form 3.
905 //
906 do {
907 CreatePopUp (
908 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
909 &Key,
910 L"",
911 L"You are going to leave third Form!",
912 L"Press ESC or ENTER to continue ...",
913 L"",
914 NULL
915 );
916 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));
917 }
918 }
919 break;
920
921 case EFI_BROWSER_ACTION_RETRIEVE:
922 {
923 if (QuestionId == 0x1111) {
924 //
925 // EfiVarstore question takes sample action (print value as debug information)
926 // after read/write question.
927 //
928 MyVarSize = 1;
929 Status = gRT->GetVariable(
930 L"MyVar",
931 &mFormSetGuid,
932 NULL,
933 &MyVarSize,
934 &MyVar
935 );
936 ASSERT_EFI_ERROR (Status);
937 DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));
938 }
939 }
940 break;
941
942 case EFI_BROWSER_ACTION_CHANGING:
943 {
944 switch (QuestionId) {
945 case 0x1234:
946 //
947 // Initialize the container for dynamic opcodes
948 //
949 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
950 ASSERT (StartOpCodeHandle != NULL);
951
952 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
953 ASSERT (EndOpCodeHandle != NULL);
954
955 //
956 // Create Hii Extend Label OpCode as the start opcode
957 //
958 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
959 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
960 StartLabel->Number = LABEL_UPDATE1;
961
962 //
963 // Create Hii Extend Label OpCode as the end opcode
964 //
965 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
966 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
967 EndLabel->Number = LABEL_END;
968
969 HiiCreateActionOpCode (
970 StartOpCodeHandle, // Container for dynamic created opcodes
971 0x1237, // Question ID
972 STRING_TOKEN(STR_EXIT_TEXT), // Prompt text
973 STRING_TOKEN(STR_EXIT_TEXT), // Help text
974 EFI_IFR_FLAG_CALLBACK, // Question flag
975 0 // Action String ID
976 );
977
978 //
979 // Create Option OpCode
980 //
981 OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
982 ASSERT (OptionsOpCodeHandle != NULL);
983
984 HiiCreateOneOfOptionOpCode (
985 OptionsOpCodeHandle,
986 STRING_TOKEN (STR_BOOT_OPTION1),
987 0,
988 EFI_IFR_NUMERIC_SIZE_1,
989 1
990 );
991
992 HiiCreateOneOfOptionOpCode (
993 OptionsOpCodeHandle,
994 STRING_TOKEN (STR_BOOT_OPTION2),
995 0,
996 EFI_IFR_NUMERIC_SIZE_1,
997 2
998 );
999
1000 //
1001 // Prepare initial value for the dynamic created oneof Question
1002 //
1003 PrivateData->Configuration.DynamicOneof = 2;
1004 Status = gRT->SetVariable(
1005 VariableName,
1006 &mFormSetGuid,
1007 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1008 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1009 &PrivateData->Configuration
1010 );
1011
1012 //
1013 // Set initial vlaue of dynamic created oneof Question in Form Browser
1014 //
1015 Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION));
1016 ASSERT (Configuration != NULL);
1017 if (HiiGetBrowserData (&mFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) {
1018 Configuration->DynamicOneof = 2;
1019
1020 //
1021 // Update uncommitted data of Browser
1022 //
1023 HiiSetBrowserData (
1024 &mFormSetGuid,
1025 VariableName,
1026 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1027 (UINT8 *) Configuration,
1028 NULL
1029 );
1030 }
1031 FreePool (Configuration);
1032
1033 HiiCreateOneOfOpCode (
1034 StartOpCodeHandle, // Container for dynamic created opcodes
1035 0x8001, // Question ID (or call it "key")
1036 CONFIGURATION_VARSTORE_ID, // VarStore ID
1037 (UINT16) DYNAMIC_ONE_OF_VAR_OFFSET, // Offset in Buffer Storage
1038 STRING_TOKEN (STR_ONE_OF_PROMPT), // Question prompt text
1039 STRING_TOKEN (STR_ONE_OF_HELP), // Question help text
1040 EFI_IFR_FLAG_CALLBACK, // Question flag
1041 EFI_IFR_NUMERIC_SIZE_1, // Data type of Question Value
1042 OptionsOpCodeHandle, // Option Opcode list
1043 NULL // Default Opcode is NULl
1044 );
1045
1046 HiiCreateOrderedListOpCode (
1047 StartOpCodeHandle, // Container for dynamic created opcodes
1048 0x8002, // Question ID
1049 CONFIGURATION_VARSTORE_ID, // VarStore ID
1050 (UINT16) DYNAMIC_ORDERED_LIST_VAR_OFFSET, // Offset in Buffer Storage
1051 STRING_TOKEN (STR_BOOT_OPTIONS), // Question prompt text
1052 STRING_TOKEN (STR_BOOT_OPTIONS), // Question help text
1053 EFI_IFR_FLAG_RESET_REQUIRED, // Question flag
1054 0, // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET
1055 EFI_IFR_NUMERIC_SIZE_1, // Data type of Question value
1056 5, // Maximum container
1057 OptionsOpCodeHandle, // Option Opcode list
1058 NULL // Default Opcode is NULl
1059 );
1060
1061 HiiCreateTextOpCode (
1062 StartOpCodeHandle,
1063 STRING_TOKEN(STR_TEXT_SAMPLE_HELP),
1064 STRING_TOKEN(STR_TEXT_SAMPLE_HELP),
1065 STRING_TOKEN(STR_TEXT_SAMPLE_STRING)
1066 );
1067
1068 HiiCreateDateOpCode (
1069 StartOpCodeHandle,
1070 0x8004,
1071 0x0,
1072 0x0,
1073 STRING_TOKEN(STR_DATE_SAMPLE_HELP),
1074 STRING_TOKEN(STR_DATE_SAMPLE_HELP),
1075 0,
1076 QF_DATE_STORAGE_TIME,
1077 NULL
1078 );
1079
1080 HiiCreateTimeOpCode (
1081 StartOpCodeHandle,
1082 0x8005,
1083 0x0,
1084 0x0,
1085 STRING_TOKEN(STR_TIME_SAMPLE_HELP),
1086 STRING_TOKEN(STR_TIME_SAMPLE_HELP),
1087 0,
1088 QF_TIME_STORAGE_TIME,
1089 NULL
1090 );
1091
1092 HiiCreateGotoOpCode (
1093 StartOpCodeHandle, // Container for dynamic created opcodes
1094 1, // Target Form ID
1095 STRING_TOKEN (STR_GOTO_FORM1), // Prompt text
1096 STRING_TOKEN (STR_GOTO_HELP), // Help text
1097 0, // Question flag
1098 0x8003 // Question ID
1099 );
1100
1101 HiiUpdateForm (
1102 PrivateData->HiiHandle[0], // HII handle
1103 &mFormSetGuid, // Formset GUID
1104 0x1234, // Form ID
1105 StartOpCodeHandle, // Label for where to insert opcodes
1106 EndOpCodeHandle // Replace data
1107 );
1108
1109 HiiFreeOpCodeHandle (StartOpCodeHandle);
1110 HiiFreeOpCodeHandle (OptionsOpCodeHandle);
1111 HiiFreeOpCodeHandle (EndOpCodeHandle);
1112 break;
1113
1114 case 0x5678:
1115 //
1116 // We will reach here once the Question is refreshed
1117 //
1118
1119 //
1120 // Initialize the container for dynamic opcodes
1121 //
1122 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
1123 ASSERT (StartOpCodeHandle != NULL);
1124
1125 //
1126 // Create Hii Extend Label OpCode as the start opcode
1127 //
1128 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
1129 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
1130 StartLabel->Number = LABEL_UPDATE2;
1131
1132 HiiCreateActionOpCode (
1133 StartOpCodeHandle, // Container for dynamic created opcodes
1134 0x1237, // Question ID
1135 STRING_TOKEN(STR_EXIT_TEXT), // Prompt text
1136 STRING_TOKEN(STR_EXIT_TEXT), // Help text
1137 EFI_IFR_FLAG_CALLBACK, // Question flag
1138 0 // Action String ID
1139 );
1140
1141 HiiUpdateForm (
1142 PrivateData->HiiHandle[0], // HII handle
1143 &mFormSetGuid, // Formset GUID
1144 0x3, // Form ID
1145 StartOpCodeHandle, // Label for where to insert opcodes
1146 NULL // Insert data
1147 );
1148
1149 HiiFreeOpCodeHandle (StartOpCodeHandle);
1150
1151 //
1152 // Refresh the Question value
1153 //
1154 PrivateData->Configuration.DynamicRefresh++;
1155 Status = gRT->SetVariable(
1156 VariableName,
1157 &mFormSetGuid,
1158 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1159 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1160 &PrivateData->Configuration
1161 );
1162
1163 //
1164 // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause
1165 // the first statement in Form 3 be suppressed
1166 //
1167 MyVarSize = 1;
1168 MyVar = 111;
1169 Status = gRT->SetVariable(
1170 L"MyVar",
1171 &mFormSetGuid,
1172 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1173 MyVarSize,
1174 &MyVar
1175 );
1176 break;
1177
1178 case 0x1237:
1179 //
1180 // User press "Exit now", request Browser to exit
1181 //
1182 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1183 break;
1184
1185 case 0x1238:
1186 //
1187 // User press "Save now", request Browser to save the uncommitted data.
1188 //
1189 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1190 break;
1191
1192 case 0x2000:
1193 //
1194 // Only used to update the state.
1195 //
1196 if ((Type == EFI_IFR_TYPE_STRING) && (Value->string == 0) &&
1197 (PrivateData->PasswordState == BROWSER_STATE_SET_PASSWORD)) {
1198 PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;
1199 return EFI_INVALID_PARAMETER;
1200 }
1201
1202 //
1203 // When try to set a new password, user will be chanlleged with old password.
1204 // The Callback is responsible for validating old password input by user,
1205 // If Callback return EFI_SUCCESS, it indicates validation pass.
1206 //
1207 switch (PrivateData->PasswordState) {
1208 case BROWSER_STATE_VALIDATE_PASSWORD:
1209 Status = ValidatePassword (PrivateData, Value->string);
1210 if (Status == EFI_SUCCESS) {
1211 PrivateData->PasswordState = BROWSER_STATE_SET_PASSWORD;
1212 }
1213 break;
1214
1215 case BROWSER_STATE_SET_PASSWORD:
1216 Status = SetPassword (PrivateData, Value->string);
1217 PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;
1218 break;
1219
1220 default:
1221 break;
1222 }
1223
1224 break;
1225
1226 case 0x1111:
1227 //
1228 // EfiVarstore question takes sample action (print value as debug information)
1229 // after read/write question.
1230 //
1231 MyVarSize = 1;
1232 Status = gRT->GetVariable(
1233 L"MyVar",
1234 &mFormSetGuid,
1235 NULL,
1236 &MyVarSize,
1237 &MyVar
1238 );
1239 ASSERT_EFI_ERROR (Status);
1240 DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));
1241 default:
1242 break;
1243 }
1244 }
1245 break;
1246
1247 default:
1248 Status = EFI_UNSUPPORTED;
1249 break;
1250 }
1251
1252 return Status;
1253 }
1254
1255 /**
1256 Main entry for this driver.
1257
1258 @param ImageHandle Image handle this driver.
1259 @param SystemTable Pointer to SystemTable.
1260
1261 @retval EFI_SUCESS This function always complete successfully.
1262
1263 **/
1264 EFI_STATUS
1265 EFIAPI
1266 DriverSampleInit (
1267 IN EFI_HANDLE ImageHandle,
1268 IN EFI_SYSTEM_TABLE *SystemTable
1269 )
1270 {
1271 EFI_STATUS Status;
1272 EFI_HII_HANDLE HiiHandle[2];
1273 EFI_SCREEN_DESCRIPTOR Screen;
1274 EFI_HII_DATABASE_PROTOCOL *HiiDatabase;
1275 EFI_HII_STRING_PROTOCOL *HiiString;
1276 EFI_FORM_BROWSER2_PROTOCOL *FormBrowser2;
1277 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
1278 CHAR16 *NewString;
1279 UINTN BufferSize;
1280 DRIVER_SAMPLE_CONFIGURATION *Configuration;
1281 BOOLEAN ActionFlag;
1282 EFI_STRING ConfigRequestHdr;
1283
1284 //
1285 // Initialize the local variables.
1286 //
1287 ConfigRequestHdr = NULL;
1288 //
1289 // Initialize screen dimensions for SendForm().
1290 // Remove 3 characters from top and bottom
1291 //
1292 ZeroMem (&Screen, sizeof (EFI_SCREEN_DESCRIPTOR));
1293 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Screen.RightColumn, &Screen.BottomRow);
1294
1295 Screen.TopRow = 3;
1296 Screen.BottomRow = Screen.BottomRow - 3;
1297
1298 //
1299 // Initialize driver private data
1300 //
1301 PrivateData = AllocateZeroPool (sizeof (DRIVER_SAMPLE_PRIVATE_DATA));
1302 if (PrivateData == NULL) {
1303 return EFI_OUT_OF_RESOURCES;
1304 }
1305
1306 PrivateData->Signature = DRIVER_SAMPLE_PRIVATE_SIGNATURE;
1307
1308 PrivateData->ConfigAccess.ExtractConfig = ExtractConfig;
1309 PrivateData->ConfigAccess.RouteConfig = RouteConfig;
1310 PrivateData->ConfigAccess.Callback = DriverCallback;
1311 PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;
1312
1313 //
1314 // Locate Hii Database protocol
1315 //
1316 Status = gBS->LocateProtocol (&gEfiHiiDatabaseProtocolGuid, NULL, (VOID **) &HiiDatabase);
1317 if (EFI_ERROR (Status)) {
1318 return Status;
1319 }
1320 PrivateData->HiiDatabase = HiiDatabase;
1321
1322 //
1323 // Locate HiiString protocol
1324 //
1325 Status = gBS->LocateProtocol (&gEfiHiiStringProtocolGuid, NULL, (VOID **) &HiiString);
1326 if (EFI_ERROR (Status)) {
1327 return Status;
1328 }
1329 PrivateData->HiiString = HiiString;
1330
1331 //
1332 // Locate Formbrowser2 protocol
1333 //
1334 Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &FormBrowser2);
1335 if (EFI_ERROR (Status)) {
1336 return Status;
1337 }
1338 PrivateData->FormBrowser2 = FormBrowser2;
1339
1340 //
1341 // Locate ConfigRouting protocol
1342 //
1343 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &HiiConfigRouting);
1344 if (EFI_ERROR (Status)) {
1345 return Status;
1346 }
1347 PrivateData->HiiConfigRouting = HiiConfigRouting;
1348
1349 Status = gBS->InstallMultipleProtocolInterfaces (
1350 &DriverHandle[0],
1351 &gEfiDevicePathProtocolGuid,
1352 &mHiiVendorDevicePath0,
1353 &gEfiHiiConfigAccessProtocolGuid,
1354 &PrivateData->ConfigAccess,
1355 NULL
1356 );
1357 ASSERT_EFI_ERROR (Status);
1358
1359 PrivateData->DriverHandle[0] = DriverHandle[0];
1360
1361 //
1362 // Publish our HII data
1363 //
1364 HiiHandle[0] = HiiAddPackages (
1365 &mFormSetGuid,
1366 DriverHandle[0],
1367 DriverSampleStrings,
1368 VfrBin,
1369 NULL
1370 );
1371 if (HiiHandle[0] == NULL) {
1372 return EFI_OUT_OF_RESOURCES;
1373 }
1374
1375 PrivateData->HiiHandle[0] = HiiHandle[0];
1376
1377 //
1378 // Publish another Fromset
1379 //
1380 Status = gBS->InstallMultipleProtocolInterfaces (
1381 &DriverHandle[1],
1382 &gEfiDevicePathProtocolGuid,
1383 &mHiiVendorDevicePath1,
1384 NULL
1385 );
1386 ASSERT_EFI_ERROR (Status);
1387
1388 PrivateData->DriverHandle[1] = DriverHandle[1];
1389
1390 HiiHandle[1] = HiiAddPackages (
1391 &mInventoryGuid,
1392 DriverHandle[1],
1393 DriverSampleStrings,
1394 InventoryBin,
1395 NULL
1396 );
1397 if (HiiHandle[1] == NULL) {
1398 DriverSampleUnload (ImageHandle);
1399 return EFI_OUT_OF_RESOURCES;
1400 }
1401
1402 PrivateData->HiiHandle[1] = HiiHandle[1];
1403
1404 //
1405 // Very simple example of how one would update a string that is already
1406 // in the HII database
1407 //
1408 NewString = L"700 Mhz";
1409
1410 if (HiiSetString (HiiHandle[0], STRING_TOKEN (STR_CPU_STRING2), NewString, NULL) == 0) {
1411 DriverSampleUnload (ImageHandle);
1412 return EFI_OUT_OF_RESOURCES;
1413 }
1414
1415 HiiSetString (HiiHandle[0], 0, NewString, NULL);
1416
1417 //
1418 // Initialize Name/Value name String ID
1419 //
1420 PrivateData->NameStringId[0] = STR_NAME_VALUE_VAR_NAME0;
1421 PrivateData->NameStringId[1] = STR_NAME_VALUE_VAR_NAME1;
1422 PrivateData->NameStringId[2] = STR_NAME_VALUE_VAR_NAME2;
1423
1424 //
1425 // Initialize configuration data
1426 //
1427 Configuration = &PrivateData->Configuration;
1428 ZeroMem (Configuration, sizeof (DRIVER_SAMPLE_CONFIGURATION));
1429
1430 //
1431 // Try to read NV config EFI variable first
1432 //
1433 ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, DriverHandle[0]);
1434 ASSERT (ConfigRequestHdr != NULL);
1435
1436 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
1437 Status = gRT->GetVariable (VariableName, &mFormSetGuid, NULL, &BufferSize, Configuration);
1438 if (EFI_ERROR (Status)) {
1439 //
1440 // Store zero data Buffer Storage to EFI variable
1441 //
1442 Status = gRT->SetVariable(
1443 VariableName,
1444 &mFormSetGuid,
1445 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1446 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1447 Configuration
1448 );
1449 ASSERT (Status == EFI_SUCCESS);
1450 //
1451 // EFI variable for NV config doesn't exit, we should build this variable
1452 // based on default values stored in IFR
1453 //
1454 ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);
1455 ASSERT (ActionFlag);
1456 } else {
1457 //
1458 // EFI variable does exist and Validate Current Setting
1459 //
1460 ActionFlag = HiiValidateSettings (ConfigRequestHdr);
1461 ASSERT (ActionFlag);
1462 }
1463
1464 FreePool (ConfigRequestHdr);
1465
1466
1467 //
1468 // In default, this driver is built into Flash device image,
1469 // the following code doesn't run.
1470 //
1471
1472 //
1473 // Example of how to display only the item we sent to HII
1474 // When this driver is not built into Flash device image,
1475 // it need to call SendForm to show front page by itself.
1476 //
1477 if (DISPLAY_ONLY_MY_ITEM <= 1) {
1478 //
1479 // Have the browser pull out our copy of the data, and only display our data
1480 //
1481 Status = FormBrowser2->SendForm (
1482 FormBrowser2,
1483 &(HiiHandle[DISPLAY_ONLY_MY_ITEM]),
1484 1,
1485 NULL,
1486 0,
1487 NULL,
1488 NULL
1489 );
1490
1491 HiiRemovePackages (HiiHandle[0]);
1492
1493 HiiRemovePackages (HiiHandle[1]);
1494 }
1495
1496 return EFI_SUCCESS;
1497 }
1498
1499 /**
1500 Unloads the application and its installed protocol.
1501
1502 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1503
1504 @retval EFI_SUCCESS The image has been unloaded.
1505 **/
1506 EFI_STATUS
1507 EFIAPI
1508 DriverSampleUnload (
1509 IN EFI_HANDLE ImageHandle
1510 )
1511 {
1512 UINTN Index;
1513
1514 ASSERT (PrivateData != NULL);
1515
1516 if (DriverHandle[0] != NULL) {
1517 gBS->UninstallMultipleProtocolInterfaces (
1518 DriverHandle[0],
1519 &gEfiDevicePathProtocolGuid,
1520 &mHiiVendorDevicePath0,
1521 &gEfiHiiConfigAccessProtocolGuid,
1522 &PrivateData->ConfigAccess,
1523 NULL
1524 );
1525 DriverHandle[0] = NULL;
1526 }
1527
1528 if (DriverHandle[1] != NULL) {
1529 gBS->UninstallMultipleProtocolInterfaces (
1530 DriverHandle[1],
1531 &gEfiDevicePathProtocolGuid,
1532 &mHiiVendorDevicePath1,
1533 NULL
1534 );
1535 DriverHandle[1] = NULL;
1536 }
1537
1538 if (PrivateData->HiiHandle[0] != NULL) {
1539 HiiRemovePackages (PrivateData->HiiHandle[0]);
1540 }
1541
1542 if (PrivateData->HiiHandle[1] != NULL) {
1543 HiiRemovePackages (PrivateData->HiiHandle[1]);
1544 }
1545
1546 for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) {
1547 if (PrivateData->NameValueName[Index] != NULL) {
1548 FreePool (PrivateData->NameValueName[Index]);
1549 }
1550 }
1551 FreePool (PrivateData);
1552 PrivateData = NULL;
1553
1554 return EFI_SUCCESS;
1555 }