TianoCore EDK2 master
Loading...
Searching...
No Matches
VConfig.c
Go to the documentation of this file.
1
10#include <Uefi.h>
11
12#include <Protocol/VlanConfig.h>
13
15#include <Library/UefiLib.h>
16#include <Library/ShellLib.h>
18#include <Library/HiiLib.h>
21#include <Library/NetLib.h>
22
23//
24// String token ID of VConfig command help message text.
25//
26GLOBAL_REMOVE_IF_UNREFERENCED EFI_STRING_ID mStringVConfigHelpTokenId = STRING_TOKEN (STR_VCONFIG_HELP);
27
28#define INVALID_NIC_INDEX 0xffff
29#define INVALID_VLAN_ID 0xffff
30
31//
32// This is the generated String package data for all .UNI files.
33// This data array is ready to be used as input of HiiAddPackages() to
34// create a packagelist (which contains Form packages, String packages, etc).
35//
36extern UINT8 VConfigStrings[];
37
38EFI_HANDLE mImageHandle = NULL;
39EFI_HII_HANDLE mHiiHandle = NULL;
40
41SHELL_PARAM_ITEM mParamList[] = {
42 {
43 L"-l",
45 },
46 {
47 L"-a",
49 },
50 {
51 L"-d",
53 },
54 {
55 NULL,
56 TypeMax
57 }
58};
59
67VOID
69 OUT UINTN *NumberOfHandles,
70 OUT EFI_HANDLE **HandleBuffer
71 )
72{
73 EFI_STATUS Status;
74
75 *NumberOfHandles = 0;
76 *HandleBuffer = NULL;
77
78 Status = gBS->LocateHandleBuffer (
80 &gEfiVlanConfigProtocolGuid,
81 NULL,
82 NumberOfHandles,
83 HandleBuffer
84 );
85 if (EFI_ERROR (Status)) {
86 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_LOCATE_FAIL), mHiiHandle, Status);
87 }
88}
89
101 IN CHAR16 *Name
102 )
103{
104 CHAR16 *Str;
105
106 Str = Name + 3;
107 if ((StrnCmp (Name, L"eth", 3) != 0) || (*Str == 0)) {
108 return INVALID_NIC_INDEX;
109 }
110
111 while (*Str != 0) {
112 if ((*Str < L'0') || (*Str > L'9')) {
113 return INVALID_NIC_INDEX;
114 }
115
116 Str++;
117 }
118
119 return (UINT16)StrDecimalToUintn (Name + 3);
120}
121
133 IN CHAR16 *Name
134 )
135{
136 UINTN NumberOfHandles;
137 EFI_HANDLE *HandleBuffer;
138 UINTN Index;
139 EFI_HANDLE Handle;
140
141 //
142 // Find all NIC handles.
143 //
144 LocateNicHandleBuffer (&NumberOfHandles, &HandleBuffer);
145 if (NumberOfHandles == 0) {
146 return NULL;
147 }
148
149 Index = NicNameToIndex (Name);
150 if (Index >= NumberOfHandles) {
151 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_IF), mHiiHandle, Name);
152 Handle = NULL;
153 } else {
154 Handle = HandleBuffer[Index];
155 }
156
157 FreePool (HandleBuffer);
158 return Handle;
159}
160
171 IN EFI_HANDLE Handle
172 )
173{
174 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
175
176 VlanConfig = NULL;
177 gBS->OpenProtocol (
178 Handle,
179 &gEfiVlanConfigProtocolGuid,
180 (VOID **)&VlanConfig,
181 mImageHandle,
182 Handle,
183 EFI_OPEN_PROTOCOL_GET_PROTOCOL
184 );
185
186 return VlanConfig;
187}
188
195VOID
197 IN EFI_HANDLE Handle
198 )
199{
200 gBS->CloseProtocol (
201 Handle,
202 &gEfiVlanConfigProtocolGuid,
203 mImageHandle,
204 Handle
205 );
206}
207
215VOID
217 IN EFI_HANDLE Handle,
218 IN UINTN NicIndex
219 )
220{
221 CHAR16 *MacStr;
222 EFI_STATUS Status;
223 UINTN Index;
224 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
225 UINT16 NumberOfVlan;
226 EFI_VLAN_FIND_DATA *VlanData;
227
228 VlanConfig = OpenVlanConfigProtocol (Handle);
229 if (VlanConfig == NULL) {
230 return;
231 }
232
233 MacStr = NULL;
234 Status = NetLibGetMacString (Handle, mImageHandle, &MacStr);
235 if (EFI_ERROR (Status)) {
236 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_MAC_FAIL), mHiiHandle, Status);
237 goto Exit;
238 }
239
240 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_ETH_MAC), mHiiHandle, NicIndex, MacStr);
241
242 Status = VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);
243 if (EFI_ERROR (Status)) {
244 if (Status == EFI_NOT_FOUND) {
245 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VLAN), mHiiHandle);
246 } else {
247 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_FIND_FAIL), mHiiHandle, Status);
248 }
249
250 goto Exit;
251 }
252
253 for (Index = 0; Index < NumberOfVlan; Index++) {
255 -1,
256 -1,
257 NULL,
258 STRING_TOKEN (STR_VCONFIG_VLAN_DISPLAY),
259 mHiiHandle,
260 VlanData[Index].VlanId,
261 VlanData[Index].Priority
262 );
263 }
264
265 FreePool (VlanData);
266
267Exit:
269
270 if (MacStr != NULL) {
271 FreePool (MacStr);
272 }
273}
274
282VOID
284 IN CHAR16 *Name OPTIONAL
285 )
286{
287 UINTN NumberOfHandles;
288 EFI_HANDLE *HandleBuffer;
289 UINTN Index;
290 EFI_HANDLE NicHandle;
291
292 if (Name != NULL) {
293 //
294 // Display specified NIC
295 //
296 NicHandle = NicNameToHandle (Name);
297 if (NicHandle == NULL) {
298 return;
299 }
300
301 ShowNicVlanInfo (NicHandle, 0);
302 return;
303 }
304
305 //
306 // Find all NIC handles
307 //
308 LocateNicHandleBuffer (&NumberOfHandles, &HandleBuffer);
309 if (NumberOfHandles == 0) {
310 return;
311 }
312
313 for (Index = 0; Index < NumberOfHandles; Index++) {
314 ShowNicVlanInfo (HandleBuffer[Index], Index);
315 }
316
317 FreePool (HandleBuffer);
318}
319
328UINT16
330 IN CHAR16 *String
331 )
332{
333 CHAR16 *Str;
334
335 if (String == NULL) {
336 return INVALID_VLAN_ID;
337 }
338
339 Str = String;
340 while ((*Str >= '0') && (*Str <= '9')) {
341 Str++;
342 }
343
344 if (*Str != 0) {
345 return INVALID_VLAN_ID;
346 }
347
348 return (UINT16)StrDecimalToUintn (String);
349}
350
357VOID
359 IN CHAR16 *ParamStr
360 )
361{
362 CHAR16 *Name;
363 CHAR16 *VlanIdStr;
364 CHAR16 *PriorityStr;
365 CHAR16 *StrPtr;
366 BOOLEAN IsSpace;
367 UINTN VlanId;
368 UINTN Priority;
369 EFI_HANDLE Handle;
370 EFI_HANDLE VlanHandle;
371 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
372 EFI_STATUS Status;
373
374 VlanConfig = NULL;
375 Priority = 0;
376
377 if (ParamStr == NULL) {
378 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_IF), mHiiHandle);
379 return;
380 }
381
382 StrPtr = AllocateCopyPool (StrSize (ParamStr), ParamStr);
383 if (StrPtr == NULL) {
384 return;
385 }
386
387 Name = StrPtr;
388 VlanIdStr = NULL;
389 PriorityStr = NULL;
390 IsSpace = FALSE;
391 while (*StrPtr != 0) {
392 if (*StrPtr == L' ') {
393 *StrPtr = 0;
394 IsSpace = TRUE;
395 } else {
396 if (IsSpace) {
397 //
398 // Start of a parameter.
399 //
400 if (VlanIdStr == NULL) {
401 //
402 // 2nd parameter is VLAN ID.
403 //
404 VlanIdStr = StrPtr;
405 } else if (PriorityStr == NULL) {
406 //
407 // 3rd parameter is Priority.
408 //
409 PriorityStr = StrPtr;
410 } else {
411 //
412 // Ignore else parameters.
413 //
414 break;
415 }
416 }
417
418 IsSpace = FALSE;
419 }
420
421 StrPtr++;
422 }
423
424 Handle = NicNameToHandle (Name);
425 if (Handle == NULL) {
426 goto Exit;
427 }
428
429 VlanConfig = OpenVlanConfigProtocol (Handle);
430 if (VlanConfig == NULL) {
431 goto Exit;
432 }
433
434 //
435 // Check VLAN ID.
436 //
437 if ((VlanIdStr == NULL) || (*VlanIdStr == 0)) {
438 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VID), mHiiHandle);
439 goto Exit;
440 }
441
442 VlanId = StrToVlanId (VlanIdStr);
443 if (VlanId > 4094) {
444 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_VID), mHiiHandle, VlanIdStr);
445 goto Exit;
446 }
447
448 //
449 // Check Priority.
450 //
451 if ((PriorityStr != NULL) && (*PriorityStr != 0)) {
452 Priority = StrDecimalToUintn (PriorityStr);
453 if (Priority > 7) {
454 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_PRIORITY), mHiiHandle, PriorityStr);
455 goto Exit;
456 }
457 }
458
459 //
460 // Set VLAN
461 //
462 Status = VlanConfig->Set (VlanConfig, (UINT16)VlanId, (UINT8)Priority);
463 if (EFI_ERROR (Status)) {
464 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_SET_FAIL), mHiiHandle, Status);
465 goto Exit;
466 }
467
468 //
469 // Connect the VLAN device.
470 //
471 VlanHandle = NetLibGetVlanHandle (Handle, (UINT16)VlanId);
472 if (VlanHandle != NULL) {
473 gBS->ConnectController (VlanHandle, NULL, NULL, TRUE);
474 }
475
476 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_SET_SUCCESS), mHiiHandle);
477
478Exit:
479 if (VlanConfig != NULL) {
481 }
482
483 FreePool (Name);
484}
485
492VOID
494 IN CHAR16 *ParamStr
495 )
496{
497 CHAR16 *Name;
498 CHAR16 *VlanIdStr;
499 CHAR16 *StrPtr;
500 UINTN VlanId;
501 EFI_HANDLE Handle;
502 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
503 EFI_STATUS Status;
504 UINT16 NumberOfVlan;
505 EFI_VLAN_FIND_DATA *VlanData;
506
507 VlanConfig = NULL;
508
509 if (ParamStr == NULL) {
510 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_IF), mHiiHandle);
511 return;
512 }
513
514 StrPtr = AllocateCopyPool (StrSize (ParamStr), ParamStr);
515 if (StrPtr == NULL) {
516 return;
517 }
518
519 Name = StrPtr;
520 VlanIdStr = NULL;
521 while (*StrPtr != 0) {
522 if (*StrPtr == L'.') {
523 *StrPtr = 0;
524 VlanIdStr = StrPtr + 1;
525 break;
526 }
527
528 StrPtr++;
529 }
530
531 Handle = NicNameToHandle (Name);
532 if (Handle == NULL) {
533 goto Exit;
534 }
535
536 VlanConfig = OpenVlanConfigProtocol (Handle);
537 if (VlanConfig == NULL) {
538 goto Exit;
539 }
540
541 //
542 // Check VLAN ID
543 //
544 if ((VlanIdStr == NULL) || (*VlanIdStr == 0)) {
545 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VID), mHiiHandle);
546 goto Exit;
547 }
548
549 VlanId = StrToVlanId (VlanIdStr);
550 if (VlanId > 4094) {
551 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_VID), mHiiHandle, VlanIdStr);
552 goto Exit;
553 }
554
555 //
556 // Delete VLAN.
557 //
558 Status = VlanConfig->Remove (VlanConfig, (UINT16)VlanId);
559 if (EFI_ERROR (Status)) {
560 if (Status == EFI_NOT_FOUND) {
561 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NOT_FOUND), mHiiHandle);
562 } else {
563 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_REMOVE_FAIL), mHiiHandle, Status);
564 }
565
566 goto Exit;
567 }
568
569 //
570 // Check whether this is the last VLAN to remove.
571 //
572 Status = VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);
573 if (EFI_ERROR (Status)) {
574 //
575 // This is the last VLAN to remove, try to connect the controller handle.
576 //
577 gBS->ConnectController (Handle, NULL, NULL, TRUE);
578 } else {
579 FreePool (VlanData);
580 }
581
582 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_REMOVE_SUCCESS), mHiiHandle);
583
584Exit:
585 if (VlanConfig != NULL) {
587 }
588
589 FreePool (Name);
590}
591
603EFIAPI
605 IN EFI_HANDLE ImageHandle,
606 IN EFI_SYSTEM_TABLE *SystemTable
607 )
608{
609 LIST_ENTRY *List;
610 CONST CHAR16 *Str;
611 EFI_HII_PACKAGE_LIST_HEADER *PackageList;
612 EFI_STATUS Status;
613
614 mImageHandle = ImageHandle;
615
616 //
617 // Retrieve HII package list from ImageHandle
618 //
619 Status = gBS->OpenProtocol (
620 ImageHandle,
621 &gEfiHiiPackageListProtocolGuid,
622 (VOID **)&PackageList,
623 ImageHandle,
624 NULL,
625 EFI_OPEN_PROTOCOL_GET_PROTOCOL
626 );
627 if (EFI_ERROR (Status)) {
628 return Status;
629 }
630
631 //
632 // Publish HII package list to HII Database.
633 //
634 Status = gHiiDatabase->NewPackageList (
636 PackageList,
637 NULL,
638 &mHiiHandle
639 );
640 if (EFI_ERROR (Status)) {
641 return Status;
642 }
643
644 if (mHiiHandle == NULL) {
645 return EFI_SUCCESS;
646 }
647
648 List = NULL;
649 ShellCommandLineParseEx (mParamList, &List, NULL, FALSE, FALSE);
650 if (List == NULL) {
651 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_ARG), mHiiHandle);
652 goto Exit;
653 }
654
655 if (ShellCommandLineGetFlag (List, L"-l")) {
656 Str = ShellCommandLineGetValue (List, L"-l");
657 DisplayVlan ((CHAR16 *)Str);
658 goto Exit;
659 }
660
661 if (ShellCommandLineGetFlag (List, L"-a")) {
662 Str = ShellCommandLineGetValue (List, L"-a");
663 AddVlan ((CHAR16 *)Str);
664 goto Exit;
665 }
666
667 if (ShellCommandLineGetFlag (List, L"-d")) {
668 Str = ShellCommandLineGetValue (List, L"-d");
669 DeleteVlan ((CHAR16 *)Str);
670 goto Exit;
671 }
672
673 //
674 // No valid argument till now.
675 //
676 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_ARG), mHiiHandle);
677
678Exit:
679 if (List != NULL) {
681 }
682
683 //
684 // Remove our string package from HII database.
685 //
686 HiiRemovePackages (mHiiHandle);
687
688 return EFI_SUCCESS;
689}
UINT64 UINTN
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
UINTN EFIAPI StrDecimalToUintn(IN CONST CHAR16 *String)
Definition: String.c:405
INTN EFIAPI StrnCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString, IN UINTN Length)
Definition: String.c:162
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
VOID EFIAPI HiiRemovePackages(IN EFI_HII_HANDLE HiiHandle)
Definition: HiiLib.c:253
#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
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
EFI_STATUS EFIAPI NetLibGetMacString(IN EFI_HANDLE ServiceHandle, IN EFI_HANDLE ImageHandle OPTIONAL, OUT CHAR16 **MacString)
Definition: DxeNetLib.c:2363
EFI_HANDLE EFIAPI NetLibGetVlanHandle(IN EFI_HANDLE ControllerHandle, IN UINT16 VlanId)
Definition: DxeNetLib.c:2180
CONST CHAR16 *EFIAPI ShellCommandLineGetValue(IN CONST LIST_ENTRY *CheckPackage, IN CHAR16 *KeyString)
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,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeValue
A flag that has some data following it with a space (IE "-a 1").
Definition: ShellLib.h:700
@ TypeMaxValue
A flag followed by all the command line data before the next flag.
Definition: ShellLib.h:704
EFI_STATUS EFIAPI ShellCommandLineParseEx(IN CONST SHELL_PARAM_ITEM *CheckList, OUT LIST_ENTRY **CheckPackage, OUT CHAR16 **ProblemParam OPTIONAL, IN BOOLEAN AutoPageBreak, IN BOOLEAN AlwaysAllowNumbers)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
VOID EFIAPI Exit(IN EFI_STATUS Status)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_HII_DATABASE_PROTOCOL * gHiiDatabase
#define STRING_TOKEN(t)
VOID * EFI_HII_HANDLE
@ ByProtocol
Definition: UefiSpec.h:1518
EFI_HANDLE NicNameToHandle(IN CHAR16 *Name)
Definition: VConfig.c:132
EFI_VLAN_CONFIG_PROTOCOL * OpenVlanConfigProtocol(IN EFI_HANDLE Handle)
Definition: VConfig.c:170
UINTN NicNameToIndex(IN CHAR16 *Name)
Definition: VConfig.c:100
UINT16 StrToVlanId(IN CHAR16 *String)
Definition: VConfig.c:329
VOID CloseVlanConfigProtocol(IN EFI_HANDLE Handle)
Definition: VConfig.c:196
VOID LocateNicHandleBuffer(OUT UINTN *NumberOfHandles, OUT EFI_HANDLE **HandleBuffer)
Definition: VConfig.c:68
VOID DisplayVlan(IN CHAR16 *Name OPTIONAL)
Definition: VConfig.c:283
VOID AddVlan(IN CHAR16 *ParamStr)
Definition: VConfig.c:358
VOID ShowNicVlanInfo(IN EFI_HANDLE Handle, IN UINTN NicIndex)
Definition: VConfig.c:216
EFI_STATUS EFIAPI VlanConfigMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: VConfig.c:604
VOID DeleteVlan(IN CHAR16 *ParamStr)
Definition: VConfig.c:493