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.
137 while (List
->Next
!= NULL
) {
147 Split a string with specified separator and save the substring to a list.
149 @param[in] String The pointer of the input string.
150 @param[in] Separator The specified separator.
152 @return The pointer of headnode of ARG_LIST.
157 IN CONST CHAR16
*String
,
166 if (*String
== L
'\0') {
171 // Copy the CONST string to a local copy.
173 Str
= AllocateCopyPool (StrSize (String
), String
);
180 // init a node for the list head.
182 ArgNode
= (ARG_LIST
*) AllocateZeroPool (sizeof (ARG_LIST
));
183 if (ArgNode
== NULL
) {
189 // Split the local copy and save in the list node.
191 while (*Str
!= L
'\0') {
192 if (*Str
== Separator
) {
194 ArgNode
->Arg
= ArgStr
;
196 ArgNode
->Next
= (ARG_LIST
*) AllocateZeroPool (sizeof (ARG_LIST
));
197 if (ArgNode
->Next
== NULL
) {
199 // Free the local copy of string stored in the first node
201 FreePool (ArgList
->Arg
);
202 FreeArgList (ArgList
);
205 ArgNode
= ArgNode
->Next
;
211 ArgNode
->Arg
= ArgStr
;
212 ArgNode
->Next
= NULL
;
218 Check the correctness of input Args with '-s' option.
220 @param[in] CheckList The pointer of VAR_CHECK_ITEM array.
221 @param[in] Name The pointer of input arg.
222 @param[in] Init The switch to execute the check.
224 @return VarCheckOk Valid parameter or Initialize check successfully.
225 @return VarCheckDuplicate Duplicated parameter happened.
226 @return VarCheckConflict Conflicted parameter happened
227 @return VarCheckUnknown Unknown parameter.
231 IfConfigRetriveCheckListByName(
232 IN VAR_CHECK_ITEM
*CheckList
,
237 STATIC UINT32 CheckDuplicate
;
238 STATIC UINT32 CheckConflict
;
239 VAR_CHECK_CODE RtCode
;
251 Arg
= CheckList
[Index
];
254 // Check the Duplicated/Conflicted/Unknown input Args.
256 while (Arg
.FlagStr
!= NULL
) {
257 if (StrCmp (Arg
.FlagStr
, Name
) == 0) {
259 if (CheckDuplicate
& Arg
.FlagID
) {
260 RtCode
= VarCheckDuplicate
;
264 if (CheckConflict
& Arg
.ConflictMask
) {
265 RtCode
= VarCheckConflict
;
269 CheckDuplicate
|= Arg
.FlagID
;
270 CheckConflict
|= Arg
.ConflictMask
;
274 Arg
= CheckList
[++Index
];
277 if (Arg
.FlagStr
== NULL
) {
278 RtCode
= VarCheckUnknown
;
285 The notify function of create event when performing a manual config.
287 @param[in] Event The event this notify function registered to.
288 @param[in] Context Pointer to the context data registered to the event.
293 IfConfigManualAddressNotify (
298 *((BOOLEAN
*) Context
) = TRUE
;
304 @param[in] Node The pointer of MAC address buffer.
305 @param[in] Size The size of MAC address buffer.
309 IfConfigPrintMacAddr (
316 ASSERT (Size
<= MACADDRMAXSIZE
);
318 for (Index
= 0; Index
< Size
; Index
++) {
319 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_BODY
), gShellNetwork1HiiHandle
, Node
[Index
]);
320 if (Index
+ 1 < Size
) {
321 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_COLON
), gShellNetwork1HiiHandle
);
325 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
330 The get current status of all handles.
332 @param[in] IfName The pointer of IfName(interface name).
333 @param[in] IfList The pointer of IfList(interface list).
335 @retval EFI_SUCCESS The get status processed successfully.
336 @retval others The get status process failed.
340 IfConfigGetInterfaceInfo (
342 IN LIST_ENTRY
*IfList
348 EFI_HANDLE
*HandleBuffer
;
349 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Cfg2
;
350 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
351 IFCONFIG_INTERFACE_CB
*IfCb
;
361 // Locate all the handles with ip4 service binding protocol.
363 Status
= gBS
->LocateHandleBuffer (
365 &gEfiIp4ServiceBindingProtocolGuid
,
370 if (EFI_ERROR (Status
) || (HandleNum
== 0)) {
375 // Enumerate all handles that installed with ip4 service binding protocol.
377 for (HandleIndex
= 0; HandleIndex
< HandleNum
; HandleIndex
++) {
383 // Ip4config protocol and ip4 service binding protocol are installed
384 // on the same handle.
386 ASSERT (HandleBuffer
!= NULL
);
387 Status
= gBS
->HandleProtocol (
388 HandleBuffer
[HandleIndex
],
389 &gEfiIp4Config2ProtocolGuid
,
393 if (EFI_ERROR (Status
)) {
398 // Get the interface information size.
400 Status
= Ip4Cfg2
->GetData (
402 Ip4Config2DataTypeInterfaceInfo
,
407 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
411 IfInfo
= AllocateZeroPool (DataSize
);
413 if (IfInfo
== NULL
) {
414 Status
= EFI_OUT_OF_RESOURCES
;
419 // Get the interface info.
421 Status
= Ip4Cfg2
->GetData (
423 Ip4Config2DataTypeInterfaceInfo
,
428 if (EFI_ERROR (Status
)) {
433 // Check the interface name if required.
435 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) != 0)) {
443 // Get the size of dns server list.
445 Status
= Ip4Cfg2
->GetData (
447 Ip4Config2DataTypeDnsServer
,
452 if ((Status
!= EFI_BUFFER_TOO_SMALL
) && (Status
!= EFI_NOT_FOUND
)) {
456 IfCb
= AllocateZeroPool (sizeof (IFCONFIG_INTERFACE_CB
) + DataSize
);
459 Status
= EFI_OUT_OF_RESOURCES
;
463 IfCb
->NicHandle
= HandleBuffer
[HandleIndex
];
464 IfCb
->IfInfo
= IfInfo
;
465 IfCb
->IfCfg
= Ip4Cfg2
;
466 IfCb
->DnsCnt
= (UINT32
) (DataSize
/ sizeof (EFI_IPv4_ADDRESS
));
469 // Get the dns server list if has.
472 Status
= Ip4Cfg2
->GetData (
474 Ip4Config2DataTypeDnsServer
,
479 if (EFI_ERROR (Status
)) {
485 // Get the config policy.
487 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
488 Status
= Ip4Cfg2
->GetData (
490 Ip4Config2DataTypePolicy
,
495 if (EFI_ERROR (Status
)) {
499 InsertTailList (IfList
, &IfCb
->Link
);
501 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) == 0)) {
503 // Only need the appointed interface, keep the allocated buffer.
511 if (HandleBuffer
!= NULL
) {
512 FreePool (HandleBuffer
);
519 if (IfInfo
!= NULL
) {
531 The list process of the ifconfig command.
533 @param[in] IfList The pointer of IfList(interface list).
535 @retval SHELL_SUCCESS The ifconfig command list processed successfully.
536 @retval others The ifconfig command list process failed.
540 IfConfigShowInterfaceInfo (
541 IN LIST_ENTRY
*IfList
546 IFCONFIG_INTERFACE_CB
*IfCb
;
547 EFI_STATUS MediaStatus
;
548 EFI_IPv4_ADDRESS Gateway
;
551 MediaStatus
= EFI_SUCCESS
;
553 if (IsListEmpty (IfList
)) {
554 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
558 // Go through the interface list.
560 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
561 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
563 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
566 // Print interface name.
568 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IF_NAME
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->Name
);
573 if (EFI_SUCCESS
== NetLibDetectMediaWaitTimeout (IfCb
->NicHandle
, 0, &MediaStatus
)) {
574 if (MediaStatus
!= EFI_SUCCESS
) {
575 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media disconnected");
577 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media present");
580 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media state unknown");
584 // Print interface config policy.
586 if (IfCb
->Policy
== Ip4Config2PolicyDhcp
) {
587 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_DHCP
), gShellNetwork1HiiHandle
);
589 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_MAN
), gShellNetwork1HiiHandle
);
593 // Print mac address of the interface.
595 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_HEAD
), gShellNetwork1HiiHandle
);
597 IfConfigPrintMacAddr (
598 IfCb
->IfInfo
->HwAddress
.Addr
,
599 IfCb
->IfInfo
->HwAddressSize
603 // Print IPv4 address list of the interface.
605 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_HEAD
), gShellNetwork1HiiHandle
);
611 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
612 gShellNetwork1HiiHandle
,
613 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[0],
614 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[1],
615 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[2],
616 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[3]
620 // Print subnet mask list of the interface.
622 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_SUBNET_MASK_HEAD
), gShellNetwork1HiiHandle
);
628 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
629 gShellNetwork1HiiHandle
,
630 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[0],
631 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[1],
632 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[2],
633 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[3]
637 // Print default gateway of the interface.
639 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_GATEWAY_HEAD
), gShellNetwork1HiiHandle
);
641 ZeroMem (&Gateway
, sizeof (EFI_IPv4_ADDRESS
));
643 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
644 if ((CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0) &&
645 (CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0) ){
646 CopyMem (&Gateway
, &IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
654 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
655 gShellNetwork1HiiHandle
,
656 (UINTN
)Gateway
.Addr
[0],
657 (UINTN
)Gateway
.Addr
[1],
658 (UINTN
)Gateway
.Addr
[2],
659 (UINTN
)Gateway
.Addr
[3]
663 // Print route table entry.
665 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_ROUTES_SIZE
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->RouteTableSize
);
667 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
668 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_ROUTES_ENTRY_INDEX
), gShellNetwork1HiiHandle
, Index
);
674 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
675 gShellNetwork1HiiHandle
,
677 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[0],
678 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[1],
679 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[2],
680 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[3]
687 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
688 gShellNetwork1HiiHandle
,
690 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[0],
691 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[1],
692 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[2],
693 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[3]
700 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
701 gShellNetwork1HiiHandle
,
703 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[0],
704 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[1],
705 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[2],
706 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[3]
711 // Print dns server addresses list of the interface if has.
713 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_HEAD
), gShellNetwork1HiiHandle
);
715 for (Index
= 0; Index
< IfCb
->DnsCnt
; Index
++) {
720 STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_BODY
),
721 gShellNetwork1HiiHandle
,
722 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[0],
723 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[1],
724 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[2],
725 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[3]
728 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
732 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
734 return SHELL_SUCCESS
;
738 The clean process of the ifconfig command to clear interface info.
740 @param[in] IfList The pointer of IfList(interface list).
741 @param[in] IfName The pointer of interface name.
743 @retval SHELL_SUCCESS The ifconfig command clean processed successfully.
744 @retval others The ifconfig command clean process failed.
748 IfConfigClearInterfaceInfo (
749 IN LIST_ENTRY
*IfList
,
754 SHELL_STATUS ShellStatus
;
757 IFCONFIG_INTERFACE_CB
*IfCb
;
758 EFI_IP4_CONFIG2_POLICY Policy
;
760 Status
= EFI_SUCCESS
;
761 ShellStatus
= SHELL_SUCCESS
;
763 if (IsListEmpty (IfList
)) {
764 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
768 // Go through the interface list.
769 // If the interface name is specified, DHCP DORA process will be
770 // triggered by the policy transition (static -> dhcp).
772 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
773 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
775 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfCb
->IfInfo
->Name
) == 0)) {
776 Policy
= Ip4Config2PolicyStatic
;
778 Status
= IfCb
->IfCfg
->SetData (
780 Ip4Config2DataTypePolicy
,
781 sizeof (EFI_IP4_CONFIG2_POLICY
),
784 if (EFI_ERROR (Status
)) {
785 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
786 ShellStatus
= SHELL_ACCESS_DENIED
;
791 Policy
= Ip4Config2PolicyDhcp
;
793 Status
= IfCb
->IfCfg
->SetData (
795 Ip4Config2DataTypePolicy
,
796 sizeof (EFI_IP4_CONFIG2_POLICY
),
799 if (EFI_ERROR (Status
)) {
800 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
801 ShellStatus
= SHELL_ACCESS_DENIED
;
810 The set process of the ifconfig command.
812 @param[in] IfList The pointer of IfList(interface list).
813 @param[in] VarArg The pointer of ARG_LIST(Args with "-s" option).
815 @retval SHELL_SUCCESS The ifconfig command set processed successfully.
816 @retval others The ifconfig command set process failed.
820 IfConfigSetInterfaceInfo (
821 IN LIST_ENTRY
*IfList
,
826 SHELL_STATUS ShellStatus
;
827 IFCONFIG_INTERFACE_CB
*IfCb
;
828 VAR_CHECK_CODE CheckCode
;
829 EFI_EVENT TimeOutEvt
;
833 EFI_IP4_CONFIG2_POLICY Policy
;
834 EFI_IP4_CONFIG2_MANUAL_ADDRESS ManualAddress
;
836 EFI_IPv4_ADDRESS Gateway
;
838 IP4_ADDR TempGateway
;
839 EFI_IPv4_ADDRESS
*Dns
;
843 CONST CHAR16
* TempString
;
847 if (IsListEmpty (IfList
)) {
848 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
849 return SHELL_INVALID_PARAMETER
;
853 // Make sure to set only one interface each time.
855 IfCb
= NET_LIST_USER_STRUCT (IfList
->ForwardLink
, IFCONFIG_INTERFACE_CB
, Link
);
856 Status
= EFI_SUCCESS
;
857 ShellStatus
= SHELL_SUCCESS
;
860 // Initialize check list mechanism.
862 CheckCode
= IfConfigRetriveCheckListByName(
869 // Create events & timers for asynchronous settings.
871 Status
= gBS
->CreateEvent (
878 if (EFI_ERROR (Status
)) {
879 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
880 ShellStatus
= SHELL_ACCESS_DENIED
;
884 Status
= gBS
->CreateEvent (
887 IfConfigManualAddressNotify
,
891 if (EFI_ERROR (Status
)) {
892 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
893 ShellStatus
= SHELL_ACCESS_DENIED
;
898 // Parse the setting variables.
900 while (VarArg
!= NULL
) {
902 // Check invalid parameters (duplication & unknown & conflict).
904 CheckCode
= IfConfigRetriveCheckListByName(
910 if (VarCheckOk
!= CheckCode
) {
912 case VarCheckDuplicate
:
913 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_DUPLICATE_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
916 case VarCheckConflict
:
917 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_CONFLICT_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
920 case VarCheckUnknown
:
922 // To handle unsupported option.
924 TempString
= PermanentString
;
925 if (StringNoCaseCompare(&VarArg
->Arg
, &TempString
) == 0) {
926 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
, PermanentString
);
931 // To handle unknown option.
933 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNKNOWN_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
940 VarArg
= VarArg
->Next
;
945 // Process valid variables.
947 if (StrCmp(VarArg
->Arg
, L
"dhcp") == 0) {
949 // Set dhcp config policy
951 Policy
= Ip4Config2PolicyDhcp
;
952 Status
= IfCb
->IfCfg
->SetData (
954 Ip4Config2DataTypePolicy
,
955 sizeof (EFI_IP4_CONFIG2_POLICY
),
958 if (EFI_ERROR(Status
)) {
959 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
960 ShellStatus
= SHELL_ACCESS_DENIED
;
964 VarArg
= VarArg
->Next
;
966 } else if (StrCmp (VarArg
->Arg
, L
"static") == 0) {
967 VarArg
= VarArg
->Next
;
968 if (VarArg
== NULL
) {
969 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
970 ShellStatus
= SHELL_INVALID_PARAMETER
;
974 ZeroMem (&ManualAddress
, sizeof (ManualAddress
));
977 // Get manual IP address.
979 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.Address
);
980 if (EFI_ERROR(Status
)) {
981 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
982 ShellStatus
= SHELL_INVALID_PARAMETER
;
989 VarArg
= VarArg
->Next
;
990 if (VarArg
== NULL
) {
991 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
992 ShellStatus
= SHELL_INVALID_PARAMETER
;
996 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.SubnetMask
);
997 if (EFI_ERROR(Status
)) {
998 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
999 ShellStatus
= SHELL_INVALID_PARAMETER
;
1006 VarArg
= VarArg
->Next
;
1007 if (VarArg
== NULL
) {
1008 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1009 ShellStatus
= SHELL_INVALID_PARAMETER
;
1013 Status
= NetLibStrToIp4 (VarArg
->Arg
, &Gateway
);
1014 if (EFI_ERROR(Status
)) {
1015 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1016 ShellStatus
= SHELL_INVALID_PARAMETER
;
1021 // Need to check the gateway validity before set Manual Address.
1022 // In case we can set manual address but fail to configure Gateway.
1024 CopyMem (&SubnetMask
, &ManualAddress
.SubnetMask
, sizeof (IP4_ADDR
));
1025 CopyMem (&TempGateway
, &Gateway
, sizeof (IP4_ADDR
));
1026 SubnetMask
= NTOHL (SubnetMask
);
1027 TempGateway
= NTOHL (TempGateway
);
1028 if ((SubnetMask
!= 0) &&
1029 (SubnetMask
!= 0xFFFFFFFFu
) &&
1030 !NetIp4IsUnicast (TempGateway
, SubnetMask
)) {
1031 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_GATEWAY
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1032 ShellStatus
= SHELL_INVALID_PARAMETER
;
1037 // Set manual config policy.
1039 Policy
= Ip4Config2PolicyStatic
;
1040 Status
= IfCb
->IfCfg
->SetData (
1042 Ip4Config2DataTypePolicy
,
1043 sizeof (EFI_IP4_CONFIG2_POLICY
),
1046 if (EFI_ERROR(Status
)) {
1047 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1048 ShellStatus
= SHELL_ACCESS_DENIED
;
1053 // Set Manual Address.
1055 IsAddressOk
= FALSE
;
1057 Status
= IfCb
->IfCfg
->RegisterDataNotify (
1059 Ip4Config2DataTypeManualAddress
,
1062 if (EFI_ERROR (Status
)) {
1063 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1064 ShellStatus
= SHELL_ACCESS_DENIED
;
1068 DataSize
= sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS
);
1070 Status
= IfCb
->IfCfg
->SetData (
1072 Ip4Config2DataTypeManualAddress
,
1077 if (Status
== EFI_NOT_READY
) {
1078 gBS
->SetTimer (TimeOutEvt
, TimerRelative
, 50000000);
1080 while (EFI_ERROR (gBS
->CheckEvent (TimeOutEvt
))) {
1082 Status
= EFI_SUCCESS
;
1088 IfCb
->IfCfg
->UnregisterDataNotify (
1090 Ip4Config2DataTypeManualAddress
,
1094 if (EFI_ERROR (Status
)) {
1095 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1096 ShellStatus
= SHELL_ACCESS_DENIED
;
1103 DataSize
= sizeof (EFI_IPv4_ADDRESS
);
1105 Status
= IfCb
->IfCfg
->SetData (
1107 Ip4Config2DataTypeGateway
,
1111 if (EFI_ERROR (Status
)) {
1112 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1113 ShellStatus
= SHELL_ACCESS_DENIED
;
1117 VarArg
= VarArg
->Next
;
1119 } else if (StrCmp (VarArg
->Arg
, L
"dns") == 0) {
1121 // Get DNS addresses.
1123 VarArg
= VarArg
->Next
;
1126 while (Tmp
!= NULL
) {
1131 Dns
= AllocatePool (Index
* sizeof (EFI_IPv4_ADDRESS
));
1133 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1134 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1139 while (Tmp
!= NULL
) {
1140 Status
= NetLibStrToIp4 (Tmp
->Arg
, Dns
+ Index
);
1141 if (EFI_ERROR(Status
)) {
1142 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, Tmp
->Arg
);
1143 ShellStatus
= SHELL_INVALID_PARAMETER
;
1153 // Set DNS addresses.
1155 DataSize
= Index
* sizeof (EFI_IPv4_ADDRESS
);
1157 Status
= IfCb
->IfCfg
->SetData (
1159 Ip4Config2DataTypeDnsServer
,
1163 if (EFI_ERROR (Status
)) {
1164 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1165 ShellStatus
= SHELL_ACCESS_DENIED
;
1181 The ifconfig command main process.
1183 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1185 @retval SHELL_SUCCESS ifconfig command processed successfully.
1186 @retval others The ifconfig command process failed.
1191 IN IFCONFIG_PRIVATE_DATA
*Private
1195 SHELL_STATUS ShellStatus
;
1197 ShellStatus
= SHELL_SUCCESS
;
1200 // Get configure information of all interfaces.
1202 Status
= IfConfigGetInterfaceInfo (
1206 if (EFI_ERROR (Status
)) {
1207 ShellStatus
= SHELL_NOT_FOUND
;
1211 switch (Private
->OpCode
) {
1212 case IfConfigOpList
:
1213 ShellStatus
= IfConfigShowInterfaceInfo (&Private
->IfList
);
1216 case IfConfigOpClear
:
1217 ShellStatus
= IfConfigClearInterfaceInfo (&Private
->IfList
, Private
->IfName
);
1221 ShellStatus
= IfConfigSetInterfaceInfo (&Private
->IfList
, Private
->VarArg
);
1225 ShellStatus
= SHELL_UNSUPPORTED
;
1233 The ifconfig command cleanup process, free the allocated memory.
1235 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1240 IN IFCONFIG_PRIVATE_DATA
*Private
1244 LIST_ENTRY
*NextEntry
;
1245 IFCONFIG_INTERFACE_CB
*IfCb
;
1247 ASSERT (Private
!= NULL
);
1250 // Clean the list which save the set config Args.
1252 if (Private
->VarArg
!= NULL
) {
1253 FreeArgList (Private
->VarArg
);
1256 if (Private
->IfName
!= NULL
) {
1257 FreePool (Private
->IfName
);
1261 // Clean the IFCONFIG_INTERFACE_CB list.
1263 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->IfList
) {
1264 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
1266 RemoveEntryList (&IfCb
->Link
);
1268 if (IfCb
->IfInfo
!= NULL
) {
1270 FreePool (IfCb
->IfInfo
);
1280 Function for 'ifconfig' command.
1282 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1283 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1285 @retval EFI_SUCCESS ifconfig command processed successfully.
1286 @retval others The ifconfig command process failed.
1291 ShellCommandRunIfconfig (
1292 IN EFI_HANDLE ImageHandle
,
1293 IN EFI_SYSTEM_TABLE
*SystemTable
1297 IFCONFIG_PRIVATE_DATA
*Private
;
1298 LIST_ENTRY
*ParamPackage
;
1299 SHELL_STATUS ShellStatus
;
1300 CONST CHAR16
*ValueStr
;
1302 CHAR16
*ProblemParam
;
1305 Status
= EFI_INVALID_PARAMETER
;
1307 ShellStatus
= SHELL_SUCCESS
;
1309 Status
= ShellCommandLineParseEx (mIfConfigCheckList
, &ParamPackage
, &ProblemParam
, TRUE
, FALSE
);
1310 if (EFI_ERROR (Status
)) {
1311 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
1312 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellNetwork1HiiHandle
, L
"ifconfig", ProblemParam
);
1313 FreePool(ProblemParam
);
1314 ShellStatus
= SHELL_INVALID_PARAMETER
;
1323 // To handle unsupported option.
1325 if (ShellCommandLineGetFlag (ParamPackage
, L
"-c")) {
1326 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
,L
"-c");
1327 ShellStatus
= SHELL_INVALID_PARAMETER
;
1332 // To handle no option.
1334 if (!ShellCommandLineGetFlag (ParamPackage
, L
"-r") && !ShellCommandLineGetFlag (ParamPackage
, L
"-s") &&
1335 !ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1336 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_OPTION
), gShellNetwork1HiiHandle
);
1337 ShellStatus
= SHELL_INVALID_PARAMETER
;
1342 // To handle conflict options.
1344 if (((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-s"))) ||
1345 ((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l"))) ||
1346 ((ShellCommandLineGetFlag (ParamPackage
, L
"-s")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l")))) {
1347 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellNetwork1HiiHandle
, L
"ifconfig");
1348 ShellStatus
= SHELL_INVALID_PARAMETER
;
1352 Private
= AllocateZeroPool (sizeof (IFCONFIG_PRIVATE_DATA
));
1353 if (Private
== NULL
) {
1354 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1358 InitializeListHead (&Private
->IfList
);
1361 // To get interface name for the list option.
1363 if (ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1364 Private
->OpCode
= IfConfigOpList
;
1365 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-l");
1366 if (ValueStr
!= NULL
) {
1367 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1369 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1370 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1373 Private
->IfName
= Str
;
1378 // To get interface name for the clear option.
1380 if (ShellCommandLineGetFlag (ParamPackage
, L
"-r")) {
1381 Private
->OpCode
= IfConfigOpClear
;
1382 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-r");
1383 if (ValueStr
!= NULL
) {
1384 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1386 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1387 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1390 Private
->IfName
= Str
;
1395 // To get interface name and corresponding Args for the set option.
1397 if (ShellCommandLineGetFlag (ParamPackage
, L
"-s")) {
1398 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-s");
1399 if (ValueStr
== NULL
) {
1400 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_INTERFACE
), gShellNetwork1HiiHandle
);
1401 ShellStatus
= SHELL_INVALID_PARAMETER
;
1406 // To split the configuration into multi-section.
1408 ArgList
= SplitStrToList (ValueStr
, L
' ');
1409 if (ArgList
== NULL
) {
1410 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1411 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1415 Private
->OpCode
= IfConfigOpSet
;
1416 Private
->IfName
= ArgList
->Arg
;
1418 Private
->VarArg
= ArgList
->Next
;
1420 if (Private
->IfName
== NULL
|| Private
->VarArg
== NULL
) {
1421 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1422 ShellStatus
= SHELL_INVALID_PARAMETER
;
1428 // Main process of ifconfig.
1430 ShellStatus
= IfConfig (Private
);
1434 ShellCommandLineFreeVarList (ParamPackage
);
1436 if (Private
!= NULL
) {
1437 IfConfigCleanup (Private
);