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 - 2016, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "UefiShellNetwork1CommandsLib.h"
26 VarCheckReserved
= -1,
40 } VAR_CHECK_FLAG_TYPE
;
42 #define MACADDRMAXSIZE 32
44 typedef struct _IFCONFIG_INTERFACE_CB
{
47 EFI_IP4_CONFIG2_PROTOCOL
*IfCfg
;
48 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
49 EFI_IP4_CONFIG2_POLICY Policy
;
51 EFI_IPv4_ADDRESS DnsAddr
[1];
52 } IFCONFIG_INTERFACE_CB
;
54 typedef struct _ARG_LIST ARG_LIST
;
61 typedef struct _IFCONFIG4_PRIVATE_DATA
{
67 } IFCONFIG_PRIVATE_DATA
;
69 typedef struct _VAR_CHECK_ITEM
{
73 VAR_CHECK_FLAG_TYPE FlagType
;
76 SHELL_PARAM_ITEM mIfConfigCheckList
[] = {
103 VAR_CHECK_ITEM mSetCheckList
[] = {
130 STATIC CONST CHAR16 PermanentString
[10] = L
"PERMANENT";
135 @param List Pointer to ARG_LIST to free.
143 while (List
->Next
!= NULL
) {
153 Split a string with specified separator and save the substring to a list.
155 @param[in] String The pointer of the input string.
156 @param[in] Separator The specified separator.
158 @return The pointer of headnode of ARG_LIST.
163 IN CONST CHAR16
*String
,
172 if (*String
== L
'\0') {
177 // Copy the CONST string to a local copy.
179 Str
= AllocateCopyPool (StrSize (String
), String
);
186 // init a node for the list head.
188 ArgNode
= (ARG_LIST
*) AllocateZeroPool (sizeof (ARG_LIST
));
189 if (ArgNode
== NULL
) {
195 // Split the local copy and save in the list node.
197 while (*Str
!= L
'\0') {
198 if (*Str
== Separator
) {
200 ArgNode
->Arg
= ArgStr
;
202 ArgNode
->Next
= (ARG_LIST
*) AllocateZeroPool (sizeof (ARG_LIST
));
203 if (ArgNode
->Next
== NULL
) {
205 // Free the local copy of string stored in the first node
207 FreePool (ArgList
->Arg
);
208 FreeArgList (ArgList
);
211 ArgNode
= ArgNode
->Next
;
217 ArgNode
->Arg
= ArgStr
;
218 ArgNode
->Next
= NULL
;
224 Check the correctness of input Args with '-s' option.
226 @param[in] CheckList The pointer of VAR_CHECK_ITEM array.
227 @param[in] Name The pointer of input arg.
228 @param[in] Init The switch to execute the check.
230 @return VarCheckOk Valid parameter or Initialize check successfully.
231 @return VarCheckDuplicate Duplicated parameter happened.
232 @return VarCheckConflict Conflicted parameter happened
233 @return VarCheckUnknown Unknown parameter.
237 IfConfigRetriveCheckListByName(
238 IN VAR_CHECK_ITEM
*CheckList
,
243 STATIC UINT32 CheckDuplicate
;
244 STATIC UINT32 CheckConflict
;
245 VAR_CHECK_CODE RtCode
;
257 Arg
= CheckList
[Index
];
260 // Check the Duplicated/Conflicted/Unknown input Args.
262 while (Arg
.FlagStr
!= NULL
) {
263 if (StrCmp (Arg
.FlagStr
, Name
) == 0) {
265 if (CheckDuplicate
& Arg
.FlagID
) {
266 RtCode
= VarCheckDuplicate
;
270 if (CheckConflict
& Arg
.ConflictMask
) {
271 RtCode
= VarCheckConflict
;
275 CheckDuplicate
|= Arg
.FlagID
;
276 CheckConflict
|= Arg
.ConflictMask
;
280 Arg
= CheckList
[++Index
];
283 if (Arg
.FlagStr
== NULL
) {
284 RtCode
= VarCheckUnknown
;
291 The notify function of create event when performing a manual config.
293 @param[in] Event The event this notify function registered to.
294 @param[in] Context Pointer to the context data registered to the event.
299 IfConfigManualAddressNotify (
304 *((BOOLEAN
*) Context
) = TRUE
;
310 @param[in] Node The pointer of MAC address buffer.
311 @param[in] Size The size of MAC address buffer.
315 IfConfigPrintMacAddr (
322 ASSERT (Size
<= MACADDRMAXSIZE
);
324 for (Index
= 0; Index
< Size
; Index
++) {
325 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_BODY
), gShellNetwork1HiiHandle
, Node
[Index
]);
326 if (Index
+ 1 < Size
) {
327 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_COLON
), gShellNetwork1HiiHandle
);
331 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
336 The get current status of all handles.
338 @param[in] IfName The pointer of IfName(interface name).
339 @param[in] IfList The pointer of IfList(interface list).
341 @retval EFI_SUCCESS The get status processed successfully.
342 @retval others The get status process failed.
346 IfConfigGetInterfaceInfo (
348 IN LIST_ENTRY
*IfList
354 EFI_HANDLE
*HandleBuffer
;
355 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Cfg2
;
356 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
357 IFCONFIG_INTERFACE_CB
*IfCb
;
367 // Locate all the handles with ip4 service binding protocol.
369 Status
= gBS
->LocateHandleBuffer (
371 &gEfiIp4ServiceBindingProtocolGuid
,
376 if (EFI_ERROR (Status
) || (HandleNum
== 0)) {
381 // Enumerate all handles that installed with ip4 service binding protocol.
383 for (HandleIndex
= 0; HandleIndex
< HandleNum
; HandleIndex
++) {
389 // Ip4config protocol and ip4 service binding protocol are installed
390 // on the same handle.
392 ASSERT (HandleBuffer
!= NULL
);
393 Status
= gBS
->HandleProtocol (
394 HandleBuffer
[HandleIndex
],
395 &gEfiIp4Config2ProtocolGuid
,
399 if (EFI_ERROR (Status
)) {
404 // Get the interface information size.
406 Status
= Ip4Cfg2
->GetData (
408 Ip4Config2DataTypeInterfaceInfo
,
413 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
417 IfInfo
= AllocateZeroPool (DataSize
);
419 if (IfInfo
== NULL
) {
420 Status
= EFI_OUT_OF_RESOURCES
;
425 // Get the interface info.
427 Status
= Ip4Cfg2
->GetData (
429 Ip4Config2DataTypeInterfaceInfo
,
434 if (EFI_ERROR (Status
)) {
439 // Check the interface name if required.
441 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) != 0)) {
449 // Get the size of dns server list.
451 Status
= Ip4Cfg2
->GetData (
453 Ip4Config2DataTypeDnsServer
,
458 if ((Status
!= EFI_BUFFER_TOO_SMALL
) && (Status
!= EFI_NOT_FOUND
)) {
462 IfCb
= AllocateZeroPool (sizeof (IFCONFIG_INTERFACE_CB
) + DataSize
);
465 Status
= EFI_OUT_OF_RESOURCES
;
469 IfCb
->NicHandle
= HandleBuffer
[HandleIndex
];
470 IfCb
->IfInfo
= IfInfo
;
471 IfCb
->IfCfg
= Ip4Cfg2
;
472 IfCb
->DnsCnt
= (UINT32
) (DataSize
/ sizeof (EFI_IPv4_ADDRESS
));
475 // Get the dns server list if has.
478 Status
= Ip4Cfg2
->GetData (
480 Ip4Config2DataTypeDnsServer
,
485 if (EFI_ERROR (Status
)) {
491 // Get the config policy.
493 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
494 Status
= Ip4Cfg2
->GetData (
496 Ip4Config2DataTypePolicy
,
501 if (EFI_ERROR (Status
)) {
505 InsertTailList (IfList
, &IfCb
->Link
);
507 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) == 0)) {
509 // Only need the appointed interface, keep the allocated buffer.
517 if (HandleBuffer
!= NULL
) {
518 FreePool (HandleBuffer
);
525 if (IfInfo
!= NULL
) {
537 The list process of the ifconfig command.
539 @param[in] IfList The pointer of IfList(interface list).
541 @retval SHELL_SUCCESS The ifconfig command list processed successfully.
542 @retval others The ifconfig command list process failed.
546 IfConfigShowInterfaceInfo (
547 IN LIST_ENTRY
*IfList
552 IFCONFIG_INTERFACE_CB
*IfCb
;
553 BOOLEAN MediaPresent
;
554 EFI_IPv4_ADDRESS Gateway
;
559 if (IsListEmpty (IfList
)) {
560 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
564 // Go through the interface list.
566 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
567 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
569 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
572 // Print interface name.
574 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IF_NAME
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->Name
);
579 NetLibDetectMedia (IfCb
->NicHandle
, &MediaPresent
);
581 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media disconnected");
583 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media present");
587 // Print interface config policy.
589 if (IfCb
->Policy
== Ip4Config2PolicyDhcp
) {
590 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_DHCP
), gShellNetwork1HiiHandle
);
592 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_MAN
), gShellNetwork1HiiHandle
);
596 // Print mac address of the interface.
598 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_HEAD
), gShellNetwork1HiiHandle
);
600 IfConfigPrintMacAddr (
601 IfCb
->IfInfo
->HwAddress
.Addr
,
602 IfCb
->IfInfo
->HwAddressSize
606 // Print IPv4 address list of the interface.
608 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_HEAD
), gShellNetwork1HiiHandle
);
614 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
615 gShellNetwork1HiiHandle
,
616 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[0],
617 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[1],
618 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[2],
619 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[3]
623 // Print subnet mask list of the interface.
625 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_SUBNET_MASK_HEAD
), gShellNetwork1HiiHandle
);
631 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
632 gShellNetwork1HiiHandle
,
633 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[0],
634 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[1],
635 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[2],
636 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[3]
640 // Print default gateway of the interface.
642 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_GATEWAY_HEAD
), gShellNetwork1HiiHandle
);
644 ZeroMem (&Gateway
, sizeof (EFI_IPv4_ADDRESS
));
646 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
647 if ((CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0) &&
648 (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
;
840 EFI_IPv4_ADDRESS
*Dns
;
844 CONST CHAR16
* TempString
;
848 if (IsListEmpty (IfList
)) {
849 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
850 return SHELL_INVALID_PARAMETER
;
854 // Make sure to set only one interface each time.
856 IfCb
= NET_LIST_USER_STRUCT (IfList
->ForwardLink
, IFCONFIG_INTERFACE_CB
, Link
);
857 Status
= EFI_SUCCESS
;
858 ShellStatus
= SHELL_SUCCESS
;
861 // Initialize check list mechanism.
863 CheckCode
= IfConfigRetriveCheckListByName(
870 // Create events & timers for asynchronous settings.
872 Status
= gBS
->CreateEvent (
879 if (EFI_ERROR (Status
)) {
880 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
881 ShellStatus
= SHELL_ACCESS_DENIED
;
885 Status
= gBS
->CreateEvent (
888 IfConfigManualAddressNotify
,
892 if (EFI_ERROR (Status
)) {
893 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
894 ShellStatus
= SHELL_ACCESS_DENIED
;
899 // Parse the setting variables.
901 while (VarArg
!= NULL
) {
903 // Check invalid parameters (duplication & unknown & conflict).
905 CheckCode
= IfConfigRetriveCheckListByName(
911 if (VarCheckOk
!= CheckCode
) {
913 case VarCheckDuplicate
:
914 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_DUPLICATE_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
917 case VarCheckConflict
:
918 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_CONFLICT_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
921 case VarCheckUnknown
:
923 // To handle unsupported option.
925 TempString
= PermanentString
;
926 if (StringNoCaseCompare(&VarArg
->Arg
, &TempString
) == 0) {
927 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
, PermanentString
);
932 // To handle unknown option.
934 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNKNOWN_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
941 VarArg
= VarArg
->Next
;
946 // Process valid variables.
948 if (StrCmp(VarArg
->Arg
, L
"dhcp") == 0) {
950 // Set dhcp config policy
952 Policy
= Ip4Config2PolicyDhcp
;
953 Status
= IfCb
->IfCfg
->SetData (
955 Ip4Config2DataTypePolicy
,
956 sizeof (EFI_IP4_CONFIG2_POLICY
),
959 if (EFI_ERROR(Status
)) {
960 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
961 ShellStatus
= SHELL_ACCESS_DENIED
;
965 VarArg
= VarArg
->Next
;
967 } else if (StrCmp (VarArg
->Arg
, L
"static") == 0) {
968 VarArg
= VarArg
->Next
;
969 if (VarArg
== NULL
) {
970 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
971 ShellStatus
= SHELL_INVALID_PARAMETER
;
975 ZeroMem (&ManualAddress
, sizeof (ManualAddress
));
978 // Get manual IP address.
980 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.Address
);
981 if (EFI_ERROR(Status
)) {
982 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
983 ShellStatus
= SHELL_INVALID_PARAMETER
;
990 VarArg
= VarArg
->Next
;
991 if (VarArg
== NULL
) {
992 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
993 ShellStatus
= SHELL_INVALID_PARAMETER
;
997 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.SubnetMask
);
998 if (EFI_ERROR(Status
)) {
999 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1000 ShellStatus
= SHELL_INVALID_PARAMETER
;
1007 VarArg
= VarArg
->Next
;
1008 if (VarArg
== NULL
) {
1009 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1010 ShellStatus
= SHELL_INVALID_PARAMETER
;
1014 Status
= NetLibStrToIp4 (VarArg
->Arg
, &Gateway
);
1015 if (EFI_ERROR(Status
)) {
1016 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1017 ShellStatus
= SHELL_INVALID_PARAMETER
;
1022 // Set manual config policy.
1024 Policy
= Ip4Config2PolicyStatic
;
1025 Status
= IfCb
->IfCfg
->SetData (
1027 Ip4Config2DataTypePolicy
,
1028 sizeof (EFI_IP4_CONFIG2_POLICY
),
1031 if (EFI_ERROR(Status
)) {
1032 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1033 ShellStatus
= SHELL_ACCESS_DENIED
;
1038 // Set Manual Address.
1040 IsAddressOk
= FALSE
;
1042 Status
= IfCb
->IfCfg
->RegisterDataNotify (
1044 Ip4Config2DataTypeManualAddress
,
1047 if (EFI_ERROR (Status
)) {
1048 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1049 ShellStatus
= SHELL_ACCESS_DENIED
;
1053 DataSize
= sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS
);
1055 Status
= IfCb
->IfCfg
->SetData (
1057 Ip4Config2DataTypeManualAddress
,
1062 if (Status
== EFI_NOT_READY
) {
1063 gBS
->SetTimer (TimeOutEvt
, TimerRelative
, 50000000);
1065 while (EFI_ERROR (gBS
->CheckEvent (TimeOutEvt
))) {
1067 Status
= EFI_SUCCESS
;
1073 IfCb
->IfCfg
->UnregisterDataNotify (
1075 Ip4Config2DataTypeManualAddress
,
1079 if (EFI_ERROR (Status
)) {
1080 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1081 ShellStatus
= SHELL_ACCESS_DENIED
;
1088 DataSize
= sizeof (EFI_IPv4_ADDRESS
);
1090 Status
= IfCb
->IfCfg
->SetData (
1092 Ip4Config2DataTypeGateway
,
1096 if (EFI_ERROR (Status
)) {
1097 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1098 ShellStatus
= SHELL_ACCESS_DENIED
;
1102 VarArg
= VarArg
->Next
;
1104 } else if (StrCmp (VarArg
->Arg
, L
"dns") == 0) {
1106 // Get DNS addresses.
1108 VarArg
= VarArg
->Next
;
1111 while (Tmp
!= NULL
) {
1116 Dns
= AllocatePool (Index
* sizeof (EFI_IPv4_ADDRESS
));
1118 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1119 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1124 while (Tmp
!= NULL
) {
1125 Status
= NetLibStrToIp4 (Tmp
->Arg
, Dns
+ Index
);
1126 if (EFI_ERROR(Status
)) {
1127 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, Tmp
->Arg
);
1128 ShellStatus
= SHELL_INVALID_PARAMETER
;
1138 // Set DNS addresses.
1140 DataSize
= Index
* sizeof (EFI_IPv4_ADDRESS
);
1142 Status
= IfCb
->IfCfg
->SetData (
1144 Ip4Config2DataTypeDnsServer
,
1148 if (EFI_ERROR (Status
)) {
1149 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1150 ShellStatus
= SHELL_ACCESS_DENIED
;
1166 The ifconfig command main process.
1168 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1170 @retval SHELL_SUCCESS ifconfig command processed successfully.
1171 @retval others The ifconfig command process failed.
1176 IN IFCONFIG_PRIVATE_DATA
*Private
1180 SHELL_STATUS ShellStatus
;
1182 ShellStatus
= SHELL_SUCCESS
;
1185 // Get configure information of all interfaces.
1187 Status
= IfConfigGetInterfaceInfo (
1191 if (EFI_ERROR (Status
)) {
1192 ShellStatus
= SHELL_NOT_FOUND
;
1196 switch (Private
->OpCode
) {
1197 case IfConfigOpList
:
1198 ShellStatus
= IfConfigShowInterfaceInfo (&Private
->IfList
);
1201 case IfConfigOpClear
:
1202 ShellStatus
= IfConfigClearInterfaceInfo (&Private
->IfList
, Private
->IfName
);
1206 ShellStatus
= IfConfigSetInterfaceInfo (&Private
->IfList
, Private
->VarArg
);
1210 ShellStatus
= SHELL_UNSUPPORTED
;
1218 The ifconfig command cleanup process, free the allocated memory.
1220 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1225 IN IFCONFIG_PRIVATE_DATA
*Private
1229 LIST_ENTRY
*NextEntry
;
1230 IFCONFIG_INTERFACE_CB
*IfCb
;
1232 ASSERT (Private
!= NULL
);
1235 // Clean the list which save the set config Args.
1237 if (Private
->VarArg
!= NULL
) {
1238 FreeArgList (Private
->VarArg
);
1241 if (Private
->IfName
!= NULL
) {
1242 FreePool (Private
->IfName
);
1246 // Clean the IFCONFIG_INTERFACE_CB list.
1248 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->IfList
) {
1249 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
1251 RemoveEntryList (&IfCb
->Link
);
1253 if (IfCb
->IfInfo
!= NULL
) {
1255 FreePool (IfCb
->IfInfo
);
1265 Function for 'ifconfig' command.
1267 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1268 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1270 @retval EFI_SUCCESS ifconfig command processed successfully.
1271 @retval others The ifconfig command process failed.
1276 ShellCommandRunIfconfig (
1277 IN EFI_HANDLE ImageHandle
,
1278 IN EFI_SYSTEM_TABLE
*SystemTable
1282 IFCONFIG_PRIVATE_DATA
*Private
;
1283 LIST_ENTRY
*ParamPackage
;
1284 SHELL_STATUS ShellStatus
;
1285 CONST CHAR16
*ValueStr
;
1287 CHAR16
*ProblemParam
;
1290 Status
= EFI_INVALID_PARAMETER
;
1292 ShellStatus
= SHELL_SUCCESS
;
1294 Status
= ShellCommandLineParseEx (mIfConfigCheckList
, &ParamPackage
, &ProblemParam
, TRUE
, FALSE
);
1295 if (EFI_ERROR (Status
)) {
1296 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
1297 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellNetwork1HiiHandle
, L
"ifconfig", ProblemParam
);
1298 FreePool(ProblemParam
);
1299 ShellStatus
= SHELL_INVALID_PARAMETER
;
1308 // To handle unsupported option.
1310 if (ShellCommandLineGetFlag (ParamPackage
, L
"-c")) {
1311 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
,L
"-c");
1312 ShellStatus
= SHELL_INVALID_PARAMETER
;
1317 // To handle no option.
1319 if (!ShellCommandLineGetFlag (ParamPackage
, L
"-r") && !ShellCommandLineGetFlag (ParamPackage
, L
"-s") &&
1320 !ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1321 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_OPTION
), gShellNetwork1HiiHandle
);
1322 ShellStatus
= SHELL_INVALID_PARAMETER
;
1327 // To handle conflict options.
1329 if (((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-s"))) ||
1330 ((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l"))) ||
1331 ((ShellCommandLineGetFlag (ParamPackage
, L
"-s")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l")))) {
1332 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellNetwork1HiiHandle
, L
"ifconfig");
1333 ShellStatus
= SHELL_INVALID_PARAMETER
;
1337 Private
= AllocateZeroPool (sizeof (IFCONFIG_PRIVATE_DATA
));
1338 if (Private
== NULL
) {
1339 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1343 InitializeListHead (&Private
->IfList
);
1346 // To get interface name for the list option.
1348 if (ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1349 Private
->OpCode
= IfConfigOpList
;
1350 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-l");
1351 if (ValueStr
!= NULL
) {
1352 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1354 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1355 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1358 Private
->IfName
= Str
;
1363 // To get interface name for the clear option.
1365 if (ShellCommandLineGetFlag (ParamPackage
, L
"-r")) {
1366 Private
->OpCode
= IfConfigOpClear
;
1367 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-r");
1368 if (ValueStr
!= NULL
) {
1369 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1371 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1372 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1375 Private
->IfName
= Str
;
1380 // To get interface name and corresponding Args for the set option.
1382 if (ShellCommandLineGetFlag (ParamPackage
, L
"-s")) {
1383 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-s");
1384 if (ValueStr
== NULL
) {
1385 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_INTERFACE
), gShellNetwork1HiiHandle
);
1386 ShellStatus
= SHELL_INVALID_PARAMETER
;
1391 // To split the configuration into multi-section.
1393 ArgList
= SplitStrToList (ValueStr
, L
' ');
1394 if (ArgList
== NULL
) {
1395 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellNetwork1HiiHandle
, L
"ifconfig");
1396 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1400 Private
->OpCode
= IfConfigOpSet
;
1401 Private
->IfName
= ArgList
->Arg
;
1403 Private
->VarArg
= ArgList
->Next
;
1405 if (Private
->IfName
== NULL
|| Private
->VarArg
== NULL
) {
1406 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1407 ShellStatus
= SHELL_INVALID_PARAMETER
;
1413 // Main process of ifconfig.
1415 ShellStatus
= IfConfig (Private
);
1419 ShellCommandLineFreeVarList (ParamPackage
);
1421 if (Private
!= NULL
) {
1422 IfConfigCleanup (Private
);