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