]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
Fixed SCT test failed caused by driver sample.
[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 /**
339 Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET
340 or WIDTH or VALUE.
341 <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
342
343 This is a internal function.
344
345 @param StringPtr String in <BlockConfig> format and points to the
346 first character of <Number>.
347 @param Number The output value. Caller takes the responsibility
348 to free memory.
349 @param Len Length of the <Number>, in characters.
350
351 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
352 structures.
353 @retval EFI_SUCCESS Value of <Number> is outputted in Number
354 successfully.
355
356 **/
357 EFI_STATUS
358 GetValueOfNumber (
359 IN EFI_STRING StringPtr,
360 OUT UINT8 **Number,
361 OUT UINTN *Len
362 )
363 {
364 EFI_STRING TmpPtr;
365 UINTN Length;
366 EFI_STRING Str;
367 UINT8 *Buf;
368 EFI_STATUS Status;
369 UINT8 DigitUint8;
370 UINTN Index;
371 CHAR16 TemStr[2];
372
373 if (StringPtr == NULL || *StringPtr == L'\0' || Number == NULL || Len == NULL) {
374 return EFI_INVALID_PARAMETER;
375 }
376
377 Buf = NULL;
378
379 TmpPtr = StringPtr;
380 while (*StringPtr != L'\0' && *StringPtr != L'&') {
381 StringPtr++;
382 }
383 *Len = StringPtr - TmpPtr;
384 Length = *Len + 1;
385
386 Str = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
387 if (Str == NULL) {
388 Status = EFI_OUT_OF_RESOURCES;
389 goto Exit;
390 }
391 CopyMem (Str, TmpPtr, *Len * sizeof (CHAR16));
392 *(Str + *Len) = L'\0';
393
394 Length = (Length + 1) / 2;
395 Buf = (UINT8 *) AllocateZeroPool (Length);
396 if (Buf == NULL) {
397 Status = EFI_OUT_OF_RESOURCES;
398 goto Exit;
399 }
400
401 Length = *Len;
402 ZeroMem (TemStr, sizeof (TemStr));
403 for (Index = 0; Index < Length; Index ++) {
404 TemStr[0] = Str[Length - Index - 1];
405 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
406 if ((Index & 1) == 0) {
407 Buf [Index/2] = DigitUint8;
408 } else {
409 Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]);
410 }
411 }
412
413 *Number = Buf;
414 Status = EFI_SUCCESS;
415
416 Exit:
417 if (Str != NULL) {
418 FreePool (Str);
419 }
420
421 return Status;
422 }
423
424 /**
425 Create altcfg string.
426
427 @param Result The request result string.
428 @param ConfigHdr The request head info. <ConfigHdr> format.
429 @param Offset The offset of the parameter int he structure.
430 @param Width The width of the parameter.
431
432
433 @retval The string with altcfg info append at the end.
434 **/
435 EFI_STRING
436 CreateAltCfgString (
437 IN EFI_STRING Result,
438 IN EFI_STRING ConfigHdr,
439 IN UINTN Offset,
440 IN UINTN Width
441 )
442 {
443 EFI_STRING StringPtr;
444 EFI_STRING TmpStr;
445 UINTN NewLen;
446
447 //
448 // String Len = ConfigResp + AltConfig + AltConfig + 1("\0")
449 //
450 NewLen = (StrLen (Result) + ((1 + StrLen (ConfigHdr) + 8 + 4) + (8 + 4 + 7 + 4 + 7 + 4)) * 2 + 1) * sizeof (CHAR16);
451 StringPtr = AllocateZeroPool (NewLen);
452 if (StringPtr == NULL) {
453 return NULL;
454 }
455
456 TmpStr = StringPtr;
457 if (Result != NULL) {
458 StrCpy (StringPtr, Result);
459 StringPtr += StrLen (Result);
460 FreePool (Result);
461 }
462
463 UnicodeSPrint (
464 StringPtr,
465 (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16),
466 L"&%s&ALTCFG=%04x",
467 ConfigHdr,
468 EFI_HII_DEFAULT_CLASS_STANDARD
469 );
470 StringPtr += StrLen (StringPtr);
471
472 UnicodeSPrint (
473 StringPtr,
474 (8 + 4 + 7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
475 L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x",
476 Offset,
477 Width,
478 DEFAULT_CLASS_STANDARD_VALUE
479 );
480 StringPtr += StrLen (StringPtr);
481
482 UnicodeSPrint (
483 StringPtr,
484 (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16),
485 L"&%s&ALTCFG=%04x",
486 ConfigHdr,
487 EFI_HII_DEFAULT_CLASS_MANUFACTURING
488 );
489 StringPtr += StrLen (StringPtr);
490
491 UnicodeSPrint (
492 StringPtr,
493 (8 + 4 + 7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
494 L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x",
495 Offset,
496 Width,
497 DEFAULT_CLASS_MANUFACTURING_VALUE
498 );
499 StringPtr += StrLen (StringPtr);
500
501 return TmpStr;
502 }
503
504 /**
505 Check whether need to add the altcfg string. if need to add, add the altcfg
506 string.
507
508 @param RequestResult The request result string.
509 @param ConfigRequestHdr The request head info. <ConfigHdr> format.
510
511 **/
512 VOID
513 AppendAltCfgString (
514 IN OUT EFI_STRING *RequestResult,
515 IN EFI_STRING ConfigRequestHdr
516 )
517 {
518 EFI_STRING StringPtr;
519 EFI_STRING TmpPtr;
520 UINTN Length;
521 UINT8 *TmpBuffer;
522 UINTN Offset;
523 UINTN Width;
524 UINTN BlockSize;
525 UINTN ValueOffset;
526 UINTN ValueWidth;
527 EFI_STATUS Status;
528
529 StringPtr = *RequestResult;
530 StringPtr = StrStr (StringPtr, L"OFFSET");
531 BlockSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
532 ValueOffset = OFFSET_OF (DRIVER_SAMPLE_CONFIGURATION, GetDefaultValueFromAccess);
533 ValueWidth = sizeof (((DRIVER_SAMPLE_CONFIGURATION *)0)->GetDefaultValueFromAccess);
534
535 if (StringPtr == NULL) {
536 return;
537 }
538
539 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
540 //
541 // Back up the header of one <BlockName>
542 //
543 TmpPtr = StringPtr;
544
545 StringPtr += StrLen (L"OFFSET=");
546 //
547 // Get Offset
548 //
549 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
550 if (EFI_ERROR (Status)) {
551 return;
552 }
553 Offset = 0;
554 CopyMem (
555 &Offset,
556 TmpBuffer,
557 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
558 );
559 FreePool (TmpBuffer);
560
561 StringPtr += Length;
562 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
563 return;
564 }
565 StringPtr += StrLen (L"&WIDTH=");
566
567 //
568 // Get Width
569 //
570 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
571 if (EFI_ERROR (Status)) {
572 return;
573 }
574 Width = 0;
575 CopyMem (
576 &Width,
577 TmpBuffer,
578 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
579 );
580 FreePool (TmpBuffer);
581
582 StringPtr += Length;
583 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
584 return;
585 }
586 StringPtr += StrLen (L"&VALUE=");
587
588 //
589 // Get Width
590 //
591 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
592 if (EFI_ERROR (Status)) {
593 return;
594 }
595 StringPtr += Length;
596
597 //
598 // Calculate Value and convert it to hex string.
599 //
600 if (Offset + Width > BlockSize) {
601 return;
602 }
603
604 if (Offset <= ValueOffset && Offset + Width >= ValueOffset + ValueWidth) {
605 *RequestResult = CreateAltCfgString(*RequestResult, ConfigRequestHdr, ValueOffset, ValueWidth);
606 return;
607 }
608 }
609 }
610
611 /**
612 This function allows a caller to extract the current configuration for one
613 or more named elements from the target driver.
614
615 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
616 @param Request A null-terminated Unicode string in
617 <ConfigRequest> format.
618 @param Progress On return, points to a character in the Request
619 string. Points to the string's null terminator if
620 request was successful. Points to the most recent
621 '&' before the first failing name/value pair (or
622 the beginning of the string if the failure is in
623 the first name/value pair) if the request was not
624 successful.
625 @param Results A null-terminated Unicode string in
626 <ConfigAltResp> format which has all values filled
627 in for the names in the Request string. String to
628 be allocated by the called function.
629
630 @retval EFI_SUCCESS The Results is filled with the requested values.
631 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
632 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
633 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
634 driver.
635
636 **/
637 EFI_STATUS
638 EFIAPI
639 ExtractConfig (
640 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
641 IN CONST EFI_STRING Request,
642 OUT EFI_STRING *Progress,
643 OUT EFI_STRING *Results
644 )
645 {
646 EFI_STATUS Status;
647 UINTN BufferSize;
648 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData;
649 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
650 EFI_STRING ConfigRequest;
651 EFI_STRING ConfigRequestHdr;
652 UINTN Size;
653 EFI_STRING Value;
654 UINTN ValueStrLen;
655 CHAR16 BackupChar;
656 CHAR16 *StrPointer;
657 BOOLEAN AllocatedRequest;
658
659 if (Progress == NULL || Results == NULL) {
660 return EFI_INVALID_PARAMETER;
661 }
662 //
663 // Initialize the local variables.
664 //
665 ConfigRequestHdr = NULL;
666 ConfigRequest = NULL;
667 Size = 0;
668 *Progress = Request;
669 AllocatedRequest = FALSE;
670
671 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
672 HiiConfigRouting = PrivateData->HiiConfigRouting;
673
674 //
675 // Get Buffer Storage data from EFI variable.
676 // Try to get the current setting from variable.
677 //
678 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
679 Status = gRT->GetVariable (
680 VariableName,
681 &mFormSetGuid,
682 NULL,
683 &BufferSize,
684 &PrivateData->Configuration
685 );
686 if (EFI_ERROR (Status)) {
687 return EFI_NOT_FOUND;
688 }
689
690 if (Request == NULL) {
691 //
692 // Request is set to NULL, construct full request string.
693 //
694
695 //
696 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
697 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
698 //
699 ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, PrivateData->DriverHandle[0]);
700 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
701 ConfigRequest = AllocateZeroPool (Size);
702 ASSERT (ConfigRequest != NULL);
703 AllocatedRequest = TRUE;
704 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);
705 FreePool (ConfigRequestHdr);
706 ConfigRequestHdr = NULL;
707 } else {
708 //
709 // Check routing data in <ConfigHdr>.
710 // Note: if only one Storage is used, then this checking could be skipped.
711 //
712 if (!HiiIsConfigHdrMatch (Request, &mFormSetGuid, NULL)) {
713 return EFI_NOT_FOUND;
714 }
715 //
716 // Set Request to the unified request string.
717 //
718 ConfigRequest = Request;
719 //
720 // Check whether Request includes Request Element.
721 //
722 if (StrStr (Request, L"OFFSET") == NULL) {
723 //
724 // Check Request Element does exist in Reques String
725 //
726 StrPointer = StrStr (Request, L"PATH");
727 if (StrPointer == NULL) {
728 return EFI_INVALID_PARAMETER;
729 }
730 if (StrStr (StrPointer, L"&") == NULL) {
731 Size = (StrLen (Request) + 32 + 1) * sizeof (CHAR16);
732 ConfigRequest = AllocateZeroPool (Size);
733 ASSERT (ConfigRequest != NULL);
734 AllocatedRequest = TRUE;
735 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", Request, (UINT64)BufferSize);
736 }
737 }
738 }
739
740 //
741 // Check if requesting Name/Value storage
742 //
743 if (StrStr (ConfigRequest, L"OFFSET") == NULL) {
744 //
745 // Update Name/Value storage Names
746 //
747 Status = LoadNameValueNames (PrivateData);
748 if (EFI_ERROR (Status)) {
749 return Status;
750 }
751
752 //
753 // Allocate memory for <ConfigResp>, e.g. Name0=0x11, Name1=0x1234, Name2="ABCD"
754 // <Request> ::=<ConfigHdr>&Name0&Name1&Name2
755 // <ConfigResp>::=<ConfigHdr>&Name0=11&Name1=1234&Name2=0041004200430044
756 //
757 BufferSize = (StrLen (ConfigRequest) +
758 1 + sizeof (PrivateData->Configuration.NameValueVar0) * 2 +
759 1 + sizeof (PrivateData->Configuration.NameValueVar1) * 2 +
760 1 + sizeof (PrivateData->Configuration.NameValueVar2) * 2 + 1) * sizeof (CHAR16);
761 *Results = AllocateZeroPool (BufferSize);
762 ASSERT (*Results != NULL);
763 StrCpy (*Results, ConfigRequest);
764 Value = *Results;
765
766 //
767 // Append value of NameValueVar0, type is UINT8
768 //
769 if ((Value = StrStr (*Results, PrivateData->NameValueName[0])) != NULL) {
770 Value += StrLen (PrivateData->NameValueName[0]);
771 ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar0) * 2) + 1);
772 CopyMem (Value + ValueStrLen, Value, StrSize (Value));
773
774 BackupChar = Value[ValueStrLen];
775 *Value++ = L'=';
776 Value += UnicodeValueToString (
777 Value,
778 PREFIX_ZERO | RADIX_HEX,
779 PrivateData->Configuration.NameValueVar0,
780 sizeof (PrivateData->Configuration.NameValueVar0) * 2
781 );
782 *Value = BackupChar;
783 }
784
785 //
786 // Append value of NameValueVar1, type is UINT16
787 //
788 if ((Value = StrStr (*Results, PrivateData->NameValueName[1])) != NULL) {
789 Value += StrLen (PrivateData->NameValueName[1]);
790 ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar1) * 2) + 1);
791 CopyMem (Value + ValueStrLen, Value, StrSize (Value));
792
793 BackupChar = Value[ValueStrLen];
794 *Value++ = L'=';
795 Value += UnicodeValueToString (
796 Value,
797 PREFIX_ZERO | RADIX_HEX,
798 PrivateData->Configuration.NameValueVar1,
799 sizeof (PrivateData->Configuration.NameValueVar1) * 2
800 );
801 *Value = BackupChar;
802 }
803
804 //
805 // Append value of NameValueVar2, type is CHAR16 *
806 //
807 if ((Value = StrStr (*Results, PrivateData->NameValueName[2])) != NULL) {
808 Value += StrLen (PrivateData->NameValueName[2]);
809 ValueStrLen = StrLen (PrivateData->Configuration.NameValueVar2) * 4 + 1;
810 CopyMem (Value + ValueStrLen, Value, StrSize (Value));
811
812 *Value++ = L'=';
813 //
814 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
815 //
816 StrPointer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;
817 for (; *StrPointer != L'\0'; StrPointer++) {
818 Value += UnicodeValueToString (Value, PREFIX_ZERO | RADIX_HEX, *StrPointer, 4);
819 }
820 }
821
822 Status = EFI_SUCCESS;
823 } else {
824 //
825 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
826 //
827 Status = HiiConfigRouting->BlockToConfig (
828 HiiConfigRouting,
829 ConfigRequest,
830 (UINT8 *) &PrivateData->Configuration,
831 BufferSize,
832 Results,
833 Progress
834 );
835 if (!EFI_ERROR (Status)) {
836 ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, PrivateData->DriverHandle[0]);
837 AppendAltCfgString(Results, ConfigRequestHdr);
838 }
839 }
840
841 //
842 // Free the allocated config request string.
843 //
844 if (AllocatedRequest) {
845 FreePool (ConfigRequest);
846 }
847
848 if (ConfigRequestHdr != NULL) {
849 FreePool (ConfigRequestHdr);
850 }
851 //
852 // Set Progress string to the original request string.
853 //
854 if (Request == NULL) {
855 *Progress = NULL;
856 } else if (StrStr (Request, L"OFFSET") == NULL) {
857 *Progress = Request + StrLen (Request);
858 }
859
860 return Status;
861 }
862
863
864 /**
865 This function processes the results of changes in configuration.
866
867 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
868 @param Configuration A null-terminated Unicode string in <ConfigResp>
869 format.
870 @param Progress A pointer to a string filled in with the offset of
871 the most recent '&' before the first failing
872 name/value pair (or the beginning of the string if
873 the failure is in the first name/value pair) or
874 the terminating NULL if all was successful.
875
876 @retval EFI_SUCCESS The Results is processed successfully.
877 @retval EFI_INVALID_PARAMETER Configuration is NULL.
878 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
879 driver.
880
881 **/
882 EFI_STATUS
883 EFIAPI
884 RouteConfig (
885 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
886 IN CONST EFI_STRING Configuration,
887 OUT EFI_STRING *Progress
888 )
889 {
890 EFI_STATUS Status;
891 UINTN BufferSize;
892 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData;
893 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
894 CHAR16 *Value;
895 CHAR16 *StrPtr;
896 CHAR16 TemStr[5];
897 UINT8 *DataBuffer;
898 UINT8 DigitUint8;
899 UINTN Index;
900 CHAR16 *StrBuffer;
901
902 if (Configuration == NULL || Progress == NULL) {
903 return EFI_INVALID_PARAMETER;
904 }
905
906 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
907 HiiConfigRouting = PrivateData->HiiConfigRouting;
908 *Progress = Configuration;
909
910 //
911 // Check routing data in <ConfigHdr>.
912 // Note: if only one Storage is used, then this checking could be skipped.
913 //
914 if (!HiiIsConfigHdrMatch (Configuration, &mFormSetGuid, NULL)) {
915 return EFI_NOT_FOUND;
916 }
917
918 //
919 // Get Buffer Storage data from EFI variable
920 //
921 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
922 Status = gRT->GetVariable (
923 VariableName,
924 &mFormSetGuid,
925 NULL,
926 &BufferSize,
927 &PrivateData->Configuration
928 );
929 if (EFI_ERROR (Status)) {
930 return Status;
931 }
932
933 //
934 // Check if configuring Name/Value storage
935 //
936 if (StrStr (Configuration, L"OFFSET") == NULL) {
937 //
938 // Update Name/Value storage Names
939 //
940 Status = LoadNameValueNames (PrivateData);
941 if (EFI_ERROR (Status)) {
942 return Status;
943 }
944
945 //
946 // Convert value for NameValueVar0
947 //
948 if ((Value = StrStr (Configuration, PrivateData->NameValueName[0])) != NULL) {
949 //
950 // Skip "Name="
951 //
952 Value += StrLen (PrivateData->NameValueName[0]);
953 Value++;
954 //
955 // Get Value String
956 //
957 StrPtr = StrStr (Value, L"&");
958 if (StrPtr == NULL) {
959 StrPtr = Value + StrLen (Value);
960 }
961 //
962 // Convert Value to Buffer data
963 //
964 DataBuffer = (UINT8 *) &PrivateData->Configuration.NameValueVar0;
965 ZeroMem (TemStr, sizeof (TemStr));
966 for (Index = 0, StrPtr --; StrPtr >= Value; StrPtr --, Index ++) {
967 TemStr[0] = *StrPtr;
968 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
969 if ((Index & 1) == 0) {
970 DataBuffer [Index/2] = DigitUint8;
971 } else {
972 DataBuffer [Index/2] = (UINT8) ((UINT8) (DigitUint8 << 4) + DataBuffer [Index/2]);
973 }
974 }
975 }
976
977 //
978 // Convert value for NameValueVar1
979 //
980 if ((Value = StrStr (Configuration, PrivateData->NameValueName[1])) != NULL) {
981 //
982 // Skip "Name="
983 //
984 Value += StrLen (PrivateData->NameValueName[1]);
985 Value++;
986 //
987 // Get Value String
988 //
989 StrPtr = StrStr (Value, L"&");
990 if (StrPtr == NULL) {
991 StrPtr = Value + StrLen (Value);
992 }
993 //
994 // Convert Value to Buffer data
995 //
996 DataBuffer = (UINT8 *) &PrivateData->Configuration.NameValueVar1;
997 ZeroMem (TemStr, sizeof (TemStr));
998 for (Index = 0, StrPtr --; StrPtr >= Value; StrPtr --, Index ++) {
999 TemStr[0] = *StrPtr;
1000 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
1001 if ((Index & 1) == 0) {
1002 DataBuffer [Index/2] = DigitUint8;
1003 } else {
1004 DataBuffer [Index/2] = (UINT8) ((UINT8) (DigitUint8 << 4) + DataBuffer [Index/2]);
1005 }
1006 }
1007 }
1008
1009 //
1010 // Convert value for NameValueVar2
1011 //
1012 if ((Value = StrStr (Configuration, PrivateData->NameValueName[2])) != NULL) {
1013 //
1014 // Skip "Name="
1015 //
1016 Value += StrLen (PrivateData->NameValueName[2]);
1017 Value++;
1018 //
1019 // Get Value String
1020 //
1021 StrPtr = StrStr (Value, L"&");
1022 if (StrPtr == NULL) {
1023 StrPtr = Value + StrLen (Value);
1024 }
1025 //
1026 // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"
1027 //
1028 StrBuffer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;
1029 ZeroMem (TemStr, sizeof (TemStr));
1030 while (Value < StrPtr) {
1031 StrnCpy (TemStr, Value, 4);
1032 *(StrBuffer++) = (CHAR16) StrHexToUint64 (TemStr);
1033 Value += 4;
1034 }
1035 *StrBuffer = L'\0';
1036 }
1037
1038 //
1039 // Store Buffer Storage back to EFI variable
1040 //
1041 Status = gRT->SetVariable(
1042 VariableName,
1043 &mFormSetGuid,
1044 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1045 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1046 &PrivateData->Configuration
1047 );
1048
1049 return Status;
1050 }
1051
1052 //
1053 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()
1054 //
1055 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
1056 Status = HiiConfigRouting->ConfigToBlock (
1057 HiiConfigRouting,
1058 Configuration,
1059 (UINT8 *) &PrivateData->Configuration,
1060 &BufferSize,
1061 Progress
1062 );
1063 if (EFI_ERROR (Status)) {
1064 return Status;
1065 }
1066
1067 //
1068 // Store Buffer Storage back to EFI variable
1069 //
1070 Status = gRT->SetVariable(
1071 VariableName,
1072 &mFormSetGuid,
1073 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1074 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1075 &PrivateData->Configuration
1076 );
1077
1078 return Status;
1079 }
1080
1081
1082 /**
1083 This function processes the results of changes in configuration.
1084
1085 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1086 @param Action Specifies the type of action taken by the browser.
1087 @param QuestionId A unique value which is sent to the original
1088 exporting driver so that it can identify the type
1089 of data to expect.
1090 @param Type The type of value for the question.
1091 @param Value A pointer to the data being sent to the original
1092 exporting driver.
1093 @param ActionRequest On return, points to the action requested by the
1094 callback function.
1095
1096 @retval EFI_SUCCESS The callback successfully handled the action.
1097 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
1098 variable and its data.
1099 @retval EFI_DEVICE_ERROR The variable could not be saved.
1100 @retval EFI_UNSUPPORTED The specified Action is not supported by the
1101 callback.
1102
1103 **/
1104 EFI_STATUS
1105 EFIAPI
1106 DriverCallback (
1107 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1108 IN EFI_BROWSER_ACTION Action,
1109 IN EFI_QUESTION_ID QuestionId,
1110 IN UINT8 Type,
1111 IN EFI_IFR_TYPE_VALUE *Value,
1112 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
1113 )
1114 {
1115 DRIVER_SAMPLE_PRIVATE_DATA *PrivateData;
1116 EFI_STATUS Status;
1117 UINT8 MyVar;
1118 VOID *StartOpCodeHandle;
1119 VOID *OptionsOpCodeHandle;
1120 EFI_IFR_GUID_LABEL *StartLabel;
1121 VOID *EndOpCodeHandle;
1122 EFI_IFR_GUID_LABEL *EndLabel;
1123 EFI_INPUT_KEY Key;
1124 DRIVER_SAMPLE_CONFIGURATION *Configuration;
1125 UINTN MyVarSize;
1126
1127 if (((Value == NULL) && (Action != EFI_BROWSER_ACTION_FORM_OPEN) && (Action != EFI_BROWSER_ACTION_FORM_CLOSE))||
1128 (ActionRequest == NULL)) {
1129 return EFI_INVALID_PARAMETER;
1130 }
1131
1132
1133 Status = EFI_SUCCESS;
1134 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
1135
1136 switch (Action) {
1137 case EFI_BROWSER_ACTION_FORM_OPEN:
1138 {
1139 if (QuestionId == 0x1234) {
1140 //
1141 // Sample CallBack for UEFI FORM_OPEN action:
1142 // Add Save action into Form 3 when Form 1 is opened.
1143 // This will be done only in FORM_OPEN CallBack of question with ID 0x1234 from Form 1.
1144 //
1145 PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);
1146
1147 //
1148 // Initialize the container for dynamic opcodes
1149 //
1150 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
1151 ASSERT (StartOpCodeHandle != NULL);
1152
1153 //
1154 // Create Hii Extend Label OpCode as the start opcode
1155 //
1156 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
1157 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
1158 StartLabel->Number = LABEL_UPDATE2;
1159
1160 HiiCreateActionOpCode (
1161 StartOpCodeHandle, // Container for dynamic created opcodes
1162 0x1238, // Question ID
1163 STRING_TOKEN(STR_SAVE_TEXT), // Prompt text
1164 STRING_TOKEN(STR_SAVE_TEXT), // Help text
1165 EFI_IFR_FLAG_CALLBACK, // Question flag
1166 0 // Action String ID
1167 );
1168
1169 HiiUpdateForm (
1170 PrivateData->HiiHandle[0], // HII handle
1171 &mFormSetGuid, // Formset GUID
1172 0x3, // Form ID
1173 StartOpCodeHandle, // Label for where to insert opcodes
1174 NULL // Insert data
1175 );
1176
1177 HiiFreeOpCodeHandle (StartOpCodeHandle);
1178 }
1179 }
1180 break;
1181
1182 case EFI_BROWSER_ACTION_FORM_CLOSE:
1183 {
1184 if (QuestionId == 0x5678) {
1185 //
1186 // Sample CallBack for UEFI FORM_CLOSE action:
1187 // Show up a pop-up to specify Form 3 will be closed when exit Form 3.
1188 //
1189 do {
1190 CreatePopUp (
1191 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
1192 &Key,
1193 L"",
1194 L"You are going to leave third Form!",
1195 L"Press ESC or ENTER to continue ...",
1196 L"",
1197 NULL
1198 );
1199 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));
1200 }
1201 }
1202 break;
1203
1204 case EFI_BROWSER_ACTION_RETRIEVE:
1205 {
1206 if (QuestionId == 0x1111) {
1207 //
1208 // EfiVarstore question takes sample action (print value as debug information)
1209 // after read/write question.
1210 //
1211 MyVarSize = 1;
1212 Status = gRT->GetVariable(
1213 L"MyVar",
1214 &mFormSetGuid,
1215 NULL,
1216 &MyVarSize,
1217 &MyVar
1218 );
1219 ASSERT_EFI_ERROR (Status);
1220 DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));
1221 }
1222 }
1223 break;
1224
1225 case EFI_BROWSER_ACTION_DEFAULT_STANDARD:
1226 {
1227 switch (QuestionId) {
1228 case 0x1240:
1229 Value->u8 = DEFAULT_CLASS_STANDARD_VALUE;
1230 break;
1231
1232 default:
1233 Status = EFI_UNSUPPORTED;
1234 break;
1235 }
1236 }
1237 break;
1238
1239 case EFI_BROWSER_ACTION_DEFAULT_MANUFACTURING:
1240 {
1241 switch (QuestionId) {
1242 case 0x1240:
1243 Value->u8 = DEFAULT_CLASS_MANUFACTURING_VALUE;
1244 break;
1245
1246 default:
1247 Status = EFI_UNSUPPORTED;
1248 break;
1249 }
1250 }
1251 break;
1252
1253 case EFI_BROWSER_ACTION_CHANGING:
1254 {
1255 switch (QuestionId) {
1256 case 0x1234:
1257 //
1258 // Initialize the container for dynamic opcodes
1259 //
1260 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
1261 ASSERT (StartOpCodeHandle != NULL);
1262
1263 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
1264 ASSERT (EndOpCodeHandle != NULL);
1265
1266 //
1267 // Create Hii Extend Label OpCode as the start opcode
1268 //
1269 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
1270 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
1271 StartLabel->Number = LABEL_UPDATE1;
1272
1273 //
1274 // Create Hii Extend Label OpCode as the end opcode
1275 //
1276 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
1277 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
1278 EndLabel->Number = LABEL_END;
1279
1280 HiiCreateActionOpCode (
1281 StartOpCodeHandle, // Container for dynamic created opcodes
1282 0x1237, // Question ID
1283 STRING_TOKEN(STR_EXIT_TEXT), // Prompt text
1284 STRING_TOKEN(STR_EXIT_TEXT), // Help text
1285 EFI_IFR_FLAG_CALLBACK, // Question flag
1286 0 // Action String ID
1287 );
1288
1289 //
1290 // Create Option OpCode
1291 //
1292 OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
1293 ASSERT (OptionsOpCodeHandle != NULL);
1294
1295 HiiCreateOneOfOptionOpCode (
1296 OptionsOpCodeHandle,
1297 STRING_TOKEN (STR_BOOT_OPTION1),
1298 0,
1299 EFI_IFR_NUMERIC_SIZE_1,
1300 1
1301 );
1302
1303 HiiCreateOneOfOptionOpCode (
1304 OptionsOpCodeHandle,
1305 STRING_TOKEN (STR_BOOT_OPTION2),
1306 0,
1307 EFI_IFR_NUMERIC_SIZE_1,
1308 2
1309 );
1310
1311 //
1312 // Prepare initial value for the dynamic created oneof Question
1313 //
1314 PrivateData->Configuration.DynamicOneof = 2;
1315 Status = gRT->SetVariable(
1316 VariableName,
1317 &mFormSetGuid,
1318 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1319 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1320 &PrivateData->Configuration
1321 );
1322
1323 //
1324 // Set initial vlaue of dynamic created oneof Question in Form Browser
1325 //
1326 Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION));
1327 ASSERT (Configuration != NULL);
1328 if (HiiGetBrowserData (&mFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) {
1329 Configuration->DynamicOneof = 2;
1330
1331 //
1332 // Update uncommitted data of Browser
1333 //
1334 HiiSetBrowserData (
1335 &mFormSetGuid,
1336 VariableName,
1337 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1338 (UINT8 *) Configuration,
1339 NULL
1340 );
1341 }
1342 FreePool (Configuration);
1343
1344 HiiCreateOneOfOpCode (
1345 StartOpCodeHandle, // Container for dynamic created opcodes
1346 0x8001, // Question ID (or call it "key")
1347 CONFIGURATION_VARSTORE_ID, // VarStore ID
1348 (UINT16) DYNAMIC_ONE_OF_VAR_OFFSET, // Offset in Buffer Storage
1349 STRING_TOKEN (STR_ONE_OF_PROMPT), // Question prompt text
1350 STRING_TOKEN (STR_ONE_OF_HELP), // Question help text
1351 EFI_IFR_FLAG_CALLBACK, // Question flag
1352 EFI_IFR_NUMERIC_SIZE_1, // Data type of Question Value
1353 OptionsOpCodeHandle, // Option Opcode list
1354 NULL // Default Opcode is NULl
1355 );
1356
1357 HiiCreateOrderedListOpCode (
1358 StartOpCodeHandle, // Container for dynamic created opcodes
1359 0x8002, // Question ID
1360 CONFIGURATION_VARSTORE_ID, // VarStore ID
1361 (UINT16) DYNAMIC_ORDERED_LIST_VAR_OFFSET, // Offset in Buffer Storage
1362 STRING_TOKEN (STR_BOOT_OPTIONS), // Question prompt text
1363 STRING_TOKEN (STR_BOOT_OPTIONS), // Question help text
1364 EFI_IFR_FLAG_RESET_REQUIRED, // Question flag
1365 0, // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET
1366 EFI_IFR_NUMERIC_SIZE_1, // Data type of Question value
1367 5, // Maximum container
1368 OptionsOpCodeHandle, // Option Opcode list
1369 NULL // Default Opcode is NULl
1370 );
1371
1372 HiiCreateTextOpCode (
1373 StartOpCodeHandle,
1374 STRING_TOKEN(STR_TEXT_SAMPLE_HELP),
1375 STRING_TOKEN(STR_TEXT_SAMPLE_HELP),
1376 STRING_TOKEN(STR_TEXT_SAMPLE_STRING)
1377 );
1378
1379 HiiCreateDateOpCode (
1380 StartOpCodeHandle,
1381 0x8004,
1382 0x0,
1383 0x0,
1384 STRING_TOKEN(STR_DATE_SAMPLE_HELP),
1385 STRING_TOKEN(STR_DATE_SAMPLE_HELP),
1386 0,
1387 QF_DATE_STORAGE_TIME,
1388 NULL
1389 );
1390
1391 HiiCreateTimeOpCode (
1392 StartOpCodeHandle,
1393 0x8005,
1394 0x0,
1395 0x0,
1396 STRING_TOKEN(STR_TIME_SAMPLE_HELP),
1397 STRING_TOKEN(STR_TIME_SAMPLE_HELP),
1398 0,
1399 QF_TIME_STORAGE_TIME,
1400 NULL
1401 );
1402
1403 HiiCreateGotoOpCode (
1404 StartOpCodeHandle, // Container for dynamic created opcodes
1405 1, // Target Form ID
1406 STRING_TOKEN (STR_GOTO_FORM1), // Prompt text
1407 STRING_TOKEN (STR_GOTO_HELP), // Help text
1408 0, // Question flag
1409 0x8003 // Question ID
1410 );
1411
1412 HiiUpdateForm (
1413 PrivateData->HiiHandle[0], // HII handle
1414 &mFormSetGuid, // Formset GUID
1415 0x1234, // Form ID
1416 StartOpCodeHandle, // Label for where to insert opcodes
1417 EndOpCodeHandle // Replace data
1418 );
1419
1420 HiiFreeOpCodeHandle (StartOpCodeHandle);
1421 HiiFreeOpCodeHandle (OptionsOpCodeHandle);
1422 HiiFreeOpCodeHandle (EndOpCodeHandle);
1423 break;
1424
1425 case 0x5678:
1426 //
1427 // We will reach here once the Question is refreshed
1428 //
1429
1430 //
1431 // Initialize the container for dynamic opcodes
1432 //
1433 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
1434 ASSERT (StartOpCodeHandle != NULL);
1435
1436 //
1437 // Create Hii Extend Label OpCode as the start opcode
1438 //
1439 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
1440 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
1441 StartLabel->Number = LABEL_UPDATE2;
1442
1443 HiiCreateActionOpCode (
1444 StartOpCodeHandle, // Container for dynamic created opcodes
1445 0x1237, // Question ID
1446 STRING_TOKEN(STR_EXIT_TEXT), // Prompt text
1447 STRING_TOKEN(STR_EXIT_TEXT), // Help text
1448 EFI_IFR_FLAG_CALLBACK, // Question flag
1449 0 // Action String ID
1450 );
1451
1452 HiiUpdateForm (
1453 PrivateData->HiiHandle[0], // HII handle
1454 &mFormSetGuid, // Formset GUID
1455 0x3, // Form ID
1456 StartOpCodeHandle, // Label for where to insert opcodes
1457 NULL // Insert data
1458 );
1459
1460 HiiFreeOpCodeHandle (StartOpCodeHandle);
1461
1462 //
1463 // Refresh the Question value
1464 //
1465 PrivateData->Configuration.DynamicRefresh++;
1466 Status = gRT->SetVariable(
1467 VariableName,
1468 &mFormSetGuid,
1469 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1470 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1471 &PrivateData->Configuration
1472 );
1473
1474 //
1475 // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause
1476 // the first statement in Form 3 be suppressed
1477 //
1478 MyVarSize = 1;
1479 MyVar = 111;
1480 Status = gRT->SetVariable(
1481 L"MyVar",
1482 &mFormSetGuid,
1483 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1484 MyVarSize,
1485 &MyVar
1486 );
1487 break;
1488
1489 case 0x1237:
1490 //
1491 // User press "Exit now", request Browser to exit
1492 //
1493 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1494 break;
1495
1496 case 0x1238:
1497 //
1498 // User press "Save now", request Browser to save the uncommitted data.
1499 //
1500 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1501 break;
1502
1503 case 0x2000:
1504 //
1505 // Only used to update the state.
1506 //
1507 if ((Type == EFI_IFR_TYPE_STRING) && (Value->string == 0) &&
1508 (PrivateData->PasswordState == BROWSER_STATE_SET_PASSWORD)) {
1509 PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;
1510 return EFI_INVALID_PARAMETER;
1511 }
1512
1513 //
1514 // When try to set a new password, user will be chanlleged with old password.
1515 // The Callback is responsible for validating old password input by user,
1516 // If Callback return EFI_SUCCESS, it indicates validation pass.
1517 //
1518 switch (PrivateData->PasswordState) {
1519 case BROWSER_STATE_VALIDATE_PASSWORD:
1520 Status = ValidatePassword (PrivateData, Value->string);
1521 if (Status == EFI_SUCCESS) {
1522 PrivateData->PasswordState = BROWSER_STATE_SET_PASSWORD;
1523 }
1524 break;
1525
1526 case BROWSER_STATE_SET_PASSWORD:
1527 Status = SetPassword (PrivateData, Value->string);
1528 PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;
1529 break;
1530
1531 default:
1532 break;
1533 }
1534
1535 break;
1536
1537 case 0x1111:
1538 //
1539 // EfiVarstore question takes sample action (print value as debug information)
1540 // after read/write question.
1541 //
1542 MyVarSize = 1;
1543 Status = gRT->GetVariable(
1544 L"MyVar",
1545 &mFormSetGuid,
1546 NULL,
1547 &MyVarSize,
1548 &MyVar
1549 );
1550 ASSERT_EFI_ERROR (Status);
1551 DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));
1552 default:
1553 break;
1554 }
1555 }
1556 break;
1557
1558 default:
1559 Status = EFI_UNSUPPORTED;
1560 break;
1561 }
1562
1563 return Status;
1564 }
1565
1566 /**
1567 Main entry for this driver.
1568
1569 @param ImageHandle Image handle this driver.
1570 @param SystemTable Pointer to SystemTable.
1571
1572 @retval EFI_SUCESS This function always complete successfully.
1573
1574 **/
1575 EFI_STATUS
1576 EFIAPI
1577 DriverSampleInit (
1578 IN EFI_HANDLE ImageHandle,
1579 IN EFI_SYSTEM_TABLE *SystemTable
1580 )
1581 {
1582 EFI_STATUS Status;
1583 EFI_HII_HANDLE HiiHandle[2];
1584 EFI_SCREEN_DESCRIPTOR Screen;
1585 EFI_HII_DATABASE_PROTOCOL *HiiDatabase;
1586 EFI_HII_STRING_PROTOCOL *HiiString;
1587 EFI_FORM_BROWSER2_PROTOCOL *FormBrowser2;
1588 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
1589 CHAR16 *NewString;
1590 UINTN BufferSize;
1591 DRIVER_SAMPLE_CONFIGURATION *Configuration;
1592 BOOLEAN ActionFlag;
1593 EFI_STRING ConfigRequestHdr;
1594
1595 //
1596 // Initialize the local variables.
1597 //
1598 ConfigRequestHdr = NULL;
1599 //
1600 // Initialize screen dimensions for SendForm().
1601 // Remove 3 characters from top and bottom
1602 //
1603 ZeroMem (&Screen, sizeof (EFI_SCREEN_DESCRIPTOR));
1604 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Screen.RightColumn, &Screen.BottomRow);
1605
1606 Screen.TopRow = 3;
1607 Screen.BottomRow = Screen.BottomRow - 3;
1608
1609 //
1610 // Initialize driver private data
1611 //
1612 PrivateData = AllocateZeroPool (sizeof (DRIVER_SAMPLE_PRIVATE_DATA));
1613 if (PrivateData == NULL) {
1614 return EFI_OUT_OF_RESOURCES;
1615 }
1616
1617 PrivateData->Signature = DRIVER_SAMPLE_PRIVATE_SIGNATURE;
1618
1619 PrivateData->ConfigAccess.ExtractConfig = ExtractConfig;
1620 PrivateData->ConfigAccess.RouteConfig = RouteConfig;
1621 PrivateData->ConfigAccess.Callback = DriverCallback;
1622 PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;
1623
1624 //
1625 // Locate Hii Database protocol
1626 //
1627 Status = gBS->LocateProtocol (&gEfiHiiDatabaseProtocolGuid, NULL, (VOID **) &HiiDatabase);
1628 if (EFI_ERROR (Status)) {
1629 return Status;
1630 }
1631 PrivateData->HiiDatabase = HiiDatabase;
1632
1633 //
1634 // Locate HiiString protocol
1635 //
1636 Status = gBS->LocateProtocol (&gEfiHiiStringProtocolGuid, NULL, (VOID **) &HiiString);
1637 if (EFI_ERROR (Status)) {
1638 return Status;
1639 }
1640 PrivateData->HiiString = HiiString;
1641
1642 //
1643 // Locate Formbrowser2 protocol
1644 //
1645 Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &FormBrowser2);
1646 if (EFI_ERROR (Status)) {
1647 return Status;
1648 }
1649 PrivateData->FormBrowser2 = FormBrowser2;
1650
1651 //
1652 // Locate ConfigRouting protocol
1653 //
1654 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &HiiConfigRouting);
1655 if (EFI_ERROR (Status)) {
1656 return Status;
1657 }
1658 PrivateData->HiiConfigRouting = HiiConfigRouting;
1659
1660 Status = gBS->InstallMultipleProtocolInterfaces (
1661 &DriverHandle[0],
1662 &gEfiDevicePathProtocolGuid,
1663 &mHiiVendorDevicePath0,
1664 &gEfiHiiConfigAccessProtocolGuid,
1665 &PrivateData->ConfigAccess,
1666 NULL
1667 );
1668 ASSERT_EFI_ERROR (Status);
1669
1670 PrivateData->DriverHandle[0] = DriverHandle[0];
1671
1672 //
1673 // Publish our HII data
1674 //
1675 HiiHandle[0] = HiiAddPackages (
1676 &mFormSetGuid,
1677 DriverHandle[0],
1678 DriverSampleStrings,
1679 VfrBin,
1680 NULL
1681 );
1682 if (HiiHandle[0] == NULL) {
1683 return EFI_OUT_OF_RESOURCES;
1684 }
1685
1686 PrivateData->HiiHandle[0] = HiiHandle[0];
1687
1688 //
1689 // Publish another Fromset
1690 //
1691 Status = gBS->InstallMultipleProtocolInterfaces (
1692 &DriverHandle[1],
1693 &gEfiDevicePathProtocolGuid,
1694 &mHiiVendorDevicePath1,
1695 NULL
1696 );
1697 ASSERT_EFI_ERROR (Status);
1698
1699 PrivateData->DriverHandle[1] = DriverHandle[1];
1700
1701 HiiHandle[1] = HiiAddPackages (
1702 &mInventoryGuid,
1703 DriverHandle[1],
1704 DriverSampleStrings,
1705 InventoryBin,
1706 NULL
1707 );
1708 if (HiiHandle[1] == NULL) {
1709 DriverSampleUnload (ImageHandle);
1710 return EFI_OUT_OF_RESOURCES;
1711 }
1712
1713 PrivateData->HiiHandle[1] = HiiHandle[1];
1714
1715 //
1716 // Very simple example of how one would update a string that is already
1717 // in the HII database
1718 //
1719 NewString = L"700 Mhz";
1720
1721 if (HiiSetString (HiiHandle[0], STRING_TOKEN (STR_CPU_STRING2), NewString, NULL) == 0) {
1722 DriverSampleUnload (ImageHandle);
1723 return EFI_OUT_OF_RESOURCES;
1724 }
1725
1726 HiiSetString (HiiHandle[0], 0, NewString, NULL);
1727
1728 //
1729 // Initialize Name/Value name String ID
1730 //
1731 PrivateData->NameStringId[0] = STR_NAME_VALUE_VAR_NAME0;
1732 PrivateData->NameStringId[1] = STR_NAME_VALUE_VAR_NAME1;
1733 PrivateData->NameStringId[2] = STR_NAME_VALUE_VAR_NAME2;
1734
1735 //
1736 // Initialize configuration data
1737 //
1738 Configuration = &PrivateData->Configuration;
1739 ZeroMem (Configuration, sizeof (DRIVER_SAMPLE_CONFIGURATION));
1740
1741 //
1742 // Try to read NV config EFI variable first
1743 //
1744 ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, DriverHandle[0]);
1745 ASSERT (ConfigRequestHdr != NULL);
1746
1747 BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
1748 Status = gRT->GetVariable (VariableName, &mFormSetGuid, NULL, &BufferSize, Configuration);
1749 if (EFI_ERROR (Status)) {
1750 //
1751 // Store zero data Buffer Storage to EFI variable
1752 //
1753 Status = gRT->SetVariable(
1754 VariableName,
1755 &mFormSetGuid,
1756 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1757 sizeof (DRIVER_SAMPLE_CONFIGURATION),
1758 Configuration
1759 );
1760 ASSERT (Status == EFI_SUCCESS);
1761 //
1762 // EFI variable for NV config doesn't exit, we should build this variable
1763 // based on default values stored in IFR
1764 //
1765 ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);
1766 ASSERT (ActionFlag);
1767 } else {
1768 //
1769 // EFI variable does exist and Validate Current Setting
1770 //
1771 ActionFlag = HiiValidateSettings (ConfigRequestHdr);
1772 ASSERT (ActionFlag);
1773 }
1774
1775 FreePool (ConfigRequestHdr);
1776
1777
1778 //
1779 // In default, this driver is built into Flash device image,
1780 // the following code doesn't run.
1781 //
1782
1783 //
1784 // Example of how to display only the item we sent to HII
1785 // When this driver is not built into Flash device image,
1786 // it need to call SendForm to show front page by itself.
1787 //
1788 if (DISPLAY_ONLY_MY_ITEM <= 1) {
1789 //
1790 // Have the browser pull out our copy of the data, and only display our data
1791 //
1792 Status = FormBrowser2->SendForm (
1793 FormBrowser2,
1794 &(HiiHandle[DISPLAY_ONLY_MY_ITEM]),
1795 1,
1796 NULL,
1797 0,
1798 NULL,
1799 NULL
1800 );
1801
1802 HiiRemovePackages (HiiHandle[0]);
1803
1804 HiiRemovePackages (HiiHandle[1]);
1805 }
1806
1807 return EFI_SUCCESS;
1808 }
1809
1810 /**
1811 Unloads the application and its installed protocol.
1812
1813 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1814
1815 @retval EFI_SUCCESS The image has been unloaded.
1816 **/
1817 EFI_STATUS
1818 EFIAPI
1819 DriverSampleUnload (
1820 IN EFI_HANDLE ImageHandle
1821 )
1822 {
1823 UINTN Index;
1824
1825 ASSERT (PrivateData != NULL);
1826
1827 if (DriverHandle[0] != NULL) {
1828 gBS->UninstallMultipleProtocolInterfaces (
1829 DriverHandle[0],
1830 &gEfiDevicePathProtocolGuid,
1831 &mHiiVendorDevicePath0,
1832 &gEfiHiiConfigAccessProtocolGuid,
1833 &PrivateData->ConfigAccess,
1834 NULL
1835 );
1836 DriverHandle[0] = NULL;
1837 }
1838
1839 if (DriverHandle[1] != NULL) {
1840 gBS->UninstallMultipleProtocolInterfaces (
1841 DriverHandle[1],
1842 &gEfiDevicePathProtocolGuid,
1843 &mHiiVendorDevicePath1,
1844 NULL
1845 );
1846 DriverHandle[1] = NULL;
1847 }
1848
1849 if (PrivateData->HiiHandle[0] != NULL) {
1850 HiiRemovePackages (PrivateData->HiiHandle[0]);
1851 }
1852
1853 if (PrivateData->HiiHandle[1] != NULL) {
1854 HiiRemovePackages (PrivateData->HiiHandle[1]);
1855 }
1856
1857 for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) {
1858 if (PrivateData->NameValueName[Index] != NULL) {
1859 FreePool (PrivateData->NameValueName[Index]);
1860 }
1861 }
1862 FreePool (PrivateData);
1863 PrivateData = NULL;
1864
1865 return EFI_SUCCESS;
1866 }