BaseTools: Add DevicePath support for PCD values
[mirror_edk2.git] / BaseTools / Source / C / DevicePath / DevicePath.c
1 /** @file
2 Definition for Device Path Tool.
3
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UefiDevicePathLib.h"
16
17 //
18 // Utility Name
19 //
20 #define UTILITY_NAME "DevicePath"
21
22 //
23 // Utility version information
24 //
25 #define UTILITY_MAJOR_VERSION 0
26 #define UTILITY_MINOR_VERSION 1
27
28 EFI_GUID gEfiDebugPortDevicePathGuid = DEVICE_PATH_MESSAGING_DEBUGPORT;
29 EFI_GUID gEfiPcAnsiGuid = EFI_PC_ANSI_GUID;
30 EFI_GUID gEfiVT100Guid = EFI_VT_100_GUID;
31 EFI_GUID gEfiVT100PlusGuid = EFI_VT_100_PLUS_GUID;
32 EFI_GUID gEfiVTUTF8Guid = EFI_VT_UTF8_GUID;
33 EFI_GUID gEfiUartDevicePathGuid = EFI_UART_DEVICE_PATH_GUID;
34 EFI_GUID gEfiSasDevicePathGuid = EFI_SAS_DEVICE_PATH_GUID;
35 EFI_GUID gEfiVirtualDiskGuid = EFI_VIRTUAL_DISK_GUID;
36 EFI_GUID gEfiVirtualCdGuid = EFI_VIRTUAL_CD_GUID;
37 EFI_GUID gEfiPersistentVirtualDiskGuid = EFI_PERSISTENT_VIRTUAL_DISK_GUID;
38 EFI_GUID gEfiPersistentVirtualCdGuid = EFI_PERSISTENT_VIRTUAL_CD_GUID;
39
40 STATIC
41 VOID
42 Version (
43 VOID
44 )
45 /*++
46
47 Routine Description:
48
49 Displays the standard utility information to SDTOUT
50
51 Arguments:
52
53 None
54
55 Returns:
56
57 None
58
59 --*/
60 {
61 fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
62 }
63
64 STATIC
65 VOID
66 Usage (
67 VOID
68 )
69 /*++
70
71 Routine Description:
72
73 Displays the utility usage syntax to STDOUT
74
75 Arguments:
76
77 None
78
79 Returns:
80
81 None
82
83 --*/
84 {
85 //
86 // Summary usage
87 //
88 fprintf (stdout, "\nUsage: %s [options]\n\n", UTILITY_NAME);
89
90 //
91 // Copyright declaration
92 //
93 fprintf (stdout, "Copyright (c) 2017, Intel Corporation. All rights reserved.\n\n");
94 //
95 // Details Option
96 //
97 fprintf (stdout, "Options:\n");
98 fprintf (stdout, " DevicePathString Device Path string is specified, no space character.\n"
99 " Example: \"PciRoot(0)/Pci(0,0)\"\n");
100
101 fprintf (stdout, " --version Show program's version number and exit.\n");
102 fprintf (stdout, " -h, --help Show this help message and exit.\n");
103 }
104
105
106 void print_mem(void const *vp, size_t n)
107 {
108 unsigned char const *p = vp;
109 for (size_t i=0; i<n; i++) {
110 printf("0x%02x ", p[i]);
111 }
112 }
113
114 VOID
115 Ascii2UnicodeString (
116 CHAR8 *String,
117 CHAR16 *UniString
118 )
119 /*++
120
121 Routine Description:
122
123 Write ascii string as unicode string format to FILE
124
125 Arguments:
126
127 String - Pointer to string that is written to FILE.
128 UniString - Pointer to unicode string
129
130 Returns:
131
132 NULL
133
134 --*/
135 {
136 while (*String != '\0') {
137 *(UniString++) = (CHAR16) *(String++);
138 }
139 //
140 // End the UniString with a NULL.
141 //
142 *UniString = '\0';
143 }
144
145 int main(int argc, CHAR8 *argv[])
146 {
147 CHAR8 * Str;
148 CHAR16* Str16;
149 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
150
151 if (argc == 1) {
152 Error (NULL, 0, 1001, "Missing options", "No input options specified.");
153 Usage ();
154 return STATUS_ERROR;
155 }
156 if ((stricmp (argv[1], "-h") == 0) || (stricmp (argv[1], "--help") == 0)) {
157 Version ();
158 Usage ();
159 return STATUS_SUCCESS;
160 }
161
162 if (stricmp (argv[1], "--version") == 0) {
163 Version ();
164 return STATUS_SUCCESS;
165 }
166 Str = argv[1];
167 if (Str == NULL) {
168 fprintf(stderr, "Invalid option value, Device Path can't be NULL");
169 return STATUS_ERROR;
170 }
171 Str16 = (CHAR16 *)malloc(1024);
172 if (Str16 == NULL) {
173 fprintf(stderr, "Resource, memory cannot be allcoated");
174 return STATUS_ERROR;
175 }
176 Ascii2UnicodeString(Str, Str16);
177 DevicePath = UefiDevicePathLibConvertTextToDevicePath(Str16);
178 while (!((DevicePath->Type == END_DEVICE_PATH_TYPE) && (DevicePath->SubType == END_ENTIRE_DEVICE_PATH_SUBTYPE)) )
179 {
180 print_mem(DevicePath, (DevicePath->Length[0] | DevicePath->Length[1] << 8));
181 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + (DevicePath->Length[0] | DevicePath->Length[1] << 8));
182 }
183 print_mem(DevicePath, (DevicePath->Length[0] | DevicePath->Length[1] << 8));
184 putchar('\n');
185 return STATUS_SUCCESS;
186 }