Add WinNtBlockIoDxe module into Nt32Pkg.
[mirror_edk2.git] / Nt32Pkg / WinNtBlockIoDxe / DriverDiagnostics.c
1 /*++
2
3 Copyright (c) 2006 - 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 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 EFI_DRIVER_DIAGNOSTICS_PROTOCOL gWinNtBlockIoDriverDiagnostics = {
47 WinNtBlockIoDriverDiagnosticsRunDiagnostics,
48 LANGUAGESUPPORTED
49 };
50
51 EFI_STATUS
52 EFIAPI
53 WinNtBlockIoDriverDiagnosticsRunDiagnostics (
54 IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *This,
55 IN EFI_HANDLE ControllerHandle,
56 IN EFI_HANDLE ChildHandle OPTIONAL,
57 IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,
58 IN CHAR8 *Language,
59 OUT EFI_GUID **ErrorType,
60 OUT UINTN *BufferSize,
61 OUT CHAR16 **Buffer
62 )
63 /*++
64
65 Routine Description:
66 Runs diagnostics on a controller.
67
68 Arguments:
69 This - A pointer to the EFI_DRIVER_DIAGNOSTICS_PROTOCOL instance.
70 ControllerHandle - The handle of the controller to run diagnostics on.
71 ChildHandle - The handle of the child controller to run diagnostics on
72 This is an optional parameter that may be NULL. It will
73 be NULL for device drivers. It will also be NULL for a
74 bus drivers that wish to run diagnostics on the bus
75 controller. It will not be NULL for a bus driver that
76 wishes to run diagnostics on one of its child controllers.
77 DiagnosticType - Indicates type of diagnostics to perform on the controller
78 specified by ControllerHandle and ChildHandle. See
79 "Related Definitions" for the list of supported types.
80 Language - A pointer to a three character ISO 639-2 language
81 identifier. This is the language in which the optional
82 error message should be returned in Buffer, and it must
83 match one of the languages specified in SupportedLanguages.
84 The number of languages supported by a driver is up to
85 the driver writer.
86 ErrorType - A GUID that defines the format of the data returned in
87 Buffer.
88 BufferSize - The size, in bytes, of the data returned in Buffer.
89 Buffer - A buffer that contains a Null-terminated Unicode string
90 plus some additional data whose format is defined by
91 ErrorType. Buffer is allocated by this function with
92 AllocatePool(), and it is the caller's responsibility
93 to free it with a call to FreePool().
94
95 Returns:
96 EFI_SUCCESS - The controller specified by ControllerHandle and
97 ChildHandle passed the diagnostic.
98 EFI_INVALID_PARAMETER - ControllerHandle is not a valid EFI_HANDLE.
99 EFI_INVALID_PARAMETER - ChildHandle is not NULL and it is not a valid
100 EFI_HANDLE.
101 EFI_INVALID_PARAMETER - Language is NULL.
102 EFI_INVALID_PARAMETER - ErrorType is NULL.
103 EFI_INVALID_PARAMETER - BufferType is NULL.
104 EFI_INVALID_PARAMETER - Buffer is NULL.
105 EFI_UNSUPPORTED - The driver specified by This does not support
106 running diagnostics for the controller specified
107 by ControllerHandle and ChildHandle.
108 EFI_UNSUPPORTED - The driver specified by This does not support the
109 type of diagnostic specified by DiagnosticType.
110 EFI_UNSUPPORTED - The driver specified by This does not support the
111 language specified by Language.
112 EFI_OUT_OF_RESOURCES - There are not enough resources available to complete
113 the diagnostics.
114 EFI_OUT_OF_RESOURCES - There are not enough resources available to return
115 the status information in ErrorType, BufferSize,
116 and Buffer.
117 EFI_DEVICE_ERROR - The controller specified by ControllerHandle and
118 ChildHandle did not pass the diagnostic.
119
120 --*/
121 {
122 EFI_STATUS Status;
123 EFI_BLOCK_IO_PROTOCOL *BlockIo;
124 CHAR8 *SupportedLanguage;
125
126 if (Language == NULL ||
127 ErrorType == NULL ||
128 Buffer == NULL ||
129 ControllerHandle == NULL ||
130 BufferSize == NULL) {
131
132 return EFI_INVALID_PARAMETER;
133 }
134
135 SupportedLanguage = This->SupportedLanguages;
136
137 Status = EFI_UNSUPPORTED;
138 while (*SupportedLanguage != 0) {
139 if (AsciiStrnCmp (Language, SupportedLanguage, 3) == 0) {
140 Status = EFI_SUCCESS;
141 break;
142 }
143
144 SupportedLanguage += 3;
145 }
146
147 if (EFI_ERROR (Status)) {
148 return EFI_UNSUPPORTED;
149 }
150
151 *ErrorType = NULL;
152 *BufferSize = 0;
153 if (DiagnosticType != EfiDriverDiagnosticTypeStandard) {
154 *ErrorType = &gEfiBlockIoProtocolGuid;
155 *BufferSize = 0x60;
156 Buffer = AllocatePool ((UINTN) (*BufferSize));
157 CopyMem (*Buffer, L"Windows Block I/O Driver Diagnostics Failed\n", *BufferSize);
158 return EFI_DEVICE_ERROR;
159 }
160
161 //
162 // Validate controller handle
163 //
164 Status = gBS->OpenProtocol (
165 ControllerHandle,
166 &gEfiWinNtIoProtocolGuid,
167 &BlockIo,
168 gWinNtBlockIoDriverBinding.DriverBindingHandle,
169 ControllerHandle,
170 EFI_OPEN_PROTOCOL_BY_DRIVER
171 );
172
173 if (!EFI_ERROR (Status)) {
174 gBS->CloseProtocol (
175 ControllerHandle,
176 &gEfiWinNtIoProtocolGuid,
177 gWinNtBlockIoDriverBinding.DriverBindingHandle,
178 ControllerHandle
179 );
180
181 return EFI_UNSUPPORTED;
182 }
183
184 if (Status == EFI_UNSUPPORTED) {
185 return Status;
186 } else if (Status != EFI_ALREADY_STARTED) {
187 return EFI_INVALID_PARAMETER;
188 }
189
190 return EFI_SUCCESS;
191 }