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