2 Shell application for VLAN configuration.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Protocol/VlanConfig.h>
20 #include <Library/UefiApplicationEntryPoint.h>
21 #include <Library/UefiLib.h>
22 #include <Library/ShellLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/HiiLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/UefiHiiServicesLib.h>
27 #include <Library/NetLib.h>
30 // String token ID of VConfig command help message text.
32 GLOBAL_REMOVE_IF_UNREFERENCED EFI_STRING_ID mStringVConfigHelpTokenId
= STRING_TOKEN (STR_VCONFIG_HELP
);
34 #define INVALID_NIC_INDEX 0xffff
35 #define INVALID_VLAN_ID 0xffff
38 // This is the generated String package data for all .UNI files.
39 // This data array is ready to be used as input of HiiAddPackages() to
40 // create a packagelist (which contains Form packages, String packages, etc).
42 extern UINT8 VConfigStrings
[];
44 EFI_HANDLE mImageHandle
= NULL
;
45 EFI_HII_HANDLE mHiiHandle
= NULL
;
47 SHELL_PARAM_ITEM mParamList
[] = {
67 Locate the network interface handle buffer.
69 @param[out] NumberOfHandles Pointer to the number of handles.
70 @param[out] HandleBuffer Pointer to the buffer to store the returned handles.
74 LocateNicHandleBuffer (
75 OUT UINTN
*NumberOfHandles
,
76 OUT EFI_HANDLE
**HandleBuffer
84 Status
= gBS
->LocateHandleBuffer (
86 &gEfiVlanConfigProtocolGuid
,
91 if (EFI_ERROR (Status
)) {
92 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_LOCATE_FAIL
), mHiiHandle
, Status
);
97 Extract the decimal index from the network interface name.
99 @param[in] Name Name of the network interface.
101 @retval INVALID_NIC_INDEX Failed to extract the network interface index.
102 @return others The network interface index.
113 if ((StrnCmp (Name
, L
"eth", 3) != 0) || (*Str
== 0)) {
114 return INVALID_NIC_INDEX
;
118 if ((*Str
< L
'0') || (*Str
> L
'9')) {
119 return INVALID_NIC_INDEX
;
125 return (UINT16
) StrDecimalToUintn (Name
+ 3);
129 Find network interface device handle by its name.
131 @param[in] Name Name of the network interface.
133 @retval NULL Cannot find the network interface.
134 @return others Handle of the network interface.
142 UINTN NumberOfHandles
;
143 EFI_HANDLE
*HandleBuffer
;
148 // Find all NIC handles.
150 LocateNicHandleBuffer (&NumberOfHandles
, &HandleBuffer
);
151 if (NumberOfHandles
== 0) {
155 Index
= NicNameToIndex (Name
);
156 if (Index
>= NumberOfHandles
) {
157 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_IF
), mHiiHandle
, Name
);
160 Handle
= HandleBuffer
[Index
];
163 FreePool (HandleBuffer
);
168 Open VlanConfig protocol from a handle.
170 @param[in] Handle The handle to open the VlanConfig protocol.
172 @return The VlanConfig protocol interface.
175 EFI_VLAN_CONFIG_PROTOCOL
*
176 OpenVlanConfigProtocol (
180 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
185 &gEfiVlanConfigProtocolGuid
,
186 (VOID
**) &VlanConfig
,
189 EFI_OPEN_PROTOCOL_GET_PROTOCOL
196 Close VlanConfig protocol of a handle.
198 @param[in] Handle The handle to close the VlanConfig protocol.
202 CloseVlanConfigProtocol (
208 &gEfiVlanConfigProtocolGuid
,
215 Display VLAN configuration of a network interface.
217 @param[in] Handle Handle of the network interface.
218 @param[in] NicIndex Index of the network interface.
223 IN EFI_HANDLE Handle
,
230 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
232 EFI_VLAN_FIND_DATA
*VlanData
;
234 VlanConfig
= OpenVlanConfigProtocol (Handle
);
235 if (VlanConfig
== NULL
) {
240 Status
= NetLibGetMacString (Handle
, mImageHandle
, &MacStr
);
241 if (EFI_ERROR (Status
)) {
242 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_MAC_FAIL
), mHiiHandle
, Status
);
246 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_ETH_MAC
), mHiiHandle
, NicIndex
, MacStr
);
248 Status
= VlanConfig
->Find (VlanConfig
, NULL
, &NumberOfVlan
, &VlanData
);
249 if (EFI_ERROR (Status
)) {
250 if (Status
== EFI_NOT_FOUND
) {
251 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_VLAN
), mHiiHandle
);
253 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_FIND_FAIL
), mHiiHandle
, Status
);
259 for (Index
= 0; Index
< NumberOfVlan
; Index
++) {
264 STRING_TOKEN (STR_VCONFIG_VLAN_DISPLAY
),
266 VlanData
[Index
].VlanId
,
267 VlanData
[Index
].Priority
274 CloseVlanConfigProtocol (Handle
);
276 if (MacStr
!= NULL
) {
282 Display the VLAN configuration of all, or a specified network interface.
284 @param[in] Name Name of the network interface. If NULL, the VLAN
285 configuration of all network will be displayed.
290 IN CHAR16
*Name OPTIONAL
293 UINTN NumberOfHandles
;
294 EFI_HANDLE
*HandleBuffer
;
296 EFI_HANDLE NicHandle
;
300 // Display specified NIC
302 NicHandle
= NicNameToHandle (Name
);
303 if (NicHandle
== NULL
) {
307 ShowNicVlanInfo (NicHandle
, 0);
312 // Find all NIC handles
314 LocateNicHandleBuffer (&NumberOfHandles
, &HandleBuffer
);
315 if (NumberOfHandles
== 0) {
319 for (Index
= 0; Index
< NumberOfHandles
; Index
++) {
320 ShowNicVlanInfo (HandleBuffer
[Index
], Index
);
323 FreePool (HandleBuffer
);
327 Convert a NULL-terminated unicode decimal VLAN ID string to VLAN ID.
329 @param[in] String Pointer to VLAN ID string from user input.
331 @retval Value translated from String, or INVALID_VLAN_ID is string is invalid.
341 if (String
== NULL
) {
342 return INVALID_VLAN_ID
;
346 while ((*Str
>= '0') && (*Str
<= '9')) {
351 return INVALID_VLAN_ID
;
354 return (UINT16
) StrDecimalToUintn (String
);
360 @param[in] ParamStr Parameter string from user input.
376 EFI_HANDLE VlanHandle
;
377 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
383 if (ParamStr
== NULL
) {
384 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_IF
), mHiiHandle
);
388 StrPtr
= AllocateCopyPool (StrSize (ParamStr
), ParamStr
);
389 if (StrPtr
== NULL
) {
397 while (*StrPtr
!= 0) {
398 if (*StrPtr
== L
' ') {
404 // Start of a parameter.
406 if (VlanIdStr
== NULL
) {
408 // 2nd parameter is VLAN ID.
411 } else if (PriorityStr
== NULL
) {
413 // 3rd parameter is Priority.
415 PriorityStr
= StrPtr
;
418 // Ignore else parameters.
430 Handle
= NicNameToHandle (Name
);
431 if (Handle
== NULL
) {
435 VlanConfig
= OpenVlanConfigProtocol (Handle
);
436 if (VlanConfig
== NULL
) {
443 if ((VlanIdStr
== NULL
) || (*VlanIdStr
== 0)) {
444 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_VID
), mHiiHandle
);
448 VlanId
= StrToVlanId (VlanIdStr
);
450 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_VID
), mHiiHandle
, VlanIdStr
);
457 if ((PriorityStr
!= NULL
) && (*PriorityStr
!= 0)) {
458 Priority
= StrDecimalToUintn (PriorityStr
);
460 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_PRIORITY
), mHiiHandle
, PriorityStr
);
468 Status
= VlanConfig
->Set (VlanConfig
, (UINT16
) VlanId
, (UINT8
) Priority
);
469 if (EFI_ERROR (Status
)) {
470 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_SET_FAIL
), mHiiHandle
, Status
);
475 // Connect the VLAN device.
477 VlanHandle
= NetLibGetVlanHandle (Handle
, (UINT16
) VlanId
);
478 if (VlanHandle
!= NULL
) {
479 gBS
->ConnectController (VlanHandle
, NULL
, NULL
, TRUE
);
482 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_SET_SUCCESS
), mHiiHandle
);
485 if (VlanConfig
!= NULL
) {
486 CloseVlanConfigProtocol (Handle
);
493 Remove a VLAN device.
495 @param[in] ParamStr Parameter string from user input.
508 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
511 EFI_VLAN_FIND_DATA
*VlanData
;
515 if (ParamStr
== NULL
) {
516 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_IF
), mHiiHandle
);
520 StrPtr
= AllocateCopyPool (StrSize (ParamStr
), ParamStr
);
521 if (StrPtr
== NULL
) {
527 while (*StrPtr
!= 0) {
528 if (*StrPtr
== L
'.') {
530 VlanIdStr
= StrPtr
+ 1;
537 Handle
= NicNameToHandle (Name
);
538 if (Handle
== NULL
) {
542 VlanConfig
= OpenVlanConfigProtocol (Handle
);
543 if (VlanConfig
== NULL
) {
550 if (VlanIdStr
== NULL
|| *VlanIdStr
== 0) {
551 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_VID
), mHiiHandle
);
555 VlanId
= StrToVlanId (VlanIdStr
);
557 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_VID
), mHiiHandle
, VlanIdStr
);
564 Status
= VlanConfig
->Remove (VlanConfig
, (UINT16
) VlanId
);
565 if (EFI_ERROR (Status
)) {
566 if (Status
== EFI_NOT_FOUND
) {
567 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NOT_FOUND
), mHiiHandle
);
569 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_REMOVE_FAIL
), mHiiHandle
, Status
);
576 // Check whether this is the last VLAN to remove.
578 Status
= VlanConfig
->Find (VlanConfig
, NULL
, &NumberOfVlan
, &VlanData
);
579 if (EFI_ERROR (Status
)) {
581 // This is the last VLAN to remove, try to connect the controller handle.
583 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
588 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_REMOVE_SUCCESS
), mHiiHandle
);
591 if (VlanConfig
!= NULL
) {
592 CloseVlanConfigProtocol (Handle
);
599 The actual entry point for the application.
601 @param[in] ImageHandle The firmware allocated handle for the EFI image.
602 @param[in] SystemTable A pointer to the EFI System Table.
604 @retval EFI_SUCCESS The entry point executed successfully.
605 @retval other Some error occur when executing this entry point.
611 IN EFI_HANDLE ImageHandle
,
612 IN EFI_SYSTEM_TABLE
*SystemTable
617 EFI_HII_PACKAGE_LIST_HEADER
*PackageList
;
620 mImageHandle
= ImageHandle
;
623 // Retrieve HII package list from ImageHandle
625 Status
= gBS
->OpenProtocol (
627 &gEfiHiiPackageListProtocolGuid
,
628 (VOID
**) &PackageList
,
631 EFI_OPEN_PROTOCOL_GET_PROTOCOL
633 if (EFI_ERROR (Status
)) {
638 // Publish HII package list to HII Database.
640 Status
= gHiiDatabase
->NewPackageList (
646 if (EFI_ERROR (Status
)) {
650 if (mHiiHandle
== NULL
) {
655 ShellCommandLineParseEx (mParamList
, &List
, NULL
, FALSE
, FALSE
);
657 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_ARG
), mHiiHandle
);
661 if (ShellCommandLineGetFlag (List
, L
"-l")) {
662 Str
= ShellCommandLineGetValue (List
, L
"-l");
663 DisplayVlan ((CHAR16
*) Str
);
667 if (ShellCommandLineGetFlag (List
, L
"-a")) {
668 Str
= ShellCommandLineGetValue (List
, L
"-a");
669 AddVlan ((CHAR16
*) Str
);
673 if (ShellCommandLineGetFlag (List
, L
"-d")) {
674 Str
= ShellCommandLineGetValue (List
, L
"-d");
675 DeleteVlan ((CHAR16
*) Str
);
680 // No valid argument till now.
682 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_ARG
), mHiiHandle
);
686 ShellCommandLineFreeVarList (List
);
690 // Remove our string package from HII database.
692 HiiRemovePackages (mHiiHandle
);