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