Port PlatformDriOverrideDxe into R9.
[mirror_edk2.git] / MdeModulePkg / Universal / PlatformDriOverrideDxe / PlatformDriOverride.c
1 /** @file
2
3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 PlatformDriOverride.c
15
16 Abstract:
17
18
19 **/
20
21
22 #include "PlatformDriOverride.h"
23
24 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL gPlatformDriverOverride = {
25 GetDriver,
26 GetDriverPath,
27 DriverLoaded
28 };
29
30 STATIC LIST_ENTRY mMappingDataBase = INITIALIZE_LIST_HEAD_VARIABLE (mMappingDataBase);
31 STATIC BOOLEAN mEnvironmentVariableRead = FALSE;
32 STATIC EFI_HANDLE mCallerImageHandle;
33
34
35 EFI_STATUS
36 EFIAPI
37 PlatformDriverOverrideEntry (
38 IN EFI_HANDLE ImageHandle,
39 IN EFI_SYSTEM_TABLE *SystemTable
40 )
41 /*++
42
43 Routine Description:
44 Platform Driver Override driver entry point, install the Platform Driver Override Protocol
45
46 Arguments:
47 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
48
49 Returns:
50 EFI_STATUS
51
52 --*/
53 {
54 mEnvironmentVariableRead = FALSE;
55 mCallerImageHandle = ImageHandle;
56 InitializeListHead (&mMappingDataBase);
57 return InstallPlatformDriverOverrideProtocol (&gPlatformDriverOverride);
58 }
59
60
61 /**
62 Retrieves the image handle of the platform override driver for a controller in the system.
63
64 @param This A pointer to the
65 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.
66 @param ControllerHandle The device handle of the controller to check if a
67 driver override exists.
68 @param DriverImageHandle On input, a pointer to the previous driver image
69 handle returned by GetDriver(). On output, a
70 pointer to the next driver image handle. Passing
71 in a NULL, will return the first driver image
72 handle for ControllerHandle.
73
74 @retval EFI_SUCCESS The driver override for ControllerHandle was
75 returned in DriverImageHandle.
76 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
77 found.
78 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not a
79 valid handle. DriverImageHandle is not a handle
80 that was returned on a previous call to
81 GetDriver().
82
83 **/
84 STATIC
85 EFI_STATUS
86 EFIAPI
87 GetDriver (
88 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL * This,
89 IN EFI_HANDLE ControllerHandle,
90 IN OUT EFI_HANDLE * DriverImageHandle
91 )
92 {
93 EFI_STATUS Status;
94 //
95 // Check that ControllerHandle is a valid handle
96 //
97 if (ControllerHandle == NULL) {
98 return EFI_INVALID_PARAMETER;
99 }
100
101 //
102 // Read the environment variable(s) that contain the override mappings from Controller Device Path to
103 // a set of Driver Device Paths, and initialize in memory database of the overrides that map Controller
104 // Device Paths to an ordered set of Driver Device Paths and Driver Handles. This action is only performed
105 // once and finished in first call.
106 //
107 if (!mEnvironmentVariableRead) {
108 mEnvironmentVariableRead = TRUE;
109
110 Status = InitOverridesMapping (&mMappingDataBase);
111 if (Status == EFI_NOT_FOUND) {
112 InitializeListHead (&mMappingDataBase);
113 return EFI_NOT_FOUND;
114 } else if (Status == EFI_VOLUME_CORRUPTED){
115 DEBUG ((DEBUG_ERROR, "Platform Driver Override Variable is corrupt\n"));
116 //
117 // The environment variable(s) that contain the override mappings from Controller Device Path to
118 // a set of Driver Device Paths is corrupted, platform code can use LibDeleteOverridesVariables to
119 // delete all orverride variables as a policy. Here can be IBV/OEM customized.
120 //
121
122 //LibDeleteOverridesVariables();
123 InitializeListHead (&mMappingDataBase);
124 return EFI_NOT_FOUND;
125 } else if (EFI_ERROR (Status)){
126 InitializeListHead (&mMappingDataBase);
127 return EFI_NOT_FOUND;
128 }
129 }
130 //
131 // if the environment variable does not exist or the variable appears to be corrupt, just return not found
132 //
133 if (IsListEmpty (&mMappingDataBase)) {
134 return EFI_NOT_FOUND;
135 }
136
137 return GetDriverFromMapping (
138 This,
139 ControllerHandle,
140 DriverImageHandle,
141 &mMappingDataBase,
142 mCallerImageHandle
143 );
144
145 }
146
147
148 /**
149 For the use of the ControllerHandle parameter in the GetDriverPath() and DriverLoaded() APIs
150 makes those APIs very difficult to use, so not support.
151
152
153
154 **/
155 STATIC
156 EFI_STATUS
157 EFIAPI
158 GetDriverPath (
159 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL * This,
160 IN EFI_HANDLE ControllerHandle,
161 IN OUT EFI_DEVICE_PATH_PROTOCOL **DriverImagePath
162 )
163 {
164 return EFI_UNSUPPORTED;
165 }
166
167
168 /**
169 For the use of the ControllerHandle parameter in the GetDriverPath() and DriverLoaded() APIs
170 makes those APIs very difficult to use, so not support.
171
172
173
174 **/
175 STATIC
176 EFI_STATUS
177 EFIAPI
178 DriverLoaded (
179 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL * This,
180 IN EFI_HANDLE ControllerHandle,
181 IN EFI_DEVICE_PATH_PROTOCOL * DriverImagePath,
182 IN EFI_HANDLE DriverImageHandle
183 )
184 {
185 return EFI_UNSUPPORTED;
186 }