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