]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellNetwork1CommandsLib/Ifconfig.c
ShellPkg: Get media status in ifconfig command
[mirror_edk2.git] / ShellPkg / Library / UefiShellNetwork1CommandsLib / Ifconfig.c
1 /** @file
2 The implementation for Shell command ifconfig based on IP4Config2 protocol.
3
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6
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.
11
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.
14
15 **/
16
17 #include "UefiShellNetwork1CommandsLib.h"
18
19 typedef enum {
20 IfConfigOpList = 1,
21 IfConfigOpSet = 2,
22 IfConfigOpClear = 3
23 } IFCONFIG_OPCODE;
24
25 typedef enum {
26 VarCheckReserved = -1,
27 VarCheckOk = 0,
28 VarCheckDuplicate,
29 VarCheckConflict,
30 VarCheckUnknown,
31 VarCheckLackValue,
32 VarCheckOutOfMem
33 } VAR_CHECK_CODE;
34
35 typedef enum {
36 FlagTypeSingle = 0,
37 FlagTypeNeedVar,
38 FlagTypeNeedSet,
39 FlagTypeSkipUnknown
40 } VAR_CHECK_FLAG_TYPE;
41
42 #define MACADDRMAXSIZE 32
43
44 typedef struct _IFCONFIG_INTERFACE_CB {
45 EFI_HANDLE NicHandle;
46 LIST_ENTRY Link;
47 EFI_IP4_CONFIG2_PROTOCOL *IfCfg;
48 EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo;
49 EFI_IP4_CONFIG2_POLICY Policy;
50 UINT32 DnsCnt;
51 EFI_IPv4_ADDRESS DnsAddr[1];
52 } IFCONFIG_INTERFACE_CB;
53
54 typedef struct _ARG_LIST ARG_LIST;
55
56 struct _ARG_LIST {
57 ARG_LIST *Next;
58 CHAR16 *Arg;
59 };
60
61 typedef struct _IFCONFIG4_PRIVATE_DATA {
62 LIST_ENTRY IfList;
63
64 UINT32 OpCode;
65 CHAR16 *IfName;
66 ARG_LIST *VarArg;
67 } IFCONFIG_PRIVATE_DATA;
68
69 typedef struct _VAR_CHECK_ITEM{
70 CHAR16 *FlagStr;
71 UINT32 FlagID;
72 UINT32 ConflictMask;
73 VAR_CHECK_FLAG_TYPE FlagType;
74 } VAR_CHECK_ITEM;
75
76 SHELL_PARAM_ITEM mIfConfigCheckList[] = {
77 {
78 L"-b",
79 TypeFlag
80 },
81 {
82 L"-l",
83 TypeValue
84 },
85 {
86 L"-r",
87 TypeValue
88 },
89 {
90 L"-c",
91 TypeValue
92 },
93 {
94 L"-s",
95 TypeMaxValue
96 },
97 {
98 NULL,
99 TypeMax
100 },
101 };
102
103 VAR_CHECK_ITEM mSetCheckList[] = {
104 {
105 L"static",
106 0x00000001,
107 0x00000001,
108 FlagTypeSingle
109 },
110 {
111 L"dhcp",
112 0x00000002,
113 0x00000001,
114 FlagTypeSingle
115 },
116 {
117 L"dns",
118 0x00000008,
119 0x00000004,
120 FlagTypeSingle
121 },
122 {
123 NULL,
124 0x0,
125 0x0,
126 FlagTypeSkipUnknown
127 },
128 };
129
130 STATIC CONST CHAR16 PermanentString[10] = L"PERMANENT";
131
132 /**
133 Split a string with specified separator and save the substring to a list.
134
135 @param[in] String The pointer of the input string.
136 @param[in] Separator The specified separator.
137
138 @return The pointer of headnode of ARG_LIST.
139
140 **/
141 ARG_LIST *
142 SplitStrToList (
143 IN CONST CHAR16 *String,
144 IN CHAR16 Separator
145 )
146 {
147 CHAR16 *Str;
148 CHAR16 *ArgStr;
149 ARG_LIST *ArgList;
150 ARG_LIST *ArgNode;
151
152 if (*String == L'\0') {
153 return NULL;
154 }
155
156 //
157 // Copy the CONST string to a local copy.
158 //
159 Str = AllocateCopyPool (StrSize (String), String);
160 ASSERT (Str != NULL);
161 ArgStr = Str;
162
163 //
164 // init a node for the list head.
165 //
166 ArgNode = (ARG_LIST *) AllocateZeroPool (sizeof (ARG_LIST));
167 ASSERT (ArgNode != NULL);
168 ArgList = ArgNode;
169
170 //
171 // Split the local copy and save in the list node.
172 //
173 while (*Str != L'\0') {
174 if (*Str == Separator) {
175 *Str = L'\0';
176 ArgNode->Arg = ArgStr;
177 ArgStr = Str + 1;
178 ArgNode->Next = (ARG_LIST *) AllocateZeroPool (sizeof (ARG_LIST));
179 ASSERT (ArgNode->Next != NULL);
180 ArgNode = ArgNode->Next;
181 }
182
183 Str++;
184 }
185
186 ArgNode->Arg = ArgStr;
187 ArgNode->Next = NULL;
188
189 return ArgList;
190 }
191
192 /**
193 Check the correctness of input Args with '-s' option.
194
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.
198
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.
203
204 **/
205 VAR_CHECK_CODE
206 IfConfigRetriveCheckListByName(
207 IN VAR_CHECK_ITEM *CheckList,
208 IN CHAR16 *Name,
209 IN BOOLEAN Init
210 )
211 {
212 STATIC UINT32 CheckDuplicate;
213 STATIC UINT32 CheckConflict;
214 VAR_CHECK_CODE RtCode;
215 UINT32 Index;
216 VAR_CHECK_ITEM Arg;
217
218 if (Init) {
219 CheckDuplicate = 0;
220 CheckConflict = 0;
221 return VarCheckOk;
222 }
223
224 RtCode = VarCheckOk;
225 Index = 0;
226 Arg = CheckList[Index];
227
228 //
229 // Check the Duplicated/Conflicted/Unknown input Args.
230 //
231 while (Arg.FlagStr != NULL) {
232 if (StrCmp (Arg.FlagStr, Name) == 0) {
233
234 if (CheckDuplicate & Arg.FlagID) {
235 RtCode = VarCheckDuplicate;
236 break;
237 }
238
239 if (CheckConflict & Arg.ConflictMask) {
240 RtCode = VarCheckConflict;
241 break;
242 }
243
244 CheckDuplicate |= Arg.FlagID;
245 CheckConflict |= Arg.ConflictMask;
246 break;
247 }
248
249 Arg = CheckList[++Index];
250 }
251
252 if (Arg.FlagStr == NULL) {
253 RtCode = VarCheckUnknown;
254 }
255
256 return RtCode;
257 }
258
259 /**
260 The notify function of create event when performing a manual config.
261
262 @param[in] Event The event this notify function registered to.
263 @param[in] Context Pointer to the context data registered to the event.
264
265 **/
266 VOID
267 EFIAPI
268 IfConfigManualAddressNotify (
269 IN EFI_EVENT Event,
270 IN VOID *Context
271 )
272 {
273 *((BOOLEAN *) Context) = TRUE;
274 }
275
276
277 /**
278 Create an IP child, use it to start the auto configuration, then destroy it.
279
280 @param[in] Controller The controller which has the service installed.
281 @param[in] Image The image handle used to open service.
282
283 @retval EFI_SUCCESS The configuration is done.
284 **/
285 EFI_STATUS
286 EFIAPI
287 IfConfigStartIp4(
288 IN EFI_HANDLE Controller,
289 IN EFI_HANDLE Image
290 )
291 {
292 EFI_IP4_PROTOCOL *Ip4;
293 EFI_HANDLE Ip4Handle;
294 EFI_IP4_CONFIG_DATA Ip4ConfigData;
295 EFI_STATUS Status;
296
297 //
298 // Get the Ip4ServiceBinding Protocol
299 //
300 Ip4Handle = NULL;
301 Ip4 = NULL;
302
303 Status = NetLibCreateServiceChild (
304 Controller,
305 Image,
306 &gEfiIp4ServiceBindingProtocolGuid,
307 &Ip4Handle
308 );
309
310 if (EFI_ERROR (Status)) {
311 return Status;
312 }
313
314 Status = gBS->OpenProtocol (
315 Ip4Handle,
316 &gEfiIp4ProtocolGuid,
317 (VOID **) &Ip4,
318 Controller,
319 Image,
320 EFI_OPEN_PROTOCOL_GET_PROTOCOL
321 );
322
323 if (EFI_ERROR (Status)) {
324 goto ON_EXIT;
325 }
326
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;
341
342 Ip4->Configure (Ip4, &Ip4ConfigData);
343
344 ON_EXIT:
345 NetLibDestroyServiceChild (
346 Controller,
347 Image,
348 &gEfiIp4ServiceBindingProtocolGuid,
349 Ip4Handle
350 );
351
352 return Status;
353 }
354
355
356 /**
357 Print MAC address.
358
359 @param[in] Node The pointer of MAC address buffer.
360 @param[in] Size The size of MAC address buffer.
361
362 **/
363 VOID
364 IfConfigPrintMacAddr (
365 IN UINT8 *Node,
366 IN UINT32 Size
367 )
368 {
369 UINTN Index;
370
371 ASSERT (Size <= MACADDRMAXSIZE);
372
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);
377 }
378 }
379
380 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE), gShellNetwork1HiiHandle);
381 }
382
383
384 /**
385 The get current status of all handles.
386
387 @param[in] IfName The pointer of IfName(interface name).
388 @param[in] IfList The pointer of IfList(interface list).
389
390 @retval EFI_SUCCESS The get status processed successfully.
391 @retval others The get status process failed.
392
393 **/
394 EFI_STATUS
395 IfConfigGetInterfaceInfo (
396 IN CHAR16 *IfName,
397 IN LIST_ENTRY *IfList
398 )
399 {
400 EFI_STATUS Status;
401 UINTN HandleIndex;
402 UINTN HandleNum;
403 EFI_HANDLE *HandleBuffer;
404 EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2;
405 EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo;
406 IFCONFIG_INTERFACE_CB *IfCb;
407 UINTN DataSize;
408
409 HandleBuffer = NULL;
410 HandleNum = 0;
411
412 IfInfo = NULL;
413 IfCb = NULL;
414
415 //
416 // Locate all the handles with ip4 service binding protocol.
417 //
418 Status = gBS->LocateHandleBuffer (
419 ByProtocol,
420 &gEfiIp4ServiceBindingProtocolGuid,
421 NULL,
422 &HandleNum,
423 &HandleBuffer
424 );
425 if (EFI_ERROR (Status) || (HandleNum == 0)) {
426 return EFI_ABORTED;
427 }
428
429 //
430 // Enumerate all handles that installed with ip4 service binding protocol.
431 //
432 for (HandleIndex = 0; HandleIndex < HandleNum; HandleIndex++) {
433 IfCb = NULL;
434 IfInfo = NULL;
435 DataSize = 0;
436
437 //
438 // Ip4config protocol and ip4 service binding protocol are installed
439 // on the same handle.
440 //
441 ASSERT (HandleBuffer != NULL);
442 Status = gBS->HandleProtocol (
443 HandleBuffer[HandleIndex],
444 &gEfiIp4Config2ProtocolGuid,
445 (VOID **) &Ip4Cfg2
446 );
447
448 if (EFI_ERROR (Status)) {
449 goto ON_ERROR;
450 }
451
452 //
453 // Get the interface information size.
454 //
455 Status = Ip4Cfg2->GetData (
456 Ip4Cfg2,
457 Ip4Config2DataTypeInterfaceInfo,
458 &DataSize,
459 NULL
460 );
461
462 if (Status != EFI_BUFFER_TOO_SMALL) {
463 goto ON_ERROR;
464 }
465
466 IfInfo = AllocateZeroPool (DataSize);
467
468 if (IfInfo == NULL) {
469 Status = EFI_OUT_OF_RESOURCES;
470 goto ON_ERROR;
471 }
472
473 //
474 // Get the interface info.
475 //
476 Status = Ip4Cfg2->GetData (
477 Ip4Cfg2,
478 Ip4Config2DataTypeInterfaceInfo,
479 &DataSize,
480 IfInfo
481 );
482
483 if (EFI_ERROR (Status)) {
484 goto ON_ERROR;
485 }
486
487 //
488 // Check the interface name if required.
489 //
490 if ((IfName != NULL) && (StrCmp (IfName, IfInfo->Name) != 0)) {
491 FreePool (IfInfo);
492 continue;
493 }
494
495 DataSize = 0;
496
497 //
498 // Get the size of dns server list.
499 //
500 Status = Ip4Cfg2->GetData (
501 Ip4Cfg2,
502 Ip4Config2DataTypeDnsServer,
503 &DataSize,
504 NULL
505 );
506
507 if ((Status != EFI_BUFFER_TOO_SMALL) && (Status != EFI_NOT_FOUND)) {
508 goto ON_ERROR;
509 }
510
511 IfCb = AllocateZeroPool (sizeof (IFCONFIG_INTERFACE_CB) + DataSize);
512
513 if (IfCb == NULL) {
514 Status = EFI_OUT_OF_RESOURCES;
515 goto ON_ERROR;
516 }
517
518 IfCb->NicHandle = HandleBuffer[HandleIndex];
519 IfCb->IfInfo = IfInfo;
520 IfCb->IfCfg = Ip4Cfg2;
521 IfCb->DnsCnt = (UINT32) (DataSize / sizeof (EFI_IPv4_ADDRESS));
522
523 //
524 // Get the dns server list if has.
525 //
526 if (DataSize > 0) {
527 Status = Ip4Cfg2->GetData (
528 Ip4Cfg2,
529 Ip4Config2DataTypeDnsServer,
530 &DataSize,
531 IfCb->DnsAddr
532 );
533
534 if (EFI_ERROR (Status)) {
535 goto ON_ERROR;
536 }
537 }
538
539 //
540 // Get the config policy.
541 //
542 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
543 Status = Ip4Cfg2->GetData (
544 Ip4Cfg2,
545 Ip4Config2DataTypePolicy,
546 &DataSize,
547 &IfCb->Policy
548 );
549
550 if (EFI_ERROR (Status)) {
551 goto ON_ERROR;
552 }
553
554 InsertTailList (IfList, &IfCb->Link);
555
556 if ((IfName != NULL) && (StrCmp (IfName, IfInfo->Name) == 0)) {
557 //
558 // Only need the appointed interface, keep the allocated buffer.
559 //
560 IfCb = NULL;
561 IfInfo = NULL;
562 break;
563 }
564 }
565
566 if (HandleBuffer != NULL) {
567 FreePool (HandleBuffer);
568 }
569
570 return EFI_SUCCESS;
571
572 ON_ERROR:
573
574 if (IfInfo != NULL) {
575 FreePool (IfInfo);
576 }
577
578 if (IfCb != NULL) {
579 FreePool (IfCb);
580 }
581
582 return Status;
583 }
584
585 /**
586 The list process of the ifconfig command.
587
588 @param[in] IfList The pointer of IfList(interface list).
589
590 @retval EFI_SUCCESS The ifconfig command list processed successfully.
591 @retval others The ifconfig command list process failed.
592
593 **/
594 EFI_STATUS
595 IfConfigShowInterfaceInfo (
596 IN LIST_ENTRY *IfList
597 )
598 {
599 LIST_ENTRY *Entry;
600 LIST_ENTRY *Next;
601 IFCONFIG_INTERFACE_CB *IfCb;
602 BOOLEAN MediaPresent;
603 EFI_IPv4_ADDRESS Gateway;
604 UINT32 Index;
605
606 MediaPresent = TRUE;
607
608 if (IsListEmpty (IfList)) {
609 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE), gShellNetwork1HiiHandle);
610 }
611
612 //
613 // Go through the interface list.
614 //
615 NET_LIST_FOR_EACH_SAFE (Entry, Next, IfList) {
616 IfCb = NET_LIST_USER_STRUCT (Entry, IFCONFIG_INTERFACE_CB, Link);
617
618 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK), gShellNetwork1HiiHandle);
619
620 //
621 // Print interface name.
622 //
623 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_IF_NAME), gShellNetwork1HiiHandle, IfCb->IfInfo->Name);
624
625 //
626 // Get Media State.
627 //
628 NetLibDetectMedia (IfCb->NicHandle, &MediaPresent);
629 if (!MediaPresent) {
630 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE), gShellNetwork1HiiHandle, L"Media disconnected");
631 } else {
632 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_MEDIA_STATE), gShellNetwork1HiiHandle, L"Media present");
633 }
634
635 //
636 // Print interface config policy.
637 //
638 if (IfCb->Policy == Ip4Config2PolicyDhcp) {
639 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_DHCP), gShellNetwork1HiiHandle);
640 } else {
641 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_MAN), gShellNetwork1HiiHandle);
642 }
643
644 //
645 // Print mac address of the interface.
646 //
647 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_HEAD), gShellNetwork1HiiHandle);
648
649 IfConfigPrintMacAddr (
650 IfCb->IfInfo->HwAddress.Addr,
651 IfCb->IfInfo->HwAddressSize
652 );
653
654 //
655 // Print IPv4 address list of the interface.
656 //
657 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_HEAD), gShellNetwork1HiiHandle);
658
659 ShellPrintHiiEx(
660 -1,
661 -1,
662 NULL,
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]
669 );
670
671 //
672 // Print subnet mask list of the interface.
673 //
674 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_SUBNET_MASK_HEAD), gShellNetwork1HiiHandle);
675
676 ShellPrintHiiEx(
677 -1,
678 -1,
679 NULL,
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]
686 );
687
688 //
689 // Print default gateway of the interface.
690 //
691 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_GATEWAY_HEAD), gShellNetwork1HiiHandle);
692
693 ZeroMem (&Gateway, sizeof (EFI_IPv4_ADDRESS));
694
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));
699 }
700 }
701
702 ShellPrintHiiEx(
703 -1,
704 -1,
705 NULL,
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]
712 );
713
714 //
715 // Print route table entry.
716 //
717 ShellPrintHiiEx(-1, -1, NULL,STRING_TOKEN (STR_IFCONFIG_ROUTES_SIZE), gShellNetwork1HiiHandle, IfCb->IfInfo->RouteTableSize);
718
719 for (Index = 0; Index < IfCb->IfInfo->RouteTableSize; Index++) {
720 ShellPrintHiiEx(-1, -1, NULL,STRING_TOKEN (STR_IFCONFIG_ROUTES_ENTRY_INDEX), gShellNetwork1HiiHandle, Index);
721
722 ShellPrintHiiEx(
723 -1,
724 -1,
725 NULL,
726 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR),
727 gShellNetwork1HiiHandle,
728 L"Subnet ",
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]
733 );
734
735 ShellPrintHiiEx(
736 -1,
737 -1,
738 NULL,
739 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR),
740 gShellNetwork1HiiHandle,
741 L"Netmask",
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]
746 );
747
748 ShellPrintHiiEx(
749 -1,
750 -1,
751 NULL,
752 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR),
753 gShellNetwork1HiiHandle,
754 L"Gateway",
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]
759 );
760 }
761
762 //
763 // Print dns server addresses list of the interface if has.
764 //
765 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_HEAD), gShellNetwork1HiiHandle);
766
767 for (Index = 0; Index < IfCb->DnsCnt; Index++) {
768 ShellPrintHiiEx(
769 -1,
770 -1,
771 NULL,
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]
778 );
779
780 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE), gShellNetwork1HiiHandle);
781 }
782 }
783
784 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK), gShellNetwork1HiiHandle);
785
786 return EFI_SUCCESS;
787 }
788
789 /**
790 The clean process of the ifconfig command to clear interface info.
791
792 @param[in] IfList The pointer of IfList(interface list).
793
794 @retval EFI_SUCCESS The ifconfig command clean processed successfully.
795 @retval others The ifconfig command clean process failed.
796
797 **/
798 EFI_STATUS
799 IfConfigClearInterfaceInfo (
800 IN LIST_ENTRY *IfList
801 )
802 {
803 EFI_STATUS Status;
804 LIST_ENTRY *Entry;
805 LIST_ENTRY *Next;
806 IFCONFIG_INTERFACE_CB *IfCb;
807 EFI_IP4_CONFIG2_POLICY Policy;
808
809 Policy = Ip4Config2PolicyDhcp;
810 Status = EFI_SUCCESS;
811
812 if (IsListEmpty (IfList)) {
813 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE), gShellNetwork1HiiHandle);
814 }
815
816 //
817 // Go through the interface list.
818 //
819 NET_LIST_FOR_EACH_SAFE (Entry, Next, IfList) {
820 IfCb = NET_LIST_USER_STRUCT (Entry, IFCONFIG_INTERFACE_CB, Link);
821
822 Status = IfCb->IfCfg->SetData (
823 IfCb->IfCfg,
824 Ip4Config2DataTypePolicy,
825 sizeof (EFI_IP4_CONFIG2_POLICY),
826 &Policy
827 );
828
829 if (EFI_ERROR (Status)) {
830 break;
831 }
832 }
833
834 return Status;
835 }
836
837 /**
838 The set process of the ifconfig command.
839
840 @param[in] IfList The pointer of IfList(interface list).
841 @param[in] VarArg The pointer of ARG_LIST(Args with "-s" option).
842
843 @retval EFI_SUCCESS The ifconfig command set processed successfully.
844 @retval others The ifconfig command set process failed.
845
846 **/
847 EFI_STATUS
848 IfConfigSetInterfaceInfo (
849 IN LIST_ENTRY *IfList,
850 IN ARG_LIST *VarArg
851 )
852 {
853
854 EFI_STATUS Status;
855 IFCONFIG_INTERFACE_CB *IfCb;
856 VAR_CHECK_CODE CheckCode;
857 EFI_EVENT TimeOutEvt;
858 EFI_EVENT MappedEvt;
859 BOOLEAN IsAddressOk;
860
861 EFI_IP4_CONFIG2_POLICY Policy;
862 EFI_IP4_CONFIG2_MANUAL_ADDRESS ManualAddress;
863 UINTN DataSize;
864 EFI_IPv4_ADDRESS Gateway;
865 EFI_IPv4_ADDRESS *Dns;
866 ARG_LIST *Tmp;
867 UINTN Index;
868
869 CONST CHAR16* TempString;
870
871 Dns = NULL;
872
873 if (IsListEmpty (IfList)) {
874 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE), gShellNetwork1HiiHandle);
875 return EFI_INVALID_PARAMETER;
876 }
877
878 //
879 // Make sure to set only one interface each time.
880 //
881 IfCb = NET_LIST_USER_STRUCT (IfList->ForwardLink, IFCONFIG_INTERFACE_CB, Link);
882 Status = EFI_SUCCESS;
883
884 //
885 // Initialize check list mechanism.
886 //
887 CheckCode = IfConfigRetriveCheckListByName(
888 NULL,
889 NULL,
890 TRUE
891 );
892
893 //
894 // Create events & timers for asynchronous settings.
895 //
896 Status = gBS->CreateEvent (
897 EVT_TIMER,
898 TPL_CALLBACK,
899 NULL,
900 NULL,
901 &TimeOutEvt
902 );
903 if (EFI_ERROR (Status)) {
904 goto ON_EXIT;
905 }
906
907 Status = gBS->CreateEvent (
908 EVT_NOTIFY_SIGNAL,
909 TPL_NOTIFY,
910 IfConfigManualAddressNotify,
911 &IsAddressOk,
912 &MappedEvt
913 );
914 if (EFI_ERROR (Status)) {
915 goto ON_EXIT;
916 }
917
918 //
919 // Parse the setting variables.
920 //
921 while (VarArg != NULL) {
922 //
923 // Check invalid parameters (duplication & unknown & conflict).
924 //
925 CheckCode = IfConfigRetriveCheckListByName(
926 mSetCheckList,
927 VarArg->Arg,
928 FALSE
929 );
930
931 if (VarCheckOk != CheckCode) {
932 switch (CheckCode) {
933 case VarCheckDuplicate:
934 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_DUPLICATE_COMMAND), gShellNetwork1HiiHandle, VarArg->Arg);
935 break;
936
937 case VarCheckConflict:
938 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_CONFLICT_COMMAND), gShellNetwork1HiiHandle, VarArg->Arg);
939 break;
940
941 case VarCheckUnknown:
942 //
943 // To handle unsupported option.
944 //
945 TempString = PermanentString;
946 if (StringNoCaseCompare(&VarArg->Arg, &TempString) == 0) {
947 ShellPrintHiiEx(-1, -1, NULL,STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION), gShellNetwork1HiiHandle, PermanentString);
948 goto ON_EXIT;
949 }
950
951 //
952 // To handle unknown option.
953 //
954 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_UNKNOWN_COMMAND), gShellNetwork1HiiHandle, VarArg->Arg);
955 break;
956
957 default:
958 break;
959 }
960
961 VarArg = VarArg->Next;
962 continue;
963 }
964
965 //
966 // Process valid variables.
967 //
968 if (StrCmp(VarArg->Arg, L"dhcp") == 0) {
969 if (IfCb->Policy == Ip4Config2PolicyDhcp) {
970 Status = IfConfigStartIp4 (IfCb->NicHandle, gImageHandle);
971 if (EFI_ERROR(Status)) {
972 goto ON_EXIT;
973 }
974 } else {
975 //
976 // Set dhcp config policy
977 //
978 Policy = Ip4Config2PolicyDhcp;
979 Status = IfCb->IfCfg->SetData (
980 IfCb->IfCfg,
981 Ip4Config2DataTypePolicy,
982 sizeof (EFI_IP4_CONFIG2_POLICY),
983 &Policy
984 );
985 if (EFI_ERROR(Status)) {
986 goto ON_EXIT;
987 }
988 }
989
990 VarArg= VarArg->Next;
991
992 } else if (StrCmp (VarArg->Arg, L"static") == 0) {
993 //
994 // Set manual config policy.
995 //
996 Policy = Ip4Config2PolicyStatic;
997 Status = IfCb->IfCfg->SetData (
998 IfCb->IfCfg,
999 Ip4Config2DataTypePolicy,
1000 sizeof (EFI_IP4_CONFIG2_POLICY),
1001 &Policy
1002 );
1003
1004 if (EFI_ERROR(Status)) {
1005 goto ON_EXIT;
1006 }
1007
1008 VarArg= VarArg->Next;
1009
1010 ZeroMem (&ManualAddress, sizeof (ManualAddress));
1011
1012 //
1013 // Get manual IP address.
1014 //
1015 Status = NetLibStrToIp4 (VarArg->Arg, &ManualAddress.Address);
1016 if (EFI_ERROR(Status)) {
1017 goto ON_EXIT;
1018 }
1019
1020 //
1021 // Get subnetmask.
1022 //
1023 VarArg = VarArg->Next;
1024 Status = NetLibStrToIp4 (VarArg->Arg, &ManualAddress.SubnetMask);
1025 if (EFI_ERROR(Status)) {
1026 goto ON_EXIT;
1027 }
1028
1029 //
1030 // Get gateway.
1031 //
1032 VarArg = VarArg->Next;
1033 Status = NetLibStrToIp4 (VarArg->Arg, &Gateway);
1034 if (EFI_ERROR(Status)) {
1035 goto ON_EXIT;
1036 }
1037
1038 IsAddressOk = FALSE;
1039
1040 Status = IfCb->IfCfg->RegisterDataNotify (
1041 IfCb->IfCfg,
1042 Ip4Config2DataTypeManualAddress,
1043 MappedEvt
1044 );
1045 if (EFI_ERROR (Status)) {
1046 goto ON_EXIT;
1047 }
1048
1049 DataSize = sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS);
1050
1051 Status = IfCb->IfCfg->SetData (
1052 IfCb->IfCfg,
1053 Ip4Config2DataTypeManualAddress,
1054 DataSize,
1055 &ManualAddress
1056 );
1057
1058 if (Status == EFI_NOT_READY) {
1059 gBS->SetTimer (TimeOutEvt, TimerRelative, 50000000);
1060
1061 while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {
1062 if (IsAddressOk) {
1063 Status = EFI_SUCCESS;
1064 break;
1065 }
1066 }
1067 }
1068
1069 IfCb->IfCfg->UnregisterDataNotify (
1070 IfCb->IfCfg,
1071 Ip4Config2DataTypeManualAddress,
1072 MappedEvt
1073 );
1074
1075 if (EFI_ERROR (Status)) {
1076 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED), gShellNetwork1HiiHandle, Status);
1077 goto ON_EXIT;
1078 }
1079
1080 //
1081 // Set gateway.
1082 //
1083 DataSize = sizeof (EFI_IPv4_ADDRESS);
1084
1085 Status = IfCb->IfCfg->SetData (
1086 IfCb->IfCfg,
1087 Ip4Config2DataTypeGateway,
1088 DataSize,
1089 &Gateway
1090 );
1091 VarArg = VarArg->Next;
1092
1093 } else if (StrCmp (VarArg->Arg, L"dns") == 0) {
1094 //
1095 // Get DNS addresses.
1096 //
1097 VarArg = VarArg->Next;
1098 Tmp = VarArg;
1099 Index = 0;
1100 while (Tmp != NULL) {
1101 Index ++;
1102 Tmp = Tmp->Next;
1103 }
1104
1105 Dns = AllocatePool (Index * sizeof (EFI_IPv4_ADDRESS));
1106 ASSERT(Dns != NULL);
1107 Tmp = VarArg;
1108 Index = 0;
1109 while (Tmp != NULL) {
1110 Status = NetLibStrToIp4 (Tmp->Arg, Dns + Index);
1111 if (EFI_ERROR(Status)) {
1112 goto ON_EXIT;
1113 }
1114 Index ++;
1115 Tmp = Tmp->Next;
1116 }
1117
1118 VarArg = Tmp;
1119
1120 //
1121 // Set DNS addresses.
1122 //
1123 DataSize = Index * sizeof (EFI_IPv4_ADDRESS);
1124
1125 Status = IfCb->IfCfg->SetData (
1126 IfCb->IfCfg,
1127 Ip4Config2DataTypeDnsServer,
1128 DataSize,
1129 Dns
1130 );
1131 }
1132 }
1133
1134 ON_EXIT:
1135 if (Dns != NULL) {
1136 FreePool (Dns);
1137 }
1138
1139 return Status;
1140
1141 }
1142
1143 /**
1144 The ifconfig command main process.
1145
1146 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1147
1148 @retval EFI_SUCCESS ifconfig command processed successfully.
1149 @retval others The ifconfig command process failed.
1150
1151 **/
1152 EFI_STATUS
1153 IfConfig (
1154 IN IFCONFIG_PRIVATE_DATA *Private
1155 )
1156 {
1157 EFI_STATUS Status;
1158
1159 //
1160 // Get configure information of all interfaces.
1161 //
1162 Status = IfConfigGetInterfaceInfo (
1163 Private->IfName,
1164 &Private->IfList
1165 );
1166
1167 if (EFI_ERROR (Status)) {
1168 goto ON_EXIT;
1169 }
1170
1171 switch (Private->OpCode) {
1172 case IfConfigOpList:
1173 Status = IfConfigShowInterfaceInfo (&Private->IfList);
1174 break;
1175
1176 case IfConfigOpClear:
1177 Status = IfConfigClearInterfaceInfo (&Private->IfList);
1178 break;
1179
1180 case IfConfigOpSet:
1181 Status = IfConfigSetInterfaceInfo (&Private->IfList, Private->VarArg);
1182 break;
1183
1184 default:
1185 Status = EFI_ABORTED;
1186 }
1187
1188 ON_EXIT:
1189
1190 return Status;
1191 }
1192
1193 /**
1194 The ifconfig command cleanup process, free the allocated memory.
1195
1196 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1197
1198 **/
1199 VOID
1200 IfConfigCleanup (
1201 IN IFCONFIG_PRIVATE_DATA *Private
1202 )
1203 {
1204 LIST_ENTRY *Entry;
1205 LIST_ENTRY *NextEntry;
1206 IFCONFIG_INTERFACE_CB *IfCb;
1207 ARG_LIST *ArgNode;
1208 ARG_LIST *ArgHead;
1209
1210 ASSERT (Private != NULL);
1211
1212 //
1213 // Clean the list which save the set config Args.
1214 //
1215 if (Private->VarArg != NULL) {
1216 ArgHead = Private->VarArg;
1217
1218 while (ArgHead->Next != NULL) {
1219 ArgNode = ArgHead->Next;
1220 FreePool (ArgHead);
1221 ArgHead = ArgNode;
1222 }
1223
1224 FreePool (ArgHead);
1225 }
1226
1227 if (Private->IfName != NULL) {
1228 FreePool (Private->IfName);
1229 }
1230
1231 //
1232 // Clean the IFCONFIG_INTERFACE_CB list.
1233 //
1234 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->IfList) {
1235 IfCb = NET_LIST_USER_STRUCT (Entry, IFCONFIG_INTERFACE_CB, Link);
1236
1237 RemoveEntryList (&IfCb->Link);
1238
1239 if (IfCb->IfInfo != NULL) {
1240
1241 FreePool (IfCb->IfInfo);
1242 }
1243
1244 FreePool (IfCb);
1245 }
1246
1247 FreePool (Private);
1248 }
1249
1250 /**
1251 Function for 'ifconfig' command.
1252
1253 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1254 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1255
1256 @retval EFI_SUCCESS ifconfig command processed successfully.
1257 @retval others The ifconfig command process failed.
1258
1259 **/
1260 SHELL_STATUS
1261 EFIAPI
1262 ShellCommandRunIfconfig (
1263 IN EFI_HANDLE ImageHandle,
1264 IN EFI_SYSTEM_TABLE *SystemTable
1265 )
1266 {
1267 EFI_STATUS Status;
1268 IFCONFIG_PRIVATE_DATA *Private;
1269 LIST_ENTRY *ParamPackage;
1270 CONST CHAR16 *ValueStr;
1271 ARG_LIST *ArgList;
1272 CHAR16 *ProblemParam;
1273 CHAR16 *Str;
1274
1275 Private = NULL;
1276
1277 Status = ShellCommandLineParseEx (mIfConfigCheckList, &ParamPackage, &ProblemParam, TRUE, FALSE);
1278 if (EFI_ERROR (Status)) {
1279 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellNetwork1HiiHandle, L"ifconfig", ProblemParam);
1280 goto ON_EXIT;
1281 }
1282
1283 //
1284 // To handle unsupported option.
1285 //
1286 if (ShellCommandLineGetFlag (ParamPackage, L"-c")) {
1287 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION), gShellNetwork1HiiHandle,L"-c");
1288 goto ON_EXIT;
1289 }
1290
1291 //
1292 // To handle no option.
1293 //
1294 if (!ShellCommandLineGetFlag (ParamPackage, L"-r") && !ShellCommandLineGetFlag (ParamPackage, L"-s") &&
1295 !ShellCommandLineGetFlag (ParamPackage, L"-l")) {
1296 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_LACK_OPTION), gShellNetwork1HiiHandle);
1297 goto ON_EXIT;
1298 }
1299
1300 //
1301 // To handle conflict options.
1302 //
1303 if (((ShellCommandLineGetFlag (ParamPackage, L"-r")) && (ShellCommandLineGetFlag (ParamPackage, L"-s"))) ||
1304 ((ShellCommandLineGetFlag (ParamPackage, L"-r")) && (ShellCommandLineGetFlag (ParamPackage, L"-l"))) ||
1305 ((ShellCommandLineGetFlag (ParamPackage, L"-s")) && (ShellCommandLineGetFlag (ParamPackage, L"-l")))) {
1306 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CON), gShellNetwork1HiiHandle, L"ifconfig");
1307 goto ON_EXIT;
1308 }
1309
1310 Status = EFI_INVALID_PARAMETER;
1311
1312 Private = AllocateZeroPool (sizeof (IFCONFIG_PRIVATE_DATA));
1313
1314 if (Private == NULL) {
1315 Status = EFI_OUT_OF_RESOURCES;
1316 goto ON_EXIT;
1317 }
1318
1319 InitializeListHead (&Private->IfList);
1320
1321 //
1322 // To get interface name for the list option.
1323 //
1324 if (ShellCommandLineGetFlag (ParamPackage, L"-l")) {
1325 Private->OpCode = IfConfigOpList;
1326 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-l");
1327 if (ValueStr != NULL) {
1328 Str = AllocateCopyPool (StrSize (ValueStr), ValueStr);
1329 ASSERT (Str != NULL);
1330 Private->IfName = Str;
1331 }
1332 }
1333
1334 //
1335 // To get interface name for the clear option.
1336 //
1337 if (ShellCommandLineGetFlag (ParamPackage, L"-r")) {
1338 Private->OpCode = IfConfigOpClear;
1339 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-r");
1340 if (ValueStr != NULL) {
1341 Str = AllocateCopyPool (StrSize (ValueStr), ValueStr);
1342 ASSERT (Str != NULL);
1343 Private->IfName = Str;
1344 }
1345 }
1346
1347 //
1348 // To get interface name and corresponding Args for the set option.
1349 //
1350 if (ShellCommandLineGetFlag (ParamPackage, L"-s")) {
1351 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-s");
1352 if (ValueStr == NULL) {
1353 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_LACK_INTERFACE), gShellNetwork1HiiHandle);
1354 goto ON_EXIT;
1355 }
1356
1357 //
1358 // To split the configuration into multi-section.
1359 //
1360 ArgList = SplitStrToList (ValueStr, L' ');
1361 ASSERT (ArgList != NULL);
1362
1363 Private->OpCode = IfConfigOpSet;
1364 Private->IfName = ArgList->Arg;
1365
1366 Private->VarArg = ArgList->Next;
1367
1368 if (Private->IfName == NULL || Private->VarArg == NULL) {
1369 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND), gShellNetwork1HiiHandle);
1370 goto ON_EXIT;
1371 }
1372 }
1373
1374 //
1375 // Main process of ifconfig.
1376 //
1377 Status = IfConfig (Private);
1378
1379 ON_EXIT:
1380
1381 ShellCommandLineFreeVarList (ParamPackage);
1382
1383 if (Private != NULL) {
1384 IfConfigCleanup (Private);
1385 }
1386
1387 return Status;
1388 }