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