]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Config2Nv.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Config2Nv.c
1 /** @file
2 Helper functions for configuring or getting the parameters relating to Ip4.
3
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Ip4Impl.h"
16
17 CHAR16 mIp4Config2StorageName[] = L"IP4_CONFIG2_IFR_NVDATA";
18
19 /**
20 Calculate the prefix length of the IPv4 subnet mask.
21
22 @param[in] SubnetMask The IPv4 subnet mask.
23
24 @return The prefix length of the subnet mask.
25 @retval 0 Other errors as indicated.
26
27 **/
28 UINT8
29 GetSubnetMaskPrefixLength (
30 IN EFI_IPv4_ADDRESS *SubnetMask
31 )
32 {
33 UINT8 Len;
34 UINT32 ReverseMask;
35
36 //
37 // The SubnetMask is in network byte order.
38 //
39 ReverseMask = SwapBytes32 (*(UINT32 *)&SubnetMask[0]);
40
41 //
42 // Reverse it.
43 //
44 ReverseMask = ~ReverseMask;
45
46 if ((ReverseMask & (ReverseMask + 1)) != 0) {
47 return 0;
48 }
49
50 Len = 0;
51
52 while (ReverseMask != 0) {
53 ReverseMask = ReverseMask >> 1;
54 Len++;
55 }
56
57 return (UINT8) (32 - Len);
58 }
59
60 /**
61 Convert the decimal dotted IPv4 address into the binary IPv4 address.
62
63 @param[in] Str The UNICODE string.
64 @param[out] Ip The storage to return the IPv4 address.
65
66 @retval EFI_SUCCESS The binary IP address is returned in Ip.
67 @retval EFI_INVALID_PARAMETER The IP string is malformatted.
68
69 **/
70 EFI_STATUS
71 Ip4Config2StrToIp (
72 IN CHAR16 *Str,
73 OUT EFI_IPv4_ADDRESS *Ip
74 )
75 {
76 UINTN Index;
77 UINTN Number;
78
79 Index = 0;
80
81 while (*Str != L'\0') {
82
83 if (Index > 3) {
84 return EFI_INVALID_PARAMETER;
85 }
86
87 Number = 0;
88 while ((*Str >= L'0') && (*Str <= L'9')) {
89 Number = Number * 10 + (*Str - L'0');
90 Str++;
91 }
92
93 if (Number > 0xFF) {
94 return EFI_INVALID_PARAMETER;
95 }
96
97 Ip->Addr[Index] = (UINT8) Number;
98
99 if ((*Str != L'\0') && (*Str != L'.')) {
100 //
101 // The current character should be either the NULL terminator or
102 // the dot delimiter.
103 //
104 return EFI_INVALID_PARAMETER;
105 }
106
107 if (*Str == L'.') {
108 //
109 // Skip the delimiter.
110 //
111 Str++;
112 }
113
114 Index++;
115 }
116
117 if (Index != 4) {
118 return EFI_INVALID_PARAMETER;
119 }
120
121 return EFI_SUCCESS;
122 }
123
124 /**
125 Convert the decimal dotted IPv4 addresses separated by space into the binary IPv4 address list.
126
127 @param[in] Str The UNICODE string contains IPv4 addresses.
128 @param[out] PtrIpList The storage to return the IPv4 address list.
129 @param[out] IpCount The size of the IPv4 address list.
130
131 @retval EFI_SUCCESS The binary IP address list is returned in PtrIpList.
132 @retval EFI_OUT_OF_RESOURCES Error occurs in allocating memory.
133 @retval EFI_INVALID_PARAMETER The IP string is malformatted.
134
135 **/
136 EFI_STATUS
137 Ip4Config2StrToIpList (
138 IN CHAR16 *Str,
139 OUT EFI_IPv4_ADDRESS **PtrIpList,
140 OUT UINTN *IpCount
141 )
142 {
143 UINTN BeginIndex;
144 UINTN EndIndex;
145 UINTN Index;
146 UINTN IpIndex;
147 CHAR16 *StrTemp;
148 BOOLEAN SpaceTag;
149
150 BeginIndex = 0;
151 EndIndex = BeginIndex;
152 Index = 0;
153 IpIndex = 0;
154 StrTemp = NULL;
155 SpaceTag = TRUE;
156
157 *PtrIpList = NULL;
158 *IpCount = 0;
159
160 if (Str == NULL) {
161 return EFI_SUCCESS;
162 }
163
164 //
165 // Get the number of Ip.
166 //
167 while (*(Str + Index) != L'\0') {
168 if (*(Str + Index) == L' ') {
169 SpaceTag = TRUE;
170 } else {
171 if (SpaceTag) {
172 (*IpCount)++;
173 SpaceTag = FALSE;
174 }
175 }
176
177 Index++;
178 }
179
180 if (*IpCount == 0) {
181 return EFI_SUCCESS;
182 }
183
184 //
185 // Allocate buffer for IpList.
186 //
187 *PtrIpList = AllocateZeroPool(*IpCount * sizeof(EFI_IPv4_ADDRESS));
188 if (*PtrIpList == NULL) {
189 return EFI_OUT_OF_RESOURCES;
190 }
191
192 //
193 // Get IpList from Str.
194 //
195 Index = 0;
196 while (*(Str + Index) != L'\0') {
197 if (*(Str + Index) == L' ') {
198 if(!SpaceTag) {
199 StrTemp = AllocateZeroPool((EndIndex - BeginIndex + 1) * sizeof(CHAR16));
200 if (StrTemp == NULL) {
201 FreePool(*PtrIpList);
202 *PtrIpList = NULL;
203 *IpCount = 0;
204 return EFI_OUT_OF_RESOURCES;
205 }
206
207 CopyMem (StrTemp, Str + BeginIndex, (EndIndex - BeginIndex) * sizeof(CHAR16));
208 *(StrTemp + (EndIndex - BeginIndex)) = L'\0';
209
210 if (Ip4Config2StrToIp (StrTemp, &((*PtrIpList)[IpIndex])) != EFI_SUCCESS) {
211 FreePool(StrTemp);
212 FreePool(*PtrIpList);
213 *PtrIpList = NULL;
214 *IpCount = 0;
215 return EFI_INVALID_PARAMETER;
216 }
217
218 BeginIndex = EndIndex;
219 IpIndex++;
220
221 FreePool(StrTemp);
222 }
223
224 BeginIndex++;
225 EndIndex++;
226 SpaceTag = TRUE;
227 } else {
228 EndIndex++;
229 SpaceTag = FALSE;
230 }
231
232 Index++;
233
234 if (*(Str + Index) == L'\0') {
235 if (!SpaceTag) {
236 StrTemp = AllocateZeroPool((EndIndex - BeginIndex + 1) * sizeof(CHAR16));
237 if (StrTemp == NULL) {
238 FreePool(*PtrIpList);
239 *PtrIpList = NULL;
240 *IpCount = 0;
241 return EFI_OUT_OF_RESOURCES;
242 }
243
244 CopyMem (StrTemp, Str + BeginIndex, (EndIndex - BeginIndex) * sizeof(CHAR16));
245 *(StrTemp + (EndIndex - BeginIndex)) = L'\0';
246
247 if (Ip4Config2StrToIp (StrTemp, &((*PtrIpList)[IpIndex])) != EFI_SUCCESS) {
248 FreePool(StrTemp);
249 FreePool(*PtrIpList);
250 *PtrIpList = NULL;
251 *IpCount = 0;
252 return EFI_INVALID_PARAMETER;
253 }
254
255 FreePool(StrTemp);
256 }
257 }
258 }
259
260 return EFI_SUCCESS;
261 }
262
263 /**
264 Convert the IPv4 address into a dotted string.
265
266 @param[in] Ip The IPv4 address.
267 @param[out] Str The dotted IP string.
268
269 **/
270 VOID
271 Ip4Config2IpToStr (
272 IN EFI_IPv4_ADDRESS *Ip,
273 OUT CHAR16 *Str
274 )
275 {
276 UnicodeSPrint (
277 Str,
278 2 * IP4_STR_MAX_SIZE,
279 L"%d.%d.%d.%d",
280 Ip->Addr[0],
281 Ip->Addr[1],
282 Ip->Addr[2],
283 Ip->Addr[3]
284 );
285 }
286
287
288 /**
289 Convert the IPv4 address list into string consists of several decimal
290 dotted IPv4 addresses separated by space.
291
292 @param[in] Ip The IPv4 address list.
293 @param[in] IpCount The size of IPv4 address list.
294 @param[out] Str The string contains several decimal dotted
295 IPv4 addresses separated by space.
296
297 @retval EFI_SUCCESS Operation is success.
298 @retval EFI_OUT_OF_RESOURCES Error occurs in allocating memory.
299
300 **/
301 EFI_STATUS
302 Ip4Config2IpListToStr (
303 IN EFI_IPv4_ADDRESS *Ip,
304 IN UINTN IpCount,
305 OUT CHAR16 *Str
306 )
307 {
308 UINTN Index;
309 UINTN TemIndex;
310 UINTN StrIndex;
311 CHAR16 *TempStr;
312 EFI_IPv4_ADDRESS *TempIp;
313
314 Index = 0;
315 TemIndex = 0;
316 StrIndex = 0;
317 TempStr = NULL;
318 TempIp = NULL;
319
320 for (Index = 0; Index < IpCount; Index ++) {
321 TempIp = Ip + Index;
322 if (TempStr == NULL) {
323 TempStr = AllocateZeroPool(2 * IP4_STR_MAX_SIZE);
324 if (TempStr == NULL) {
325 return EFI_OUT_OF_RESOURCES;
326 }
327 }
328
329 UnicodeSPrint (
330 TempStr,
331 2 * IP4_STR_MAX_SIZE,
332 L"%d.%d.%d.%d",
333 TempIp->Addr[0],
334 TempIp->Addr[1],
335 TempIp->Addr[2],
336 TempIp->Addr[3]
337 );
338
339 for (TemIndex = 0; TemIndex < IP4_STR_MAX_SIZE; TemIndex ++) {
340 if (*(TempStr + TemIndex) == L'\0') {
341 if (Index == IpCount - 1) {
342 Str[StrIndex++] = L'\0';
343 } else {
344 Str[StrIndex++] = L' ';
345 }
346 break;
347 } else {
348 Str[StrIndex++] = *(TempStr + TemIndex);
349 }
350 }
351 }
352
353 if (TempStr != NULL) {
354 FreePool(TempStr);
355 }
356
357 return EFI_SUCCESS;
358 }
359
360 /**
361 The notify function of create event when performing a manual configuration.
362
363 @param[in] Event The pointer of Event.
364 @param[in] Context The pointer of Context.
365
366 **/
367 VOID
368 EFIAPI
369 Ip4Config2ManualAddressNotify (
370 IN EFI_EVENT Event,
371 IN VOID *Context
372 )
373 {
374 *((BOOLEAN *) Context) = TRUE;
375 }
376
377 /**
378 Convert the network configuration data into the IFR data.
379
380 @param[in] Instance The IP4 config2 instance.
381 @param[in, out] IfrNvData The IFR nv data.
382
383 @retval EFI_SUCCESS The configure parameter to IFR data was
384 set successfully.
385 @retval EFI_INVALID_PARAMETER Source instance or target IFR data is not available.
386 @retval Others Other errors as indicated.
387
388 **/
389 EFI_STATUS
390 Ip4Config2ConvertConfigNvDataToIfrNvData (
391 IN IP4_CONFIG2_INSTANCE *Instance,
392 IN OUT IP4_CONFIG2_IFR_NVDATA *IfrNvData
393 )
394 {
395 IP4_SERVICE *IpSb;
396 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
397 EFI_IP4_CONFIG2_INTERFACE_INFO *Ip4Info;
398 EFI_IP4_CONFIG2_POLICY Policy;
399 UINTN DataSize;
400 UINTN GatewaySize;
401 EFI_IPv4_ADDRESS GatewayAddress;
402 EFI_STATUS Status;
403 UINTN DnsSize;
404 UINTN DnsCount;
405 EFI_IPv4_ADDRESS *DnsAddress;
406
407 Status = EFI_SUCCESS;
408 Ip4Config2 = &Instance->Ip4Config2;
409 Ip4Info = NULL;
410 DnsAddress = NULL;
411 GatewaySize = sizeof (EFI_IPv4_ADDRESS);
412
413 if ((IfrNvData == NULL) || (Instance == NULL)) {
414 return EFI_INVALID_PARAMETER;
415 }
416
417 NET_CHECK_SIGNATURE (Instance, IP4_CONFIG2_INSTANCE_SIGNATURE);
418
419 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
420
421 if (IpSb->DefaultInterface->Configured) {
422 IfrNvData->Configure = 1;
423 } else {
424 IfrNvData->Configure = 0;
425 goto Exit;
426 }
427
428 //
429 // Get the Policy info.
430 //
431 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
432 Status = Ip4Config2->GetData (
433 Ip4Config2,
434 Ip4Config2DataTypePolicy,
435 &DataSize,
436 &Policy
437 );
438 if (EFI_ERROR (Status)) {
439 goto Exit;
440 }
441
442 if (Policy == Ip4Config2PolicyStatic) {
443 IfrNvData->DhcpEnable = FALSE;
444 } else if (Policy == Ip4Config2PolicyDhcp) {
445 IfrNvData->DhcpEnable = TRUE;
446 goto Exit;
447 }
448
449 //
450 // Get the interface info.
451 //
452 DataSize = 0;
453 Status = Ip4Config2->GetData (
454 Ip4Config2,
455 Ip4Config2DataTypeInterfaceInfo,
456 &DataSize,
457 NULL
458 );
459 if (Status != EFI_BUFFER_TOO_SMALL) {
460 return Status;
461 }
462
463 Ip4Info = AllocateZeroPool (DataSize);
464 if (Ip4Info == NULL) {
465 Status = EFI_OUT_OF_RESOURCES;
466 return Status;
467 }
468
469 Status = Ip4Config2->GetData (
470 Ip4Config2,
471 Ip4Config2DataTypeInterfaceInfo,
472 &DataSize,
473 Ip4Info
474 );
475 if (EFI_ERROR (Status)) {
476 goto Exit;
477 }
478
479 //
480 // Get the Gateway info.
481 //
482 Status = Ip4Config2->GetData (
483 Ip4Config2,
484 Ip4Config2DataTypeGateway,
485 &GatewaySize,
486 &GatewayAddress
487 );
488 if (EFI_ERROR (Status)) {
489 goto Exit;
490 }
491
492 //
493 // Get the Dns info.
494 //
495 DnsSize = 0;
496 Status = Ip4Config2->GetData (
497 Ip4Config2,
498 Ip4Config2DataTypeDnsServer,
499 &DnsSize,
500 NULL
501 );
502 if ((Status != EFI_BUFFER_TOO_SMALL) && (Status != EFI_NOT_FOUND)) {
503 goto Exit;
504 }
505
506 DnsCount = (UINT32) (DnsSize / sizeof (EFI_IPv4_ADDRESS));
507
508 if (DnsSize > 0) {
509 DnsAddress = AllocateZeroPool(DnsSize);
510 if (DnsAddress == NULL) {
511 Status = EFI_OUT_OF_RESOURCES;
512 goto Exit;
513 }
514
515 Status = Ip4Config2->GetData (
516 Ip4Config2,
517 Ip4Config2DataTypeDnsServer,
518 &DnsSize,
519 DnsAddress
520 );
521 if (EFI_ERROR (Status)) {
522 goto Exit;
523 }
524 }
525
526 Ip4Config2IpToStr (&Ip4Info->StationAddress, IfrNvData->StationAddress);
527 Ip4Config2IpToStr (&Ip4Info->SubnetMask, IfrNvData->SubnetMask);
528 Ip4Config2IpToStr (&GatewayAddress, IfrNvData->GatewayAddress);
529 Status = Ip4Config2IpListToStr (DnsAddress, DnsCount, IfrNvData->DnsAddress);
530
531 Exit:
532
533 if (DnsAddress != NULL) {
534 FreePool(DnsAddress);
535 }
536
537 if (Ip4Info != NULL) {
538 FreePool(Ip4Info);
539 }
540
541 return Status;
542 }
543
544 /**
545 Convert the IFR data into the network configuration data and set the IP
546 configure parameters for the NIC.
547
548 @param[in] IfrFormNvData The IFR NV data.
549 @param[in, out] Instance The IP4 config2 instance.
550
551 @retval EFI_SUCCESS The configure parameter for this NIC was
552 set successfully.
553 @retval EFI_INVALID_PARAMETER The address information for setting is invalid.
554 @retval Others Other errors as indicated.
555
556 **/
557 EFI_STATUS
558 Ip4Config2ConvertIfrNvDataToConfigNvData (
559 IN IP4_CONFIG2_IFR_NVDATA *IfrFormNvData,
560 IN OUT IP4_CONFIG2_INSTANCE *Instance
561 )
562 {
563 EFI_STATUS Status;
564 EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2;
565 IP4_CONFIG2_NVDATA *Ip4NvData;
566
567 EFI_IP_ADDRESS StationAddress;
568 EFI_IP_ADDRESS SubnetMask;
569 EFI_IP_ADDRESS Gateway;
570 IP4_ADDR Ip;
571 EFI_IPv4_ADDRESS *DnsAddress;
572 UINTN DnsCount;
573 UINTN Index;
574
575 EFI_EVENT TimeoutEvent;
576 EFI_EVENT SetAddressEvent;
577 BOOLEAN IsAddressOk;
578 UINTN DataSize;
579 EFI_INPUT_KEY Key;
580
581 Status = EFI_SUCCESS;
582 Ip4Cfg2 = &Instance->Ip4Config2;
583 Ip4NvData = &Instance->Ip4NvData;
584
585 DnsCount = 0;
586 DnsAddress = NULL;
587
588 TimeoutEvent = NULL;
589 SetAddressEvent = NULL;
590
591
592
593 if (Instance == NULL || IfrFormNvData == NULL) {
594 return EFI_INVALID_PARAMETER;
595 }
596
597 if (IfrFormNvData->Configure != TRUE) {
598 return EFI_SUCCESS;
599 }
600
601 if (IfrFormNvData->DhcpEnable == TRUE) {
602 Ip4NvData->Policy = Ip4Config2PolicyDhcp;
603
604 Status = Ip4Cfg2->SetData (
605 Ip4Cfg2,
606 Ip4Config2DataTypePolicy,
607 sizeof (EFI_IP4_CONFIG2_POLICY),
608 &Ip4NvData->Policy
609 );
610 if (EFI_ERROR(Status)) {
611 return Status;
612 }
613 } else {
614 //
615 // Get Ip4NvData from IfrFormNvData if it is valid.
616 //
617 Ip4NvData->Policy = Ip4Config2PolicyStatic;
618
619 Status = Ip4Config2StrToIp (IfrFormNvData->SubnetMask, &SubnetMask.v4);
620 if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (GetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) {
621 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Subnet Mask!", NULL);
622 return EFI_INVALID_PARAMETER;
623 }
624
625 Status = Ip4Config2StrToIp (IfrFormNvData->StationAddress, &StationAddress.v4);
626 if (EFI_ERROR (Status) ||
627 (SubnetMask.Addr[0] != 0 && !NetIp4IsUnicast (NTOHL (StationAddress.Addr[0]), NTOHL (SubnetMask.Addr[0]))) ||
628 !Ip4StationAddressValid (NTOHL (StationAddress.Addr[0]), NTOHL (SubnetMask.Addr[0]))) {
629 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid IP address!", NULL);
630 return EFI_INVALID_PARAMETER;
631 }
632
633 Status = Ip4Config2StrToIp (IfrFormNvData->GatewayAddress, &Gateway.v4);
634 if (EFI_ERROR (Status) ||
635 (Gateway.Addr[0] != 0 && SubnetMask.Addr[0] != 0 && !NetIp4IsUnicast (NTOHL (Gateway.Addr[0]), NTOHL (SubnetMask.Addr[0])))) {
636 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Gateway!", NULL);
637 return EFI_INVALID_PARAMETER;
638 }
639
640 Status = Ip4Config2StrToIpList (IfrFormNvData->DnsAddress, &DnsAddress, &DnsCount);
641 if (!EFI_ERROR (Status) && DnsCount > 0) {
642 for (Index = 0; Index < DnsCount; Index ++) {
643 CopyMem (&Ip, &DnsAddress[Index], sizeof (IP4_ADDR));
644 if (IP4_IS_UNSPECIFIED (NTOHL (Ip)) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) {
645 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL);
646 FreePool(DnsAddress);
647 return EFI_INVALID_PARAMETER;
648 }
649 }
650 } else {
651 if (EFI_ERROR (Status)) {
652 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL);
653 }
654 }
655
656 if (Ip4NvData->ManualAddress != NULL) {
657 FreePool(Ip4NvData->ManualAddress);
658 }
659 Ip4NvData->ManualAddressCount = 1;
660 Ip4NvData->ManualAddress = AllocateZeroPool(sizeof(EFI_IP4_CONFIG2_MANUAL_ADDRESS));
661 if (Ip4NvData->ManualAddress == NULL) {
662 if (DnsAddress != NULL) {
663 FreePool(DnsAddress);
664 }
665
666 return EFI_OUT_OF_RESOURCES;
667 }
668 CopyMem(&Ip4NvData->ManualAddress->Address, &StationAddress.v4, sizeof(EFI_IPv4_ADDRESS));
669 CopyMem(&Ip4NvData->ManualAddress->SubnetMask, &SubnetMask.v4, sizeof(EFI_IPv4_ADDRESS));
670
671 if (Ip4NvData->GatewayAddress != NULL) {
672 FreePool(Ip4NvData->GatewayAddress);
673 }
674 Ip4NvData->GatewayAddressCount = 1;
675 Ip4NvData->GatewayAddress = AllocateZeroPool(sizeof(EFI_IPv4_ADDRESS));
676 if (Ip4NvData->GatewayAddress == NULL) {
677 if (DnsAddress != NULL) {
678 FreePool(DnsAddress);
679 }
680 return EFI_OUT_OF_RESOURCES;
681 }
682 CopyMem(Ip4NvData->GatewayAddress, &Gateway.v4, sizeof(EFI_IPv4_ADDRESS));
683
684 if (Ip4NvData->DnsAddress != NULL) {
685 FreePool(Ip4NvData->DnsAddress);
686 }
687 Ip4NvData->DnsAddressCount = (UINT32) DnsCount;
688 Ip4NvData->DnsAddress = DnsAddress;
689
690 //
691 // Setting Ip4NvData.
692 //
693 Status = Ip4Cfg2->SetData (
694 Ip4Cfg2,
695 Ip4Config2DataTypePolicy,
696 sizeof (EFI_IP4_CONFIG2_POLICY),
697 &Ip4NvData->Policy
698 );
699 if (EFI_ERROR(Status)) {
700 return Status;
701 }
702
703 //
704 // Create events & timers for asynchronous settings.
705 //
706 Status = gBS->CreateEvent (
707 EVT_TIMER,
708 TPL_CALLBACK,
709 NULL,
710 NULL,
711 &TimeoutEvent
712 );
713 if (EFI_ERROR (Status)) {
714 return EFI_OUT_OF_RESOURCES;
715 }
716
717 Status = gBS->CreateEvent (
718 EVT_NOTIFY_SIGNAL,
719 TPL_NOTIFY,
720 Ip4Config2ManualAddressNotify,
721 &IsAddressOk,
722 &SetAddressEvent
723 );
724 if (EFI_ERROR (Status)) {
725 goto Exit;
726 }
727
728 IsAddressOk = FALSE;
729
730 Status = Ip4Cfg2->RegisterDataNotify (
731 Ip4Cfg2,
732 Ip4Config2DataTypeManualAddress,
733 SetAddressEvent
734 );
735 if (EFI_ERROR (Status)) {
736 goto Exit;
737 }
738
739 //
740 // Set ManualAddress.
741 //
742 DataSize = Ip4NvData->ManualAddressCount * sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS);
743 Status = Ip4Cfg2->SetData (
744 Ip4Cfg2,
745 Ip4Config2DataTypeManualAddress,
746 DataSize,
747 (VOID *) Ip4NvData->ManualAddress
748 );
749
750 if (Status == EFI_NOT_READY) {
751 gBS->SetTimer (TimeoutEvent, TimerRelative, 50000000);
752 while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {
753 if (IsAddressOk) {
754 Status = EFI_SUCCESS;
755 break;
756 }
757 }
758 }
759
760 Ip4Cfg2->UnregisterDataNotify (
761 Ip4Cfg2,
762 Ip4Config2DataTypeManualAddress,
763 SetAddressEvent
764 );
765 if (EFI_ERROR (Status)) {
766 goto Exit;
767 }
768
769 //
770 // Set gateway.
771 //
772 DataSize = Ip4NvData->GatewayAddressCount * sizeof (EFI_IPv4_ADDRESS);
773 Status = Ip4Cfg2->SetData (
774 Ip4Cfg2,
775 Ip4Config2DataTypeGateway,
776 DataSize,
777 Ip4NvData->GatewayAddress
778 );
779 if (EFI_ERROR (Status)) {
780 goto Exit;
781 }
782
783 //
784 // Set DNS addresses.
785 //
786 if (Ip4NvData->DnsAddressCount > 0 && Ip4NvData->DnsAddress != NULL) {
787 DataSize = Ip4NvData->DnsAddressCount * sizeof (EFI_IPv4_ADDRESS);
788 Status = Ip4Cfg2->SetData (
789 Ip4Cfg2,
790 Ip4Config2DataTypeDnsServer,
791 DataSize,
792 Ip4NvData->DnsAddress
793 );
794
795 if (EFI_ERROR (Status)) {
796 goto Exit;
797 }
798 }
799 }
800
801 Exit:
802 if (SetAddressEvent != NULL) {
803 gBS->CloseEvent (SetAddressEvent);
804 }
805
806 if (TimeoutEvent != NULL) {
807 gBS->CloseEvent (TimeoutEvent);
808 }
809
810 return Status;
811 }
812
813 /**
814 This function allows the caller to request the current
815 configuration for one or more named elements. The resulting
816 string is in <ConfigAltResp> format. Any and all alternative
817 configuration strings shall also be appended to the end of the
818 current configuration string. If they are, they must appear
819 after the current configuration. They must contain the same
820 routing (GUID, NAME, PATH) as the current configuration string.
821 They must have an additional description indicating the type of
822 alternative configuration the string represents,
823 "ALTCFG=<StringToken>". That <StringToken> (when
824 converted from Hex UNICODE to binary) is a reference to a
825 string in the associated string pack.
826
827 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
828 @param[in] Request A null-terminated Unicode string in
829 <ConfigRequest> format. Note that this
830 includes the routing information as well as
831 the configurable name / value pairs. It is
832 invalid for this string to be in
833 <MultiConfigRequest> format.
834 @param[out] Progress On return, points to a character in the
835 Request string. Points to the string's null
836 terminator if request was successful. Points
837 to the most recent "&" before the first
838 failing name / value pair (or the beginning
839 of the string if the failure is in the first
840 name / value pair) if the request was not
841 successful.
842 @param[out] Results A null-terminated Unicode string in
843 <ConfigAltResp> format which has all values
844 filled in for the names in the Request string.
845 String to be allocated by the called function.
846
847 @retval EFI_SUCCESS The Results string is filled with the
848 values corresponding to all requested
849 names.
850 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
851 parts of the results that must be
852 stored awaiting possible future
853 protocols.
854 @retval EFI_NOT_FOUND Routing data doesn't match any
855 known driver. Progress set to the
856 first character in the routing header.
857 Note: There is no requirement that the
858 driver validate the routing data. It
859 must skip the <ConfigHdr> in order to
860 process the names.
861 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
862 to most recent & before the
863 error or the beginning of the
864 string.
865 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
866 to the & before the name in
867 question.Currently not implemented.
868 **/
869 EFI_STATUS
870 EFIAPI
871 Ip4FormExtractConfig (
872 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
873 IN CONST EFI_STRING Request,
874 OUT EFI_STRING *Progress,
875 OUT EFI_STRING *Results
876 )
877 {
878 EFI_STATUS Status;
879 IP4_CONFIG2_INSTANCE *Ip4Config2Instance;
880 IP4_FORM_CALLBACK_INFO *Private;
881 IP4_CONFIG2_IFR_NVDATA *IfrFormNvData;
882 EFI_STRING ConfigRequestHdr;
883 EFI_STRING ConfigRequest;
884 BOOLEAN AllocatedRequest;
885 EFI_STRING FormResult;
886 UINTN Size;
887 UINTN BufferSize;
888
889 if (Progress == NULL || Results == NULL) {
890 return EFI_INVALID_PARAMETER;
891 }
892
893 Status = EFI_SUCCESS;
894 IfrFormNvData = NULL;
895 ConfigRequest = NULL;
896 FormResult = NULL;
897 Size = 0;
898 AllocatedRequest = FALSE;
899 ConfigRequest = Request;
900 Private = IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(This);
901 Ip4Config2Instance = IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Private);
902 BufferSize = sizeof (IP4_CONFIG2_IFR_NVDATA);
903 *Progress = Request;
904
905 //
906 // Check Request data in <ConfigHdr>.
907 //
908 if ((Request == NULL) || HiiIsConfigHdrMatch (Request, &gIp4Config2NvDataGuid, mIp4Config2StorageName)) {
909 IfrFormNvData = AllocateZeroPool (sizeof (IP4_CONFIG2_IFR_NVDATA));
910 if (IfrFormNvData == NULL) {
911 return EFI_OUT_OF_RESOURCES;
912 }
913
914 Ip4Config2ConvertConfigNvDataToIfrNvData (Ip4Config2Instance, IfrFormNvData);
915
916 if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {
917 //
918 // Request has no request element, construct full request string.
919 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
920 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
921 //
922 ConfigRequestHdr = HiiConstructConfigHdr (&gIp4Config2NvDataGuid, mIp4Config2StorageName, Private->ChildHandle);
923 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
924 ConfigRequest = AllocateZeroPool (Size);
925 if (ConfigRequest == NULL) {
926 Status = EFI_OUT_OF_RESOURCES;
927 goto Failure;
928 }
929 AllocatedRequest = TRUE;
930
931 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);
932 FreePool (ConfigRequestHdr);
933 }
934
935 //
936 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
937 //
938 Status = gHiiConfigRouting->BlockToConfig (
939 gHiiConfigRouting,
940 ConfigRequest,
941 (UINT8 *) IfrFormNvData,
942 BufferSize,
943 &FormResult,
944 Progress
945 );
946
947 FreePool (IfrFormNvData);
948
949 //
950 // Free the allocated config request string.
951 //
952 if (AllocatedRequest) {
953 FreePool (ConfigRequest);
954 ConfigRequest = NULL;
955 }
956
957 if (EFI_ERROR (Status)) {
958 goto Failure;
959 }
960 }
961
962 if (Request == NULL || HiiIsConfigHdrMatch (Request, &gIp4Config2NvDataGuid, mIp4Config2StorageName)) {
963 *Results = FormResult;
964 } else {
965 return EFI_NOT_FOUND;
966 }
967
968 Failure:
969 //
970 // Set Progress string to the original request string.
971 //
972 if (Request == NULL) {
973 *Progress = NULL;
974 } else if (StrStr (Request, L"OFFSET") == NULL) {
975 *Progress = Request + StrLen (Request);
976 }
977
978 return Status;
979 }
980
981 /**
982 This function applies changes in a driver's configuration.
983 Input is a Configuration, which has the routing data for this
984 driver followed by name / value configuration pairs. The driver
985 must apply those pairs to its configurable storage. If the
986 driver's configuration is stored in a linear block of data
987 and the driver's name / value pairs are in <BlockConfig>
988 format, it may use the ConfigToBlock helper function (above) to
989 simplify the job. Currently not implemented.
990
991 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
992 @param[in] Configuration A null-terminated Unicode string in
993 <ConfigString> format.
994 @param[out] Progress A pointer to a string filled in with the
995 offset of the most recent '&' before the
996 first failing name / value pair (or the
997 beginn ing of the string if the failure
998 is in the first name / value pair) or
999 the terminating NULL if all was
1000 successful.
1001
1002 @retval EFI_SUCCESS The results have been distributed or are
1003 awaiting distribution.
1004 @retval EFI_OUT_OF_MEMORY Not enough memory to store the
1005 parts of the results that must be
1006 stored awaiting possible future
1007 protocols.
1008 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
1009 Results parameter would result
1010 in this type of error.
1011 @retval EFI_NOT_FOUND Target for the specified routing data
1012 was not found.
1013 **/
1014 EFI_STATUS
1015 EFIAPI
1016 Ip4FormRouteConfig (
1017 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1018 IN CONST EFI_STRING Configuration,
1019 OUT EFI_STRING *Progress
1020 )
1021 {
1022 EFI_STATUS Status;
1023 UINTN BufferSize;
1024 IP4_CONFIG2_IFR_NVDATA *IfrFormNvData;
1025 IP4_CONFIG2_INSTANCE *Ip4Config2Instance;
1026 IP4_FORM_CALLBACK_INFO *Private;
1027
1028 Status = EFI_SUCCESS;
1029 IfrFormNvData = NULL;
1030
1031 if (Configuration == NULL || Progress == NULL) {
1032 return EFI_INVALID_PARAMETER;
1033 }
1034
1035 *Progress = Configuration;
1036
1037 Private = IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(This);
1038 Ip4Config2Instance = IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Private);
1039
1040 //
1041 // Check Routing data in <ConfigHdr>.
1042 //
1043 if (HiiIsConfigHdrMatch (Configuration, &gIp4Config2NvDataGuid, mIp4Config2StorageName)) {
1044 //
1045 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
1046 //
1047 IfrFormNvData = AllocateZeroPool (sizeof (IP4_CONFIG2_IFR_NVDATA));
1048 if (IfrFormNvData == NULL) {
1049 return EFI_OUT_OF_RESOURCES;
1050 }
1051
1052 BufferSize = 0;
1053
1054 Status = gHiiConfigRouting->ConfigToBlock (
1055 gHiiConfigRouting,
1056 Configuration,
1057 (UINT8 *) IfrFormNvData,
1058 &BufferSize,
1059 Progress
1060 );
1061 if (Status != EFI_BUFFER_TOO_SMALL) {
1062 return Status;
1063 }
1064
1065 Status = gHiiConfigRouting->ConfigToBlock (
1066 gHiiConfigRouting,
1067 Configuration,
1068 (UINT8 *) IfrFormNvData,
1069 &BufferSize,
1070 Progress
1071 );
1072 if (!EFI_ERROR (Status)) {
1073 Status = Ip4Config2ConvertIfrNvDataToConfigNvData (IfrFormNvData, Ip4Config2Instance);
1074 }
1075
1076 FreePool (IfrFormNvData);
1077 } else {
1078 return EFI_NOT_FOUND;
1079 }
1080
1081 return Status;
1082
1083 }
1084
1085 /**
1086 This function is called to provide results data to the driver.
1087 This data consists of a unique key that is used to identify
1088 which data is either being passed back or being asked for.
1089
1090 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1091 @param[in] Action Specifies the type of action taken by the browser.
1092 @param[in] QuestionId A unique value which is sent to the original
1093 exporting driver so that it can identify the type
1094 of data to expect. The format of the data tends to
1095 vary based on the opcode that enerated the callback.
1096 @param[in] Type The type of value for the question.
1097 @param[in] Value A pointer to the data being sent to the original
1098 exporting driver.
1099 @param[out] ActionRequest On return, points to the action requested by the
1100 callback function.
1101
1102 @retval EFI_SUCCESS The callback successfully handled the action.
1103 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
1104 variable and its data.
1105 @retval EFI_DEVICE_ERROR The variable could not be saved.
1106 @retval EFI_UNSUPPORTED The specified Action is not supported by the
1107 callback.Currently not implemented.
1108 @retval EFI_INVALID_PARAMETERS Passing in wrong parameter.
1109 @retval Others Other errors as indicated.
1110
1111 **/
1112 EFI_STATUS
1113 EFIAPI
1114 Ip4FormCallback (
1115 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1116 IN EFI_BROWSER_ACTION Action,
1117 IN EFI_QUESTION_ID QuestionId,
1118 IN UINT8 Type,
1119 IN EFI_IFR_TYPE_VALUE *Value,
1120 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
1121 )
1122 {
1123 EFI_STATUS Status;
1124 IP4_CONFIG2_INSTANCE *Instance;
1125 IP4_CONFIG2_IFR_NVDATA *IfrFormNvData;
1126 IP4_FORM_CALLBACK_INFO *Private;
1127
1128 EFI_IP_ADDRESS StationAddress;
1129 EFI_IP_ADDRESS SubnetMask;
1130 EFI_IP_ADDRESS Gateway;
1131 IP4_ADDR Ip;
1132 EFI_IPv4_ADDRESS *DnsAddress;
1133 UINTN DnsCount;
1134 UINTN Index;
1135 EFI_INPUT_KEY Key;
1136
1137 IfrFormNvData = NULL;
1138 DnsCount = 0;
1139 DnsAddress = NULL;
1140
1141 if (Action == EFI_BROWSER_ACTION_CHANGED) {
1142 Private = IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(This);
1143 Instance = IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Private);
1144
1145 IfrFormNvData = AllocateZeroPool (sizeof (IP4_CONFIG2_IFR_NVDATA));
1146 if (IfrFormNvData == NULL) {
1147 return EFI_OUT_OF_RESOURCES;
1148 }
1149
1150 //
1151 // Retrieve uncommitted data from Browser
1152 //
1153 if (!HiiGetBrowserData (&gIp4Config2NvDataGuid, mIp4Config2StorageName, sizeof (IP4_CONFIG2_IFR_NVDATA), (UINT8 *) IfrFormNvData)) {
1154 FreePool (IfrFormNvData);
1155 return EFI_NOT_FOUND;
1156 }
1157
1158 Status = EFI_SUCCESS;
1159
1160 switch (QuestionId) {
1161 case KEY_LOCAL_IP:
1162 Status = Ip4Config2StrToIp (IfrFormNvData->StationAddress, &StationAddress.v4);
1163 if (EFI_ERROR (Status) || IP4_IS_UNSPECIFIED (NTOHL (StationAddress.Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (StationAddress.Addr[0]))) {
1164 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid IP address!", NULL);
1165 Status = EFI_INVALID_PARAMETER;
1166 }
1167 break;
1168
1169 case KEY_SUBNET_MASK:
1170 Status = Ip4Config2StrToIp (IfrFormNvData->SubnetMask, &SubnetMask.v4);
1171 if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (GetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) {
1172 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Subnet Mask!", NULL);
1173 Status = EFI_INVALID_PARAMETER;
1174 }
1175 break;
1176
1177 case KEY_GATE_WAY:
1178 Status = Ip4Config2StrToIp (IfrFormNvData->GatewayAddress, &Gateway.v4);
1179 if (EFI_ERROR (Status) || IP4_IS_LOCAL_BROADCAST(NTOHL(Gateway.Addr[0]))) {
1180 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Gateway!", NULL);
1181 Status = EFI_INVALID_PARAMETER;
1182 }
1183 break;
1184
1185 case KEY_DNS:
1186 Status = Ip4Config2StrToIpList (IfrFormNvData->DnsAddress, &DnsAddress, &DnsCount);
1187 if (!EFI_ERROR (Status) && DnsCount > 0) {
1188 for (Index = 0; Index < DnsCount; Index ++) {
1189 CopyMem (&Ip, &DnsAddress[Index], sizeof (IP4_ADDR));
1190 if (IP4_IS_UNSPECIFIED (NTOHL (Ip)) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) {
1191 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL);
1192 Status = EFI_INVALID_PARAMETER;
1193 break;
1194 }
1195 }
1196 } else {
1197 if (EFI_ERROR (Status)) {
1198 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL);
1199 }
1200 }
1201
1202 if(DnsAddress != NULL) {
1203 FreePool(DnsAddress);
1204 }
1205 break;
1206
1207 case KEY_SAVE_CHANGES:
1208 Status = Ip4Config2ConvertIfrNvDataToConfigNvData (IfrFormNvData, Instance);
1209 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1210 break;
1211
1212 default:
1213 break;
1214 }
1215
1216 FreePool (IfrFormNvData);
1217
1218 return Status;
1219 }
1220
1221 //
1222 // All other action return unsupported.
1223 //
1224 return EFI_UNSUPPORTED;
1225 }
1226
1227 /**
1228 Install HII Config Access protocol for network device and allocate resource.
1229
1230 @param[in, out] Instance The IP4 config2 Instance.
1231
1232 @retval EFI_SUCCESS The HII Config Access protocol is installed.
1233 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1234 @retval Others Other errors as indicated.
1235
1236 **/
1237 EFI_STATUS
1238 Ip4Config2FormInit (
1239 IN OUT IP4_CONFIG2_INSTANCE *Instance
1240 )
1241 {
1242 EFI_STATUS Status;
1243 IP4_SERVICE *IpSb;
1244 IP4_FORM_CALLBACK_INFO *CallbackInfo;
1245 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
1246 VENDOR_DEVICE_PATH VendorDeviceNode;
1247 EFI_SERVICE_BINDING_PROTOCOL *MnpSb;
1248 CHAR16 *MacString;
1249 CHAR16 MenuString[128];
1250 CHAR16 PortString[128];
1251 CHAR16 *OldMenuString;
1252 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
1253
1254 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
1255 ASSERT (IpSb != NULL);
1256
1257 CallbackInfo = &Instance->CallbackInfo;
1258
1259 CallbackInfo->Signature = IP4_FORM_CALLBACK_INFO_SIGNATURE;
1260
1261 Status = gBS->HandleProtocol (
1262 IpSb->Controller,
1263 &gEfiDevicePathProtocolGuid,
1264 (VOID **) &ParentDevicePath
1265 );
1266 if (EFI_ERROR (Status)) {
1267 return Status;
1268 }
1269
1270 //
1271 // Construct device path node for EFI HII Config Access protocol,
1272 // which consists of controller physical device path and one hardware
1273 // vendor guid node.
1274 //
1275 ZeroMem (&VendorDeviceNode, sizeof (VENDOR_DEVICE_PATH));
1276 VendorDeviceNode.Header.Type = HARDWARE_DEVICE_PATH;
1277 VendorDeviceNode.Header.SubType = HW_VENDOR_DP;
1278
1279 CopyGuid (&VendorDeviceNode.Guid, &gEfiCallerIdGuid);
1280
1281 SetDevicePathNodeLength (&VendorDeviceNode.Header, sizeof (VENDOR_DEVICE_PATH));
1282 CallbackInfo->HiiVendorDevicePath = AppendDevicePathNode (
1283 ParentDevicePath,
1284 (EFI_DEVICE_PATH_PROTOCOL *) &VendorDeviceNode
1285 );
1286 if (CallbackInfo->HiiVendorDevicePath == NULL) {
1287 Status = EFI_OUT_OF_RESOURCES;
1288 goto Error;
1289 }
1290
1291 ConfigAccess = &CallbackInfo->HiiConfigAccessProtocol;
1292 ConfigAccess->ExtractConfig = Ip4FormExtractConfig;
1293 ConfigAccess->RouteConfig = Ip4FormRouteConfig;
1294 ConfigAccess->Callback = Ip4FormCallback;
1295
1296 //
1297 // Install Device Path Protocol and Config Access protocol on new handle
1298 //
1299 Status = gBS->InstallMultipleProtocolInterfaces (
1300 &CallbackInfo->ChildHandle,
1301 &gEfiDevicePathProtocolGuid,
1302 CallbackInfo->HiiVendorDevicePath,
1303 &gEfiHiiConfigAccessProtocolGuid,
1304 ConfigAccess,
1305 NULL
1306 );
1307
1308 if (!EFI_ERROR (Status)) {
1309 //
1310 // Open the Parent Handle for the child
1311 //
1312 Status = gBS->OpenProtocol (
1313 IpSb->Controller,
1314 &gEfiManagedNetworkServiceBindingProtocolGuid,
1315 (VOID **) &MnpSb,
1316 IpSb->Image,
1317 CallbackInfo->ChildHandle,
1318 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1319 );
1320 }
1321
1322 if (EFI_ERROR (Status)) {
1323 goto Error;
1324 }
1325
1326 //
1327 // Publish our HII data
1328 //
1329 CallbackInfo->RegisteredHandle = HiiAddPackages (
1330 &gIp4Config2NvDataGuid,
1331 CallbackInfo->ChildHandle,
1332 Ip4DxeStrings,
1333 Ip4Config2Bin,
1334 NULL
1335 );
1336 if (CallbackInfo->RegisteredHandle == NULL) {
1337 Status = EFI_OUT_OF_RESOURCES;
1338 goto Error;
1339 }
1340
1341 //
1342 // Append MAC string in the menu help string and tile help string
1343 //
1344 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &MacString);
1345 if (!EFI_ERROR (Status)) {
1346 OldMenuString = HiiGetString (
1347 CallbackInfo->RegisteredHandle,
1348 STRING_TOKEN (STR_IP4_CONFIG2_FORM_HELP),
1349 NULL
1350 );
1351 UnicodeSPrint (MenuString, 128, L"%s (MAC:%s)", OldMenuString, MacString);
1352 HiiSetString (
1353 CallbackInfo->RegisteredHandle,
1354 STRING_TOKEN (STR_IP4_CONFIG2_FORM_HELP),
1355 MenuString,
1356 NULL
1357 );
1358
1359 UnicodeSPrint (PortString, 128, L"MAC:%s", MacString);
1360 HiiSetString (
1361 CallbackInfo->RegisteredHandle,
1362 STRING_TOKEN (STR_IP4_DEVICE_FORM_HELP),
1363 PortString,
1364 NULL
1365 );
1366
1367 FreePool (MacString);
1368 FreePool (OldMenuString);
1369
1370 return EFI_SUCCESS;
1371 }
1372
1373 Error:
1374 Ip4Config2FormUnload (Instance);
1375 return Status;
1376 }
1377
1378 /**
1379 Uninstall the HII Config Access protocol for network devices and free up the resources.
1380
1381 @param[in, out] Instance The IP4 config2 instance to unload a form.
1382
1383 **/
1384 VOID
1385 Ip4Config2FormUnload (
1386 IN OUT IP4_CONFIG2_INSTANCE *Instance
1387 )
1388 {
1389 IP4_SERVICE *IpSb;
1390 IP4_FORM_CALLBACK_INFO *CallbackInfo;
1391 IP4_CONFIG2_NVDATA *Ip4NvData;
1392
1393 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
1394 ASSERT (IpSb != NULL);
1395
1396 CallbackInfo = &Instance->CallbackInfo;
1397
1398 if (CallbackInfo->ChildHandle != NULL) {
1399 //
1400 // Close the child handle
1401 //
1402 gBS->CloseProtocol (
1403 IpSb->Controller,
1404 &gEfiManagedNetworkServiceBindingProtocolGuid,
1405 IpSb->Image,
1406 CallbackInfo->ChildHandle
1407 );
1408
1409 //
1410 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
1411 //
1412 gBS->UninstallMultipleProtocolInterfaces (
1413 CallbackInfo->ChildHandle,
1414 &gEfiDevicePathProtocolGuid,
1415 CallbackInfo->HiiVendorDevicePath,
1416 &gEfiHiiConfigAccessProtocolGuid,
1417 &CallbackInfo->HiiConfigAccessProtocol,
1418 NULL
1419 );
1420 }
1421
1422 if (CallbackInfo->HiiVendorDevicePath != NULL) {
1423 FreePool (CallbackInfo->HiiVendorDevicePath);
1424 }
1425
1426 if (CallbackInfo->RegisteredHandle != NULL) {
1427 //
1428 // Remove HII package list
1429 //
1430 HiiRemovePackages (CallbackInfo->RegisteredHandle);
1431 }
1432
1433 Ip4NvData = &Instance->Ip4NvData;
1434
1435 if(Ip4NvData->ManualAddress != NULL) {
1436 FreePool(Ip4NvData->ManualAddress);
1437 }
1438
1439 if(Ip4NvData->GatewayAddress != NULL) {
1440 FreePool(Ip4NvData->GatewayAddress);
1441 }
1442
1443 if(Ip4NvData->DnsAddress != NULL) {
1444 FreePool(Ip4NvData->DnsAddress);
1445 }
1446
1447 Ip4NvData->ManualAddressCount = 0;
1448 Ip4NvData->GatewayAddressCount = 0;
1449 Ip4NvData->DnsAddressCount = 0;
1450 }