1 /** @file
2   Implementation of UEFI Driver Configuration Protocol for IDE bus driver which
3   provides ability to set IDE bus controller specific options.
4 
5   Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 
17 #include "IdeBus.h"
18 
19 CHAR16 *OptionString[4] = {
20   L"Enable Primary Master    (Y/N)? -->",
21   L"Enable Primary Slave     (Y/N)? -->",
22   L"Enable Secondary Master  (Y/N)? -->",
23   L"Enable Secondary Slave   (Y/N)? -->"
24 };
25 
26 //
27 // EFI Driver Configuration Protocol
28 //
29 EFI_DRIVER_CONFIGURATION_PROTOCOL gIDEBusDriverConfiguration = {
30   IDEBusDriverConfigurationSetOptions,
31   IDEBusDriverConfigurationOptionsValid,
32   IDEBusDriverConfigurationForceDefaults,
33   "eng"
34 };
35 
36 /**
37   Interprete keyboard input.
38 
39   @retval  EFI_ABORTED  Get an 'ESC' key inputed.
40   @retval  EFI_SUCCESS  Get an 'Y' or 'y' inputed.
41   @retval  EFI_NOT_FOUND Get an 'N' or 'n' inputed..
42 
43 **/
44 EFI_STATUS
GetResponse(VOID)45 GetResponse (
46   VOID
47   )
48 {
49   EFI_STATUS    Status;
50   EFI_INPUT_KEY Key;
51 
52   while (TRUE) {
53     Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
54     if (!EFI_ERROR (Status)) {
55       if (Key.ScanCode == SCAN_ESC) {
56         return EFI_ABORTED;
57       }
58 
59       switch (Key.UnicodeChar) {
60 
61       //
62       // fall through
63       //
64       case L'y':
65       case L'Y':
66         gST->ConOut->OutputString (gST->ConOut, L"Y\n");
67         return EFI_SUCCESS;
68 
69       //
70       // fall through
71       //
72       case L'n':
73       case L'N':
74         gST->ConOut->OutputString (gST->ConOut, L"N\n");
75         return EFI_NOT_FOUND;
76       }
77 
78     }
79   }
80 }
81 
82 /**
83   Allows the user to set controller specific options for a controller that a
84   driver is currently managing.
85 
86   @param  This              A pointer to the EFI_DRIVER_CONFIGURATION_ PROTOCOL instance.
87   @param  ControllerHandle  The handle of the controller to set options on.
88   @param  ChildHandle       The handle of the child controller to set options on.
89                             This is an optional parameter that may be NULL.
90                             It will be NULL for device drivers, and for a bus drivers
91                             that wish to set options for the bus controller.
92                             It will not be NULL for a bus driver that wishes to set
93                             options for one of its child controllers.
94   @param  Language          A pointer to a three character ISO 639-2 language identifier.
95                             This is the language of the user interface that should be presented
96                             to the user, and it must match one of the languages specified in
97                             SupportedLanguages. The number of languages supported by a driver is up to
98                             the driver writer.
99   @param  ActionRequired    A pointer to the action that the calling agent is required
100                             to perform when this function returns.
101 
102 
103   @retval  EFI_SUCCESS           The driver specified by This successfully set the configuration
104                                  options for the controller specified by ControllerHandle..
105   @retval  EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
106   @retval  EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE.
107   @retval  EFI_INVALID_PARAMETER ActionRequired is NULL.
108   @retval  EFI_UNSUPPORTED       The driver specified by This does not support setting configuration options for
109                                  the controller specified by ControllerHandle and ChildHandle.
110   @retval  EFI_UNSUPPORTED       The driver specified by This does not support the language specified by Language.
111   @retval  EFI_DEVICE_ERROR      A device error occurred while attempt to set the configuration options for the
112                                  controller specified by ControllerHandle and ChildHandle.
113   @retval  EFI_OUT_RESOURCES     There are not enough resources available to set the configuration options for the
114                                  controller specified by ControllerHandle and ChildHandle
115 **/
116 EFI_STATUS
117 EFIAPI
IDEBusDriverConfigurationSetOptions(IN EFI_DRIVER_CONFIGURATION_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL,IN CHAR8 * Language,OUT EFI_DRIVER_CONFIGURATION_ACTION_REQUIRED * ActionRequired)118 IDEBusDriverConfigurationSetOptions (
119   IN  EFI_DRIVER_CONFIGURATION_PROTOCOL                      *This,
120   IN  EFI_HANDLE                                             ControllerHandle,
121   IN  EFI_HANDLE                                             ChildHandle  OPTIONAL,
122   IN  CHAR8                                                  *Language,
123   OUT EFI_DRIVER_CONFIGURATION_ACTION_REQUIRED               *ActionRequired
124   )
125 {
126   EFI_STATUS  Status;
127   UINT8       Value;
128   UINT8       NewValue;
129   UINTN       DataSize;
130   UINTN       Index;
131 
132   if (ChildHandle != NULL) {
133     return EFI_UNSUPPORTED;
134   }
135 
136   *ActionRequired = EfiDriverConfigurationActionNone;
137 
138   DataSize        = sizeof (Value);
139   Status = gRT->GetVariable (
140                   L"Configuration",
141                   &gEfiCallerIdGuid,
142                   NULL,
143                   &DataSize,
144                   &Value
145                   );
146 
147   gST->ConOut->OutputString (gST->ConOut, L"IDE Bus Driver Configuration\n");
148   gST->ConOut->OutputString (gST->ConOut, L"===============================\n");
149 
150   NewValue = 0;
151   for (Index = 0; Index < 4; Index++) {
152     gST->ConOut->OutputString (gST->ConOut, OptionString[Index]);
153 
154     Status = GetResponse ();
155     if (Status == EFI_ABORTED) {
156       return EFI_SUCCESS;
157     }
158 
159     if (!EFI_ERROR (Status)) {
160       NewValue = (UINT8) (NewValue | (1 << Index));
161     }
162   }
163 
164   if (EFI_ERROR (Status) || (NewValue != Value)) {
165     gRT->SetVariable (
166           L"Configuration",
167           &gEfiCallerIdGuid,
168           EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
169           sizeof (NewValue),
170           &NewValue
171           );
172 
173     *ActionRequired = EfiDriverConfigurationActionRestartController;
174   } else {
175     *ActionRequired = EfiDriverConfigurationActionNone;
176   }
177 
178   return EFI_SUCCESS;
179 }
180 
181 /**
182   Tests to see if a controller's current configuration options are valid.
183 
184   @param  This             A pointer to the EFI_DRIVER_CONFIGURATION_PROTOCOL instance.
185   @param  ControllerHandle The handle of the controller to test if it's current configuration options
186                            are valid.
187   @param  ChildHandle      The handle of the child controller to test if it's current configuration
188                            options are valid.  This is an optional parameter that may be NULL. It will
189                            be NULL for device drivers.  It will also be NULL for a bus drivers that
190                            wish to test the configuration options for the bus controller. It will
191                            not be NULL for a bus driver that wishes to test configuration options for
192                            one of its child controllers.
193   @retval  EFI_SUCCESS           The controller specified by ControllerHandle and ChildHandle that is being
194                                  managed by the driver specified by This has a valid set of  configuration
195                                  options.
196   @retval  EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
197   @retval  EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE.
198   @retval  EFI_UNSUPPORTED       The driver specified by This is not currently  managing the controller
199                                  specified by ControllerHandle and ChildHandle.
200   @retval  EFI_DEVICE_ERROR      The controller specified by ControllerHandle and ChildHandle that is being
201                                  managed by the driver specified by This has an invalid set of configuration
202                                  options.
203 **/
204 EFI_STATUS
205 EFIAPI
IDEBusDriverConfigurationOptionsValid(IN EFI_DRIVER_CONFIGURATION_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL)206 IDEBusDriverConfigurationOptionsValid (
207   IN  EFI_DRIVER_CONFIGURATION_PROTOCOL               *This,
208   IN  EFI_HANDLE                                      ControllerHandle,
209   IN  EFI_HANDLE                                      ChildHandle  OPTIONAL
210   )
211 {
212   EFI_STATUS  Status;
213   UINT8       Value;
214   UINTN       DataSize;
215 
216   if (ChildHandle != NULL) {
217     return EFI_UNSUPPORTED;
218   }
219 
220   DataSize = sizeof (Value);
221   Status = gRT->GetVariable (
222                   L"Configuration",
223                   &gEfiCallerIdGuid,
224                   NULL,
225                   &DataSize,
226                   &Value
227                   );
228   if (EFI_ERROR (Status) || Value > 0x0f) {
229     return EFI_DEVICE_ERROR;
230   }
231 
232   return EFI_SUCCESS;
233 }
234 /**
235   Forces a driver to set the default configuration options for a controller.
236 
237   @param  This             A pointer to the EFI_DRIVER_CONFIGURATION_ PROTOCOL instance.
238   @param  ControllerHandle The handle of the controller to force default configuration options on.
239   @param  ChildHandle      The handle of the child controller to force default configuration
240                            options on  This is an optional parameter that may be NULL.  It
241                            will be NULL for device drivers. It will also be NULL for a bus
242                            drivers that wish to force default configuration options for the bus
243                            controller.  It will not be NULL for a bus driver that wishes to force
244                            default configuration options for one of its child controllers.
245   @param  DefaultType      The type of default configuration options to force on the controller
246                            specified by ControllerHandle and ChildHandle.
247   @param  ActionRequired   A pointer to the action that the calling agent is required to perform
248                            when this function returns.
249 
250   @retval  EFI_SUCCESS           The driver specified by This successfully forced the
251                                  default configuration options on the controller specified by
252                                  ControllerHandle and ChildHandle.
253   @retval  EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
254   @retval  EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE.
255   @retval  EFI_INVALID_PARAMETER ActionRequired is NULL.
256   @retval  EFI_UNSUPPORTED       The driver specified by This does not support forcing the default
257                                  configuration options on the controller specified by ControllerHandle
258                                  and ChildHandle.
259   @retval  EFI_UNSUPPORTED       The driver specified by This does not support the configuration type
260                                  specified by DefaultType.
261   @retval  EFI_DEVICE_ERROR      A device error occurred while attempt to force the default configuration
262                                  options on the controller specified by  ControllerHandle and ChildHandle.
263   @retval  EFI_OUT_RESOURCES     There are not enough resources available to force the default configuration
264                                  options on the controller specified by ControllerHandle and ChildHandle.
265 **/
266 EFI_STATUS
267 EFIAPI
IDEBusDriverConfigurationForceDefaults(IN EFI_DRIVER_CONFIGURATION_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL,IN UINT32 DefaultType,OUT EFI_DRIVER_CONFIGURATION_ACTION_REQUIRED * ActionRequired)268 IDEBusDriverConfigurationForceDefaults (
269   IN  EFI_DRIVER_CONFIGURATION_PROTOCOL                      *This,
270   IN  EFI_HANDLE                                             ControllerHandle,
271   IN  EFI_HANDLE                                             ChildHandle  OPTIONAL,
272   IN  UINT32                                                 DefaultType,
273   OUT EFI_DRIVER_CONFIGURATION_ACTION_REQUIRED               *ActionRequired
274   )
275 {
276   UINT8 Value;
277 
278   if (ChildHandle != NULL) {
279     return EFI_UNSUPPORTED;
280   }
281 
282   Value = 0x0f;
283   gRT->SetVariable (
284         L"Configuration",
285         &gEfiCallerIdGuid,
286         EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
287         sizeof (Value),
288         &Value
289         );
290   *ActionRequired = EfiDriverConfigurationActionRestartController;
291   return EFI_SUCCESS;
292 }
293