DriverDiagnostics.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**@file
  2. Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. Module Name:
  5. DriverDiagnostics.c
  6. Abstract:
  7. **/
  8. #include "EmuBlockIo.h"
  9. //
  10. // EFI Driver Diagnostics Functions
  11. //
  12. EFI_STATUS
  13. EFIAPI
  14. EmuBlockIoDriverDiagnosticsRunDiagnostics (
  15. IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *This,
  16. IN EFI_HANDLE ControllerHandle,
  17. IN EFI_HANDLE ChildHandle OPTIONAL,
  18. IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,
  19. IN CHAR8 *Language,
  20. OUT EFI_GUID **ErrorType,
  21. OUT UINTN *BufferSize,
  22. OUT CHAR16 **Buffer
  23. );
  24. //
  25. // EFI Driver Diagnostics Protocol
  26. //
  27. EFI_DRIVER_DIAGNOSTICS_PROTOCOL gEmuBlockIoDriverDiagnostics = {
  28. EmuBlockIoDriverDiagnosticsRunDiagnostics,
  29. "eng"
  30. };
  31. //
  32. // EFI Driver Diagnostics 2 Protocol
  33. //
  34. GLOBAL_REMOVE_IF_UNREFERENCED EFI_DRIVER_DIAGNOSTICS2_PROTOCOL gEmuBlockIoDriverDiagnostics2 = {
  35. (EFI_DRIVER_DIAGNOSTICS2_RUN_DIAGNOSTICS)EmuBlockIoDriverDiagnosticsRunDiagnostics,
  36. "en"
  37. };
  38. EFI_STATUS
  39. EFIAPI
  40. EmuBlockIoDriverDiagnosticsRunDiagnostics (
  41. IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *This,
  42. IN EFI_HANDLE ControllerHandle,
  43. IN EFI_HANDLE ChildHandle OPTIONAL,
  44. IN EFI_DRIVER_DIAGNOSTIC_TYPE DiagnosticType,
  45. IN CHAR8 *Language,
  46. OUT EFI_GUID **ErrorType,
  47. OUT UINTN *BufferSize,
  48. OUT CHAR16 **Buffer
  49. )
  50. /*++
  51. Routine Description:
  52. Runs diagnostics on a controller.
  53. Arguments:
  54. This - A pointer to the EFI_DRIVER_DIAGNOSTICS_PROTOCOL instance.
  55. ControllerHandle - The handle of the controller to run diagnostics on.
  56. ChildHandle - The handle of the child controller to run diagnostics on
  57. This is an optional parameter that may be NULL. It will
  58. be NULL for device drivers. It will also be NULL for a
  59. bus drivers that wish to run diagnostics on the bus
  60. controller. It will not be NULL for a bus driver that
  61. wishes to run diagnostics on one of its child controllers.
  62. DiagnosticType - Indicates type of diagnostics to perform on the controller
  63. specified by ControllerHandle and ChildHandle. See
  64. "Related Definitions" for the list of supported types.
  65. Language - A pointer to a three character ISO 639-2 language
  66. identifier or a Null-terminated ASCII string array indicating
  67. the language. This is the language in which the optional
  68. error message should be returned in Buffer, and it must
  69. match one of the languages specified in SupportedLanguages.
  70. The number of languages supported by a driver is up to
  71. the driver writer.
  72. ErrorType - A GUID that defines the format of the data returned in
  73. Buffer.
  74. BufferSize - The size, in bytes, of the data returned in Buffer.
  75. Buffer - A buffer that contains a Null-terminated Unicode string
  76. plus some additional data whose format is defined by
  77. ErrorType. Buffer is allocated by this function with
  78. AllocatePool(), and it is the caller's responsibility
  79. to free it with a call to FreePool().
  80. Returns:
  81. EFI_SUCCESS - The controller specified by ControllerHandle and
  82. ChildHandle passed the diagnostic.
  83. EFI_INVALID_PARAMETER - ControllerHandle is not a valid EFI_HANDLE.
  84. EFI_INVALID_PARAMETER - ChildHandle is not NULL and it is not a valid
  85. EFI_HANDLE.
  86. EFI_INVALID_PARAMETER - Language is NULL.
  87. EFI_INVALID_PARAMETER - ErrorType is NULL.
  88. EFI_INVALID_PARAMETER - BufferType is NULL.
  89. EFI_INVALID_PARAMETER - Buffer is NULL.
  90. EFI_UNSUPPORTED - The driver specified by This does not support
  91. running diagnostics for the controller specified
  92. by ControllerHandle and ChildHandle.
  93. EFI_UNSUPPORTED - The driver specified by This does not support the
  94. type of diagnostic specified by DiagnosticType.
  95. EFI_UNSUPPORTED - The driver specified by This does not support the
  96. language specified by Language.
  97. EFI_OUT_OF_RESOURCES - There are not enough resources available to complete
  98. the diagnostics.
  99. EFI_OUT_OF_RESOURCES - There are not enough resources available to return
  100. the status information in ErrorType, BufferSize,
  101. and Buffer.
  102. EFI_DEVICE_ERROR - The controller specified by ControllerHandle and
  103. ChildHandle did not pass the diagnostic.
  104. --*/
  105. {
  106. EFI_STATUS Status;
  107. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  108. CHAR8 *SupportedLanguages;
  109. BOOLEAN Iso639Language;
  110. BOOLEAN Found;
  111. UINTN Index;
  112. if ((Language == NULL) ||
  113. (ErrorType == NULL) ||
  114. (Buffer == NULL) ||
  115. (ControllerHandle == NULL) ||
  116. (BufferSize == NULL))
  117. {
  118. return EFI_INVALID_PARAMETER;
  119. }
  120. SupportedLanguages = This->SupportedLanguages;
  121. Iso639Language = (BOOLEAN)(This == &gEmuBlockIoDriverDiagnostics);
  122. //
  123. // Make sure Language is in the set of Supported Languages
  124. //
  125. Found = FALSE;
  126. while (*SupportedLanguages != 0) {
  127. if (Iso639Language) {
  128. if (CompareMem (Language, SupportedLanguages, 3) == 0) {
  129. Found = TRUE;
  130. break;
  131. }
  132. SupportedLanguages += 3;
  133. } else {
  134. for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++) {
  135. }
  136. if ((AsciiStrnCmp (SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {
  137. Found = TRUE;
  138. break;
  139. }
  140. SupportedLanguages += Index;
  141. for ( ; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++) {
  142. }
  143. }
  144. }
  145. //
  146. // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
  147. //
  148. if (!Found) {
  149. return EFI_UNSUPPORTED;
  150. }
  151. *ErrorType = NULL;
  152. *BufferSize = 0;
  153. if (DiagnosticType != EfiDriverDiagnosticTypeStandard) {
  154. *ErrorType = &gEfiBlockIoProtocolGuid;
  155. *BufferSize = 0x60;
  156. Buffer = AllocatePool ((UINTN)(*BufferSize));
  157. CopyMem (*Buffer, L"Windows Block I/O Driver Diagnostics Failed\n", *BufferSize);
  158. return EFI_DEVICE_ERROR;
  159. }
  160. //
  161. // This is a device driver, so ChildHandle must be NULL.
  162. //
  163. if (ChildHandle != NULL) {
  164. return EFI_UNSUPPORTED;
  165. }
  166. //
  167. // Validate controller handle
  168. //
  169. Status = gBS->OpenProtocol (
  170. ControllerHandle,
  171. &gEmuIoThunkProtocolGuid,
  172. (VOID **)&BlockIo,
  173. gEmuBlockIoDriverBinding.DriverBindingHandle,
  174. ControllerHandle,
  175. EFI_OPEN_PROTOCOL_BY_DRIVER
  176. );
  177. if (!EFI_ERROR (Status)) {
  178. gBS->CloseProtocol (
  179. ControllerHandle,
  180. &gEmuIoThunkProtocolGuid,
  181. gEmuBlockIoDriverBinding.DriverBindingHandle,
  182. ControllerHandle
  183. );
  184. return EFI_UNSUPPORTED;
  185. }
  186. if (Status == EFI_UNSUPPORTED) {
  187. return Status;
  188. } else if (Status != EFI_ALREADY_STARTED) {
  189. return EFI_INVALID_PARAMETER;
  190. }
  191. return EFI_SUCCESS;
  192. }