1 /**@file
2 
3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
4 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   DriverDiagnostics.c
15 
16 Abstract:
17 
18 **/
19 #include <Uefi.h>
20 #include <WinNtDxe.h>
21 #include <Protocol/BlockIo.h>
22 #include <Protocol/ComponentName.h>
23 #include <Protocol/DriverBinding.h>
24 
25 #include "WinNtBlockIo.h"
26 
27 //
28 // EFI Driver Diagnostics Functions
29 //
30 EFI_STATUS
31 EFIAPI
32 WinNtBlockIoDriverDiagnosticsRunDiagnostics (
33   IN  EFI_DRIVER_DIAGNOSTICS_PROTOCOL               *This,
34   IN  EFI_HANDLE                                    ControllerHandle,
35   IN  EFI_HANDLE                                    ChildHandle  OPTIONAL,
36   IN  EFI_DRIVER_DIAGNOSTIC_TYPE                    DiagnosticType,
37   IN  CHAR8                                         *Language,
38   OUT EFI_GUID                                      **ErrorType,
39   OUT UINTN                                         *BufferSize,
40   OUT CHAR16                                        **Buffer
41   );
42 
43 //
44 // EFI Driver Diagnostics Protocol
45 //
46 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DRIVER_DIAGNOSTICS_PROTOCOL gWinNtBlockIoDriverDiagnostics = {
47   WinNtBlockIoDriverDiagnosticsRunDiagnostics,
48   "eng"
49 };
50 
51 //
52 // EFI Driver Diagnostics 2 Protocol
53 //
54 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DRIVER_DIAGNOSTICS2_PROTOCOL gWinNtBlockIoDriverDiagnostics2 = {
55   (EFI_DRIVER_DIAGNOSTICS2_RUN_DIAGNOSTICS) WinNtBlockIoDriverDiagnosticsRunDiagnostics,
56   "en"
57 };
58 
59 EFI_STATUS
60 EFIAPI
WinNtBlockIoDriverDiagnosticsRunDiagnostics(IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL,IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,IN CHAR8 * Language,OUT EFI_GUID ** ErrorType,OUT UINTN * BufferSize,OUT CHAR16 ** Buffer)61 WinNtBlockIoDriverDiagnosticsRunDiagnostics (
62   IN  EFI_DRIVER_DIAGNOSTICS_PROTOCOL               *This,
63   IN  EFI_HANDLE                                    ControllerHandle,
64   IN  EFI_HANDLE                                    ChildHandle  OPTIONAL,
65   IN  EFI_DRIVER_DIAGNOSTIC_TYPE                    DiagnosticType,
66   IN  CHAR8                                         *Language,
67   OUT EFI_GUID                                      **ErrorType,
68   OUT UINTN                                         *BufferSize,
69   OUT CHAR16                                        **Buffer
70   )
71 /*++
72 
73   Routine Description:
74     Runs diagnostics on a controller.
75 
76   Arguments:
77     This             - A pointer to the EFI_DRIVER_DIAGNOSTICS_PROTOCOL instance.
78     ControllerHandle - The handle of the controller to run diagnostics on.
79     ChildHandle      - The handle of the child controller to run diagnostics on
80                        This is an optional parameter that may be NULL.  It will
81                        be NULL for device drivers.  It will also be NULL for a
82                        bus drivers that wish to run diagnostics on the bus
83                        controller.  It will not be NULL for a bus driver that
84                        wishes to run diagnostics on one of its child controllers.
85     DiagnosticType   - Indicates type of diagnostics to perform on the controller
86                        specified by ControllerHandle and ChildHandle.   See
87                        "Related Definitions" for the list of supported types.
88     Language         - A pointer to a three character ISO 639-2 language
89                        identifier or a Null-terminated ASCII string array indicating
90                        the language.  This is the language in which the optional
91                        error message should be returned in Buffer, and it must
92                        match one of the languages specified in SupportedLanguages.
93                        The number of languages supported by a driver is up to
94                        the driver writer.
95     ErrorType        - A GUID that defines the format of the data returned in
96                        Buffer.
97     BufferSize       - The size, in bytes, of the data returned in Buffer.
98     Buffer           - A buffer that contains a Null-terminated Unicode string
99                        plus some additional data whose format is defined by
100                        ErrorType.  Buffer is allocated by this function with
101                        AllocatePool(), and it is the caller's responsibility
102                        to free it with a call to FreePool().
103 
104   Returns:
105     EFI_SUCCESS           - The controller specified by ControllerHandle and
106                             ChildHandle passed the diagnostic.
107     EFI_INVALID_PARAMETER - ControllerHandle is not a valid EFI_HANDLE.
108     EFI_INVALID_PARAMETER - ChildHandle is not NULL and it is not a valid
109                             EFI_HANDLE.
110     EFI_INVALID_PARAMETER - Language is NULL.
111     EFI_INVALID_PARAMETER - ErrorType is NULL.
112     EFI_INVALID_PARAMETER - BufferType is NULL.
113     EFI_INVALID_PARAMETER - Buffer is NULL.
114     EFI_UNSUPPORTED       - The driver specified by This does not support
115                             running diagnostics for the controller specified
116                             by ControllerHandle and ChildHandle.
117     EFI_UNSUPPORTED       - The driver specified by This does not support the
118                             type of diagnostic specified by DiagnosticType.
119     EFI_UNSUPPORTED       - The driver specified by This does not support the
120                             language specified by Language.
121     EFI_OUT_OF_RESOURCES  - There are not enough resources available to complete
122                             the diagnostics.
123     EFI_OUT_OF_RESOURCES  - There are not enough resources available to return
124                             the status information in ErrorType, BufferSize,
125                             and Buffer.
126     EFI_DEVICE_ERROR      - The controller specified by ControllerHandle and
127                             ChildHandle did not pass the diagnostic.
128 
129 --*/
130 {
131   EFI_STATUS            Status;
132   EFI_BLOCK_IO_PROTOCOL *BlockIo;
133   CHAR8                 *SupportedLanguages;
134   BOOLEAN               Iso639Language;
135   BOOLEAN               Found;
136   UINTN                 Index;
137 
138   if (Language         == NULL ||
139       ErrorType        == NULL ||
140       Buffer           == NULL ||
141       ControllerHandle == NULL ||
142       BufferSize       == NULL) {
143 
144     return EFI_INVALID_PARAMETER;
145   }
146 
147   SupportedLanguages = This->SupportedLanguages;
148   Iso639Language = (BOOLEAN)(This == &gWinNtBlockIoDriverDiagnostics);
149   //
150   // Make sure Language is in the set of Supported Languages
151   //
152   Found = FALSE;
153   while (*SupportedLanguages != 0) {
154     if (Iso639Language) {
155       if (CompareMem (Language, SupportedLanguages, 3) == 0) {
156         Found = TRUE;
157         break;
158       }
159       SupportedLanguages += 3;
160     } else {
161       for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
162       if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {
163         Found = TRUE;
164         break;
165       }
166       SupportedLanguages += Index;
167       for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
168     }
169   }
170   //
171   // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
172   //
173   if (!Found) {
174     return EFI_UNSUPPORTED;
175   }
176 
177   *ErrorType  = NULL;
178   *BufferSize = 0;
179   if (DiagnosticType != EfiDriverDiagnosticTypeStandard) {
180     *ErrorType  = &gEfiBlockIoProtocolGuid;
181     *BufferSize = 0x60;
182     *Buffer = AllocatePool ((UINTN) (*BufferSize));
183     CopyMem (*Buffer, L"Windows Block I/O Driver Diagnostics Failed\n", *BufferSize);
184     return EFI_DEVICE_ERROR;
185   }
186 
187   //
188   // This is a device driver, so ChildHandle must be NULL.
189   //
190   if (ChildHandle != NULL) {
191     return EFI_UNSUPPORTED;
192   }
193 
194   //
195   // Validate controller handle
196   //
197   Status = gBS->OpenProtocol (
198                   ControllerHandle,
199                   &gEfiWinNtIoProtocolGuid,
200                   (VOID **) &BlockIo,
201                   gWinNtBlockIoDriverBinding.DriverBindingHandle,
202                   ControllerHandle,
203                   EFI_OPEN_PROTOCOL_BY_DRIVER
204                   );
205 
206   if (!EFI_ERROR (Status)) {
207     gBS->CloseProtocol (
208           ControllerHandle,
209           &gEfiWinNtIoProtocolGuid,
210           gWinNtBlockIoDriverBinding.DriverBindingHandle,
211           ControllerHandle
212           );
213 
214     return EFI_UNSUPPORTED;
215   }
216 
217 
218   if (Status == EFI_UNSUPPORTED) {
219     return Status;
220   } else if (Status != EFI_ALREADY_STARTED) {
221     return EFI_INVALID_PARAMETER;
222   }
223 
224   return EFI_SUCCESS;
225 }
226