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 - 2015, 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";
133 Split a string with specified separator and save the substring to a list.
135 @param[in] String The pointer of the input string.
136 @param[in] Separator The specified separator.
138 @return The pointer of headnode of ARG_LIST.
143 IN CONST CHAR16
*String
,
152 if (*String
== L
'\0') {
157 // Copy the CONST string to a local copy.
159 Str
= AllocateCopyPool (StrSize (String
), String
);
160 ASSERT (Str
!= NULL
);
164 // init a node for the list head.
166 ArgNode
= (ARG_LIST
*) AllocateZeroPool (sizeof (ARG_LIST
));
167 ASSERT (ArgNode
!= NULL
);
171 // Split the local copy and save in the list node.
173 while (*Str
!= L
'\0') {
174 if (*Str
== Separator
) {
176 ArgNode
->Arg
= ArgStr
;
178 ArgNode
->Next
= (ARG_LIST
*) AllocateZeroPool (sizeof (ARG_LIST
));
179 ASSERT (ArgNode
->Next
!= NULL
);
180 ArgNode
= ArgNode
->Next
;
186 ArgNode
->Arg
= ArgStr
;
187 ArgNode
->Next
= NULL
;
193 Check the correctness of input Args with '-s' option.
195 @param[in] CheckList The pointer of VAR_CHECK_ITEM array.
196 @param[in] Name The pointer of input arg.
197 @param[in] Init The switch to execute the check.
199 @return VarCheckOk Valid parameter or Initialize check successfully.
200 @return VarCheckDuplicate Duplicated parameter happened.
201 @return VarCheckConflict Conflicted parameter happened
202 @return VarCheckUnknown Unknown parameter.
206 IfConfigRetriveCheckListByName(
207 IN VAR_CHECK_ITEM
*CheckList
,
212 STATIC UINT32 CheckDuplicate
;
213 STATIC UINT32 CheckConflict
;
214 VAR_CHECK_CODE RtCode
;
226 Arg
= CheckList
[Index
];
229 // Check the Duplicated/Conflicted/Unknown input Args.
231 while (Arg
.FlagStr
!= NULL
) {
232 if (StrCmp (Arg
.FlagStr
, Name
) == 0) {
234 if (CheckDuplicate
& Arg
.FlagID
) {
235 RtCode
= VarCheckDuplicate
;
239 if (CheckConflict
& Arg
.ConflictMask
) {
240 RtCode
= VarCheckConflict
;
244 CheckDuplicate
|= Arg
.FlagID
;
245 CheckConflict
|= Arg
.ConflictMask
;
249 Arg
= CheckList
[++Index
];
252 if (Arg
.FlagStr
== NULL
) {
253 RtCode
= VarCheckUnknown
;
260 The notify function of create event when performing a manual config.
262 @param[in] Event The event this notify function registered to.
263 @param[in] Context Pointer to the context data registered to the event.
268 IfConfigManualAddressNotify (
273 *((BOOLEAN
*) Context
) = TRUE
;
278 Create an IP child, use it to start the auto configuration, then destroy it.
280 @param[in] Controller The controller which has the service installed.
281 @param[in] Image The image handle used to open service.
283 @retval EFI_SUCCESS The configuration is done.
288 IN EFI_HANDLE Controller
,
292 EFI_IP4_PROTOCOL
*Ip4
;
293 EFI_HANDLE Ip4Handle
;
294 EFI_IP4_CONFIG_DATA Ip4ConfigData
;
298 // Get the Ip4ServiceBinding Protocol
303 Status
= NetLibCreateServiceChild (
306 &gEfiIp4ServiceBindingProtocolGuid
,
310 if (EFI_ERROR (Status
)) {
314 Status
= gBS
->OpenProtocol (
316 &gEfiIp4ProtocolGuid
,
320 EFI_OPEN_PROTOCOL_GET_PROTOCOL
323 if (EFI_ERROR (Status
)) {
327 Ip4ConfigData
.DefaultProtocol
= EFI_IP_PROTO_ICMP
;
328 Ip4ConfigData
.AcceptAnyProtocol
= FALSE
;
329 Ip4ConfigData
.AcceptIcmpErrors
= FALSE
;
330 Ip4ConfigData
.AcceptBroadcast
= FALSE
;
331 Ip4ConfigData
.AcceptPromiscuous
= FALSE
;
332 Ip4ConfigData
.UseDefaultAddress
= TRUE
;
333 ZeroMem (&Ip4ConfigData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
334 ZeroMem (&Ip4ConfigData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
335 Ip4ConfigData
.TypeOfService
= 0;
336 Ip4ConfigData
.TimeToLive
= 1;
337 Ip4ConfigData
.DoNotFragment
= FALSE
;
338 Ip4ConfigData
.RawData
= FALSE
;
339 Ip4ConfigData
.ReceiveTimeout
= 0;
340 Ip4ConfigData
.TransmitTimeout
= 0;
342 Ip4
->Configure (Ip4
, &Ip4ConfigData
);
345 NetLibDestroyServiceChild (
348 &gEfiIp4ServiceBindingProtocolGuid
,
359 @param[in] Node The pointer of MAC address buffer.
360 @param[in] Size The size of MAC address buffer.
364 IfConfigPrintMacAddr (
371 ASSERT (Size
<= MACADDRMAXSIZE
);
373 for (Index
= 0; Index
< Size
; Index
++) {
374 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_BODY
), gShellNetwork1HiiHandle
, Node
[Index
]);
375 if (Index
+ 1 < Size
) {
376 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_COLON
), gShellNetwork1HiiHandle
);
380 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
385 The get current status of all handles.
387 @param[in] IfName The pointer of IfName(interface name).
388 @param[in] IfList The pointer of IfList(interface list).
390 @retval EFI_SUCCESS The get status processed successfully.
391 @retval others The get status process failed.
395 IfConfigGetInterfaceInfo (
397 IN LIST_ENTRY
*IfList
403 EFI_HANDLE
*HandleBuffer
;
404 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Cfg2
;
405 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
406 IFCONFIG_INTERFACE_CB
*IfCb
;
416 // Locate all the handles with ip4 service binding protocol.
418 Status
= gBS
->LocateHandleBuffer (
420 &gEfiIp4ServiceBindingProtocolGuid
,
425 if (EFI_ERROR (Status
) || (HandleNum
== 0)) {
430 // Enumerate all handles that installed with ip4 service binding protocol.
432 for (HandleIndex
= 0; HandleIndex
< HandleNum
; HandleIndex
++) {
438 // Ip4config protocol and ip4 service binding protocol are installed
439 // on the same handle.
441 ASSERT (HandleBuffer
!= NULL
);
442 Status
= gBS
->HandleProtocol (
443 HandleBuffer
[HandleIndex
],
444 &gEfiIp4Config2ProtocolGuid
,
448 if (EFI_ERROR (Status
)) {
453 // Get the interface information size.
455 Status
= Ip4Cfg2
->GetData (
457 Ip4Config2DataTypeInterfaceInfo
,
462 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
466 IfInfo
= AllocateZeroPool (DataSize
);
468 if (IfInfo
== NULL
) {
469 Status
= EFI_OUT_OF_RESOURCES
;
474 // Get the interface info.
476 Status
= Ip4Cfg2
->GetData (
478 Ip4Config2DataTypeInterfaceInfo
,
483 if (EFI_ERROR (Status
)) {
488 // Check the interface name if required.
490 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) != 0)) {
498 // Get the size of dns server list.
500 Status
= Ip4Cfg2
->GetData (
502 Ip4Config2DataTypeDnsServer
,
507 if ((Status
!= EFI_BUFFER_TOO_SMALL
) && (Status
!= EFI_NOT_FOUND
)) {
511 IfCb
= AllocateZeroPool (sizeof (IFCONFIG_INTERFACE_CB
) + DataSize
);
514 Status
= EFI_OUT_OF_RESOURCES
;
518 IfCb
->NicHandle
= HandleBuffer
[HandleIndex
];
519 IfCb
->IfInfo
= IfInfo
;
520 IfCb
->IfCfg
= Ip4Cfg2
;
521 IfCb
->DnsCnt
= (UINT32
) (DataSize
/ sizeof (EFI_IPv4_ADDRESS
));
524 // Get the dns server list if has.
527 Status
= Ip4Cfg2
->GetData (
529 Ip4Config2DataTypeDnsServer
,
534 if (EFI_ERROR (Status
)) {
540 // Get the config policy.
542 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
543 Status
= Ip4Cfg2
->GetData (
545 Ip4Config2DataTypePolicy
,
550 if (EFI_ERROR (Status
)) {
554 InsertTailList (IfList
, &IfCb
->Link
);
556 if ((IfName
!= NULL
) && (StrCmp (IfName
, IfInfo
->Name
) == 0)) {
558 // Only need the appointed interface, keep the allocated buffer.
566 if (HandleBuffer
!= NULL
) {
567 FreePool (HandleBuffer
);
574 if (IfInfo
!= NULL
) {
586 The list process of the ifconfig command.
588 @param[in] IfList The pointer of IfList(interface list).
590 @retval SHELL_SUCCESS The ifconfig command list processed successfully.
591 @retval others The ifconfig command list process failed.
595 IfConfigShowInterfaceInfo (
596 IN LIST_ENTRY
*IfList
601 IFCONFIG_INTERFACE_CB
*IfCb
;
602 BOOLEAN MediaPresent
;
603 EFI_IPv4_ADDRESS Gateway
;
608 if (IsListEmpty (IfList
)) {
609 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
613 // Go through the interface list.
615 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
616 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
618 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
621 // Print interface name.
623 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IF_NAME
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->Name
);
628 NetLibDetectMedia (IfCb
->NicHandle
, &MediaPresent
);
630 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media disconnected");
632 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE
), gShellNetwork1HiiHandle
, L
"Media present");
636 // Print interface config policy.
638 if (IfCb
->Policy
== Ip4Config2PolicyDhcp
) {
639 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_DHCP
), gShellNetwork1HiiHandle
);
641 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_MAN
), gShellNetwork1HiiHandle
);
645 // Print mac address of the interface.
647 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_HEAD
), gShellNetwork1HiiHandle
);
649 IfConfigPrintMacAddr (
650 IfCb
->IfInfo
->HwAddress
.Addr
,
651 IfCb
->IfInfo
->HwAddressSize
655 // Print IPv4 address list of the interface.
657 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_HEAD
), gShellNetwork1HiiHandle
);
663 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
664 gShellNetwork1HiiHandle
,
665 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[0],
666 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[1],
667 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[2],
668 (UINTN
)IfCb
->IfInfo
->StationAddress
.Addr
[3]
672 // Print subnet mask list of the interface.
674 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_SUBNET_MASK_HEAD
), gShellNetwork1HiiHandle
);
680 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
681 gShellNetwork1HiiHandle
,
682 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[0],
683 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[1],
684 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[2],
685 (UINTN
)IfCb
->IfInfo
->SubnetMask
.Addr
[3]
689 // Print default gateway of the interface.
691 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_GATEWAY_HEAD
), gShellNetwork1HiiHandle
);
693 ZeroMem (&Gateway
, sizeof (EFI_IPv4_ADDRESS
));
695 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
696 if ((CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0) &&
697 (CompareMem (&IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
, &mZeroIp4Addr
, sizeof (EFI_IPv4_ADDRESS
)) == 0) ){
698 CopyMem (&Gateway
, &IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
706 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY
),
707 gShellNetwork1HiiHandle
,
708 (UINTN
)Gateway
.Addr
[0],
709 (UINTN
)Gateway
.Addr
[1],
710 (UINTN
)Gateway
.Addr
[2],
711 (UINTN
)Gateway
.Addr
[3]
715 // Print route table entry.
717 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_ROUTES_SIZE
), gShellNetwork1HiiHandle
, IfCb
->IfInfo
->RouteTableSize
);
719 for (Index
= 0; Index
< IfCb
->IfInfo
->RouteTableSize
; Index
++) {
720 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_ROUTES_ENTRY_INDEX
), gShellNetwork1HiiHandle
, Index
);
726 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
727 gShellNetwork1HiiHandle
,
729 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[0],
730 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[1],
731 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[2],
732 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetAddress
.Addr
[3]
739 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
740 gShellNetwork1HiiHandle
,
742 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[0],
743 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[1],
744 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[2],
745 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].SubnetMask
.Addr
[3]
752 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR
),
753 gShellNetwork1HiiHandle
,
755 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[0],
756 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[1],
757 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[2],
758 (UINTN
)IfCb
->IfInfo
->RouteTable
[Index
].GatewayAddress
.Addr
[3]
763 // Print dns server addresses list of the interface if has.
765 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_HEAD
), gShellNetwork1HiiHandle
);
767 for (Index
= 0; Index
< IfCb
->DnsCnt
; Index
++) {
772 STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_BODY
),
773 gShellNetwork1HiiHandle
,
774 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[0],
775 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[1],
776 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[2],
777 (UINTN
) IfCb
->DnsAddr
[Index
].Addr
[3]
780 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE
), gShellNetwork1HiiHandle
);
784 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK
), gShellNetwork1HiiHandle
);
786 return SHELL_SUCCESS
;
790 The clean process of the ifconfig command to clear interface info.
792 @param[in] IfList The pointer of IfList(interface list).
794 @retval SHELL_SUCCESS The ifconfig command clean processed successfully.
795 @retval others The ifconfig command clean process failed.
799 IfConfigClearInterfaceInfo (
800 IN LIST_ENTRY
*IfList
804 SHELL_STATUS ShellStatus
;
807 IFCONFIG_INTERFACE_CB
*IfCb
;
808 EFI_IP4_CONFIG2_POLICY Policy
;
810 Policy
= Ip4Config2PolicyDhcp
;
811 Status
= EFI_SUCCESS
;
812 ShellStatus
= SHELL_SUCCESS
;
814 if (IsListEmpty (IfList
)) {
815 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
819 // Go through the interface list.
821 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, IfList
) {
822 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
824 Status
= IfCb
->IfCfg
->SetData (
826 Ip4Config2DataTypePolicy
,
827 sizeof (EFI_IP4_CONFIG2_POLICY
),
830 if (EFI_ERROR (Status
)) {
831 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
832 ShellStatus
= SHELL_ACCESS_DENIED
;
841 The set process of the ifconfig command.
843 @param[in] IfList The pointer of IfList(interface list).
844 @param[in] VarArg The pointer of ARG_LIST(Args with "-s" option).
846 @retval SHELL_SUCCESS The ifconfig command set processed successfully.
847 @retval others The ifconfig command set process failed.
851 IfConfigSetInterfaceInfo (
852 IN LIST_ENTRY
*IfList
,
857 SHELL_STATUS ShellStatus
;
858 IFCONFIG_INTERFACE_CB
*IfCb
;
859 VAR_CHECK_CODE CheckCode
;
860 EFI_EVENT TimeOutEvt
;
864 EFI_IP4_CONFIG2_POLICY Policy
;
865 EFI_IP4_CONFIG2_MANUAL_ADDRESS ManualAddress
;
867 EFI_IPv4_ADDRESS Gateway
;
868 EFI_IPv4_ADDRESS
*Dns
;
872 CONST CHAR16
* TempString
;
876 if (IsListEmpty (IfList
)) {
877 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE
), gShellNetwork1HiiHandle
);
878 return SHELL_INVALID_PARAMETER
;
882 // Make sure to set only one interface each time.
884 IfCb
= NET_LIST_USER_STRUCT (IfList
->ForwardLink
, IFCONFIG_INTERFACE_CB
, Link
);
885 Status
= EFI_SUCCESS
;
886 ShellStatus
= SHELL_SUCCESS
;
889 // Initialize check list mechanism.
891 CheckCode
= IfConfigRetriveCheckListByName(
898 // Create events & timers for asynchronous settings.
900 Status
= gBS
->CreateEvent (
907 if (EFI_ERROR (Status
)) {
908 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
909 ShellStatus
= SHELL_ACCESS_DENIED
;
913 Status
= gBS
->CreateEvent (
916 IfConfigManualAddressNotify
,
920 if (EFI_ERROR (Status
)) {
921 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
922 ShellStatus
= SHELL_ACCESS_DENIED
;
927 // Parse the setting variables.
929 while (VarArg
!= NULL
) {
931 // Check invalid parameters (duplication & unknown & conflict).
933 CheckCode
= IfConfigRetriveCheckListByName(
939 if (VarCheckOk
!= CheckCode
) {
941 case VarCheckDuplicate
:
942 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_DUPLICATE_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
945 case VarCheckConflict
:
946 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_CONFLICT_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
949 case VarCheckUnknown
:
951 // To handle unsupported option.
953 TempString
= PermanentString
;
954 if (StringNoCaseCompare(&VarArg
->Arg
, &TempString
) == 0) {
955 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
, PermanentString
);
960 // To handle unknown option.
962 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNKNOWN_COMMAND
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
969 VarArg
= VarArg
->Next
;
974 // Process valid variables.
976 if (StrCmp(VarArg
->Arg
, L
"dhcp") == 0) {
977 if (IfCb
->Policy
== Ip4Config2PolicyDhcp
) {
978 Status
= IfConfigStartIp4 (IfCb
->NicHandle
, gImageHandle
);
979 if (EFI_ERROR(Status
)) {
980 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
981 ShellStatus
= SHELL_ACCESS_DENIED
;
986 // Set dhcp config policy
988 Policy
= Ip4Config2PolicyDhcp
;
989 Status
= IfCb
->IfCfg
->SetData (
991 Ip4Config2DataTypePolicy
,
992 sizeof (EFI_IP4_CONFIG2_POLICY
),
995 if (EFI_ERROR(Status
)) {
996 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
997 ShellStatus
= SHELL_ACCESS_DENIED
;
1002 VarArg
= VarArg
->Next
;
1004 } else if (StrCmp (VarArg
->Arg
, L
"static") == 0) {
1005 VarArg
= VarArg
->Next
;
1006 if (VarArg
== NULL
) {
1007 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1008 ShellStatus
= SHELL_INVALID_PARAMETER
;
1012 ZeroMem (&ManualAddress
, sizeof (ManualAddress
));
1015 // Get manual IP address.
1017 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.Address
);
1018 if (EFI_ERROR(Status
)) {
1019 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1020 ShellStatus
= SHELL_INVALID_PARAMETER
;
1027 VarArg
= VarArg
->Next
;
1028 if (VarArg
== NULL
) {
1029 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1030 ShellStatus
= SHELL_INVALID_PARAMETER
;
1034 Status
= NetLibStrToIp4 (VarArg
->Arg
, &ManualAddress
.SubnetMask
);
1035 if (EFI_ERROR(Status
)) {
1036 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1037 ShellStatus
= SHELL_INVALID_PARAMETER
;
1044 VarArg
= VarArg
->Next
;
1045 if (VarArg
== NULL
) {
1046 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1047 ShellStatus
= SHELL_INVALID_PARAMETER
;
1051 Status
= NetLibStrToIp4 (VarArg
->Arg
, &Gateway
);
1052 if (EFI_ERROR(Status
)) {
1053 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, VarArg
->Arg
);
1054 ShellStatus
= SHELL_INVALID_PARAMETER
;
1059 // Set manual config policy.
1061 Policy
= Ip4Config2PolicyStatic
;
1062 Status
= IfCb
->IfCfg
->SetData (
1064 Ip4Config2DataTypePolicy
,
1065 sizeof (EFI_IP4_CONFIG2_POLICY
),
1068 if (EFI_ERROR(Status
)) {
1069 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1070 ShellStatus
= SHELL_ACCESS_DENIED
;
1075 // Set Manual Address.
1077 IsAddressOk
= FALSE
;
1079 Status
= IfCb
->IfCfg
->RegisterDataNotify (
1081 Ip4Config2DataTypeManualAddress
,
1084 if (EFI_ERROR (Status
)) {
1085 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1086 ShellStatus
= SHELL_ACCESS_DENIED
;
1090 DataSize
= sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS
);
1092 Status
= IfCb
->IfCfg
->SetData (
1094 Ip4Config2DataTypeManualAddress
,
1099 if (Status
== EFI_NOT_READY
) {
1100 gBS
->SetTimer (TimeOutEvt
, TimerRelative
, 50000000);
1102 while (EFI_ERROR (gBS
->CheckEvent (TimeOutEvt
))) {
1104 Status
= EFI_SUCCESS
;
1110 IfCb
->IfCfg
->UnregisterDataNotify (
1112 Ip4Config2DataTypeManualAddress
,
1116 if (EFI_ERROR (Status
)) {
1117 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1118 ShellStatus
= SHELL_ACCESS_DENIED
;
1125 DataSize
= sizeof (EFI_IPv4_ADDRESS
);
1127 Status
= IfCb
->IfCfg
->SetData (
1129 Ip4Config2DataTypeGateway
,
1133 if (EFI_ERROR (Status
)) {
1134 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED
), gShellNetwork1HiiHandle
, Status
);
1135 ShellStatus
= SHELL_ACCESS_DENIED
;
1139 VarArg
= VarArg
->Next
;
1141 } else if (StrCmp (VarArg
->Arg
, L
"dns") == 0) {
1143 // Get DNS addresses.
1145 VarArg
= VarArg
->Next
;
1148 while (Tmp
!= NULL
) {
1153 Dns
= AllocatePool (Index
* sizeof (EFI_IPv4_ADDRESS
));
1154 ASSERT(Dns
!= NULL
);
1157 while (Tmp
!= NULL
) {
1158 Status
= NetLibStrToIp4 (Tmp
->Arg
, Dns
+ Index
);
1159 if (EFI_ERROR(Status
)) {
1160 ShellPrintHiiEx(-1, -1, NULL
,STRING_TOKEN (STR_IFCONFIG_INVALID_IPADDRESS
), gShellNetwork1HiiHandle
, Tmp
->Arg
);
1161 ShellStatus
= SHELL_INVALID_PARAMETER
;
1171 // Set DNS addresses.
1173 DataSize
= Index
* sizeof (EFI_IPv4_ADDRESS
);
1175 Status
= IfCb
->IfCfg
->SetData (
1177 Ip4Config2DataTypeDnsServer
,
1181 if (EFI_ERROR (Status
)) {
1182 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellNetwork1HiiHandle
, L
"ifconfig");
1183 ShellStatus
= SHELL_ACCESS_DENIED
;
1199 The ifconfig command main process.
1201 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1203 @retval SHELL_SUCCESS ifconfig command processed successfully.
1204 @retval others The ifconfig command process failed.
1209 IN IFCONFIG_PRIVATE_DATA
*Private
1213 SHELL_STATUS ShellStatus
;
1215 ShellStatus
= SHELL_SUCCESS
;
1218 // Get configure information of all interfaces.
1220 Status
= IfConfigGetInterfaceInfo (
1224 if (EFI_ERROR (Status
)) {
1225 ShellStatus
= SHELL_NOT_FOUND
;
1229 switch (Private
->OpCode
) {
1230 case IfConfigOpList
:
1231 ShellStatus
= IfConfigShowInterfaceInfo (&Private
->IfList
);
1234 case IfConfigOpClear
:
1235 ShellStatus
= IfConfigClearInterfaceInfo (&Private
->IfList
);
1239 ShellStatus
= IfConfigSetInterfaceInfo (&Private
->IfList
, Private
->VarArg
);
1243 ShellStatus
= SHELL_UNSUPPORTED
;
1251 The ifconfig command cleanup process, free the allocated memory.
1253 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1258 IN IFCONFIG_PRIVATE_DATA
*Private
1262 LIST_ENTRY
*NextEntry
;
1263 IFCONFIG_INTERFACE_CB
*IfCb
;
1267 ASSERT (Private
!= NULL
);
1270 // Clean the list which save the set config Args.
1272 if (Private
->VarArg
!= NULL
) {
1273 ArgHead
= Private
->VarArg
;
1275 while (ArgHead
->Next
!= NULL
) {
1276 ArgNode
= ArgHead
->Next
;
1284 if (Private
->IfName
!= NULL
) {
1285 FreePool (Private
->IfName
);
1289 // Clean the IFCONFIG_INTERFACE_CB list.
1291 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->IfList
) {
1292 IfCb
= NET_LIST_USER_STRUCT (Entry
, IFCONFIG_INTERFACE_CB
, Link
);
1294 RemoveEntryList (&IfCb
->Link
);
1296 if (IfCb
->IfInfo
!= NULL
) {
1298 FreePool (IfCb
->IfInfo
);
1308 Function for 'ifconfig' command.
1310 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1311 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1313 @retval EFI_SUCCESS ifconfig command processed successfully.
1314 @retval others The ifconfig command process failed.
1319 ShellCommandRunIfconfig (
1320 IN EFI_HANDLE ImageHandle
,
1321 IN EFI_SYSTEM_TABLE
*SystemTable
1325 IFCONFIG_PRIVATE_DATA
*Private
;
1326 LIST_ENTRY
*ParamPackage
;
1327 SHELL_STATUS ShellStatus
;
1328 CONST CHAR16
*ValueStr
;
1330 CHAR16
*ProblemParam
;
1333 Status
= EFI_INVALID_PARAMETER
;
1335 ShellStatus
= SHELL_SUCCESS
;
1337 Status
= ShellCommandLineParseEx (mIfConfigCheckList
, &ParamPackage
, &ProblemParam
, TRUE
, FALSE
);
1338 if (EFI_ERROR (Status
)) {
1339 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
1340 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellNetwork1HiiHandle
, L
"ifconfig", ProblemParam
);
1341 FreePool(ProblemParam
);
1342 ShellStatus
= SHELL_INVALID_PARAMETER
;
1351 // To handle unsupported option.
1353 if (ShellCommandLineGetFlag (ParamPackage
, L
"-c")) {
1354 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION
), gShellNetwork1HiiHandle
,L
"-c");
1355 ShellStatus
= SHELL_INVALID_PARAMETER
;
1360 // To handle no option.
1362 if (!ShellCommandLineGetFlag (ParamPackage
, L
"-r") && !ShellCommandLineGetFlag (ParamPackage
, L
"-s") &&
1363 !ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1364 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_OPTION
), gShellNetwork1HiiHandle
);
1365 ShellStatus
= SHELL_INVALID_PARAMETER
;
1370 // To handle conflict options.
1372 if (((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-s"))) ||
1373 ((ShellCommandLineGetFlag (ParamPackage
, L
"-r")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l"))) ||
1374 ((ShellCommandLineGetFlag (ParamPackage
, L
"-s")) && (ShellCommandLineGetFlag (ParamPackage
, L
"-l")))) {
1375 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellNetwork1HiiHandle
, L
"ifconfig");
1376 ShellStatus
= SHELL_INVALID_PARAMETER
;
1380 Private
= AllocateZeroPool (sizeof (IFCONFIG_PRIVATE_DATA
));
1381 if (Private
== NULL
) {
1382 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1386 InitializeListHead (&Private
->IfList
);
1389 // To get interface name for the list option.
1391 if (ShellCommandLineGetFlag (ParamPackage
, L
"-l")) {
1392 Private
->OpCode
= IfConfigOpList
;
1393 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-l");
1394 if (ValueStr
!= NULL
) {
1395 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1396 ASSERT (Str
!= NULL
);
1397 Private
->IfName
= Str
;
1402 // To get interface name for the clear option.
1404 if (ShellCommandLineGetFlag (ParamPackage
, L
"-r")) {
1405 Private
->OpCode
= IfConfigOpClear
;
1406 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-r");
1407 if (ValueStr
!= NULL
) {
1408 Str
= AllocateCopyPool (StrSize (ValueStr
), ValueStr
);
1409 ASSERT (Str
!= NULL
);
1410 Private
->IfName
= Str
;
1415 // To get interface name and corresponding Args for the set option.
1417 if (ShellCommandLineGetFlag (ParamPackage
, L
"-s")) {
1418 ValueStr
= ShellCommandLineGetValue (ParamPackage
, L
"-s");
1419 if (ValueStr
== NULL
) {
1420 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_INTERFACE
), gShellNetwork1HiiHandle
);
1421 ShellStatus
= SHELL_INVALID_PARAMETER
;
1426 // To split the configuration into multi-section.
1428 ArgList
= SplitStrToList (ValueStr
, L
' ');
1429 ASSERT (ArgList
!= NULL
);
1431 Private
->OpCode
= IfConfigOpSet
;
1432 Private
->IfName
= ArgList
->Arg
;
1434 Private
->VarArg
= ArgList
->Next
;
1436 if (Private
->IfName
== NULL
|| Private
->VarArg
== NULL
) {
1437 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND
), gShellNetwork1HiiHandle
);
1438 ShellStatus
= SHELL_INVALID_PARAMETER
;
1444 // Main process of ifconfig.
1446 ShellStatus
= IfConfig (Private
);
1450 ShellCommandLineFreeVarList (ParamPackage
);
1452 if (Private
!= NULL
) {
1453 IfConfigCleanup (Private
);