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