Unix version of EFI emulator
[mirror_edk2.git] / EdkUnixPkg / Dxe / UnixThunk / Bus / BlockIo / DriverDiagnostics.c
1 /*++
2
3 Copyright (c) 2004 - 2005, 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 "UnixBlockIo.h"
21
22 //
23 // EFI Driver Diagnostics Functions
24 //
25 EFI_STATUS
26 EFIAPI
27 UnixBlockIoDriverDiagnosticsRunDiagnostics (
28 IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *This,
29 IN EFI_HANDLE ControllerHandle,
30 IN EFI_HANDLE ChildHandle OPTIONAL,
31 IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,
32 IN CHAR8 *Language,
33 OUT EFI_GUID **ErrorType,
34 OUT UINTN *BufferSize,
35 OUT CHAR16 **Buffer
36 );
37
38 //
39 // EFI Driver Diagnostics Protocol
40 //
41 EFI_DRIVER_DIAGNOSTICS_PROTOCOL gUnixBlockIoDriverDiagnostics = {
42 UnixBlockIoDriverDiagnosticsRunDiagnostics,
43 LANGUAGESUPPORTED
44 };
45
46 EFI_STATUS
47 EFIAPI
48 UnixBlockIoDriverDiagnosticsRunDiagnostics (
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 instance.
65 ControllerHandle - The handle of the controller to run diagnostics on.
66 ChildHandle - The handle of the child controller to run diagnostics on
67 This is an optional parameter that may be NULL. It will
68 be NULL for device drivers. It will also be NULL for a
69 bus drivers that wish to run diagnostics on the bus
70 controller. It will not be NULL for a bus driver that
71 wishes to run diagnostics on one of its child controllers.
72 DiagnosticType - Indicates type of diagnostics to perform on the controller
73 specified by ControllerHandle and ChildHandle. See
74 "Related Definitions" for the list of supported types.
75 Language - A pointer to a three character ISO 639-2 language
76 identifier. This is the language in which the optional
77 error message should be returned in Buffer, and it must
78 match one of the languages specified in SupportedLanguages.
79 The number of languages supported by a driver is up to
80 the driver writer.
81 ErrorType - A GUID that defines the format of the data returned in
82 Buffer.
83 BufferSize - The size, in bytes, of the data returned in Buffer.
84 Buffer - A buffer that contains a Null-terminated Unicode string
85 plus some additional data whose format is defined by
86 ErrorType. Buffer is allocated by this function with
87 AllocatePool(), and it is the caller's responsibility
88 to free it with a call to FreePool().
89
90 Returns:
91 EFI_SUCCESS - The controller specified by ControllerHandle and
92 ChildHandle passed the diagnostic.
93 EFI_INVALID_PARAMETER - ControllerHandle is not a valid EFI_HANDLE.
94 EFI_INVALID_PARAMETER - ChildHandle is not NULL and it is not a valid
95 EFI_HANDLE.
96 EFI_INVALID_PARAMETER - Language is NULL.
97 EFI_INVALID_PARAMETER - ErrorType is NULL.
98 EFI_INVALID_PARAMETER - BufferType is NULL.
99 EFI_INVALID_PARAMETER - Buffer is NULL.
100 EFI_UNSUPPORTED - The driver specified by This does not support
101 running diagnostics for the controller specified
102 by ControllerHandle and ChildHandle.
103 EFI_UNSUPPORTED - The driver specified by This does not support the
104 type of diagnostic specified by DiagnosticType.
105 EFI_UNSUPPORTED - The driver specified by This does not support the
106 language specified by Language.
107 EFI_OUT_OF_RESOURCES - There are not enough resources available to complete
108 the diagnostics.
109 EFI_OUT_OF_RESOURCES - There are not enough resources available to return
110 the status information in ErrorType, BufferSize,
111 and Buffer.
112 EFI_DEVICE_ERROR - The controller specified by ControllerHandle and
113 ChildHandle did not pass the diagnostic.
114
115 --*/
116 {
117 EFI_STATUS Status;
118 EFI_BLOCK_IO_PROTOCOL *BlockIo;
119 CHAR8 *SupportedLanguage;
120
121 if (Language == NULL ||
122 ErrorType == NULL ||
123 Buffer == NULL ||
124 ControllerHandle == NULL ||
125 BufferSize == NULL) {
126
127 return EFI_INVALID_PARAMETER;
128 }
129
130 SupportedLanguage = This->SupportedLanguages;
131
132 Status = EFI_UNSUPPORTED;
133 while (*SupportedLanguage != 0) {
134 if (AsciiStrnCmp (Language, SupportedLanguage, 3)) {
135 Status = EFI_SUCCESS;
136 break;
137 }
138
139 SupportedLanguage += 3;
140 }
141
142 if (EFI_ERROR (Status)) {
143 return EFI_UNSUPPORTED;
144 }
145
146 *ErrorType = NULL;
147 *BufferSize = 0;
148 if (DiagnosticType != EfiDriverDiagnosticTypeStandard) {
149 *ErrorType = &gEfiBlockIoProtocolGuid;
150 *BufferSize = 0x60;
151 gBS->AllocatePool (EfiBootServicesData, (UINTN) (*BufferSize),
152 (void *)Buffer);
153 CopyMem (*Buffer, L"Unix Block I/O Driver Diagnostics Failed\n", *BufferSize);
154 return EFI_DEVICE_ERROR;
155 }
156
157 //
158 // Validate controller handle
159 //
160 Status = gBS->OpenProtocol (
161 ControllerHandle,
162 &gEfiUnixIoProtocolGuid,
163 (void *)&BlockIo,
164 gUnixBlockIoDriverBinding.DriverBindingHandle,
165 ControllerHandle,
166 EFI_OPEN_PROTOCOL_BY_DRIVER
167 );
168
169 if (!EFI_ERROR (Status)) {
170 gBS->CloseProtocol (
171 ControllerHandle,
172 &gEfiUnixIoProtocolGuid,
173 gUnixBlockIoDriverBinding.DriverBindingHandle,
174 ControllerHandle
175 );
176
177 return EFI_UNSUPPORTED;
178 }
179
180 if (Status == EFI_UNSUPPORTED) {
181 return Status;
182 } else if (Status != EFI_ALREADY_STARTED) {
183 return EFI_INVALID_PARAMETER;
184 }
185
186 return EFI_SUCCESS;
187 }