]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideDxe.c
Update HiiDataBase to fix the SCT hang issues by the invalid device path.
[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 (Progress == NULL || Results == NULL || Request == NULL) {
1166 return EFI_INVALID_PARAMETER;
1167 }
1168 *Progress = Request;
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 if (Configuration == NULL || Progress == NULL) {
1214 return EFI_INVALID_PARAMETER;
1215 }
1216 *Progress = Configuration;
1217
1218 if (!HiiIsConfigHdrMatch (Configuration, &mPlatformOverridesManagerGuid, mVariableName)) {
1219 return EFI_NOT_FOUND;
1220 }
1221
1222 Private = EFI_CALLBACK_INFO_FROM_THIS (This);
1223 FakeNvData = &Private->FakeNvData;
1224 if (!HiiGetBrowserData (&mPlatformOverridesManagerGuid, mVariableName, sizeof (PLAT_OVER_MNGR_DATA), (UINT8 *) FakeNvData)) {
1225 return EFI_NOT_FOUND;
1226 }
1227
1228 if (mCurrentPage == FORM_ID_DRIVER) {
1229 KeyValue = KEY_VALUE_DRIVER_GOTO_ORDER;
1230 UpdatePrioritySelectPage (Private, KeyValue, FakeNvData);
1231 KeyValue = KEY_VALUE_ORDER_SAVE_AND_EXIT;
1232 CommintChanges (Private, KeyValue, FakeNvData);
1233 //
1234 // Since UpdatePrioritySelectPage will change mCurrentPage,
1235 // should ensure the mCurrentPage still indicate the second page here
1236 //
1237 mCurrentPage = FORM_ID_DRIVER;
1238 }
1239
1240 if (mCurrentPage == FORM_ID_ORDER) {
1241 KeyValue = KEY_VALUE_ORDER_SAVE_AND_EXIT;
1242 CommintChanges (Private, KeyValue, FakeNvData);
1243 }
1244
1245 *Progress = Configuration + StrLen (Configuration);
1246 return EFI_SUCCESS;
1247 }
1248
1249 /**
1250 This is the function that is called to provide results data to the driver. This data
1251 consists of a unique key which is used to identify what data is either being passed back
1252 or being asked for.
1253
1254 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1255 @param Action A null-terminated Unicode string in <ConfigRequest> format.
1256 @param KeyValue A unique Goto OpCode callback value which record user's selection.
1257 0x100 <= KeyValue <0x500 : user select a controller item in the first page;
1258 KeyValue == 0x1234 : user select 'Refresh' in first page, or user select 'Go to Previous Menu' in second page
1259 KeyValue == 0x1235 : user select 'Pci device filter' in first page
1260 KeyValue == 0x1500 : user select 'order ... priority' item in second page
1261 KeyValue == 0x1800 : user select 'commint changes' in third page
1262 KeyValue == 0x2000 : user select 'Go to Previous Menu' in third page
1263 @param Type The type of value for the question.
1264 @param Value A pointer to the data being sent to the original exporting driver.
1265 @param ActionRequest On return, points to the action requested by the callback function.
1266
1267 @retval EFI_SUCCESS Always returned.
1268
1269 **/
1270 EFI_STATUS
1271 EFIAPI
1272 PlatOverMngrCallback (
1273 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1274 IN EFI_BROWSER_ACTION Action,
1275 IN EFI_QUESTION_ID KeyValue,
1276 IN UINT8 Type,
1277 IN EFI_IFR_TYPE_VALUE *Value,
1278 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
1279 )
1280 {
1281 EFI_CALLBACK_INFO *Private;
1282 EFI_STATUS Status;
1283 EFI_STRING_ID NewStringToken;
1284 EFI_INPUT_KEY Key;
1285 PLAT_OVER_MNGR_DATA *FakeNvData;
1286
1287 Private = EFI_CALLBACK_INFO_FROM_THIS (This);
1288 FakeNvData = &Private->FakeNvData;
1289 if (!HiiGetBrowserData (&mPlatformOverridesManagerGuid, mVariableName, sizeof (PLAT_OVER_MNGR_DATA), (UINT8 *) FakeNvData)) {
1290 return EFI_NOT_FOUND;
1291 }
1292
1293 if (KeyValue == KEY_VALUE_DEVICE_REFRESH ||
1294 KeyValue == KEY_VALUE_DEVICE_FILTER ||
1295 KeyValue == KEY_VALUE_DRIVER_GOTO_PREVIOUS
1296 ) {
1297 UpdateDeviceSelectPage (Private, KeyValue, FakeNvData);
1298 //
1299 // Update page title string
1300 //
1301 NewStringToken = STRING_TOKEN (STR_TITLE);
1302 if (HiiSetString (Private->RegisteredHandle, NewStringToken, L"First, Select the controller by device path", NULL) == 0) {
1303 ASSERT (FALSE);
1304 }
1305 }
1306
1307 if (((KeyValue >= KEY_VALUE_DEVICE_OFFSET) && (KeyValue < KEY_VALUE_DEVICE_MAX)) || (KeyValue == KEY_VALUE_ORDER_GOTO_PREVIOUS)) {
1308 if (KeyValue == KEY_VALUE_ORDER_GOTO_PREVIOUS) {
1309 KeyValue = (EFI_QUESTION_ID) (mSelectedCtrIndex + KEY_VALUE_DEVICE_OFFSET);
1310 }
1311 UpdateBindingDriverSelectPage (Private, KeyValue, FakeNvData);
1312 //
1313 // Update page title string
1314 //
1315 NewStringToken = STRING_TOKEN (STR_TITLE);
1316 if (HiiSetString (Private->RegisteredHandle, NewStringToken, L"Second, Select drivers for the previous selected controller", NULL) == 0) {
1317 ASSERT (FALSE);
1318 }
1319 }
1320
1321 if (KeyValue == KEY_VALUE_DRIVER_GOTO_ORDER) {
1322 UpdatePrioritySelectPage (Private, KeyValue, FakeNvData);
1323 //
1324 // Update page title string
1325 //
1326 NewStringToken = STRING_TOKEN (STR_TITLE);
1327 if (HiiSetString (Private->RegisteredHandle, NewStringToken, L"Finally, Set the priority order for the drivers and save them", NULL) == 0) {
1328 ASSERT (FALSE);
1329 }
1330 }
1331
1332 if (KeyValue == KEY_VALUE_ORDER_SAVE_AND_EXIT) {
1333 Status = CommintChanges (Private, KeyValue, FakeNvData);
1334 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1335 if (EFI_ERROR (Status)) {
1336 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Single Override Info too large, Saving Error!", NULL);
1337 return EFI_DEVICE_ERROR;
1338 }
1339 }
1340
1341 if (KeyValue == KEY_VALUE_DEVICE_CLEAR) {
1342 //
1343 // Deletes all environment variable(s) that contain the override mappings info
1344 //
1345 FreeMappingDatabase (&mMappingDataBase);
1346 Status = SaveOverridesMapping (&mMappingDataBase);
1347 UpdateDeviceSelectPage (Private, KeyValue, FakeNvData);
1348 }
1349 //
1350 // Pass changed uncommitted data back to Form Browser
1351 //
1352 HiiSetBrowserData (&mPlatformOverridesManagerGuid, mVariableName, sizeof (PLAT_OVER_MNGR_DATA), (UINT8 *) FakeNvData, NULL);
1353
1354 return EFI_SUCCESS;
1355 }
1356
1357 /**
1358 Retrieves the image handle of the platform override driver for a controller in the system.
1359
1360 @param This A pointer to the
1361 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.
1362 @param ControllerHandle The device handle of the controller to check if a
1363 driver override exists.
1364 @param DriverImageHandle On input, a pointer to the previous driver image
1365 handle returned by GetDriver(). On output, a
1366 pointer to the next driver image handle. Passing
1367 in a NULL, will return the first driver image
1368 handle for ControllerHandle.
1369
1370 @retval EFI_SUCCESS The driver override for ControllerHandle was
1371 returned in DriverImageHandle.
1372 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
1373 found.
1374 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not a
1375 valid handle. DriverImageHandle is not a handle
1376 that was returned on a previous call to
1377 GetDriver().
1378
1379 **/
1380 EFI_STATUS
1381 EFIAPI
1382 GetDriver (
1383 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *This,
1384 IN EFI_HANDLE ControllerHandle,
1385 IN OUT EFI_HANDLE *DriverImageHandle
1386 )
1387 {
1388 EFI_STATUS Status;
1389
1390 //
1391 // Check that ControllerHandle is a valid handle
1392 //
1393 if (ControllerHandle == NULL) {
1394 return EFI_INVALID_PARAMETER;
1395 }
1396
1397 //
1398 // Read the environment variable(s) that contain the override mappings from Controller Device Path to
1399 // a set of Driver Device Paths, and initialize in memory database of the overrides that map Controller
1400 // Device Paths to an ordered set of Driver Device Paths and Driver Handles. This action is only performed
1401 // once and finished in first call.
1402 //
1403 if (!mEnvironmentVariableRead) {
1404 mEnvironmentVariableRead = TRUE;
1405
1406 Status = InitOverridesMapping (&mMappingDataBase);
1407 if (EFI_ERROR (Status)){
1408 DEBUG ((DEBUG_ERROR, "The status to Get Platform Driver Override Variable is %r\n", Status));
1409 InitializeListHead (&mMappingDataBase);
1410 return EFI_NOT_FOUND;
1411 }
1412 }
1413
1414 //
1415 // if the environment variable does not exist, just return not found
1416 //
1417 if (IsListEmpty (&mMappingDataBase)) {
1418 return EFI_NOT_FOUND;
1419 }
1420
1421 return GetDriverFromMapping (
1422 ControllerHandle,
1423 DriverImageHandle,
1424 &mMappingDataBase,
1425 mCallerImageHandle
1426 );
1427 }
1428
1429 /**
1430 Retrieves the device path of the platform override driver for a controller in the system.
1431 This driver doesn't support this API.
1432
1433 @param This A pointer to the EFI_PLATFORM_DRIVER_OVERRIDE_
1434 PROTOCOL instance.
1435 @param ControllerHandle The device handle of the controller to check if a driver override
1436 exists.
1437 @param DriverImagePath On input, a pointer to the previous driver device path returned by
1438 GetDriverPath(). On output, a pointer to the next driver
1439 device path. Passing in a pointer to NULL, will return the first
1440 driver device path for ControllerHandle.
1441
1442 @retval EFI_UNSUPPORTED
1443 **/
1444 EFI_STATUS
1445 EFIAPI
1446 GetDriverPath (
1447 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *This,
1448 IN EFI_HANDLE ControllerHandle,
1449 IN OUT EFI_DEVICE_PATH_PROTOCOL **DriverImagePath
1450 )
1451 {
1452 return EFI_UNSUPPORTED;
1453 }
1454
1455
1456 /**
1457 Used to associate a driver image handle with a device path that was returned on a prior call to the
1458 GetDriverPath() service. This driver image handle will then be available through the
1459 GetDriver() service. This driver doesn't support this API.
1460
1461 @param This A pointer to the EFI_PLATFORM_DRIVER_OVERRIDE_
1462 PROTOCOL instance.
1463 @param ControllerHandle The device handle of the controller.
1464 @param DriverImagePath A pointer to the driver device path that was returned in a prior
1465 call to GetDriverPath().
1466 @param DriverImageHandle The driver image handle that was returned by LoadImage()
1467 when the driver specified by DriverImagePath was loaded
1468 into memory.
1469
1470 @retval EFI_UNSUPPORTED
1471 **/
1472 EFI_STATUS
1473 EFIAPI
1474 DriverLoaded (
1475 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *This,
1476 IN EFI_HANDLE ControllerHandle,
1477 IN EFI_DEVICE_PATH_PROTOCOL *DriverImagePath,
1478 IN EFI_HANDLE DriverImageHandle
1479 )
1480 {
1481 return EFI_UNSUPPORTED;
1482 }
1483
1484 /**
1485 The driver Entry Point. The funciton will export a disk device class formset and
1486 its callback function to hii database.
1487
1488 @param ImageHandle The firmware allocated handle for the EFI image.
1489 @param SystemTable A pointer to the EFI System Table.
1490
1491 @retval EFI_SUCCESS The entry point is executed successfully.
1492 @retval other Some error occurs when executing this entry point.
1493
1494 **/
1495 EFI_STATUS
1496 EFIAPI
1497 PlatDriOverrideDxeInit (
1498 IN EFI_HANDLE ImageHandle,
1499 IN EFI_SYSTEM_TABLE *SystemTable
1500 )
1501 {
1502 EFI_STATUS Status;
1503 EFI_FORM_BROWSER2_PROTOCOL *FormBrowser2;
1504 VOID *Instance;
1505
1506 //
1507 // There should only be one Form Configuration protocol
1508 //
1509 Status = gBS->LocateProtocol (
1510 &gEfiFormBrowser2ProtocolGuid,
1511 NULL,
1512 (VOID **) &FormBrowser2
1513 );
1514 if (EFI_ERROR (Status)) {
1515 return Status;
1516 }
1517
1518 //
1519 // According to UEFI spec, there can be at most a single instance
1520 // in the system of the EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL.
1521 // So here we check the existence.
1522 //
1523 Status = gBS->LocateProtocol (
1524 &gEfiPlatformDriverOverrideProtocolGuid,
1525 NULL,
1526 &Instance
1527 );
1528 //
1529 // If there was no error, assume there is an installation and return error
1530 //
1531 if (!EFI_ERROR (Status)) {
1532 return EFI_ALREADY_STARTED;
1533 }
1534
1535 mCallerImageHandle = ImageHandle;
1536 mCallbackInfo = AllocateZeroPool (sizeof (EFI_CALLBACK_INFO));
1537 if (mCallbackInfo == NULL) {
1538 return EFI_BAD_BUFFER_SIZE;
1539 }
1540
1541 mCallbackInfo->Signature = EFI_CALLBACK_INFO_SIGNATURE;
1542 mCallbackInfo->ConfigAccess.ExtractConfig = PlatOverMngrExtractConfig;
1543 mCallbackInfo->ConfigAccess.RouteConfig = PlatOverMngrRouteConfig;
1544 mCallbackInfo->ConfigAccess.Callback = PlatOverMngrCallback;
1545 mCallbackInfo->PlatformDriverOverride.GetDriver = GetDriver;
1546 mCallbackInfo->PlatformDriverOverride.GetDriverPath = GetDriverPath;
1547 mCallbackInfo->PlatformDriverOverride.DriverLoaded = DriverLoaded;
1548 //
1549 // Install Device Path Protocol and Config Access protocol to driver handle
1550 // Install Platform Driver Override Protocol to driver handle
1551 //
1552 Status = gBS->InstallMultipleProtocolInterfaces (
1553 &mCallbackInfo->DriverHandle,
1554 &gEfiDevicePathProtocolGuid,
1555 &mHiiVendorDevicePath,
1556 &gEfiHiiConfigAccessProtocolGuid,
1557 &mCallbackInfo->ConfigAccess,
1558 &gEfiPlatformDriverOverrideProtocolGuid,
1559 &mCallbackInfo->PlatformDriverOverride,
1560 NULL
1561 );
1562 if (EFI_ERROR (Status)) {
1563 goto Finish;
1564 }
1565
1566 //
1567 // Publish our HII data
1568 //
1569 mCallbackInfo->RegisteredHandle = HiiAddPackages (
1570 &mPlatformOverridesManagerGuid,
1571 mCallbackInfo->DriverHandle,
1572 VfrBin,
1573 PlatDriOverrideDxeStrings,
1574 NULL
1575 );
1576 if (mCallbackInfo->RegisteredHandle == NULL) {
1577 Status = EFI_OUT_OF_RESOURCES;
1578 goto Finish;
1579 }
1580
1581 //
1582 // Locate ConfigRouting protocol
1583 //
1584 Status = gBS->LocateProtocol (
1585 &gEfiHiiConfigRoutingProtocolGuid,
1586 NULL,
1587 (VOID **) &mCallbackInfo->HiiConfigRouting
1588 );
1589 if (EFI_ERROR (Status)) {
1590 goto Finish;
1591 }
1592
1593 //
1594 // Clear all the globle variable
1595 //
1596 mDriverImageHandleCount = 0;
1597 mCurrentPage = 0;
1598 ZeroMem (mDriverImageToken, MAX_CHOICE_NUM * sizeof (EFI_STRING_ID));
1599 ZeroMem (mDriverImageFilePathToken, MAX_CHOICE_NUM * sizeof (EFI_STRING_ID));
1600 ZeroMem (mControllerToken, MAX_CHOICE_NUM * sizeof (EFI_STRING_ID));
1601 ZeroMem (mDriverImageProtocol, MAX_CHOICE_NUM * sizeof (EFI_LOADED_IMAGE_PROTOCOL *));
1602
1603 return EFI_SUCCESS;
1604
1605 Finish:
1606 if (mCallbackInfo->DriverHandle != NULL) {
1607 gBS->UninstallMultipleProtocolInterfaces (
1608 mCallbackInfo->DriverHandle,
1609 &gEfiDevicePathProtocolGuid,
1610 &mHiiVendorDevicePath,
1611 &gEfiHiiConfigAccessProtocolGuid,
1612 &mCallbackInfo->ConfigAccess,
1613 &gEfiPlatformDriverOverrideProtocolGuid,
1614 &mCallbackInfo->PlatformDriverOverride,
1615 NULL
1616 );
1617 }
1618
1619 if (mCallbackInfo->RegisteredHandle != NULL) {
1620 HiiRemovePackages (mCallbackInfo->RegisteredHandle);
1621 }
1622
1623 if (mCallbackInfo != NULL) {
1624 FreePool (mCallbackInfo);
1625 }
1626
1627 return Status;
1628 }
1629
1630 /**
1631 Unload its installed protocol.
1632
1633 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1634
1635 @retval EFI_SUCCESS The image has been unloaded.
1636 **/
1637 EFI_STATUS
1638 EFIAPI
1639 PlatDriOverrideDxeUnload (
1640 IN EFI_HANDLE ImageHandle
1641 )
1642 {
1643 if (mCallbackInfo->DriverHandle != NULL) {
1644 gBS->UninstallMultipleProtocolInterfaces (
1645 mCallbackInfo->DriverHandle,
1646 &gEfiDevicePathProtocolGuid,
1647 &mHiiVendorDevicePath,
1648 &gEfiHiiConfigAccessProtocolGuid,
1649 &mCallbackInfo->ConfigAccess,
1650 &gEfiPlatformDriverOverrideProtocolGuid,
1651 &mCallbackInfo->PlatformDriverOverride,
1652 NULL
1653 );
1654 }
1655
1656 if (mCallbackInfo->RegisteredHandle != NULL) {
1657 HiiRemovePackages (mCallbackInfo->RegisteredHandle);
1658 }
1659
1660 if (mCallbackInfo != NULL) {
1661 FreePool (mCallbackInfo);
1662 }
1663
1664 return EFI_SUCCESS;
1665 }