summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c')
-rw-r--r--MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c369
1 files changed, 147 insertions, 222 deletions
diff --git a/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c b/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
index b948cf8c7b..696f363a62 100644
--- a/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
+++ b/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
@@ -1,8 +1,8 @@
/** @file
Routines used to operate the Ip4 configure variable.
-Copyright (c) 2006 - 2009, Intel Corporation.<BR>
-All rights reserved. This program and the accompanying materials
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at<BR>
http://opensource.org/licenses/bsd-license.php
@@ -12,8 +12,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
+#include "Ip4Config.h"
#include "NicIp4Variable.h"
+BOOLEAN mIp4ConfigVariableReclaimed = FALSE;
/**
Check whether the configure parameter is valid.
@@ -77,302 +79,225 @@ Ip4ConfigIsValid (
/**
Read the ip4 configure variable from the EFI variable.
- None
+ @param Instance The IP4 CONFIG instance.
- @return The IP4 configure read if it is there and is valid, otherwise NULL
+ @return The IP4 configure read if it is there and is valid, otherwise NULL.
**/
-IP4_CONFIG_VARIABLE *
+NIC_IP4_CONFIG_INFO *
Ip4ConfigReadVariable (
- VOID
+ IN IP4_CONFIG_INSTANCE *Instance
)
{
- IP4_CONFIG_VARIABLE *Variable;
- EFI_STATUS Status;
- UINTN Size;
- UINT16 CheckSum;
-
- //
- // Get the size of variable, then allocate a buffer to read the variable.
- //
- Size = 0;
- Variable = NULL;
- Status = gRT->GetVariable (
- EFI_NIC_IP4_CONFIG_VARIABLE,
- &gEfiNicIp4ConfigVariableGuid,
- NULL,
- &Size,
- NULL
- );
-
- if (Status != EFI_BUFFER_TOO_SMALL) {
- return NULL;
- }
-
- if (Size < sizeof (IP4_CONFIG_VARIABLE)) {
- goto REMOVE_VARIABLE;
- }
-
- Variable = AllocatePool (Size);
+ NIC_IP4_CONFIG_INFO *NicConfig;
- if (Variable == NULL) {
- return NULL;
+ NicConfig = GetVariable (Instance->MacString, &gEfiNicIp4ConfigVariableGuid);
+ if (NicConfig != NULL) {
+ Ip4ConfigFixRouteTablePointer (&NicConfig->Ip4Info);
}
- Status = gRT->GetVariable (
- EFI_NIC_IP4_CONFIG_VARIABLE,
- &gEfiNicIp4ConfigVariableGuid,
- NULL,
- &Size,
- Variable
- );
-
- if (EFI_ERROR (Status)) {
- goto ON_ERROR;
- }
-
- //
- // Verify the checksum, variable size and count
- //
- CheckSum = (UINT16) (~NetblockChecksum ((UINT8 *) Variable, (UINT32)Size));
-
- if ((CheckSum != 0) || (Size != Variable->Len)) {
- goto REMOVE_VARIABLE;
- }
-
- if ((Variable->Count < 1) || (Variable->Count > MAX_IP4_CONFIG_IN_VARIABLE)) {
- goto REMOVE_VARIABLE;
- }
-
- return Variable;
-
-REMOVE_VARIABLE:
- Ip4ConfigWriteVariable (NULL);
-
-ON_ERROR:
- if (Variable != NULL) {
- FreePool (Variable);
- }
-
- return NULL;
+ return NicConfig;
}
-
/**
Write the IP4 configure variable to the NVRAM. If Config
is NULL, remove the variable.
- @param Config The IP4 configure data to write
+ @param Instance The IP4 CONFIG instance.
+ @param NicConfig The IP4 configure data to write.
- @retval EFI_SUCCESS The variable is written to the NVRam
+ @retval EFI_SUCCESS The variable is written to the NVRam.
@retval Others Failed to write the variable.
**/
EFI_STATUS
Ip4ConfigWriteVariable (
- IN IP4_CONFIG_VARIABLE *Config OPTIONAL
+ IN IP4_CONFIG_INSTANCE *Instance,
+ IN NIC_IP4_CONFIG_INFO *NicConfig OPTIONAL
)
{
- EFI_STATUS Status;
+ EFI_STATUS Status;
Status = gRT->SetVariable (
- EFI_NIC_IP4_CONFIG_VARIABLE,
+ Instance->MacString,
&gEfiNicIp4ConfigVariableGuid,
IP4_CONFIG_VARIABLE_ATTRIBUTES,
- (Config == NULL) ? 0 : Config->Len,
- Config
+ (NicConfig == NULL) ? 0 : SIZEOF_NIC_IP4_CONFIG_INFO (NicConfig),
+ NicConfig
);
return Status;
}
-
/**
- Locate the IP4 configure parameters from the variable.If a
- configuration is found, copy it to a newly allocated block
- of memory to avoid the alignment problem. Caller should
- release the memory after use.
+ Check whether a NIC exist in the platform given its MAC address.
- @param Variable The IP4 configure variable to search in
- @param NicAddr The interface address to check
+ @param NicAddr The MAC address for the NIC to be checked.
- @return The point to the NIC's IP4 configure info if it is found
- in the IP4 variable, otherwise NULL.
+ @retval TRUE The NIC exist in the platform.
+ @retval FALSE The NIC doesn't exist in the platform.
**/
-NIC_IP4_CONFIG_INFO *
-Ip4ConfigFindNicVariable (
- IN IP4_CONFIG_VARIABLE *Variable,
+BOOLEAN
+Ip4ConfigIsNicExist (
IN NIC_ADDR *NicAddr
)
{
- NIC_IP4_CONFIG_INFO Temp;
- NIC_IP4_CONFIG_INFO *Config;
- UINT32 Index;
- UINT8 *Cur;
- UINT32 Len;
-
- Cur = (UINT8*)&Variable->ConfigInfo;
+ EFI_STATUS Status;
+ EFI_HANDLE *HandleBuffer;
+ UINTN NumberOfHandles;
+ UINTN Index;
+ BOOLEAN Found;
+ UINTN AddrSize;
+ EFI_MAC_ADDRESS MacAddr;
- for (Index = 0; Index < Variable->Count; Index++) {
- //
- // Copy the data to Temp to avoid the alignment problems
- //
- CopyMem (&Temp, Cur, sizeof (NIC_IP4_CONFIG_INFO));
- Len = SIZEOF_NIC_IP4_CONFIG_INFO (&Temp);
+ //
+ // Locate Service Binding handles.
+ //
+ Status = gBS->LocateHandleBuffer (
+ ByProtocol,
+ &gEfiManagedNetworkServiceBindingProtocolGuid,
+ NULL,
+ &NumberOfHandles,
+ &HandleBuffer
+ );
+ if (EFI_ERROR (Status)) {
+ return FALSE;
+ }
+ Found = FALSE;
+ for (Index = 0; Index < NumberOfHandles; Index++) {
//
- // Found the matching configuration parameters, allocate
- // a block of memory then copy it out.
+ // Get MAC address.
//
- if (NIC_ADDR_EQUAL (&Temp.NicAddr, NicAddr)) {
- Config = AllocatePool (Len);
-
- if (Config == NULL) {
- return NULL;
- }
-
- CopyMem (Config, Cur, Len);
- Ip4ConfigFixRouteTablePointer (&Config->Ip4Info);
- return Config;
+ AddrSize = 0;
+ Status = NetLibGetMacAddress (HandleBuffer[Index], &MacAddr, &AddrSize);
+ if (EFI_ERROR (Status)) {
+ Found = FALSE;
+ goto Exit;
}
- Cur += Len;
+ if ((NicAddr->Len == AddrSize) && (CompareMem (NicAddr->MacAddr.Addr, MacAddr.Addr, AddrSize) == 0)) {
+ Found = TRUE;
+ goto Exit;
+ }
}
- return NULL;
+Exit:
+ FreePool (HandleBuffer);
+ return Found;
}
-
/**
- Modify the configuration parameter for the NIC in the variable.
- If Config is NULL, old configuration will be remove from the new
- variable. Otherwise, append it or replace the old one.
-
- @param Variable The IP4 variable to change
- @param NicAddr The interface to search
- @param Config The new configuration parameter (NULL to remove the old)
-
- @return The new IP4_CONFIG_VARIABLE variable if the new variable has at
- least one NIC configure and no EFI_OUT_OF_RESOURCES failure.
- Return NULL either because failed to locate memory for new variable
- or the only NIC configure is removed from the Variable.
+ Reclaim Ip4Config Variables for NIC which has been removed from the platform.
**/
-IP4_CONFIG_VARIABLE *
-Ip4ConfigModifyVariable (
- IN IP4_CONFIG_VARIABLE *Variable OPTIONAL,
- IN NIC_ADDR *NicAddr,
- IN NIC_IP4_CONFIG_INFO *Config OPTIONAL
+VOID
+Ip4ConfigReclaimVariable (
+ VOID
)
{
- NIC_IP4_CONFIG_INFO Temp;
- NIC_IP4_CONFIG_INFO *Old;
- IP4_CONFIG_VARIABLE *NewVar;
- UINT32 Len;
- UINT32 TotalLen;
- UINT32 Count;
- UINT8 *Next;
- UINT8 *Cur;
- UINT32 Index;
-
- ASSERT ((Variable != NULL) || (Config != NULL));
+ EFI_STATUS Status;
+ UINTN VariableNameSize;
+ CHAR16 *VariableName;
+ CHAR16 *CurrentVariableName;
+ EFI_GUID VendorGuid;
+ UINTN VariableNameBufferSize;
+ NIC_IP4_CONFIG_INFO *NicConfig;
//
- // Compute the total length
+ // Check whether we need perform reclaim.
//
- if (Variable != NULL) {
- //
- // Variable != NULL, then Config can be NULL or not. and so is
- // the Old. If old configure exists, it is removed from the
- // Variable. New configure is append to the variable.
- //
- //
- Count = Variable->Count;
- Cur = (UINT8 *)&Variable->ConfigInfo;
- TotalLen = Variable->Len;
+ if (mIp4ConfigVariableReclaimed) {
+ return;
+ }
+ mIp4ConfigVariableReclaimed = TRUE;
- Old = Ip4ConfigFindNicVariable (Variable, NicAddr);
+ //
+ // Get all Ip4Config Variable.
+ //
+ VariableNameSize = sizeof (CHAR16);
+ VariableName = AllocateZeroPool (VariableNameSize);
+ VariableNameBufferSize = VariableNameSize;
+
+ while (TRUE) {
+ Status = gRT->GetNextVariableName (
+ &VariableNameSize,
+ VariableName,
+ &VendorGuid
+ );
- if (Old != NULL) {
- TotalLen -= SIZEOF_NIC_IP4_CONFIG_INFO (Old);
- FreePool (Old);
+Check:
+ if (Status == EFI_BUFFER_TOO_SMALL) {
+ VariableName = ReallocatePool (VariableNameBufferSize, VariableNameSize, VariableName);
+ VariableNameBufferSize = VariableNameSize;
+ //
+ // Try again using the new buffer.
+ //
+ Status = gRT->GetNextVariableName (
+ &VariableNameSize,
+ VariableName,
+ &VendorGuid
+ );
}
- if (Config != NULL) {
- TotalLen += SIZEOF_NIC_IP4_CONFIG_INFO (Config);
+ if (EFI_ERROR (Status)) {
+ //
+ // No more variable available, finish search.
+ //
+ break;
}
//
- // Return NULL if the only NIC_IP4_CONFIG_INFO is being removed.
+ // Check variable GUID.
//
- if (TotalLen < sizeof (IP4_CONFIG_VARIABLE)) {
- return NULL;
+ if (!CompareGuid (&VendorGuid, &gEfiNicIp4ConfigVariableGuid)) {
+ continue;
}
- } else {
- //
- // Variable == NULL and Config != NULL, Create a new variable with
- // this NIC configure.
- //
- Count = 0;
- Cur = NULL;
- TotalLen = sizeof (IP4_CONFIG_VARIABLE) - sizeof (NIC_IP4_CONFIG_INFO)
- + SIZEOF_NIC_IP4_CONFIG_INFO (Config);
- }
-
- ASSERT (TotalLen >= sizeof (IP4_CONFIG_VARIABLE));
-
- NewVar = AllocateZeroPool (TotalLen);
-
- if (NewVar == NULL) {
- return NULL;
- }
-
- NewVar->Len = TotalLen;
-
- //
- // Copy the other configure parameters from the old variable
- //
- Next = (UINT8 *)&NewVar->ConfigInfo;
-
- for (Index = 0; Index < Count; Index++) {
- CopyMem (&Temp, Cur, sizeof (NIC_IP4_CONFIG_INFO));
- Len = SIZEOF_NIC_IP4_CONFIG_INFO (&Temp);
-
- if (!NIC_ADDR_EQUAL (&Temp.NicAddr, NicAddr)) {
- CopyMem (Next, Cur, Len);
- Next += Len;
- NewVar->Count++;
+ NicConfig = GetVariable (VariableName, &gEfiNicIp4ConfigVariableGuid);
+ if (NicConfig == NULL) {
+ break;
}
- Cur += Len;
- }
-
- //
- // Append the new configure if it isn't NULL.
- //
- Len = 0;
-
- if (Config != NULL) {
- Len = SIZEOF_NIC_IP4_CONFIG_INFO (Config);
-
- CopyMem (Next, Config, Len);
- NewVar->Count++;
+ if (!Ip4ConfigIsNicExist (&NicConfig->NicAddr)) {
+ //
+ // No NIC found for this Ip4Config variable, remove it.
+ // Since we are in loop of GetNextVariableName(), we need move on to next
+ // Variable first and then delete current Variable.
+ //
+ CurrentVariableName = AllocateCopyPool (VariableNameSize, VariableName);
+ Status = gRT->GetNextVariableName (
+ &VariableNameSize,
+ VariableName,
+ &VendorGuid
+ );
+
+ gRT->SetVariable (
+ CurrentVariableName,
+ &gEfiNicIp4ConfigVariableGuid,
+ IP4_CONFIG_VARIABLE_ATTRIBUTES,
+ 0,
+ NULL
+ );
+ FreePool (CurrentVariableName);
+
+ //
+ // We already get next variable, go to check it.
+ //
+ goto Check;
+ }
}
- ASSERT (Next + Len == (UINT8 *) NewVar + TotalLen);
-
- NewVar->CheckSum = (UINT16) (~NetblockChecksum ((UINT8 *) NewVar, TotalLen));
- return NewVar;
+ FreePool (VariableName);
}
/**
- Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure.
-
+ Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure.
+
The pointer is set to be immediately follow the ConfigData if there're entries
in the RouteTable. Otherwise it is set to NULL.
-
+
@param ConfigData The IP4 IP configure data.
**/
@@ -382,7 +307,7 @@ Ip4ConfigFixRouteTablePointer (
)
{
//
- // The memory used for route table entries must immediately follow
+ // The memory used for route table entries must immediately follow
// the ConfigData and be not packed.
//
if (ConfigData->RouteTableSize > 0) {