]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/IdeBusDxe/DriverDiagnostics.c
Code scrub for IdeBusDxe driver and PeiS3Lib.(undergoing)
[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 EFIAPI
93 IDEBusDriverDiagnosticsRunDiagnostics (
94 IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *This,
95 IN EFI_HANDLE ControllerHandle,
96 IN EFI_HANDLE ChildHandle OPTIONAL,
97 IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,
98 IN CHAR8 *Language,
99 OUT EFI_GUID **ErrorType,
100 OUT UINTN *BufferSize,
101 OUT CHAR16 **Buffer
102 )
103 {
104 EFI_STATUS Status;
105 EFI_PCI_IO_PROTOCOL *PciIo;
106 EFI_BLOCK_IO_PROTOCOL *BlkIo;
107 IDE_BLK_IO_DEV *IdeBlkIoDevice;
108 UINT32 VendorDeviceId;
109 VOID *BlockBuffer;
110 CHAR8 *SupportedLanguages;
111 BOOLEAN Iso639Language;
112 BOOLEAN Found;
113 UINTN Index;
114
115 if (Language == NULL ||
116 ErrorType == NULL ||
117 Buffer == NULL ||
118 ControllerHandle == NULL ||
119 BufferSize == NULL) {
120
121 return EFI_INVALID_PARAMETER;
122 }
123
124 SupportedLanguages = This->SupportedLanguages;
125 Iso639Language = (BOOLEAN)(This == &gIDEBusDriverDiagnostics);
126 //
127 // Make sure Language is in the set of Supported Languages
128 //
129 Found = FALSE;
130 while (*SupportedLanguages != 0) {
131 if (Iso639Language) {
132 if (CompareMem (Language, SupportedLanguages, 3) == 0) {
133 Found = TRUE;
134 break;
135 }
136 SupportedLanguages += 3;
137 } else {
138 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
139 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {
140 Found = TRUE;
141 break;
142 }
143 SupportedLanguages += Index;
144 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
145 }
146 }
147 //
148 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
149 //
150 if (!Found) {
151 return EFI_UNSUPPORTED;
152 }
153
154 *ErrorType = NULL;
155 *BufferSize = 0;
156
157 if (ChildHandle == NULL) {
158 Status = gBS->OpenProtocol (
159 ControllerHandle,
160 &gEfiCallerIdGuid,
161 NULL,
162 gIDEBusDriverBinding.DriverBindingHandle,
163 ControllerHandle,
164 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
165 );
166 if (EFI_ERROR (Status)) {
167 return Status;
168 }
169
170 Status = gBS->OpenProtocol (
171 ControllerHandle,
172 &gEfiPciIoProtocolGuid,
173 (VOID **) &PciIo,
174 gIDEBusDriverBinding.DriverBindingHandle,
175 ControllerHandle,
176 EFI_OPEN_PROTOCOL_GET_PROTOCOL
177 );
178 if (EFI_ERROR (Status)) {
179 return EFI_DEVICE_ERROR;
180 }
181
182 //
183 // Use services of PCI I/O Protocol to test the PCI IDE/ATAPI Controller
184 // The following test simply reads the Device ID and Vendor ID.
185 // It should never fail. A real test would perform more advanced
186 // diagnostics.
187 //
188
189 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0, 1, &VendorDeviceId);
190 if (EFI_ERROR (Status) || VendorDeviceId == 0xffffffff) {
191 return EFI_DEVICE_ERROR;
192 }
193
194 return EFI_SUCCESS;
195 }
196
197 Status = gBS->OpenProtocol (
198 ChildHandle,
199 &gEfiBlockIoProtocolGuid,
200 (VOID **) &BlkIo,
201 gIDEBusDriverBinding.DriverBindingHandle,
202 ChildHandle,
203 EFI_OPEN_PROTOCOL_GET_PROTOCOL
204 );
205 if (EFI_ERROR (Status)) {
206 return Status;
207 }
208
209 IdeBlkIoDevice = IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo);
210
211 //
212 // Use services available from IdeBlkIoDevice to test the IDE/ATAPI device
213 //
214 Status = gBS->AllocatePool (
215 EfiBootServicesData,
216 IdeBlkIoDevice->BlkMedia.BlockSize,
217 (VOID **) &BlockBuffer
218 );
219 if (EFI_ERROR (Status)) {
220 return Status;
221 }
222
223 Status = IdeBlkIoDevice->BlkIo.ReadBlocks (
224 &IdeBlkIoDevice->BlkIo,
225 IdeBlkIoDevice->BlkMedia.MediaId,
226 0,
227 IdeBlkIoDevice->BlkMedia.BlockSize,
228 BlockBuffer
229 );
230
231 if (EFI_ERROR (Status)) {
232 *ErrorType = &gEfiCallerIdGuid;
233 *BufferSize = sizeof (IDE_BUS_DIAGNOSTIC_ERROR);
234
235 Status = gBS->AllocatePool (
236 EfiBootServicesData,
237 (UINTN) (*BufferSize),
238 (VOID **) Buffer
239 );
240 if (EFI_ERROR (Status)) {
241 return Status;
242 }
243
244 CopyMem (*Buffer, IDE_BUS_DIAGNOSTIC_ERROR, *BufferSize);
245
246 Status = EFI_DEVICE_ERROR;
247 }
248
249 gBS->FreePool (BlockBuffer);
250
251 return Status;
252 }