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