/** Check whether the DevicePath is in the device path forbid list (mAccessInfo.LoadForbid). @param[in] DevicePath Points to device path. @retval TRUE The DevicePath is in the device path forbid list. @retval FALSE The DevicePath is not in the device path forbid list. **/ BOOLEAN IsLoadForbidden ( IN EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { UINTN OffSet; UINTN DPSize; UINTN Size; EFI_DEVICE_PATH_PROTOCOL *Dp; OffSet = 0; Size = GetDevicePathSize (DevicePath); // // Check each device path. // while (OffSet < mAccessInfo.LoadForbidLen) { Dp = (EFI_DEVICE_PATH_PROTOCOL *) (mAccessInfo.LoadForbid + OffSet); DPSize = GetDevicePathSize (Dp); // // Compare device path. // if ((DPSize == Size) && (CompareMem (DevicePath, Dp, Size) == 0)) { return TRUE; } OffSet += DPSize; } return FALSE; }
/** Update the device path of "ConOut", "ConIn" and "ErrOut" based on the new BaudRate, Data Bits, parity and Stop Bits set. **/ VOID Var_UpdateAllConsoleOption ( VOID ) { EFI_DEVICE_PATH_PROTOCOL *OutDevicePath; EFI_DEVICE_PATH_PROTOCOL *InpDevicePath; EFI_DEVICE_PATH_PROTOCOL *ErrDevicePath; EFI_STATUS Status; OutDevicePath = EfiLibGetVariable (L"ConOut", &gEfiGlobalVariableGuid); InpDevicePath = EfiLibGetVariable (L"ConIn", &gEfiGlobalVariableGuid); ErrDevicePath = EfiLibGetVariable (L"ErrOut", &gEfiGlobalVariableGuid); if (OutDevicePath != NULL) { ChangeVariableDevicePath (OutDevicePath); Status = gRT->SetVariable ( L"ConOut", &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (OutDevicePath), OutDevicePath ); // // Changing variable without increasing its size with current variable implementation shouldn't fail. // ASSERT_EFI_ERROR (Status); } if (InpDevicePath != NULL) { ChangeVariableDevicePath (InpDevicePath); Status = gRT->SetVariable ( L"ConIn", &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (InpDevicePath), InpDevicePath ); // // Changing variable without increasing its size with current variable implementation shouldn't fail. // ASSERT_EFI_ERROR (Status); } if (ErrDevicePath != NULL) { ChangeVariableDevicePath (ErrDevicePath); Status = gRT->SetVariable ( L"ErrOut", &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (ErrDevicePath), ErrDevicePath ); // // Changing variable without increasing its size with current variable implementation shouldn't fail. // ASSERT_EFI_ERROR (Status); } }
/** Update the device path of "ConOut", "ConIn" and "ErrOut" based on the new BaudRate, Data Bits, parity and Stop Bits set. **/ VOID Var_UpdateAllConsoleOption ( VOID ) { EFI_DEVICE_PATH_PROTOCOL *OutDevicePath; EFI_DEVICE_PATH_PROTOCOL *InpDevicePath; EFI_DEVICE_PATH_PROTOCOL *ErrDevicePath; EFI_STATUS Status; GetEfiGlobalVariable2 (L"ConOut", (VOID**)&OutDevicePath, NULL); GetEfiGlobalVariable2 (L"ConIn", (VOID**)&InpDevicePath, NULL); GetEfiGlobalVariable2 (L"ErrOut", (VOID**)&ErrDevicePath, NULL); if (OutDevicePath != NULL) { ChangeVariableDevicePath (OutDevicePath); Status = gRT->SetVariable ( L"ConOut", &gEfiGlobalVariableGuid, VAR_FLAG, GetDevicePathSize (OutDevicePath), OutDevicePath ); ASSERT (!EFI_ERROR (Status)); } if (InpDevicePath != NULL) { ChangeVariableDevicePath (InpDevicePath); Status = gRT->SetVariable ( L"ConIn", &gEfiGlobalVariableGuid, VAR_FLAG, GetDevicePathSize (InpDevicePath), InpDevicePath ); ASSERT (!EFI_ERROR (Status)); } if (ErrDevicePath != NULL) { ChangeVariableDevicePath (ErrDevicePath); Status = gRT->SetVariable ( L"ErrOut", &gEfiGlobalVariableGuid, VAR_FLAG, GetDevicePathSize (ErrDevicePath), ErrDevicePath ); ASSERT (!EFI_ERROR (Status)); } }
/** Update the device path of "ConOut", "ConIn" and "ErrOut" based on the new BaudRate, Data Bits, parity and Stop Bits set. **/ VOID Var_UpdateAllConsoleOption ( VOID ) { EFI_DEVICE_PATH_PROTOCOL *OutDevicePath; EFI_DEVICE_PATH_PROTOCOL *InpDevicePath; EFI_DEVICE_PATH_PROTOCOL *ErrDevicePath; EFI_STATUS Status; OutDevicePath = EfiLibGetVariable (L"ConOut", &gEfiGlobalVariableGuid); InpDevicePath = EfiLibGetVariable (L"ConIn", &gEfiGlobalVariableGuid); ErrDevicePath = EfiLibGetVariable (L"ErrOut", &gEfiGlobalVariableGuid); if (OutDevicePath != NULL) { ChangeVariableDevicePath (OutDevicePath); Status = gRT->SetVariable ( L"ConOut", &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (OutDevicePath), OutDevicePath ); ASSERT (!EFI_ERROR (Status)); } if (InpDevicePath != NULL) { ChangeVariableDevicePath (InpDevicePath); Status = gRT->SetVariable ( L"ConIn", &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (InpDevicePath), InpDevicePath ); ASSERT (!EFI_ERROR (Status)); } if (ErrDevicePath != NULL) { ChangeVariableDevicePath (ErrDevicePath); Status = gRT->SetVariable ( L"ErrOut", &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (ErrDevicePath), ErrDevicePath ); ASSERT (!EFI_ERROR (Status)); } }
/** Check whether the image pointed to by DevicePath is in the device path list specified by AccessType. @param[in] DevicePath Points to device path. @param[in] AccessType The type of user access control. @retval TURE The DevicePath is in the specified List. @retval FALSE The DevicePath is not in the specified List. **/ BOOLEAN IsDevicePathInList ( IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN UINT32 AccessType ) { EFI_STATUS Status; EFI_USER_INFO_ACCESS_CONTROL *Access; EFI_DEVICE_PATH_PROTOCOL *Path; UINTN OffSet; Status = GetAccessControl (&Access, AccessType); if (EFI_ERROR (Status)) { return FALSE; } OffSet = 0; while (OffSet < Access->Size - sizeof (EFI_USER_INFO_ACCESS_CONTROL)) { Path = (EFI_DEVICE_PATH_PROTOCOL*)((UINT8*)(Access + 1) + OffSet); if (CheckDevicePath (Path, DevicePath)) { // // The device path is found in list. // FreePool (Access); return TRUE; } OffSet += GetDevicePathSize (Path); } FreePool (Access); return FALSE; }
UINTN GetUnalignedDevicePathSize ( IN EFI_DEVICE_PATH* DevicePath ) { UINTN Size; EFI_DEVICE_PATH* AlignedDevicePath; if ((UINTN)DevicePath & 0x1) { AlignedDevicePath = DuplicateDevicePath (DevicePath); Size = GetDevicePathSize (AlignedDevicePath); FreePool (AlignedDevicePath); } else { Size = GetDevicePathSize (DevicePath); } return Size; }
/** Returns the size of a device path in bytes. This function returns the size, in bytes, of the device path data structure specified by DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned. @param DevicePath A pointer to a device path data structure. @return The size of a device path in bytes. **/ UINTN EFIAPI GetDevicePathSizeProtocolInterface ( IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { return GetDevicePathSize (DevicePath); }
EFI_STATUS UpdateFdtPath ( IN LIST_ENTRY *BootOptionsList ) { EFI_STATUS Status; UINTN FdtDevicePathSize; BDS_SUPPORTED_DEVICE *SupportedBootDevice; EFI_DEVICE_PATH_PROTOCOL *FdtDevicePathNodes; EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath; Status = SelectBootDevice (&SupportedBootDevice); if (EFI_ERROR(Status)) { Status = EFI_ABORTED; goto EXIT; } // Create the specific device path node Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNodes); if (EFI_ERROR(Status)) { Status = EFI_ABORTED; goto EXIT; } if (FdtDevicePathNodes != NULL) { // Append the Device Path node to the select device path FdtDevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNodes); FdtDevicePathSize = GetDevicePathSize (FdtDevicePath); Status = gRT->SetVariable ( (CHAR16*)L"Fdt", &gArmGlobalVariableGuid, EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, FdtDevicePathSize, FdtDevicePath ); ASSERT_EFI_ERROR(Status); } else { gRT->SetVariable ( (CHAR16*)L"Fdt", &gArmGlobalVariableGuid, EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, 0, NULL ); ASSERT_EFI_ERROR(Status); } EXIT: if (Status == EFI_ABORTED) { Print(L"\n"); } FreePool(SupportedBootDevice); return Status; }
/** Compare two device pathes to check if they are exactly same. @param DevicePath1 A pointer to the first device path data structure. @param DevicePath2 A pointer to the second device path data structure. @retval TRUE They are same. @retval FALSE They are not same. **/ BOOLEAN EfiCompareDevicePath ( IN EFI_DEVICE_PATH_PROTOCOL *DevicePath1, IN EFI_DEVICE_PATH_PROTOCOL *DevicePath2 ) { UINTN Size1; UINTN Size2; Size1 = GetDevicePathSize (DevicePath1); Size2 = GetDevicePathSize (DevicePath2); if (Size1 != Size2) { return FALSE; } if (CompareMem (DevicePath1, DevicePath2, Size1) != 0) { return FALSE; } return TRUE; }
/** Delete the specified device path by DriverIndex from the forbid device path list (mAccessInfo.LoadForbid). @param[in] DriverIndex The index of driver in forbidden device path list. **/ VOID DeleteFromForbidLoad ( IN UINT16 DriverIndex ) { UINTN OffSet; UINTN DPSize; UINTN OffLen; EFI_DEVICE_PATH_PROTOCOL *Dp; OffSet = 0; // // Find the specified device path. // while ((OffSet < mAccessInfo.LoadForbidLen) && (DriverIndex > 0)) { Dp = (EFI_DEVICE_PATH_PROTOCOL *) (mAccessInfo.LoadForbid + OffSet); DPSize = GetDevicePathSize (Dp); OffSet += DPSize; DriverIndex--; } // // Specified device path found. // if (DriverIndex == 0) { Dp = (EFI_DEVICE_PATH_PROTOCOL *) (mAccessInfo.LoadForbid + OffSet); DPSize = GetDevicePathSize (Dp); OffLen = mAccessInfo.LoadForbidLen - OffSet - DPSize; if (OffLen > 0) { CopyMem ( mAccessInfo.LoadForbid + OffSet, mAccessInfo.LoadForbid + OffSet + DPSize, OffLen ); } mAccessInfo.LoadForbidLen -= DPSize; } }
/** Connect all the system drivers to controllers and create the network device list in NV storage. @retval EFI_SUCCESS Network devices are connected. @retval EFI_DEVICE_ERROR No network device is connected. **/ EFI_STATUS ConnectAllAndCreateNetworkDeviceList ( VOID ) { EFI_STATUS Status; EFI_HANDLE *Handles; UINTN HandleCount; EFI_DEVICE_PATH_PROTOCOL *SingleDevice; EFI_DEVICE_PATH_PROTOCOL *Devices; EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; EfiBootManagerConnectAll (); Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiManagedNetworkServiceBindingProtocolGuid, NULL, &HandleCount, &Handles); if (EFI_ERROR (Status)) { Handles = NULL; HandleCount = 0; } Devices = NULL; while (HandleCount-- != 0) { Status = gBS->HandleProtocol (Handles[HandleCount], &gEfiDevicePathProtocolGuid, (VOID **) &SingleDevice); if (EFI_ERROR (Status) || (SingleDevice == NULL)) { continue; } TempDevicePath = Devices; Devices = AppendDevicePathInstance (Devices, SingleDevice); if (TempDevicePath != NULL) { FreePool (TempDevicePath); } } if (Devices != NULL) { Status = gRT->SetVariable ( mNetworkDeviceList, &gEfiCallerIdGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE, GetDevicePathSize (Devices), Devices ); // // Fails to save the network device list to NV storage is not a fatal error. // Only impact is performance. // FreePool (Devices); } return (Devices == NULL) ? EFI_DEVICE_ERROR : EFI_SUCCESS; }
STATIC BOOLEAN IsParentDevicePath ( IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath, IN EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath ) { UINTN ParentSize; UINTN ChildSize; ParentSize = GetDevicePathSize (ParentDevicePath); ChildSize = GetDevicePathSize (ChildDevicePath); if (ParentSize > ChildSize) { return FALSE; } if (CompareMem (ParentDevicePath, ChildDevicePath, ParentSize - END_DEVICE_PATH_LENGTH) != 0) { return FALSE; } return TRUE; }
/** Process (load and execute) the load option. @param LoadOption Pointer to the load option. @retval EFI_INVALID_PARAMETER The load option type is invalid, or the load option file path doesn't point to a valid file. @retval EFI_UNSUPPORTED The load option type is of LoadOptionTypeBoot. @retval EFI_SUCCESS The load option is inactive, or successfully loaded and executed. **/ EFI_STATUS EFIAPI EfiBootManagerProcessLoadOption ( IN EFI_BOOT_MANAGER_LOAD_OPTION *LoadOption ) { EFI_STATUS Status; EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_HANDLE ImageHandle; EFI_LOADED_IMAGE_PROTOCOL *ImageInfo; VOID *FileBuffer; UINTN FileSize; if ((UINT32) LoadOption->OptionType >= LoadOptionTypeMax) { return EFI_INVALID_PARAMETER; } if (LoadOption->OptionType == LoadOptionTypeBoot) { return EFI_UNSUPPORTED; } // // If a load option is not marked as LOAD_OPTION_ACTIVE, // the boot manager will not automatically load the option. // if ((LoadOption->Attributes & LOAD_OPTION_ACTIVE) == 0) { return EFI_SUCCESS; } Status = EFI_INVALID_PARAMETER; // // Load and start the load option. // DEBUG (( DEBUG_INFO | DEBUG_LOAD, "Process Load Option (%s%04x) ...\n", mBmLoadOptionName[LoadOption->OptionType], LoadOption->OptionNumber )); ImageHandle = NULL; FileBuffer = BmGetLoadOptionBuffer (LoadOption->FilePath, &FilePath, &FileSize); DEBUG_CODE ( if (FileBuffer != NULL && CompareMem (LoadOption->FilePath, FilePath, GetDevicePathSize (FilePath)) != 0) { DEBUG ((EFI_D_INFO, "[Bds] DevicePath expand: ")); BmPrintDp (LoadOption->FilePath); DEBUG ((EFI_D_INFO, " -> ")); BmPrintDp (FilePath); DEBUG ((EFI_D_INFO, "\n")); } );
EFIAPI BdsLibDelPartMatchInstance ( IN EFI_DEVICE_PATH_PROTOCOL *Multi, IN EFI_DEVICE_PATH_PROTOCOL *Single ) { EFI_DEVICE_PATH_PROTOCOL *Instance; EFI_DEVICE_PATH_PROTOCOL *NewDevicePath; EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath; UINTN InstanceSize; UINTN SingleDpSize; UINTN Size; NewDevicePath = NULL; TempNewDevicePath = NULL; if (Multi == NULL || Single == NULL) { return Multi; } Instance = GetNextDevicePathInstance (&Multi, &InstanceSize); SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH; InstanceSize -= END_DEVICE_PATH_LENGTH; while (Instance != NULL) { Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize; if ((CompareMem (Instance, Single, Size) != 0)) { // // Append the device path instance which does not match with Single // TempNewDevicePath = NewDevicePath; NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance); if (TempNewDevicePath != NULL) { FreePool(TempNewDevicePath); } } FreePool(Instance); Instance = GetNextDevicePathInstance (&Multi, &InstanceSize); InstanceSize -= END_DEVICE_PATH_LENGTH; } return NewDevicePath; }
/** Add the image info to a deferred image list. @param[in] ImageDevicePath A pointer to the device path of a image. @param[in] Image Points to the first byte of the image, or NULL if the image is not available. @param[in] ImageSize The size of the image, or 0 if the image is not available. **/ VOID PutDefferedImageInfo ( IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath, IN VOID *Image, IN UINTN ImageSize ) { DEFERRED_IMAGE_INFO *CurImageInfo; UINTN PathSize; // // Expand memory for the new deferred image. // if (mDeferredImage.Count == 0) { mDeferredImage.ImageInfo = AllocatePool (sizeof (DEFERRED_IMAGE_INFO)); ASSERT (mDeferredImage.ImageInfo != NULL); } else { CurImageInfo = AllocatePool ((mDeferredImage.Count + 1) * sizeof (DEFERRED_IMAGE_INFO)); ASSERT (CurImageInfo != NULL); CopyMem ( CurImageInfo, mDeferredImage.ImageInfo, mDeferredImage.Count * sizeof (DEFERRED_IMAGE_INFO) ); FreePool (mDeferredImage.ImageInfo); mDeferredImage.ImageInfo = CurImageInfo; } mDeferredImage.Count++; // // Save the deferred image information. // CurImageInfo = &mDeferredImage.ImageInfo[mDeferredImage.Count - 1]; PathSize = GetDevicePathSize (ImageDevicePath); CurImageInfo->ImageDevicePath = AllocateZeroPool (PathSize); ASSERT (CurImageInfo->ImageDevicePath != NULL); CopyMem (CurImageInfo->ImageDevicePath, ImageDevicePath, PathSize); CurImageInfo->Image = Image; CurImageInfo->ImageSize = ImageSize; CurImageInfo->BootOption = IsBootOption (ImageDevicePath); }
EFIAPI UefiDevicePathLibDuplicateDevicePath ( IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { UINTN Size; // // Compute the size // Size = GetDevicePathSize (DevicePath); if (Size == 0) { return NULL; } // // Allocate space for duplicate device path // return AllocateCopyPool (Size, DevicePath); }
/** Return the index of the load option in the load option array. The function consider two load options are equal when the OptionType, Attributes, Description, FilePath and OptionalData are equal. @param Key Pointer to the load option to be found. @param Array Pointer to the array of load options to be found. @param Count Number of entries in the Array. @retval -1 Key wasn't found in the Array. @retval 0 ~ Count-1 The index of the Key in the Array. **/ INTN PlatformFindLoadOption ( IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Key, IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Array, IN UINTN Count ) { UINTN Index; for (Index = 0; Index < Count; Index++) { if ((Key->OptionType == Array[Index].OptionType) && (Key->Attributes == Array[Index].Attributes) && (StrCmp (Key->Description, Array[Index].Description) == 0) && (CompareMem (Key->FilePath, Array[Index].FilePath, GetDevicePathSize (Key->FilePath)) == 0) && (Key->OptionalDataSize == Array[Index].OptionalDataSize) && (CompareMem (Key->OptionalData, Array[Index].OptionalData, Key->OptionalDataSize) == 0)) { return (INTN) Index; } } return -1; }
EFIAPI GlueDuplicateDevicePath ( IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { EFI_DEVICE_PATH_PROTOCOL *NewDevicePath; UINTN Size; // // Compute the size // Size = GetDevicePathSize (DevicePath); if (Size == 0) { return NULL; } // // Allocate space for duplicate device path // NewDevicePath = AllocateCopyPool (Size, DevicePath); return NewDevicePath; }
VOID BdsPlatformAddBootableImage ( IN CHAR16 *NameString, IN EFI_DEVICE_PATH_PROTOCOL *FileDevicePath ) { UINTN Size; UINTN Index; if (mDefaultBootOptionsCount >= mDefaultBootOptionsMaxCount) { if (mDefaultBootOptionsMaxCount == 0) { // Very first time set a default size mDefaultBootOptionsMaxCount = 4; } Size = mDefaultBootOptionsMaxCount * sizeof (EFI_BOOT_MANAGER_BOOT_OPTION); mDefaultBootOptions = ReallocateCopyPool (Size * 2, Size, mDefaultBootOptions); mDefaultBootOptionsMaxCount *= 2; } for (Index = 0; Index < mDefaultBootOptionsCount; Index++ ) { // // If the boot option has already in buffer, don't add it. // if (!CompareMem (&mDefaultBootOptions[Index].FilePath, FileDevicePath, GetDevicePathSize (FileDevicePath))){ return; } } EfiBootManagerInitializeBootOption ( &mDefaultBootOptions[mDefaultBootOptionsCount++], NameString, FileDevicePath, LOAD_OPTION_ACTIVE, NULL, 0 ); }
/** Create the Boot####, Driver####, SysPrep####, variable from the load option. @param LoadOption Pointer to the load option. @retval EFI_SUCCESS The variable was created. @retval Others Error status returned by RT->SetVariable. **/ EFI_STATUS EFIAPI EfiBootManagerLoadOptionToVariable ( IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Option ) { UINTN VariableSize; UINT8 *Variable; UINT8 *Ptr; CHAR16 OptionName[BM_OPTION_NAME_LEN]; CHAR16 *Description; CHAR16 NullChar; UINT32 VariableAttributes; if ((Option->OptionNumber == LoadOptionNumberUnassigned) || (Option->FilePath == NULL) || ((UINT32) Option->OptionType >= LoadOptionTypeMax) ) { return EFI_INVALID_PARAMETER; } // // Convert NULL description to empty description // NullChar = L'\0'; Description = Option->Description; if (Description == NULL) { Description = &NullChar; } /* UINT32 Attributes; UINT16 FilePathListLength; CHAR16 Description[]; EFI_DEVICE_PATH_PROTOCOL FilePathList[]; UINT8 OptionalData[]; TODO: FilePathList[] IS: A packed array of UEFI device paths. The first element of the array is a device path that describes the device and location of the Image for this load option. The FilePathList[0] is specific to the device type. Other device paths may optionally exist in the FilePathList, but their usage is OSV specific. Each element in the array is variable length, and ends at the device path end structure. */ VariableSize = sizeof (Option->Attributes) + sizeof (UINT16) + StrSize (Description) + GetDevicePathSize (Option->FilePath) + Option->OptionalDataSize; Variable = AllocatePool (VariableSize); ASSERT (Variable != NULL); Ptr = Variable; WriteUnaligned32 ((UINT32 *) Ptr, Option->Attributes); Ptr += sizeof (Option->Attributes); WriteUnaligned16 ((UINT16 *) Ptr, (UINT16) GetDevicePathSize (Option->FilePath)); Ptr += sizeof (UINT16); CopyMem (Ptr, Description, StrSize (Description)); Ptr += StrSize (Description); CopyMem (Ptr, Option->FilePath, GetDevicePathSize (Option->FilePath)); Ptr += GetDevicePathSize (Option->FilePath); CopyMem (Ptr, Option->OptionalData, Option->OptionalDataSize); UnicodeSPrint (OptionName, sizeof (OptionName), L"%s%04x", mBmLoadOptionName[Option->OptionType], Option->OptionNumber); VariableAttributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE; return gRT->SetVariable ( OptionName, &gEfiGlobalVariableGuid, VariableAttributes, VariableSize, Variable ); }
EFI_STATUS LinuxLoaderConfig ( IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage ) { EFI_STATUS Status; LINUX_LOADER_ACTION Choice; UINTN BootOrderSize; UINT16* BootOrder; UINTN BootOrderCount; UINTN Index; CHAR16 Description[MAX_ASCII_INPUT]; CHAR8 CmdLine[MAX_ASCII_INPUT]; CHAR16 Initrd[MAX_STR_INPUT]; UINT16 InitrdPathListLength; UINT16 CmdLineLength; BDS_LOAD_OPTION* BdsLoadOption; BDS_LOAD_OPTION** SupportedBdsLoadOptions; UINTN SupportedBdsLoadOptionCount; LINUX_LOADER_OPTIONAL_DATA* LinuxOptionalData; EFI_DEVICE_PATH* DevicePathRoot; Choice = (LINUX_LOADER_ACTION)0; SupportedBdsLoadOptions = NULL; SupportedBdsLoadOptionCount = 0; do { Print (L"[%d] Create new Linux Boot Entry\n",LINUX_LOADER_NEW); Print (L"[%d] Update Linux Boot Entry\n",LINUX_LOADER_UPDATE); Print (L"Option: "); Status = GetHIInputInteger ((UINTN*)&Choice); if (Status == EFI_INVALID_PARAMETER) { Print (L"\n"); return Status; } else if ((Choice != LINUX_LOADER_NEW) && (Choice != LINUX_LOADER_UPDATE)) { Print (L"Error: the option should be either '%d' or '%d'\n",LINUX_LOADER_NEW,LINUX_LOADER_UPDATE); Status = EFI_INVALID_PARAMETER; } } while (EFI_ERROR(Status)); if (Choice == LINUX_LOADER_UPDATE) { // If no compatible entry then we just create a new entry Choice = LINUX_LOADER_NEW; // Scan the OptionalData of every entry for the correct signature Status = GetGlobalEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder); if (!EFI_ERROR(Status)) { BootOrderCount = BootOrderSize / sizeof(UINT16); // Allocate an array to handle maximum number of supported Boot Entry SupportedBdsLoadOptions = (BDS_LOAD_OPTION**)AllocatePool(sizeof(BDS_LOAD_OPTION*) * BootOrderCount); SupportedBdsLoadOptionCount = 0; // Check if the signature is present in the list of the current Boot entries for (Index = 0; Index < BootOrderCount; Index++) { Status = BootOptionFromLoadOptionIndex (BootOrder[Index], &BdsLoadOption); if (!EFI_ERROR(Status)) { if ((BdsLoadOption->OptionalDataSize >= sizeof(UINT32)) && (*(UINT32*)BdsLoadOption->OptionalData == LINUX_LOADER_SIGNATURE)) { SupportedBdsLoadOptions[SupportedBdsLoadOptionCount++] = BdsLoadOption; Choice = LINUX_LOADER_UPDATE; } } } } FreePool (BootOrder); } if (Choice == LINUX_LOADER_NEW) { Description[0] = '\0'; CmdLine[0] = '\0'; Initrd[0] = '\0'; BdsLoadOption = (BDS_LOAD_OPTION*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION)); DEBUG_CODE_BEGIN(); CHAR16* DevicePathTxt; EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol; Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol); ASSERT_EFI_ERROR(Status); DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (LoadedImage->FilePath, TRUE, TRUE); Print(L"EFI OS Loader: %s\n",DevicePathTxt); FreePool(DevicePathTxt); DEBUG_CODE_END(); // // Fill the known fields of BdsLoadOption // BdsLoadOption->Attributes = LOAD_OPTION_ACTIVE | LOAD_OPTION_CATEGORY_BOOT; // Get the full Device Path for this file Status = gBS->HandleProtocol (LoadedImage->DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathRoot); ASSERT_EFI_ERROR(Status); BdsLoadOption->FilePathList = AppendDevicePath (DevicePathRoot, LoadedImage->FilePath); BdsLoadOption->FilePathListLength = GetDevicePathSize (BdsLoadOption->FilePathList); } else { if (SupportedBdsLoadOptionCount > 1) { for (Index = 0; Index < SupportedBdsLoadOptionCount; Index++) { Print (L"[%d] %s\n",Index + 1,SupportedBdsLoadOptions[Index]->Description); } do { Print (L"Update Boot Entry: "); Status = GetHIInputInteger ((UINTN*)&Choice); if (Status == EFI_INVALID_PARAMETER) { Print (L"\n"); return Status; } else if ((Choice < 1) && (Choice > SupportedBdsLoadOptionCount)) { Print (L"Choose entry from 1 to %d\n",SupportedBdsLoadOptionCount); Status = EFI_INVALID_PARAMETER; } } while (EFI_ERROR(Status)); BdsLoadOption = SupportedBdsLoadOptions[Choice-1]; } StrnCpy (Description, BdsLoadOption->Description, MAX_STR_INPUT); LinuxOptionalData = (LINUX_LOADER_OPTIONAL_DATA*)BdsLoadOption->OptionalData; if (LinuxOptionalData->CmdLineLength > 0) { CopyMem (CmdLine, (CHAR8*)LinuxOptionalData + sizeof(LINUX_LOADER_OPTIONAL_DATA), LinuxOptionalData->CmdLineLength); } else { CmdLine[0] = '\0'; } if (LinuxOptionalData->InitrdPathListLength > 0) { CopyMem (Initrd, (CHAR8*)LinuxOptionalData + sizeof(LINUX_LOADER_OPTIONAL_DATA) + LinuxOptionalData->CmdLineLength, LinuxOptionalData->InitrdPathListLength); } else { Initrd[0] = L'\0'; } DEBUG((EFI_D_ERROR,"L\n")); } // Description Print (L"Description: "); Status = EditHIInputStr (Description, MAX_STR_INPUT); if (EFI_ERROR(Status)) { return Status; } if (StrLen (Description) == 0) { StrnCpy (Description, DEFAULT_BOOT_ENTRY_DESCRIPTION, MAX_STR_INPUT); } BdsLoadOption->Description = Description; // CmdLine Print (L"Command Line: "); Status = EditHIInputAscii (CmdLine, MAX_ASCII_INPUT); if (EFI_ERROR(Status)) { return Status; } // Initrd Print (L"Initrd name: "); Status = EditHIInputStr (Initrd, MAX_STR_INPUT); if (EFI_ERROR(Status)) { return Status; } CmdLineLength = AsciiStrLen (CmdLine); if (CmdLineLength > 0) { CmdLineLength += sizeof(CHAR8); } InitrdPathListLength = StrLen (Initrd) * sizeof(CHAR16); if (InitrdPathListLength > 0) { InitrdPathListLength += sizeof(CHAR16); } BdsLoadOption->OptionalDataSize = sizeof(LINUX_LOADER_OPTIONAL_DATA) + CmdLineLength + InitrdPathListLength; LinuxOptionalData = (LINUX_LOADER_OPTIONAL_DATA*)AllocatePool (BdsLoadOption->OptionalDataSize); BdsLoadOption->OptionalData = LinuxOptionalData; LinuxOptionalData->Signature = LINUX_LOADER_SIGNATURE; LinuxOptionalData->CmdLineLength = CmdLineLength; LinuxOptionalData->InitrdPathListLength = InitrdPathListLength; if (CmdLineLength > 0) { CopyMem (LinuxOptionalData + 1, CmdLine, CmdLineLength); } if (InitrdPathListLength > 0) { CopyMem ((UINT8*)(LinuxOptionalData + 1) + CmdLineLength, Initrd, InitrdPathListLength); } // Create or Update the boot entry Status = BootOptionToLoadOptionVariable (BdsLoadOption); return Status; }
/** Check whether the DevicePath2 is identical with DevicePath1, or identical with DevicePath1's child device path. If DevicePath2 is identical with DevicePath1, or with DevicePath1's child device path, then TRUE returned. Otherwise, FALSE is returned. If DevicePath1 is NULL, then ASSERT(). If DevicePath2 is NULL, then ASSERT(). @param[in] DevicePath1 A pointer to a device path. @param[in] DevicePath2 A pointer to a device path. @retval TRUE Two device paths are identical , or DevicePath2 is DevicePath1's child device path. @retval FALSE Two device paths are not identical, and DevicePath2 is not DevicePath1's child device path. **/ BOOLEAN CheckDevicePath ( IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath1, IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath2 ) { UINTN DevicePathSize; UINTN FileNameSize1; UINTN FileNameSize2; UINT8 *FileName1; UINT8 *FileName2; UINTN FileNameOffset1; UINTN FileNameOffset2; BOOLEAN DevicePathEqual; FileName1 = NULL; FileName2 = NULL; DevicePathEqual = TRUE; ASSERT (DevicePath1 != NULL); ASSERT (DevicePath2 != NULL); if (IsDevicePathEnd (DevicePath1)) { return FALSE; } // // The file name may contain one or more device path node. // To compare the file name, copy file name to a buffer and compare the buffer. // FileNameSize1 = GetFileName (DevicePath1, &FileName1, &FileNameOffset1); if (FileNameSize1 != 0) { FileNameSize2 = GetFileName (DevicePath2, &FileName2, &FileNameOffset2); if (FileNameOffset1 != FileNameOffset2) { DevicePathEqual = FALSE; goto Done; } if (CompareMem (DevicePath1, DevicePath2, FileNameOffset1) != 0) { DevicePathEqual = FALSE; goto Done; } if (FileNameSize1 > FileNameSize2) { DevicePathEqual = FALSE; goto Done; } if (CompareMem (FileName1, FileName2, FileNameSize1) != 0) { DevicePathEqual = FALSE; goto Done; } DevicePathEqual = TRUE; goto Done; } DevicePathSize = GetDevicePathSize (DevicePath1); if (DevicePathSize > GetDevicePathSize (DevicePath2)) { return FALSE; } // // Exclude the end of device path node. // DevicePathSize -= sizeof (EFI_DEVICE_PATH_PROTOCOL); if (CompareMem (DevicePath1, DevicePath2, DevicePathSize) != 0) { DevicePathEqual = FALSE; } Done: if (FileName1 != NULL) { FreePool (FileName1); } if (FileName2 != NULL) { FreePool (FileName2); } return DevicePathEqual; }
EFI_STATUS GetGopDevicePath ( IN EFI_DEVICE_PATH_PROTOCOL *PciDevicePath, OUT EFI_DEVICE_PATH_PROTOCOL **GopDevicePath ) { UINTN Index; EFI_STATUS Status; EFI_HANDLE PciDeviceHandle; EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; EFI_DEVICE_PATH_PROTOCOL *TempPciDevicePath; UINTN GopHandleCount; EFI_HANDLE *GopHandleBuffer; if (PciDevicePath == NULL || GopDevicePath == NULL) { return EFI_INVALID_PARAMETER; } // // Initialize the GopDevicePath to be PciDevicePath // *GopDevicePath = PciDevicePath; TempPciDevicePath = PciDevicePath; Status = gBS->LocateDevicePath ( &gEfiDevicePathProtocolGuid, &TempPciDevicePath, &PciDeviceHandle ); if (EFI_ERROR (Status)) { return Status; } // // Try to connect this handle, so that GOP dirver could start on this // device and create child handles with GraphicsOutput Protocol installed // on them, then we get device paths of these child handles and select // them as possible console device. // gBS->ConnectController (PciDeviceHandle, NULL, NULL, FALSE); Status = gBS->LocateHandleBuffer ( ByProtocol, &gEfiGraphicsOutputProtocolGuid, NULL, &GopHandleCount, &GopHandleBuffer ); if (!EFI_ERROR (Status)) { // // Add all the child handles as possible Console Device // for (Index = 0; Index < GopHandleCount; Index++) { Status = gBS->HandleProtocol (GopHandleBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID*)&TempDevicePath); if (EFI_ERROR (Status)) { continue; } if (CompareMem ( PciDevicePath, TempDevicePath, GetDevicePathSize (PciDevicePath) - END_DEVICE_PATH_LENGTH ) == 0) { // // In current implementation, we only enable one of the child handles // as console device, i.e. sotre one of the child handle's device // path to variable "ConOut" // In futhure, we could select all child handles to be console device // *GopDevicePath = TempDevicePath; // // Delete the PCI device's path that added by GetPlugInPciVgaDevicePath() // Add the integrity GOP device path. // BdsLibUpdateConsoleVariable (VarConsoleOutDev, NULL, PciDevicePath); BdsLibUpdateConsoleVariable (VarConsoleOutDev, TempDevicePath, NULL); } } gBS->FreePool (GopHandleBuffer); } return EFI_SUCCESS; }
/** Display the forbid load device path list (mAccessInfo.LoadForbid). **/ VOID DisplayLoadForbid ( VOID ) { UINTN Offset; UINTN DPSize; UINTN Index; EFI_DEVICE_PATH_PROTOCOL *Dp; VOID *StartOpCodeHandle; VOID *EndOpCodeHandle; EFI_IFR_GUID_LABEL *StartLabel; EFI_IFR_GUID_LABEL *EndLabel; // // Initialize the container for dynamic opcodes. // StartOpCodeHandle = HiiAllocateOpCodeHandle (); ASSERT (StartOpCodeHandle != NULL); EndOpCodeHandle = HiiAllocateOpCodeHandle (); ASSERT (EndOpCodeHandle != NULL); // // Create Hii Extend Label OpCode. // StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL) ); StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; StartLabel->Number = LABLE_FORBID_LOAD_FUNC; EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL) ); EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; EndLabel->Number = LABEL_END; // // Add each forbid load drivers. // Offset = 0; Index = 0; while (Offset < mAccessInfo.LoadForbidLen) { Dp = (EFI_DEVICE_PATH_PROTOCOL *) (mAccessInfo.LoadForbid + Offset); DPSize = GetDevicePathSize (Dp); AddDevicePath ( KEY_MODIFY_USER | KEY_MODIFY_AP_DP | KEY_LOAD_FORBID_MODIFY | Index, Dp, StartOpCodeHandle ); Index++; Offset += DPSize; } HiiUpdateForm ( mCallbackInfo->HiiHandle, // HII handle &gUserProfileManagerGuid, // Formset GUID FORMID_FORBID_LOAD_DP, // Form ID StartOpCodeHandle, // Label for where to insert opcodes EndOpCodeHandle // Replace data ); HiiFreeOpCodeHandle (StartOpCodeHandle); HiiFreeOpCodeHandle (EndOpCodeHandle); }
/** This function will register the new boot#### or driver#### option base on the VariableName. The new registered boot#### or driver#### will be linked to BdsOptionList and also update to the VariableName. After the boot#### or driver#### updated, the BootOrder or DriverOrder will also be updated. @param BdsOptionList The header of the boot#### or driver#### link list @param DevicePath The device path which the boot#### or driver#### option present @param String The description of the boot#### or driver#### @param VariableName Indicate if the boot#### or driver#### option @retval EFI_SUCCESS The boot#### or driver#### have been success registered @retval EFI_STATUS Return the status of gRT->SetVariable (). **/ EFI_STATUS EFIAPI BdsLibRegisterNewOption ( IN LIST_ENTRY *BdsOptionList, IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN CHAR16 *String, IN CHAR16 *VariableName ) { EFI_STATUS Status; UINTN Index; UINT16 RegisterOptionNumber; UINT16 *TempOptionPtr; UINTN TempOptionSize; UINT16 *OptionOrderPtr; VOID *OptionPtr; UINTN OptionSize; UINT8 *TempPtr; EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath; CHAR16 *Description; CHAR16 OptionName[10]; BOOLEAN UpdateDescription; UINT16 BootOrderEntry; UINTN OrderItemNum; if (DevicePath == NULL) { return EFI_INVALID_PARAMETER; } OptionPtr = NULL; OptionSize = 0; TempPtr = NULL; OptionDevicePath = NULL; Description = NULL; OptionOrderPtr = NULL; UpdateDescription = FALSE; Status = EFI_SUCCESS; ZeroMem (OptionName, sizeof (OptionName)); TempOptionSize = 0; TempOptionPtr = BdsLibGetVariableAndSize ( VariableName, &gEfiGlobalVariableGuid, &TempOptionSize ); // // Compare with current option variable if the previous option is set in global variable. // for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) { // // TempOptionPtr must not be NULL if we have non-zero TempOptionSize. // // ASSERT (TempOptionPtr != NULL); if (!TempOptionPtr) { return EFI_NOT_FOUND; } if (*VariableName == 'B') { UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]); } else { UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]); } OptionPtr = BdsLibGetVariableAndSize ( OptionName, &gEfiGlobalVariableGuid, &OptionSize ); if (OptionPtr == NULL) { continue; } // // Validate the variable. // if (!ValidateOption(OptionPtr, OptionSize)) { FreePool(OptionPtr); continue; } TempPtr = OptionPtr; TempPtr += sizeof (UINT32) + sizeof (UINT16); Description = (CHAR16 *) TempPtr; TempPtr += StrSize ((CHAR16 *) TempPtr); OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr; // // Notes: the description may will change base on the GetStringToken // if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) { if (CompareMem (Description, String, StrSize (Description)) == 0) { // // Got the option, so just return // FreePool (OptionPtr); FreePool (TempOptionPtr); return EFI_SUCCESS; } else { // // Option description changed, need update. // UpdateDescription = TRUE; FreePool (OptionPtr); break; } } FreePool (OptionPtr); } OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String); OptionSize += GetDevicePathSize (DevicePath); OptionPtr = AllocateZeroPool (OptionSize); // ASSERT (OptionPtr != NULL); if (!OptionPtr) { return EFI_OUT_OF_RESOURCES; } TempPtr = OptionPtr; *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE; TempPtr += sizeof (UINT32); *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath); TempPtr += sizeof (UINT16); CopyMem (TempPtr, String, StrSize (String)); TempPtr += StrSize (String); CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath)); if (UpdateDescription) { // // The number in option#### to be updated. // In this case, we must have non-NULL TempOptionPtr. // // ASSERT (TempOptionPtr != NULL); if (!TempOptionPtr) { return EFI_NOT_FOUND; } RegisterOptionNumber = TempOptionPtr[Index]; } else { // // The new option#### number // RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName); } if (*VariableName == 'B') { UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber); } else { UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber); } Status = gRT->SetVariable ( OptionName, &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, OptionSize, OptionPtr ); // // Return if only need to update a changed description or fail to set option. // if (EFI_ERROR (Status) || UpdateDescription) { FreePool (OptionPtr); if (TempOptionPtr != NULL) { FreePool (TempOptionPtr); } return Status; } FreePool (OptionPtr); // // Update the option order variable // // // If no option order // if (TempOptionSize == 0) { BootOrderEntry = 0; Status = gRT->SetVariable ( VariableName, &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, sizeof (UINT16), &BootOrderEntry ); if (TempOptionPtr != NULL) { FreePool (TempOptionPtr); } return Status; } // // TempOptionPtr must not be NULL if TempOptionSize is not zero. // // ASSERT (TempOptionPtr != NULL); if (!TempOptionPtr) { return EFI_NOT_FOUND; } // // Append the new option number to the original option order // OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ; OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16)); // ASSERT (OptionOrderPtr!= NULL); if (!OptionOrderPtr) { return EFI_NOT_FOUND; } CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16)); OptionOrderPtr[Index] = RegisterOptionNumber; Status = gRT->SetVariable ( VariableName, &gEfiGlobalVariableGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, OrderItemNum * sizeof (UINT16), OptionOrderPtr ); FreePool (TempOptionPtr); FreePool (OptionOrderPtr); return Status; }
EFIAPI BdsLibVariableToOption ( IN OUT LIST_ENTRY *BdsCommonOptionList, IN CHAR16 *VariableName ) { UINT32 Attribute; UINT16 FilePathSize; UINT8 *Variable; UINT8 *TempPtr; UINTN VariableSize; EFI_DEVICE_PATH_PROTOCOL *DevicePath; BDS_COMMON_OPTION *Option; VOID *LoadOptions; UINT32 LoadOptionsSize; CHAR16 *Description; UINT8 NumOff; // // Read the variable. We will never free this data. // Variable = BdsLibGetVariableAndSize ( VariableName, &gEfiGlobalVariableGuid, &VariableSize ); if (Variable == NULL) { return NULL; } // // Validate Boot#### variable data. // if (!ValidateOption(Variable, VariableSize)) { FreePool (Variable); return NULL; } // // Notes: careful defined the variable of Boot#### or // Driver####, consider use some macro to abstract the code // // // Get the option attribute // TempPtr = Variable; Attribute = *(UINT32 *) Variable; TempPtr += sizeof (UINT32); // // Get the option's device path size // FilePathSize = *(UINT16 *) TempPtr; TempPtr += sizeof (UINT16); // // Get the option's description string // Description = (CHAR16 *) TempPtr; // // Get the option's description string size // TempPtr += StrSize((CHAR16 *) TempPtr); // // Get the option's device path // DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr; TempPtr += FilePathSize; // // Get load opion data. // LoadOptions = TempPtr; LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable)); // // The Console variables may have multiple device paths, so make // an Entry for each one. // Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION)); if (Option == NULL) { FreePool (Variable); return NULL; } Option->Signature = BDS_LOAD_OPTION_SIGNATURE; Option->DevicePath = AllocateCopyPool (GetDevicePathSize (DevicePath), DevicePath); // ASSERT(Option->DevicePath != NULL); if (!Option->DevicePath) { FreePool (Option); return NULL; } // CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath)); Option->Attribute = Attribute; Option->Description = AllocateCopyPool (StrSize (Description), Description); // ASSERT(Option->Description != NULL); if (!Option->Description) { FreePool (Option); return NULL; } // CopyMem (Option->Description, Description, StrSize (Description)); Option->LoadOptions = AllocateCopyPool (LoadOptionsSize, LoadOptions); // ASSERT(Option->LoadOptions != NULL); if (!Option->LoadOptions) { FreePool (Option); return NULL; } // CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize); Option->LoadOptionsSize = LoadOptionsSize; // // Get the value from VariableName Unicode string // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this // Unicode stream to ASCII without any loss in meaning. // if (*VariableName == 'B') { NumOff = (UINT8) (sizeof (L"Boot") / sizeof (CHAR16) - 1); Option->BootCurrent = (UINT16) (CharToUint (VariableName[NumOff+0]) * 0x1000) + (UINT16) (CharToUint (VariableName[NumOff+1]) * 0x100) + (UINT16) (CharToUint (VariableName[NumOff+2]) * 0x10) + (UINT16) (CharToUint (VariableName[NumOff+3]) * 0x1); } InsertTailList (BdsCommonOptionList, &Option->Link); FreePool (Variable); return Option; }
STATIC EFI_STATUS GetConsoleDevicePathFromVariable ( IN CHAR16* ConsoleVarName, IN CHAR16* DefaultConsolePaths, OUT EFI_DEVICE_PATH** DevicePaths ) { EFI_STATUS Status; UINTN Size; EFI_DEVICE_PATH_PROTOCOL* DevicePathInstances; EFI_DEVICE_PATH_PROTOCOL* DevicePathInstance; CHAR16* DevicePathStr; CHAR16* NextDevicePathStr; EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol; Status = GetGlobalEnvironmentVariable (ConsoleVarName, NULL, NULL, (VOID**)&DevicePathInstances); if (EFI_ERROR(Status)) { // In case no default console device path has been defined we assume a driver handles the console (eg: SimpleTextInOutSerial) if ((DefaultConsolePaths == NULL) || (DefaultConsolePaths[0] == L'\0')) { *DevicePaths = NULL; return EFI_SUCCESS; } Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol); ASSERT_EFI_ERROR(Status); DevicePathInstances = NULL; // Extract the Device Path instances from the multi-device path string while ((DefaultConsolePaths != NULL) && (DefaultConsolePaths[0] != L'\0')) { NextDevicePathStr = StrStr (DefaultConsolePaths, L";"); if (NextDevicePathStr == NULL) { DevicePathStr = DefaultConsolePaths; DefaultConsolePaths = NULL; } else { DevicePathStr = (CHAR16*)AllocateCopyPool ((NextDevicePathStr - DefaultConsolePaths + 1) * sizeof(CHAR16), DefaultConsolePaths); *(DevicePathStr + (NextDevicePathStr - DefaultConsolePaths)) = L'\0'; DefaultConsolePaths = NextDevicePathStr; if (DefaultConsolePaths[0] == L';') { DefaultConsolePaths++; } } DevicePathInstance = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (DevicePathStr); ASSERT(DevicePathInstance != NULL); DevicePathInstances = AppendDevicePathInstance (DevicePathInstances, DevicePathInstance); if (NextDevicePathStr != NULL) { FreePool (DevicePathStr); } FreePool (DevicePathInstance); } // Set the environment variable with this device path multi-instances Size = GetDevicePathSize (DevicePathInstances); if (Size > 0) { gRT->SetVariable ( ConsoleVarName, &gEfiGlobalVariableGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, Size, DevicePathInstances ); } else { Status = EFI_INVALID_PARAMETER; } } if (!EFI_ERROR(Status)) { *DevicePaths = DevicePathInstances; } return Status; }
/** This function create a currently loaded Boot Option from the BMM. It then appends this Boot Option to the end of the "BootOrder" list. It also append this Boot Opotion to the end of BootOptionMenu. @param CallbackData The BMM context data. @retval EFI_OUT_OF_RESOURCES If not enought memory to complete the operation. @retval EFI_SUCCESS If function completes successfully. **/ EFI_STATUS Var_UpdateBootOption ( IN BMM_CALLBACK_DATA *CallbackData ) { UINT16 *BootOrderList; UINT16 *NewBootOrderList; UINTN BootOrderListSize; UINT16 BootString[10]; VOID *Buffer; UINTN BufferSize; UINT8 *Ptr; UINT16 Index; BM_MENU_ENTRY *NewMenuEntry; BM_LOAD_CONTEXT *NewLoadContext; BOOLEAN OptionalDataExist; EFI_STATUS Status; BMM_FAKE_NV_DATA *NvRamMap; OptionalDataExist = FALSE; NvRamMap = &CallbackData->BmmFakeNvData; Index = BOpt_GetBootOptionNumber () ; UnicodeSPrint (BootString, sizeof (BootString), L"Boot%04x", Index); if (NvRamMap->BootDescriptionData[0] == 0x0000) { StrCpyS (NvRamMap->BootDescriptionData, sizeof (NvRamMap->BootDescriptionData) / sizeof (NvRamMap->BootDescriptionData[0]), BootString); } BufferSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (NvRamMap->BootDescriptionData); BufferSize += GetDevicePathSize (CallbackData->LoadContext->FilePathList); if (NvRamMap->BootOptionalData[0] != 0x0000) { OptionalDataExist = TRUE; BufferSize += StrSize (NvRamMap->BootOptionalData); } Buffer = AllocateZeroPool (BufferSize); if (NULL == Buffer) { return EFI_OUT_OF_RESOURCES; } NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT); if (NULL == NewMenuEntry) { return EFI_OUT_OF_RESOURCES; } NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext; NewLoadContext->Deleted = FALSE; NewLoadContext->LoadOptionSize = BufferSize; Ptr = (UINT8 *) Buffer; NewLoadContext->LoadOption = Ptr; *((UINT32 *) Ptr) = LOAD_OPTION_ACTIVE; NewLoadContext->Attributes = *((UINT32 *) Ptr); NewLoadContext->IsActive = TRUE; NewLoadContext->ForceReconnect = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_FORCE_RECONNECT); Ptr += sizeof (UINT32); *((UINT16 *) Ptr) = (UINT16) GetDevicePathSize (CallbackData->LoadContext->FilePathList); NewLoadContext->FilePathListLength = *((UINT16 *) Ptr); Ptr += sizeof (UINT16); CopyMem ( Ptr, NvRamMap->BootDescriptionData, StrSize (NvRamMap->BootDescriptionData) ); NewLoadContext->Description = AllocateZeroPool (StrSize (NvRamMap->BootDescriptionData)); ASSERT (NewLoadContext->Description != NULL); NewMenuEntry->DisplayString = NewLoadContext->Description; CopyMem ( NewLoadContext->Description, (VOID *) Ptr, StrSize (NvRamMap->BootDescriptionData) ); Ptr += StrSize (NvRamMap->BootDescriptionData); CopyMem ( Ptr, CallbackData->LoadContext->FilePathList, GetDevicePathSize (CallbackData->LoadContext->FilePathList) ); NewLoadContext->FilePathList = AllocateZeroPool (GetDevicePathSize (CallbackData->LoadContext->FilePathList)); ASSERT (NewLoadContext->FilePathList != NULL); CopyMem ( NewLoadContext->FilePathList, (VOID *) Ptr, GetDevicePathSize (CallbackData->LoadContext->FilePathList) ); NewMenuEntry->HelpString = UiDevicePathToStr (NewLoadContext->FilePathList); NewMenuEntry->OptionNumber = Index; NewMenuEntry->DisplayStringToken = HiiSetString (CallbackData->BmmHiiHandle, 0, NewMenuEntry->DisplayString, NULL); NewMenuEntry->HelpStringToken = HiiSetString (CallbackData->BmmHiiHandle, 0, NewMenuEntry->HelpString, NULL); if (OptionalDataExist) { Ptr += (UINT8) GetDevicePathSize (CallbackData->LoadContext->FilePathList); CopyMem (Ptr, NvRamMap->BootOptionalData, StrSize (NvRamMap->BootOptionalData)); } Status = gRT->SetVariable ( BootString, &gEfiGlobalVariableGuid, VAR_FLAG, BufferSize, Buffer ); ASSERT_EFI_ERROR (Status); GetEfiGlobalVariable2 (L"BootOrder", (VOID **) &BootOrderList, &BootOrderListSize); NewBootOrderList = AllocateZeroPool (BootOrderListSize + sizeof (UINT16)); ASSERT (NewBootOrderList != NULL); if (BootOrderList != NULL){ CopyMem (NewBootOrderList, BootOrderList, BootOrderListSize); } NewBootOrderList[BootOrderListSize / sizeof (UINT16)] = Index; if (BootOrderList != NULL) { FreePool (BootOrderList); } Status = gRT->SetVariable ( L"BootOrder", &gEfiGlobalVariableGuid, VAR_FLAG, BootOrderListSize + sizeof (UINT16), NewBootOrderList ); ASSERT_EFI_ERROR (Status); FreePool (NewBootOrderList); NewBootOrderList = NULL; InsertTailList (&BootOptionMenu.Head, &NewMenuEntry->Link); BootOptionMenu.MenuNumber++; return EFI_SUCCESS; }
/** This function create a currently loaded Drive Option from the BMM. It then appends this Driver Option to the end of the "DriverOrder" list. It append this Driver Opotion to the end of DriverOptionMenu. @param CallbackData The BMM context data. @param HiiHandle The HII handle associated with the BMM formset. @param DescriptionData The description of this driver option. @param OptionalData The optional load option. @param ForceReconnect If to force reconnect. @retval EFI_OUT_OF_RESOURCES If not enought memory to complete the operation. @retval EFI_SUCCESS If function completes successfully. **/ EFI_STATUS Var_UpdateDriverOption ( IN BMM_CALLBACK_DATA *CallbackData, IN EFI_HII_HANDLE HiiHandle, IN UINT16 *DescriptionData, IN UINT16 *OptionalData, IN UINT8 ForceReconnect ) { UINT16 Index; UINT16 *DriverOrderList; UINT16 *NewDriverOrderList; UINT16 DriverString[12]; UINTN DriverOrderListSize; VOID *Buffer; UINTN BufferSize; UINT8 *Ptr; BM_MENU_ENTRY *NewMenuEntry; BM_LOAD_CONTEXT *NewLoadContext; BOOLEAN OptionalDataExist; EFI_STATUS Status; OptionalDataExist = FALSE; Index = BOpt_GetDriverOptionNumber (); UnicodeSPrint ( DriverString, sizeof (DriverString), L"Driver%04x", Index ); if (*DescriptionData == 0x0000) { StrCpyS (DescriptionData, MAX_MENU_NUMBER, DriverString); } BufferSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (DescriptionData); BufferSize += GetDevicePathSize (CallbackData->LoadContext->FilePathList); if (*OptionalData != 0x0000) { OptionalDataExist = TRUE; BufferSize += StrSize (OptionalData); } Buffer = AllocateZeroPool (BufferSize); if (NULL == Buffer) { return EFI_OUT_OF_RESOURCES; } NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT); if (NULL == NewMenuEntry) { FreePool (Buffer); return EFI_OUT_OF_RESOURCES; } NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext; NewLoadContext->Deleted = FALSE; NewLoadContext->LoadOptionSize = BufferSize; Ptr = (UINT8 *) Buffer; NewLoadContext->LoadOption = Ptr; *((UINT32 *) Ptr) = LOAD_OPTION_ACTIVE | (ForceReconnect << 1); NewLoadContext->Attributes = *((UINT32 *) Ptr); NewLoadContext->IsActive = TRUE; NewLoadContext->ForceReconnect = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_FORCE_RECONNECT); Ptr += sizeof (UINT32); *((UINT16 *) Ptr) = (UINT16) GetDevicePathSize (CallbackData->LoadContext->FilePathList); NewLoadContext->FilePathListLength = *((UINT16 *) Ptr); Ptr += sizeof (UINT16); CopyMem ( Ptr, DescriptionData, StrSize (DescriptionData) ); NewLoadContext->Description = AllocateZeroPool (StrSize (DescriptionData)); ASSERT (NewLoadContext->Description != NULL); NewMenuEntry->DisplayString = NewLoadContext->Description; CopyMem ( NewLoadContext->Description, (VOID *) Ptr, StrSize (DescriptionData) ); Ptr += StrSize (DescriptionData); CopyMem ( Ptr, CallbackData->LoadContext->FilePathList, GetDevicePathSize (CallbackData->LoadContext->FilePathList) ); NewLoadContext->FilePathList = AllocateZeroPool (GetDevicePathSize (CallbackData->LoadContext->FilePathList)); ASSERT (NewLoadContext->FilePathList != NULL); CopyMem ( NewLoadContext->FilePathList, (VOID *) Ptr, GetDevicePathSize (CallbackData->LoadContext->FilePathList) ); NewMenuEntry->HelpString = UiDevicePathToStr (NewLoadContext->FilePathList); NewMenuEntry->OptionNumber = Index; NewMenuEntry->DisplayStringToken = HiiSetString (HiiHandle, 0, NewMenuEntry->DisplayString, NULL); NewMenuEntry->HelpStringToken = HiiSetString (HiiHandle, 0, NewMenuEntry->HelpString, NULL); if (OptionalDataExist) { Ptr += (UINT8) GetDevicePathSize (CallbackData->LoadContext->FilePathList); CopyMem ( Ptr, OptionalData, StrSize (OptionalData) ); } Status = gRT->SetVariable ( DriverString, &gEfiGlobalVariableGuid, VAR_FLAG, BufferSize, Buffer ); ASSERT_EFI_ERROR (Status); GetEfiGlobalVariable2 (L"DriverOrder", (VOID **) &DriverOrderList, &DriverOrderListSize); NewDriverOrderList = AllocateZeroPool (DriverOrderListSize + sizeof (UINT16)); ASSERT (NewDriverOrderList != NULL); if (DriverOrderList != NULL){ CopyMem (NewDriverOrderList, DriverOrderList, DriverOrderListSize); } NewDriverOrderList[DriverOrderListSize / sizeof (UINT16)] = Index; if (DriverOrderList != NULL) { EfiLibDeleteVariable (L"DriverOrder", &gEfiGlobalVariableGuid); } Status = gRT->SetVariable ( L"DriverOrder", &gEfiGlobalVariableGuid, VAR_FLAG, DriverOrderListSize + sizeof (UINT16), NewDriverOrderList ); ASSERT_EFI_ERROR (Status); if (DriverOrderList != NULL) { FreePool (DriverOrderList); } DriverOrderList = NULL; FreePool (NewDriverOrderList); InsertTailList (&DriverOptionMenu.Head, &NewMenuEntry->Link); DriverOptionMenu.MenuNumber++; return EFI_SUCCESS; }
/** This function delete and build multi-instance device path for specified type of console device. This function clear the EFI variable defined by ConsoleName and gEfiGlobalVariableGuid. It then build the multi-instance device path by appending the device path of the Console (In/Out/Err) instance in ConsoleMenu. Then it scan all corresponding console device by scanning Terminal (built from device supporting Serial I/O instances) devices in TerminalMenu. At last, it save a EFI variable specifed by ConsoleName and gEfiGlobalVariableGuid. @param ConsoleName The name for the console device type. They are usually "ConIn", "ConOut" and "ErrOut". @param ConsoleMenu The console memu which is a list of console devices. @param UpdatePageId The flag specifying which type of console device to be processed. @retval EFI_SUCCESS The function complete successfully. @return The EFI variable can not be saved. See gRT->SetVariable for detail return information. **/ EFI_STATUS Var_UpdateConsoleOption ( IN UINT16 *ConsoleName, IN BM_MENU_OPTION *ConsoleMenu, IN UINT16 UpdatePageId ) { EFI_DEVICE_PATH_PROTOCOL *ConDevicePath; BM_MENU_ENTRY *NewMenuEntry; BM_CONSOLE_CONTEXT *NewConsoleContext; BM_TERMINAL_CONTEXT *NewTerminalContext; EFI_STATUS Status; VENDOR_DEVICE_PATH Vendor; EFI_DEVICE_PATH_PROTOCOL *TerminalDevicePath; UINTN Index; GetEfiGlobalVariable2 (ConsoleName, (VOID**)&ConDevicePath, NULL); if (ConDevicePath != NULL) { EfiLibDeleteVariable (ConsoleName, &gEfiGlobalVariableGuid); FreePool (ConDevicePath); ConDevicePath = NULL; }; // // First add all console input device from console input menu // for (Index = 0; Index < ConsoleMenu->MenuNumber; Index++) { NewMenuEntry = BOpt_GetMenuEntry (ConsoleMenu, Index); NewConsoleContext = (BM_CONSOLE_CONTEXT *) NewMenuEntry->VariableContext; if (NewConsoleContext->IsActive) { ConDevicePath = AppendDevicePathInstance ( ConDevicePath, NewConsoleContext->DevicePath ); } } for (Index = 0; Index < TerminalMenu.MenuNumber; Index++) { NewMenuEntry = BOpt_GetMenuEntry (&TerminalMenu, Index); NewTerminalContext = (BM_TERMINAL_CONTEXT *) NewMenuEntry->VariableContext; if (((NewTerminalContext->IsConIn != 0) && (UpdatePageId == FORM_CON_IN_ID)) || ((NewTerminalContext->IsConOut != 0) && (UpdatePageId == FORM_CON_OUT_ID)) || ((NewTerminalContext->IsStdErr != 0) && (UpdatePageId == FORM_CON_ERR_ID)) ) { Vendor.Header.Type = MESSAGING_DEVICE_PATH; Vendor.Header.SubType = MSG_VENDOR_DP; ASSERT (NewTerminalContext->TerminalType < (sizeof (TerminalTypeGuid) / sizeof (TerminalTypeGuid[0]))); CopyMem ( &Vendor.Guid, &TerminalTypeGuid[NewTerminalContext->TerminalType], sizeof (EFI_GUID) ); SetDevicePathNodeLength (&Vendor.Header, sizeof (VENDOR_DEVICE_PATH)); TerminalDevicePath = AppendDevicePathNode ( NewTerminalContext->DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &Vendor ); ASSERT (TerminalDevicePath != NULL); ChangeTerminalDevicePath (TerminalDevicePath, TRUE); ConDevicePath = AppendDevicePathInstance ( ConDevicePath, TerminalDevicePath ); } } if (ConDevicePath != NULL) { Status = gRT->SetVariable ( ConsoleName, &gEfiGlobalVariableGuid, VAR_FLAG, GetDevicePathSize (ConDevicePath), ConDevicePath ); if (EFI_ERROR (Status)) { return Status; } } return EFI_SUCCESS; }