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