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