X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FDriverSampleDxe%2FDriverSample.c;h=14ac72fe1dcc7fd8f6892f3f8915c85bfc4d2bbf;hb=61f0f4375b35dd9b3ab6f1e1803385b57a22c7fb;hp=f74ae3e9594b20e69199e0ceaff8411cab6798d5;hpb=ccee6099223bb2d05e7fcacaab285a9cb7331ad7;p=mirror_edk2.git diff --git a/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c b/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c index f74ae3e959..14ac72fe1d 100644 --- a/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c +++ b/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c @@ -2,8 +2,8 @@ This is an example of how a driver might export data to the HII protocol to be later utilized by the Setup Protocol -Copyright (c) 2004 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -43,7 +43,7 @@ HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath0 = { { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, - { + { (UINT8) (END_DEVICE_PATH_LENGTH), (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8) } @@ -68,7 +68,7 @@ HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath1 = { { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, - { + { (UINT8) (END_DEVICE_PATH_LENGTH), (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8) } @@ -77,10 +77,10 @@ HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath1 = { /** Encode the password using a simple algorithm. - + @param Password The string to be encoded. @param MaxSize The size of the string. - + **/ VOID EncodePassword ( @@ -111,10 +111,10 @@ EncodePassword ( /** Validate the user's password. - + @param PrivateData This driver's private context data. @param StringId The user's input. - + @retval EFI_SUCCESS The user's input matches the password. @retval EFI_NOT_READY The user's input does not match the password. **/ @@ -204,13 +204,13 @@ ValidatePassword ( /** Encode the password using a simple algorithm. - + @param PrivateData This driver's private context data. @param StringId The password from User. - + @retval EFI_SUCESS The operation is successful. @return Other value if gRT->SetVariable () fails. - + **/ EFI_STATUS SetPassword ( @@ -244,9 +244,9 @@ SetPassword ( // Get user input password // Password = &PrivateData->Configuration.WhatIsThePassword2[0]; - PasswordSize = sizeof (PrivateData->Configuration.WhatIsThePassword2); + PasswordSize = sizeof (PrivateData->Configuration.WhatIsThePassword2); ZeroMem (Password, PasswordSize); - + TempPassword = HiiGetString (PrivateData->HiiHandle[0], StringId, NULL); if (TempPassword == NULL) { return EFI_NOT_READY; @@ -301,6 +301,38 @@ SetPassword ( return Status; } +/** + Update names of Name/Value storage to current language. + + @param PrivateData Points to the driver private data. + + @retval EFI_SUCCESS All names are successfully updated. + @retval EFI_NOT_FOUND Failed to get Name from HII database. + +**/ +EFI_STATUS +LoadNameValueNames ( + IN DRIVER_SAMPLE_PRIVATE_DATA *PrivateData + ) +{ + UINTN Index; + + // + // Get Name/Value name string of current language + // + for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) { + PrivateData->NameValueName[Index] = HiiGetString ( + PrivateData->HiiHandle[0], + PrivateData->NameStringId[Index], + NULL + ); + if (PrivateData->NameValueName[Index] == NULL) { + return EFI_NOT_FOUND; + } + } + + return EFI_SUCCESS; +} /** This function allows a caller to extract the current configuration for one @@ -344,8 +376,13 @@ ExtractConfig ( EFI_STRING ConfigRequest; EFI_STRING ConfigRequestHdr; UINTN Size; - - if (Progress == NULL || Results == NULL || Request == NULL) { + EFI_STRING Value; + UINTN ValueStrLen; + CHAR16 BackupChar; + CHAR16 *StrPointer; + BOOLEAN AllocatedRequest; + + if (Progress == NULL || Results == NULL) { return EFI_INVALID_PARAMETER; } // @@ -355,6 +392,7 @@ ExtractConfig ( ConfigRequest = NULL; Size = 0; *Progress = Request; + AllocatedRequest = FALSE; PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This); HiiConfigRouting = PrivateData->HiiConfigRouting; @@ -374,19 +412,21 @@ ExtractConfig ( if (EFI_ERROR (Status)) { return EFI_NOT_FOUND; } - + if (Request == NULL) { // // Request is set to NULL, construct full request string. // // - // Allocate and fill a buffer large enough to hold the template + // Allocate and fill a buffer large enough to hold the template // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator // ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, PrivateData->DriverHandle[0]); - Size = (StrLen (ConfigRequest) + 32 + 1) * sizeof (CHAR16); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); ConfigRequest = AllocateZeroPool (Size); + ASSERT (ConfigRequest != NULL); + AllocatedRequest = TRUE; UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize); FreePool (ConfigRequestHdr); } else { @@ -394,27 +434,144 @@ ExtractConfig ( // Check routing data in . // Note: if only one Storage is used, then this checking could be skipped. // - if (!HiiIsConfigHdrMatch (Request, &mFormSetGuid, VariableName)) { + if (!HiiIsConfigHdrMatch (Request, &mFormSetGuid, NULL)) { return EFI_NOT_FOUND; } + // + // Set Request to the unified request string. + // ConfigRequest = Request; + // + // Check whether Request includes Request Element. + // + if (StrStr (Request, L"OFFSET") == NULL) { + // + // Check Request Element does exist in Reques String + // + StrPointer = StrStr (Request, L"PATH"); + if (StrPointer == NULL) { + return EFI_INVALID_PARAMETER; + } + if (StrStr (StrPointer, L"&") == NULL) { + Size = (StrLen (Request) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + ASSERT (ConfigRequest != NULL); + AllocatedRequest = TRUE; + UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", Request, (UINT64)BufferSize); + } + } } // - // Convert buffer data to by helper function BlockToConfig() + // Check if requesting Name/Value storage // - Status = HiiConfigRouting->BlockToConfig ( - HiiConfigRouting, - ConfigRequest, - (UINT8 *) &PrivateData->Configuration, - BufferSize, - Results, - Progress - ); - - if (Request == NULL) { + if (StrStr (ConfigRequest, L"OFFSET") == NULL) { + // + // Update Name/Value storage Names + // + Status = LoadNameValueNames (PrivateData); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Allocate memory for , e.g. Name0=0x11, Name1=0x1234, Name2="ABCD" + // ::=&Name0&Name1&Name2 + // ::=&Name0=11&Name1=1234&Name2=0041004200430044 + // + BufferSize = (StrLen (ConfigRequest) + + 1 + sizeof (PrivateData->Configuration.NameValueVar0) * 2 + + 1 + sizeof (PrivateData->Configuration.NameValueVar1) * 2 + + 1 + sizeof (PrivateData->Configuration.NameValueVar2) * 2 + 1) * sizeof (CHAR16); + *Results = AllocateZeroPool (BufferSize); + ASSERT (*Results != NULL); + StrCpy (*Results, ConfigRequest); + Value = *Results; + + // + // Append value of NameValueVar0, type is UINT8 + // + if ((Value = StrStr (*Results, PrivateData->NameValueName[0])) != NULL) { + Value += StrLen (PrivateData->NameValueName[0]); + ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar0) * 2) + 1); + CopyMem (Value + ValueStrLen, Value, StrSize (Value)); + + BackupChar = Value[ValueStrLen]; + *Value++ = L'='; + Value += UnicodeValueToString ( + Value, + PREFIX_ZERO | RADIX_HEX, + PrivateData->Configuration.NameValueVar0, + sizeof (PrivateData->Configuration.NameValueVar0) * 2 + ); + *Value = BackupChar; + } + + // + // Append value of NameValueVar1, type is UINT16 + // + if ((Value = StrStr (*Results, PrivateData->NameValueName[1])) != NULL) { + Value += StrLen (PrivateData->NameValueName[1]); + ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar1) * 2) + 1); + CopyMem (Value + ValueStrLen, Value, StrSize (Value)); + + BackupChar = Value[ValueStrLen]; + *Value++ = L'='; + Value += UnicodeValueToString ( + Value, + PREFIX_ZERO | RADIX_HEX, + PrivateData->Configuration.NameValueVar1, + sizeof (PrivateData->Configuration.NameValueVar1) * 2 + ); + *Value = BackupChar; + } + + // + // Append value of NameValueVar2, type is CHAR16 * + // + if ((Value = StrStr (*Results, PrivateData->NameValueName[2])) != NULL) { + Value += StrLen (PrivateData->NameValueName[2]); + ValueStrLen = StrLen (PrivateData->Configuration.NameValueVar2) * 4 + 1; + CopyMem (Value + ValueStrLen, Value, StrSize (Value)); + + *Value++ = L'='; + // + // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044" + // + StrPointer = (CHAR16 *) PrivateData->Configuration.NameValueVar2; + for (; *StrPointer != L'\0'; StrPointer++) { + Value += UnicodeValueToString (Value, PREFIX_ZERO | RADIX_HEX, *StrPointer, 4); + } + } + + Status = EFI_SUCCESS; + } else { + // + // Convert buffer data to by helper function BlockToConfig() + // + Status = HiiConfigRouting->BlockToConfig ( + HiiConfigRouting, + ConfigRequest, + (UINT8 *) &PrivateData->Configuration, + BufferSize, + Results, + Progress + ); + } + + // + // Free the allocated config request string. + // + if (AllocatedRequest) { FreePool (ConfigRequest); + } + // + // Set Progress string to the original request string. + // + if (Request == NULL) { *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); } return Status; @@ -451,6 +608,13 @@ RouteConfig ( UINTN BufferSize; DRIVER_SAMPLE_PRIVATE_DATA *PrivateData; EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting; + CHAR16 *Value; + CHAR16 *StrPtr; + CHAR16 TemStr[5]; + UINT8 *DataBuffer; + UINT8 DigitUint8; + UINTN Index; + CHAR16 *StrBuffer; if (Configuration == NULL || Progress == NULL) { return EFI_INVALID_PARAMETER; @@ -464,7 +628,7 @@ RouteConfig ( // Check routing data in . // Note: if only one Storage is used, then this checking could be skipped. // - if (!HiiIsConfigHdrMatch (Configuration, &mFormSetGuid, VariableName)) { + if (!HiiIsConfigHdrMatch (Configuration, &mFormSetGuid, NULL)) { return EFI_NOT_FOUND; } @@ -483,6 +647,125 @@ RouteConfig ( return Status; } + // + // Check if configuring Name/Value storage + // + if (StrStr (Configuration, L"OFFSET") == NULL) { + // + // Update Name/Value storage Names + // + Status = LoadNameValueNames (PrivateData); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Convert value for NameValueVar0 + // + if ((Value = StrStr (Configuration, PrivateData->NameValueName[0])) != NULL) { + // + // Skip "Name=" + // + Value += StrLen (PrivateData->NameValueName[0]); + Value++; + // + // Get Value String + // + StrPtr = StrStr (Value, L"&"); + if (StrPtr == NULL) { + StrPtr = Value + StrLen (Value); + } + // + // Convert Value to Buffer data + // + DataBuffer = (UINT8 *) &PrivateData->Configuration.NameValueVar0; + ZeroMem (TemStr, sizeof (TemStr)); + for (Index = 0, StrPtr --; StrPtr >= Value; StrPtr --, Index ++) { + TemStr[0] = *StrPtr; + DigitUint8 = (UINT8) StrHexToUint64 (TemStr); + if ((Index & 1) == 0) { + DataBuffer [Index/2] = DigitUint8; + } else { + DataBuffer [Index/2] = (UINT8) ((UINT8) (DigitUint8 << 4) + DataBuffer [Index/2]); + } + } + } + + // + // Convert value for NameValueVar1 + // + if ((Value = StrStr (Configuration, PrivateData->NameValueName[1])) != NULL) { + // + // Skip "Name=" + // + Value += StrLen (PrivateData->NameValueName[1]); + Value++; + // + // Get Value String + // + StrPtr = StrStr (Value, L"&"); + if (StrPtr == NULL) { + StrPtr = Value + StrLen (Value); + } + // + // Convert Value to Buffer data + // + DataBuffer = (UINT8 *) &PrivateData->Configuration.NameValueVar1; + ZeroMem (TemStr, sizeof (TemStr)); + for (Index = 0, StrPtr --; StrPtr >= Value; StrPtr --, Index ++) { + TemStr[0] = *StrPtr; + DigitUint8 = (UINT8) StrHexToUint64 (TemStr); + if ((Index & 1) == 0) { + DataBuffer [Index/2] = DigitUint8; + } else { + DataBuffer [Index/2] = (UINT8) ((UINT8) (DigitUint8 << 4) + DataBuffer [Index/2]); + } + } + } + + // + // Convert value for NameValueVar2 + // + if ((Value = StrStr (Configuration, PrivateData->NameValueName[2])) != NULL) { + // + // Skip "Name=" + // + Value += StrLen (PrivateData->NameValueName[2]); + Value++; + // + // Get Value String + // + StrPtr = StrStr (Value, L"&"); + if (StrPtr == NULL) { + StrPtr = Value + StrLen (Value); + } + // + // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD" + // + StrBuffer = (CHAR16 *) PrivateData->Configuration.NameValueVar2; + ZeroMem (TemStr, sizeof (TemStr)); + while (Value < StrPtr) { + StrnCpy (TemStr, Value, 4); + *(StrBuffer++) = (CHAR16) StrHexToUint64 (TemStr); + Value += 4; + } + *StrBuffer = L'\0'; + } + + // + // Store Buffer Storage back to EFI variable + // + Status = gRT->SetVariable( + VariableName, + &mFormSetGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, + sizeof (DRIVER_SAMPLE_CONFIGURATION), + &PrivateData->Configuration + ); + + return Status; + } + // // Convert to buffer data by helper function ConfigToBlock() // @@ -554,15 +837,76 @@ DriverCallback ( EFI_IFR_GUID_LABEL *StartLabel; VOID *EndOpCodeHandle; EFI_IFR_GUID_LABEL *EndLabel; + EFI_INPUT_KEY Key; + DRIVER_SAMPLE_CONFIGURATION *Configuration; + UINTN MyVarSize; + + if (Action == EFI_BROWSER_ACTION_FORM_OPEN) { + // + // On FORM_OPEN event, update the form on-the-fly + // + PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This); + + // + // Initialize the container for dynamic opcodes + // + StartOpCodeHandle = HiiAllocateOpCodeHandle (); + ASSERT (StartOpCodeHandle != NULL); + + // + // Create Hii Extend Label OpCode as the start opcode + // + StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL)); + StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + StartLabel->Number = LABEL_UPDATE2; + + HiiCreateActionOpCode ( + StartOpCodeHandle, // Container for dynamic created opcodes + 0x1238, // Question ID + STRING_TOKEN(STR_SAVE_TEXT), // Prompt text + STRING_TOKEN(STR_SAVE_TEXT), // Help text + EFI_IFR_FLAG_CALLBACK, // Question flag + 0 // Action String ID + ); + + HiiUpdateForm ( + PrivateData->HiiHandle[0], // HII handle + &mFormSetGuid, // Formset GUID + 0x3, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + NULL // Insert data + ); + + HiiFreeOpCodeHandle (StartOpCodeHandle); + return EFI_SUCCESS; + } + + if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) { + // + // On FORM_CLOSE event, show up a pop-up + // + do { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"", + L"You are going to leave the Form!", + L"Press ESC or ENTER to continue ...", + L"", + NULL + ); + } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN)); + + return EFI_SUCCESS; + } if ((Value == NULL) || (ActionRequest == NULL)) { return EFI_INVALID_PARAMETER; } - + if ((Type == EFI_IFR_TYPE_STRING) && (Value->string == 0)) { return EFI_INVALID_PARAMETER; } - Status = EFI_SUCCESS; PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This); @@ -635,6 +979,27 @@ DriverCallback ( &PrivateData->Configuration ); + // + // Set initial vlaue of dynamic created oneof Question in Form Browser + // + Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION)); + ASSERT (Configuration != NULL); + if (HiiGetBrowserData (&mFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) { + Configuration->DynamicOneof = 2; + + // + // Update uncommitted data of Browser + // + HiiSetBrowserData ( + &mFormSetGuid, + VariableName, + sizeof (DRIVER_SAMPLE_CONFIGURATION), + (UINT8 *) Configuration, + NULL + ); + } + FreePool (Configuration); + HiiCreateOneOfOpCode ( StartOpCodeHandle, // Container for dynamic created opcodes 0x8001, // Question ID (or call it "key") @@ -682,8 +1047,9 @@ DriverCallback ( HiiFreeOpCodeHandle (StartOpCodeHandle); HiiFreeOpCodeHandle (OptionsOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); break; - + case 0x5678: // // We will reach here once the Question is refreshed @@ -710,7 +1076,7 @@ DriverCallback ( EFI_IFR_FLAG_CALLBACK, // Question flag 0 // Action String ID ); - + HiiUpdateForm ( PrivateData->HiiHandle[0], // HII handle &mFormSetGuid, // Formset GUID @@ -719,8 +1085,8 @@ DriverCallback ( NULL // Insert data ); - HiiFreeOpCodeHandle (StartOpCodeHandle); - + HiiFreeOpCodeHandle (StartOpCodeHandle); + // // Refresh the Question value // @@ -732,17 +1098,18 @@ DriverCallback ( sizeof (DRIVER_SAMPLE_CONFIGURATION), &PrivateData->Configuration ); - + // // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause // the first statement in Form 3 be suppressed // + MyVarSize = 1; MyVar = 111; Status = gRT->SetVariable( L"MyVar", &mFormSetGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, - 1, + MyVarSize, &MyVar ); break; @@ -786,6 +1153,21 @@ DriverCallback ( break; + case 0x1111: + // + // EfiVarstore question takes sample action (print value as debug information) + // after read/write question. + // + MyVarSize = 1; + Status = gRT->GetVariable( + L"MyVar", + &mFormSetGuid, + NULL, + &MyVarSize, + &MyVar + ); + ASSERT_EFI_ERROR (Status); + DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize)); default: break; } @@ -795,7 +1177,7 @@ DriverCallback ( /** Main entry for this driver. - + @param ImageHandle Image handle this driver. @param SystemTable Pointer to SystemTable. @@ -820,8 +1202,8 @@ DriverSampleInit ( UINTN BufferSize; DRIVER_SAMPLE_CONFIGURATION *Configuration; BOOLEAN ActionFlag; - EFI_STRING ConfigRequestHdr; - + EFI_STRING ConfigRequestHdr; + // // Initialize the local variables. // @@ -936,6 +1318,7 @@ DriverSampleInit ( NULL ); if (HiiHandle[1] == NULL) { + DriverSampleUnload (ImageHandle); return EFI_OUT_OF_RESOURCES; } @@ -948,9 +1331,19 @@ DriverSampleInit ( NewString = L"700 Mhz"; if (HiiSetString (HiiHandle[0], STRING_TOKEN (STR_CPU_STRING2), NewString, NULL) == 0) { + DriverSampleUnload (ImageHandle); return EFI_OUT_OF_RESOURCES; } + HiiSetString (HiiHandle[0], 0, NewString, NULL); + + // + // Initialize Name/Value name String ID + // + PrivateData->NameStringId[0] = STR_NAME_VALUE_VAR_NAME0; + PrivateData->NameStringId[1] = STR_NAME_VALUE_VAR_NAME1; + PrivateData->NameStringId[2] = STR_NAME_VALUE_VAR_NAME2; + // // Initialize configuration data // @@ -990,18 +1383,18 @@ DriverSampleInit ( ActionFlag = HiiValidateSettings (ConfigRequestHdr); ASSERT (ActionFlag); } - + FreePool (ConfigRequestHdr); // - // In default, this driver is built into Flash device image, + // In default, this driver is built into Flash device image, // the following code doesn't run. // // // Example of how to display only the item we sent to HII - // When this driver is not built into Flash device image, + // When this driver is not built into Flash device image, // it need to call SendForm to show front page by itself. // if (DISPLAY_ONLY_MY_ITEM <= 1) { @@ -1017,9 +1410,9 @@ DriverSampleInit ( NULL, NULL ); - + HiiRemovePackages (HiiHandle[0]); - + HiiRemovePackages (HiiHandle[1]); } @@ -1039,6 +1432,7 @@ DriverSampleUnload ( IN EFI_HANDLE ImageHandle ) { + UINTN Index; if (DriverHandle[0] != NULL) { gBS->UninstallMultipleProtocolInterfaces ( DriverHandle[0], @@ -1070,6 +1464,11 @@ DriverSampleUnload ( } if (PrivateData != NULL) { + for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) { + if (PrivateData->NameValueName[Index] != NULL) { + FreePool (PrivateData->NameValueName[Index]); + } + } FreePool (PrivateData); PrivateData = NULL; }