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