BaseTools/DevicePath: fix GCC build error in print_mem(), and clean it up
[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 STATIC
107 VOID
108 PrintMem (
109 CONST VOID *Buffer,
110 UINTN Count
111 )
112 {
113 CONST UINT8 *Bytes;
114 UINTN Idx;
115
116 Bytes = Buffer;
117 for (Idx = 0; Idx < Count; Idx++) {
118 printf("0x%02x ", Bytes[Idx]);
119 }
120 }
121
122 VOID
123 Ascii2UnicodeString (
124 CHAR8 *String,
125 CHAR16 *UniString
126 )
127 /*++
128
129 Routine Description:
130
131 Write ascii string as unicode string format to FILE
132
133 Arguments:
134
135 String - Pointer to string that is written to FILE.
136 UniString - Pointer to unicode string
137
138 Returns:
139
140 NULL
141
142 --*/
143 {
144 while (*String != '\0') {
145 *(UniString++) = (CHAR16) *(String++);
146 }
147 //
148 // End the UniString with a NULL.
149 //
150 *UniString = '\0';
151 }
152
153 int main(int argc, CHAR8 *argv[])
154 {
155 CHAR8 * Str;
156 CHAR16* Str16;
157 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
158
159 if (argc == 1) {
160 Error (NULL, 0, 1001, "Missing options", "No input options specified.");
161 Usage ();
162 return STATUS_ERROR;
163 }
164 if ((stricmp (argv[1], "-h") == 0) || (stricmp (argv[1], "--help") == 0)) {
165 Version ();
166 Usage ();
167 return STATUS_SUCCESS;
168 }
169
170 if (stricmp (argv[1], "--version") == 0) {
171 Version ();
172 return STATUS_SUCCESS;
173 }
174 Str = argv[1];
175 if (Str == NULL) {
176 fprintf(stderr, "Invalid option value, Device Path can't be NULL");
177 return STATUS_ERROR;
178 }
179 Str16 = (CHAR16 *)malloc(1024);
180 if (Str16 == NULL) {
181 fprintf(stderr, "Resource, memory cannot be allcoated");
182 return STATUS_ERROR;
183 }
184 Ascii2UnicodeString(Str, Str16);
185 DevicePath = UefiDevicePathLibConvertTextToDevicePath(Str16);
186 while (!((DevicePath->Type == END_DEVICE_PATH_TYPE) && (DevicePath->SubType == END_ENTIRE_DEVICE_PATH_SUBTYPE)) )
187 {
188 PrintMem (DevicePath, DevicePath->Length[0] | DevicePath->Length[1] << 8);
189 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + (DevicePath->Length[0] | DevicePath->Length[1] << 8));
190 }
191 PrintMem (DevicePath, DevicePath->Length[0] | DevicePath->Length[1] << 8);
192 putchar('\n');
193 return STATUS_SUCCESS;
194 }