2 Shell application for VLAN configuration.
4 Copyright (c) 2009 - 2013, 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/NetLib.h>
28 #define INVALID_NIC_INDEX 0xffff
29 #define INVALID_VLAN_ID 0xffff
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).
36 extern UINT8 VConfigStrings
[];
38 EFI_HANDLE mImageHandle
= NULL
;
39 EFI_HII_HANDLE mHiiHandle
= NULL
;
41 SHELL_PARAM_ITEM mParamList
[] = {
61 Locate the network interface handle buffer.
63 @param[out] NumberOfHandles Pointer to the number of handles.
64 @param[out] HandleBuffer Pointer to the buffer to store the returned handles.
68 LocateNicHandleBuffer (
69 OUT UINTN
*NumberOfHandles
,
70 OUT EFI_HANDLE
**HandleBuffer
78 Status
= gBS
->LocateHandleBuffer (
80 &gEfiVlanConfigProtocolGuid
,
85 if (EFI_ERROR (Status
)) {
86 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_LOCATE_FAIL
), mHiiHandle
, Status
);
91 Extract the decimal index from the network interface name.
93 @param[in] Name Name of the network interface.
95 @retval INVALID_NIC_INDEX Failed to extract the network interface index.
96 @return others The network interface index.
107 if ((StrnCmp (Name
, L
"eth", 3) != 0) || (*Str
== 0)) {
108 return INVALID_NIC_INDEX
;
112 if ((*Str
< L
'0') || (*Str
> L
'9')) {
113 return INVALID_NIC_INDEX
;
119 return (UINT16
) StrDecimalToUintn (Name
+ 3);
123 Find network interface device handle by its name.
125 @param[in] Name Name of the network interface.
127 @retval NULL Cannot find the network interface.
128 @return others Handle of the network interface.
136 UINTN NumberOfHandles
;
137 EFI_HANDLE
*HandleBuffer
;
142 // Find all NIC handles.
144 LocateNicHandleBuffer (&NumberOfHandles
, &HandleBuffer
);
145 if (NumberOfHandles
== 0) {
149 Index
= NicNameToIndex (Name
);
150 if (Index
>= NumberOfHandles
) {
151 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_IF
), mHiiHandle
, Name
);
154 Handle
= HandleBuffer
[Index
];
157 FreePool (HandleBuffer
);
162 Open VlanConfig protocol from a handle.
164 @param[in] Handle The handle to open the VlanConfig protocol.
166 @return The VlanConfig protocol interface.
169 EFI_VLAN_CONFIG_PROTOCOL
*
170 OpenVlanConfigProtocol (
174 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
179 &gEfiVlanConfigProtocolGuid
,
180 (VOID
**) &VlanConfig
,
183 EFI_OPEN_PROTOCOL_GET_PROTOCOL
190 Close VlanConfig protocol of a handle.
192 @param[in] Handle The handle to close the VlanConfig protocol.
196 CloseVlanConfigProtocol (
202 &gEfiVlanConfigProtocolGuid
,
209 Display VLAN configuration of a network interface.
211 @param[in] Handle Handle of the network interface.
212 @param[in] NicIndex Index of the network interface.
217 IN EFI_HANDLE Handle
,
224 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
226 EFI_VLAN_FIND_DATA
*VlanData
;
228 VlanConfig
= OpenVlanConfigProtocol (Handle
);
229 if (VlanConfig
== 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
);
240 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_ETH_MAC
), mHiiHandle
, NicIndex
, MacStr
);
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
);
247 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_FIND_FAIL
), mHiiHandle
, Status
);
253 for (Index
= 0; Index
< NumberOfVlan
; Index
++) {
258 STRING_TOKEN (STR_VCONFIG_VLAN_DISPLAY
),
260 VlanData
[Index
].VlanId
,
261 VlanData
[Index
].Priority
268 CloseVlanConfigProtocol (Handle
);
270 if (MacStr
!= NULL
) {
276 Display the VLAN configuration of all, or a specified network interface.
278 @param[in] Name Name of the network interface. If NULL, the VLAN
279 configuration of all network will be displayed.
284 IN CHAR16
*Name OPTIONAL
287 UINTN NumberOfHandles
;
288 EFI_HANDLE
*HandleBuffer
;
290 EFI_HANDLE NicHandle
;
294 // Display specified NIC
296 NicHandle
= NicNameToHandle (Name
);
297 if (NicHandle
== NULL
) {
301 ShowNicVlanInfo (NicHandle
, 0);
306 // Find all NIC handles
308 LocateNicHandleBuffer (&NumberOfHandles
, &HandleBuffer
);
309 if (NumberOfHandles
== 0) {
313 for (Index
= 0; Index
< NumberOfHandles
; Index
++) {
314 ShowNicVlanInfo (HandleBuffer
[Index
], Index
);
317 FreePool (HandleBuffer
);
321 Convert a NULL-terminated unicode decimal VLAN ID string to VLAN ID.
323 @param[in] String Pointer to VLAN ID string from user input.
325 @retval Value translated from String, or INVALID_VLAN_ID is string is invalid.
335 if (String
== NULL
) {
336 return INVALID_VLAN_ID
;
340 while ((*Str
>= '0') && (*Str
<= '9')) {
345 return INVALID_VLAN_ID
;
348 return (UINT16
) StrDecimalToUintn (String
);
354 @param[in] ParamStr Parameter string from user input.
370 EFI_HANDLE VlanHandle
;
371 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
377 if (ParamStr
== NULL
) {
378 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_IF
), mHiiHandle
);
382 StrPtr
= AllocateCopyPool (StrSize (ParamStr
), ParamStr
);
383 if (StrPtr
== NULL
) {
391 while (*StrPtr
!= 0) {
392 if (*StrPtr
== L
' ') {
398 // Start of a parameter.
400 if (VlanIdStr
== NULL
) {
402 // 2nd parameter is VLAN ID.
405 } else if (PriorityStr
== NULL
) {
407 // 3rd parameter is Priority.
409 PriorityStr
= StrPtr
;
412 // Ignore else parameters.
424 Handle
= NicNameToHandle (Name
);
425 if (Handle
== NULL
) {
429 VlanConfig
= OpenVlanConfigProtocol (Handle
);
430 if (VlanConfig
== NULL
) {
437 if ((VlanIdStr
== NULL
) || (*VlanIdStr
== 0)) {
438 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_VID
), mHiiHandle
);
442 VlanId
= StrToVlanId (VlanIdStr
);
444 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_VID
), mHiiHandle
, VlanIdStr
);
451 if ((PriorityStr
!= NULL
) && (*PriorityStr
!= 0)) {
452 Priority
= StrDecimalToUintn (PriorityStr
);
454 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_PRIORITY
), mHiiHandle
, PriorityStr
);
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
);
469 // Connect the VLAN device.
471 VlanHandle
= NetLibGetVlanHandle (Handle
, (UINT16
) VlanId
);
472 if (VlanHandle
!= NULL
) {
473 gBS
->ConnectController (VlanHandle
, NULL
, NULL
, TRUE
);
476 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_SET_SUCCESS
), mHiiHandle
);
479 if (VlanConfig
!= NULL
) {
480 CloseVlanConfigProtocol (Handle
);
487 Remove a VLAN device.
489 @param[in] ParamStr Parameter string from user input.
502 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
505 EFI_VLAN_FIND_DATA
*VlanData
;
509 if (ParamStr
== NULL
) {
510 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_IF
), mHiiHandle
);
514 StrPtr
= AllocateCopyPool (StrSize (ParamStr
), ParamStr
);
515 if (StrPtr
== NULL
) {
521 while (*StrPtr
!= 0) {
522 if (*StrPtr
== L
'.') {
524 VlanIdStr
= StrPtr
+ 1;
531 Handle
= NicNameToHandle (Name
);
532 if (Handle
== NULL
) {
536 VlanConfig
= OpenVlanConfigProtocol (Handle
);
537 if (VlanConfig
== NULL
) {
544 if (VlanIdStr
== NULL
|| *VlanIdStr
== 0) {
545 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_VID
), mHiiHandle
);
549 VlanId
= StrToVlanId (VlanIdStr
);
551 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_INVALID_VID
), mHiiHandle
, VlanIdStr
);
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
);
563 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_REMOVE_FAIL
), mHiiHandle
, Status
);
570 // Check whether this is the last VLAN to remove.
572 Status
= VlanConfig
->Find (VlanConfig
, NULL
, &NumberOfVlan
, &VlanData
);
573 if (EFI_ERROR (Status
)) {
575 // This is the last VLAN to remove, try to connect the controller handle.
577 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
582 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_REMOVE_SUCCESS
), mHiiHandle
);
585 if (VlanConfig
!= NULL
) {
586 CloseVlanConfigProtocol (Handle
);
593 The actual entry point for the application.
595 @param[in] ImageHandle The firmware allocated handle for the EFI image.
596 @param[in] SystemTable A pointer to the EFI System Table.
598 @retval EFI_SUCCESS The entry point executed successfully.
599 @retval other Some error occur when executing this entry point.
605 IN EFI_HANDLE ImageHandle
,
606 IN EFI_SYSTEM_TABLE
*SystemTable
612 mImageHandle
= ImageHandle
;
615 // Register our string package to HII database.
617 mHiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, ImageHandle
, VConfigStrings
, NULL
);
618 if (mHiiHandle
== NULL
) {
623 ShellCommandLineParseEx (mParamList
, &List
, NULL
, FALSE
, FALSE
);
625 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_ARG
), mHiiHandle
);
629 if (ShellCommandLineGetFlag (List
, L
"-?")) {
630 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_HELP
), mHiiHandle
);
634 if (ShellCommandLineGetFlag (List
, L
"-l")) {
635 Str
= ShellCommandLineGetValue (List
, L
"-l");
636 DisplayVlan ((CHAR16
*) Str
);
640 if (ShellCommandLineGetFlag (List
, L
"-a")) {
641 Str
= ShellCommandLineGetValue (List
, L
"-a");
642 AddVlan ((CHAR16
*) Str
);
646 if (ShellCommandLineGetFlag (List
, L
"-d")) {
647 Str
= ShellCommandLineGetValue (List
, L
"-d");
648 DeleteVlan ((CHAR16
*) Str
);
653 // No valid argument till now.
655 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_VCONFIG_NO_ARG
), mHiiHandle
);
659 ShellCommandLineFreeVarList (List
);
663 // Remove our string package from HII database.
665 HiiRemovePackages (mHiiHandle
);