/** @file
Network library.
Copyright (c) 2005 - 2011, 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
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include <IndustryStandard/SmBios.h>
#include <Protocol/DriverBinding.h>
#include <Protocol/ServiceBinding.h>
#include <Protocol/SimpleNetwork.h>
#include <Protocol/ManagedNetwork.h>
#include <Protocol/HiiConfigRouting.h>
#include <Protocol/ComponentName.h>
#include <Protocol/ComponentName2.h>
#include <Protocol/HiiConfigAccess.h>
#include <Guid/NicIp4ConfigNvData.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DevicePathLib.h>
#include <Library/PrintLib.h>
#define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
//
// All the supported IP4 maskes in host byte order.
//
0x00000000,
0x80000000,
0xC0000000,
0xE0000000,
0xF0000000,
0xF8000000,
0xFC000000,
0xFE000000,
0xFF000000,
0xFF800000,
0xFFC00000,
0xFFE00000,
0xFFF00000,
0xFFF80000,
0xFFFC0000,
0xFFFE0000,
0xFFFF0000,
0xFFFF8000,
0xFFFFC000,
0xFFFFE000,
0xFFFFF000,
0xFFFFF800,
0xFFFFFC00,
0xFFFFFE00,
0xFFFFFF00,
0xFFFFFF80,
0xFFFFFFC0,
0xFFFFFFE0,
0xFFFFFFF0,
0xFFFFFFF8,
0xFFFFFFFC,
0xFFFFFFFE,
0xFFFFFFFF,
};
//
// Any error level digitally larger than mNetDebugLevelMax
// will be silently discarded.
//
//
// You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
// here to direct the syslog packets to the syslog deamon. The
// default is broadcast to both the ethernet and IP.
//
GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
//
// VLAN device path node template
//
{
{
(UINT8) (sizeof (VLAN_DEVICE_PATH)),
}
},
0
};
/**
Locate the handles that support SNP, then open one of them
to send the syslog packets. The caller isn't required to close
the SNP after use because the SNP is opened by HandleProtocol.
@return The point to SNP if one is properly openned. Otherwise NULL
**/
)
{
//
// Locate the handles which has SNP installed.
//
NULL,
);
return NULL;
}
//
// Try to open one of the ethernet SNP protocol to send packet
//
);
break;
}
}
return Snp;
}
/**
Transmit a syslog packet synchronously through SNP. The Packet
already has the ethernet header prepended. This function should
fill in the source MAC because it will try to locate a SNP each
time it is called to avoid the problem if SNP is unloaded.
This code snip is copied from MNP.
@param[in] Packet The Syslog packet
@param[in] Length The length of the packet
@retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
@retval EFI_TIMEOUT Timeout happened to send the packet.
@retval EFI_SUCCESS Packet is sent.
**/
)
{
Snp = SyslogLocateSnp ();
return EFI_DEVICE_ERROR;
}
//
// Start the timeout event.
//
NULL,
NULL,
);
return Status;
}
goto ON_EXIT;
}
for (;;) {
//
// Transmit the packet through SNP.
//
break;
}
//
// If Status is EFI_SUCCESS, the packet is put in the transmit queue.
// if Status is EFI_NOT_READY, the transmit engine of the network
// interface is busy. Both need to sync SNP.
//
do {
//
// Get the recycled transmit buffer status.
//
break;
}
break;
}
//
// Status is EFI_NOT_READY. Restart the timer event and
// call Snp->Transmit again.
//
}
return Status;
}
/**
and user's message.
@param[in] Level Syslog servity level
@param[in] Module The module that generates the log
@param[in] File The file that contains the current log
@param[in] Line The line of code in the File that contains the current log
@param[in] Message The log message
@param[in] BufLen The lenght of the Buf
@param[out] Buf The buffer to put the packet data
@return The length of the syslog packet built.
**/
)
{
//
// Fill in the Ethernet header. Leave alone the source MAC.
// SyslogSendPacket will fill in the address for us.
//
Buf += sizeof (ETHER_HEAD);
BufLen -= sizeof (ETHER_HEAD);
//
// Fill in the IP header
//
//
// Fill in the UDP header, Udp checksum is optional. Leave it zero.
//
Buf += sizeof (EFI_UDP_HEADER);
BufLen -= sizeof (EFI_UDP_HEADER);
//
// Build the syslog message body with <PRI> Timestamp machine module Message
//
//
// Use %a to format the ASCII strings, %s to format UNICODE strings
//
Len = 0;
Buf,
"<%d> %a %d %d:%d:%d ",
Pri,
);
Len--;
"Tiano %a: %a (Line: %d File: %a)",
Line,
);
Len--;
//
//
Len += sizeof (EFI_UDP_HEADER);
return Len + sizeof (ETHER_HEAD);
}
/**
Allocate a buffer, then format the message to it. This is a
help function for the NET_DEBUG_XXX macros. The PrintArg of
these macros treats the variable length print parameters as a
single parameter, and pass it to the NetDebugASPrint. For
example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
if extracted to:
NetDebugOutput (
NETDEBUG_LEVEL_TRACE,
"Tcp",
__FILE__,
__LINE__,
NetDebugASPrint ("State transit to %a\n", Name)
)
@param Format The ASCII format string.
@param ... The variable length parameter whose format is determined
by the Format string.
@return The buffer containing the formatted message,
or NULL if failed to allocate memory.
**/
CHAR8 *
...
)
{
return NULL;
}
return Buf;
}
/**
Builds an UDP4 syslog packet and send it using SNP.
This function will locate a instance of SNP then send the message through it.
Because it isn't open the SNP BY_DRIVER, apply caution when using it.
@param Level The servity level of the message.
@param Module The Moudle that generates the log.
@param File The file that contains the log.
@param Line The exact line that contains the log.
@param Message The user message to log.
@retval EFI_INVALID_PARAMETER Any input parameter is invalid.
@retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
@retval EFI_SUCCESS The log is discard because that it is more verbose
than the mNetDebugLevelMax. Or, it has been sent out.
**/
)
{
//
// Check whether the message should be sent out
//
return EFI_INVALID_PARAMETER;
}
if (Level > mNetDebugLevelMax) {
goto ON_EXIT;
}
//
// Allocate a maxium of 1024 bytes, the caller should ensure
// than this
//
goto ON_EXIT;
}
//
// Build the message: Ethernet header + IP header + Udp Header + user data
//
Len = SyslogBuildPacket (
File,
Line,
);
return Status;
}
/**
Return the length of the mask.
Return the length of the mask, the correct value is from 0 to 32.
If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
NetMask is in the host byte order.
@param[in] NetMask The netmask to get the length from.
@return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
**/
)
{
break;
}
}
return Index;
}
/**
Return the class of the IP address, such as class A, B, C.
Addr is in host byte order.
The address of class A starts with 0.
If the address belong to class A, return IP4_ADDR_CLASSA.
The address of class B starts with 10.
If the address belong to class B, return IP4_ADDR_CLASSB.
The address of class C starts with 110.
If the address belong to class C, return IP4_ADDR_CLASSC.
The address of class D starts with 1110.
If the address belong to class D, return IP4_ADDR_CLASSD.
The address of class E starts with 1111.
If the address belong to class E, return IP4_ADDR_CLASSE.
@param[in] Addr The address to get the class from.
@return IP address class, such as IP4_ADDR_CLASSA.
**/
)
{
if ((ByteOne & 0x80) == 0) {
return IP4_ADDR_CLASSA;
return IP4_ADDR_CLASSB;
return IP4_ADDR_CLASSC;
return IP4_ADDR_CLASSD;
} else {
return IP4_ADDR_CLASSE;
}
}
/**
Check whether the IP is a valid unicast address according to
the netmask. If NetMask is zero, use the IP address's class to get the default mask.
If Ip is 0, IP is not a valid unicast address.
Class D address is used for multicasting and class E address is reserved for future. If Ip
belongs to class D or class E, IP is not a valid unicast address.
If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
@param[in] Ip The IP to check against.
@param[in] NetMask The mask of the IP.
@return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
**/
)
{
return FALSE;
}
if (NetMask == 0) {
}
return FALSE;
}
return TRUE;
}
/**
Check whether the incoming IPv6 address is a valid unicast address.
If the address is a multicast address has binary 0xFF at the start, it is not
a valid unicast address. If the address is unspecified ::, it is not a valid
unicast address to be assigned to any node. If the address is loopback address
::1, it is also not a valid unicast address to be assigned to any physical
interface.
@param[in] Ip6 The IPv6 address to check against.
@return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
**/
)
{
return FALSE;
}
return TRUE;
}
}
return FALSE;
}
return TRUE;
}
/**
Check whether the incoming Ipv6 address is the unspecified address or not.
@param[in] Ip6 - Ip6 address, in network order.
@retval TRUE - Yes, unspecified
@retval FALSE - No
**/
)
{
return FALSE;
}
}
return TRUE;
}
/**
Check whether the incoming Ipv6 address is a link-local address.
@param[in] Ip6 - Ip6 address, in network order.
@retval TRUE - Yes, link-local address
@retval FALSE - No
**/
)
{
return FALSE;
}
return FALSE;
}
return FALSE;
}
}
return TRUE;
}
/**
Check whether the Ipv6 address1 and address2 are on the connected network.
@param[in] Ip1 - Ip6 address1, in network order.
@param[in] Ip2 - Ip6 address2, in network order.
@param[in] PrefixLength - The prefix length of the checking net.
@retval TRUE - Yes, connected.
@retval FALSE - No.
**/
)
{
if (PrefixLength == 0) {
return TRUE;
}
return FALSE;
}
if (Bit > 0) {
return FALSE;
}
}
return TRUE;
}
/**
Switches the endianess of an IPv6 address
This function swaps the bytes in a 128-bit IPv6 address to switch the value
from little endian to big endian or vice versa. The byte swapped value is
returned.
@param Ip6 Points to an IPv6 address
@return The byte swapped IPv6 address.
**/
)
{
return Ip6;
}
/**
Initialize a random seed using current time.
Get current time first. Then initialize a random seed based on some basic
mathematics operation on the hour, day, minute, second, nanosecond and year
of the current time.
@return The random seed initialized with current time.
**/
)
{
return Seed;
}
/**
Extract a UINT32 from a byte stream.
Copy a UINT32 from a byte stream, then converts it from Network
byte order to host byte order. Use this function to avoid alignment error.
@param[in] Buf The buffer to extract the UINT32.
@return The UINT32 extracted.
**/
)
{
}
/**
Put a UINT32 to the byte stream in network byte order.
Converts a UINT32 from host byte order to network byte order. Then copy it to the
byte stream.
@param[in, out] Buf The buffer to put the UINT32.
@param[in] Data The data to be converted and put into the byte stream.
**/
)
{
}
/**
Remove the first node entry on the list, and return the removed node entry.
Removes the first node Entry from a doubly linked list. It is up to the caller of
this function to release the memory used by the first node if that is required. On
exit, the removed node is returned.
If Head is NULL, then ASSERT().
If Head was not initialized, then ASSERT().
If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
then ASSERT().
@param[in, out] Head The list header.
@return The first node entry that is removed from the list, NULL if the list is empty.
**/
)
{
if (IsListEmpty (Head)) {
return NULL;
}
);
return First;
}
/**
Remove the last node entry on the list and and return the removed node entry.
Removes the last node entry from a doubly linked list. It is up to the caller of
this function to release the memory used by the first node if that is required. On
exit, the removed node is returned.
If Head is NULL, then ASSERT().
If Head was not initialized, then ASSERT().
If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
then ASSERT().
@param[in, out] Head The list head.
@return The last node entry that is removed from the list, NULL if the list is empty.
**/
)
{
if (IsListEmpty (Head)) {
return NULL;
}
);
return Last;
}
/**
Insert a new node entry after a designated node entry of a doubly linked list.
Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
of the doubly linked list.
@param[in, out] PrevEntry The previous entry to insert after.
@param[in, out] NewEntry The new entry to insert.
**/
)
{
}
/**
Insert a new node entry before a designated node entry of a doubly linked list.
Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
of the doubly linked list.
@param[in, out] PostEntry The entry to insert before.
@param[in, out] NewEntry The new entry to insert.
**/
)
{
}
/**
Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
Initialize the forward and backward links of two head nodes donated by Map->Used
and Map->Recycled of two doubly linked lists.
Initializes the count of the <Key, Value> pairs in the netmap to zero.
If Map is NULL, then ASSERT().
If the address of Map->Used is NULL, then ASSERT().
If the address of Map->Recycled is NULl, then ASSERT().
@param[in, out] Map The netmap to initialize.
**/
)
{
}
/**
To clean up the netmap, that is, release allocated memories.
Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
The number of the <Key, Value> pairs in the netmap is set to be zero.
If Map is NULL, then ASSERT().
@param[in, out] Map The netmap to clean up.
**/
)
{
}
}
}
/**
Test whether the netmap is empty and return true if it is.
If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
If Map is NULL, then ASSERT().
@param[in] Map The net map to test.
@return TRUE if the netmap is empty, otherwise FALSE.
**/
)
{
}
/**
Return the number of the <Key, Value> pairs in the netmap.
@param[in] Map The netmap to get the entry number.
@return The entry number in the netmap.
**/
)
{
}
/**
Return one allocated item.
If the Recycled doubly linked list of the netmap is empty, it will try to allocate
a batch of items if there are enough resources and add corresponding nodes to the begining
of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
the fist node entry of the Recycled doubly linked list and return the corresponding item.
If Map is NULL, then ASSERT().
@param[in, out] Map The netmap to allocate item for.
@return The allocated item. If NULL, the
allocation failed due to resource limit.
**/
)
{
if (IsListEmpty (Head)) {
if (Index == 0) {
return NULL;
}
break;
}
}
}
return Item;
}
/**
Allocate an item to save the <Key, Value> pair to the head of the netmap.
Allocate an item to save the <Key, Value> pair and add corresponding node entry
to the beginning of the Used doubly linked list. The number of the <Key, Value>
pairs in the netmap increase by 1.
If Map is NULL, then ASSERT().
@param[in, out] Map The netmap to insert into.
@param[in] Key The user's key.
@param[in] Value The user's value for the key.
@retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
@retval EFI_SUCCESS The item is inserted to the head.
**/
)
{
return EFI_OUT_OF_RESOURCES;
}
return EFI_SUCCESS;
}
/**
Allocate an item to save the <Key, Value> pair to the tail of the netmap.
Allocate an item to save the <Key, Value> pair and add corresponding node entry
to the tail of the Used doubly linked list. The number of the <Key, Value>
pairs in the netmap increase by 1.
If Map is NULL, then ASSERT().
@param[in, out] Map The netmap to insert into.
@param[in] Key The user's key.
@param[in] Value The user's value for the key.
@retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
@retval EFI_SUCCESS The item is inserted to the tail.
**/
)
{
return EFI_OUT_OF_RESOURCES;
}
return EFI_SUCCESS;
}
/**
Check whether the item is in the Map and return TRUE if it is.
@param[in] Map The netmap to search within.
@param[in] Item The item to search.
@return TRUE if the item is in the netmap, otherwise FALSE.
**/
)
{
return TRUE;
}
}
return FALSE;
}
/**
Find the key in the netmap and returns the point to the item contains the Key.
Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
item with the key to search. It returns the point to the item contains the Key if found.
If Map is NULL, then ASSERT().
@param[in] Map The netmap to search within.
@param[in] Key The key to search.
@return The point to the item contains the Key, or NULL if Key isn't in the map.
**/
)
{
return Item;
}
}
return NULL;
}
/**
Remove the node entry of the item from the netmap and return the key of the removed item.
Remove the node entry of the item from the Used doubly linked list of the netmap.
The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
Value will point to the value of the item. It returns the key of the removed item.
If Map is NULL, then ASSERT().
If Item is NULL, then ASSERT().
if item in not in the netmap, then ASSERT().
@param[in, out] Map The netmap to remove the item from.
@param[in, out] Item The item to remove.
@param[out] Value The variable to receive the value if not NULL.
@return The key of the removed item.
**/
VOID *
)
{
}
}
/**
Remove the first node entry on the netmap and return the key of the removed item.
Remove the first node entry from the Used doubly linked list of the netmap.
The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
parameter Value will point to the value of the item. It returns the key of the removed item.
If Map is NULL, then ASSERT().
If the Used doubly linked list is empty, then ASSERT().
@param[in, out] Map The netmap to remove the head from.
@param[out] Value The variable to receive the value if not NULL.
@return The key of the item removed.
**/
VOID *
)
{
//
// Often, it indicates a programming error to remove
// the first entry in an empty list
//
}
}
/**
Remove the last node entry on the netmap and return the key of the removed item.
Remove the last node entry from the Used doubly linked list of the netmap.
The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
parameter Value will point to the value of the item. It returns the key of the removed item.
If Map is NULL, then ASSERT().
If the Used doubly linked list is empty, then ASSERT().
@param[in, out] Map The netmap to remove the tail from.
@param[out] Value The variable to receive the value if not NULL.
@return The key of the item removed.
**/
VOID *
)
{
//
// Often, it indicates a programming error to remove
// the last entry in an empty list
//
}
}
/**
Iterate through the netmap and call CallBack for each item.
It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
from the loop. It returns the CallBack's last return value. This function is
delete safe for the current item.
If Map is NULL, then ASSERT().
If CallBack is NULL, then ASSERT().
@param[in] Map The Map to iterate through.
@param[in] CallBack The callback function to call for each item.
@param[in] Arg The opaque parameter to the callback.
@retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
return EFI_SUCCESS.
@retval Others It returns the CallBack's last return value.
**/
)
{
if (IsListEmpty (Head)) {
return EFI_SUCCESS;
}
return Result;
}
}
return EFI_SUCCESS;
}
/**
Internal function to get the child handle of the NIC handle.
@param[in] Controller NIC controller handle.
@param[out] ChildHandle Returned child handle.
@retval EFI_SUCCESS Successfully to get child handle.
@retval Others Failed to get child handle.
**/
)
{
//
// Locate all EFI Hii Config Access protocols
//
NULL,
);
return Status;
}
Status = EfiTestChildHandle (Controller, Handles[Index], &gEfiManagedNetworkServiceBindingProtocolGuid);
//
// Get device path on the child handle
//
(VOID **) &ChildDeviceDevicePath
);
while (!IsDevicePathEnd (ChildDeviceDevicePath)) {
//
// Parse one instance
//
//
// Found item matched gEfiNicIp4ConfigVariableGuid
//
return EFI_SUCCESS;
}
}
}
}
}
}
return Status;
}
/**
This is the default unload handle for all the network drivers.
Disconnect the driver specified by ImageHandle from all the devices in the handle database.
Uninstall all the protocols installed in the driver entry point.
@param[in] ImageHandle The drivers' driver image.
@retval EFI_SUCCESS The image is unloaded.
@retval Others Failed to unload the image.
**/
)
{
//
// Get the list of all the handles in the handle database.
// If there is an error getting the list, then the unload
// operation fails.
//
NULL,
NULL,
);
return Status;
}
//
// Disconnect the driver specified by ImageHandle from all
// the devices in the handle database.
//
);
}
//
// Uninstall all the protocols installed in the driver entry point
//
(VOID **) &DriverBinding
);
continue;
}
continue;
}
);
(VOID **) &ComponentName
);
);
}
(VOID **) &ComponentName2
);
);
}
}
//
// Free the buffer containing the list of handles from the handle database
//
if (DeviceHandleBuffer != NULL) {
}
return EFI_SUCCESS;
}
/**
Create a child of the service that is identified by ServiceBindingGuid.
Get the ServiceBinding Protocol first, then use it to create a child.
If ServiceBindingGuid is NULL, then ASSERT().
If ChildHandle is NULL, then ASSERT().
@param[in] Controller The controller which has the service installed.
@param[in] Image The image handle used to open service.
@param[in] ServiceBindingGuid The service's Guid.
@param[in, out] ChildHandle The handle to receive the create child.
@retval EFI_SUCCESS The child is successfully created.
@retval Others Failed to create the child.
**/
)
{
//
// Get the ServiceBinding Protocol
//
);
return Status;
}
//
// Create a child
//
return Status;
}
/**
Destory a child of the service that is identified by ServiceBindingGuid.
Get the ServiceBinding Protocol first, then use it to destroy a child.
If ServiceBindingGuid is NULL, then ASSERT().
@param[in] Controller The controller which has the service installed.
@param[in] Image The image handle used to open service.
@param[in] ServiceBindingGuid The service's Guid.
@param[in] ChildHandle The child to destory.
@retval EFI_SUCCESS The child is successfully destoried.
@retval Others Failed to destory the child.
**/
)
{
//
// Get the ServiceBinding Protocol
//
);
return Status;
}
//
// destory the child
//
return Status;
}
/**
Get handle with Simple Network Protocol installed on it.
There should be MNP Service Binding Protocol installed on the input ServiceHandle.
If Simple Network Protocol is already installed on the ServiceHandle, the
ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,
try to find its parent handle with SNP installed.
@param[in] ServiceHandle The handle where network service binding protocols are
installed on.
@param[out] Snp The pointer to store the address of the SNP instance.
This is an optional parameter that may be NULL.
@return The SNP handle, or NULL if not found.
**/
)
{
//
// Try to open SNP from ServiceHandle
//
SnpInstance = NULL;
Status = gBS->HandleProtocol (ServiceHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);
*Snp = SnpInstance;
}
return ServiceHandle;
}
//
// Failed to open SNP, try to get SNP handle by LocateDevicePath()
//
if (DevicePath == NULL) {
return NULL;
}
//
// Failed to find SNP handle
//
return NULL;
}
*Snp = SnpInstance;
}
return SnpHandle;
}
return NULL;
}
/**
Retrieve VLAN ID of a VLAN device handle.
Search VLAN device path node in Device Path of specified ServiceHandle and
return its VLAN ID. If no VLAN device path node found, then this ServiceHandle
is not a VLAN device handle, and 0 will be returned.
@param[in] ServiceHandle The handle where network service binding protocols are
installed on.
@return VLAN ID of the device handle, or 0 if not a VLAN device.
**/
)
{
if (DevicePath == NULL) {
return 0;
}
Node = DevicePath;
while (!IsDevicePathEnd (Node)) {
}
}
return 0;
}
/**
Find VLAN device handle with specified VLAN ID.
The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.
This function will append VLAN device path node to the parent device path,
and then use LocateDevicePath() to find the correct VLAN device handle.
@param[in] ControllerHandle The handle where network service binding protocols are
installed on.
@param[in] VlanId The configured VLAN ID for the VLAN device.
@return The VLAN device handle, or NULL if not found.
**/
)
{
if (ParentDevicePath == NULL) {
return NULL;
}
//
// Construct VLAN device path
//
);
if (VlanDevicePath == NULL) {
return NULL;
}
//
// Find VLAN device handle
//
);
if (!IsDevicePathEnd (DevicePath)) {
//
// Device path is not exactly match
//
}
return Handle;
}
/**
Get MAC address associated with the network service handle.
There should be MNP Service Binding Protocol installed on the input ServiceHandle.
If SNP is installed on the ServiceHandle or its parent handle, MAC address will
be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.
@param[in] ServiceHandle The handle where network service binding protocols are
installed on.
@param[out] MacAddress The pointer to store the returned MAC address.
@param[out] AddressSize The length of returned MAC address.
@retval EFI_SUCCESS MAC address is returned successfully.
@retval Others Failed to get SNP mode data.
**/
)
{
//
// Try to get SNP handle
//
//
// SNP found, use it directly
//
} else {
//
// Failed to get SNP handle, try to get MAC address from MNP
//
);
return Status;
}
//
// Create a MNP child
//
return Status;
}
//
// Open MNP protocol
//
);
return Status;
}
//
// Try to get SNP mode from MNP
//
return Status;
}
SnpMode = &SnpModeData;
//
// Destroy the MNP child
//
}
return EFI_SUCCESS;
}
/**
Convert MAC address of the NIC associated with specified Service Binding Handle
to a unicode string. Callers are responsible for freeing the string storage.
Locate simple network protocol associated with the Service Binding Handle and
get the mac address from SNP. Then convert the mac address into a unicode
string. It takes 2 unicode characters to represent a 1 byte binary buffer.
Plus one unicode character for the null-terminator.
@param[in] ServiceHandle The handle where network service binding protocol is
installed on.
@param[in] ImageHandle The image handle used to act as the agent handle to
get the simple network protocol.
@param[out] MacString The pointer to store the address of the string
representation of the mac address.
@retval EFI_SUCCESS Convert the mac address a unicode string successfully.
@retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
@retval Others Failed to open the simple network protocol.
**/
)
{
//
// Get MAC address of the network device
//
return Status;
}
//
// It takes 2 unicode characters to represent a 1 byte binary buffer.
// If VLAN is configured, it will need extra 5 characters like "\0005".
// Plus one unicode character for the null-terminator.
//
return EFI_OUT_OF_RESOURCES;
}
//
// Convert the MAC address into a unicode string.
//
}
//
// Append VLAN ID if any
//
if (VlanId != 0) {
*String++ = L'\\';
}
//
// Null terminate the Unicode string
//
*String = L'\0';
return EFI_SUCCESS;
}
/**
Detect media status for specified network device.
The underlying UNDI driver may or may not support reporting media status from
GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine
will try to invoke Snp->GetStatus() to get the media status: if media already
present, it return directly; if media not present, it will stop SNP and then
restart SNP to get the latest media status, this give chance to get the correct
media status for old UNDI driver which doesn't support reporting media status
from GET_STATUS command.
Note: there will be two limitations for current algorithm:
1) for UNDI with this capability, in case of cable is not attached, there will
2) for UNDI without this capability, in case that network cable is attached when
Snp->Initialize() is invoked while network cable is unattached later,
NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer
apps to wait for timeout time.
@param[in] ServiceHandle The handle where network service binding protocols are
installed on.
@param[out] MediaPresent The pointer to store the media status.
@retval EFI_SUCCESS Media detection success.
@retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.
@retval EFI_UNSUPPORTED Network device does not support media detection.
@retval EFI_DEVICE_ERROR SNP is in unknown state.
**/
)
{
//
// Get SNP handle
//
return EFI_INVALID_PARAMETER;
}
//
// Check whether SNP support media detection
//
return EFI_UNSUPPORTED;
}
//
// Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data
//
return Status;
}
//
// Media is present, return directly
//
*MediaPresent = TRUE;
return EFI_SUCCESS;
}
//
// Till now, GetStatus() report no media; while, in case UNDI not support
// reporting media status from GetStatus(), this media status may be incorrect.
// So, we will stop SNP and then restart it to get the correct media status.
//
if (OldState >= EfiSimpleNetworkMaxState) {
return EFI_DEVICE_ERROR;
}
MCastFilter = NULL;
if (OldState == EfiSimpleNetworkInitialized) {
//
// SNP is already in use, need Shutdown/Stop and then Start/Initialize
//
//
// Backup current SNP receive filter settings
//
if (MCastFilterCount != 0) {
MCastFilterCount * sizeof (EFI_MAC_ADDRESS),
);
}
//
//
}
goto Exit;
}
//
// Start/Initialize the simple network
//
}
goto Exit;
}
//
// Here we get the correct media status
//
//
// Restore SNP receive filter settings
//
Snp,
);
if (MCastFilter != NULL) {
}
return Status;
}
//
// SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted
//
if (OldState == EfiSimpleNetworkStopped) {
//
// SNP not start yet, start it
//
goto Exit;
}
}
//
// Initialize the simple network
//
goto Exit;
}
//
// Here we get the correct media status
//
//
// Shut down the simple network
//
Exit:
if (OldState == EfiSimpleNetworkStopped) {
//
// Original SNP sate is Stopped, restore to original state
//
}
if (MCastFilter != NULL) {
}
return Status;
}
/**
Check the default address used by the IPv4 driver is static or dynamic (acquired
from DHCP).
If the controller handle does not have the NIC Ip4 Config Protocol installed, the
default address is static. If the EFI variable to save the configuration is not found,
the default address is static. Otherwise, get the result from the EFI variable which
saving the configuration.
@param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
relative with the default address to judge.
@retval TRUE If the default address is static.
@retval FALSE If the default address is acquired from DHCP.
**/
)
{
ConfigInfo = NULL;
ConfigResp = NULL;
NULL,
(VOID **) &HiiConfigRouting
);
return TRUE;
}
return TRUE;
}
//
// Construct config request string header
//
ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, ChildHandle);
return TRUE;
}
if (ConfigResp == NULL) {
goto ON_EXIT;
}
L"&OFFSET=%04X&WIDTH=%04X",
sizeof (UINT32)
);
);
goto ON_EXIT;
}
if (ConfigInfo == NULL) {
goto ON_EXIT;
}
(UINT8 *) ConfigInfo,
&Len,
);
goto ON_EXIT;
}
if (AccessResults != NULL) {
}
if (ConfigInfo != NULL) {
}
if (ConfigResp != NULL) {
}
}
return IsStatic;
}
/**
Create an IPv4 device path node.
The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
The header subtype of IPv4 device path node is MSG_IPv4_DP.
Get other info from parameters to make up the whole IPv4 device path node.
@param[in, out] Node Pointer to the IPv4 device path node.
@param[in] Controller The controller handle.
@param[in] LocalIp The local IPv4 address.
@param[in] LocalPort The local port.
@param[in] RemoteIp The remote IPv4 address.
@param[in] RemotePort The remote port.
@param[in] Protocol The protocol type in the IP header.
@param[in] UseDefaultAddress Whether this instance is using default address or not.
**/
)
{
if (!UseDefaultAddress) {
} else {
}
//
// Set the Gateway IP address to default value 0:0:0:0.
// Set the Subnet mask to default value 255:255:255:0.
//
}
/**
Create an IPv6 device path node.
The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
The header subtype of IPv6 device path node is MSG_IPv6_DP.
Get other info from parameters to make up the whole IPv6 device path node.
@param[in, out] Node Pointer to the IPv6 device path node.
@param[in] Controller The controller handle.
@param[in] LocalIp The local IPv6 address.
@param[in] LocalPort The local port.
@param[in] RemoteIp The remote IPv6 address.
@param[in] RemotePort The remote port.
@param[in] Protocol The protocol type in the IP header.
**/
)
{
//
// Set default value to IPAddressOrigin, PrefixLength.
// Set the Gateway IP address to unspecified address.
//
Node->IpAddressOrigin = 0;
}
/**
packets, when MNP is stopped, IP should also be stopped. IP
needs to find its own private data which is related the IP's
Now, the controller is either a MNP or ARP child handle. But
IP opens these handle BY_DRIVER, use that info, we can get the
@param[in] Controller Then protocol handle to check.
@param[in] ProtocolGuid The protocol that is related with the handle.
**/
)
{
);
return NULL;
}
break;
}
}
return Handle;
}
/**
Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.
@param[in] String The pointer to the Ascii string.
@param[out] Ip4Address The pointer to the converted IPv4 address.
@retval EFI_SUCCESS Convert to IPv4 address successfully.
@retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
**/
)
{
return EFI_INVALID_PARAMETER;
}
Ip4Str++;
}
//
// The IPv4 address is X.X.X.X
//
if (*Ip4Str == '.') {
if (Index == 3) {
return EFI_INVALID_PARAMETER;
}
} else {
if (Index != 3) {
return EFI_INVALID_PARAMETER;
}
}
//
// Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the
// first character that is not a valid decimal character, '.' or '\0' here.
//
if (NodeVal > 0xFF) {
return EFI_INVALID_PARAMETER;
}
Ip4Str++;
}
return EFI_SUCCESS;
}
/**
Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the
string is defined in RFC 4291 - Text Pepresentation of Addresses.
@param[in] String The pointer to the Ascii string.
@param[out] Ip6Address The pointer to the converted IPv6 address.
@retval EFI_SUCCESS Convert to IPv6 address successfully.
@retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
**/
)
{
return EFI_INVALID_PARAMETER;
}
AllowedCnt = 6;
LeadZeroCnt = 0;
//
// An IPv6 address leading with : looks strange.
//
if (*Ip6Str == ':') {
return EFI_INVALID_PARAMETER;
} else {
AllowedCnt = 7;
}
}
NodeCnt = 0;
TailNodeCnt = 0;
Ip6Str++;
}
return EFI_INVALID_PARAMETER;
}
if (*Ip6Str == ':') {
if ((NodeCnt > 6) ||
//
// ::0 looks strange. report error to user.
//
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
//
// Skip the abbreviation part of IPv6 address.
//
while ((*TempStr2 != '\0')) {
if (*TempStr2 == ':') {
//
// :: can only appear once in IPv6 address.
//
return EFI_INVALID_PARAMETER;
}
TailNodeCnt++;
//
// :: indicates one or more groups of 16 bits of zeros.
//
return EFI_INVALID_PARAMETER;
}
}
TempStr2++;
}
} else {
return EFI_INVALID_PARAMETER;
}
Ip6Str++;
NodeCnt++;
//
// There are more than 8 groups of 16 bits of zeros.
//
return EFI_INVALID_PARAMETER;
}
}
}
//
// Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first
// character that is not a valid hexadecimal character, ':' or '\0' here.
//
return EFI_INVALID_PARAMETER;
}
if (NodeVal != 0) {
if ((*TempStr == '0') &&
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
} else {
return EFI_INVALID_PARAMETER;
}
}
Cnt = 0;
Cnt++;
}
if (LeadZeroCnt == 0) {
LeadZeroCnt++;
}
LeadZeroCnt++;
}
} else {
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
}
//
// Skip the groups of zeros by ::
//
}
}
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.
@param[in] String The pointer to the Ascii string.
@param[out] Ip4Address The pointer to the converted IPv4 address.
@retval EFI_SUCCESS Convert to IPv4 address successfully.
@retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
@retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_OUT_OF_RESOURCES;
}
return Status;
}
/**
Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of
the string is defined in RFC 4291 - Text Pepresentation of Addresses.
@param[in] String The pointer to the Ascii string.
@param[out] Ip6Address The pointer to the converted IPv6 address.
@retval EFI_SUCCESS Convert to IPv6 address successfully.
@retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
@retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_OUT_OF_RESOURCES;
}
return Status;
}
/**
Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.
The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses
Prefixes: ipv6-address/prefix-length.
@param[in] String The pointer to the Ascii string.
@param[out] Ip6Address The pointer to the converted IPv6 address.
@param[out] PrefixLength The pointer to the converted prefix length.
@retval EFI_SUCCESS Convert to IPv6 address successfully.
@retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
@retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_OUT_OF_RESOURCES;
}
//
// Get the sub string describing prefix length.
//
TempStr++;
}
if (*TempStr == '/') {
} else {
}
//
// Get the sub string describing IPv6 address and convert it.
//
*TempStr = '\0';
goto Exit;
}
//
// If input string doesn't indicate the prefix length, return 0xff.
//
Length = 0xFF;
//
// Convert the string to prefix length
//
Length = 0;
while (*PrefixStr != '\0') {
if (NET_IS_DIGIT (*PrefixStr)) {
if (Length >= IP6_PREFIX_NUM) {
goto Exit;
}
} else {
goto Exit;
}
PrefixStr++;
}
}
*PrefixLength = Length;
Exit:
return Status;
}
/**
This function obtains the system guid from the smbios table.
@param[out] SystemGuid The pointer of the returned system guid.
@retval EFI_SUCCESS Successfully obtained the system guid.
@retval EFI_NOT_FOUND Did not find the SMBIOS table.
**/
)
{
SmbiosTable = NULL;
return EFI_NOT_FOUND;
}
do {
//
// Older version did not support UUID.
//
return EFI_NOT_FOUND;
}
//
// SMBIOS tables are byte packed so we need to do a byte copy to
// prevend alignment faults on Itanium-based platform.
//
return EFI_SUCCESS;
}
//
// Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:
// 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed
// to skip one SMBIOS structure.
//
//
// Step 1: Skip over formatted section.
//
//
// Step 2: Skip over unformated string section.
//
do {
//
// Each string is terminated with a NULL(00h) BYTE and the sets of strings
// is terminated with an additional NULL(00h) BYTE.
//
}
//
// Pointer to the next SMBIOS structure.
//
break;
}
} while (TRUE);
return EFI_NOT_FOUND;
}