2 The implementation for Shell command ifconfig based on IP4Config2 protocol.
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "UefiShellNetwork1CommandsLib.h"
20 VarCheckReserved
= -1,
34 } VAR_CHECK_FLAG_TYPE
;
36 #define MACADDRMAXSIZE 32
38 typedef struct _IFCONFIG_INTERFACE_CB
{
41 EFI_IP4_CONFIG2_PROTOCOL
*IfCfg
;
42 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
43 EFI_IP4_CONFIG2_POLICY Policy
;
45 EFI_IPv4_ADDRESS DnsAddr
[1];
46 } IFCONFIG_INTERFACE_CB
;
48 typedef struct _ARG_LIST ARG_LIST
;
55 typedef struct _IFCONFIG4_PRIVATE_DATA
{
61 } IFCONFIG_PRIVATE_DATA
;
63 typedef struct _VAR_CHECK_ITEM
{
67 VAR_CHECK_FLAG_TYPE FlagType
;
70 SHELL_PARAM_ITEM mIfConfigCheckList
[] = {
97 VAR_CHECK_ITEM mSetCheckList
[] = {
124 STATIC CONST CHAR16 PermanentString
[10] = L
"PERMANENT";
129 @param List Pointer to ARG_LIST to free.
138 while (List
->Next
!= NULL
) {
148 Split a string with specified separator and save the substring to a list.
150 @param[in] String The pointer of the input string.
151 @param[in] Separator The specified separator.
153 @return The pointer of headnode of ARG_LIST.
158 IN CONST CHAR16
*String
,
167 if (*String
== L
'\0') {
172 // Copy the CONST string to a local copy.
174 Str
= AllocateCopyPool (StrSize (String
), String
);
182 // init a node for the list head.
184 ArgNode
= (ARG_LIST
*)AllocateZeroPool (sizeof (ARG_LIST
));
185 if (ArgNode
== NULL
) {
192 // Split the local copy and save in the list node.
194 while (*Str
!= L
'\0') {
195 if (*Str
== Separator
) {
197 ArgNode
->Arg
= ArgStr
;
199 ArgNode
->Next
= (ARG_LIST
*)AllocateZeroPool (sizeof (ARG_LIST
));
200 if (ArgNode
->Next
== NULL
) {
202 // Free the local copy of string stored in the first node
204 FreePool (ArgList
->Arg
);
205 FreeArgList (ArgList
);
209 ArgNode
= ArgNode
->Next
;
215 ArgNode
->Arg
= ArgStr
;
216 ArgNode
->Next
= NULL
;
222 Check the correctness of input Args with '-s' option.
224 @param[in] CheckList The pointer of VAR_CHECK_ITEM array.
225 @param[in] Name The pointer of input arg.
226 @param[in] Init The switch to execute the check.
228 @return VarCheckOk Valid parameter or Initialize check successfully.
229 @return VarCheckDuplicate Duplicated parameter happened.
230 @return VarCheckConflict Conflicted parameter happened
231 @return VarCheckUnknown Unknown parameter.
235 IfConfigRetriveCheckListByName (
236 IN VAR_CHECK_ITEM
*CheckList
,
241 STATIC UINT32 CheckDuplicate
;
242 STATIC UINT32 CheckConflict
;
243 VAR_CHECK_CODE RtCode
;
255 Arg
= CheckList
[Index
];
258 // Check the Duplicated/Conflicted/Unknown input Args.
260 while (Arg
.FlagStr
!= NULL
) {
261 if (StrCmp (Arg
.FlagStr
, Name
) == 0) {
262 if (CheckDuplicate
& Arg
.FlagID
) {
263 RtCode
= VarCheckDuplicate
;
267 if (CheckConflict
& Arg
.ConflictMask
) {
268 RtCode
= VarCheckConflict
;
272 CheckDuplicate
|= Arg
.FlagID
;
273 CheckConflict
|= Arg
.ConflictMask
;
277 Arg
= CheckList
[++Index
];
280 if (Arg
.FlagStr
== NULL
) {
281 RtCode
= VarCheckUnknown
;
288 The notify function of create event when performing a manual config.
290 @param[in] Event The event this notify function registered to.
291 @param[in] Context Pointer to the context data registered to the event.
296 IfConfigManualAddressNotify (
301 *((BOOLEAN
*)Context
) = TRUE
;
307 @param[in] Node The pointer of MAC address buffer.
308 @param[in] Size The size of MAC address buffer.
312 IfConfigPrintMacAddr (
319 ASSERT (Size
<= MACADDRMAXSIZE
);
321 for (Index
= 0; Index
< Size
; Index
++) {
322 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_BODY
), gShellNetwork1HiiHandle
, Node
[Index
]);
323 if (Index
+ 1 < Size
) {
324 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_COLON
), gShellNetwork1HiiHandle
);
328 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
332 The get current status of all handles.
334 @param[in] IfName The pointer of IfName(interface name).
335 @param[in] IfList The pointer of IfList(interface list).
337 @retval EFI_SUCCESS The get status processed successfully.
338 @retval others The get status process failed.
342 IfConfigGetInterfaceInfo (
344 IN LIST_ENTRY
*IfList
350 EFI_HANDLE
*HandleBuffer
;
351 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Cfg2
;
352 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
353 IFCONFIG_INTERFACE_CB
*IfCb
;
363 // Locate all the handles with ip4 service binding protocol.
365 Status
= gBS
->LocateHandleBuffer (
367 &gEfiIp4ServiceBindingProtocolGuid
,
372 if (EFI_ERROR (Status
) || (HandleNum
== 0)) {
377 // Enumerate all handles that installed with ip4 service binding protocol.
379 for (HandleIndex
= 0; HandleIndex
< HandleNum
; HandleIndex
++) {
385 // Ip4config protocol and ip4 service binding protocol are installed
386 // on the same handle.
388 ASSERT (HandleBuffer
!= NULL
);
389 Status
= gBS
->HandleProtocol (
390 HandleBuffer
[HandleIndex
],
391 &gEfiIp4Config2ProtocolGuid
,
395 if (EFI_ERROR (Status
)) {
400 // Get the interface information size.
402 Status
= Ip4Cfg2
->GetData (
404 Ip4Config2DataTypeInterfaceInfo
,
409 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
413 IfInfo
= AllocateZeroPool (DataSize
);
415 if (IfInfo
== NULL
) {
416 Status
= EFI_OUT_OF_RESOURCES
;
421 // Get the interface info.
423 Status
= Ip4Cfg2
->GetData (
425 Ip4Config2DataTypeInterfaceInfo
,
430 if (EFI_ERROR (Status
)) {
435 // Check the interface name if required.
437 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) != 0)) {
445 // Get the size of dns server list.
447 Status
= Ip4Cfg2
->GetData (
449 Ip4Config2DataTypeDnsServer
,
454 if ((Status
!= EFI_BUFFER_TOO_SMALL
) && (Status
!= EFI_NOT_FOUND
)) {
458 IfCb
= AllocateZeroPool (sizeof (IFCONFIG_INTERFACE_CB
) + DataSize
);
461 Status
= EFI_OUT_OF_RESOURCES
;
465 IfCb
->NicHandle
= HandleBuffer
[HandleIndex
];
466 IfCb
->IfInfo
= IfInfo
;
467 IfCb
->IfCfg
= Ip4Cfg2
;
468 IfCb
->DnsCnt
= (UINT32
)(DataSize
/ sizeof (EFI_IPv4_ADDRESS
));
471 // Get the dns server list if has.
474 Status
= Ip4Cfg2
->GetData (
476 Ip4Config2DataTypeDnsServer
,
481 if (EFI_ERROR (Status
)) {
487 // Get the config policy.
489 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
490 Status
= Ip4Cfg2
->GetData (
492 Ip4Config2DataTypePolicy
,
497 if (EFI_ERROR (Status
)) {
501 InsertTailList (IfList
, &IfCb
->Link
);
503 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) == 0)) {
505 // Only need the appointed interface, keep the allocated buffer.
513 if (HandleBuffer
!= NULL
) {
514 FreePool (HandleBuffer
);
521 if (IfInfo
!= NULL
) {
533 The list process of the ifconfig command.
535 @param[in] IfList The pointer of IfList(interface list).
537 @retval SHELL_SUCCESS The ifconfig command list processed successfully.
538 @retval others The ifconfig command list process failed.
542 IfConfigShowInterfaceInfo (
543 IN LIST_ENTRY
*IfList
548 IFCONFIG_INTERFACE_CB
*IfCb
;
549 EFI_STATUS MediaStatus
;
550 EFI_IPv4_ADDRESS Gateway
;
553 MediaStatus
= EFI_SUCCESS
;
555 if (IsListEmpty (IfList
)) {
556 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
560 // Go through the interface list.
562 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
563 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
565 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
568 // Print interface name.
570 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IF_NAME
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->Name
);
575 if (EFI_SUCCESS
== NetLibDetectMediaWaitTimeout (IfCb
->NicHandle
, 0, &MediaStatus
)) {
576 if (MediaStatus
!= EFI_SUCCESS
) {
577 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media disconnected");
579 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media present");
582 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media state unknown");
586 // Print interface config policy.
588 if (IfCb
->Policy
== Ip4Config2PolicyDhcp
) {
589 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_DHCP
), gShellNetwork1HiiHandle
);
591 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_MAN
), gShellNetwork1HiiHandle
);
595 // Print mac address of the interface.
597 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_HEAD
), gShellNetwork1HiiHandle
);
599 IfConfigPrintMacAddr (
600 IfCb
->IfInfo
->HwAddress
.Addr
,
601 IfCb
->IfInfo
->HwAddressSize
605 // Print IPv4 address list of the interface.
607 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_HEAD
), gShellNetwork1HiiHandle
);
613 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
614 gShellNetwork1HiiHandle
,
615 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[0],
616 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[1],
617 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[2],
618 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[3]
622 // Print subnet mask list of the interface.
624 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_SUBNET_MASK_HEAD
), gShellNetwork1HiiHandle
);
630 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
631 gShellNetwork1HiiHandle
,
632 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[0],
633 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[1],
634 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[2],
635 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[3]
639 // Print default gateway of the interface.
641 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_GATEWAY_HEAD
), gShellNetwork1HiiHandle
);
643 ZeroMem (&Gateway
, sizeof (EFI_IPv4_ADDRESS
));
645 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
646 if ((CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0) &&
647 (CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0))
649 CopyMem (&Gateway
, &IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
657 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
658 gShellNetwork1HiiHandle
,
659 (UINTN
)Gateway
.Addr
[0],
660 (UINTN
)Gateway
.Addr
[1],
661 (UINTN
)Gateway
.Addr
[2],
662 (UINTN
)Gateway
.Addr
[3]
666 // Print route table entry.
668 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_ROUTES_SIZE
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->RouteTableSize
);
670 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
671 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_ROUTES_ENTRY_INDEX
), gShellNetwork1HiiHandle
, Index
);
677 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
678 gShellNetwork1HiiHandle
,
680 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[0],
681 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[1],
682 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[2],
683 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[3]
690 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
691 gShellNetwork1HiiHandle
,
693 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[0],
694 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[1],
695 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[2],
696 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[3]
703 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
704 gShellNetwork1HiiHandle
,
706 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[0],
707 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[1],
708 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[2],
709 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[3]
714 // Print dns server addresses list of the interface if has.
716 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_HEAD
), gShellNetwork1HiiHandle
);
718 for (Index
= 0; Index
< IfCb
->DnsCnt
; Index
++) {
723 STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_BODY
),
724 gShellNetwork1HiiHandle
,
725 (UINTN
)IfCb
->DnsAddr
[Index
].Addr
[0],
726 (UINTN
)IfCb
->DnsAddr
[Index
].Addr
[1],
727 (UINTN
)IfCb
->DnsAddr
[Index
].Addr
[2],
728 (UINTN
)IfCb
->DnsAddr
[Index
].Addr
[3]
731 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
735 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
737 return SHELL_SUCCESS
;
741 The clean process of the ifconfig command to clear interface info.
743 @param[in] IfList The pointer of IfList(interface list).
744 @param[in] IfName The pointer of interface name.
746 @retval SHELL_SUCCESS The ifconfig command clean processed successfully.
747 @retval others The ifconfig command clean process failed.
751 IfConfigClearInterfaceInfo (
752 IN LIST_ENTRY
*IfList
,
757 SHELL_STATUS ShellStatus
;
760 IFCONFIG_INTERFACE_CB
*IfCb
;
761 EFI_IP4_CONFIG2_POLICY Policy
;
763 Status
= EFI_SUCCESS
;
764 ShellStatus
= SHELL_SUCCESS
;
766 if (IsListEmpty (IfList
)) {
767 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
771 // Go through the interface list.
772 // If the interface name is specified, DHCP DORA process will be
773 // triggered by the policy transition (static -> dhcp).
775 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
776 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
778 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfCb
->IfInfo
->Name
) == 0)) {
779 Policy
= Ip4Config2PolicyStatic
;
781 Status
= IfCb
->IfCfg
->SetData (
783 Ip4Config2DataTypePolicy
,
784 sizeof (EFI_IP4_CONFIG2_POLICY
),
787 if (EFI_ERROR (Status
)) {
788 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
789 ShellStatus
= SHELL_ACCESS_DENIED
;
794 Policy
= Ip4Config2PolicyDhcp
;
796 Status
= IfCb
->IfCfg
->SetData (
798 Ip4Config2DataTypePolicy
,
799 sizeof (EFI_IP4_CONFIG2_POLICY
),
802 if (EFI_ERROR (Status
)) {
803 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
804 ShellStatus
= SHELL_ACCESS_DENIED
;
813 The set process of the ifconfig command.
815 @param[in] IfList The pointer of IfList(interface list).
816 @param[in] VarArg The pointer of ARG_LIST(Args with "-s" option).
818 @retval SHELL_SUCCESS The ifconfig command set processed successfully.
819 @retval others The ifconfig command set process failed.
823 IfConfigSetInterfaceInfo (
824 IN LIST_ENTRY
*IfList
,
829 SHELL_STATUS ShellStatus
;
830 IFCONFIG_INTERFACE_CB
*IfCb
;
831 VAR_CHECK_CODE CheckCode
;
832 EFI_EVENT TimeOutEvt
;
836 EFI_IP4_CONFIG2_POLICY Policy
;
837 EFI_IP4_CONFIG2_MANUAL_ADDRESS ManualAddress
;
839 EFI_IPv4_ADDRESS Gateway
;
841 IP4_ADDR TempGateway
;
842 EFI_IPv4_ADDRESS
*Dns
;
846 CONST CHAR16
*TempString
;
850 if (IsListEmpty (IfList
)) {
851 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
852 return SHELL_INVALID_PARAMETER
;
856 // Make sure to set only one interface each time.
858 IfCb
= NET_LIST_USER_STRUCT (IfList
->ForwardLink
, IFCONFIG_INTERFACE_CB
, Link
);
859 Status
= EFI_SUCCESS
;
860 ShellStatus
= SHELL_SUCCESS
;
863 // Initialize check list mechanism.
865 CheckCode
= IfConfigRetriveCheckListByName (
872 // Create events & timers for asynchronous settings.
874 Status
= gBS
->CreateEvent (
881 if (EFI_ERROR (Status
)) {
882 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
883 ShellStatus
= SHELL_ACCESS_DENIED
;
887 Status
= gBS
->CreateEvent (
890 IfConfigManualAddressNotify
,
894 if (EFI_ERROR (Status
)) {
895 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
896 ShellStatus
= SHELL_ACCESS_DENIED
;
901 // Parse the setting variables.
903 while (VarArg
!= NULL
) {
905 // Check invalid parameters (duplication & unknown & conflict).
907 CheckCode
= IfConfigRetriveCheckListByName (
913 if (VarCheckOk
!= CheckCode
) {
915 case VarCheckDuplicate
:
916 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_DUPLICATE_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
919 case VarCheckConflict
:
920 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_CONFLICT_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
923 case VarCheckUnknown
:
925 // To handle unsupported option.
927 TempString
= PermanentString
;
928 if (StringNoCaseCompare (&VarArg
->Arg
, &TempString
) == 0) {
929 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
, PermanentString
);
934 // To handle unknown option.
936 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNKNOWN_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
943 VarArg
= VarArg
->Next
;
948 // Process valid variables.
950 if (StrCmp (VarArg
->Arg
, L
"dhcp") == 0) {
952 // Set dhcp config policy
954 Policy
= Ip4Config2PolicyDhcp
;
955 Status
= IfCb
->IfCfg
->SetData (
957 Ip4Config2DataTypePolicy
,
958 sizeof (EFI_IP4_CONFIG2_POLICY
),
961 if (EFI_ERROR (Status
)) {
962 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
963 ShellStatus
= SHELL_ACCESS_DENIED
;
967 VarArg
= VarArg
->Next
;
968 } else if (StrCmp (VarArg
->Arg
, L
"static") == 0) {
969 VarArg
= VarArg
->Next
;
970 if (VarArg
== NULL
) {
971 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
972 ShellStatus
= SHELL_INVALID_PARAMETER
;
976 ZeroMem (&ManualAddress
, sizeof (ManualAddress
));
979 // Get manual IP address.
981 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.Address
);
982 if (EFI_ERROR (Status
)) {
983 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
984 ShellStatus
= SHELL_INVALID_PARAMETER
;
991 VarArg
= VarArg
->Next
;
992 if (VarArg
== NULL
) {
993 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
994 ShellStatus
= SHELL_INVALID_PARAMETER
;
998 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.SubnetMask
);
999 if (EFI_ERROR (Status
)) {
1000 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1001 ShellStatus
= SHELL_INVALID_PARAMETER
;
1008 VarArg
= VarArg
->Next
;
1009 if (VarArg
== NULL
) {
1010 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1011 ShellStatus
= SHELL_INVALID_PARAMETER
;
1015 Status
= NetLibStrToIp4 (VarArg
->Arg
, &Gateway
);
1016 if (EFI_ERROR (Status
)) {
1017 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1018 ShellStatus
= SHELL_INVALID_PARAMETER
;
1023 // Need to check the gateway validity before set Manual Address.
1024 // In case we can set manual address but fail to configure Gateway.
1026 CopyMem (&SubnetMask
, &ManualAddress
.SubnetMask
, sizeof (IP4_ADDR
));
1027 CopyMem (&TempGateway
, &Gateway
, sizeof (IP4_ADDR
));
1028 SubnetMask
= NTOHL (SubnetMask
);
1029 TempGateway
= NTOHL (TempGateway
);
1030 if ((SubnetMask
!= 0) &&
1031 (SubnetMask
!= 0xFFFFFFFFu
) &&
1032 !NetIp4IsUnicast (TempGateway
, SubnetMask
))
1034 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_GATEWAY
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1035 ShellStatus
= SHELL_INVALID_PARAMETER
;
1040 // Set manual config policy.
1042 Policy
= Ip4Config2PolicyStatic
;
1043 Status
= IfCb
->IfCfg
->SetData (
1045 Ip4Config2DataTypePolicy
,
1046 sizeof (EFI_IP4_CONFIG2_POLICY
),
1049 if (EFI_ERROR (Status
)) {
1050 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1051 ShellStatus
= SHELL_ACCESS_DENIED
;
1056 // Set Manual Address.
1058 IsAddressOk
= FALSE
;
1060 Status
= IfCb
->IfCfg
->RegisterDataNotify (
1062 Ip4Config2DataTypeManualAddress
,
1065 if (EFI_ERROR (Status
)) {
1066 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1067 ShellStatus
= SHELL_ACCESS_DENIED
;
1071 DataSize
= sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS
);
1073 Status
= IfCb
->IfCfg
->SetData (
1075 Ip4Config2DataTypeManualAddress
,
1080 if (Status
== EFI_NOT_READY
) {
1081 gBS
->SetTimer (TimeOutEvt
, TimerRelative
, 50000000);
1083 while (EFI_ERROR (gBS
->CheckEvent (TimeOutEvt
))) {
1085 Status
= EFI_SUCCESS
;
1091 IfCb
->IfCfg
->UnregisterDataNotify (
1093 Ip4Config2DataTypeManualAddress
,
1097 if (EFI_ERROR (Status
)) {
1098 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1099 ShellStatus
= SHELL_ACCESS_DENIED
;
1106 DataSize
= sizeof (EFI_IPv4_ADDRESS
);
1108 Status
= IfCb
->IfCfg
->SetData (
1110 Ip4Config2DataTypeGateway
,
1114 if (EFI_ERROR (Status
)) {
1115 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1116 ShellStatus
= SHELL_ACCESS_DENIED
;
1120 VarArg
= VarArg
->Next
;
1121 } else if (StrCmp (VarArg
->Arg
, L
"dns") == 0) {
1123 // Get DNS addresses.
1125 VarArg
= VarArg
->Next
;
1128 while (Tmp
!= NULL
) {
1133 Dns
= AllocatePool (Index
* sizeof (EFI_IPv4_ADDRESS
));
1135 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1136 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1142 while (Tmp
!= NULL
) {
1143 Status
= NetLibStrToIp4 (Tmp
->Arg
, Dns
+ Index
);
1144 if (EFI_ERROR (Status
)) {
1145 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, Tmp
->Arg
);
1146 ShellStatus
= SHELL_INVALID_PARAMETER
;
1157 // Set DNS addresses.
1159 DataSize
= Index
* sizeof (EFI_IPv4_ADDRESS
);
1161 Status
= IfCb
->IfCfg
->SetData (
1163 Ip4Config2DataTypeDnsServer
,
1167 if (EFI_ERROR (Status
)) {
1168 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1169 ShellStatus
= SHELL_ACCESS_DENIED
;
1184 The ifconfig command main process.
1186 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1188 @retval SHELL_SUCCESS ifconfig command processed successfully.
1189 @retval others The ifconfig command process failed.
1194 IN IFCONFIG_PRIVATE_DATA
*Private
1198 SHELL_STATUS ShellStatus
;
1200 ShellStatus
= SHELL_SUCCESS
;
1203 // Get configure information of all interfaces.
1205 Status
= IfConfigGetInterfaceInfo (
1209 if (EFI_ERROR (Status
)) {
1210 ShellStatus
= SHELL_NOT_FOUND
;
1214 switch (Private
->OpCode
) {
1215 case IfConfigOpList
:
1216 ShellStatus
= IfConfigShowInterfaceInfo (&Private
->IfList
);
1219 case IfConfigOpClear
:
1220 ShellStatus
= IfConfigClearInterfaceInfo (&Private
->IfList
, Private
->IfName
);
1224 ShellStatus
= IfConfigSetInterfaceInfo (&Private
->IfList
, Private
->VarArg
);
1228 ShellStatus
= SHELL_UNSUPPORTED
;
1236 The ifconfig command cleanup process, free the allocated memory.
1238 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1243 IN IFCONFIG_PRIVATE_DATA
*Private
1247 LIST_ENTRY
*NextEntry
;
1248 IFCONFIG_INTERFACE_CB
*IfCb
;
1250 ASSERT (Private
!= NULL
);
1253 // Clean the list which save the set config Args.
1255 if (Private
->VarArg
!= NULL
) {
1256 FreeArgList (Private
->VarArg
);
1259 if (Private
->IfName
!= NULL
) {
1260 FreePool (Private
->IfName
);
1264 // Clean the IFCONFIG_INTERFACE_CB list.
1266 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->IfList
) {
1267 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
1269 RemoveEntryList (&IfCb
->Link
);
1271 if (IfCb
->IfInfo
!= NULL
) {
1272 FreePool (IfCb
->IfInfo
);
1282 Function for 'ifconfig' command.
1284 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1285 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1287 @retval EFI_SUCCESS ifconfig command processed successfully.
1288 @retval others The ifconfig command process failed.
1293 ShellCommandRunIfconfig (
1294 IN EFI_HANDLE ImageHandle
,
1295 IN EFI_SYSTEM_TABLE
*SystemTable
1299 IFCONFIG_PRIVATE_DATA
*Private
;
1300 LIST_ENTRY
*ParamPackage
;
1301 SHELL_STATUS ShellStatus
;
1302 CONST CHAR16
*ValueStr
;
1304 CHAR16
*ProblemParam
;
1307 Status
= EFI_INVALID_PARAMETER
;
1309 ShellStatus
= SHELL_SUCCESS
;
1311 Status
= ShellCommandLineParseEx (mIfConfigCheckList
, &ParamPackage
, &ProblemParam
, TRUE
, FALSE
);
1312 if (EFI_ERROR (Status
)) {
1313 if ((Status
== EFI_VOLUME_CORRUPTED
) && (ProblemParam
!= NULL
)) {
1314 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellNetwork1HiiHandle
, L
"ifconfig", ProblemParam
);
1315 FreePool (ProblemParam
);
1316 ShellStatus
= SHELL_INVALID_PARAMETER
;
1325 // To handle unsupported option.
1327 if (ShellCommandLineGetFlag (ParamPackage
, L
"-c")) {
1328 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
, L
"-c");
1329 ShellStatus
= SHELL_INVALID_PARAMETER
;
1334 // To handle no option.
1336 if (!ShellCommandLineGetFlag (ParamPackage
, L
"-r") && !ShellCommandLineGetFlag (ParamPackage
, L
"-s") &&
1337 !ShellCommandLineGetFlag (ParamPackage
, L
"-l"))
1339 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_OPTION
), gShellNetwork1HiiHandle
);
1340 ShellStatus
= SHELL_INVALID_PARAMETER
;
1345 // To handle conflict options.
1347 if (((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-s"))) ||
1348 ((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l"))) ||
1349 ((ShellCommandLineGetFlag (ParamPackage
, L
"-s")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l"))))
1351 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellNetwork1HiiHandle
, L
"ifconfig");
1352 ShellStatus
= SHELL_INVALID_PARAMETER
;
1356 Private
= AllocateZeroPool (sizeof (IFCONFIG_PRIVATE_DATA
));
1357 if (Private
== NULL
) {
1358 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1362 InitializeListHead (&Private
->IfList
);
1365 // To get interface name for the list option.
1367 if (ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1368 Private
->OpCode
= IfConfigOpList
;
1369 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-l");
1370 if (ValueStr
!= NULL
) {
1371 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1373 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1374 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1378 Private
->IfName
= Str
;
1383 // To get interface name for the clear option.
1385 if (ShellCommandLineGetFlag (ParamPackage
, L
"-r")) {
1386 Private
->OpCode
= IfConfigOpClear
;
1387 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-r");
1388 if (ValueStr
!= NULL
) {
1389 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1391 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1392 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1396 Private
->IfName
= Str
;
1401 // To get interface name and corresponding Args for the set option.
1403 if (ShellCommandLineGetFlag (ParamPackage
, L
"-s")) {
1404 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-s");
1405 if (ValueStr
== NULL
) {
1406 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_INTERFACE
), gShellNetwork1HiiHandle
);
1407 ShellStatus
= SHELL_INVALID_PARAMETER
;
1412 // To split the configuration into multi-section.
1414 ArgList
= SplitStrToList (ValueStr
, L
' ');
1415 if (ArgList
== NULL
) {
1416 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1417 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1421 Private
->OpCode
= IfConfigOpSet
;
1422 Private
->IfName
= ArgList
->Arg
;
1424 Private
->VarArg
= ArgList
->Next
;
1426 if ((Private
->IfName
== NULL
) || (Private
->VarArg
== NULL
)) {
1427 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1428 ShellStatus
= SHELL_INVALID_PARAMETER
;
1434 // Main process of ifconfig.
1436 ShellStatus
= IfConfig (Private
);
1440 ShellCommandLineFreeVarList (ParamPackage
);
1442 if (Private
!= NULL
) {
1443 IfConfigCleanup (Private
);