]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideDxe.c
085b8dd5cd06257eafc7e5546d7066b473addeb9
[mirror_edk2.git] / MdeModulePkg / Universal / PlatformDriOverrideDxe / PlatDriOverrideDxe.c
1 /** @file
2 This file also installs UEFI PLATFORM_DRIVER_OVERRIDE_PROTOCOL.
3
4 The main code offers a UI interface in device manager to let user configure
5 platform override protocol to override the default algorithm for matching
6 drivers to controllers.
7
8 The main flow:
9 1. It dynamicly locate all controller device path.
10 2. It dynamicly locate all drivers which support binding protocol.
11 3. It export and dynamicly update two menu to let user select the
12 mapping between drivers to controllers.
13 4. It save all the mapping info in NV variables which will be consumed
14 by platform override protocol driver to publish the platform override protocol.
15
16 Copyright (c) 2007 - 2009, Intel Corporation
17 All rights reserved. This program and the accompanying materials
18 are licensed and made available under the terms and conditions of the BSD License
19 which accompanies this distribution. The full text of the license may be found at
20 http://opensource.org/licenses/bsd-license.php
21
22 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
23 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
24
25 **/
26
27 #include "InternalPlatDriOverrideDxe.h"
28 #include "PlatOverMngr.h"
29
30 #define EFI_CALLBACK_INFO_SIGNATURE SIGNATURE_32 ('C', 'l', 'b', 'k')
31 #define EFI_CALLBACK_INFO_FROM_THIS(a) CR (a, EFI_CALLBACK_INFO, ConfigAccess, EFI_CALLBACK_INFO_SIGNATURE)
32
33 typedef struct {
34 UINTN Signature;
35 EFI_HANDLE DriverHandle;
36 EFI_HII_HANDLE RegisteredHandle;
37 PLAT_OVER_MNGR_DATA FakeNvData;
38 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
39 EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess;
40 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL PlatformDriverOverride;
41 } EFI_CALLBACK_INFO;
42
43 #pragma pack(1)
44
45 ///
46 /// HII specific Vendor Device Path definition.
47 ///
48 typedef struct {
49 VENDOR_DEVICE_PATH VendorDevicePath;
50 EFI_DEVICE_PATH_PROTOCOL End;
51 } HII_VENDOR_DEVICE_PATH;
52
53 #pragma pack()
54
55 //
56 // uni string and Vfr Binary data.
57 //
58 extern UINT8 VfrBin[];
59 extern UINT8 PlatDriOverrideDxeStrings[];
60
61 //
62 // module global data
63 //
64 EFI_GUID mPlatformOverridesManagerGuid = PLAT_OVER_MNGR_GUID;
65 CHAR16 mVariableName[] = L"Data";
66 LIST_ENTRY mMappingDataBase = INITIALIZE_LIST_HEAD_VARIABLE (mMappingDataBase);
67 BOOLEAN mEnvironmentVariableRead = FALSE;
68 EFI_HANDLE mCallerImageHandle = NULL;
69
70 EFI_HANDLE *mDevicePathHandleBuffer;
71 EFI_HANDLE *mDriverImageHandleBuffer;
72
73 INTN mSelectedCtrIndex;
74 EFI_STRING_ID mControllerToken[MAX_CHOICE_NUM];
75 UINTN mDriverImageHandleCount;
76 EFI_STRING_ID mDriverImageToken[MAX_CHOICE_NUM];
77 EFI_STRING_ID mDriverImageFilePathToken[MAX_CHOICE_NUM];
78 EFI_LOADED_IMAGE_PROTOCOL *mDriverImageProtocol[MAX_CHOICE_NUM];
79 EFI_DEVICE_PATH_PROTOCOL *mControllerDevicePathProtocol[MAX_CHOICE_NUM];
80 UINTN mSelectedDriverImageNum;
81 UINTN mLastSavedDriverImageNum;
82 UINT16 mCurrentPage;
83 EFI_CALLBACK_INFO *mCallbackInfo;
84
85 HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath = {
86 {
87 {
88 HARDWARE_DEVICE_PATH,
89 HW_VENDOR_DP,
90 {
91 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
92 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
93 }
94 },
95 EFI_CALLER_ID_GUID
96 },
97 {
98 END_DEVICE_PATH_TYPE,
99 END_ENTIRE_DEVICE_PATH_SUBTYPE,
100 {
101 (UINT8) (END_DEVICE_PATH_LENGTH),
102 (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
103 }
104 }
105 };
106
107 /**
108 Converting a given device to an unicode string.
109
110 This function will dependent on gEfiDevicePathToTextProtocolGuid, if protocol
111 does not installed, then return unknown device path L"?" directly.
112
113 @param DevPath Given device path instance
114
115 @return Converted string from given device path.
116 @retval L"?" Can not locate gEfiDevicePathToTextProtocolGuid protocol for converting.
117 **/
118 CHAR16 *
119 DevicePathToStr (
120 IN EFI_DEVICE_PATH_PROTOCOL *DevPath
121 )
122 {
123 EFI_STATUS Status;
124 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevPathToText;
125 CHAR16 *ToText;
126
127 if (DevPath == NULL) {
128 return L"";
129 }
130
131 Status = gBS->LocateProtocol (
132 &gEfiDevicePathToTextProtocolGuid,
133 NULL,
134 (VOID **) &DevPathToText
135 );
136 if (!EFI_ERROR (Status)) {
137 ToText = DevPathToText->ConvertDevicePathToText (
138 DevPath,
139 FALSE,
140 TRUE
141 );
142 ASSERT (ToText != NULL);
143 return ToText;
144 }
145
146 return L"?";
147 }
148
149 /**
150 Worker function to get the driver name by ComponentName or ComponentName2 protocol
151 according to the driver binding handle.
152
153 @param DriverBindingHandle The Handle of DriverBinding.
154 @param ProtocolGuid The pointer to Component Name (2) protocol GUID.
155 @param VariableName The name of the RFC 4646 or ISO 639-2 language variable.
156
157 @retval !NULL Pointer into the image name if the image name is found,
158 @retval NULL Pointer to NULL if the image name is not found.
159
160 **/
161 CHAR16 *
162 GetComponentNameWorker (
163 IN EFI_HANDLE DriverBindingHandle,
164 IN EFI_GUID *ProtocolGuid,
165 IN CONST CHAR16 *VariableName
166 )
167 {
168 EFI_STATUS Status;
169 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
170 CHAR16 *DriverName;
171 CHAR8 *Language;
172 CHAR8 *BestLanguage;
173
174 Status = gBS->OpenProtocol (
175 DriverBindingHandle,
176 ProtocolGuid,
177 (VOID *) &ComponentName,
178 NULL,
179 NULL,
180 EFI_OPEN_PROTOCOL_GET_PROTOCOL
181 );
182 if (EFI_ERROR (Status)) {
183 return NULL;
184 }
185
186 //
187 // Find the best matching language.
188 //
189 Language = GetEfiGlobalVariable (VariableName);
190 BestLanguage = GetBestLanguage (
191 ComponentName->SupportedLanguages,
192 (BOOLEAN) (ProtocolGuid == &gEfiComponentNameProtocolGuid),
193 Language,
194 NULL
195 );
196
197 DriverName = NULL;
198 if (BestLanguage != NULL) {
199 ComponentName->GetDriverName (
200 ComponentName,
201 BestLanguage,
202 &DriverName
203 );
204 FreePool (BestLanguage);
205 }
206
207 if (Language != NULL) {
208 FreePool (Language);
209 }
210
211 return DriverName;
212 }
213
214
215 /**
216 Get the driver name by ComponentName or ComponentName2 protocol
217 according to the driver binding handle
218
219 @param DriverBindingHandle The Handle of DriverBinding.
220
221 @retval !NULL Pointer into the image name if the image name is found,
222 @retval NULL Pointer to NULL if the image name is not found.
223
224 **/
225 CHAR16 *
226 GetComponentName (
227 IN EFI_HANDLE DriverBindingHandle
228 )
229 {
230 CHAR16 *DriverName;
231
232 //
233 // Try RFC 4646 Component Name 2 protocol first.
234 //
235 DriverName = GetComponentNameWorker (DriverBindingHandle, &gEfiComponentName2ProtocolGuid, L"PlatformLang");
236 if (DriverName == NULL) {
237 //
238 // If we can not get driver name from Component Name 2 protocol, we can try ISO 639-2 Component Name protocol.
239 //
240 DriverName = GetComponentNameWorker (DriverBindingHandle, &gEfiComponentNameProtocolGuid, L"Lang");
241 }
242
243 return DriverName;
244 }
245
246 /**
247 Get the image name from EFI UI section.
248 Get FV protocol by its loaded image protocol to abstract EFI UI section.
249
250 @param Image Pointer to the loaded image protocol
251
252 @retval !NULL Pointer to the image name if the image name is found,
253 @retval NULL NULL if the image name is not found.
254
255 **/
256 CHAR16 *
257 GetImageName (
258 IN EFI_LOADED_IMAGE_PROTOCOL *Image
259 )
260 {
261 EFI_STATUS Status;
262 EFI_DEVICE_PATH_PROTOCOL *DevPathNode;
263 EFI_DEVICE_PATH_PROTOCOL *AlignedDevPathNode;
264 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFilePath;
265 VOID *Buffer;
266 UINTN BufferSize;
267 UINT32 AuthenticationStatus;
268 EFI_GUID *NameGuid;
269 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv2;
270
271 Fv2 = NULL;
272 Buffer = NULL;
273 BufferSize = 0;
274
275 if (Image->FilePath == NULL) {
276 return NULL;
277 }
278 DevPathNode = Image->FilePath;
279
280 while (!IsDevicePathEnd (DevPathNode)) {
281 //
282 // Make sure device path node is aligned when accessing it's FV Name Guid field.
283 //
284 AlignedDevPathNode = AllocateCopyPool (DevicePathNodeLength(DevPathNode), DevPathNode);
285
286 //
287 // Find the Fv File path
288 //
289 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)AlignedDevPathNode);
290 if (NameGuid != NULL) {
291 FvFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) AlignedDevPathNode;
292 Status = gBS->HandleProtocol (
293 Image->DeviceHandle,
294 &gEfiFirmwareVolume2ProtocolGuid,
295 (VOID **) &Fv2
296 );
297 //
298 // Locate Image EFI UI section to get the image name.
299 //
300 if (!EFI_ERROR (Status)) {
301 Status = Fv2->ReadSection (
302 Fv2,
303 &FvFilePath->FvFileName,
304 EFI_SECTION_USER_INTERFACE,
305 0,
306 &Buffer,
307 &BufferSize,
308 &AuthenticationStatus
309 );
310 if (!EFI_ERROR (Status)) {
311 FreePool (AlignedDevPathNode);
312 break;
313 }
314 Buffer = NULL;
315 }
316 }
317
318 FreePool (AlignedDevPathNode);
319
320 //
321 // Next device path node
322 //
323 DevPathNode = NextDevicePathNode (DevPathNode);
324 }
325
326 return Buffer;
327 }
328
329 /**
330 Prepare the first page to let user select the device controller which need to
331 add mapping drivers if user select 'Refresh' in first page.
332 During first page, user will see all currnet controller device path in system,
333 select any device path will go to second page to select its overrides drivers.
334
335 @param Private Pointer to EFI_CALLBACK_INFO.
336 @param KeyValue The callback key value of device controller item in first page.
337 @param FakeNvData Pointer to PLAT_OVER_MNGR_DATA.
338
339 @retval EFI_SUCCESS Always returned.
340
341 **/
342 EFI_STATUS
343 UpdateDeviceSelectPage (
344 IN EFI_CALLBACK_INFO *Private,
345 IN UINT16 KeyValue,
346 IN PLAT_OVER_MNGR_DATA *FakeNvData
347 )
348 {
349 EFI_STATUS Status;
350 UINTN Index;
351 UINTN DevicePathHandleCount;
352 CHAR16 *NewString;
353 EFI_STRING_ID NewStringToken;
354 CHAR16 *ControllerName;
355 EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath;
356 EFI_PCI_IO_PROTOCOL *PciIo;
357 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL *BusSpecificDriverOverride;
358 UINTN Len;
359 VOID *StartOpCodeHandle;
360 VOID *EndOpCodeHandle;
361 EFI_IFR_GUID_LABEL *StartLabel;
362 EFI_IFR_GUID_LABEL *EndLabel;
363
364 //
365 // Set current page form ID.
366 //
367 mCurrentPage = FORM_ID_DEVICE;
368
369 //
370 // Initial the mapping database in memory
371 //
372 FreeMappingDatabase (&mMappingDataBase);
373 InitOverridesMapping (&mMappingDataBase);
374
375 //
376 // Init OpCode Handle
377 //
378 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
379 ASSERT (StartOpCodeHandle != NULL);
380
381 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
382 ASSERT (EndOpCodeHandle != NULL);
383
384 //
385 // Create Hii Extend Label OpCode as the start opcode
386 //
387 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
388 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
389 StartLabel->Number = FORM_ID_DEVICE;
390
391 //
392 // Create Hii Extend Label OpCode as the end opcode
393 //
394 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
395 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
396 EndLabel->Number = LABEL_END;
397
398 //
399 // Clear first page form
400 //
401 HiiUpdateForm (
402 Private->RegisteredHandle,
403 &mPlatformOverridesManagerGuid,
404 FORM_ID_DEVICE,
405 StartOpCodeHandle, // Label FORM_ID_DEVICE
406 EndOpCodeHandle // LABEL_END
407 );
408
409 //
410 // When user enter the page at first time, the 'first refresh' string is given to notify user to refresh all the drivers,
411 // then the 'first refresh' string will be replaced by the 'refresh' string, and the two strings content are same after the replacement
412 //
413 NewStringToken = STRING_TOKEN (STR_FIRST_REFRESH);
414 NewString = HiiGetString (Private->RegisteredHandle, STRING_TOKEN (STR_REFRESH), NULL);
415 ASSERT (NewString != NULL);
416 if (HiiSetString (Private->RegisteredHandle, NewStringToken, NewString, NULL) == 0) {
417 ASSERT (FALSE);
418 }
419 FreePool (NewString);
420
421 NewStringToken = STRING_TOKEN (STR_FIRST_REFRESH_HELP);
422 NewString = HiiGetString (Private->RegisteredHandle, STRING_TOKEN (STR_REFRESH_HELP), NULL);
423 ASSERT (NewString != NULL);
424 if (HiiSetString (Private->RegisteredHandle, NewStringToken, NewString, NULL) == 0) {
425 ASSERT (FALSE);
426 }
427 FreePool (NewString);
428
429 //
430 // created needed controller device item in first page
431 //
432 DevicePathHandleCount = 0;
433 Status = gBS->LocateHandleBuffer (
434 ByProtocol,
435 &gEfiDevicePathProtocolGuid,
436 NULL,
437 &DevicePathHandleCount,
438 &mDevicePathHandleBuffer
439 );
440 if (EFI_ERROR (Status) || (DevicePathHandleCount == 0)) {
441 return EFI_SUCCESS;
442 }
443
444 for (Index = 0; Index < DevicePathHandleCount; Index++) {
445 if (FakeNvData->PciDeviceFilter == 0x01) {
446 //
447 // Only care PCI device which contain efi driver in its option rom.
448 //
449
450 //
451 // Check whether it is a pci device
452 //
453 ControllerDevicePath = NULL;
454 Status = gBS->OpenProtocol (
455 mDevicePathHandleBuffer[Index],
456 &gEfiPciIoProtocolGuid,
457 (VOID **) &PciIo,
458 NULL,
459 NULL,
460 EFI_OPEN_PROTOCOL_GET_PROTOCOL
461 );
462 if (EFI_ERROR (Status)) {
463 continue;
464 }
465 //
466 // Check whether it contain efi driver in its option rom
467 //
468 Status = gBS->HandleProtocol(
469 mDevicePathHandleBuffer[Index],
470 &gEfiBusSpecificDriverOverrideProtocolGuid,
471 (VOID **) &BusSpecificDriverOverride
472 );
473 if (EFI_ERROR (Status) || BusSpecificDriverOverride == NULL) {
474 continue;
475 }
476 }
477
478 ControllerDevicePath = NULL;
479 Status = gBS->OpenProtocol (
480 mDevicePathHandleBuffer[Index],
481 &gEfiDevicePathProtocolGuid,
482 (VOID **) &ControllerDevicePath,
483 NULL,
484 NULL,
485 EFI_OPEN_PROTOCOL_GET_PROTOCOL
486 );
487 ASSERT_EFI_ERROR (Status);
488 //
489 // Save the device path protocol interface
490 //
491 mControllerDevicePathProtocol[Index] = ControllerDevicePath;
492
493 //
494 // Get the driver name
495 //
496 ControllerName = DevicePathToStr (ControllerDevicePath);
497
498 //
499 // Export the driver name string and create item in set options page
500 //
501 Len = StrSize (ControllerName);
502 NewString = AllocateZeroPool (Len + StrSize (L"--"));
503 ASSERT (NewString != NULL);
504 if (EFI_ERROR (CheckMapping (ControllerDevicePath,NULL, &mMappingDataBase, NULL, NULL))) {
505 StrCat (NewString, L"--");
506 } else {
507 StrCat (NewString, L"**");
508 }
509 StrCat (NewString, ControllerName);
510
511 NewStringToken = HiiSetString (Private->RegisteredHandle, mControllerToken[Index], NewString, NULL);
512 ASSERT (NewStringToken != 0);
513 FreePool (NewString);
514 //
515 // Save the device path string toke for next access use
516 //
517 mControllerToken[Index] = NewStringToken;
518
519 HiiCreateGotoOpCode (
520 StartOpCodeHandle,
521 FORM_ID_DRIVER,
522 NewStringToken,
523 STRING_TOKEN (STR_GOTO_HELP_DRIVER),
524 EFI_IFR_FLAG_CALLBACK,
525 (UINT16) (Index + KEY_VALUE_DEVICE_OFFSET)
526 );
527 }
528
529 //
530 // Update first page form
531 //
532 HiiUpdateForm (
533 Private->RegisteredHandle,
534 &mPlatformOverridesManagerGuid,
535 FORM_ID_DEVICE,
536 StartOpCodeHandle, // Label FORM_ID_DEVICE
537 EndOpCodeHandle // LABEL_END
538 );
539
540 HiiFreeOpCodeHandle (StartOpCodeHandle);
541 HiiFreeOpCodeHandle (EndOpCodeHandle);
542
543 return EFI_SUCCESS;
544 }
545
546 /**
547 Get the first Driver Binding handle which has the specific image handle.
548
549 @param ImageHandle The Image handle
550
551 @return Handle to Driver binding
552 @retval NULL The paramter is not valid or the driver binding handle is not found.
553
554 **/
555 EFI_HANDLE
556 GetDriverBindingHandleFromImageHandle (
557 IN EFI_HANDLE ImageHandle
558 )
559 {
560 EFI_STATUS Status;
561 UINTN Index;
562 UINTN DriverBindingHandleCount;
563 EFI_HANDLE *DriverBindingHandleBuffer;
564 EFI_DRIVER_BINDING_PROTOCOL *DriverBindingInterface;
565 EFI_HANDLE DriverBindingHandle;
566
567 DriverBindingHandle = NULL;
568
569 if (ImageHandle == NULL) {
570 return NULL;
571 }
572 //
573 // Get all drivers which support driver binding protocol
574 //
575 DriverBindingHandleCount = 0;
576 Status = gBS->LocateHandleBuffer (
577 ByProtocol,
578 &gEfiDriverBindingProtocolGuid,
579 NULL,
580 &DriverBindingHandleCount,
581 &DriverBindingHandleBuffer
582 );
583 if (EFI_ERROR (Status) || (DriverBindingHandleCount == 0)) {
584 return NULL;
585 }
586
587 //
588 // Get the first Driver Binding handle which has the specific image handle.
589 //
590 for (Index = 0; Index < DriverBindingHandleCount; Index++) {
591 DriverBindingInterface = NULL;
592 Status = gBS->OpenProtocol (
593 DriverBindingHandleBuffer[Index],
594 &gEfiDriverBindingProtocolGuid,
595 (VOID **) &DriverBindingInterface,
596 NULL,
597 NULL,
598 EFI_OPEN_PROTOCOL_GET_PROTOCOL
599 );
600 if (EFI_ERROR (Status)) {
601 continue;
602 }
603
604 if (DriverBindingInterface->ImageHandle == ImageHandle) {
605 DriverBindingHandle = DriverBindingHandleBuffer[Index];
606 break;
607 }
608 }
609
610 FreePool (DriverBindingHandleBuffer);
611 return DriverBindingHandle;
612 }
613
614 /**
615 Prepare to let user select the drivers which need mapping with the device controller
616 selected in first page.
617
618 @param Private Pointer to EFI_CALLBACK_INFO.
619 @param KeyValue The callback key value of device controller item in first page.
620 KeyValue is larger than or equal to KEY_VALUE_DEVICE_OFFSET.
621 @param FakeNvData Pointer to PLAT_OVER_MNGR_DATA.
622
623 @retval EFI_SUCCESS Always returned.
624
625 **/
626 EFI_STATUS
627 UpdateBindingDriverSelectPage (
628 IN EFI_CALLBACK_INFO *Private,
629 IN UINT16 KeyValue,
630 IN PLAT_OVER_MNGR_DATA *FakeNvData
631 )
632 {
633 EFI_STATUS Status;
634 UINTN Index;
635 CHAR16 *NewString;
636 EFI_STRING_ID NewStringToken;
637 EFI_STRING_ID NewStringHelpToken;
638 UINTN DriverImageHandleCount;
639 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
640 CHAR16 *DriverName;
641 BOOLEAN FreeDriverName;
642 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
643 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL *BusSpecificDriverOverride;
644 EFI_HANDLE DriverBindingHandle;
645 VOID *StartOpCodeHandle;
646 VOID *EndOpCodeHandle;
647 EFI_IFR_GUID_LABEL *StartLabel;
648 EFI_IFR_GUID_LABEL *EndLabel;
649
650 //
651 // If user select a controller item in the first page the following code will be run.
652 // During second page, user will see all currnet driver bind protocol driver, the driver name and its device path will be shown
653 //
654 //First acquire the list of Loaded Image Protocols, and then when want the name of the driver, look up all the Driver Binding Protocols
655 // and find the first one whose ImageHandle field matches the image handle of the Loaded Image Protocol.
656 // then use the Component Name Protocol on the same handle as the first matching Driver Binding Protocol to look up the name of the driver.
657 //
658
659 mCurrentPage = FORM_ID_DRIVER;
660 //
661 // Switch the item callback key value to its NO. in mDevicePathHandleBuffer
662 //
663 mSelectedCtrIndex = KeyValue - KEY_VALUE_DEVICE_OFFSET;
664 ASSERT (mSelectedCtrIndex < MAX_CHOICE_NUM);
665
666 mLastSavedDriverImageNum = 0;
667
668 //
669 // Init OpCode Handle
670 //
671 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
672 ASSERT (StartOpCodeHandle != NULL);
673
674 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
675 ASSERT (EndOpCodeHandle != NULL);
676
677 //
678 // Create Hii Extend Label OpCode as the start opcode
679 //
680 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
681 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
682 StartLabel->Number = FORM_ID_DRIVER;
683
684 //
685 // Create Hii Extend Label OpCode as the end opcode
686 //
687 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
688 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
689 EndLabel->Number = LABEL_END;
690
691 //
692 // Clear second page form
693 //
694 HiiUpdateForm (
695 Private->RegisteredHandle,
696 &mPlatformOverridesManagerGuid,
697 FORM_ID_DRIVER,
698 StartOpCodeHandle,
699 EndOpCodeHandle
700 );
701
702 //
703 // Show all driver which support loaded image protocol in second page
704 //
705 DriverImageHandleCount = 0;
706 Status = gBS->LocateHandleBuffer (
707 ByProtocol,
708 &gEfiLoadedImageProtocolGuid,
709 NULL,
710 &DriverImageHandleCount,
711 &mDriverImageHandleBuffer
712 );
713 if (EFI_ERROR (Status) || (DriverImageHandleCount == 0)) {
714 return EFI_NOT_FOUND;
715 }
716
717 mDriverImageHandleCount = DriverImageHandleCount;
718 for (Index = 0; Index < DriverImageHandleCount; Index++) {
719 //
720 // Step1: Get the driver image total file path for help string and the driver name.
721 //
722
723 //
724 // Find driver's Loaded Image protocol
725 //
726 LoadedImage =NULL;
727
728 Status = gBS->OpenProtocol (
729 mDriverImageHandleBuffer[Index],
730 &gEfiLoadedImageProtocolGuid,
731 (VOID **) &LoadedImage,
732 NULL,
733 NULL,
734 EFI_OPEN_PROTOCOL_GET_PROTOCOL
735 );
736 if (EFI_ERROR (Status)) {
737 FakeNvData->DriSelection[Index] = 0x00;
738 continue;
739 }
740 mDriverImageProtocol[Index] = LoadedImage;
741 //
742 // Find its related driver binding protocol
743 //
744 DriverBindingHandle = GetDriverBindingHandleFromImageHandle (mDriverImageHandleBuffer[Index]);
745 if (DriverBindingHandle == NULL) {
746 FakeNvData->DriSelection[Index] = 0x00;
747 continue;
748 }
749
750 //
751 // Get the EFI Loaded Image Device Path Protocol
752 //
753 LoadedImageDevicePath = NULL;
754 Status = gBS->HandleProtocol (
755 mDriverImageHandleBuffer[Index],
756 &gEfiLoadedImageDevicePathProtocolGuid,
757 (VOID **) &LoadedImageDevicePath
758 );
759 if (LoadedImageDevicePath == NULL) {
760 FakeNvData->DriSelection[Index] = 0x00;
761 continue;
762 }
763
764 if (FakeNvData->PciDeviceFilter == 0x01) {
765 //
766 // only care the driver which is in a Pci device option rom,
767 // and the driver's LoadedImage->DeviceHandle must point to a pci device which has efi option rom
768 //
769 if (!EFI_ERROR (Status)) {
770 Status = gBS->HandleProtocol(
771 LoadedImage->DeviceHandle,
772 &gEfiBusSpecificDriverOverrideProtocolGuid,
773 (VOID **) &BusSpecificDriverOverride
774 );
775 if (EFI_ERROR (Status) || BusSpecificDriverOverride == NULL) {
776 FakeNvData->DriSelection[Index] = 0x00;
777 continue;
778 }
779 } else {
780 FakeNvData->DriSelection[Index] = 0x00;
781 continue;
782 }
783 }
784
785 //
786 // For driver name, try to get its component name, if fail, get its image name,
787 // if also fail, give a default name.
788 //
789 FreeDriverName = FALSE;
790 DriverName = GetComponentName (DriverBindingHandle);
791 if (DriverName == NULL) {
792 //
793 // get its image name
794 //
795 DriverName = GetImageName (LoadedImage);
796 }
797 if (DriverName == NULL) {
798 //
799 // give a default name
800 //
801 DriverName = HiiGetString (Private->RegisteredHandle, STRING_TOKEN (STR_DRIVER_DEFAULT_NAME), NULL);
802 ASSERT (DriverName != NULL);
803 FreeDriverName = TRUE; // the DriverName string need to free pool
804 }
805
806
807 //
808 // Step2 Export the driver name string and create check box item in second page
809 //
810
811 //
812 // First create the driver image name
813 //
814 NewString = AllocateZeroPool (StrSize (DriverName));
815 ASSERT (NewString != NULL);
816 if (EFI_ERROR (CheckMapping (mControllerDevicePathProtocol[mSelectedCtrIndex], LoadedImageDevicePath, &mMappingDataBase, NULL, NULL))) {
817 FakeNvData->DriSelection[Index] = 0x00;
818 } else {
819 FakeNvData->DriSelection[Index] = 0x01;
820 mLastSavedDriverImageNum++;
821 }
822 StrCat (NewString, DriverName);
823 NewStringToken = HiiSetString (Private->RegisteredHandle, mDriverImageToken[Index], NewString, NULL);
824 ASSERT (NewStringToken != 0);
825 mDriverImageToken[Index] = NewStringToken;
826 FreePool (NewString);
827 if (FreeDriverName) {
828 FreePool (DriverName);
829 }
830
831 //
832 // Second create the driver image device path as item help string
833 //
834 DriverName = DevicePathToStr (LoadedImageDevicePath);
835
836 NewString = AllocateZeroPool (StrSize (DriverName));
837 ASSERT (NewString != NULL);
838 StrCat (NewString, DriverName);
839 NewStringHelpToken = HiiSetString (Private->RegisteredHandle, mDriverImageFilePathToken[Index], NewString, NULL);
840 ASSERT (NewStringHelpToken != 0);
841 mDriverImageFilePathToken[Index] = NewStringHelpToken;
842 FreePool (NewString);
843 FreePool (DriverName);
844
845 HiiCreateCheckBoxOpCode (
846 StartOpCodeHandle,
847 (UINT16) (DRIVER_SELECTION_QUESTION_ID + Index),
848 VARSTORE_ID_PLAT_OVER_MNGR,
849 (UINT16) (DRIVER_SELECTION_VAR_OFFSET + Index),
850 NewStringToken,
851 NewStringHelpToken,
852 0,
853 0,
854 NULL
855 );
856 }
857
858 //
859 // Update second page form
860 //
861 HiiUpdateForm (
862 Private->RegisteredHandle,
863 &mPlatformOverridesManagerGuid,
864 FORM_ID_DRIVER,
865 StartOpCodeHandle, // Label FORM_ID_DRIVER
866 EndOpCodeHandle // LABEL_END
867 );
868
869 HiiFreeOpCodeHandle (StartOpCodeHandle);
870 HiiFreeOpCodeHandle (EndOpCodeHandle);
871 return EFI_SUCCESS;
872 }
873
874 /**
875 Prepare to let user select the priority order of the drivers which are
876 selected in second page.
877
878 @param Private Pointer to EFI_CALLBACK_INFO.
879 @param KeyValue The callback key value of device controller item in first page.
880 @param FakeNvData Pointer to PLAT_OVER_MNGR_DATA.
881
882 @retval EFI_SUCCESS Always returned.
883
884 **/
885 EFI_STATUS
886 UpdatePrioritySelectPage (
887 IN EFI_CALLBACK_INFO *Private,
888 IN UINT16 KeyValue,
889 IN PLAT_OVER_MNGR_DATA *FakeNvData
890 )
891 {
892 UINTN Index;
893 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
894 UINTN SelectedDriverImageNum;
895 UINT32 DriverImageNO;
896 UINTN MinNO;
897 UINTN Index1;
898 UINTN TempNO[100];
899 UINTN OrderNO[100];
900 VOID *StartOpCodeHandle;
901 VOID *EndOpCodeHandle;
902 VOID *OptionsOpCodeHandle;
903 EFI_IFR_GUID_LABEL *StartLabel;
904 EFI_IFR_GUID_LABEL *EndLabel;
905
906 //
907 // Following code will be run if user select 'order ... priority' item in second page
908 // Prepare third page. In third page, user will order the drivers priority which are selected in second page
909 //
910 mCurrentPage = FORM_ID_ORDER;
911
912 //
913 // Init OpCode Handle
914 //
915 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
916 ASSERT (StartOpCodeHandle != NULL);
917
918 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
919 ASSERT (EndOpCodeHandle != NULL);
920
921 //
922 // Create Hii Extend Label OpCode as the start opcode
923 //
924 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
925 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
926 StartLabel->Number = FORM_ID_ORDER;
927
928 //
929 // Create Hii Extend Label OpCode as the end opcode
930 //
931 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
932 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
933 EndLabel->Number = LABEL_END;
934
935 //
936 // Clear third page form
937 //
938 HiiUpdateForm (
939 Private->RegisteredHandle,
940 &mPlatformOverridesManagerGuid,
941 FORM_ID_ORDER,
942 StartOpCodeHandle,
943 EndOpCodeHandle
944 );
945
946 //
947 // Check how many drivers have been selected
948 //
949 SelectedDriverImageNum = 0;
950 for (Index = 0; Index < mDriverImageHandleCount; Index++) {
951 if (FakeNvData->DriSelection[Index] != 0) {
952 SelectedDriverImageNum ++;
953 }
954 }
955
956 mSelectedDriverImageNum = SelectedDriverImageNum;
957 if (SelectedDriverImageNum == 0) {
958 return EFI_SUCCESS;
959 }
960
961 OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
962 ASSERT (OptionsOpCodeHandle != NULL);
963
964 //
965 // Create order list for those selected drivers
966 //
967 SelectedDriverImageNum = 0;
968 for (Index = 0; Index < mDriverImageHandleCount; Index++) {
969 if (FakeNvData->DriSelection[Index] != 0) {
970 //
971 // Use the NO. in driver binding buffer as value, will use it later
972 //
973 HiiCreateOneOfOptionOpCode (
974 OptionsOpCodeHandle,
975 mDriverImageToken[Index],
976 0,
977 EFI_IFR_NUMERIC_SIZE_1,
978 Index + 1
979 );
980
981 //
982 // Get the EFI Loaded Image Device Path Protocol
983 //
984 LoadedImageDevicePath = NULL;
985 gBS->HandleProtocol (
986 mDriverImageHandleBuffer[Index],
987 &gEfiLoadedImageDevicePathProtocolGuid,
988 (VOID **) &LoadedImageDevicePath
989 );
990 ASSERT (LoadedImageDevicePath != NULL);
991
992 //
993 // Check the driver DriverImage's order number in mapping database
994 //
995 DriverImageNO = 0;
996 CheckMapping (
997 mControllerDevicePathProtocol[mSelectedCtrIndex],
998 LoadedImageDevicePath,
999 &mMappingDataBase,
1000 NULL,
1001 &DriverImageNO
1002 );
1003 if (DriverImageNO == 0) {
1004 DriverImageNO = (UINT32) mLastSavedDriverImageNum + 1;
1005 mLastSavedDriverImageNum++;
1006 }
1007 TempNO[SelectedDriverImageNum] = DriverImageNO;
1008 OrderNO[SelectedDriverImageNum] = Index + 1;
1009 SelectedDriverImageNum ++;
1010 }
1011 }
1012
1013 ASSERT (SelectedDriverImageNum == mSelectedDriverImageNum);
1014 //
1015 // NvRamMap Must be clear firstly
1016 //
1017 ZeroMem (FakeNvData->DriOrder, sizeof (FakeNvData->DriOrder));
1018
1019 //
1020 // Order the selected drivers according to the info already in mapping database
1021 // the less order number in mapping database the less order number in NvRamMap
1022 //
1023 for (Index=0; Index < SelectedDriverImageNum; Index++) {
1024 //
1025 // Find the minimal order number in TempNO array, its index in TempNO is same as IfrOptionList array
1026 //
1027 MinNO = 0;
1028 for (Index1=0; Index1 < SelectedDriverImageNum; Index1++) {
1029 if (TempNO[Index1] < TempNO[MinNO]) {
1030 MinNO = Index1;
1031 }
1032 }
1033 //
1034 // the IfrOptionList[MinNO].Value = the driver NO. in driver binding buffer
1035 //
1036 FakeNvData->DriOrder[Index] = (UINT8) OrderNO[MinNO];
1037 TempNO[MinNO] = MAX_CHOICE_NUM + 1;
1038 }
1039
1040 //
1041 // Create Order List OpCode
1042 //
1043 HiiCreateOrderedListOpCode (
1044 StartOpCodeHandle,
1045 (UINT16) DRIVER_ORDER_QUESTION_ID,
1046 VARSTORE_ID_PLAT_OVER_MNGR,
1047 (UINT16) DRIVER_ORDER_VAR_OFFSET,
1048 mControllerToken[mSelectedCtrIndex],
1049 mControllerToken[mSelectedCtrIndex],
1050 EFI_IFR_FLAG_RESET_REQUIRED,
1051 0,
1052 EFI_IFR_NUMERIC_SIZE_1,
1053 (UINT8) MAX_CHOICE_NUM,
1054 OptionsOpCodeHandle,
1055 NULL
1056 );
1057
1058 //
1059 // Update third page form
1060 //
1061 HiiUpdateForm (
1062 Private->RegisteredHandle,
1063 &mPlatformOverridesManagerGuid,
1064 FORM_ID_ORDER,
1065 StartOpCodeHandle, // Label FORM_ID_ORDER
1066 EndOpCodeHandle // LABEL_END
1067 );
1068
1069 HiiFreeOpCodeHandle (StartOpCodeHandle);
1070 HiiFreeOpCodeHandle (EndOpCodeHandle);
1071 HiiFreeOpCodeHandle (OptionsOpCodeHandle);
1072
1073 return EFI_SUCCESS;
1074 }
1075
1076 /**
1077 Save the save the mapping database to NV variable.
1078
1079 @param Private Pointer to EFI_CALLBACK_INFO.
1080 @param KeyValue The callback key value of device controller item in first page.
1081 @param FakeNvData Pointer to PLAT_OVER_MNGR_DATA.
1082
1083 @retval EFI_SUCCESS Always returned.
1084
1085 **/
1086 EFI_STATUS
1087 CommintChanges (
1088 IN EFI_CALLBACK_INFO *Private,
1089 IN UINT16 KeyValue,
1090 IN PLAT_OVER_MNGR_DATA *FakeNvData
1091 )
1092 {
1093 EFI_STATUS Status;
1094 UINTN Index;
1095 UINTN SelectedDriverImageNum;
1096 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
1097 //
1098 // Following code will be run if user select 'commint changes' in third page
1099 // user enter 'Commit Changes' to save the mapping database
1100 //
1101 DeleteDriverImage (mControllerDevicePathProtocol[mSelectedCtrIndex], NULL, &mMappingDataBase);
1102 for (SelectedDriverImageNum = 0; SelectedDriverImageNum < mSelectedDriverImageNum; SelectedDriverImageNum++) {
1103 //
1104 // DriOrder[SelectedDriverImageNum] = the driver NO. in driver binding buffer
1105 //
1106 Index = FakeNvData->DriOrder[SelectedDriverImageNum] - 1;
1107
1108 //
1109 // Get the EFI Loaded Image Device Path Protocol
1110 //
1111 LoadedImageDevicePath = NULL;
1112 Status = gBS->HandleProtocol (
1113 mDriverImageHandleBuffer[Index],
1114 &gEfiLoadedImageDevicePathProtocolGuid,
1115 (VOID **) &LoadedImageDevicePath
1116 );
1117 ASSERT (LoadedImageDevicePath != NULL);
1118
1119 InsertDriverImage (
1120 mControllerDevicePathProtocol[mSelectedCtrIndex],
1121 LoadedImageDevicePath,
1122 &mMappingDataBase,
1123 (UINT32)SelectedDriverImageNum + 1
1124 );
1125 }
1126 Status = SaveOverridesMapping (&mMappingDataBase);
1127
1128 return Status;
1129 }
1130
1131 /**
1132 This function allows a caller to extract the current configuration for one
1133 or more named elements from the target driver.
1134
1135 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1136 @param Request A null-terminated Unicode string in <ConfigRequest> format.
1137 @param Progress On return, points to a character in the Request string.
1138 Points to the string's null terminator if request was successful.
1139 Points to the most recent '&' before the first failing name/value
1140 pair (or the beginning of the string if the failure is in the
1141 first name/value pair) if the request was not successful.
1142 @param Results A null-terminated Unicode string in <ConfigAltResp> format which
1143 has all values filled in for the names in the Request string.
1144 String to be allocated by the called function.
1145
1146 @retval EFI_SUCCESS The Results is filled with the requested values.
1147 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
1148 @retval EFI_INVALID_PARAMETER Request is NULL, illegal syntax, or unknown name.
1149 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
1150
1151 **/
1152 EFI_STATUS
1153 EFIAPI
1154 PlatOverMngrExtractConfig (
1155 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1156 IN CONST EFI_STRING Request,
1157 OUT EFI_STRING *Progress,
1158 OUT EFI_STRING *Results
1159 )
1160 {
1161 EFI_STATUS Status;
1162 EFI_CALLBACK_INFO *Private;
1163 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
1164
1165 if (Request == NULL) {
1166 return EFI_NOT_FOUND;
1167 }
1168
1169 Private = EFI_CALLBACK_INFO_FROM_THIS (This);
1170 HiiConfigRouting = Private->HiiConfigRouting;
1171
1172 //
1173 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
1174 //
1175 Status = HiiConfigRouting->BlockToConfig (
1176 HiiConfigRouting,
1177 Request,
1178 (UINT8 *) &Private->FakeNvData,
1179 sizeof (PLAT_OVER_MNGR_DATA),
1180 Results,
1181 Progress
1182 );
1183 return Status;
1184 }
1185
1186 /**
1187 This function processes the results of changes in configuration.
1188
1189 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1190 @param Configuration A null-terminated Unicode string in <ConfigRequest> format.
1191 @param Progress A pointer to a string filled in with the offset of the most
1192 recent '&' before the first failing name/value pair (or the
1193 beginning of the string if the failure is in the first
1194 name/value pair) or the terminating NULL if all was successful.
1195
1196 @retval EFI_SUCCESS The Results is processed successfully.
1197 @retval EFI_INVALID_PARAMETER Configuration is NULL.
1198 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
1199
1200 **/
1201 EFI_STATUS
1202 EFIAPI
1203 PlatOverMngrRouteConfig (
1204 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1205 IN CONST EFI_STRING Configuration,
1206 OUT EFI_STRING *Progress
1207 )
1208 {
1209 EFI_CALLBACK_INFO *Private;
1210 UINT16 KeyValue;
1211 PLAT_OVER_MNGR_DATA *FakeNvData;
1212
1213 Private = EFI_CALLBACK_INFO_FROM_THIS (This);
1214 FakeNvData = &Private->FakeNvData;
1215 if (!HiiGetBrowserData (&mPlatformOverridesManagerGuid, mVariableName, sizeof (PLAT_OVER_MNGR_DATA), (UINT8 *) FakeNvData)) {
1216 return EFI_NOT_FOUND;
1217 }
1218
1219 if (mCurrentPage == FORM_ID_DRIVER) {
1220 KeyValue = KEY_VALUE_DRIVER_GOTO_ORDER;
1221 UpdatePrioritySelectPage (Private, KeyValue, FakeNvData);
1222 KeyValue = KEY_VALUE_ORDER_SAVE_AND_EXIT;
1223 CommintChanges (Private, KeyValue, FakeNvData);
1224 //
1225 // Since UpdatePrioritySelectPage will change mCurrentPage,
1226 // should ensure the mCurrentPage still indicate the second page here
1227 //
1228 mCurrentPage = FORM_ID_DRIVER;
1229 }
1230
1231 if (mCurrentPage == FORM_ID_ORDER) {
1232 KeyValue = KEY_VALUE_ORDER_SAVE_AND_EXIT;
1233 CommintChanges (Private, KeyValue, FakeNvData);
1234 }
1235
1236 return EFI_SUCCESS;
1237 }
1238
1239 /**
1240 This is the function that is called to provide results data to the driver. This data
1241 consists of a unique key which is used to identify what data is either being passed back
1242 or being asked for.
1243
1244 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1245 @param Action A null-terminated Unicode string in <ConfigRequest> format.
1246 @param KeyValue A unique Goto OpCode callback value which record user's selection.
1247 0x100 <= KeyValue <0x500 : user select a controller item in the first page;
1248 KeyValue == 0x1234 : user select 'Refresh' in first page, or user select 'Go to Previous Menu' in second page
1249 KeyValue == 0x1235 : user select 'Pci device filter' in first page
1250 KeyValue == 0x1500 : user select 'order ... priority' item in second page
1251 KeyValue == 0x1800 : user select 'commint changes' in third page
1252 KeyValue == 0x2000 : user select 'Go to Previous Menu' in third page
1253 @param Type The type of value for the question.
1254 @param Value A pointer to the data being sent to the original exporting driver.
1255 @param ActionRequest On return, points to the action requested by the callback function.
1256
1257 @retval EFI_SUCCESS Always returned.
1258
1259 **/
1260 EFI_STATUS
1261 EFIAPI
1262 PlatOverMngrCallback (
1263 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1264 IN EFI_BROWSER_ACTION Action,
1265 IN EFI_QUESTION_ID KeyValue,
1266 IN UINT8 Type,
1267 IN EFI_IFR_TYPE_VALUE *Value,
1268 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
1269 )
1270 {
1271 EFI_CALLBACK_INFO *Private;
1272 EFI_STATUS Status;
1273 EFI_STRING_ID NewStringToken;
1274 EFI_INPUT_KEY Key;
1275 PLAT_OVER_MNGR_DATA *FakeNvData;
1276
1277 Private = EFI_CALLBACK_INFO_FROM_THIS (This);
1278 FakeNvData = &Private->FakeNvData;
1279 if (!HiiGetBrowserData (&mPlatformOverridesManagerGuid, mVariableName, sizeof (PLAT_OVER_MNGR_DATA), (UINT8 *) FakeNvData)) {
1280 return EFI_NOT_FOUND;
1281 }
1282
1283 if (KeyValue == KEY_VALUE_DEVICE_REFRESH ||
1284 KeyValue == KEY_VALUE_DEVICE_FILTER ||
1285 KeyValue == KEY_VALUE_DRIVER_GOTO_PREVIOUS
1286 ) {
1287 UpdateDeviceSelectPage (Private, KeyValue, FakeNvData);
1288 //
1289 // Update page title string
1290 //
1291 NewStringToken = STRING_TOKEN (STR_TITLE);
1292 if (HiiSetString (Private->RegisteredHandle, NewStringToken, L"First, Select the controller by device path", NULL) == 0) {
1293 ASSERT (FALSE);
1294 }
1295 }
1296
1297 if (((KeyValue >= KEY_VALUE_DEVICE_OFFSET) && (KeyValue < KEY_VALUE_DEVICE_MAX)) || (KeyValue == KEY_VALUE_ORDER_GOTO_PREVIOUS)) {
1298 if (KeyValue == KEY_VALUE_ORDER_GOTO_PREVIOUS) {
1299 KeyValue = (EFI_QUESTION_ID) (mSelectedCtrIndex + KEY_VALUE_DEVICE_OFFSET);
1300 }
1301 UpdateBindingDriverSelectPage (Private, KeyValue, FakeNvData);
1302 //
1303 // Update page title string
1304 //
1305 NewStringToken = STRING_TOKEN (STR_TITLE);
1306 if (HiiSetString (Private->RegisteredHandle, NewStringToken, L"Second, Select drivers for the previous selected controller", NULL) == 0) {
1307 ASSERT (FALSE);
1308 }
1309 }
1310
1311 if (KeyValue == KEY_VALUE_DRIVER_GOTO_ORDER) {
1312 UpdatePrioritySelectPage (Private, KeyValue, FakeNvData);
1313 //
1314 // Update page title string
1315 //
1316 NewStringToken = STRING_TOKEN (STR_TITLE);
1317 if (HiiSetString (Private->RegisteredHandle, NewStringToken, L"Finally, Set the priority order for the drivers and save them", NULL) == 0) {
1318 ASSERT (FALSE);
1319 }
1320 }
1321
1322 if (KeyValue == KEY_VALUE_ORDER_SAVE_AND_EXIT) {
1323 Status = CommintChanges (Private, KeyValue, FakeNvData);
1324 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1325 if (EFI_ERROR (Status)) {
1326 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Single Override Info too large, Saving Error!", NULL);
1327 return EFI_DEVICE_ERROR;
1328 }
1329 }
1330
1331 if (KeyValue == KEY_VALUE_DEVICE_CLEAR) {
1332 //
1333 // Deletes all environment variable(s) that contain the override mappings info
1334 //
1335 FreeMappingDatabase (&mMappingDataBase);
1336 Status = SaveOverridesMapping (&mMappingDataBase);
1337 UpdateDeviceSelectPage (Private, KeyValue, FakeNvData);
1338 }
1339 //
1340 // Pass changed uncommitted data back to Form Browser
1341 //
1342 HiiSetBrowserData (&mPlatformOverridesManagerGuid, mVariableName, sizeof (PLAT_OVER_MNGR_DATA), (UINT8 *) FakeNvData, NULL);
1343
1344 return EFI_SUCCESS;
1345 }
1346
1347 /**
1348 Retrieves the image handle of the platform override driver for a controller in the system.
1349
1350 @param This A pointer to the
1351 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.
1352 @param ControllerHandle The device handle of the controller to check if a
1353 driver override exists.
1354 @param DriverImageHandle On input, a pointer to the previous driver image
1355 handle returned by GetDriver(). On output, a
1356 pointer to the next driver image handle. Passing
1357 in a NULL, will return the first driver image
1358 handle for ControllerHandle.
1359
1360 @retval EFI_SUCCESS The driver override for ControllerHandle was
1361 returned in DriverImageHandle.
1362 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
1363 found.
1364 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not a
1365 valid handle. DriverImageHandle is not a handle
1366 that was returned on a previous call to
1367 GetDriver().
1368
1369 **/
1370 EFI_STATUS
1371 EFIAPI
1372 GetDriver (
1373 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *This,
1374 IN EFI_HANDLE ControllerHandle,
1375 IN OUT EFI_HANDLE *DriverImageHandle
1376 )
1377 {
1378 EFI_STATUS Status;
1379
1380 //
1381 // Check that ControllerHandle is a valid handle
1382 //
1383 if (ControllerHandle == NULL) {
1384 return EFI_INVALID_PARAMETER;
1385 }
1386
1387 //
1388 // Read the environment variable(s) that contain the override mappings from Controller Device Path to
1389 // a set of Driver Device Paths, and initialize in memory database of the overrides that map Controller
1390 // Device Paths to an ordered set of Driver Device Paths and Driver Handles. This action is only performed
1391 // once and finished in first call.
1392 //
1393 if (!mEnvironmentVariableRead) {
1394 mEnvironmentVariableRead = TRUE;
1395
1396 Status = InitOverridesMapping (&mMappingDataBase);
1397 if (EFI_ERROR (Status)){
1398 DEBUG ((DEBUG_ERROR, "The status to Get Platform Driver Override Variable is %r\n", Status));
1399 InitializeListHead (&mMappingDataBase);
1400 return EFI_NOT_FOUND;
1401 }
1402 }
1403
1404 //
1405 // if the environment variable does not exist, just return not found
1406 //
1407 if (IsListEmpty (&mMappingDataBase)) {
1408 return EFI_NOT_FOUND;
1409 }
1410
1411 return GetDriverFromMapping (
1412 ControllerHandle,
1413 DriverImageHandle,
1414 &mMappingDataBase,
1415 mCallerImageHandle
1416 );
1417 }
1418
1419 /**
1420 Retrieves the device path of the platform override driver for a controller in the system.
1421 This driver doesn't support this API.
1422
1423 @param This A pointer to the EFI_PLATFORM_DRIVER_OVERRIDE_
1424 PROTOCOL instance.
1425 @param ControllerHandle The device handle of the controller to check if a driver override
1426 exists.
1427 @param DriverImagePath On input, a pointer to the previous driver device path returned by
1428 GetDriverPath(). On output, a pointer to the next driver
1429 device path. Passing in a pointer to NULL, will return the first
1430 driver device path for ControllerHandle.
1431
1432 @retval EFI_UNSUPPORTED
1433 **/
1434 EFI_STATUS
1435 EFIAPI
1436 GetDriverPath (
1437 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *This,
1438 IN EFI_HANDLE ControllerHandle,
1439 IN OUT EFI_DEVICE_PATH_PROTOCOL **DriverImagePath
1440 )
1441 {
1442 return EFI_UNSUPPORTED;
1443 }
1444
1445
1446 /**
1447 Used to associate a driver image handle with a device path that was returned on a prior call to the
1448 GetDriverPath() service. This driver image handle will then be available through the
1449 GetDriver() service. This driver doesn't support this API.
1450
1451 @param This A pointer to the EFI_PLATFORM_DRIVER_OVERRIDE_
1452 PROTOCOL instance.
1453 @param ControllerHandle The device handle of the controller.
1454 @param DriverImagePath A pointer to the driver device path that was returned in a prior
1455 call to GetDriverPath().
1456 @param DriverImageHandle The driver image handle that was returned by LoadImage()
1457 when the driver specified by DriverImagePath was loaded
1458 into memory.
1459
1460 @retval EFI_UNSUPPORTED
1461 **/
1462 EFI_STATUS
1463 EFIAPI
1464 DriverLoaded (
1465 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *This,
1466 IN EFI_HANDLE ControllerHandle,
1467 IN EFI_DEVICE_PATH_PROTOCOL *DriverImagePath,
1468 IN EFI_HANDLE DriverImageHandle
1469 )
1470 {
1471 return EFI_UNSUPPORTED;
1472 }
1473
1474 /**
1475 The driver Entry Point. The funciton will export a disk device class formset and
1476 its callback function to hii database.
1477
1478 @param ImageHandle The firmware allocated handle for the EFI image.
1479 @param SystemTable A pointer to the EFI System Table.
1480
1481 @retval EFI_SUCCESS The entry point is executed successfully.
1482 @retval other Some error occurs when executing this entry point.
1483
1484 **/
1485 EFI_STATUS
1486 EFIAPI
1487 PlatDriOverrideDxeInit (
1488 IN EFI_HANDLE ImageHandle,
1489 IN EFI_SYSTEM_TABLE *SystemTable
1490 )
1491 {
1492 EFI_STATUS Status;
1493 EFI_FORM_BROWSER2_PROTOCOL *FormBrowser2;
1494 VOID *Instance;
1495
1496 //
1497 // There should only be one Form Configuration protocol
1498 //
1499 Status = gBS->LocateProtocol (
1500 &gEfiFormBrowser2ProtocolGuid,
1501 NULL,
1502 (VOID **) &FormBrowser2
1503 );
1504 if (EFI_ERROR (Status)) {
1505 return Status;
1506 }
1507
1508 //
1509 // According to UEFI spec, there can be at most a single instance
1510 // in the system of the EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL.
1511 // So here we check the existence.
1512 //
1513 Status = gBS->LocateProtocol (
1514 &gEfiPlatformDriverOverrideProtocolGuid,
1515 NULL,
1516 &Instance
1517 );
1518 //
1519 // If there was no error, assume there is an installation and return error
1520 //
1521 if (!EFI_ERROR (Status)) {
1522 return EFI_ALREADY_STARTED;
1523 }
1524
1525 mCallerImageHandle = ImageHandle;
1526 mCallbackInfo = AllocateZeroPool (sizeof (EFI_CALLBACK_INFO));
1527 if (mCallbackInfo == NULL) {
1528 return EFI_BAD_BUFFER_SIZE;
1529 }
1530
1531 mCallbackInfo->Signature = EFI_CALLBACK_INFO_SIGNATURE;
1532 mCallbackInfo->ConfigAccess.ExtractConfig = PlatOverMngrExtractConfig;
1533 mCallbackInfo->ConfigAccess.RouteConfig = PlatOverMngrRouteConfig;
1534 mCallbackInfo->ConfigAccess.Callback = PlatOverMngrCallback;
1535 mCallbackInfo->PlatformDriverOverride.GetDriver = GetDriver;
1536 mCallbackInfo->PlatformDriverOverride.GetDriverPath = GetDriverPath;
1537 mCallbackInfo->PlatformDriverOverride.DriverLoaded = DriverLoaded;
1538 //
1539 // Install Device Path Protocol and Config Access protocol to driver handle
1540 // Install Platform Driver Override Protocol to driver handle
1541 //
1542 Status = gBS->InstallMultipleProtocolInterfaces (
1543 &mCallbackInfo->DriverHandle,
1544 &gEfiDevicePathProtocolGuid,
1545 &mHiiVendorDevicePath,
1546 &gEfiHiiConfigAccessProtocolGuid,
1547 &mCallbackInfo->ConfigAccess,
1548 &gEfiPlatformDriverOverrideProtocolGuid,
1549 &mCallbackInfo->PlatformDriverOverride,
1550 NULL
1551 );
1552 if (EFI_ERROR (Status)) {
1553 goto Finish;
1554 }
1555
1556 //
1557 // Publish our HII data
1558 //
1559 mCallbackInfo->RegisteredHandle = HiiAddPackages (
1560 &mPlatformOverridesManagerGuid,
1561 mCallbackInfo->DriverHandle,
1562 VfrBin,
1563 PlatDriOverrideDxeStrings,
1564 NULL
1565 );
1566 if (mCallbackInfo->RegisteredHandle == NULL) {
1567 Status = EFI_OUT_OF_RESOURCES;
1568 goto Finish;
1569 }
1570
1571 //
1572 // Locate ConfigRouting protocol
1573 //
1574 Status = gBS->LocateProtocol (
1575 &gEfiHiiConfigRoutingProtocolGuid,
1576 NULL,
1577 (VOID **) &mCallbackInfo->HiiConfigRouting
1578 );
1579 if (EFI_ERROR (Status)) {
1580 goto Finish;
1581 }
1582
1583 //
1584 // Clear all the globle variable
1585 //
1586 mDriverImageHandleCount = 0;
1587 mCurrentPage = 0;
1588 ZeroMem (mDriverImageToken, MAX_CHOICE_NUM * sizeof (EFI_STRING_ID));
1589 ZeroMem (mDriverImageFilePathToken, MAX_CHOICE_NUM * sizeof (EFI_STRING_ID));
1590 ZeroMem (mControllerToken, MAX_CHOICE_NUM * sizeof (EFI_STRING_ID));
1591 ZeroMem (mDriverImageProtocol, MAX_CHOICE_NUM * sizeof (EFI_LOADED_IMAGE_PROTOCOL *));
1592
1593 return EFI_SUCCESS;
1594
1595 Finish:
1596 if (mCallbackInfo->DriverHandle != NULL) {
1597 gBS->UninstallMultipleProtocolInterfaces (
1598 mCallbackInfo->DriverHandle,
1599 &gEfiDevicePathProtocolGuid,
1600 &mHiiVendorDevicePath,
1601 &gEfiHiiConfigAccessProtocolGuid,
1602 &mCallbackInfo->ConfigAccess,
1603 &gEfiPlatformDriverOverrideProtocolGuid,
1604 &mCallbackInfo->PlatformDriverOverride,
1605 NULL
1606 );
1607 }
1608
1609 if (mCallbackInfo->RegisteredHandle != NULL) {
1610 HiiRemovePackages (mCallbackInfo->RegisteredHandle);
1611 }
1612
1613 if (mCallbackInfo != NULL) {
1614 FreePool (mCallbackInfo);
1615 }
1616
1617 return Status;
1618 }
1619
1620 /**
1621 Unload its installed protocol.
1622
1623 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1624
1625 @retval EFI_SUCCESS The image has been unloaded.
1626 **/
1627 EFI_STATUS
1628 EFIAPI
1629 PlatDriOverrideDxeUnload (
1630 IN EFI_HANDLE ImageHandle
1631 )
1632 {
1633 if (mCallbackInfo->DriverHandle != NULL) {
1634 gBS->UninstallMultipleProtocolInterfaces (
1635 mCallbackInfo->DriverHandle,
1636 &gEfiDevicePathProtocolGuid,
1637 &mHiiVendorDevicePath,
1638 &gEfiHiiConfigAccessProtocolGuid,
1639 &mCallbackInfo->ConfigAccess,
1640 &gEfiPlatformDriverOverrideProtocolGuid,
1641 &mCallbackInfo->PlatformDriverOverride,
1642 NULL
1643 );
1644 }
1645
1646 if (mCallbackInfo->RegisteredHandle != NULL) {
1647 HiiRemovePackages (mCallbackInfo->RegisteredHandle);
1648 }
1649
1650 if (mCallbackInfo != NULL) {
1651 FreePool (mCallbackInfo);
1652 }
1653
1654 return EFI_SUCCESS;
1655 }