3 Copyright (c) 2006 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Routines used to operate the Ip4 configure variable
24 #include <Library/NetLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
31 #include "NicIp4Variable.h"
35 Check whether the configure parameter is valid.
37 @param NicConfig The configure parameter to check
39 @return TRUE if the parameter is valid for the interface, otherwise FALSE.
44 IN NIC_IP4_CONFIG_INFO
*NicConfig
47 EFI_IP4_IPCONFIG_DATA
*IpConfig
;
53 IpConfig
= &NicConfig
->Ip4Info
;
55 if (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
) {
57 // Validate that the addresses are unicast and mask
58 // is properly formated
60 Station
= EFI_NTOHL (IpConfig
->StationAddress
);
61 Netmask
= EFI_NTOHL (IpConfig
->SubnetMask
);
63 if ((Netmask
== 0) || !IP4_IS_VALID_NETMASK (Netmask
) ||
64 (Station
== 0) || !Ip4IsUnicast (Station
, Netmask
)) {
69 // Validate that the next hops are on the connected network
70 // or that is a direct route (Gateway == 0).
72 for (Index
= 0; Index
< IpConfig
->RouteTableSize
; Index
++) {
73 Gateway
= EFI_NTOHL (IpConfig
->RouteTable
[Index
].GatewayAddress
);
75 if ((Gateway
!= 0) && (!IP4_NET_EQUAL (Station
, Gateway
, Netmask
) ||
76 !Ip4IsUnicast (Gateway
, Netmask
))) {
85 // return false if it is an unkown configure source. Valid
86 // sources are static and dhcp.
88 return (BOOLEAN
) (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
);
94 Read the ip4 configure variable from the EFI variable.
98 @return The IP4 configure read if it is there and is valid, otherwise NULL
101 IP4_CONFIG_VARIABLE
*
102 Ip4ConfigReadVariable (
106 IP4_CONFIG_VARIABLE
*Variable
;
112 // Get the size of variable, then allocate a buffer to read the variable.
116 Status
= gRT
->GetVariable (
117 EFI_NIC_IP4_CONFIG_VARIABLE
,
118 &gEfiNicIp4ConfigVariableGuid
,
124 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
128 if (Size
< sizeof (IP4_CONFIG_VARIABLE
)) {
129 goto REMOVE_VARIABLE
;
132 Variable
= AllocatePool (Size
);
134 if (Variable
== NULL
) {
138 Status
= gRT
->GetVariable (
139 EFI_NIC_IP4_CONFIG_VARIABLE
,
140 &gEfiNicIp4ConfigVariableGuid
,
146 if (EFI_ERROR (Status
)) {
151 // Verify the checksum, variable size and count
153 CheckSum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Variable
, (UINT32
)Size
));
155 if ((CheckSum
!= 0) || (Size
!= Variable
->Len
)) {
156 goto REMOVE_VARIABLE
;
159 if ((Variable
->Count
< 1) || (Variable
->Count
> MAX_IP4_CONFIG_IN_VARIABLE
)) {
160 goto REMOVE_VARIABLE
;
166 Ip4ConfigWriteVariable (NULL
);
169 if (Variable
!= NULL
) {
170 gBS
->FreePool (Variable
);
178 Write the IP4 configure variable to the NVRAM. If Config
179 is NULL, remove the variable.
181 @param Config The IP4 configure data to write
183 @retval EFI_SUCCESS The variable is written to the NVRam
184 @retval Others Failed to write the variable.
188 Ip4ConfigWriteVariable (
189 IN IP4_CONFIG_VARIABLE
*Config OPTIONAL
194 Status
= gRT
->SetVariable (
195 EFI_NIC_IP4_CONFIG_VARIABLE
,
196 &gEfiNicIp4ConfigVariableGuid
,
197 IP4_CONFIG_VARIABLE_ATTRIBUTES
,
198 (Config
== NULL
) ? 0 : Config
->Len
,
207 Locate the IP4 configure parameters from the variable.If a
208 configuration is found, copy it to a newly allocated block
209 of memory to avoid the alignment problem. Caller should
210 release the memory after use.
212 @param Variable The IP4 configure variable to search in
213 @param NicAddr The interface address to check
215 @return The point to the NIC's IP4 configure info if it is found
216 @return in the IP4 variable, otherwise NULL.
219 NIC_IP4_CONFIG_INFO
*
220 Ip4ConfigFindNicVariable (
221 IN IP4_CONFIG_VARIABLE
*Variable
,
225 NIC_IP4_CONFIG_INFO Temp
;
226 NIC_IP4_CONFIG_INFO
*Config
;
231 Cur
= (UINT8
*)&Variable
->ConfigInfo
;
233 for (Index
= 0; Index
< Variable
->Count
; Index
++) {
235 // Copy the data to Temp to avoid the alignment problems
237 CopyMem (&Temp
, Cur
, sizeof (NIC_IP4_CONFIG_INFO
));
238 Len
= SIZEOF_NIC_IP4_CONFIG_INFO (&Temp
);
241 // Found the matching configuration parameters, allocate
242 // a block of memory then copy it out.
244 if (NIC_ADDR_EQUAL (&Temp
.NicAddr
, NicAddr
)) {
245 Config
= AllocatePool (Len
);
247 if (Config
== NULL
) {
251 CopyMem (Config
, Cur
, Len
);
252 Ip4ConfigFixRouteTablePointer (&Config
->Ip4Info
);
264 Modify the configuration parameter for the NIC in the variable.
265 If Config is NULL, old configuration will be remove from the new
266 variable. Otherwise, append it or replace the old one.
268 @param Variable The IP4 variable to change
269 @param NicAddr The interface to search
270 @param Config The new configuration parameter (NULL to remove the old)
272 @return The new IP4_CONFIG_VARIABLE variable if the new variable has at
273 @return least one NIC configure and no EFI_OUT_OF_RESOURCES failure.
274 @return Return NULL either because failed to locate memory for new variable
275 @return or the only NIC configure is removed from the Variable.
278 IP4_CONFIG_VARIABLE
*
279 Ip4ConfigModifyVariable (
280 IN IP4_CONFIG_VARIABLE
*Variable
, OPTIONAL
281 IN NIC_ADDR
*NicAddr
,
282 IN NIC_IP4_CONFIG_INFO
*Config OPTIONAL
285 NIC_IP4_CONFIG_INFO Temp
;
286 NIC_IP4_CONFIG_INFO
*Old
;
287 IP4_CONFIG_VARIABLE
*NewVar
;
295 ASSERT ((Variable
!= NULL
) || (Config
!= NULL
));
298 // Compute the total length
300 if (Variable
!= NULL
) {
302 // Variable != NULL, then Config can be NULL or not. and so is
303 // the Old. If old configure exists, it is removed from the
304 // Variable. New configure is append to the variable.
307 Count
= Variable
->Count
;
308 Cur
= (UINT8
*)&Variable
->ConfigInfo
;
309 TotalLen
= Variable
->Len
;
311 Old
= Ip4ConfigFindNicVariable (Variable
, NicAddr
);
314 TotalLen
-= SIZEOF_NIC_IP4_CONFIG_INFO (Old
);
318 if (Config
!= NULL
) {
319 TotalLen
+= SIZEOF_NIC_IP4_CONFIG_INFO (Config
);
323 // Return NULL if the only NIC_IP4_CONFIG_INFO is being removed.
325 if (TotalLen
< sizeof (IP4_CONFIG_VARIABLE
)) {
331 // Variable == NULL and Config != NULL, Create a new variable with
332 // this NIC configure.
336 TotalLen
= sizeof (IP4_CONFIG_VARIABLE
) - sizeof (NIC_IP4_CONFIG_INFO
)
337 + SIZEOF_NIC_IP4_CONFIG_INFO (Config
);
340 ASSERT (TotalLen
>= sizeof (IP4_CONFIG_VARIABLE
));
342 NewVar
= AllocateZeroPool (TotalLen
);
344 if (NewVar
== NULL
) {
348 NewVar
->Len
= TotalLen
;
351 // Copy the other configure parameters from the old variable
353 Next
= (UINT8
*)&NewVar
->ConfigInfo
;
355 for (Index
= 0; Index
< Count
; Index
++) {
356 CopyMem (&Temp
, Cur
, sizeof (NIC_IP4_CONFIG_INFO
));
357 Len
= SIZEOF_NIC_IP4_CONFIG_INFO (&Temp
);
359 if (!NIC_ADDR_EQUAL (&Temp
.NicAddr
, NicAddr
)) {
360 CopyMem (Next
, Cur
, Len
);
369 // Append the new configure if it isn't NULL.
373 if (Config
!= NULL
) {
374 Len
= SIZEOF_NIC_IP4_CONFIG_INFO (Config
);
376 CopyMem (Next
, Config
, Len
);
380 ASSERT (Next
+ Len
== (UINT8
*) NewVar
+ TotalLen
);
382 NewVar
->CheckSum
= (UINT16
) (~NetblockChecksum ((UINT8
*) NewVar
, TotalLen
));
387 Ip4ConfigFixRouteTablePointer (
388 IN EFI_IP4_IPCONFIG_DATA
*ConfigData
392 // The memory used for route table entries must immediately follow
393 // the ConfigData and be not packed.
395 if (ConfigData
->RouteTableSize
> 0) {
396 ConfigData
->RouteTable
= (EFI_IP4_ROUTE_TABLE
*) (ConfigData
+ 1);
398 ConfigData
->RouteTable
= NULL
;