]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/IdeBusDxe/DriverDiagnostics.c
e28c93d34633afe474cd9488ab7c953f8a5ec2f2
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / IdeBusDxe / DriverDiagnostics.c
1 /** @file
2 Copyright (c) 2006 - 2008, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 **/
12
13
14 #include "IdeBus.h"
15
16 #define IDE_BUS_DIAGNOSTIC_ERROR L"PCI IDE/ATAPI Driver Diagnostics Failed"
17
18 //
19 // EFI Driver Diagnostics Protocol
20 //
21 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DRIVER_DIAGNOSTICS_PROTOCOL gIDEBusDriverDiagnostics = {
22 IDEBusDriverDiagnosticsRunDiagnostics,
23 "eng"
24 };
25
26 //
27 // EFI Driver Diagnostics 2 Protocol
28 //
29 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DRIVER_DIAGNOSTICS2_PROTOCOL gIDEBusDriverDiagnostics2 = {
30 (EFI_DRIVER_DIAGNOSTICS2_RUN_DIAGNOSTICS) IDEBusDriverDiagnosticsRunDiagnostics,
31 "en"
32 };
33
34 /**
35 Runs diagnostics on a controller.
36
37 @param This A pointer to the EFI_DRIVER_DIAGNOSTICS_PROTOCOL
38 instance.
39 @param ControllerHandle The handle of the controller to run diagnostics on.
40 @param ChildHandle The handle of the child controller to run diagnostics on
41 This is an optional parameter that may be NULL. It will
42 be NULL for device drivers. It will also be NULL for a
43 bus drivers that wish to run diagnostics on the bus
44 controller. It will not be NULL for a bus driver that
45 wishes to run diagnostics on one of its child
46 controllers.
47 @param DiagnosticType Indicates type of diagnostics to perform on the
48 controller specified by ControllerHandle and ChildHandle.
49 See "Related Definitions" for the list of supported
50 types.
51 @param Language A pointer to a three character ISO 639-2 language
52 identifier. This is the language in which the optional
53 error message should be returned in Buffer, and it must
54 match one of the languages specified in
55 SupportedLanguages. The number of languages supported by
56 a driver is up to the driver writer.
57 @param ErrorType A GUID that defines the format of the data returned in
58 Buffer.
59 @param BufferSize The size, in bytes, of the data returned in Buffer.
60 @param Buffer A buffer that contains a Null-terminated Unicode string
61 plus some additional data whose format is defined by
62 ErrorType. Buffer is allocated by this function with
63 AllocatePool(), and it is the caller's responsibility
64 to free it with a call to FreePool().
65
66 @retval EFI_SUCCESS The controller specified by ControllerHandle and
67 ChildHandle passed the diagnostic.
68 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
69 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
70 EFI_HANDLE.
71 @retval EFI_INVALID_PARAMETER Language is NULL.
72 @retval EFI_INVALID_PARAMETER ErrorType is NULL.
73 @retval EFI_INVALID_PARAMETER BufferType is NULL.
74 @retval EFI_INVALID_PARAMETER Buffer is NULL.
75 @retval EFI_UNSUPPORTED The driver specified by This does not support
76 running diagnostics for the controller specified
77 by ControllerHandle and ChildHandle.
78 @retval EFI_UNSUPPORTED The driver specified by This does not support the
79 type of diagnostic specified by DiagnosticType.
80 @retval EFI_UNSUPPORTED The driver specified by This does not support the
81 language specified by Language.
82 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to complete
83 the diagnostics.
84 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to return
85 the status information in ErrorType, BufferSize,
86 and Buffer.
87 @retval EFI_DEVICE_ERROR The controller specified by ControllerHandle and
88 ChildHandle did not pass the diagnostic.
89
90 **/
91 EFI_STATUS
92 IDEBusDriverDiagnosticsRunDiagnostics (
93 IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *This,
94 IN EFI_HANDLE ControllerHandle,
95 IN EFI_HANDLE ChildHandle OPTIONAL,
96 IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,
97 IN CHAR8 *Language,
98 OUT EFI_GUID **ErrorType,
99 OUT UINTN *BufferSize,
100 OUT CHAR16 **Buffer
101 )
102 {
103 EFI_STATUS Status;
104 EFI_PCI_IO_PROTOCOL *PciIo;
105 EFI_BLOCK_IO_PROTOCOL *BlkIo;
106 IDE_BLK_IO_DEV *IdeBlkIoDevice;
107 UINT32 VendorDeviceId;
108 VOID *BlockBuffer;
109 CHAR8 *SupportedLanguages;
110 BOOLEAN Iso639Language;
111 BOOLEAN Found;
112 UINTN Index;
113
114 if (Language == NULL ||
115 ErrorType == NULL ||
116 Buffer == NULL ||
117 ControllerHandle == NULL ||
118 BufferSize == NULL) {
119
120 return EFI_INVALID_PARAMETER;
121 }
122
123 SupportedLanguages = This->SupportedLanguages;
124 Iso639Language = (BOOLEAN)(This == &gIDEBusDriverDiagnostics);
125 //
126 // Make sure Language is in the set of Supported Languages
127 //
128 Found = FALSE;
129 while (*SupportedLanguages != 0) {
130 if (Iso639Language) {
131 if (CompareMem (Language, SupportedLanguages, 3) == 0) {
132 Found = TRUE;
133 break;
134 }
135 SupportedLanguages += 3;
136 } else {
137 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
138 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {
139 Found = TRUE;
140 break;
141 }
142 SupportedLanguages += Index;
143 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
144 }
145 }
146 //
147 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
148 //
149 if (!Found) {
150 return EFI_UNSUPPORTED;
151 }
152
153 *ErrorType = NULL;
154 *BufferSize = 0;
155
156 if (ChildHandle == NULL) {
157 Status = gBS->OpenProtocol (
158 ControllerHandle,
159 &gEfiCallerIdGuid,
160 NULL,
161 gIDEBusDriverBinding.DriverBindingHandle,
162 ControllerHandle,
163 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
164 );
165 if (EFI_ERROR (Status)) {
166 return Status;
167 }
168
169 Status = gBS->OpenProtocol (
170 ControllerHandle,
171 &gEfiPciIoProtocolGuid,
172 (VOID **) &PciIo,
173 gIDEBusDriverBinding.DriverBindingHandle,
174 ControllerHandle,
175 EFI_OPEN_PROTOCOL_GET_PROTOCOL
176 );
177 if (EFI_ERROR (Status)) {
178 return EFI_DEVICE_ERROR;
179 }
180
181 //
182 // Use services of PCI I/O Protocol to test the PCI IDE/ATAPI Controller
183 // The following test simply reads the Device ID and Vendor ID.
184 // It should never fail. A real test would perform more advanced
185 // diagnostics.
186 //
187
188 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0, 1, &VendorDeviceId);
189 if (EFI_ERROR (Status) || VendorDeviceId == 0xffffffff) {
190 return EFI_DEVICE_ERROR;
191 }
192
193 return EFI_SUCCESS;
194 }
195
196 Status = gBS->OpenProtocol (
197 ChildHandle,
198 &gEfiBlockIoProtocolGuid,
199 (VOID **) &BlkIo,
200 gIDEBusDriverBinding.DriverBindingHandle,
201 ChildHandle,
202 EFI_OPEN_PROTOCOL_GET_PROTOCOL
203 );
204 if (EFI_ERROR (Status)) {
205 return Status;
206 }
207
208 IdeBlkIoDevice = IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo);
209
210 //
211 // Use services available from IdeBlkIoDevice to test the IDE/ATAPI device
212 //
213 Status = gBS->AllocatePool (
214 EfiBootServicesData,
215 IdeBlkIoDevice->BlkMedia.BlockSize,
216 (VOID **) &BlockBuffer
217 );
218 if (EFI_ERROR (Status)) {
219 return Status;
220 }
221
222 Status = IdeBlkIoDevice->BlkIo.ReadBlocks (
223 &IdeBlkIoDevice->BlkIo,
224 IdeBlkIoDevice->BlkMedia.MediaId,
225 0,
226 IdeBlkIoDevice->BlkMedia.BlockSize,
227 BlockBuffer
228 );
229
230 if (EFI_ERROR (Status)) {
231 *ErrorType = &gEfiCallerIdGuid;
232 *BufferSize = sizeof (IDE_BUS_DIAGNOSTIC_ERROR);
233
234 Status = gBS->AllocatePool (
235 EfiBootServicesData,
236 (UINTN) (*BufferSize),
237 (VOID **) Buffer
238 );
239 if (EFI_ERROR (Status)) {
240 return Status;
241 }
242
243 CopyMem (*Buffer, IDE_BUS_DIAGNOSTIC_ERROR, *BufferSize);
244
245 Status = EFI_DEVICE_ERROR;
246 }
247
248 gBS->FreePool (BlockBuffer);
249
250 return Status;
251 }