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