2 Routines used to operate the Ip4 configure variable.
4 Copyright (c) 2006 - 2010, 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<BR>
8 http://opensource.org/licenses/bsd-license.php
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.
15 #include "Ip4Config.h"
16 #include "NicIp4Variable.h"
18 BOOLEAN mIp4ConfigVariableReclaimed
= FALSE
;
21 Check whether the configure parameter is valid.
23 @param NicConfig The configure parameter to check
25 @return TRUE if the parameter is valid for the interface, otherwise FALSE.
30 IN NIC_IP4_CONFIG_INFO
*NicConfig
33 EFI_IP4_IPCONFIG_DATA
*IpConfig
;
39 IpConfig
= &NicConfig
->Ip4Info
;
41 if (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
) {
43 // Validate that the addresses are unicast and mask
44 // is properly formated
46 Station
= EFI_NTOHL (IpConfig
->StationAddress
);
47 Netmask
= EFI_NTOHL (IpConfig
->SubnetMask
);
49 if ((Netmask
== 0) || !IP4_IS_VALID_NETMASK (Netmask
) ||
50 (Station
== 0) || !NetIp4IsUnicast (Station
, Netmask
)) {
55 // Validate that the next hops are on the connected network
56 // or that is a direct route (Gateway == 0).
58 for (Index
= 0; Index
< IpConfig
->RouteTableSize
; Index
++) {
59 Gateway
= EFI_NTOHL (IpConfig
->RouteTable
[Index
].GatewayAddress
);
61 if ((Gateway
!= 0) && (!IP4_NET_EQUAL (Station
, Gateway
, Netmask
) ||
62 !NetIp4IsUnicast (Gateway
, Netmask
))) {
71 // return false if it is an unkown configure source. Valid
72 // sources are static and dhcp.
74 return (BOOLEAN
) (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
);
80 Read the ip4 configure variable from the EFI variable.
82 @param Instance The IP4 CONFIG instance.
84 @return The IP4 configure read if it is there and is valid, otherwise NULL.
88 Ip4ConfigReadVariable (
89 IN IP4_CONFIG_INSTANCE
*Instance
92 NIC_IP4_CONFIG_INFO
*NicConfig
;
94 NicConfig
= GetVariable (Instance
->MacString
, &gEfiNicIp4ConfigVariableGuid
);
95 if (NicConfig
!= NULL
) {
96 Ip4ConfigFixRouteTablePointer (&NicConfig
->Ip4Info
);
103 Write the IP4 configure variable to the NVRAM. If Config
104 is NULL, remove the variable.
106 @param Instance The IP4 CONFIG instance.
107 @param NicConfig The IP4 configure data to write.
109 @retval EFI_SUCCESS The variable is written to the NVRam.
110 @retval Others Failed to write the variable.
114 Ip4ConfigWriteVariable (
115 IN IP4_CONFIG_INSTANCE
*Instance
,
116 IN NIC_IP4_CONFIG_INFO
*NicConfig OPTIONAL
121 Status
= gRT
->SetVariable (
123 &gEfiNicIp4ConfigVariableGuid
,
124 IP4_CONFIG_VARIABLE_ATTRIBUTES
,
125 (NicConfig
== NULL
) ? 0 : SIZEOF_NIC_IP4_CONFIG_INFO (NicConfig
),
133 Check whether a NIC exist in the platform given its MAC address.
135 @param NicAddr The MAC address for the NIC to be checked.
137 @retval TRUE The NIC exist in the platform.
138 @retval FALSE The NIC doesn't exist in the platform.
142 Ip4ConfigIsNicExist (
147 EFI_HANDLE
*HandleBuffer
;
148 UINTN NumberOfHandles
;
152 EFI_MAC_ADDRESS MacAddr
;
155 // Locate Service Binding handles.
157 Status
= gBS
->LocateHandleBuffer (
159 &gEfiManagedNetworkServiceBindingProtocolGuid
,
164 if (EFI_ERROR (Status
)) {
169 for (Index
= 0; Index
< NumberOfHandles
; Index
++) {
174 Status
= NetLibGetMacAddress (HandleBuffer
[Index
], &MacAddr
, &AddrSize
);
175 if (EFI_ERROR (Status
)) {
180 if ((NicAddr
->Len
== AddrSize
) && (CompareMem (NicAddr
->MacAddr
.Addr
, MacAddr
.Addr
, AddrSize
) == 0)) {
187 FreePool (HandleBuffer
);
192 Reclaim Ip4Config Variables for NIC which has been removed from the platform.
196 Ip4ConfigReclaimVariable (
201 UINTN VariableNameSize
;
202 CHAR16
*VariableName
;
203 CHAR16
*CurrentVariableName
;
205 UINTN VariableNameBufferSize
;
206 NIC_IP4_CONFIG_INFO
*NicConfig
;
209 // Check whether we need perform reclaim.
211 if (mIp4ConfigVariableReclaimed
) {
214 mIp4ConfigVariableReclaimed
= TRUE
;
217 // Get all Ip4Config Variable.
219 VariableNameSize
= sizeof (CHAR16
);
220 VariableName
= AllocateZeroPool (VariableNameSize
);
221 VariableNameBufferSize
= VariableNameSize
;
224 Status
= gRT
->GetNextVariableName (
231 if (Status
== EFI_BUFFER_TOO_SMALL
) {
232 VariableName
= ReallocatePool (VariableNameBufferSize
, VariableNameSize
, VariableName
);
233 VariableNameBufferSize
= VariableNameSize
;
235 // Try again using the new buffer.
237 Status
= gRT
->GetNextVariableName (
244 if (EFI_ERROR (Status
)) {
246 // No more variable available, finish search.
252 // Check variable GUID.
254 if (!CompareGuid (&VendorGuid
, &gEfiNicIp4ConfigVariableGuid
)) {
258 NicConfig
= GetVariable (VariableName
, &gEfiNicIp4ConfigVariableGuid
);
259 if (NicConfig
== NULL
) {
263 if (!Ip4ConfigIsNicExist (&NicConfig
->NicAddr
)) {
265 // No NIC found for this Ip4Config variable, remove it.
266 // Since we are in loop of GetNextVariableName(), we need move on to next
267 // Variable first and then delete current Variable.
269 CurrentVariableName
= AllocateCopyPool (VariableNameSize
, VariableName
);
270 Status
= gRT
->GetNextVariableName (
278 &gEfiNicIp4ConfigVariableGuid
,
279 IP4_CONFIG_VARIABLE_ATTRIBUTES
,
283 FreePool (CurrentVariableName
);
286 // We already get next variable, go to check it.
292 FreePool (VariableName
);
296 Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure.
298 The pointer is set to be immediately follow the ConfigData if there're entries
299 in the RouteTable. Otherwise it is set to NULL.
301 @param ConfigData The IP4 IP configure data.
305 Ip4ConfigFixRouteTablePointer (
306 IN OUT EFI_IP4_IPCONFIG_DATA
*ConfigData
310 // The memory used for route table entries must immediately follow
311 // the ConfigData and be not packed.
313 if (ConfigData
->RouteTableSize
> 0) {
314 ConfigData
->RouteTable
= (EFI_IP4_ROUTE_TABLE
*) (ConfigData
+ 1);
316 ConfigData
->RouteTable
= NULL
;