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