TianoCore EDK2 master
Loading...
Searching...
No Matches
SerMode.c
Go to the documentation of this file.
1
11#include <Library/ShellLib.h>
12#include <Protocol/SerialIo.h>
13
27 IN UINTN HandleIdx,
28 IN BOOLEAN HandleValid
29 )
30{
31 EFI_SERIAL_IO_PROTOCOL *SerialIo;
32 UINTN NoHandles;
33 EFI_HANDLE *Handles;
34 EFI_STATUS Status;
35 UINTN Index;
36 CHAR16 *StopBits;
37 CHAR16 Parity;
38 SHELL_STATUS ShellStatus;
39
40 Handles = NULL;
41 StopBits = NULL;
42
43 ShellStatus = SHELL_SUCCESS;
44
45 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSerialIoProtocolGuid, NULL, &NoHandles, &Handles);
46 if (EFI_ERROR (Status)) {
47 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_NO_FOUND), gShellDebug1HiiHandle, L"sermode");
49 }
50
51 for (Index = 0; Index < NoHandles; Index++) {
52 if (HandleValid) {
53 if (ConvertHandleIndexToHandle (HandleIdx) != Handles[Index]) {
54 continue;
55 }
56 }
57
58 Status = gBS->HandleProtocol (Handles[Index], &gEfiSerialIoProtocolGuid, (VOID **)&SerialIo);
59 if (!EFI_ERROR (Status)) {
60 switch (SerialIo->Mode->Parity) {
61 case DefaultParity:
62
63 Parity = 'D';
64 break;
65
66 case NoParity:
67
68 Parity = 'N';
69 break;
70
71 case EvenParity:
72
73 Parity = 'E';
74 break;
75
76 case OddParity:
77
78 Parity = 'O';
79 break;
80
81 case MarkParity:
82
83 Parity = 'M';
84 break;
85
86 case SpaceParity:
87
88 Parity = 'S';
89 break;
90
91 default:
92
93 Parity = 'U';
94 }
95
96 switch (SerialIo->Mode->StopBits) {
97 case DefaultStopBits:
98
99 StopBits = L"Default";
100 break;
101
102 case OneStopBit:
103
104 StopBits = L"1";
105 break;
106
107 case TwoStopBits:
108
109 StopBits = L"2";
110 break;
111
112 case OneFiveStopBits:
113
114 StopBits = L"1.5";
115 break;
116
117 default:
118
119 StopBits = L"Unknown";
120 }
121
123 -1,
124 -1,
125 NULL,
126 STRING_TOKEN (STR_SERMODE_DISPLAY),
127 gShellDebug1HiiHandle,
128 ConvertHandleToHandleIndex (Handles[Index]),
129 Handles[Index],
130 SerialIo->Mode->BaudRate,
131 Parity,
132 SerialIo->Mode->DataBits,
133 StopBits
134 );
135 } else {
136 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_NO_FOUND), gShellDebug1HiiHandle, L"sermode");
137 ShellStatus = SHELL_NOT_FOUND;
138 break;
139 }
140
141 if (HandleValid) {
142 break;
143 }
144 }
145
146 if (Index == NoHandles) {
147 if (((NoHandles != 0) && HandleValid) || (0 == NoHandles)) {
148 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_NOT_FOUND), gShellDebug1HiiHandle, L"sermode");
149 ShellStatus = SHELL_NOT_FOUND;
150 }
151 }
152
153 return ShellStatus;
154}
155
163EFIAPI
165 IN EFI_HANDLE ImageHandle,
166 IN EFI_SYSTEM_TABLE *SystemTable
167 )
168{
169 EFI_STATUS Status;
170 SHELL_STATUS ShellStatus;
171 UINTN Index;
172 UINTN NoHandles;
173 EFI_HANDLE *Handles;
174 EFI_PARITY_TYPE Parity;
175 EFI_STOP_BITS_TYPE StopBits;
176 UINTN HandleIdx;
177 UINTN BaudRate;
178 UINTN DataBits;
179 UINTN Value;
180 EFI_SERIAL_IO_PROTOCOL *SerialIo;
181 LIST_ENTRY *Package;
182 CHAR16 *ProblemParam;
183 CONST CHAR16 *Temp;
184 UINT64 Intermediate;
185
186 ShellStatus = SHELL_SUCCESS;
187 HandleIdx = 0;
188 Parity = DefaultParity;
189 Handles = NULL;
190 NoHandles = 0;
191 Index = 0;
192 Package = NULL;
193
194 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
195 if (EFI_ERROR (Status)) {
196 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
197 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"sermode", ProblemParam);
198 FreePool (ProblemParam);
199 ShellStatus = SHELL_INVALID_PARAMETER;
200 } else {
201 ASSERT (FALSE);
202 }
203 } else {
204 if ((ShellCommandLineGetCount (Package) < 6) && (ShellCommandLineGetCount (Package) > 2)) {
205 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"sermode");
206 ShellStatus = SHELL_INVALID_PARAMETER;
207 } else if (ShellCommandLineGetCount (Package) > 6) {
208 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"sermode");
209 ShellStatus = SHELL_INVALID_PARAMETER;
210 } else {
211 Temp = ShellCommandLineGetRawValue (Package, 1);
212 if (Temp != NULL) {
213 Status = ShellConvertStringToUint64 (Temp, &Intermediate, TRUE, FALSE);
214 HandleIdx = (UINTN)Intermediate;
215 Temp = ShellCommandLineGetRawValue (Package, 2);
216 if (Temp == NULL) {
217 ShellStatus = DisplaySettings (HandleIdx, TRUE);
218 goto Done;
219 }
220 } else {
221 ShellStatus = DisplaySettings (0, FALSE);
222 goto Done;
223 }
224
225 Temp = ShellCommandLineGetRawValue (Package, 2);
226 if (Temp != NULL) {
227 BaudRate = ShellStrToUintn (Temp);
228 } else {
229 ASSERT (FALSE);
230 BaudRate = 0;
231 }
232
233 Temp = ShellCommandLineGetRawValue (Package, 3);
234 if ((Temp == NULL) || (StrLen (Temp) > 1)) {
235 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"sermode", Temp);
236 ShellStatus = SHELL_INVALID_PARAMETER;
237 } else {
238 switch (Temp[0]) {
239 case 'd':
240 case 'D':
241 Parity = DefaultParity;
242 break;
243 case 'n':
244 case 'N':
245 Parity = NoParity;
246 break;
247 case 'e':
248 case 'E':
249 Parity = EvenParity;
250 break;
251 case 'o':
252 case 'O':
253 Parity = OddParity;
254 break;
255 case 'm':
256 case 'M':
257 Parity = MarkParity;
258 break;
259 case 's':
260 case 'S':
261 Parity = SpaceParity;
262 break;
263 default:
264 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"sermode", Temp);
265 ShellStatus = SHELL_INVALID_PARAMETER;
266 goto Done;
267 }
268 }
269
270 Temp = ShellCommandLineGetRawValue (Package, 4);
271 if (Temp != NULL) {
272 DataBits = ShellStrToUintn (Temp);
273 } else {
274 //
275 // make sure this is some number not in the list below.
276 //
277 DataBits = 0;
278 }
279
280 switch (DataBits) {
281 case 4:
282 case 7:
283 case 8:
284 break;
285 default:
286 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"sermode", Temp);
287 ShellStatus = SHELL_INVALID_PARAMETER;
288 goto Done;
289 }
290
291 Temp = ShellCommandLineGetRawValue (Package, 5);
292 if (Temp == NULL) {
293 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"sermode");
294 ShellStatus = SHELL_INVALID_PARAMETER;
295 goto Done;
296 }
297
298 Value = ShellStrToUintn (Temp);
299 switch (Value) {
300 case 0:
301 StopBits = DefaultStopBits;
302 break;
303
304 case 1:
305 StopBits = OneStopBit;
306 break;
307
308 case 2:
309 StopBits = TwoStopBits;
310 break;
311
312 case 15:
313 StopBits = OneFiveStopBits;
314 break;
315
316 default:
317 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"sermode", Temp);
318 ShellStatus = SHELL_INVALID_PARAMETER;
319 goto Done;
320 }
321
322 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSerialIoProtocolGuid, NULL, &NoHandles, &Handles);
323 if (EFI_ERROR (Status)) {
324 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_NO_FOUND), gShellDebug1HiiHandle, L"sermode");
325 ShellStatus = SHELL_INVALID_PARAMETER;
326 goto Done;
327 }
328
329 for (Index = 0; Index < NoHandles; Index++) {
330 if (ConvertHandleIndexToHandle (HandleIdx) != Handles[Index]) {
331 continue;
332 }
333
334 Status = gBS->HandleProtocol (Handles[Index], &gEfiSerialIoProtocolGuid, (VOID **)&SerialIo);
335 if (!EFI_ERROR (Status)) {
336 Status = SerialIo->SetAttributes (
337 SerialIo,
338 (UINT64)BaudRate,
339 SerialIo->Mode->ReceiveFifoDepth,
340 SerialIo->Mode->Timeout,
341 Parity,
342 (UINT8)DataBits,
343 StopBits
344 );
345 if (EFI_ERROR (Status)) {
346 if (Status == EFI_INVALID_PARAMETER) {
347 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_SET_UNSUPPORTED), gShellDebug1HiiHandle, L"sermode", ConvertHandleToHandleIndex (Handles[Index]));
348 ShellStatus = SHELL_UNSUPPORTED;
349 } else if (Status == EFI_DEVICE_ERROR) {
350 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_SET_DEV_ERROR), gShellDebug1HiiHandle, L"sermode", ConvertHandleToHandleIndex (Handles[Index]));
351 ShellStatus = SHELL_ACCESS_DENIED;
352 } else {
353 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_SET_FAIL), gShellDebug1HiiHandle, L"sermode", ConvertHandleToHandleIndex (Handles[Index]));
354 ShellStatus = SHELL_ACCESS_DENIED;
355 }
356 } else {
357 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_SET_HANDLE), gShellDebug1HiiHandle, ConvertHandleToHandleIndex (Handles[Index]));
358 }
359
360 break;
361 }
362 }
363 }
364 }
365
366 if ((ShellStatus == SHELL_SUCCESS) && (Index == NoHandles)) {
367 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SERMODE_BAD_HANDLE), gShellDebug1HiiHandle, L"sermode", HandleIdx);
368 ShellStatus = SHELL_INVALID_PARAMETER;
369 }
370
371Done:
372 if (Package != NULL) {
374 }
375
376 if (Handles != NULL) {
377 FreePool (Handles);
378 }
379
380 return ShellStatus;
381}
UINT64 UINTN
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_HANDLE EFIAPI ConvertHandleIndexToHandle(IN CONST UINTN TheIndex)
UINTN EFIAPI ConvertHandleToHandleIndex(IN CONST EFI_HANDLE TheHandle)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_ACCESS_DENIED
Definition: Shell.h:106
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_UNSUPPORTED
Definition: Shell.h:40
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
SHELL_STATUS DisplaySettings(IN UINTN HandleIdx, IN BOOLEAN HandleValid)
Definition: SerMode.c:26
SHELL_STATUS EFIAPI ShellCommandRunSerMode(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: SerMode.c:164
EFI_STOP_BITS_TYPE
Definition: SerialIo.h:53
EFI_PARITY_TYPE
Definition: SerialIo.h:41
UINTN EFIAPI ShellStrToUintn(IN CONST CHAR16 *String)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellConvertStringToUint64(IN CONST CHAR16 *String, OUT UINT64 *Value, IN CONST BOOLEAN ForceHex, IN CONST BOOLEAN StopAtSpace)
SHELL_PARAM_ITEM EmptyParamList[]
Helper structure for no parameters (besides -? and -b)
Definition: UefiShellLib.c:19
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)
@ ByProtocol
Definition: UefiSpec.h:1518