DevicePath.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** @file
  2. Definition for Device Path Tool.
  3. Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "UefiDevicePathLib.h"
  7. //
  8. // Utility Name
  9. //
  10. #define UTILITY_NAME "DevicePath"
  11. //
  12. // Utility version information
  13. //
  14. #define UTILITY_MAJOR_VERSION 0
  15. #define UTILITY_MINOR_VERSION 1
  16. EFI_GUID gEfiDebugPortProtocolGuid = DEVICE_PATH_MESSAGING_DEBUGPORT;
  17. EFI_GUID gEfiPcAnsiGuid = EFI_PC_ANSI_GUID;
  18. EFI_GUID gEfiVT100Guid = EFI_VT_100_GUID;
  19. EFI_GUID gEfiVT100PlusGuid = EFI_VT_100_PLUS_GUID;
  20. EFI_GUID gEfiVTUTF8Guid = EFI_VT_UTF8_GUID;
  21. EFI_GUID gEfiUartDevicePathGuid = EFI_UART_DEVICE_PATH_GUID;
  22. EFI_GUID gEfiSasDevicePathGuid = EFI_SAS_DEVICE_PATH_GUID;
  23. EFI_GUID gEfiVirtualDiskGuid = EFI_VIRTUAL_DISK_GUID;
  24. EFI_GUID gEfiVirtualCdGuid = EFI_VIRTUAL_CD_GUID;
  25. EFI_GUID gEfiPersistentVirtualDiskGuid = EFI_PERSISTENT_VIRTUAL_DISK_GUID;
  26. EFI_GUID gEfiPersistentVirtualCdGuid = EFI_PERSISTENT_VIRTUAL_CD_GUID;
  27. STATIC
  28. VOID
  29. Version (
  30. VOID
  31. )
  32. /*++
  33. Routine Description:
  34. Displays the standard utility information to SDTOUT
  35. Arguments:
  36. None
  37. Returns:
  38. None
  39. --*/
  40. {
  41. fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
  42. }
  43. STATIC
  44. VOID
  45. Usage (
  46. VOID
  47. )
  48. /*++
  49. Routine Description:
  50. Displays the utility usage syntax to STDOUT
  51. Arguments:
  52. None
  53. Returns:
  54. None
  55. --*/
  56. {
  57. //
  58. // Summary usage
  59. //
  60. fprintf (stdout, "\nUsage: %s [options]\n\n", UTILITY_NAME);
  61. //
  62. // Copyright declaration
  63. //
  64. fprintf (stdout, "Copyright (c) 2017, Intel Corporation. All rights reserved.\n\n");
  65. //
  66. // Details Option
  67. //
  68. fprintf (stdout, "Options:\n");
  69. fprintf (stdout, " DevicePathString Device Path string is specified, no space character.\n"
  70. " Example: \"PciRoot(0)/Pci(0,0)\"\n");
  71. fprintf (stdout, " --version Show program's version number and exit.\n");
  72. fprintf (stdout, " -h, --help Show this help message and exit.\n");
  73. }
  74. STATIC
  75. VOID
  76. PrintMem (
  77. CONST VOID *Buffer,
  78. UINTN Count
  79. )
  80. {
  81. CONST UINT8 *Bytes;
  82. UINTN Idx;
  83. Bytes = Buffer;
  84. for (Idx = 0; Idx < Count; Idx++) {
  85. printf("0x%02x ", Bytes[Idx]);
  86. }
  87. }
  88. VOID
  89. Ascii2UnicodeString (
  90. CHAR8 *String,
  91. CHAR16 *UniString
  92. )
  93. /*++
  94. Routine Description:
  95. Write ascii string as unicode string format to FILE
  96. Arguments:
  97. String - Pointer to string that is written to FILE.
  98. UniString - Pointer to unicode string
  99. Returns:
  100. NULL
  101. --*/
  102. {
  103. while (*String != '\0') {
  104. *(UniString++) = (CHAR16) *(String++);
  105. }
  106. //
  107. // End the UniString with a NULL.
  108. //
  109. *UniString = '\0';
  110. }
  111. int main(int argc, CHAR8 *argv[])
  112. {
  113. CHAR8 * Str;
  114. CHAR16* Str16;
  115. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  116. if (argc == 1) {
  117. Error (NULL, 0, 1001, "Missing options", "No input options specified.");
  118. Usage ();
  119. return STATUS_ERROR;
  120. }
  121. if ((stricmp (argv[1], "-h") == 0) || (stricmp (argv[1], "--help") == 0)) {
  122. Version ();
  123. Usage ();
  124. return STATUS_SUCCESS;
  125. }
  126. if (stricmp (argv[1], "--version") == 0) {
  127. Version ();
  128. return STATUS_SUCCESS;
  129. }
  130. Str = argv[1];
  131. if (Str == NULL) {
  132. fprintf(stderr, "Invalid option value, Device Path can't be NULL");
  133. return STATUS_ERROR;
  134. }
  135. Str16 = (CHAR16 *)malloc(1024);
  136. if (Str16 == NULL) {
  137. fprintf(stderr, "Resource, memory cannot be allocated");
  138. return STATUS_ERROR;
  139. }
  140. Ascii2UnicodeString(Str, Str16);
  141. DevicePath = UefiDevicePathLibConvertTextToDevicePath(Str16);
  142. if (DevicePath == NULL) {
  143. fprintf(stderr, "Convert fail, Cannot convert text to a device path");
  144. free(Str16);
  145. return STATUS_ERROR;
  146. }
  147. while (!((DevicePath->Type == END_DEVICE_PATH_TYPE) && (DevicePath->SubType == END_ENTIRE_DEVICE_PATH_SUBTYPE)) )
  148. {
  149. PrintMem (DevicePath, DevicePath->Length[0] | DevicePath->Length[1] << 8);
  150. DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + (DevicePath->Length[0] | DevicePath->Length[1] << 8));
  151. }
  152. PrintMem (DevicePath, DevicePath->Length[0] | DevicePath->Length[1] << 8);
  153. putchar('\n');
  154. free(Str16);
  155. return STATUS_SUCCESS;
  156. }