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