--- /dev/null
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. 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
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ IpIo.h
+
+Abstract:
+
+
+**/
+
+#ifndef _IP_IO_H_
+#define _IP_IO_H_
+
+#include <PiDxe.h>
+#include <Protocol/Ip4.h>\r
+#include <Library/IpIoLib.h>\r
+#include <Library/NetLib.h>
+
+//\r
+// type and code define for ICMP protocol error got\r
+// from IP\r
+//\r
+#define ICMP_TYPE_UNREACH 3\r
+#define ICMP_TYPE_TIMXCEED 11\r
+#define ICMP_TYPE_PARAMPROB 12\r
+#define ICMP_TYPE_SOURCEQUENCH 4\r
+\r
+#define ICMP_CODE_UNREACH_NET 0\r
+#define ICMP_CODE_UNREACH_HOST 1\r
+#define ICMP_CODE_UNREACH_PROTOCOL 2\r
+#define ICMP_CODE_UNREACH_PORT 3\r
+#define ICMP_CODE_UNREACH_NEEDFRAG 4\r
+#define ICMP_CODE_UNREACH_SRCFAIL 5\r
+#define ICMP_CODE_UNREACH_NET_UNKNOWN 6\r
+#define ICMP_CODE_UNREACH_HOST_UNKNOWN 7\r
+#define ICMP_CODE_UNREACH_ISOLATED 8\r
+#define ICMP_CODE_UNREACH_NET_PROHIB 9\r
+#define ICMP_CODE_UNREACH_HOST_PROHIB 10\r
+#define ICMP_CODE_UNREACH_TOSNET 11\r
+#define ICMP_CODE_UNREACH_TOSHOST 12\r
+\r
+//\r
+// this error will be delivered to the\r
+// listening transportation layer protocol\r
+// consuming IpIO\r
+//\r
+typedef enum {\r
+ ICMP_ERR_UNREACH_NET = 0,\r
+ ICMP_ERR_UNREACH_HOST,\r
+ ICMP_ERR_UNREACH_PROTOCOL,\r
+ ICMP_ERR_UNREACH_PORT,\r
+ ICMP_ERR_MSGSIZE,\r
+ ICMP_ERR_UNREACH_SRCFAIL,\r
+ ICMP_ERR_TIMXCEED_INTRANS,\r
+ ICMP_ERR_TIMXCEED_REASS,\r
+ ICMP_ERR_QUENCH,\r
+ ICMP_ERR_PARAMPROB\r
+} ICMP_ERROR;\r
+\r
+typedef struct _ICMP_ERROR_INFO {\r
+ EFI_STATUS Error;\r
+ BOOLEAN IsHard;\r
+ BOOLEAN Notify;\r
+} ICMP_ERROR_INFO;\r
+
+//
+// Driver Consumed Protocol Prototypes
+//
+//@MT:#include EFI_PROTOCOL_CONSUMER (Ip4)
+//@MT:#include EFI_PROTOCOL_CONSUMER (Udp4)
+
+#define EFI_IP4_HEADER_LEN(HdrPtr) ((HdrPtr)->HeaderLength << 2)
+
+extern EFI_IP4_CONFIG_DATA mIpIoDefaultIpConfigData;
+
+typedef struct _EFI_NET_SESSION_DATA {
+ IP4_ADDR Source;
+ IP4_ADDR Dest;
+ EFI_IP4_HEADER *IpHdr;
+} EFI_NET_SESSION_DATA;
+
+typedef
+VOID
+(*PKT_RCVD_NOTIFY) (
+ IN EFI_STATUS Status, // rcvd pkt result
+ IN ICMP_ERROR IcmpErr, // if Status == EFI_ICMP_ERROR, this
+ // field is valid for user
+ IN EFI_NET_SESSION_DATA *NetSession, // the communication point
+ IN NET_BUF *Pkt, // packet received
+ IN VOID *Context // the Context provided by user for recive data
+ );
+
+typedef
+VOID
+(*PKT_SENT_NOTIFY) (
+ IN EFI_STATUS Status, // sent pkt result
+ IN VOID *Context, // the context provided by user for sending data
+ IN VOID *Sender, // the sender to be notified
+ IN VOID *NotifyData // sent pkt related data to notify
+ );
+
+typedef struct _IP_IO {
+
+ //
+ // the node used to link this IpIo to the active IpIo list.
+ //
+ NET_LIST_ENTRY Entry;
+
+ // the list used to maintain the IP instance for different sending purpose.
+ //
+ NET_LIST_ENTRY IpList;
+
+ //
+ // the ip instance consumed by this IP IO
+ //
+ EFI_HANDLE Controller;
+ EFI_HANDLE Image;
+ EFI_HANDLE ChildHandle;
+ EFI_IP4_PROTOCOL *Ip;
+ BOOLEAN IsConfigured;
+
+ //
+ // some ip config data can be changed
+ //
+ UINT8 Protocol;
+
+ //
+ // token and event used to get data from IP
+ //
+ EFI_IP4_COMPLETION_TOKEN RcvToken;
+
+ //
+ // list entry used to link the token passed to IP_IO
+ //
+ NET_LIST_ENTRY PendingSndList;
+
+ //
+ // User interface used to get notify from IP_IO
+ //
+ VOID *RcvdContext;
+ VOID *SndContext;
+ PKT_RCVD_NOTIFY PktRcvdNotify;
+ PKT_SENT_NOTIFY PktSentNotify;
+} IP_IO;
+
+typedef struct _IP_IO_OPEN_DATA {
+ EFI_IP4_CONFIG_DATA IpConfigData;
+ VOID *RcvdContext;
+ VOID *SndContext;
+ PKT_RCVD_NOTIFY PktRcvdNotify;
+ PKT_SENT_NOTIFY PktSentNotify;
+} IP_IO_OPEN_DATA;
+
+typedef struct _IP_IO_SEND_ENTRY {
+ NET_LIST_ENTRY Entry;
+ IP_IO *IpIo;
+ VOID *Context;
+ VOID *NotifyData;
+ EFI_IP4_PROTOCOL *Ip;
+ NET_BUF *Pkt;
+ EFI_IP4_COMPLETION_TOKEN *SndToken;
+} IP_IO_SEND_ENTRY;
+
+typedef struct _EFI_IP4_OVERRIDE_DATA IP_IO_OVERRIDE;
+
+typedef struct _IP_IO_IP_INFO {
+ IP4_ADDR Addr;
+ IP4_ADDR SubnetMask;
+ NET_LIST_ENTRY Entry;
+ EFI_HANDLE ChildHandle;
+ EFI_IP4_PROTOCOL *Ip;
+ EFI_IP4_COMPLETION_TOKEN DummyRcvToken;
+ INTN RefCnt;
+} IP_IO_IP_INFO;
+
+IP_IO *
+IpIoCreate (
+ IN EFI_HANDLE Image,
+ IN EFI_HANDLE Controller
+ );
+
+EFI_STATUS
+IpIoDestroy (
+ IN IP_IO *IpIo
+ );
+
+EFI_STATUS
+IpIoStop (
+ IN IP_IO *IpIo
+ );
+
+EFI_STATUS
+IpIoOpen (
+ IN IP_IO *IpIo,
+ IN IP_IO_OPEN_DATA *OpenData
+ );
+
+EFI_STATUS
+IpIoSend (
+ IN IP_IO *IpIo,
+ IN NET_BUF *Pkt,
+ IN IP_IO_IP_INFO *Sender,
+ IN VOID *Context OPTIONAL,
+ IN VOID *NotifyData OPTIONAL,
+ IN IP4_ADDR Dest,
+ IN IP_IO_OVERRIDE *OverrideData
+ );
+
+VOID
+IpIoCancelTxToken (
+ IN IP_IO *IpIo,
+ IN VOID *Packet
+ );
+
+IP_IO_IP_INFO *
+IpIoAddIp (
+ IN IP_IO *IpIo
+ );
+
+EFI_STATUS
+IpIoConfigIp (
+ IN IP_IO_IP_INFO *IpInfo,
+ IN OUT EFI_IP4_CONFIG_DATA *Ip4ConfigData OPTIONAL
+ );
+
+VOID
+IpIoRemoveIp (
+ IN IP_IO *IpIo,
+ IN IP_IO_IP_INFO *IpInfo
+ );
+
+IP_IO_IP_INFO *
+IpIoFindSender (
+ IN OUT IP_IO **IpIo,
+ IN IP4_ADDR Src
+ );
+
+EFI_STATUS
+IpIoGetIcmpErrStatus (
+ IN ICMP_ERROR IcmpError,
+ OUT BOOLEAN *IsHard, OPTIONAL
+ OUT BOOLEAN *Notify OPTIONAL
+ );
+
+#endif
--- /dev/null
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. 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
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ NetLib.h
+
+Abstract:
+
+ Library for the UEFI network stack.
+
+
+**/
+
+#ifndef _NET_LIB_H_
+#define _NET_LIB_H_
+
+#include <PiDxe.h>
+#include <Protocol/DriverBinding.h>
+#include <Protocol/ComponentName.h>
+#include <Protocol/DriverConfiguration.h>
+#include <Protocol/DriverDiagnostics.h>
+
+#define EFI_NET_LITTLE_ENDIAN
+
+typedef UINT32 IP4_ADDR;
+typedef UINT32 TCP_SEQNO;
+typedef UINT16 TCP_PORTNO;
+
+enum {
+ NET_ETHER_ADDR_LEN = 6,
+ NET_IFTYPE_ETHERNET = 0x01,
+
+ EFI_IP_PROTO_UDP = 0x11,
+ EFI_IP_PROTO_TCP = 0x06,
+ EFI_IP_PROTO_ICMP = 0x01,
+
+ //
+ // The address classfication
+ //
+ IP4_ADDR_CLASSA = 1,
+ IP4_ADDR_CLASSB,
+ IP4_ADDR_CLASSC,
+ IP4_ADDR_CLASSD,
+ IP4_ADDR_CLASSE,
+
+ IP4_MASK_NUM = 33,
+};
+
+#pragma pack(1)
+
+//
+// Ethernet head definition
+//
+typedef struct {
+ UINT8 DstMac [NET_ETHER_ADDR_LEN];
+ UINT8 SrcMac [NET_ETHER_ADDR_LEN];
+ UINT16 EtherType;
+} ETHER_HEAD;
+
+
+//
+// The EFI_IP4_HEADER is hard to use because the source and
+// destination address are defined as EFI_IPv4_ADDRESS, which
+// is a structure. Two structures can't be compared or masked
+// directly. This is why there is an internal representation.
+//
+typedef struct {
+#ifdef EFI_NET_LITTLE_ENDIAN
+ UINT8 HeadLen : 4;
+ UINT8 Ver : 4;
+#else
+ UINT8 Ver : 4;
+ UINT8 HeadLen : 4;
+#endif
+ UINT8 Tos;
+ UINT16 TotalLen;
+ UINT16 Id;
+ UINT16 Fragment;
+ UINT8 Ttl;
+ UINT8 Protocol;
+ UINT16 Checksum;
+ IP4_ADDR Src;
+ IP4_ADDR Dst;
+} IP4_HEAD;
+
+
+//
+// ICMP head definition. ICMP message is categoried as either an error
+// message or query message. Two message types have their own head format.
+//
+typedef struct {
+ UINT8 Type;
+ UINT8 Code;
+ UINT16 Checksum;
+} IP4_ICMP_HEAD;
+
+typedef struct {
+ IP4_ICMP_HEAD Head;
+ UINT32 Fourth; // 4th filed of the head, it depends on Type.
+ IP4_HEAD IpHead;
+} IP4_ICMP_ERROR_HEAD;
+
+typedef struct {
+ IP4_ICMP_HEAD Head;
+ UINT16 Id;
+ UINT16 Seq;
+} IP4_ICMP_QUERY_HEAD;
+
+
+//
+// UDP header definition
+//
+typedef struct {
+ UINT16 SrcPort;
+ UINT16 DstPort;
+ UINT16 Length;
+ UINT16 Checksum;
+} EFI_UDP4_HEADER;
+
+
+//
+// TCP header definition
+//
+typedef struct {
+ TCP_PORTNO SrcPort;
+ TCP_PORTNO DstPort;
+ TCP_SEQNO Seq;
+ TCP_SEQNO Ack;
+#ifdef EFI_NET_LITTLE_ENDIAN
+ UINT8 Res : 4;
+ UINT8 HeadLen : 4;
+#else
+ UINT8 HeadLen : 4;
+ UINT8 Res : 4;
+#endif
+ UINT8 Flag;
+ UINT16 Wnd;
+ UINT16 Checksum;
+ UINT16 Urg;
+} TCP_HEAD;
+
+#pragma pack()
+
+#define NET_MAC_EQUAL(pMac1, pMac2, Len) \
+ (NetCompareMem ((pMac1), (pMac2), Len) == 0)
+
+#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
+ (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
+
+#ifdef EFI_NET_LITTLE_ENDIAN
+#define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
+ (((UINT32) (x) & 0xff00) << 8) | \
+ (((UINT32) (x) & 0xff0000) >> 8) | \
+ (((UINT32) (x) & 0xff000000) >> 24))
+
+#define HTONL(x) NTOHL(x)
+
+#define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
+ (((UINT16) (x) & 0xff00) >> 8))
+
+#define HTONS(x) NTOHS(x)
+#else
+#define NTOHL(x) (UINT32)(x)
+#define HTONL(x) (UINT32)(x)
+#define NTOHS(x) (UINT16)(x)
+#define HTONS(x) (UINT16)(x)
+#endif
+
+//
+// Test the IP's attribute, All the IPs are in host byte order.
+//
+#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)
+#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)
+#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))
+#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)
+
+//
+// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.
+//
+#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))
+#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))
+#define EFI_IP_EQUAL(Ip1, Ip2) (EFI_IP4 (Ip1) == EFI_IP4 (Ip2))
+
+INTN
+NetGetMaskLength (
+ IN IP4_ADDR Mask
+ );
+
+INTN
+NetGetIpClass (
+ IN IP4_ADDR Addr
+ );
+
+BOOLEAN
+Ip4IsUnicast (
+ IN IP4_ADDR Ip,
+ IN IP4_ADDR NetMask
+ );
+
+extern IP4_ADDR mIp4AllMasks [IP4_MASK_NUM];
+
+//@MT:#include EFI_PROTOCOL_CONSUMER (LoadedImage)
+//@MT:#include EFI_PROTOCOL_CONSUMER (ServiceBinding)
+//@MT:#include EFI_PROTOCOL_CONSUMER (SimpleNetwork)
+
+//
+// Wrap functions to ease the impact of EFI library changes.
+//
+#define NetAllocateZeroPool AllocateZeroPool
+#define NetAllocatePool AllocatePool
+#define NetFreePool gBS->FreePool
+#define NetCopyMem CopyMem
+#define NetSetMem SetMem
+#define NetZeroMem(Dest, Len) SetMem ((Dest), (Len), 0)
+#define NetCompareMem CompareMem
+
+//
+// Lock primitives: the stack implements its lock primitives according
+// to the standard EFI enviornment. It will NOT consider multiprocessor.
+//
+#define NET_TPL_LOCK TPL_CALLBACK
+#define NET_TPL_RECYCLE_LOCK (NET_TPL_LOCK + 1)
+#define NET_TPL_EVENT TPL_CALLBACK
+#define NET_TPL_RECYCLE (NET_TPL_LOCK + 1)
+#define NET_TPL_SLOW_TIMER (TPL_CALLBACK - 1)
+#define NET_TPL_FAST_TIMER NET_TPL_RECYCLE
+#define NET_TPL_TIMER TPL_CALLBACK
+
+#define NET_LOCK EFI_LOCK
+#define NET_LOCK_INIT(x) EfiInitializeLock (x, NET_TPL_LOCK)
+#define NET_RECYCLE_LOCK_INIT(x) EfiInitializeLock (x, NET_TPL_RECYCLE_LOCK)
+#define NET_TRYLOCK(x) EfiAcquireLockOrFail (x)
+#define NET_UNLOCK(x) EfiReleaseLock (x)
+
+#define NET_RAISE_TPL(x) (gBS->RaiseTPL (x))
+#define NET_RESTORE_TPL(x) (gBS->RestoreTPL (x))
+
+#define TICKS_PER_MS 10000U
+#define TICKS_PER_SECOND 10000000U
+
+#define NET_MIN(a, b) ((a) < (b) ? (a) : (b))
+#define NET_MAX(a, b) ((a) > (b) ? (a) : (b))
+#define NET_RANDOM(Seed) (((Seed) * 1103515245L + 12345) % 4294967295L)
+
+
+UINT32
+NetGetUint32 (
+ IN UINT8 *Buf
+ );
+
+VOID
+NetPutUint32 (
+ IN UINT8 *Buf,
+ IN UINT32 Data
+ );
+
+UINT32
+NetRandomInitSeed (
+ VOID
+ );
+
+
+//
+// Double linked list entry functions, this extends the
+// EFI list functions.
+//
+typedef LIST_ENTRY NET_LIST_ENTRY;
+
+#define NetListInit(Head) InitializeListHead(Head)
+#define NetListInsertHead(Head, Entry) InsertHeadList((Head), (Entry))
+#define NetListInsertTail(Head, Entry) InsertTailList((Head), (Entry))
+#define NetListIsEmpty(List) IsListEmpty(List)
+
+#define NET_LIST_USER_STRUCT(Entry, Type, Field) \
+ _CR(Entry, Type, Field)
+
+#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \
+ CR(Entry, Type, Field, Sig)
+
+//
+// Iterate through the doule linked list. It is NOT delete safe
+//
+#define NET_LIST_FOR_EACH(Entry, ListHead) \
+ for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)
+
+//
+// Iterate through the doule linked list. This is delete-safe.
+// Don't touch NextEntry. Also, don't use this macro if list
+// entries other than the Entry may be deleted when processing
+// the current Entry.
+//
+#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \
+ for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \
+ Entry != (ListHead); \
+ Entry = NextEntry, NextEntry = Entry->ForwardLink \
+ )
+
+//
+// Make sure the list isn't empty before get the frist/last record.
+//
+#define NET_LIST_HEAD(ListHead, Type, Field) \
+ NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)
+
+#define NET_LIST_TAIL(ListHead, Type, Field) \
+ NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)
+
+#define NetListRemoveEntry(Entry) RemoveEntryList (Entry)
+
+NET_LIST_ENTRY*
+NetListRemoveHead (
+ NET_LIST_ENTRY *Head
+ );
+
+NET_LIST_ENTRY*
+NetListRemoveTail (
+ NET_LIST_ENTRY *Head
+ );
+
+VOID
+NetListInsertAfter (
+ IN NET_LIST_ENTRY *PrevEntry,
+ IN NET_LIST_ENTRY *NewEntry
+ );
+
+VOID
+NetListInsertBefore (
+ IN NET_LIST_ENTRY *PostEntry,
+ IN NET_LIST_ENTRY *NewEntry
+ );
+
+
+//
+// Object container: EFI network stack spec defines various kinds of
+// tokens. The drivers can share code to manage those objects.
+//
+typedef struct {
+ NET_LIST_ENTRY Link;
+ VOID *Key;
+ VOID *Value;
+} NET_MAP_ITEM;
+
+typedef struct {
+ NET_LIST_ENTRY Used;
+ NET_LIST_ENTRY Recycled;
+ UINTN Count;
+} NET_MAP;
+
+#define NET_MAP_INCREAMENT 64
+
+VOID
+NetMapInit (
+ IN NET_MAP *Map
+ );
+
+VOID
+NetMapClean (
+ IN NET_MAP *Map
+ );
+
+BOOLEAN
+NetMapIsEmpty (
+ IN NET_MAP *Map
+ );
+
+UINTN
+NetMapGetCount (
+ IN NET_MAP *Map
+ );
+
+EFI_STATUS
+NetMapInsertHead (
+ IN NET_MAP *Map,
+ IN VOID *Key,
+ IN VOID *Value OPTIONAL
+ );
+
+EFI_STATUS
+NetMapInsertTail (
+ IN NET_MAP *Map,
+ IN VOID *Key,
+ IN VOID *Value OPTIONAL
+ );
+
+NET_MAP_ITEM *
+NetMapFindKey (
+ IN NET_MAP *Map,
+ IN VOID *Key
+ );
+
+VOID *
+NetMapRemoveItem (
+ IN NET_MAP *Map,
+ IN NET_MAP_ITEM *Item,
+ OUT VOID **Value OPTIONAL
+ );
+
+VOID *
+NetMapRemoveHead (
+ IN NET_MAP *Map,
+ OUT VOID **Value OPTIONAL
+ );
+
+VOID *
+NetMapRemoveTail (
+ IN NET_MAP *Map,
+ OUT VOID **Value OPTIONAL
+ );
+
+typedef
+EFI_STATUS
+(*NET_MAP_CALLBACK) (
+ IN NET_MAP *Map,
+ IN NET_MAP_ITEM *Item,
+ IN VOID *Arg
+ );
+
+EFI_STATUS
+NetMapIterate (
+ IN NET_MAP *Map,
+ IN NET_MAP_CALLBACK CallBack,
+ IN VOID *Arg OPTIONAL
+ );
+
+
+//
+// Helper functions to implement driver binding and service binding protocols.
+//
+EFI_STATUS
+NetLibCreateServiceChild (
+ IN EFI_HANDLE ControllerHandle,
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_GUID *ServiceBindingGuid,
+ OUT EFI_HANDLE *ChildHandle
+ );
+
+EFI_STATUS
+NetLibDestroyServiceChild (
+ IN EFI_HANDLE ControllerHandle,
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_GUID *ServiceBindingGuid,
+ IN EFI_HANDLE ChildHandle
+ );
+
+EFI_STATUS
+NetLibGetMacString (
+ IN EFI_HANDLE SnpHandle,
+ IN EFI_HANDLE ImageHandle,
+ IN OUT CONST CHAR16 **MacString
+ );
+
+EFI_HANDLE
+NetLibGetNicHandle (
+ IN EFI_HANDLE Controller,
+ IN EFI_GUID *ProtocolGuid
+ );
+
+typedef
+EFI_STATUS
+(EFIAPI *NET_LIB_DRIVER_UNLOAD) (
+ IN EFI_HANDLE ImageHandle
+ );
+
+EFI_STATUS
+EFIAPI
+NetLibDefaultUnload (
+ IN EFI_HANDLE ImageHandle
+ );
+
+EFI_STATUS
+NetLibInstallAllDriverProtocolsWithUnload (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable,
+ IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding,
+ IN EFI_HANDLE DriverBindingHandle,
+ IN EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL
+ IN EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL
+ IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics, OPTIONAL
+ IN NET_LIB_DRIVER_UNLOAD CustomizedUnload
+ );
+
+EFI_STATUS
+NetLibInstallAllDriverProtocols (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable,
+ IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding,
+ IN EFI_HANDLE DriverBindingHandle,
+ IN EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL
+ IN EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL
+ IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics OPTIONAL
+ );
+\r
+enum {
+ //
+ //Various signatures
+ //
+ NET_BUF_SIGNATURE = EFI_SIGNATURE_32 ('n', 'b', 'u', 'f'),
+ NET_VECTOR_SIGNATURE = EFI_SIGNATURE_32 ('n', 'v', 'e', 'c'),
+ NET_QUE_SIGNATURE = EFI_SIGNATURE_32 ('n', 'b', 'q', 'u'),
+
+
+ NET_PROTO_DATA = 64, // Opaque buffer for protocols
+ NET_BUF_HEAD = 1, // Trim or allocate space from head
+ NET_BUF_TAIL = 0, // Trim or allocate space from tail
+ NET_VECTOR_OWN_FIRST = 0x01, // We allocated the 1st block in the vector
+};
+
+#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \
+ ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))
+
+#define NET_SWAP_SHORT(Value) \
+ ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))
+
+//
+// Single memory block in the vector.
+//
+typedef struct {
+ UINT32 Len; // The block's length
+ UINT8 *Bulk; // The block's Data
+} NET_BLOCK;
+
+typedef VOID (*NET_VECTOR_EXT_FREE) (VOID *Arg);
+
+//
+//NET_VECTOR contains several blocks to hold all packet's
+//fragments and other house-keeping stuff for sharing. It
+//doesn't specify the where actual packet fragment begins.
+//
+typedef struct {
+ UINT32 Signature;
+ INTN RefCnt; // Reference count to share NET_VECTOR.
+ NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR
+ VOID *Arg; // opeque argument to Free
+ UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST
+ UINT32 Len; // Total length of the assocated BLOCKs
+
+ UINT32 BlockNum;
+ NET_BLOCK Block[1];
+} NET_VECTOR;
+
+//
+//NET_BLOCK_OP operate on the NET_BLOCK, It specifies
+//where the actual fragment begins and where it ends
+//
+typedef struct {
+ UINT8 *BlockHead; // Block's head, or the smallest valid Head
+ UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length
+ UINT8 *Head; // 1st byte of the data in the block
+ UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size
+ UINT32 Size; // The size of the data
+} NET_BLOCK_OP;
+
+
+//
+//NET_BUF is the buffer manage structure used by the
+//network stack. Every network packet may be fragmented,
+//and contains multiple fragments. The Vector points to
+//memory blocks used by the each fragment, and BlockOp
+//specifies where each fragment begins and ends.
+//
+//It also contains a opaque area for protocol to store
+//per-packet informations. Protocol must be caution not
+//to overwrite the members after that.
+//
+typedef struct {
+ UINT32 Signature;
+ INTN RefCnt;
+ NET_LIST_ENTRY List; // The List this NET_BUF is on
+
+ IP4_HEAD *Ip; // Network layer header, for fast access
+ TCP_HEAD *Tcp; // Transport layer header, for fast access
+ UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data
+
+ NET_VECTOR *Vector; // The vector containing the packet
+
+ UINT32 BlockOpNum; // Total number of BlockOp in the buffer
+ UINT32 TotalSize; // Total size of the actual packet
+ NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet
+} NET_BUF;
+
+
+//
+//A queue of NET_BUFs, It is just a thin extension of
+//NET_BUF functions.
+//
+typedef struct {
+ UINT32 Signature;
+ INTN RefCnt;
+ NET_LIST_ENTRY List; // The List this buffer queue is on
+
+ NET_LIST_ENTRY BufList; // list of queued buffers
+ UINT32 BufSize; // total length of DATA in the buffers
+ UINT32 BufNum; // total number of buffers on the chain
+} NET_BUF_QUEUE;
+
+//
+// Pseudo header for TCP and UDP checksum
+//
+#pragma pack(1)
+typedef struct {
+ IP4_ADDR SrcIp;
+ IP4_ADDR DstIp;
+ UINT8 Reserved;
+ UINT8 Protocol;
+ UINT16 Len;
+} NET_PSEUDO_HDR;
+#pragma pack()
+
+//
+// The fragment entry table used in network interfaces. This is
+// the same as NET_BLOCK now. Use two different to distinguish
+// the two in case that NET_BLOCK be enhanced later.
+//
+typedef struct {
+ UINT32 Len;
+ UINT8 *Bulk;
+} NET_FRAGMENT;
+
+#define NET_GET_REF(PData) ((PData)->RefCnt++)
+#define NET_PUT_REF(PData) ((PData)->RefCnt--)
+#define NETBUF_FROM_PROTODATA(Info) _CR((Info), NET_BUF, ProtoData)
+
+#define NET_BUF_SHARED(Buf) \
+ (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))
+
+#define NET_VECTOR_SIZE(BlockNum) \
+ (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))
+
+#define NET_BUF_SIZE(BlockOpNum) \
+ (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
+
+#define NET_HEADSPACE(BlockOp) \
+ (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
+
+#define NET_TAILSPACE(BlockOp) \
+ (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
+
+NET_BUF *
+NetbufAlloc (
+ IN UINT32 Len
+ );
+
+VOID
+NetbufFree (
+ IN NET_BUF *Nbuf
+ );
+
+
+UINT8 *
+NetbufGetByte (
+ IN NET_BUF *Nbuf,
+ IN UINT32 Offset,
+ OUT UINT32 *Index OPTIONAL
+ );
+
+NET_BUF *
+NetbufClone (
+ IN NET_BUF *Nbuf
+ );
+
+NET_BUF *
+NetbufDuplicate (
+ IN NET_BUF *Nbuf,
+ IN NET_BUF *Duplicate OPTIONAL,
+ IN UINT32 HeadSpace
+ );
+
+NET_BUF *
+NetbufGetFragment (
+ IN NET_BUF *Nbuf,
+ IN UINT32 Offset,
+ IN UINT32 Len,
+ IN UINT32 HeadSpace
+ );
+
+VOID
+NetbufReserve (
+ IN NET_BUF *Nbuf,
+ IN UINT32 Len
+ );
+
+UINT8 *
+NetbufAllocSpace (
+ IN NET_BUF *Nbuf,
+ IN UINT32 Len,
+ IN BOOLEAN FromHead
+ );
+
+UINT32
+NetbufTrim (
+ IN NET_BUF *Nbuf,
+ IN UINT32 Len,
+ IN BOOLEAN FromHead
+ );
+
+UINT32
+NetbufCopy (
+ IN NET_BUF *Nbuf,
+ IN UINT32 Offset,
+ IN UINT32 Len,
+ IN UINT8 *Dest
+ );
+
+NET_BUF *
+NetbufFromExt (
+ IN NET_FRAGMENT *ExtFragment,
+ IN UINT32 ExtNum,
+ IN UINT32 HeadSpace,
+ IN UINT32 HeadLen,
+ IN NET_VECTOR_EXT_FREE ExtFree,
+ IN VOID *Arg OPTIONAL
+ );
+
+EFI_STATUS
+NetbufBuildExt (
+ IN NET_BUF *Nbuf,
+ IN NET_FRAGMENT *ExtFragment,
+ IN UINT32 *ExtNum
+ );
+
+NET_BUF *
+NetbufFromBufList (
+ IN NET_LIST_ENTRY *BufList,
+ IN UINT32 HeadSpace,
+ IN UINT32 HeaderLen,
+ IN NET_VECTOR_EXT_FREE ExtFree,
+ IN VOID *Arg OPTIONAL
+ );
+
+VOID
+NetbufFreeList (
+ IN NET_LIST_ENTRY *Head
+ );
+
+VOID
+NetbufQueInit (
+ IN NET_BUF_QUEUE *NbufQue
+ );
+
+NET_BUF_QUEUE *
+NetbufQueAlloc (
+ VOID
+ );
+
+VOID
+NetbufQueFree (
+ IN NET_BUF_QUEUE *NbufQue
+ );
+
+NET_BUF *
+NetbufQueRemove (
+ IN NET_BUF_QUEUE *NbufQue
+ );
+
+VOID
+NetbufQueAppend (
+ IN NET_BUF_QUEUE *NbufQue,
+ IN NET_BUF *Nbuf
+ );
+
+UINT32
+NetbufQueCopy (
+ IN NET_BUF_QUEUE *NbufQue,
+ IN UINT32 Offset,
+ IN UINT32 Len,
+ IN UINT8 *Dest
+ );
+
+UINT32
+NetbufQueTrim (
+ IN NET_BUF_QUEUE *NbufQue,
+ IN UINT32 Len
+ );
+
+VOID
+NetbufQueFlush (
+ IN NET_BUF_QUEUE *NbufQue
+ );
+
+UINT16
+NetblockChecksum (
+ IN UINT8 *Bulk,
+ IN UINT32 Len
+ );
+
+UINT16
+NetAddChecksum (
+ IN UINT16 Checksum1,
+ IN UINT16 Checksum2
+ );
+
+UINT16
+NetbufChecksum (
+ IN NET_BUF *Nbuf
+ );
+
+UINT16
+NetPseudoHeadChecksum (
+ IN IP4_ADDR Src,
+ IN IP4_ADDR Dst,
+ IN UINT8 Proto,
+ IN UINT16 Len
+ );
+#endif
--- /dev/null
+/** @file
+
+Copyright (c) 2006, Intel Corporation
+All rights reserved. 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
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+
+Module Name:
+
+ Udp4Io.h
+
+Abstract:
+
+ The helper routines to access UDP service. It is used by both
+ DHCP and MTFTP.
+
+
+**/
+
+#ifndef _UDP4IO_H_
+#define _UDP4IO_H_
+
+#include <PiDxe.h>\r
+\r
+#include <Protocol/Udp4.h>\r
+\r
+#include <Library/UdpIoLib.h>
+#include <Library/NetLib.h>
+
+typedef struct _UDP_IO_PORT UDP_IO_PORT;
+
+enum {
+ UDP_IO_RX_SIGNATURE = EFI_SIGNATURE_32 ('U', 'D', 'P', 'R'),
+ UDP_IO_TX_SIGNATURE = EFI_SIGNATURE_32 ('U', 'D', 'P', 'T'),
+ UDP_IO_SIGNATURE = EFI_SIGNATURE_32 ('U', 'D', 'P', 'I'),
+};
+
+typedef struct {
+ IP4_ADDR LocalAddr;
+ UINT16 LocalPort;
+ IP4_ADDR RemoteAddr;
+ UINT16 RemotePort;
+} UDP_POINTS;
+
+//
+// This prototype is used by both receive and transmission.
+// When receiving Netbuf is allocated by UDP access point, and
+// released by user. When transmitting, the NetBuf is from user,
+// and provided to the callback as a reference.
+//
+typedef
+VOID
+(*UDP_IO_CALLBACK) (
+ IN NET_BUF *Packet,
+ IN UDP_POINTS *Points,
+ IN EFI_STATUS IoStatus,
+ IN VOID *Context
+ );
+
+//
+// Each receive request is wrapped in an UDP_RX_TOKEN. Upon completion,
+// the CallBack will be called. Only one receive request is send to UDP.
+// HeadLen gives the length of the application's header. UDP_IO will
+// make the application's header continous before delivery up.
+//
+typedef struct {
+ UINT32 Signature;
+ UDP_IO_PORT *UdpIo;
+
+ UDP_IO_CALLBACK CallBack;
+ VOID *Context;
+
+ UINT32 HeadLen;
+ EFI_UDP4_COMPLETION_TOKEN UdpToken;
+} UDP_RX_TOKEN;
+
+//
+// Each transmit request is wrapped in an UDP_TX_TOKEN. Upon completion,
+// the CallBack will be called. There can be several transmit requests.
+//
+typedef struct {
+ UINT32 Signature;
+ NET_LIST_ENTRY Link;
+ UDP_IO_PORT *UdpIo;
+
+ UDP_IO_CALLBACK CallBack;
+ NET_BUF *Packet;
+ VOID *Context;
+
+ EFI_UDP4_SESSION_DATA UdpSession;
+ EFI_IPv4_ADDRESS Gateway;
+
+ EFI_UDP4_COMPLETION_TOKEN UdpToken;
+ EFI_UDP4_TRANSMIT_DATA UdpTxData;
+} UDP_TX_TOKEN;
+
+typedef struct _UDP_IO_PORT {
+ UINT32 Signature;
+ NET_LIST_ENTRY Link;
+ INTN RefCnt;
+
+ //
+ // Handle used to create/destory UDP child
+ //
+ EFI_HANDLE Controller;
+ EFI_HANDLE Image;
+ EFI_HANDLE UdpHandle;
+
+ EFI_UDP4_PROTOCOL *Udp;
+ EFI_UDP4_CONFIG_DATA UdpConfig;
+ EFI_SIMPLE_NETWORK_MODE SnpMode;
+
+ NET_LIST_ENTRY SentDatagram;
+ UDP_RX_TOKEN *RecvRequest;
+};
+
+typedef
+EFI_STATUS
+(*UDP_IO_CONFIG) (
+ IN UDP_IO_PORT *UdpIo,
+ IN VOID *Context
+ );
+
+typedef
+BOOLEAN
+(*UDP_IO_TO_CANCEL) (
+ IN UDP_TX_TOKEN *Token,
+ IN VOID *Context
+ );
+
+UDP_IO_PORT *
+UdpIoCreatePort (
+ IN EFI_HANDLE Controller,
+ IN EFI_HANDLE ImageHandle,
+ IN UDP_IO_CONFIG Configure,
+ IN VOID *Context
+ );
+
+EFI_STATUS
+UdpIoFreePort (
+ IN UDP_IO_PORT *UdpIo
+ );
+
+VOID
+UdpIoCleanPort (
+ IN UDP_IO_PORT *UdpIo
+ );
+
+EFI_STATUS
+UdpIoSendDatagram (
+ IN UDP_IO_PORT *UdpIo,
+ IN NET_BUF *Packet,
+ IN UDP_POINTS *EndPoint, OPTIONAL
+ IN IP4_ADDR Gateway,
+ IN UDP_IO_CALLBACK CallBack,
+ IN VOID *Context
+ );
+
+VOID
+UdpIoCancelSentDatagram (
+ IN UDP_IO_PORT *UdpIo,
+ IN NET_BUF *Packet
+ );
+
+EFI_STATUS
+UdpIoRecvDatagram (
+ IN UDP_IO_PORT *UdpIo,
+ IN UDP_IO_CALLBACK CallBack,
+ IN VOID *Context,
+ IN UINT32 HeadLen
+ );
+#endif
--- /dev/null
+/** @file\r
+\r
+Copyright (c) 2005 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+ IpIo.c\r
+\r
+Abstract:\r
+\r
+ The implementation of the IpIo layer.\r
+\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Protocol/Udp4.h>\r
+\r
+#include <Library/IpIoLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+\r
+#define NET_PROTO_HDR(Buf, Type) ((Type *) ((Buf)->BlockOp[0].Head))\r
+#define ICMP_ERRLEN(IpHdr) \\r
+ (sizeof(IP4_ICMP_HEAD) + EFI_IP4_HEADER_LEN(IpHdr) + 8)\r
+\r
+NET_LIST_ENTRY mActiveIpIoList = {\r
+ &mActiveIpIoList,\r
+ &mActiveIpIoList\r
+};\r
+\r
+EFI_IP4_CONFIG_DATA mIpIoDefaultIpConfigData = {\r
+ EFI_IP_PROTO_UDP,\r
+ FALSE,\r
+ TRUE,\r
+ FALSE,\r
+ FALSE,\r
+ FALSE,\r
+ {0, 0, 0, 0},\r
+ {0, 0, 0, 0},\r
+ 0,\r
+ 255,\r
+ FALSE,\r
+ FALSE,\r
+ 0,\r
+ 0\r
+};\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+IpIoTransmitHandler (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ );\r
+\r
+\r
+/**\r
+ This function create an ip child ,open the IP protocol, return the opened\r
+ Ip protocol to Interface.\r
+\r
+ @param ControllerHandle The controller handle.\r
+ @param ImageHandle The image handle.\r
+ @param ChildHandle Pointer to the buffer to save the ip child handle.\r
+ @param Interface Pointer used to get the ip protocol interface.\r
+\r
+ @retval EFI_SUCCESS The ip child is created and the ip protocol\r
+ interface is retrieved.\r
+ @retval other The required operation failed.\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+IpIoCreateIpChildOpenProtocol (\r
+ IN EFI_HANDLE ControllerHandle,\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_HANDLE *ChildHandle,\r
+ OUT VOID **Interface\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Create an ip child.\r
+ //\r
+ Status = NetLibCreateServiceChild (\r
+ ControllerHandle,\r
+ ImageHandle,\r
+ &gEfiIp4ServiceBindingProtocolGuid,\r
+ ChildHandle\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Open the ip protocol installed on the *ChildHandle.\r
+ //\r
+ Status = gBS->OpenProtocol (\r
+ *ChildHandle,\r
+ &gEfiIp4ProtocolGuid,\r
+ Interface,\r
+ ImageHandle,\r
+ ControllerHandle,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // On failure, destroy the ip child.\r
+ //\r
+ NetLibDestroyServiceChild (\r
+ ControllerHandle,\r
+ ImageHandle,\r
+ &gEfiIp4ServiceBindingProtocolGuid,\r
+ *ChildHandle\r
+ );\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ This function close the previously openned ip protocol and destroy the ip child.\r
+\r
+ @param ControllerHandle The controller handle.\r
+ @param ImageHandle the image handle.\r
+ @param ChildHandle The child handle of the ip child.\r
+\r
+ @retval EFI_SUCCESS The ip protocol is closed and the relevant ip child\r
+ is destroyed.\r
+ @retval other The required operation failed.\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+IpIoCloseProtocolDestroyIpChild (\r
+ IN EFI_HANDLE ControllerHandle,\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_HANDLE ChildHandle\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Close the previously openned ip protocol.\r
+ //\r
+ gBS->CloseProtocol (\r
+ ChildHandle,\r
+ &gEfiIp4ProtocolGuid,\r
+ ImageHandle,\r
+ ControllerHandle\r
+ );\r
+\r
+ //\r
+ // Destroy the ip child.\r
+ //\r
+ Status = NetLibDestroyServiceChild (\r
+ ControllerHandle,\r
+ ImageHandle,\r
+ &gEfiIp4ServiceBindingProtocolGuid,\r
+ ChildHandle\r
+ );\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Handle ICMP packets.\r
+\r
+ @param IpIo Pointer to the IP_IO instance.\r
+ @param Pkt Pointer to the ICMP packet.\r
+ @param Session Pointer to the net session of this ICMP packet.\r
+\r
+ @retval EFI_SUCCESS The ICMP packet is handled successfully.\r
+ @retval EFI_ABORTED This type of ICMP packet is not supported.\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+IpIoIcmpHandler (\r
+ IN IP_IO *IpIo,\r
+ IN NET_BUF *Pkt,\r
+ IN EFI_NET_SESSION_DATA *Session\r
+ )\r
+{\r
+ IP4_ICMP_ERROR_HEAD *IcmpHdr;\r
+ EFI_IP4_HEADER *IpHdr;\r
+ ICMP_ERROR IcmpErr;\r
+ UINT8 *PayLoadHdr;\r
+ UINT8 Type;\r
+ UINT8 Code;\r
+ UINT32 TrimBytes;\r
+\r
+ IcmpHdr = NET_PROTO_HDR (Pkt, IP4_ICMP_ERROR_HEAD);\r
+ IpHdr = (EFI_IP4_HEADER *) (&IcmpHdr->IpHead);\r
+\r
+ //\r
+ // Check the ICMP packet length.\r
+ //\r
+ if (Pkt->TotalSize < ICMP_ERRLEN (IpHdr)) {\r
+\r
+ return EFI_ABORTED;\r
+ }\r
+\r
+ Type = IcmpHdr->Head.Type;\r
+ Code = IcmpHdr->Head.Code;\r
+\r
+ //\r
+ // Analyze the ICMP Error in this ICMP pkt\r
+ //\r
+ switch (Type) {\r
+ case ICMP_TYPE_UNREACH:\r
+ switch (Code) {\r
+ case ICMP_CODE_UNREACH_NET:\r
+ case ICMP_CODE_UNREACH_HOST:\r
+ case ICMP_CODE_UNREACH_PROTOCOL:\r
+ case ICMP_CODE_UNREACH_PORT:\r
+ case ICMP_CODE_UNREACH_SRCFAIL:\r
+ IcmpErr = ICMP_ERR_UNREACH_NET + Code;\r
+\r
+ break;\r
+\r
+ case ICMP_CODE_UNREACH_NEEDFRAG:\r
+ IcmpErr = ICMP_ERR_MSGSIZE;\r
+\r
+ break;\r
+\r
+ case ICMP_CODE_UNREACH_NET_UNKNOWN:\r
+ case ICMP_CODE_UNREACH_NET_PROHIB:\r
+ case ICMP_CODE_UNREACH_TOSNET:\r
+ IcmpErr = ICMP_ERR_UNREACH_NET;\r
+\r
+ break;\r
+\r
+ case ICMP_CODE_UNREACH_HOST_UNKNOWN:\r
+ case ICMP_CODE_UNREACH_ISOLATED:\r
+ case ICMP_CODE_UNREACH_HOST_PROHIB:\r
+ case ICMP_CODE_UNREACH_TOSHOST:\r
+ IcmpErr = ICMP_ERR_UNREACH_HOST;\r
+\r
+ break;\r
+\r
+ default:\r
+ return EFI_ABORTED;\r
+\r
+ break;\r
+ }\r
+\r
+ break;\r
+\r
+ case ICMP_TYPE_TIMXCEED:\r
+ if (Code > 1) {\r
+ return EFI_ABORTED;\r
+ }\r
+\r
+ IcmpErr = Code + ICMP_ERR_TIMXCEED_INTRANS;\r
+\r
+ break;\r
+\r
+ case ICMP_TYPE_PARAMPROB:\r
+ if (Code > 1) {\r
+ return EFI_ABORTED;\r
+ }\r
+\r
+ IcmpErr = ICMP_ERR_PARAMPROB;\r
+\r
+ break;\r
+\r
+ case ICMP_TYPE_SOURCEQUENCH:\r
+ if (Code != 0) {\r
+ return EFI_ABORTED;\r
+ }\r
+\r
+ IcmpErr = ICMP_ERR_QUENCH;\r
+\r
+ break;\r
+\r
+ default:\r
+ return EFI_ABORTED;\r
+\r
+ break;\r
+ }\r
+\r
+ //\r
+ // Notify user the ICMP pkt only containing payload except\r
+ // IP and ICMP header\r
+ //\r
+ PayLoadHdr = (UINT8 *) ((UINT8 *) IpHdr + EFI_IP4_HEADER_LEN (IpHdr));\r
+ TrimBytes = (UINT32) (PayLoadHdr - (UINT8 *) IcmpHdr);\r
+\r
+ NetbufTrim (Pkt, TrimBytes, TRUE);\r
+\r
+ IpIo->PktRcvdNotify (EFI_ICMP_ERROR, IcmpErr, Session, Pkt, IpIo->RcvdContext);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Ext free function for net buffer. This function is\r
+ called when the net buffer is freed. It is used to\r
+ signal the recycle event to notify IP to recycle the\r
+ data buffer.\r
+\r
+ @param Event The event to be signaled.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+IpIoExtFree (\r
+ IN VOID *Event\r
+ )\r
+{\r
+ gBS->SignalEvent ((EFI_EVENT) Event);\r
+}\r
+\r
+\r
+/**\r
+ Create a send entry to wrap a packet before sending\r
+ out it through IP.\r
+\r
+ @param IpIo Pointer to the IP_IO instance.\r
+ @param Pkt Pointer to the packet.\r
+ @param Sender Pointer to the IP sender.\r
+ @param NotifyData Pointer to the notify data.\r
+ @param Dest Pointer to the destination IP address.\r
+ @param Override Pointer to the overriden IP_IO data.\r
+\r
+ @return Pointer to the data structure created to wrap the packet. If NULL,\r
+ @return resource limit occurred.\r
+\r
+**/\r
+STATIC\r
+IP_IO_SEND_ENTRY *\r
+IpIoCreateSndEntry (\r
+ IN IP_IO *IpIo,\r
+ IN NET_BUF *Pkt,\r
+ IN EFI_IP4_PROTOCOL *Sender,\r
+ IN VOID *Context OPTIONAL,\r
+ IN VOID *NotifyData OPTIONAL,\r
+ IN IP4_ADDR Dest,\r
+ IN IP_IO_OVERRIDE *Override\r
+ )\r
+{\r
+ IP_IO_SEND_ENTRY *SndEntry;\r
+ EFI_IP4_COMPLETION_TOKEN *SndToken;\r
+ EFI_IP4_TRANSMIT_DATA *TxData;\r
+ EFI_STATUS Status;\r
+ EFI_IP4_OVERRIDE_DATA *OverrideData;\r
+ UINT32 Index;\r
+\r
+ //\r
+ // Allocate resource for SndEntry\r
+ //\r
+ SndEntry = NetAllocatePool (sizeof (IP_IO_SEND_ENTRY));\r
+ if (NULL == SndEntry) {\r
+ return NULL;\r
+ }\r
+\r
+ //\r
+ // Allocate resource for SndToken\r
+ //\r
+ SndToken = NetAllocatePool (sizeof (EFI_IP4_COMPLETION_TOKEN));\r
+ if (NULL == SndToken) {\r
+ goto ReleaseSndEntry;\r
+ }\r
+\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ NET_TPL_EVENT,\r
+ IpIoTransmitHandler,\r
+ SndEntry,\r
+ &(SndToken->Event)\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto ReleaseSndToken;\r
+ }\r
+\r
+ //\r
+ // Allocate resource for TxData\r
+ //\r
+ TxData = NetAllocatePool (\r
+ sizeof (EFI_IP4_TRANSMIT_DATA) +\r
+ sizeof (EFI_IP4_FRAGMENT_DATA) * (Pkt->BlockOpNum - 1)\r
+ );\r
+\r
+ if (NULL == TxData) {\r
+ goto ReleaseEvent;\r
+ }\r
+\r
+ //\r
+ // Allocate resource for OverrideData if needed\r
+ //\r
+ OverrideData = NULL;\r
+ if (NULL != Override) {\r
+\r
+ OverrideData = NetAllocatePool (sizeof (EFI_IP4_OVERRIDE_DATA));\r
+ if (NULL == OverrideData) {\r
+ goto ReleaseResource;\r
+ }\r
+ //\r
+ // Set the fields of OverrideData\r
+ //\r
+ *OverrideData = * (EFI_IP4_OVERRIDE_DATA *) Override;\r
+ }\r
+\r
+ //\r
+ // Set the fields of TxData\r
+ //\r
+ EFI_IP4 (TxData->DestinationAddress) = Dest;\r
+ TxData->OverrideData = OverrideData;\r
+ TxData->OptionsLength = 0;\r
+ TxData->OptionsBuffer = NULL;\r
+ TxData->TotalDataLength = Pkt->TotalSize;\r
+ TxData->FragmentCount = Pkt->BlockOpNum;\r
+\r
+ for (Index = 0; Index < Pkt->BlockOpNum; Index++) {\r
+\r
+ TxData->FragmentTable[Index].FragmentBuffer = Pkt->BlockOp[Index].Head;\r
+ TxData->FragmentTable[Index].FragmentLength = Pkt->BlockOp[Index].Size;\r
+ }\r
+\r
+ //\r
+ // Set the fields of SndToken\r
+ //\r
+ SndToken->Packet.TxData = TxData;\r
+\r
+ //\r
+ // Set the fields of SndEntry\r
+ //\r
+ SndEntry->IpIo = IpIo;\r
+ SndEntry->Ip = Sender;\r
+ SndEntry->Context = Context;\r
+ SndEntry->NotifyData = NotifyData;\r
+\r
+ SndEntry->Pkt = Pkt;\r
+ NET_GET_REF (Pkt);\r
+\r
+ SndEntry->SndToken = SndToken;\r
+\r
+ NetListInsertTail (&IpIo->PendingSndList, &SndEntry->Entry);\r
+\r
+ return SndEntry;\r
+\r
+ReleaseResource:\r
+ NetFreePool (TxData);\r
+\r
+ReleaseEvent:\r
+ gBS->CloseEvent (SndToken->Event);\r
+\r
+ReleaseSndToken:\r
+ NetFreePool (SndToken);\r
+\r
+ReleaseSndEntry:\r
+ NetFreePool (SndEntry);\r
+\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Destroy the SndEntry.\r
+\r
+ @param SndEntry Pointer to the send entry to be destroyed.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+IpIoDestroySndEntry (\r
+ IN IP_IO_SEND_ENTRY *SndEntry\r
+ )\r
+{\r
+ EFI_IP4_TRANSMIT_DATA *TxData;\r
+\r
+ TxData = SndEntry->SndToken->Packet.TxData;\r
+\r
+ if (NULL != TxData->OverrideData) {\r
+ NetFreePool (TxData->OverrideData);\r
+ }\r
+\r
+ NetFreePool (TxData);\r
+ NetbufFree (SndEntry->Pkt);\r
+ gBS->CloseEvent (SndEntry->SndToken->Event);\r
+\r
+ NetFreePool (SndEntry->SndToken);\r
+ NetListRemoveEntry (&SndEntry->Entry);\r
+\r
+ NetFreePool (SndEntry);\r
+}\r
+\r
+\r
+/**\r
+ Notify function for IP transmit token.\r
+\r
+ @param Event The event signaled.\r
+ @param Context The context passed in by the event notifier.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+IpIoTransmitHandler (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ IP_IO *IpIo;\r
+ IP_IO_SEND_ENTRY *SndEntry;\r
+\r
+ SndEntry = (IP_IO_SEND_ENTRY *) Context;\r
+\r
+ IpIo = SndEntry->IpIo;\r
+\r
+ if (IpIo->PktSentNotify && SndEntry->NotifyData) {\r
+ IpIo->PktSentNotify (\r
+ SndEntry->SndToken->Status,\r
+ SndEntry->Context,\r
+ SndEntry->Ip,\r
+ SndEntry->NotifyData\r
+ );\r
+ }\r
+\r
+ IpIoDestroySndEntry (SndEntry);\r
+}\r
+\r
+\r
+/**\r
+ The dummy handler for the dummy IP receive token.\r
+\r
+ @param Evt The event signaled.\r
+ @param Context The context passed in by the event notifier.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+IpIoDummyHandler (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ IP_IO_IP_INFO *IpInfo;\r
+ EFI_IP4_COMPLETION_TOKEN *DummyToken;\r
+\r
+ ASSERT (Event && Context);\r
+\r
+ IpInfo = (IP_IO_IP_INFO *) Context;\r
+ DummyToken = &(IpInfo->DummyRcvToken);\r
+\r
+ if (EFI_SUCCESS == DummyToken->Status) {\r
+ ASSERT (DummyToken->Packet.RxData);\r
+\r
+ gBS->SignalEvent (DummyToken->Packet.RxData->RecycleSignal);\r
+ }\r
+\r
+ IpInfo->Ip->Receive (IpInfo->Ip, DummyToken);\r
+}\r
+\r
+\r
+/**\r
+ Notify function for the IP receive token, used to process\r
+ the received IP packets.\r
+\r
+ @param Event The event signaled.\r
+ @param Context The context passed in by the event notifier.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+IpIoListenHandler (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ IP_IO *IpIo;\r
+ EFI_STATUS Status;\r
+ EFI_IP4_RECEIVE_DATA *RxData;\r
+ EFI_IP4_PROTOCOL *Ip;\r
+ EFI_NET_SESSION_DATA Session;\r
+ NET_BUF *Pkt;\r
+\r
+ IpIo = (IP_IO *) Context;\r
+\r
+ Ip = IpIo->Ip;\r
+ Status = IpIo->RcvToken.Status;\r
+ RxData = IpIo->RcvToken.Packet.RxData;\r
+\r
+ if (((EFI_SUCCESS != Status) && (EFI_ICMP_ERROR != Status)) || (NULL == RxData)) {\r
+ //\r
+ // Only process the normal packets and the icmp error packets, if RxData is NULL\r
+ // with Status == EFI_SUCCESS or EFI_ICMP_ERROR, just resume the receive although\r
+ // this should be a bug of the low layer (IP).\r
+ //\r
+ goto Resume;\r
+ }\r
+\r
+ if (NULL == IpIo->PktRcvdNotify) {\r
+ goto CleanUp;\r
+ }\r
+\r
+ if ((EFI_IP4 (RxData->Header->SourceAddress) != 0) &&\r
+ !Ip4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), 0)) {\r
+ //\r
+ // The source address is not zero and it's not a unicast IP address, discard it.\r
+ //\r
+ goto CleanUp;\r
+ }\r
+\r
+ //\r
+ // Create a netbuffer representing packet\r
+ //\r
+ Pkt = NetbufFromExt (\r
+ (NET_FRAGMENT *) RxData->FragmentTable,\r
+ RxData->FragmentCount,\r
+ 0,\r
+ 0,\r
+ IpIoExtFree,\r
+ RxData->RecycleSignal\r
+ );\r
+ if (NULL == Pkt) {\r
+ goto CleanUp;\r
+ }\r
+\r
+ //\r
+ // Create a net session\r
+ //\r
+ Session.Source = EFI_IP4 (RxData->Header->SourceAddress);\r
+ Session.Dest = EFI_IP4 (RxData->Header->DestinationAddress);\r
+ Session.IpHdr = RxData->Header;\r
+\r
+ if (EFI_SUCCESS == Status) {\r
+\r
+ IpIo->PktRcvdNotify (EFI_SUCCESS, 0, &Session, Pkt, IpIo->RcvdContext);\r
+ } else {\r
+ //\r
+ // Status is EFI_ICMP_ERROR\r
+ //\r
+ Status = IpIoIcmpHandler (IpIo, Pkt, &Session);\r
+ if (EFI_ERROR (Status)) {\r
+ NetbufFree (Pkt);\r
+ }\r
+ }\r
+\r
+ goto Resume;\r
+\r
+CleanUp:\r
+ gBS->SignalEvent (RxData->RecycleSignal);\r
+\r
+Resume:\r
+ Ip->Receive (Ip, &(IpIo->RcvToken));\r
+}\r
+\r
+\r
+/**\r
+ Create a new IP_IO instance.\r
+\r
+ @param Image The image handle of an IP_IO consumer protocol.\r
+ @param Controller The controller handle of an IP_IO consumer protocol\r
+ installed on.\r
+\r
+ @return Pointer to a newly created IP_IO instance.\r
+\r
+**/\r
+IP_IO *\r
+IpIoCreate (\r
+ IN EFI_HANDLE Image,\r
+ IN EFI_HANDLE Controller\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ IP_IO *IpIo;\r
+\r
+ IpIo = NetAllocateZeroPool (sizeof (IP_IO));\r
+ if (NULL == IpIo) {\r
+ return NULL;\r
+ }\r
+\r
+ NetListInit (&(IpIo->PendingSndList));\r
+ NetListInit (&(IpIo->IpList));\r
+ IpIo->Controller = Controller;\r
+ IpIo->Image = Image;\r
+\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ NET_TPL_EVENT,\r
+ IpIoListenHandler,\r
+ IpIo,\r
+ &(IpIo->RcvToken.Event)\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto ReleaseIpIo;\r
+ }\r
+\r
+ //\r
+ // Create an IP child and open IP protocol\r
+ //\r
+ Status = IpIoCreateIpChildOpenProtocol (\r
+ Controller,\r
+ Image,\r
+ &IpIo->ChildHandle,\r
+ (VOID **)&(IpIo->Ip)\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto ReleaseIpIo;\r
+ }\r
+\r
+ return IpIo;\r
+\r
+ReleaseIpIo:\r
+\r
+ if (NULL != IpIo->RcvToken.Event) {\r
+ gBS->CloseEvent (IpIo->RcvToken.Event);\r
+ }\r
+\r
+ NetFreePool (IpIo);\r
+\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Open an IP_IO instance for use.\r
+\r
+ @param IpIo Pointer to an IP_IO instance that needs to open.\r
+ @param OpenData The configuration data for the IP_IO instance.\r
+\r
+ @retval EFI_SUCCESS The IP_IO instance opened with OpenData\r
+ successfully.\r
+ @retval other Error condition occurred.\r
+\r
+**/\r
+EFI_STATUS\r
+IpIoOpen (\r
+ IN IP_IO *IpIo,\r
+ IN IP_IO_OPEN_DATA *OpenData\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_IP4_PROTOCOL *Ip;\r
+ EFI_IPv4_ADDRESS ZeroIp;\r
+\r
+ if (IpIo->IsConfigured) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+\r
+ Ip = IpIo->Ip;\r
+\r
+ //\r
+ // configure ip\r
+ //\r
+ Status = Ip->Configure (Ip, &OpenData->IpConfigData);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // bugbug: to delete the default route entry in this Ip, if it is:\r
+ // (0.0.0.0, 0.0.0.0, 0.0.0.0). Delete this statement if Ip modified\r
+ // its code\r
+ //\r
+ EFI_IP4 (ZeroIp) = 0;\r
+ Status = Ip->Routes (Ip, TRUE, &ZeroIp, &ZeroIp, &ZeroIp);\r
+\r
+ if (EFI_ERROR (Status) && (EFI_NOT_FOUND != Status)) {\r
+ return Status;\r
+ }\r
+\r
+ IpIo->PktRcvdNotify = OpenData->PktRcvdNotify;\r
+ IpIo->PktSentNotify = OpenData->PktSentNotify;\r
+\r
+ IpIo->RcvdContext = OpenData->RcvdContext;\r
+ IpIo->SndContext = OpenData->SndContext;\r
+\r
+ IpIo->Protocol = OpenData->IpConfigData.DefaultProtocol;\r
+\r
+ //\r
+ // start to listen incoming packet\r
+ //\r
+ Status = Ip->Receive (Ip, &(IpIo->RcvToken));\r
+ if (EFI_ERROR (Status)) {\r
+ Ip->Configure (Ip, NULL);\r
+ goto ErrorExit;\r
+ }\r
+\r
+ IpIo->IsConfigured = TRUE;\r
+ NetListInsertTail (&mActiveIpIoList, &IpIo->Entry);\r
+\r
+ErrorExit:\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Stop an IP_IO instance.\r
+\r
+ @param IpIo Pointer to the IP_IO instance that needs to stop.\r
+\r
+ @retval EFI_SUCCESS The IP_IO instance stopped successfully.\r
+ @retval other Error condition occurred.\r
+\r
+**/\r
+EFI_STATUS\r
+IpIoStop (\r
+ IN IP_IO *IpIo\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_IP4_PROTOCOL *Ip;\r
+ IP_IO_IP_INFO *IpInfo;\r
+\r
+ if (!IpIo->IsConfigured) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // Remove the IpIo from the active IpIo list.\r
+ //\r
+ NetListRemoveEntry (&IpIo->Entry);\r
+\r
+ Ip = IpIo->Ip;\r
+\r
+ //\r
+ // Configure NULL Ip\r
+ //\r
+ Status = Ip->Configure (Ip, NULL);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ IpIo->IsConfigured = FALSE;\r
+\r
+ //\r
+ // Detroy the Ip List used by IpIo\r
+ //\r
+ while (!NetListIsEmpty (&(IpIo->IpList))) {\r
+ IpInfo = NET_LIST_HEAD (&(IpIo->IpList), IP_IO_IP_INFO, Entry);\r
+\r
+ IpIoRemoveIp (IpIo, IpInfo);\r
+ }\r
+\r
+ //\r
+ // All pending snd tokens should be flushed by reseting the IP instances.\r
+ //\r
+ ASSERT (NetListIsEmpty (&IpIo->PendingSndList));\r
+\r
+ //\r
+ // Close the receive event.\r
+ //\r
+ gBS->CloseEvent (IpIo->RcvToken.Event);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Destroy an IP_IO instance.\r
+\r
+ @param IpIo Pointer to the IP_IO instance that needs to\r
+ destroy.\r
+\r
+ @retval EFI_SUCCESS The IP_IO instance destroyed successfully.\r
+ @retval other Error condition occurred.\r
+\r
+**/\r
+EFI_STATUS\r
+IpIoDestroy (\r
+ IN IP_IO *IpIo\r
+ )\r
+{\r
+ //\r
+ // Stop the IpIo.\r
+ //\r
+ IpIoStop (IpIo);\r
+\r
+ //\r
+ // Close the IP protocol and destroy the child.\r
+ //\r
+ IpIoCloseProtocolDestroyIpChild (IpIo->Controller, IpIo->Image, IpIo->ChildHandle);\r
+\r
+ NetFreePool (IpIo);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Send out an IP packet.\r
+\r
+ @param IpIo Pointer to an IP_IO instance used for sending IP\r
+ packet.\r
+ @param Pkt Pointer to the IP packet to be sent.\r
+ @param Sender The IP protocol instance used for sending.\r
+ @param NotifyData\r
+ @param Dest The destination IP address to send this packet to.\r
+ @param OverrideData The data to override some configuration of the IP\r
+ instance used for sending.\r
+\r
+ @retval EFI_SUCCESS The operation is completed successfully.\r
+ @retval EFI_NOT_STARTED The IpIo is not configured.\r
+ @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.\r
+\r
+**/\r
+EFI_STATUS\r
+IpIoSend (\r
+ IN IP_IO *IpIo,\r
+ IN NET_BUF *Pkt,\r
+ IN IP_IO_IP_INFO *Sender,\r
+ IN VOID *Context OPTIONAL,\r
+ IN VOID *NotifyData OPTIONAL,\r
+ IN IP4_ADDR Dest,\r
+ IN IP_IO_OVERRIDE *OverrideData\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_IP4_PROTOCOL *Ip;\r
+ IP_IO_SEND_ENTRY *SndEntry;\r
+\r
+ if (!IpIo->IsConfigured) {\r
+ return EFI_NOT_STARTED;\r
+ }\r
+\r
+ Ip = (NULL == Sender) ? IpIo->Ip : Sender->Ip;\r
+\r
+ //\r
+ // create a new SndEntry\r
+ //\r
+ SndEntry = IpIoCreateSndEntry (IpIo, Pkt, Ip, Context, NotifyData, Dest, OverrideData);\r
+ if (NULL == SndEntry) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ //\r
+ // Send this Packet\r
+ //\r
+ Status = Ip->Transmit (Ip, SndEntry->SndToken);\r
+ if (EFI_ERROR (Status)) {\r
+ IpIoDestroySndEntry (SndEntry);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Cancel the IP transmit token which wraps this Packet.\r
+\r
+ @param IpIo Pointer to the IP_IO instance.\r
+ @param Packet Pointer to the packet to cancel.\r
+\r
+ @return N/A.\r
+\r
+**/\r
+VOID\r
+IpIoCancelTxToken (\r
+ IN IP_IO *IpIo,\r
+ IN VOID *Packet\r
+ )\r
+{\r
+ NET_LIST_ENTRY *Node;\r
+ IP_IO_SEND_ENTRY *SndEntry;\r
+ EFI_IP4_PROTOCOL *Ip;\r
+\r
+ ASSERT (IpIo && Packet);\r
+\r
+ NET_LIST_FOR_EACH (Node, &IpIo->PendingSndList) {\r
+\r
+ SndEntry = NET_LIST_USER_STRUCT (Node, IP_IO_SEND_ENTRY, Entry);\r
+\r
+ if (SndEntry->Pkt == Packet) {\r
+\r
+ Ip = SndEntry->Ip;\r
+ Ip->Cancel (Ip, SndEntry->SndToken);\r
+\r
+ //\r
+ // Abort the user token.\r
+ //\r
+ SndEntry->SndToken->Status = EFI_ABORTED;\r
+ IpIoTransmitHandler (NULL, SndEntry);\r
+\r
+ break;\r
+ }\r
+ }\r
+\r
+}\r
+\r
+\r
+/**\r
+ Add a new IP instance for sending data.\r
+\r
+ @param IpIo Pointer to a IP_IO instance to add a new IP\r
+ instance for sending purpose.\r
+\r
+ @return Pointer to the created IP_IO_IP_INFO structure, NULL is failed.\r
+\r
+**/\r
+IP_IO_IP_INFO *\r
+IpIoAddIp (\r
+ IN IP_IO *IpIo\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ IP_IO_IP_INFO *IpInfo;\r
+\r
+ ASSERT (IpIo);\r
+\r
+ IpInfo = NetAllocatePool (sizeof (IP_IO_IP_INFO));\r
+ if (IpInfo == NULL) {\r
+ return IpInfo;\r
+ }\r
+\r
+ //\r
+ // Init this IpInfo, set the Addr and SubnetMask to 0 before we configure the IP\r
+ // instance.\r
+ //\r
+ NetListInit (&IpInfo->Entry);\r
+ IpInfo->ChildHandle = NULL;\r
+ IpInfo->Addr = 0;\r
+ IpInfo->SubnetMask = 0;\r
+ IpInfo->RefCnt = 1;\r
+\r
+ //\r
+ // Create the IP instance and open the Ip4 protocol.\r
+ //\r
+ Status = IpIoCreateIpChildOpenProtocol (\r
+ IpIo->Controller,\r
+ IpIo->Image,\r
+ &IpInfo->ChildHandle,\r
+ &IpInfo->Ip\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto ReleaseIpInfo;\r
+ }\r
+\r
+ //\r
+ // Create the event for the DummyRcvToken.\r
+ //\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ NET_TPL_EVENT,\r
+ IpIoDummyHandler,\r
+ IpInfo,\r
+ &IpInfo->DummyRcvToken.Event\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto ReleaseIpChild;\r
+ }\r
+\r
+ //\r
+ // Link this IpInfo into the IpIo.\r
+ //\r
+ NetListInsertTail (&IpIo->IpList, &IpInfo->Entry);\r
+\r
+ return IpInfo;\r
+\r
+ReleaseIpChild:\r
+\r
+ IpIoCloseProtocolDestroyIpChild (\r
+ IpIo->Controller,\r
+ IpIo->Image,\r
+ IpInfo->ChildHandle\r
+ );\r
+\r
+ReleaseIpInfo:\r
+\r
+ NetFreePool (IpInfo);\r
+\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Configure the IP instance of this IpInfo and start the receiving if Ip4ConfigData\r
+ is not NULL.\r
+\r
+ @param IpInfo Pointer to the IP_IO_IP_INFO instance.\r
+ @param Ip4ConfigData The IP4 configure data used to configure the ip\r
+ instance, if NULL the ip instance is reseted. If\r
+ UseDefaultAddress is set to TRUE, and the configure\r
+ operation succeeds, the default address information\r
+ is written back in this Ip4ConfigData.\r
+\r
+ @retval EFI_STATUS The status returned by IP4->Configure or\r
+ IP4->Receive.\r
+\r
+**/\r
+EFI_STATUS\r
+IpIoConfigIp (\r
+ IN IP_IO_IP_INFO *IpInfo,\r
+ IN OUT EFI_IP4_CONFIG_DATA *Ip4ConfigData OPTIONAL\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_IP4_PROTOCOL *Ip;\r
+ EFI_IP4_MODE_DATA Ip4ModeData;\r
+\r
+ ASSERT (IpInfo);\r
+\r
+ if (IpInfo->RefCnt > 1) {\r
+ //\r
+ // This IP instance is shared, don't reconfigure it until it has only one\r
+ // consumer. Currently, only the tcp children cloned from their passive parent\r
+ // will share the same IP. So this cases only happens while Ip4ConfigData is NULL,\r
+ // let the last consumer clean the IP instance.\r
+ //\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ Ip = IpInfo->Ip;\r
+\r
+ Status = Ip->Configure (Ip, Ip4ConfigData);\r
+ if (EFI_ERROR (Status)) {\r
+ goto OnExit;\r
+ }\r
+\r
+ if (Ip4ConfigData != NULL) {\r
+\r
+ if (Ip4ConfigData->UseDefaultAddress) {\r
+ Ip->GetModeData (Ip, &Ip4ModeData, NULL, NULL);\r
+\r
+ Ip4ConfigData->StationAddress = Ip4ModeData.ConfigData.StationAddress;\r
+ Ip4ConfigData->SubnetMask = Ip4ModeData.ConfigData.SubnetMask;\r
+ }\r
+\r
+ IpInfo->Addr = EFI_IP4 (Ip4ConfigData->StationAddress);\r
+ IpInfo->SubnetMask = EFI_IP4 (Ip4ConfigData->SubnetMask);\r
+\r
+ Status = Ip->Receive (Ip, &IpInfo->DummyRcvToken);\r
+ if (EFI_ERROR (Status)) {\r
+ Ip->Configure (Ip, NULL);\r
+ }\r
+ } else {\r
+\r
+ //\r
+ // The IP instance is reseted, set the stored Addr and SubnetMask to zero.\r
+ //\r
+ IpInfo->Addr = 0;\r
+ IpInfo->SubnetMask =0;\r
+ }\r
+\r
+OnExit:\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Destroy an IP instance maintained in IpIo->IpList for\r
+ sending purpose.\r
+\r
+ @param IpIo Pointer to the IP_IO instance.\r
+ @param IpInfo Pointer to the IpInfo to be removed.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+IpIoRemoveIp (\r
+ IN IP_IO *IpIo,\r
+ IN IP_IO_IP_INFO *IpInfo\r
+ )\r
+{\r
+ ASSERT (IpInfo->RefCnt > 0);\r
+\r
+ NET_PUT_REF (IpInfo);\r
+\r
+ if (IpInfo->RefCnt > 0) {\r
+\r
+ return;\r
+ }\r
+\r
+ NetListRemoveEntry (&IpInfo->Entry);\r
+\r
+ IpInfo->Ip->Configure (IpInfo->Ip, NULL);\r
+\r
+ IpIoCloseProtocolDestroyIpChild (IpIo->Controller, IpIo->Image, IpInfo->ChildHandle);\r
+\r
+ gBS->CloseEvent (IpInfo->DummyRcvToken.Event);\r
+\r
+ NetFreePool (IpInfo);\r
+}\r
+\r
+\r
+/**\r
+ Find the first IP protocol maintained in IpIo whose local\r
+ address is the same with Src.\r
+\r
+ @param IpIo Pointer to the pointer of the IP_IO instance.\r
+ @param Src The local IP address.\r
+\r
+ @return Pointer to the IP protocol can be used for sending purpose and its local\r
+ @return address is the same with Src.\r
+\r
+**/\r
+IP_IO_IP_INFO *\r
+IpIoFindSender (\r
+ IN OUT IP_IO **IpIo,\r
+ IN IP4_ADDR Src\r
+ )\r
+{\r
+ NET_LIST_ENTRY *IpIoEntry;\r
+ IP_IO *IpIoPtr;\r
+ NET_LIST_ENTRY *IpInfoEntry;\r
+ IP_IO_IP_INFO *IpInfo;\r
+\r
+ NET_LIST_FOR_EACH (IpIoEntry, &mActiveIpIoList) {\r
+ IpIoPtr = NET_LIST_USER_STRUCT (IpIoEntry, IP_IO, Entry);\r
+\r
+ if ((*IpIo != NULL) && (*IpIo != IpIoPtr)) {\r
+ continue;\r
+ }\r
+\r
+ NET_LIST_FOR_EACH (IpInfoEntry, &IpIoPtr->IpList) {\r
+ IpInfo = NET_LIST_USER_STRUCT (IpInfoEntry, IP_IO_IP_INFO, Entry);\r
+\r
+ if (IpInfo->Addr == Src) {\r
+ *IpIo = IpIoPtr;\r
+ return IpInfo;\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // No match.\r
+ //\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Get the ICMP error map information, the ErrorStatus will be returned.\r
+ The IsHard and Notify are optional. If they are not NULL, this rouine will\r
+ fill them.\r
+ We move IcmpErrMap[] to local variable to enable EBC build.\r
+\r
+ @param IcmpError IcmpError Type\r
+ @param IsHard Whether it is a hard error\r
+ @param Notify Whether it need to notify SockError\r
+\r
+ @return ICMP Error Status\r
+\r
+**/\r
+EFI_STATUS\r
+IpIoGetIcmpErrStatus (\r
+ IN ICMP_ERROR IcmpError,\r
+ OUT BOOLEAN *IsHard, OPTIONAL\r
+ OUT BOOLEAN *Notify OPTIONAL\r
+ )\r
+{\r
+ ICMP_ERROR_INFO IcmpErrMap[] = {\r
+ { EFI_NETWORK_UNREACHABLE, FALSE, TRUE }, // ICMP_ERR_UNREACH_NET\r
+ { EFI_HOST_UNREACHABLE, FALSE, TRUE }, // ICMP_ERR_UNREACH_HOST\r
+ { EFI_PROTOCOL_UNREACHABLE, TRUE, TRUE }, // ICMP_ERR_UNREACH_PROTOCOL\r
+ { EFI_PORT_UNREACHABLE, TRUE, TRUE }, // ICMP_ERR_UNREACH_PORT\r
+ { EFI_ICMP_ERROR, TRUE, TRUE }, // ICMP_ERR_MSGSIZE\r
+ { EFI_ICMP_ERROR, FALSE, TRUE }, // ICMP_ERR_UNREACH_SRCFAIL\r
+ { EFI_HOST_UNREACHABLE, FALSE, TRUE }, // ICMP_ERR_TIMXCEED_INTRANS\r
+ { EFI_HOST_UNREACHABLE, FALSE, TRUE }, // ICMP_ERR_TIMEXCEED_REASS\r
+ { EFI_ICMP_ERROR, FALSE, FALSE }, // ICMP_ERR_QUENCH\r
+ { EFI_ICMP_ERROR, FALSE, TRUE } // ICMP_ERR_PARAMPROB\r
+ };\r
+\r
+ ASSERT ((IcmpError >= ICMP_ERR_UNREACH_NET) && (IcmpError <= ICMP_ERR_PARAMPROB));\r
+\r
+ if (IsHard != NULL) {\r
+ *IsHard = IcmpErrMap[IcmpError].IsHard;\r
+ }\r
+\r
+ if (Notify != NULL) {\r
+ *Notify = IcmpErrMap[IcmpError].Notify;\r
+ }\r
+\r
+ return IcmpErrMap[IcmpError].Error;\r
+}\r
+\r
--- /dev/null
+#/** @file\r
+# Component name for module NetLib\r
+#\r
+# FIX ME!\r
+# Copyright (c) 2006, Intel Corporation. All right reserved.\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+[Defines]\r
+ INF_VERSION = 0x00010005\r
+ BASE_NAME = NetIpIoDxe\r
+ FILE_GUID = A302F877-8625-425c-B1EC-7487B62C4FDA\r
+ MODULE_TYPE = DXE_DRIVER\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = IpIoLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
+ EDK_RELEASE_VERSION = 0x00020000\r
+ EFI_SPECIFICATION_VERSION = 0x00020000\r
+\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+# VALID_ARCHITECTURES = IA32 X64 IPF EBC\r
+#\r
+\r
+[Sources.common]\r
+ DxeIpIoLib.c\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
+ MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+[LibraryClasses]\r
+ MemoryAllocationLib\r
+ UefiLib\r
+ BaseLib\r
+ UefiBootServicesTableLib\r
+ UefiRuntimeServicesTableLib\r
+ BaseMemoryLib\r
+ DebugLib\r
+ PrintLib\r
+\r
+\r
+[Protocols]\r
+ gEfiIp4ProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverDiagnosticsProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiUdp4ServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiLoadedImageProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiIp4ServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverConfigurationProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiSimpleNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiUdp4ProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiComponentNameProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+\r
--- /dev/null
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. 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
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ NetLib.c
+
+Abstract:
+
+
+
+**/
+
+#include <PiDxe.h>
+
+#include <Protocol/ServiceBinding.h>
+#include <Protocol/SimpleNetwork.h>
+#include <Protocol/LoadedImage.h>
+
+#include <Library/NetLib.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+#include <Library/UefiLib.h>\r
+#include <Library/MemoryAllocationLib.h>
+
+
+//
+// All the supported IP4 maskes in host byte order.
+//
+IP4_ADDR mIp4AllMasks[IP4_MASK_NUM] = {
+ 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,
+};
+
+
+/**\r
+ Converts the low nibble of a byte to hex unicode character.\r
+\r
+ @param Nibble lower nibble of a byte.\r
+\r
+ @return Hex unicode character.\r
+\r
+**/\r
+CHAR16\r
+NibbleToHexChar (\r
+ IN UINT8 Nibble\r
+ )\r
+{\r
+ //\r
+ // Porting Guide:\r
+ // This library interface is simply obsolete.\r
+ // Include the source code to user code.\r
+ //\r
+\r
+ Nibble &= 0x0F;\r
+ if (Nibble <= 0x9) {\r
+ return (CHAR16)(Nibble + L'0');\r
+ }\r
+\r
+ return (CHAR16)(Nibble - 0xA + L'A');\r
+}\r
+\r
+/**
+ Return the length of the mask. If the mask is invalid,
+ return the invalid length 33, which is IP4_MASK_NUM.
+ NetMask is in the host byte order.
+
+ @param NetMask The netmask to get the length from
+
+ @return The length of the netmask, IP4_MASK_NUM if the mask isn't
+ @return supported.
+
+**/
+INTN
+NetGetMaskLength (
+ IN IP4_ADDR NetMask
+ )
+{
+ INTN Index;
+
+ for (Index = 0; Index < IP4_MASK_NUM; Index++) {
+ if (NetMask == mIp4AllMasks[Index]) {
+ break;
+ }
+ }
+
+ return Index;
+}
+
+
+
+/**
+ Return the class of the address, such as class a, b, c.
+ Addr is in host byte order.
+
+ @param Addr The address to get the class from
+
+ @return IP address class, such as IP4_ADDR_CLASSA
+
+**/
+INTN
+NetGetIpClass (
+ IN IP4_ADDR Addr
+ )
+{
+ UINT8 ByteOne;
+
+ ByteOne = (UINT8) (Addr >> 24);
+
+ if ((ByteOne & 0x80) == 0) {
+ return IP4_ADDR_CLASSA;
+
+ } else if ((ByteOne & 0xC0) == 0x80) {
+ return IP4_ADDR_CLASSB;
+
+ } else if ((ByteOne & 0xE0) == 0xC0) {
+ return IP4_ADDR_CLASSC;
+
+ } else if ((ByteOne & 0xF0) == 0xE0) {
+ 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.
+
+ @param Ip The IP to check againist
+ @param NetMask The mask of the IP
+
+ @return TRUE if IP is a valid unicast address on the network, otherwise FALSE
+
+**/
+BOOLEAN
+Ip4IsUnicast (
+ IN IP4_ADDR Ip,
+ IN IP4_ADDR NetMask
+ )
+{
+ INTN Class;
+
+ Class = NetGetIpClass (Ip);
+
+ if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) {
+ return FALSE;
+ }
+
+ if (NetMask == 0) {
+ NetMask = mIp4AllMasks[Class << 3];
+ }
+
+ if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+/**
+ Initialize a random seed using current time.
+
+ None
+
+ @return The random seed initialized with current time.
+
+**/
+UINT32
+NetRandomInitSeed (
+ VOID
+ )
+{
+ EFI_TIME Time;
+ UINT32 Seed;
+
+ gRT->GetTime (&Time, NULL);
+ Seed = (~Time.Hour << 24 | Time.Second << 16 | Time.Minute << 8 | Time.Day);
+ Seed ^= Time.Nanosecond;
+ Seed ^= Time.Year << 7;
+
+ return Seed;
+}
+
+
+/**
+ Extract a UINT32 from a byte stream, then convert it to host
+ byte order. Use this function to avoid alignment error.
+
+ @param Buf The buffer to extract the UINT32.
+
+ @return The UINT32 extracted.
+
+**/
+UINT32
+NetGetUint32 (
+ IN UINT8 *Buf
+ )
+{
+ UINT32 Value;
+
+ NetCopyMem (&Value, Buf, sizeof (UINT32));
+ return NTOHL (Value);
+}
+
+
+/**
+ Put a UINT32 to the byte stream. Convert it from host byte order
+ to network byte order before putting.
+
+ @param Buf The buffer to put the UINT32
+ @param Data The data to put
+
+ @return None
+
+**/
+VOID
+NetPutUint32 (
+ IN UINT8 *Buf,
+ IN UINT32 Data
+ )
+{
+ Data = HTONL (Data);
+ NetCopyMem (Buf, &Data, sizeof (UINT32));
+}
+
+
+/**
+ Remove the first entry on the list
+
+ @param Head The list header
+
+ @return The entry that is removed from the list, NULL if the list is empty.
+
+**/
+NET_LIST_ENTRY *
+NetListRemoveHead (
+ NET_LIST_ENTRY *Head
+ )
+{
+ NET_LIST_ENTRY *First;
+
+ ASSERT (Head != NULL);
+
+ if (NetListIsEmpty (Head)) {
+ return NULL;
+ }
+
+ First = Head->ForwardLink;
+ Head->ForwardLink = First->ForwardLink;
+ First->ForwardLink->BackLink = Head;
+
+ DEBUG_CODE (
+ First->ForwardLink = (LIST_ENTRY *) NULL;
+ First->BackLink = (LIST_ENTRY *) NULL;
+ );
+
+ return First;
+}
+
+
+/**
+ Remove the last entry on the list
+
+ @param Head The list head
+
+ @return The entry that is removed from the list, NULL if the list is empty.
+
+**/
+NET_LIST_ENTRY *
+NetListRemoveTail (
+ NET_LIST_ENTRY *Head
+ )
+{
+ NET_LIST_ENTRY *Last;
+
+ ASSERT (Head != NULL);
+
+ if (NetListIsEmpty (Head)) {
+ return NULL;
+ }
+
+ Last = Head->BackLink;
+ Head->BackLink = Last->BackLink;
+ Last->BackLink->ForwardLink = Head;
+
+ DEBUG_CODE (
+ Last->ForwardLink = (LIST_ENTRY *) NULL;
+ Last->BackLink = (LIST_ENTRY *) NULL;
+ );
+
+ return Last;
+}
+
+
+/**
+ Insert the NewEntry after the PrevEntry
+
+ @param PrevEntry The previous entry to insert after
+ @param NewEntry The new entry to insert
+
+ @return None
+
+**/
+VOID
+NetListInsertAfter (
+ IN NET_LIST_ENTRY *PrevEntry,
+ IN NET_LIST_ENTRY *NewEntry
+ )
+{
+ NewEntry->BackLink = PrevEntry;
+ NewEntry->ForwardLink = PrevEntry->ForwardLink;
+ PrevEntry->ForwardLink->BackLink = NewEntry;
+ PrevEntry->ForwardLink = NewEntry;
+}
+
+
+/**
+ Insert the NewEntry before the PostEntry
+
+ @param PostEntry The entry to insert before
+ @param NewEntry The new entry to insert
+
+ @return None
+
+**/
+VOID
+NetListInsertBefore (
+ IN NET_LIST_ENTRY *PostEntry,
+ IN NET_LIST_ENTRY *NewEntry
+ )
+{
+ NewEntry->ForwardLink = PostEntry;
+ NewEntry->BackLink = PostEntry->BackLink;
+ PostEntry->BackLink->ForwardLink = NewEntry;
+ PostEntry->BackLink = NewEntry;
+}
+
+
+/**
+ Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
+
+ @param Map The netmap to initialize
+
+ @return None
+
+**/
+VOID
+NetMapInit (
+ IN NET_MAP *Map
+ )
+{
+ ASSERT (Map != NULL);
+
+ NetListInit (&Map->Used);
+ NetListInit (&Map->Recycled);
+ Map->Count = 0;
+}
+
+
+/**
+ To clean up the netmap, that is, release allocated memories.
+
+ @param Map The netmap to clean up.
+
+ @return None
+
+**/
+VOID
+NetMapClean (
+ IN NET_MAP *Map
+ )
+{
+ NET_MAP_ITEM *Item;
+ NET_LIST_ENTRY *Entry;
+ NET_LIST_ENTRY *Next;
+
+ ASSERT (Map != NULL);
+
+ NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {
+ Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
+
+ NetListRemoveEntry (&Item->Link);
+ Map->Count--;
+
+ NetFreePool (Item);
+ }
+
+ ASSERT ((Map->Count == 0) && NetListIsEmpty (&Map->Used));
+
+ NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {
+ Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
+
+ NetListRemoveEntry (&Item->Link);
+ NetFreePool (Item);
+ }
+
+ ASSERT (NetListIsEmpty (&Map->Recycled));
+}
+
+
+/**
+ Test whether the netmap is empty
+
+ @param Map The net map to test
+
+ @return TRUE if the netmap is empty, otherwise FALSE.
+
+**/
+BOOLEAN
+NetMapIsEmpty (
+ IN NET_MAP *Map
+ )
+{
+ ASSERT (Map != NULL);
+ return (BOOLEAN) (Map->Count == 0);
+}
+
+
+/**
+ Return the number of the <Key, Value> pairs in the netmap.
+
+ @param Map The netmap to get the entry number
+
+ @return The entry number in the netmap.
+
+**/
+UINTN
+NetMapGetCount (
+ IN NET_MAP *Map
+ )
+{
+ return Map->Count;
+}
+
+
+/**
+ Allocate an item for the netmap. It will try to allocate
+ a batch of items and return one.
+
+ @param Map The netmap to allocate item for
+
+ @return The allocated item or NULL
+
+**/
+STATIC
+NET_MAP_ITEM *
+NetMapAllocItem (
+ IN NET_MAP *Map
+ )
+{
+ NET_MAP_ITEM *Item;
+ NET_LIST_ENTRY *Head;
+ UINTN Index;
+
+ ASSERT (Map != NULL);
+
+ Head = &Map->Recycled;
+
+ if (NetListIsEmpty (Head)) {
+ for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {
+ Item = NetAllocatePool (sizeof (NET_MAP_ITEM));
+
+ if (Item == NULL) {
+ if (Index == 0) {
+ return NULL;
+ }
+
+ break;
+ }
+
+ NetListInsertHead (Head, &Item->Link);
+ }
+ }
+
+ Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);
+ NetListRemoveHead (Head);
+
+ return Item;
+}
+
+
+/**
+ Allocate an item to save the <Key, Value> pair to the head of the netmap.
+
+ @param Map The netmap to insert into
+ @param Key The user's key
+ @param 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
+
+**/
+EFI_STATUS
+NetMapInsertHead (
+ IN NET_MAP *Map,
+ IN VOID *Key,
+ IN VOID *Value OPTIONAL
+ )
+{
+ NET_MAP_ITEM *Item;
+
+ ASSERT (Map != NULL);
+
+ Item = NetMapAllocItem (Map);
+
+ if (Item == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ Item->Key = Key;
+ Item->Value = Value;
+ NetListInsertHead (&Map->Used, &Item->Link);
+
+ Map->Count++;
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Allocate an item to save the <Key, Value> pair to the tail of the netmap.
+
+ @param Map The netmap to insert into
+ @param Key The user's key
+ @param 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
+
+**/
+EFI_STATUS
+NetMapInsertTail (
+ IN NET_MAP *Map,
+ IN VOID *Key,
+ IN VOID *Value OPTIONAL
+ )
+{
+ NET_MAP_ITEM *Item;
+
+ ASSERT (Map != NULL);
+
+ Item = NetMapAllocItem (Map);
+
+ if (Item == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ Item->Key = Key;
+ Item->Value = Value;
+ NetListInsertTail (&Map->Used, &Item->Link);
+
+ Map->Count++;
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Check whther the item is in the Map
+
+ @param Map The netmap to search within
+ @param Item The item to search
+
+ @return TRUE if the item is in the netmap, otherwise FALSE.
+
+**/
+STATIC
+BOOLEAN
+NetItemInMap (
+ IN NET_MAP *Map,
+ IN NET_MAP_ITEM *Item
+ )
+{
+ NET_LIST_ENTRY *ListEntry;
+
+ NET_LIST_FOR_EACH (ListEntry, &Map->Used) {
+ if (ListEntry == &Item->Link) {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+
+/**
+ Find the key in the netmap
+
+ @param Map The netmap to search within
+ @param Key The key to search
+
+ @return The point to the item contains the Key, or NULL if Key isn't in the map.
+
+**/
+NET_MAP_ITEM *
+NetMapFindKey (
+ IN NET_MAP *Map,
+ IN VOID *Key
+ )
+{
+ NET_LIST_ENTRY *Entry;
+ NET_MAP_ITEM *Item;
+
+ ASSERT (Map != NULL);
+
+ NET_LIST_FOR_EACH (Entry, &Map->Used) {
+ Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
+
+ if (Item->Key == Key) {
+ return Item;
+ }
+ }
+
+ return NULL;
+}
+
+
+/**
+ Remove the item from the netmap
+
+ @param Map The netmap to remove the item from
+ @param Item The item to remove
+ @param Value The variable to receive the value if not NULL
+
+ @return The key of the removed item.
+
+**/
+VOID *
+NetMapRemoveItem (
+ IN NET_MAP *Map,
+ IN NET_MAP_ITEM *Item,
+ OUT VOID **Value OPTIONAL
+ )
+{
+ ASSERT ((Map != NULL) && (Item != NULL));
+ ASSERT (NetItemInMap (Map, Item));
+
+ NetListRemoveEntry (&Item->Link);
+ Map->Count--;
+ NetListInsertHead (&Map->Recycled, &Item->Link);
+
+ if (Value != NULL) {
+ *Value = Item->Value;
+ }
+
+ return Item->Key;
+}
+
+
+/**
+ Remove the first entry on the netmap
+
+ @param Map The netmap to remove the head from
+ @param Value The variable to receive the value if not NULL
+
+ @return The key of the item removed
+
+**/
+VOID *
+NetMapRemoveHead (
+ IN NET_MAP *Map,
+ OUT VOID **Value OPTIONAL
+ )
+{
+ NET_MAP_ITEM *Item;
+
+ //
+ // Often, it indicates a programming error to remove
+ // the first entry in an empty list
+ //
+ ASSERT (Map && !NetListIsEmpty (&Map->Used));
+
+ Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);
+ NetListRemoveEntry (&Item->Link);
+ Map->Count--;
+ NetListInsertHead (&Map->Recycled, &Item->Link);
+
+ if (Value != NULL) {
+ *Value = Item->Value;
+ }
+
+ return Item->Key;
+}
+
+
+/**
+ Remove the last entry on the netmap
+
+ @param Map The netmap to remove the tail from
+ @param Value The variable to receive the value if not NULL
+
+ @return The key of the item removed
+
+**/
+VOID *
+NetMapRemoveTail (
+ IN NET_MAP *Map,
+ OUT VOID **Value OPTIONAL
+ )
+{
+ NET_MAP_ITEM *Item;
+
+ //
+ // Often, it indicates a programming error to remove
+ // the last entry in an empty list
+ //
+ ASSERT (Map && !NetListIsEmpty (&Map->Used));
+
+ Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);
+ NetListRemoveEntry (&Item->Link);
+ Map->Count--;
+ NetListInsertHead (&Map->Recycled, &Item->Link);
+
+ if (Value != NULL) {
+ *Value = Item->Value;
+ }
+
+ return Item->Key;
+}
+
+
+/**
+ 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.
+
+ @param Map The Map to iterate through
+ @param CallBack The callback function to call for each item.
+ @param Arg The opaque parameter to the callback
+
+ @return It returns the CallBack's last return value.
+
+**/
+EFI_STATUS
+NetMapIterate (
+ IN NET_MAP *Map,
+ IN NET_MAP_CALLBACK CallBack,
+ IN VOID *Arg
+ )
+{
+
+ NET_LIST_ENTRY *Entry;
+ NET_LIST_ENTRY *Next;
+ NET_LIST_ENTRY *Head;
+ NET_MAP_ITEM *Item;
+ EFI_STATUS Result;
+
+ ASSERT ((Map != NULL) && (CallBack != NULL));
+
+ Head = &Map->Used;
+
+ if (NetListIsEmpty (Head)) {
+ return EFI_SUCCESS;
+ }
+
+ NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
+ Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
+ Result = CallBack (Map, Item, Arg);
+
+ if (EFI_ERROR (Result)) {
+ return Result;
+ }
+ }
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ This is the default unload handle for all the network drivers.
+
+ @param ImageHandle The drivers' driver image.
+
+ @retval EFI_SUCCESS The image is unloaded.
+ @retval Others Failed to unload the image.
+
+**/
+EFI_STATUS
+EFIAPI
+NetLibDefaultUnload (
+ IN EFI_HANDLE ImageHandle
+ )
+{
+ EFI_STATUS Status;
+ EFI_HANDLE *DeviceHandleBuffer;
+ UINTN DeviceHandleCount;
+ UINTN Index;
+ EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
+#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
+ EFI_COMPONENT_NAME2_PROTOCOL *ComponentName;
+#else
+ EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
+#endif
+ EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration;
+ EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics;
+
+ //
+ // Get the list of all the handles in the handle database.
+ // If there is an error getting the list, then the unload
+ // operation fails.
+ //
+ Status = gBS->LocateHandleBuffer (
+ AllHandles,
+ NULL,
+ NULL,
+ &DeviceHandleCount,
+ &DeviceHandleBuffer
+ );
+
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ //
+ // Disconnect the driver specified by ImageHandle from all
+ // the devices in the handle database.
+ //
+ for (Index = 0; Index < DeviceHandleCount; Index++) {
+ Status = gBS->DisconnectController (
+ DeviceHandleBuffer[Index],
+ ImageHandle,
+ NULL
+ );
+ }
+
+ //
+ // Uninstall all the protocols installed in the driver entry point
+ //
+ for (Index = 0; Index < DeviceHandleCount; Index++) {
+ Status = gBS->HandleProtocol (
+ DeviceHandleBuffer[Index],
+ &gEfiDriverBindingProtocolGuid,
+ &DriverBinding
+ );
+
+ if (EFI_ERROR (Status)) {
+ continue;
+ }
+
+ if (DriverBinding->ImageHandle != ImageHandle) {
+ continue;
+ }
+
+ gBS->UninstallProtocolInterface (
+ ImageHandle,
+ &gEfiDriverBindingProtocolGuid,
+ DriverBinding
+ );
+#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
+ Status = gBS->HandleProtocol (
+ DeviceHandleBuffer[Index],
+ &gEfiComponentName2ProtocolGuid,
+ &ComponentName
+ );
+ if (!EFI_ERROR (Status)) {
+ gBS->UninstallProtocolInterface (
+ ImageHandle,
+ &gEfiComponentName2ProtocolGuid,
+ ComponentName
+ );
+ }
+#else
+ Status = gBS->HandleProtocol (
+ DeviceHandleBuffer[Index],
+ &gEfiComponentNameProtocolGuid,
+ &ComponentName
+ );
+ if (!EFI_ERROR (Status)) {
+ gBS->UninstallProtocolInterface (
+ ImageHandle,
+ &gEfiComponentNameProtocolGuid,
+ ComponentName
+ );
+ }
+#endif
+
+ Status = gBS->HandleProtocol (
+ DeviceHandleBuffer[Index],
+ &gEfiDriverConfigurationProtocolGuid,
+ &DriverConfiguration
+ );
+
+ if (!EFI_ERROR (Status)) {
+ gBS->UninstallProtocolInterface (
+ ImageHandle,
+ &gEfiDriverConfigurationProtocolGuid,
+ DriverConfiguration
+ );
+ }
+
+ Status = gBS->HandleProtocol (
+ DeviceHandleBuffer[Index],
+ &gEfiDriverDiagnosticsProtocolGuid,
+ &DriverDiagnostics
+ );
+
+ if (!EFI_ERROR (Status)) {
+ gBS->UninstallProtocolInterface (
+ ImageHandle,
+ &gEfiDriverDiagnosticsProtocolGuid,
+ DriverDiagnostics
+ );
+ }
+ }
+
+ //
+ // Free the buffer containing the list of handles from the handle database
+ //
+ if (DeviceHandleBuffer != NULL) {
+ gBS->FreePool (DeviceHandleBuffer);
+ }
+
+ return EFI_SUCCESS;
+}
+
+
+
+/**
+ Create a child of the service that is identified by ServiceBindingGuid.
+
+ @param Controller The controller which has the service installed.
+ @param Image The image handle used to open service.
+ @param ServiceBindingGuid The service's Guid.
+ @param ChildHandle The handle to receive the create child
+
+ @retval EFI_SUCCESS The child is successfully created.
+ @retval Others Failed to create the child.
+
+**/
+EFI_STATUS
+NetLibCreateServiceChild (
+ IN EFI_HANDLE Controller,
+ IN EFI_HANDLE Image,
+ IN EFI_GUID *ServiceBindingGuid,
+ OUT EFI_HANDLE *ChildHandle
+ )
+{
+ EFI_STATUS Status;
+ EFI_SERVICE_BINDING_PROTOCOL *Service;
+
+
+ ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));
+
+ //
+ // Get the ServiceBinding Protocol
+ //
+ Status = gBS->OpenProtocol (
+ Controller,
+ ServiceBindingGuid,
+ (VOID **) &Service,
+ Image,
+ Controller,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ //
+ // Create a child
+ //
+ Status = Service->CreateChild (Service, ChildHandle);
+ return Status;
+}
+
+
+/**
+ Destory a child of the service that is identified by ServiceBindingGuid.
+
+ @param Controller The controller which has the service installed.
+ @param Image The image handle used to open service.
+ @param ServiceBindingGuid The service's Guid.
+ @param ChildHandle The child to destory
+
+ @retval EFI_SUCCESS The child is successfully destoried.
+ @retval Others Failed to destory the child.
+
+**/
+EFI_STATUS
+NetLibDestroyServiceChild (
+ IN EFI_HANDLE Controller,
+ IN EFI_HANDLE Image,
+ IN EFI_GUID *ServiceBindingGuid,
+ IN EFI_HANDLE ChildHandle
+ )
+{
+ EFI_STATUS Status;
+ EFI_SERVICE_BINDING_PROTOCOL *Service;
+
+ ASSERT (ServiceBindingGuid != NULL);
+
+ //
+ // Get the ServiceBinding Protocol
+ //
+ Status = gBS->OpenProtocol (
+ Controller,
+ ServiceBindingGuid,
+ (VOID **) &Service,
+ Image,
+ Controller,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ //
+ // destory the child
+ //
+ Status = Service->DestroyChild (Service, ChildHandle);
+ return Status;
+}
+
+
+/**
+ Convert the mac address of the simple network protocol installed on
+ SnpHandle to a unicode string. Callers are responsible for freeing the
+ string storage.
+
+ @param SnpHandle The handle where the simple network protocol is
+ installed on.
+ @param ImageHandle The image handle used to act as the agent handle to
+ get the simple network protocol.
+ @param MacString The pointer to store the address of the string
+ representation of the mac address.
+
+ @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
+ @retval other Failed to open the simple network protocol.
+
+**/
+EFI_STATUS
+NetLibGetMacString (
+ IN EFI_HANDLE SnpHandle,
+ IN EFI_HANDLE ImageHandle,
+ IN OUT CONST CHAR16 **MacString
+ )
+{
+ EFI_STATUS Status;
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
+ EFI_SIMPLE_NETWORK_MODE *Mode;
+ CHAR16 *MacAddress;
+ UINTN Index;
+
+ *MacString = NULL;
+
+ //
+ // Get the Simple Network protocol from the SnpHandle.
+ //
+ Status = gBS->OpenProtocol (
+ SnpHandle,
+ &gEfiSimpleNetworkProtocolGuid,
+ (VOID **) &Snp,
+ ImageHandle,
+ SnpHandle,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ Mode = Snp->Mode;
+
+ //
+ // It takes 2 unicode characters to represent a 1 byte binary buffer.
+ // Plus one unicode character for the null-terminator.
+ //
+ MacAddress = NetAllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));
+ if (MacAddress == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ //
+ // Convert the mac address into a unicode string.
+ //
+ for (Index = 0; Index < Mode->HwAddressSize; Index++) {
+ MacAddress[Index * 2] = NibbleToHexChar (Mode->CurrentAddress.Addr[Index] >> 4);
+ MacAddress[Index * 2 + 1] = NibbleToHexChar (Mode->CurrentAddress.Addr[Index]);
+ }
+
+ MacAddress[Mode->HwAddressSize * 2] = L'\0';
+
+ *MacString = MacAddress;
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Find the UNDI/SNP handle from controller and protocol GUID.
+ For example, IP will open a MNP child to transmit/receive
+ packets, when MNP is stopped, IP should also be stopped. IP
+ needs to find its own private data which is related the IP's
+ service binding instance that is install on UNDI/SNP handle.
+ 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
+ UNDI/SNP handle.
+
+ @param Controller Then protocol handle to check
+ @param ProtocolGuid The protocol that is related with the handle.
+
+ @return The UNDI/SNP handle or NULL.
+
+**/
+EFI_HANDLE
+NetLibGetNicHandle (
+ IN EFI_HANDLE Controller,
+ IN EFI_GUID *ProtocolGuid
+ )
+{
+ EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;
+ EFI_HANDLE Handle;
+ EFI_STATUS Status;
+ UINTN OpenCount;
+ UINTN Index;
+
+ Status = gBS->OpenProtocolInformation (
+ Controller,
+ ProtocolGuid,
+ &OpenBuffer,
+ &OpenCount
+ );
+
+ if (EFI_ERROR (Status)) {
+ return NULL;
+ }
+
+ Handle = NULL;
+
+ for (Index = 0; Index < OpenCount; Index++) {
+ if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
+ Handle = OpenBuffer[Index].ControllerHandle;
+ break;
+ }
+ }
+
+ gBS->FreePool (OpenBuffer);
+ return Handle;
+}
+
+EFI_STATUS
+NetLibInstallAllDriverProtocols (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable,
+ IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding,
+ IN EFI_HANDLE DriverBindingHandle,
+ IN EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL
+ IN EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL
+ IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics OPTIONAL
+ )
+/*++
+
+Routine Description:
+
+ Intialize a driver by installing the Driver Binding Protocol onto the
+ driver's DriverBindingHandle. This is typically the same as the driver's
+ ImageHandle, but it can be different if the driver produces multiple
+ DriverBinding Protocols. This function also initializes the EFI Driver
+ Library that initializes the global variables gST, gBS, gRT.
+
+Arguments:
+
+ ImageHandle - The image handle of the driver
+ SystemTable - The EFI System Table that was passed to the driver's
+ entry point
+ DriverBinding - A Driver Binding Protocol instance that this driver
+ is producing.
+ DriverBindingHandle - The handle that DriverBinding is to be installe onto.
+ If this parameter is NULL, then a new handle is created.
+ ComponentName - A Component Name Protocol instance that this driver is
+ producing.
+ DriverConfiguration - A Driver Configuration Protocol instance that this
+ driver is producing.
+ DriverDiagnostics - A Driver Diagnostics Protocol instance that this
+ driver is producing.
+
+Returns:
+
+ EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
+ Otherwise, then return status from gBS->InstallProtocolInterface()
+
+--*/
+{
+ return NetLibInstallAllDriverProtocolsWithUnload (
+ ImageHandle,
+ SystemTable,
+ DriverBinding,
+ DriverBindingHandle,
+ ComponentName,
+ DriverConfiguration,
+ DriverDiagnostics,
+ NetLibDefaultUnload
+ );
+}
+
+EFI_STATUS
+NetLibInstallAllDriverProtocolsWithUnload (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable,
+ IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding,
+ IN EFI_HANDLE DriverBindingHandle,
+ IN EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL
+ IN EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL
+ IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics, OPTIONAL
+ IN NET_LIB_DRIVER_UNLOAD Unload
+ )
+/*++
+
+Routine Description:
+
+ Intialize a driver by installing the Driver Binding Protocol onto the
+ driver's DriverBindingHandle. This is typically the same as the driver's
+ ImageHandle, but it can be different if the driver produces multiple
+ DriverBinding Protocols. This function also initializes the EFI Driver
+ Library that initializes the global variables gST, gBS, gRT.
+
+Arguments:
+
+ ImageHandle - The image handle of the driver
+ SystemTable - The EFI System Table that was passed to the driver's
+ entry point
+ DriverBinding - A Driver Binding Protocol instance that this driver
+ is producing.
+ DriverBindingHandle - The handle that DriverBinding is to be installe onto.
+ If this parameter is NULL, then a new handle is created.
+ ComponentName - A Component Name Protocol instance that this driver is
+ producing.
+ DriverConfiguration - A Driver Configuration Protocol instance that this
+ driver is producing.
+ DriverDiagnostics - A Driver Diagnostics Protocol instance that this
+ driver is producing.
+ Unload - The customized unload to install.
+
+Returns:
+
+ EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
+ Otherwise, then return status from gBS->InstallProtocolInterface()
+
+--*/
+{
+ EFI_STATUS Status;
+ EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
+
+ Status = EfiLibInstallAllDriverProtocols (
+ ImageHandle,
+ SystemTable,
+ DriverBinding,
+ DriverBindingHandle,
+ ComponentName,
+ DriverConfiguration,
+ DriverDiagnostics
+ );
+
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ //
+ // Retrieve the Loaded Image Protocol from Image Handle
+ //
+ Status = gBS->OpenProtocol (
+ ImageHandle,
+ &gEfiLoadedImageProtocolGuid,
+ (VOID **) &LoadedImage,
+ ImageHandle,
+ ImageHandle,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ //
+ // Fill in the Unload() service of the Loaded Image Protocol
+ //
+ LoadedImage->Unload = (Unload == NULL) ? NetLibDefaultUnload : Unload;
+ return EFI_SUCCESS;
+}
+
--- /dev/null
+#/** @file\r
+# Component name for module NetLib\r
+#\r
+# FIX ME!\r
+# Copyright (c) 2006, Intel Corporation. All right reserved.\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+[Defines]\r
+ INF_VERSION = 0x00010005\r
+ BASE_NAME = NetLibDxe\r
+ FILE_GUID = db6dcef3-9f4e-4340-9351-fc35aa8a5888\r
+ MODULE_TYPE = DXE_DRIVER\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = NetLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
+ EDK_RELEASE_VERSION = 0x00020000\r
+ EFI_SPECIFICATION_VERSION = 0x00020000\r
+\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+# VALID_ARCHITECTURES = IA32 X64 IPF EBC\r
+#\r
+\r
+[Sources.common]\r
+ DxeNetLib.c\r
+ NetBuffer.c\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
+ MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+[LibraryClasses]\r
+ MemoryAllocationLib\r
+ UefiLib\r
+ BaseLib\r
+ UefiBootServicesTableLib\r
+ UefiRuntimeServicesTableLib\r
+ BaseMemoryLib\r
+ DebugLib\r
+ PrintLib\r
+\r
+\r
+[Protocols]\r
+ gEfiIp4ProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverDiagnosticsProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiUdp4ServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiLoadedImageProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiIp4ServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverConfigurationProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiSimpleNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiUdp4ProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiComponentNameProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+\r
--- /dev/null
+/** @file\r
+\r
+Copyright (c) 2005 - 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+ NetBuffer.c\r
+\r
+Abstract:\r
+\r
+\r
+\r
+**/\r
+\r
+#include <PiDxe.h>
+
+#include <Library/NetLib.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/MemoryAllocationLib.h>\r
+\r
+\r
+/**\r
+ Allocate and build up the sketch for a NET_BUF. The net buffer allocated\r
+ has the BlockOpNum's NET_BLOCK_OP, and its associated NET_VECTOR has the\r
+ BlockNum's NET_BLOCK.\r
+\r
+ @param BlockNum The number of NET_BLOCK in the Vector of net buffer\r
+ @param BlockOpNum The number of NET_BLOCK_OP in the net buffer\r
+\r
+ @retval * Pointer to the allocated NET_BUF. If NULL the\r
+ allocation failed due to resource limit.\r
+\r
+**/\r
+STATIC\r
+NET_BUF *\r
+NetbufAllocStruct (\r
+ IN UINT32 BlockNum,\r
+ IN UINT32 BlockOpNum\r
+ )\r
+{\r
+ NET_BUF *Nbuf;\r
+ NET_VECTOR *Vector;\r
+\r
+ ASSERT (BlockOpNum >= 1);\r
+\r
+ //\r
+ // Allocate three memory blocks.\r
+ //\r
+ Nbuf = NetAllocateZeroPool (NET_BUF_SIZE (BlockOpNum));\r
+\r
+ if (Nbuf == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Nbuf->Signature = NET_BUF_SIGNATURE;\r
+ Nbuf->RefCnt = 1;\r
+ Nbuf->BlockOpNum = BlockOpNum;\r
+ NetListInit (&Nbuf->List);\r
+\r
+ if (BlockNum != 0) {\r
+ Vector = NetAllocateZeroPool (NET_VECTOR_SIZE (BlockNum));\r
+\r
+ if (Vector == NULL) {\r
+ goto FreeNbuf;\r
+ }\r
+\r
+ Vector->Signature = NET_VECTOR_SIGNATURE;\r
+ Vector->RefCnt = 1;\r
+ Vector->BlockNum = BlockNum;\r
+ Nbuf->Vector = Vector;\r
+ }\r
+\r
+ return Nbuf;\r
+\r
+FreeNbuf:\r
+\r
+ NetFreePool (Nbuf);\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Allocate a single block NET_BUF. Upon allocation, all the\r
+ free space is in the tail room.\r
+\r
+ @param Len The length of the block.\r
+\r
+ @retval * Pointer to the allocated NET_BUF. If NULL the\r
+ allocation failed due to resource limit.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufAlloc (\r
+ IN UINT32 Len\r
+ )\r
+{\r
+ NET_BUF *Nbuf;\r
+ NET_VECTOR *Vector;\r
+ UINT8 *Bulk;\r
+\r
+ ASSERT (Len > 0);\r
+\r
+ Nbuf = NetbufAllocStruct (1, 1);\r
+\r
+ if (Nbuf == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Bulk = NetAllocatePool (Len);\r
+\r
+ if (Bulk == NULL) {\r
+ goto FreeNBuf;\r
+ }\r
+\r
+ Vector = Nbuf->Vector;\r
+ Vector->Len = Len;\r
+\r
+ Vector->Block[0].Bulk = Bulk;\r
+ Vector->Block[0].Len = Len;\r
+\r
+ Nbuf->BlockOp[0].BlockHead = Bulk;\r
+ Nbuf->BlockOp[0].BlockTail = Bulk + Len;\r
+\r
+ Nbuf->BlockOp[0].Head = Bulk;\r
+ Nbuf->BlockOp[0].Tail = Bulk;\r
+ Nbuf->BlockOp[0].Size = 0;\r
+\r
+ return Nbuf;\r
+\r
+FreeNBuf:\r
+ NetFreePool (Nbuf);\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Free the vector\r
+\r
+ @param Vector Pointer to the NET_VECTOR to be freed.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+NetbufFreeVector (\r
+ IN NET_VECTOR *Vector\r
+ )\r
+{\r
+ UINT32 Index;\r
+\r
+ NET_CHECK_SIGNATURE (Vector, NET_VECTOR_SIGNATURE);\r
+ ASSERT (Vector->RefCnt > 0);\r
+\r
+ Vector->RefCnt--;\r
+\r
+ if (Vector->RefCnt > 0) {\r
+ return;\r
+ }\r
+\r
+ if (Vector->Free != NULL) {\r
+ //\r
+ // Call external free function to free the vector if it\r
+ // isn't NULL. If NET_VECTOR_OWN_FIRST is set, release the\r
+ // first block since it is allocated by us\r
+ //\r
+ if (Vector->Flag & NET_VECTOR_OWN_FIRST) {\r
+ NetFreePool (Vector->Block[0].Bulk);\r
+ }\r
+\r
+ Vector->Free (Vector->Arg);\r
+\r
+ } else {\r
+ //\r
+ // Free each memory block associated with the Vector\r
+ //\r
+ for (Index = 0; Index < Vector->BlockNum; Index++) {\r
+ NetFreePool (Vector->Block[Index].Bulk);\r
+ }\r
+ }\r
+\r
+ NetFreePool (Vector);\r
+}\r
+\r
+\r
+/**\r
+ Free the buffer and its associated NET_VECTOR.\r
+\r
+ @param Nbuf Pointer to the NET_BUF to be freed.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufFree (\r
+ IN NET_BUF *Nbuf\r
+ )\r
+{\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ ASSERT (Nbuf->RefCnt > 0);\r
+\r
+ Nbuf->RefCnt--;\r
+\r
+ if (Nbuf->RefCnt == 0) {\r
+ //\r
+ // Update Vector only when NBuf is to be released. That is,\r
+ // all the sharing of Nbuf increse Vector's RefCnt by one\r
+ //\r
+ NetbufFreeVector (Nbuf->Vector);\r
+ NetFreePool (Nbuf);\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Create a copy of NET_BUF that share the associated NET_DATA.\r
+\r
+ @param Nbuf Pointer to the net buffer to be cloned.\r
+\r
+ @retval * Pointer to the cloned net buffer.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufClone (\r
+ IN NET_BUF *Nbuf\r
+ )\r
+{\r
+ NET_BUF *Clone;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ Clone = NetAllocatePool (NET_BUF_SIZE (Nbuf->BlockOpNum));\r
+\r
+ if (Clone == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Clone->Signature = NET_BUF_SIGNATURE;\r
+ Clone->RefCnt = 1;\r
+ NetListInit (&Clone->List);\r
+\r
+ Clone->Ip = Nbuf->Ip;\r
+ Clone->Tcp = Nbuf->Tcp;\r
+\r
+ NetCopyMem (Clone->ProtoData, Nbuf->ProtoData, NET_PROTO_DATA);\r
+\r
+ NET_GET_REF (Nbuf->Vector);\r
+\r
+ Clone->Vector = Nbuf->Vector;\r
+ Clone->BlockOpNum = Nbuf->BlockOpNum;\r
+ Clone->TotalSize = Nbuf->TotalSize;\r
+ NetCopyMem (Clone->BlockOp, Nbuf->BlockOp, sizeof (NET_BLOCK_OP) * Nbuf->BlockOpNum);\r
+\r
+ return Clone;\r
+}\r
+\r
+\r
+/**\r
+ Create a duplicated copy of Nbuf, data is copied. Also leave some\r
+ head space before the data.\r
+\r
+ @param Nbuf Pointer to the net buffer to be cloned.\r
+ @param Duplicate Pointer to the net buffer to duplicate to, if NULL\r
+ a new net buffer is allocated.\r
+ @param HeadSpace Length of the head space to reserve\r
+\r
+ @retval * Pointer to the duplicated net buffer.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufDuplicate (\r
+ IN NET_BUF *Nbuf,\r
+ IN NET_BUF *Duplicate OPTIONAL,\r
+ IN UINT32 HeadSpace\r
+ )\r
+{\r
+ UINT8 *Dst;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ if (Duplicate == NULL) {\r
+ Duplicate = NetbufAlloc (Nbuf->TotalSize + HeadSpace);\r
+ }\r
+\r
+ if (Duplicate == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ //\r
+ // Don't set the IP and TCP head point, since it is most\r
+ // like that they are pointing to the memory of Nbuf.\r
+ //\r
+ NetCopyMem (Duplicate->ProtoData, Nbuf->ProtoData, NET_PROTO_DATA);\r
+ NetbufReserve (Duplicate, HeadSpace);\r
+\r
+ Dst = NetbufAllocSpace (Duplicate, Nbuf->TotalSize, NET_BUF_TAIL);\r
+ NetbufCopy (Nbuf, 0, Nbuf->TotalSize, Dst);\r
+\r
+ return Duplicate;\r
+}\r
+\r
+\r
+/**\r
+ Free a list of net buffers.\r
+\r
+ @param Head Pointer to the head of linked net buffers.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufFreeList (\r
+ IN NET_LIST_ENTRY *Head\r
+ )\r
+{\r
+ NET_LIST_ENTRY *Entry;\r
+ NET_LIST_ENTRY *Next;\r
+ NET_BUF *Nbuf;\r
+\r
+ Entry = Head->ForwardLink;\r
+\r
+ NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r
+ Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ NetListRemoveEntry (Entry);\r
+ NetbufFree (Nbuf);\r
+ }\r
+\r
+ ASSERT (NetListIsEmpty (Head));\r
+}\r
+\r
+\r
+/**\r
+ Get the position of some byte in the net buffer. This can be used\r
+ to, for example, retrieve the IP header in the packet. It also\r
+ returns the fragment that contains the byte which is used mainly by\r
+ the buffer implementation itself.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Offset The index or offset of the byte\r
+ @param Index Index of the fragment that contains the block\r
+\r
+ @retval * Pointer to the nth byte of data in the net buffer.\r
+ If NULL, there is no such data in the net buffer.\r
+\r
+**/\r
+UINT8 *\r
+NetbufGetByte (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT32 Offset,\r
+ OUT UINT32 *Index OPTIONAL\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+ UINT32 Loop;\r
+ UINT32 Len;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ if (Offset >= Nbuf->TotalSize) {\r
+ return NULL;\r
+ }\r
+\r
+ BlockOp = Nbuf->BlockOp;\r
+ Len = 0;\r
+\r
+ for (Loop = 0; Loop < Nbuf->BlockOpNum; Loop++) {\r
+\r
+ if (Len + BlockOp[Loop].Size <= Offset) {\r
+ Len += BlockOp[Loop].Size;\r
+ continue;\r
+ }\r
+\r
+ if (Index != NULL) {\r
+ *Index = Loop;\r
+ }\r
+\r
+ return BlockOp[Loop].Head + (Offset - Len);\r
+ }\r
+\r
+ return NULL;\r
+}\r
+\r
+\r
+\r
+/**\r
+ Set the NET_BLOCK and corresponding NET_BLOCK_OP in\r
+ the buffer. All the pointers in NET_BLOCK and NET_BLOCK_OP\r
+ are set to the bulk's head and tail respectively. So, this\r
+ function alone can't be used by NetbufAlloc.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Bulk Pointer to the data.\r
+ @param Len Length of the bulk data.\r
+ @param Index The data block index in the net buffer the bulk\r
+ data should belong to.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+NetbufSetBlock (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT8 *Bulk,\r
+ IN UINT32 Len,\r
+ IN UINT32 Index\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+ NET_BLOCK *Block;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE);\r
+ ASSERT (Index < Nbuf->BlockOpNum);\r
+\r
+ Block = &(Nbuf->Vector->Block[Index]);\r
+ BlockOp = &(Nbuf->BlockOp[Index]);\r
+ Block->Len = Len;\r
+ Block->Bulk = Bulk;\r
+ BlockOp->BlockHead = Bulk;\r
+ BlockOp->BlockTail = Bulk + Len;\r
+ BlockOp->Head = Bulk;\r
+ BlockOp->Tail = Bulk + Len;\r
+ BlockOp->Size = Len;\r
+}\r
+\r
+\r
+\r
+/**\r
+ Set the NET_BLOCK_OP in the buffer. The corresponding NET_BLOCK\r
+ structure is left untouched. Some times, there is no 1:1 relationship\r
+ between NET_BLOCK and NET_BLOCK_OP. For example, that in NetbufGetFragment.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Bulk Pointer to the data.\r
+ @param Len Length of the bulk data.\r
+ @param Index The data block index in the net buffer the bulk\r
+ data should belong to.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+NetbufSetBlockOp (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT8 *Bulk,\r
+ IN UINT32 Len,\r
+ IN UINT32 Index\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ ASSERT (Index < Nbuf->BlockOpNum);\r
+\r
+ BlockOp = &(Nbuf->BlockOp[Index]);\r
+ BlockOp->BlockHead = Bulk;\r
+ BlockOp->BlockTail = Bulk + Len;\r
+ BlockOp->Head = Bulk;\r
+ BlockOp->Tail = Bulk + Len;\r
+ BlockOp->Size = Len;\r
+}\r
+\r
+\r
+/**\r
+ Helper function for NetbufClone. It is necessary because NetbufGetFragment\r
+ may allocate the first block to accomodate the HeadSpace and HeadLen. So, it\r
+ need to create a new NET_VECTOR. But, we want to avoid data copy by sharing\r
+ the old NET_VECTOR.\r
+\r
+ @param Arg Point to the old NET_VECTOR\r
+\r
+ @return NONE\r
+\r
+**/\r
+STATIC\r
+VOID\r
+NetbufGetFragmentFree (\r
+ IN VOID *Arg\r
+ )\r
+{\r
+ NET_VECTOR *Vector;\r
+\r
+ Vector = (NET_VECTOR *)Arg;\r
+ NetbufFreeVector (Vector);\r
+}\r
+\r
+\r
+\r
+/**\r
+ Create a NET_BUF structure which contains Len byte data of\r
+ Nbuf starting from Offset. A new NET_BUF structure will be\r
+ created but the associated data in NET_VECTOR is shared.\r
+ This function exists to do IP packet fragmentation.\r
+\r
+ @param Nbuf Pointer to the net buffer to be cloned.\r
+ @param Offset Starting point of the data to be included in new\r
+ buffer.\r
+ @param Len How many data to include in new data\r
+ @param HeadSpace How many bytes of head space to reserve for\r
+ protocol header\r
+\r
+ @retval * Pointer to the cloned net buffer.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufGetFragment (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT32 Offset,\r
+ IN UINT32 Len,\r
+ IN UINT32 HeadSpace\r
+ )\r
+{\r
+ NET_BUF *Child;\r
+ NET_VECTOR *Vector;\r
+ NET_BLOCK_OP *BlockOp;\r
+ UINT32 CurBlockOp;\r
+ UINT32 BlockOpNum;\r
+ UINT8 *FirstBulk;\r
+ UINT32 Index;\r
+ UINT32 First;\r
+ UINT32 Last;\r
+ UINT32 FirstSkip;\r
+ UINT32 FirstLen;\r
+ UINT32 LastLen;\r
+ UINT32 Cur;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ if ((Len == 0) || (Offset + Len > Nbuf->TotalSize)) {\r
+ return NULL;\r
+ }\r
+\r
+ //\r
+ // First find the first and last BlockOp that contains\r
+ // the valid data, and compute the offset of the first\r
+ // BlockOp and length of the last BlockOp\r
+ //\r
+ BlockOp = Nbuf->BlockOp;\r
+ Cur = 0;\r
+\r
+ for (Index = 0; Index < Nbuf->BlockOpNum; Index++) {\r
+ if (Offset < Cur + BlockOp[Index].Size) {\r
+ break;\r
+ }\r
+\r
+ Cur += BlockOp[Index].Size;\r
+ }\r
+\r
+ //\r
+ // First is the index of the first BlockOp, FirstSkip is\r
+ // the offset of the first byte in the first BlockOp.\r
+ //\r
+ First = Index;\r
+ FirstSkip = Offset - Cur;\r
+ FirstLen = BlockOp[Index].Size - FirstSkip;\r
+\r
+ //\r
+ //redundant assignment to make compiler happy.\r
+ //\r
+ Last = 0;\r
+ LastLen = 0;\r
+\r
+ if (Len > FirstLen) {\r
+ Cur += BlockOp[Index].Size;\r
+ Index++;\r
+\r
+ for (; Index < Nbuf->BlockOpNum; Index++) {\r
+ if (Offset + Len <= Cur + BlockOp[Index].Size) {\r
+ Last = Index;\r
+ LastLen = Offset + Len - Cur;\r
+ break;\r
+ }\r
+\r
+ Cur += BlockOp[Index].Size;\r
+ }\r
+\r
+ } else {\r
+ Last = First;\r
+ LastLen = Len;\r
+ FirstLen = Len;\r
+ }\r
+\r
+ BlockOpNum = Last - First + 1;\r
+ CurBlockOp = 0;\r
+\r
+ if (HeadSpace != 0) {\r
+ //\r
+ // Allocate an extra block to accomdate the head space.\r
+ //\r
+ BlockOpNum++;\r
+\r
+ Child = NetbufAllocStruct (1, BlockOpNum);\r
+\r
+ if (Child == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ FirstBulk = NetAllocatePool (HeadSpace);\r
+\r
+ if (FirstBulk == NULL) {\r
+ goto FreeChild;\r
+ }\r
+\r
+ Vector = Child->Vector;\r
+ Vector->Free = NetbufGetFragmentFree;\r
+ Vector->Arg = Nbuf->Vector;\r
+ Vector->Flag = NET_VECTOR_OWN_FIRST;\r
+ Vector->Len = HeadSpace;\r
+\r
+ //\r
+ //Reserve the head space in the first block\r
+ //\r
+ NetbufSetBlock (Child, FirstBulk, HeadSpace, 0);\r
+ Child->BlockOp[0].Head += HeadSpace;\r
+ Child->BlockOp[0].Size = 0;\r
+ CurBlockOp++;\r
+\r
+ }else {\r
+ Child = NetbufAllocStruct (0, BlockOpNum);\r
+\r
+ if (Child == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Child->Vector = Nbuf->Vector;\r
+ }\r
+\r
+ NET_GET_REF (Nbuf->Vector);\r
+ Child->TotalSize = Len;\r
+\r
+ //\r
+ // Set all the BlockOp up, the first and last one are special\r
+ // and need special process.\r
+ //\r
+ NetbufSetBlockOp (\r
+ Child,\r
+ Nbuf->BlockOp[First].Head + FirstSkip,\r
+ FirstLen,\r
+ CurBlockOp++\r
+ );\r
+\r
+ for (Index = First + 1; Index <= Last - 1 ; Index++) {\r
+ NetbufSetBlockOp (\r
+ Child,\r
+ BlockOp[Index].Head,\r
+ BlockOp[Index].Size,\r
+ CurBlockOp++\r
+ );\r
+ }\r
+\r
+ if (First != Last) {\r
+ NetbufSetBlockOp (\r
+ Child,\r
+ BlockOp[Last].Head,\r
+ LastLen,\r
+ CurBlockOp\r
+ );\r
+ }\r
+\r
+ NetCopyMem (Child->ProtoData, Nbuf->ProtoData, NET_PROTO_DATA);\r
+ return Child;\r
+\r
+FreeChild:\r
+\r
+ NetFreePool (Child);\r
+ return NULL;\r
+}\r
+\r
+\r
+\r
+/**\r
+ Build a NET_BUF from external blocks.\r
+\r
+ @param ExtFragment Pointer to the data block.\r
+ @param ExtNum The number of the data block.\r
+ @param HeadSpace The head space to be reserved.\r
+ @param HeadLen The length of the protocol header, This function\r
+ will pull that number of data into a linear block.\r
+ @param ExtFree Pointer to the caller provided free function.\r
+ @param Arg The argument passed to ExtFree when ExtFree is\r
+ called.\r
+\r
+ @retval * Pointer to the net buffer built from the data\r
+ blocks.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufFromExt (\r
+ IN NET_FRAGMENT *ExtFragment,\r
+ IN UINT32 ExtNum,\r
+ IN UINT32 HeadSpace,\r
+ IN UINT32 HeadLen,\r
+ IN NET_VECTOR_EXT_FREE ExtFree,\r
+ IN VOID *Arg OPTIONAL\r
+ )\r
+{\r
+ NET_BUF *Nbuf;\r
+ NET_VECTOR *Vector;\r
+ NET_FRAGMENT SavedFragment;\r
+ UINT32 SavedIndex;\r
+ UINT32 TotalLen;\r
+ UINT32 BlockNum;\r
+ UINT8 *FirstBlock;\r
+ UINT32 FirstBlockLen;\r
+ UINT8 *Header;\r
+ UINT32 CurBlock;\r
+ UINT32 Index;\r
+ UINT32 Len;\r
+ UINT32 Copied;\r
+\r
+ ASSERT ((ExtFragment != NULL) && (ExtNum > 0) && (ExtFree != NULL));\r
+\r
+ SavedFragment.Bulk = NULL;\r
+ SavedFragment.Len = 0;\r
+\r
+ FirstBlockLen = 0;\r
+ FirstBlock = NULL;\r
+ BlockNum = ExtNum;\r
+ Index = 0;\r
+ TotalLen = 0;\r
+ SavedIndex = 0;\r
+ Len = 0;\r
+ Copied = 0;\r
+\r
+ //\r
+ // No need to consolidate the header if the first block is\r
+ // longer than the header length or there is only one block.\r
+ //\r
+ if ((ExtFragment[0].Len >= HeadLen) || (ExtNum == 1)) {\r
+ HeadLen = 0;\r
+ }\r
+\r
+ //\r
+ // Allocate an extra block if we need to:\r
+ // 1. Allocate some header space\r
+ // 2. aggreate the packet header\r
+ //\r
+ if ((HeadSpace != 0) || (HeadLen != 0)) {\r
+ FirstBlockLen = HeadLen + HeadSpace;\r
+ FirstBlock = NetAllocatePool (FirstBlockLen);\r
+\r
+ if (FirstBlock == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ BlockNum++;\r
+ }\r
+\r
+ //\r
+ // Copy the header to the first block, reduce the NET_BLOCK\r
+ // to allocate by one for each block that is completely covered\r
+ // by the first bulk.\r
+ //\r
+ if (HeadLen != 0) {\r
+ Len = HeadLen;\r
+ Header = FirstBlock + HeadSpace;\r
+\r
+ for (Index = 0; Index < ExtNum; Index++) {\r
+ if (Len >= ExtFragment[Index].Len) {\r
+ NetCopyMem (Header, ExtFragment[Index].Bulk, ExtFragment[Index].Len);\r
+\r
+ Copied += ExtFragment[Index].Len;\r
+ Len -= ExtFragment[Index].Len;\r
+ Header += ExtFragment[Index].Len;\r
+ TotalLen += ExtFragment[Index].Len;\r
+ BlockNum--;\r
+\r
+ if (Len == 0) {\r
+ //\r
+ // Increament the index number to point to the next\r
+ // non-empty fragment.\r
+ //\r
+ Index++;\r
+ break;\r
+ }\r
+\r
+ } else {\r
+ NetCopyMem (Header, ExtFragment[Index].Bulk, Len);\r
+\r
+ Copied += Len;\r
+ TotalLen += Len;\r
+\r
+ //\r
+ // Adjust the block structure to exclude the data copied,\r
+ // So, the left-over block can be processed as other blocks.\r
+ // But it must be recovered later. (SavedIndex > 0) always\r
+ // holds since we don't aggreate the header if the first block\r
+ // is bigger enough that the header is continuous\r
+ //\r
+ SavedIndex = Index;\r
+ SavedFragment = ExtFragment[Index];\r
+ ExtFragment[Index].Bulk += Len;\r
+ ExtFragment[Index].Len -= Len;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ Nbuf = NetbufAllocStruct (BlockNum, BlockNum);\r
+\r
+ if (Nbuf == NULL) {\r
+ goto FreeFirstBlock;\r
+ }\r
+\r
+ Vector = Nbuf->Vector;\r
+ Vector->Free = ExtFree;\r
+ Vector->Arg = Arg;\r
+ Vector->Flag = (FirstBlockLen ? NET_VECTOR_OWN_FIRST : 0);\r
+\r
+ //\r
+ // Set the first block up which may contain\r
+ // some head space and aggregated header\r
+ //\r
+ CurBlock = 0;\r
+\r
+ if (FirstBlockLen != 0) {\r
+ NetbufSetBlock (Nbuf, FirstBlock, HeadSpace + Copied, 0);\r
+ Nbuf->BlockOp[0].Head += HeadSpace;\r
+ Nbuf->BlockOp[0].Size = Copied;\r
+\r
+ CurBlock++;\r
+ }\r
+\r
+ for (; Index < ExtNum; Index++) {\r
+ NetbufSetBlock (Nbuf, ExtFragment[Index].Bulk, ExtFragment[Index].Len, CurBlock);\r
+ TotalLen += ExtFragment[Index].Len;\r
+ CurBlock++;\r
+ }\r
+\r
+ Vector->Len = TotalLen + HeadSpace;\r
+ Nbuf->TotalSize = TotalLen;\r
+\r
+ if (SavedIndex) {\r
+ ExtFragment[SavedIndex] = SavedFragment;\r
+ }\r
+\r
+ return Nbuf;\r
+\r
+FreeFirstBlock:\r
+ NetFreePool (FirstBlock);\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Build a fragment table to contain the fragments in the\r
+ buffer. This is the opposite of the NetbufFromExt.\r
+\r
+ @param Nbuf Point to the net buffer\r
+ @param ExtFragment Pointer to the data block.\r
+ @param ExtNum The number of the data block.\r
+\r
+ @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than ExtNum\r
+ @retval EFI_SUCCESS Fragment table built.\r
+\r
+**/\r
+EFI_STATUS\r
+NetbufBuildExt (\r
+ IN NET_BUF *Nbuf,\r
+ IN NET_FRAGMENT *ExtFragment,\r
+ IN UINT32 *ExtNum\r
+ )\r
+{\r
+ UINT32 Index;\r
+ UINT32 Current;\r
+\r
+ Current = 0;\r
+\r
+ for (Index = 0; (Index < Nbuf->BlockOpNum); Index++) {\r
+ if (Nbuf->BlockOp[Index].Size == 0) {\r
+ continue;\r
+ }\r
+\r
+ if (Current < *ExtNum) {\r
+ ExtFragment[Current].Bulk = Nbuf->BlockOp[Index].Head;\r
+ ExtFragment[Current].Len = Nbuf->BlockOp[Index].Size;\r
+ Current++;\r
+ } else {\r
+ return EFI_BUFFER_TOO_SMALL;\r
+ }\r
+ }\r
+\r
+ *ExtNum = Current;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Build a NET_BUF from a list of NET_BUF.\r
+\r
+ @param BufList A List of NET_BUF.\r
+ @param HeadSpace The head space to be reserved.\r
+ @param HeaderLen The length of the protocol header, This function\r
+ will pull that number of data into a linear block.\r
+ @param ExtFree Pointer to the caller provided free function.\r
+ @param Arg The argument passed to ExtFree when ExtFree is\r
+ called.\r
+\r
+ @retval * Pointer to the net buffer built from the data\r
+ blocks.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufFromBufList (\r
+ IN NET_LIST_ENTRY *BufList,\r
+ IN UINT32 HeadSpace,\r
+ IN UINT32 HeaderLen,\r
+ IN NET_VECTOR_EXT_FREE ExtFree,\r
+ IN VOID *Arg OPTIONAL\r
+ )\r
+{\r
+ NET_FRAGMENT *Fragment;\r
+ UINT32 FragmentNum;\r
+ NET_LIST_ENTRY *Entry;\r
+ NET_BUF *Nbuf;\r
+ UINT32 Index;\r
+ UINT32 Current;\r
+\r
+ //\r
+ //Compute how many blocks are there\r
+ //\r
+ FragmentNum = 0;\r
+\r
+ NET_LIST_FOR_EACH (Entry, BufList) {\r
+ Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ FragmentNum += Nbuf->BlockOpNum;\r
+ }\r
+\r
+ //\r
+ //Allocate and copy block points\r
+ //\r
+ Fragment = NetAllocatePool (sizeof (NET_FRAGMENT) * FragmentNum);\r
+\r
+ if (Fragment == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Current = 0;\r
+\r
+ NET_LIST_FOR_EACH (Entry, BufList) {\r
+ Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ for (Index = 0; Index < Nbuf->BlockOpNum; Index++) {\r
+ if (Nbuf->BlockOp[Index].Size) {\r
+ Fragment[Current].Bulk = Nbuf->BlockOp[Index].Head;\r
+ Fragment[Current].Len = Nbuf->BlockOp[Index].Size;\r
+ Current++;\r
+ }\r
+ }\r
+ }\r
+\r
+ Nbuf = NetbufFromExt (Fragment, Current, HeadSpace, HeaderLen, ExtFree, Arg);\r
+ NetFreePool (Fragment);\r
+\r
+ return Nbuf;\r
+}\r
+\r
+\r
+/**\r
+ Reserve some space in the header room of the buffer.\r
+ Upon allocation, all the space are in the tail room\r
+ of the buffer. Call this function to move some space\r
+ to the header room. This function is quite limited in\r
+ that it can only reserver space from the first block\r
+ of an empty NET_BUF not built from the external. But\r
+ it should be enough for the network stack.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Len The length of buffer to be reserverd.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufReserve (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT32 Len\r
+ )\r
+{\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE);\r
+\r
+ ASSERT ((Nbuf->BlockOpNum == 1) && (Nbuf->TotalSize == 0));\r
+ ASSERT ((Nbuf->Vector->Free == NULL) && (Nbuf->Vector->Len >= Len));\r
+\r
+ Nbuf->BlockOp[0].Head += Len;\r
+ Nbuf->BlockOp[0].Tail += Len;\r
+\r
+ ASSERT (Nbuf->BlockOp[0].Tail <= Nbuf->BlockOp[0].BlockTail);\r
+}\r
+\r
+\r
+/**\r
+ Allocate some space from the header or tail of the buffer.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Len The length of the buffer to be allocated.\r
+ @param FromHead The flag to indicate whether reserve the data from\r
+ head or tail. TRUE for from head, and FALSE for\r
+ from tail.\r
+\r
+ @retval * Pointer to the first byte of the allocated buffer.\r
+\r
+**/\r
+UINT8 *\r
+NetbufAllocSpace (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT32 Len,\r
+ IN BOOLEAN FromHead\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+ UINT32 Index;\r
+ UINT8 *SavedTail;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE);\r
+\r
+ ASSERT (Len > 0);\r
+\r
+ if (FromHead) {\r
+ //\r
+ // Allocate some space from head. If the buffer is empty,\r
+ // allocate from the first block. If it isn't, allocate\r
+ // from the first non-empty block, or the block before that.\r
+ //\r
+ if (Nbuf->TotalSize == 0) {\r
+ Index = 0;\r
+ } else {\r
+ NetbufGetByte (Nbuf, 0, &Index);\r
+\r
+ if ((NET_HEADSPACE(&(Nbuf->BlockOp[Index])) < Len) && (Index > 0)) {\r
+ Index--;\r
+ }\r
+ }\r
+\r
+ BlockOp = &(Nbuf->BlockOp[Index]);\r
+\r
+ if (NET_HEADSPACE (BlockOp) < Len) {\r
+ return NULL;\r
+ }\r
+\r
+ BlockOp->Head -= Len;\r
+ BlockOp->Size += Len;\r
+ Nbuf->TotalSize += Len;\r
+\r
+ return BlockOp->Head;\r
+\r
+ } else {\r
+ //\r
+ // Allocate some space from the tail. If the buffer is empty,\r
+ // allocate from the first block. If it isn't, allocate\r
+ // from the last non-empty block, or the block after that.\r
+ //\r
+ if (Nbuf->TotalSize == 0) {\r
+ Index = 0;\r
+ } else {\r
+ NetbufGetByte (Nbuf, Nbuf->TotalSize - 1, &Index);\r
+\r
+ if ((NET_TAILSPACE(&(Nbuf->BlockOp[Index])) < Len) &&\r
+ (Index < Nbuf->BlockOpNum - 1)) {\r
+\r
+ Index++;\r
+ }\r
+ }\r
+\r
+ BlockOp = &(Nbuf->BlockOp[Index]);\r
+\r
+ if (NET_TAILSPACE (BlockOp) < Len) {\r
+ return NULL;\r
+ }\r
+\r
+ SavedTail = BlockOp->Tail;\r
+\r
+ BlockOp->Tail += Len;\r
+ BlockOp->Size += Len;\r
+ Nbuf->TotalSize += Len;\r
+\r
+ return SavedTail;\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Trim a single NET_BLOCK.\r
+\r
+ @param BlockOp Pointer to the NET_BLOCK.\r
+ @param Len The length of the data to be trimmed.\r
+ @param FromHead The flag to indicate whether trim data from head or\r
+ tail. TRUE for from head, and FALSE for from tail.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+NetblockTrim (\r
+ IN NET_BLOCK_OP *BlockOp,\r
+ IN UINT32 Len,\r
+ IN BOOLEAN FromHead\r
+ )\r
+{\r
+ ASSERT (BlockOp && (BlockOp->Size >= Len));\r
+\r
+ BlockOp->Size -= Len;\r
+\r
+ if (FromHead) {\r
+ BlockOp->Head += Len;\r
+ } else {\r
+ BlockOp->Tail -= Len;\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Trim some data from the header or tail of the buffer.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Len The length of the data to be trimmed.\r
+ @param FromHead The flag to indicate whether trim data from head or\r
+ tail. TRUE for from head, and FALSE for from tail.\r
+\r
+ @retval UINTN Length of the actually trimmed data.\r
+\r
+**/\r
+UINT32\r
+NetbufTrim (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT32 Len,\r
+ IN BOOLEAN FromHead\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+ UINT32 Index;\r
+ UINT32 Trimmed;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ if (Len > Nbuf->TotalSize) {\r
+ Len = Nbuf->TotalSize;\r
+ }\r
+\r
+ //\r
+ // If FromTail is true, iterate backward. That\r
+ // is, init Index to NBuf->BlockNum - 1, and\r
+ // decrease it by 1 during each loop. Otherwise,\r
+ // iterate forward. That is, init Index to 0, and\r
+ // increase it by 1 during each loop.\r
+ //\r
+ Trimmed = 0;\r
+ Nbuf->TotalSize -= Len;\r
+\r
+ Index = (FromHead ? 0 : Nbuf->BlockOpNum - 1);\r
+ BlockOp = Nbuf->BlockOp;\r
+\r
+ for (;;) {\r
+ if (BlockOp[Index].Size == 0) {\r
+ Index += (FromHead ? 1 : -1);\r
+ continue;\r
+ }\r
+\r
+ if (Len > BlockOp[Index].Size) {\r
+ Len -= BlockOp[Index].Size;\r
+ Trimmed += BlockOp[Index].Size;\r
+ NetblockTrim (&BlockOp[Index], BlockOp[Index].Size, FromHead);\r
+ } else {\r
+ Trimmed += Len;\r
+ NetblockTrim (&BlockOp[Index], Len, FromHead);\r
+ break;\r
+ }\r
+\r
+ Index += (FromHead ? 1 : -1);\r
+ }\r
+\r
+ return Trimmed;\r
+}\r
+\r
+\r
+/**\r
+ Copy the data from the specific offset to the destination.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+ @param Offset The sequence number of the first byte to copy.\r
+ @param Len Length of the data to copy.\r
+ @param Dest The destination of the data to copy to.\r
+\r
+ @retval UINTN The length of the copied data.\r
+\r
+**/\r
+UINT32\r
+NetbufCopy (\r
+ IN NET_BUF *Nbuf,\r
+ IN UINT32 Offset,\r
+ IN UINT32 Len,\r
+ IN UINT8 *Dest\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+ UINT32 Skip;\r
+ UINT32 Left;\r
+ UINT32 Copied;\r
+ UINT32 Index;\r
+ UINT32 Cur;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+ ASSERT (Dest);\r
+\r
+ if ((Len == 0) || (Nbuf->TotalSize <= Offset)) {\r
+ return 0;\r
+ }\r
+\r
+ if (Nbuf->TotalSize - Offset < Len) {\r
+ Len = Nbuf->TotalSize - Offset;\r
+ }\r
+\r
+ BlockOp = Nbuf->BlockOp;\r
+\r
+ //\r
+ // Skip to the offset. Don't make "Offset-By-One" error here.\r
+ // Cur + BLOCK.SIZE is the first sequence number of next block.\r
+ // So, (Offset < Cur + BLOCK.SIZE) means that the first byte\r
+ // is in the current block. if (Offset == Cur + BLOCK.SIZE), the\r
+ // first byte is the next block's first byte.\r
+ //\r
+ Cur = 0;\r
+\r
+ for (Index = 0; Index < Nbuf->BlockOpNum; Index++) {\r
+ if (BlockOp[Index].Size == 0) {\r
+ continue;\r
+ }\r
+\r
+ if (Offset < Cur + BlockOp[Index].Size) {\r
+ break;\r
+ }\r
+\r
+ Cur += BlockOp[Index].Size;\r
+ }\r
+\r
+ //\r
+ // Cur is the sequence number of the first byte in the block\r
+ // Offset - Cur is the number of bytes before first byte to\r
+ // to copy in the current block.\r
+ //\r
+ Skip = Offset - Cur;\r
+ Left = BlockOp[Index].Size - Skip;\r
+\r
+ if (Len <= Left) {\r
+ NetCopyMem (Dest, BlockOp[Index].Head + Skip, Len);\r
+ return Len;\r
+ }\r
+\r
+ NetCopyMem (Dest, BlockOp[Index].Head + Skip, Left);\r
+\r
+ Dest += Left;\r
+ Len -= Left;\r
+ Copied = Left;\r
+\r
+ Index++;\r
+\r
+ for (; Index < Nbuf->BlockOpNum; Index++) {\r
+ if (Len > BlockOp[Index].Size) {\r
+ Len -= BlockOp[Index].Size;\r
+ Copied += BlockOp[Index].Size;\r
+\r
+ NetCopyMem (Dest, BlockOp[Index].Head, BlockOp[Index].Size);\r
+ Dest += BlockOp[Index].Size;\r
+ } else {\r
+ Copied += Len;\r
+ NetCopyMem (Dest, BlockOp[Index].Head, Len);\r
+ break;\r
+ }\r
+ }\r
+\r
+ return Copied;\r
+}\r
+\r
+\r
+/**\r
+ Initiate the net buffer queue.\r
+\r
+ @param NbufQue Pointer to the net buffer queue to be initiated.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufQueInit (\r
+ IN NET_BUF_QUEUE *NbufQue\r
+ )\r
+{\r
+ NbufQue->Signature = NET_QUE_SIGNATURE;\r
+ NbufQue->RefCnt = 1;\r
+ NetListInit (&NbufQue->List);\r
+\r
+ NetListInit (&NbufQue->BufList);\r
+ NbufQue->BufSize = 0;\r
+ NbufQue->BufNum = 0;\r
+}\r
+\r
+\r
+/**\r
+ Allocate an initialized net buffer queue.\r
+\r
+ None.\r
+\r
+ @retval * Pointer to the allocated net buffer queue.\r
+\r
+**/\r
+NET_BUF_QUEUE *\r
+NetbufQueAlloc (\r
+ VOID\r
+ )\r
+{\r
+ NET_BUF_QUEUE *NbufQue;\r
+\r
+ NbufQue = NetAllocatePool (sizeof (NET_BUF_QUEUE));\r
+ if (NbufQue == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ NetbufQueInit (NbufQue);\r
+\r
+ return NbufQue;\r
+}\r
+\r
+\r
+/**\r
+ Free a net buffer queue.\r
+\r
+ @param NbufQue Poitner to the net buffer queue to be freed.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufQueFree (\r
+ IN NET_BUF_QUEUE *NbufQue\r
+ )\r
+{\r
+ NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE);\r
+\r
+ NbufQue->RefCnt--;\r
+\r
+ if (NbufQue->RefCnt == 0) {\r
+ NetbufQueFlush (NbufQue);\r
+ NetFreePool (NbufQue);\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Append a buffer to the end of the queue.\r
+\r
+ @param NbufQue Pointer to the net buffer queue.\r
+ @param Nbuf Pointer to the net buffer to be appended.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufQueAppend (\r
+ IN NET_BUF_QUEUE *NbufQue,\r
+ IN NET_BUF *Nbuf\r
+ )\r
+{\r
+ NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE);\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ NetListInsertTail (&NbufQue->BufList, &Nbuf->List);\r
+\r
+ NbufQue->BufSize += Nbuf->TotalSize;\r
+ NbufQue->BufNum++;\r
+}\r
+\r
+\r
+/**\r
+ Remove a net buffer from head in the specific queue.\r
+\r
+ @param NbufQue Pointer to the net buffer queue.\r
+\r
+ @retval * Pointer to the net buffer removed from the specific\r
+ queue.\r
+\r
+**/\r
+NET_BUF *\r
+NetbufQueRemove (\r
+ IN NET_BUF_QUEUE *NbufQue\r
+ )\r
+{\r
+ NET_BUF *First;\r
+\r
+ NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE);\r
+\r
+ if (NbufQue->BufNum == 0) {\r
+ return NULL;\r
+ }\r
+\r
+ First = NET_LIST_USER_STRUCT (NbufQue->BufList.ForwardLink, NET_BUF, List);\r
+\r
+ NetListRemoveHead (&NbufQue->BufList);\r
+\r
+ NbufQue->BufSize -= First->TotalSize;\r
+ NbufQue->BufNum--;\r
+ return First;\r
+}\r
+\r
+\r
+/**\r
+ Copy some data from the buffer queue to the destination.\r
+\r
+ @param NbufQue Pointer to the net buffer queue.\r
+ @param Offset The sequence number of the first byte to copy.\r
+ @param Len Length of the data to copy.\r
+ @param Dest The destination of the data to copy to.\r
+\r
+ @retval UINTN The length of the copied data.\r
+\r
+**/\r
+UINT32\r
+NetbufQueCopy (\r
+ IN NET_BUF_QUEUE *NbufQue,\r
+ IN UINT32 Offset,\r
+ IN UINT32 Len,\r
+ IN UINT8 *Dest\r
+ )\r
+{\r
+ NET_LIST_ENTRY *Entry;\r
+ NET_BUF *Nbuf;\r
+ UINT32 Skip;\r
+ UINT32 Left;\r
+ UINT32 Cur;\r
+ UINT32 Copied;\r
+\r
+ NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE);\r
+ ASSERT (Dest != NULL);\r
+\r
+ if ((Len == 0) || (NbufQue->BufSize <= Offset)) {\r
+ return 0;\r
+ }\r
+\r
+ if (NbufQue->BufSize - Offset < Len) {\r
+ Len = NbufQue->BufSize - Offset;\r
+ }\r
+\r
+ //\r
+ // skip to the Offset\r
+ //\r
+ Cur = 0;\r
+ Nbuf = NULL;\r
+\r
+ NET_LIST_FOR_EACH (Entry, &NbufQue->BufList) {\r
+ Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
+\r
+ if (Offset < Cur + Nbuf->TotalSize) {\r
+ break;\r
+ }\r
+\r
+ Cur += Nbuf->TotalSize;\r
+ }\r
+\r
+ //\r
+ // Copy the data in the first buffer.\r
+ //\r
+ Skip = Offset - Cur;\r
+ Left = Nbuf->TotalSize - Skip;\r
+\r
+ if (Len < Left) {\r
+ return NetbufCopy (Nbuf, Skip, Len, Dest);\r
+ }\r
+\r
+ NetbufCopy (Nbuf, Skip, Left, Dest);\r
+ Dest += Left;\r
+ Len -= Left;\r
+ Copied = Left;\r
+\r
+ //\r
+ // Iterate over the others\r
+ //\r
+ Entry = Entry->ForwardLink;\r
+\r
+ while ((Len > 0) && (Entry != &NbufQue->BufList)) {\r
+ Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
+\r
+ if (Len > Nbuf->TotalSize) {\r
+ Len -= Nbuf->TotalSize;\r
+ Copied += Nbuf->TotalSize;\r
+\r
+ NetbufCopy (Nbuf, 0, Nbuf->TotalSize, Dest);\r
+ Dest += Nbuf->TotalSize;\r
+\r
+ } else {\r
+ NetbufCopy (Nbuf, 0, Len, Dest);\r
+ Copied += Len;\r
+ break;\r
+ }\r
+\r
+ Entry = Entry->ForwardLink;\r
+ }\r
+\r
+ return Copied;\r
+}\r
+\r
+\r
+/**\r
+ Trim some data from the queue header, release the buffer if\r
+ whole buffer is trimmed.\r
+\r
+ @param NbufQue Pointer to the net buffer queue.\r
+ @param Len Length of the data to trim.\r
+\r
+ @retval UINTN The length of the data trimmed.\r
+\r
+**/\r
+UINT32\r
+NetbufQueTrim (\r
+ IN NET_BUF_QUEUE *NbufQue,\r
+ IN UINT32 Len\r
+ )\r
+{\r
+ NET_LIST_ENTRY *Entry;\r
+ NET_LIST_ENTRY *Next;\r
+ NET_BUF *Nbuf;\r
+ UINT32 Trimmed;\r
+\r
+ NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE);\r
+\r
+ if (Len == 0) {\r
+ return 0;\r
+ }\r
+\r
+ if (Len > NbufQue->BufSize) {\r
+ Len = NbufQue->BufSize;\r
+ }\r
+\r
+ NbufQue->BufSize -= Len;\r
+ Trimmed = 0;\r
+\r
+ NET_LIST_FOR_EACH_SAFE (Entry, Next, &NbufQue->BufList) {\r
+ Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
+\r
+ if (Len >= Nbuf->TotalSize) {\r
+ Trimmed += Nbuf->TotalSize;\r
+ Len -= Nbuf->TotalSize;\r
+\r
+ NetListRemoveEntry (Entry);\r
+ NetbufFree (Nbuf);\r
+\r
+ NbufQue->BufNum--;\r
+\r
+ if (Len == 0) {\r
+ break;\r
+ }\r
+\r
+ } else {\r
+ Trimmed += NetbufTrim (Nbuf, Len, NET_BUF_HEAD);\r
+ break;\r
+ }\r
+ }\r
+\r
+ return Trimmed;\r
+}\r
+\r
+\r
+/**\r
+ Flush the net buffer queue.\r
+\r
+ @param NbufQue Pointer to the queue to be flushed.\r
+\r
+ @return None.\r
+\r
+**/\r
+VOID\r
+NetbufQueFlush (\r
+ IN NET_BUF_QUEUE *NbufQue\r
+ )\r
+{\r
+ NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE);\r
+\r
+ NetbufFreeList (&NbufQue->BufList);\r
+\r
+ NbufQue->BufNum = 0;\r
+ NbufQue->BufSize = 0;\r
+}\r
+\r
+\r
+/**\r
+ Compute checksum for a bulk of data.\r
+\r
+ @param Bulk Pointer to the data.\r
+ @param Len Length of the data, in bytes.\r
+\r
+ @retval UINT16 The computed checksum.\r
+\r
+**/\r
+UINT16\r
+NetblockChecksum (\r
+ IN UINT8 *Bulk,\r
+ IN UINT32 Len\r
+ )\r
+{\r
+ register UINT32 Sum;\r
+\r
+ Sum = 0;\r
+\r
+ while (Len > 1) {\r
+ Sum += *(UINT16 *) Bulk;\r
+ Bulk += 2;\r
+ Len -= 2;\r
+ }\r
+\r
+ //\r
+ // Add left-over byte, if any\r
+ //\r
+ if (Len > 0) {\r
+ Sum += *(UINT8 *) Bulk;\r
+ }\r
+\r
+ //\r
+ // Fold 32-bit sum to 16 bits\r
+ //\r
+ while (Sum >> 16) {\r
+ Sum = (Sum & 0xffff) + (Sum >> 16);\r
+\r
+ }\r
+\r
+ return (UINT16) Sum;\r
+}\r
+\r
+\r
+/**\r
+ Add two checksums.\r
+\r
+ @param Checksum1 The first checksum to be added.\r
+ @param Checksum2 The second checksum to be added.\r
+\r
+ @retval UINT16 The new checksum.\r
+\r
+**/\r
+UINT16\r
+NetAddChecksum (\r
+ IN UINT16 Checksum1,\r
+ IN UINT16 Checksum2\r
+ )\r
+{\r
+ UINT32 Sum;\r
+\r
+ Sum = Checksum1 + Checksum2;\r
+\r
+ //\r
+ // two UINT16 can only add up to a carry of 1.\r
+ //\r
+ if (Sum >> 16) {\r
+ Sum = (Sum & 0xffff) + 1;\r
+\r
+ }\r
+\r
+ return (UINT16) Sum;\r
+}\r
+\r
+\r
+/**\r
+ Compute the checksum for a NET_BUF.\r
+\r
+ @param Nbuf Pointer to the net buffer.\r
+\r
+ @retval UINT16 The computed checksum.\r
+\r
+**/\r
+UINT16\r
+NetbufChecksum (\r
+ IN NET_BUF *Nbuf\r
+ )\r
+{\r
+ NET_BLOCK_OP *BlockOp;\r
+ UINT32 Offset;\r
+ UINT16 TotalSum;\r
+ UINT16 BlockSum;\r
+ UINT32 Index;\r
+\r
+ NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);\r
+\r
+ TotalSum = 0;\r
+ Offset = 0;\r
+ BlockOp = Nbuf->BlockOp;\r
+\r
+ for (Index = 0; Index < Nbuf->BlockOpNum; Index++) {\r
+ if (BlockOp[Index].Size == 0) {\r
+ continue;\r
+ }\r
+\r
+ BlockSum = NetblockChecksum (BlockOp[Index].Head, BlockOp[Index].Size);\r
+\r
+ if (Offset & 0x01) {\r
+ //\r
+ // The checksum starts with an odd byte, swap\r
+ // the checksum before added to total checksum\r
+ //\r
+ BlockSum = NET_SWAP_SHORT (BlockSum);\r
+ }\r
+\r
+ TotalSum = NetAddChecksum (BlockSum, TotalSum);\r
+ Offset += BlockOp[Index].Size;\r
+ }\r
+\r
+ return TotalSum;\r
+}\r
+\r
+\r
+/**\r
+ Compute the checksum for TCP/UDP pseudo header.\r
+ Src, Dst are in network byte order. and Len is\r
+ in host byte order.\r
+\r
+ @param Src The source address of the packet.\r
+ @param Dst The destination address of the packet.\r
+ @param Proto The protocol type of the packet.\r
+ @param Len The length of the packet.\r
+\r
+ @retval UINT16 The computed checksum.\r
+\r
+**/\r
+UINT16\r
+NetPseudoHeadChecksum (\r
+ IN IP4_ADDR Src,\r
+ IN IP4_ADDR Dst,\r
+ IN UINT8 Proto,\r
+ IN UINT16 Len\r
+ )\r
+{\r
+ NET_PSEUDO_HDR Hdr;\r
+\r
+ //\r
+ // Zero the memory to relieve align problems\r
+ //\r
+ NetZeroMem (&Hdr, sizeof (Hdr));\r
+\r
+ Hdr.SrcIp = Src;\r
+ Hdr.DstIp = Dst;\r
+ Hdr.Protocol = Proto;\r
+ Hdr.Len = HTONS (Len);\r
+\r
+ return NetblockChecksum ((UINT8 *) &Hdr, sizeof (Hdr));\r
+}\r
--- /dev/null
+/** @file\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+\r
+Module Name:\r
+\r
+ Udp4Io.c\r
+\r
+Abstract:\r
+\r
+ Help functions to access UDP service, it is used by both the DHCP and MTFTP.\r
+\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Protocol/Udp4.h>\r
+\r
+#include <Library/UdpIoLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+UdpIoOnDgramSent (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ );\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+UdpIoOnDgramRcvd (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ );\r
+\r
+\r
+/**\r
+ Wrap a transmit request into a UDP_TX_TOKEN.\r
+\r
+ @param UdpIo The UdpIo port to send packet to\r
+ @param Packet The user's packet\r
+ @param EndPoint The local and remote access point\r
+ @param Gateway The overrided next hop\r
+ @param CallBack The function to call when transmission completed.\r
+ @param Context The opaque parameter to the call back\r
+\r
+ @return The wrapped transmission request or NULL if failed to allocate resources.\r
+\r
+**/\r
+STATIC\r
+UDP_TX_TOKEN *\r
+UdpIoWrapTx (\r
+ IN UDP_IO_PORT *UdpIo,\r
+ IN NET_BUF *Packet,\r
+ IN UDP_POINTS *EndPoint, OPTIONAL\r
+ IN IP4_ADDR Gateway,\r
+ IN UDP_IO_CALLBACK CallBack,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ UDP_TX_TOKEN *Token;\r
+ EFI_UDP4_COMPLETION_TOKEN *UdpToken;\r
+ EFI_UDP4_TRANSMIT_DATA *UdpTxData;\r
+ EFI_STATUS Status;\r
+ UINT32 Count;\r
+\r
+ Token = NetAllocatePool (sizeof (UDP_TX_TOKEN) +\r
+ sizeof (EFI_UDP4_FRAGMENT_DATA) * (Packet->BlockOpNum - 1));\r
+\r
+ if (Token == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Token->Signature = UDP_IO_TX_SIGNATURE;\r
+ NetListInit (&Token->Link);\r
+\r
+ Token->UdpIo = UdpIo;\r
+ Token->CallBack = CallBack;\r
+ Token->Packet = Packet;\r
+ Token->Context = Context;\r
+\r
+ UdpToken = &(Token->UdpToken);\r
+ UdpToken->Status = EFI_NOT_READY;\r
+\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ TPL_CALLBACK,\r
+ UdpIoOnDgramSent,\r
+ Token,\r
+ &UdpToken->Event\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ NetFreePool (Token);\r
+ return NULL;\r
+ }\r
+\r
+ UdpTxData = &Token->UdpTxData;\r
+ UdpToken->Packet.TxData = UdpTxData;\r
+\r
+ UdpTxData->UdpSessionData = NULL;\r
+ UdpTxData->GatewayAddress = NULL;\r
+\r
+ if (EndPoint != NULL) {\r
+ EFI_IP4 (Token->UdpSession.SourceAddress) = HTONL (EndPoint->LocalAddr);\r
+ EFI_IP4 (Token->UdpSession.DestinationAddress) = HTONL (EndPoint->RemoteAddr);\r
+ Token->UdpSession.SourcePort = EndPoint->LocalPort;\r
+ Token->UdpSession.DestinationPort = EndPoint->RemotePort;\r
+ UdpTxData->UdpSessionData = &Token->UdpSession;\r
+ }\r
+\r
+ if (Gateway != 0) {\r
+ EFI_IP4 (Token->Gateway) = HTONL (Gateway);\r
+ UdpTxData->GatewayAddress = &Token->Gateway;\r
+ }\r
+\r
+ UdpTxData->DataLength = Packet->TotalSize;\r
+ Count = Packet->BlockOpNum;\r
+ NetbufBuildExt (Packet, (NET_FRAGMENT *) UdpTxData->FragmentTable, &Count);\r
+ UdpTxData->FragmentCount = Count;\r
+\r
+ return Token;\r
+}\r
+\r
+\r
+/**\r
+ Free a UDP_TX_TOKEN. The event is closed and memory released.\r
+\r
+ @param Token The UDP_TX_TOKEN to release.\r
+\r
+ @return None\r
+\r
+**/\r
+VOID\r
+UdpIoFreeTxToken (\r
+ IN UDP_TX_TOKEN *Token\r
+ )\r
+{\r
+ gBS->CloseEvent (Token->UdpToken.Event);\r
+ NetFreePool (Token);\r
+}\r
+\r
+\r
+/**\r
+ Create a UDP_RX_TOKEN to wrap the request.\r
+\r
+ @param UdpIo The UdpIo to receive packets from\r
+ @param CallBack The function to call when receive finished.\r
+ @param Context The opaque parameter to the CallBack\r
+ @param HeadLen The head length to reserver for the packet.\r
+\r
+ @return The Wrapped request or NULL if failed to allocate resources.\r
+\r
+**/\r
+UDP_RX_TOKEN *\r
+UdpIoCreateRxToken (\r
+ IN UDP_IO_PORT *UdpIo,\r
+ IN UDP_IO_CALLBACK CallBack,\r
+ IN VOID *Context,\r
+ IN UINT32 HeadLen\r
+ )\r
+{\r
+ UDP_RX_TOKEN *Token;\r
+ EFI_STATUS Status;\r
+\r
+ Token = NetAllocatePool (sizeof (UDP_RX_TOKEN));\r
+\r
+ if (Token == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Token->Signature = UDP_IO_RX_SIGNATURE;\r
+ Token->UdpIo = UdpIo;\r
+ Token->CallBack = CallBack;\r
+ Token->Context = Context;\r
+ Token->HeadLen = HeadLen;\r
+\r
+ Token->UdpToken.Status = EFI_NOT_READY;\r
+ Token->UdpToken.Packet.RxData = NULL;\r
+\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ TPL_CALLBACK,\r
+ UdpIoOnDgramRcvd,\r
+ Token,\r
+ &Token->UdpToken.Event\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ NetFreePool (Token);\r
+ return NULL;\r
+ }\r
+\r
+ return Token;\r
+}\r
+\r
+\r
+/**\r
+ Free a receive request wrap.\r
+\r
+ @param Token The receive request to release.\r
+\r
+ @return None\r
+\r
+**/\r
+VOID\r
+UdpIoFreeRxToken (\r
+ IN UDP_RX_TOKEN *Token\r
+ )\r
+{\r
+ gBS->CloseEvent (Token->UdpToken.Event);\r
+ NetFreePool (Token);\r
+}\r
+\r
+\r
+/**\r
+ Create a UDP IO port to access the UDP service. It will\r
+ create and configure a UDP child.\r
+\r
+ @param Controller The controller that has the UDP service binding\r
+ protocol installed.\r
+ @param Image The image handle for the driver.\r
+ @param Configure The function to configure the created UDP child\r
+ @param Context The opaque parameter for the Configure funtion.\r
+\r
+ @return A point to just created UDP IO port or NULL if failed.\r
+\r
+**/\r
+UDP_IO_PORT *\r
+UdpIoCreatePort (\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_HANDLE Image,\r
+ IN UDP_IO_CONFIG Configure,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ UDP_IO_PORT *UdpIo;\r
+ EFI_STATUS Status;\r
+\r
+ ASSERT (Configure != NULL);\r
+\r
+ UdpIo = NetAllocatePool (sizeof (UDP_IO_PORT));\r
+\r
+ if (UdpIo == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ UdpIo->Signature = UDP_IO_SIGNATURE;\r
+ NetListInit (&UdpIo->Link);\r
+ UdpIo->RefCnt = 1;\r
+\r
+ UdpIo->Controller = Controller;\r
+ UdpIo->Image = Image;\r
+\r
+ NetListInit (&UdpIo->SentDatagram);\r
+ UdpIo->RecvRequest = NULL;\r
+ UdpIo->UdpHandle = NULL;\r
+\r
+ //\r
+ // Create a UDP child then open and configure it\r
+ //\r
+ Status = NetLibCreateServiceChild (\r
+ Controller,\r
+ Image,\r
+ &gEfiUdp4ServiceBindingProtocolGuid,\r
+ &UdpIo->UdpHandle\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto FREE_MEM;\r
+ }\r
+\r
+ Status = gBS->OpenProtocol (\r
+ UdpIo->UdpHandle,\r
+ &gEfiUdp4ProtocolGuid,\r
+ &UdpIo->Udp,\r
+ Image,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto FREE_CHILD;\r
+ }\r
+\r
+ if (EFI_ERROR (Configure (UdpIo, Context))) {\r
+ goto CLOSE_PROTOCOL;\r
+ }\r
+\r
+ Status = UdpIo->Udp->GetModeData (UdpIo->Udp, NULL, NULL, NULL, &UdpIo->SnpMode);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto CLOSE_PROTOCOL;\r
+ }\r
+\r
+ return UdpIo;\r
+\r
+CLOSE_PROTOCOL:\r
+ gBS->CloseProtocol (UdpIo->UdpHandle, &gEfiUdp4ProtocolGuid, Image, Controller);\r
+\r
+FREE_CHILD:\r
+ NetLibDestroyServiceChild (\r
+ Controller,\r
+ Image,\r
+ &gEfiUdp4ServiceBindingProtocolGuid,\r
+ UdpIo->UdpHandle\r
+ );\r
+\r
+FREE_MEM:\r
+ NetFreePool (UdpIo);\r
+ return NULL;\r
+}\r
+\r
+\r
+/**\r
+ Cancel all the sent datagram that pass the selection of ToCancel.\r
+ If ToCancel is NULL, all the datagrams are cancelled.\r
+\r
+ @param UdpIo The UDP IO port to cancel packet\r
+ @param IoStatus The IoStatus to return to the packet owners.\r
+ @param ToCancel The select funtion to test whether to cancel this\r
+ packet or not.\r
+ @param Context The opaque parameter to the ToCancel.\r
+\r
+ @return None\r
+\r
+**/\r
+STATIC\r
+VOID\r
+UdpIoCancelDgrams (\r
+ IN UDP_IO_PORT *UdpIo,\r
+ IN EFI_STATUS IoStatus,\r
+ IN UDP_IO_TO_CANCEL ToCancel, OPTIONAL\r
+ IN VOID *Context\r
+ )\r
+{\r
+ NET_LIST_ENTRY *Entry;\r
+ NET_LIST_ENTRY *Next;\r
+ UDP_TX_TOKEN *Token;\r
+\r
+ NET_LIST_FOR_EACH_SAFE (Entry, Next, &UdpIo->SentDatagram) {\r
+ Token = NET_LIST_USER_STRUCT (Entry, UDP_TX_TOKEN, Link);\r
+\r
+ if ((ToCancel == NULL) || (ToCancel (Token, Context))) {\r
+ NetListRemoveEntry (Entry);\r
+ UdpIo->Udp->Cancel (UdpIo->Udp, &Token->UdpToken);\r
+ Token->CallBack (Token->Packet, NULL, IoStatus, Token->Context);\r
+ UdpIoFreeTxToken (Token);\r
+ }\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Free the UDP IO port and all its related resources including\r
+ all the transmitted packet.\r
+\r
+ @param UdpIo The UDP IO port to free.\r
+\r
+ @retval EFI_SUCCESS The UDP IO port is freed.\r
+\r
+**/\r
+EFI_STATUS\r
+UdpIoFreePort (\r
+ IN UDP_IO_PORT *UdpIo\r
+ )\r
+{\r
+ UDP_RX_TOKEN *RxToken;\r
+\r
+ //\r
+ // Cancel all the sent datagram and receive requests. The\r
+ // callbacks of transmit requests are executed to allow the\r
+ // caller to release the resource. The callback of receive\r
+ // request are NOT executed. This is because it is most\r
+ // likely that the current user of the UDP IO port is closing\r
+ // itself.\r
+ //\r
+ UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);\r
+\r
+ if ((RxToken = UdpIo->RecvRequest) != NULL) {\r
+ UdpIo->RecvRequest = NULL;\r
+ UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);\r
+ UdpIoFreeRxToken (RxToken);\r
+ }\r
+\r
+ //\r
+ // Close then destory the UDP child\r
+ //\r
+ gBS->CloseProtocol (\r
+ UdpIo->UdpHandle,\r
+ &gEfiUdp4ProtocolGuid,\r
+ UdpIo->Image,\r
+ UdpIo->Controller\r
+ );\r
+\r
+ NetLibDestroyServiceChild (\r
+ UdpIo->Controller,\r
+ UdpIo->Image,\r
+ &gEfiUdp4ServiceBindingProtocolGuid,\r
+ UdpIo->UdpHandle\r
+ );\r
+\r
+ NetListRemoveEntry (&UdpIo->Link);\r
+ NetFreePool (UdpIo);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Clean up the UDP IO port. It will release all the transmitted\r
+ datagrams and receive request. It will also configure NULL the\r
+ UDP child.\r
+\r
+ @param UdpIo UDP IO port to clean up.\r
+\r
+ @return None\r
+\r
+**/\r
+VOID\r
+UdpIoCleanPort (\r
+ IN UDP_IO_PORT *UdpIo\r
+ )\r
+{\r
+ UDP_RX_TOKEN *RxToken;\r
+\r
+ //\r
+ // Cancel all the sent datagram and receive requests.\r
+ //\r
+ UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);\r
+\r
+ if ((RxToken = UdpIo->RecvRequest) != NULL) {\r
+ UdpIo->RecvRequest = NULL;\r
+ UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);\r
+ UdpIoFreeRxToken (RxToken);\r
+ }\r
+\r
+ UdpIo->Udp->Configure (UdpIo->Udp, NULL);\r
+}\r
+\r
+\r
+/**\r
+ The callback function when the packet is sent by UDP.\r
+ It will remove the packet from the local list then call\r
+ the packet owner's callback function.\r
+\r
+ @param Event The event signalled.\r
+ @param Context The UDP TX Token.\r
+\r
+ @return None\r
+\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+UdpIoOnDgramSent (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ UDP_TX_TOKEN *Token;\r
+\r
+ Token = (UDP_TX_TOKEN *) Context;\r
+ ASSERT (Token->Signature == UDP_IO_TX_SIGNATURE);\r
+\r
+ NetListRemoveEntry (&Token->Link);\r
+ Token->CallBack (Token->Packet, NULL, Token->UdpToken.Status, Token->Context);\r
+\r
+ UdpIoFreeTxToken (Token);\r
+}\r
+\r
+\r
+/**\r
+ Send a packet through the UDP IO port.\r
+\r
+ @param UdpIo The UDP IO Port to send the packet through\r
+ @param Packet The packet to send\r
+ @param EndPoint The local and remote access point\r
+ @param Gateway The gateway to use\r
+ @param CallBack The call back function to call when packet is\r
+ transmitted or failed.\r
+ @param Context The opque parameter to the CallBack\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet\r
+ @retval EFI_SUCCESS The packet is successfully delivered to UDP for\r
+ transmission.\r
+\r
+**/\r
+EFI_STATUS\r
+UdpIoSendDatagram (\r
+ IN UDP_IO_PORT *UdpIo,\r
+ IN NET_BUF *Packet,\r
+ IN UDP_POINTS *EndPoint, OPTIONAL\r
+ IN IP4_ADDR Gateway,\r
+ IN UDP_IO_CALLBACK CallBack,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ UDP_TX_TOKEN *Token;\r
+ EFI_STATUS Status;\r
+\r
+ Token = UdpIoWrapTx (UdpIo, Packet, EndPoint, Gateway, CallBack, Context);\r
+\r
+ if (Token == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Status = UdpIo->Udp->Transmit (UdpIo->Udp, &Token->UdpToken);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ UdpIoFreeTxToken (Token);\r
+ return Status;\r
+ }\r
+\r
+ NetListInsertHead (&UdpIo->SentDatagram, &Token->Link);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ The selection function to cancel a single sent datagram.\r
+\r
+ @param Token The UDP TX token to test againist.\r
+ @param Context The context\r
+\r
+ @return TRUE if the packet is to be cancelled, otherwise FALSE.\r
+\r
+**/\r
+STATIC\r
+BOOLEAN\r
+UdpIoCancelSingleDgram (\r
+ IN UDP_TX_TOKEN *Token,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ NET_BUF *Packet;\r
+\r
+ Packet = (NET_BUF *) Context;\r
+\r
+ if (Token->Packet == Packet) {\r
+ return TRUE;\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+\r
+/**\r
+ Cancel a single sent datagram.\r
+\r
+ @param UdpIo The UDP IO port to cancel the packet from\r
+ @param Packet The packet to cancel\r
+\r
+ @return None\r
+\r
+**/\r
+VOID\r
+UdpIoCancelSentDatagram (\r
+ IN UDP_IO_PORT *UdpIo,\r
+ IN NET_BUF *Packet\r
+ )\r
+{\r
+ UdpIoCancelDgrams (UdpIo, EFI_ABORTED, UdpIoCancelSingleDgram, Packet);\r
+}\r
+\r
+\r
+/**\r
+ Recycle the received UDP data.\r
+\r
+ @param Context The UDP_RX_TOKEN\r
+\r
+ @return None\r
+\r
+**/\r
+STATIC\r
+VOID\r
+UdpIoRecycleDgram (\r
+ IN VOID *Context\r
+ )\r
+{\r
+ UDP_RX_TOKEN *Token;\r
+\r
+ Token = (UDP_RX_TOKEN *) Context;\r
+ gBS->SignalEvent (Token->UdpToken.Packet.RxData->RecycleSignal);\r
+ UdpIoFreeRxToken (Token);\r
+}\r
+\r
+\r
+/**\r
+ The event handle for UDP receive request. It will build\r
+ a NET_BUF from the recieved UDP data, then deliver it\r
+ to the receiver.\r
+\r
+ @param Event The UDP receive request event\r
+ @param Context The UDP RX token.\r
+\r
+ @return None\r
+\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+UdpIoOnDgramRcvd (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ EFI_UDP4_COMPLETION_TOKEN *UdpToken;\r
+ EFI_UDP4_RECEIVE_DATA *UdpRxData;\r
+ EFI_UDP4_SESSION_DATA *UdpSession;\r
+ UDP_RX_TOKEN *Token;\r
+ UDP_POINTS Points;\r
+ NET_BUF *Netbuf;\r
+\r
+ Token = (UDP_RX_TOKEN *) Context;\r
+\r
+ ASSERT ((Token->Signature == UDP_IO_RX_SIGNATURE) &&\r
+ (Token == Token->UdpIo->RecvRequest));\r
+\r
+ //\r
+ // Clear the receive request first in case that the caller\r
+ // wants to restart the receive in the callback.\r
+ //\r
+ Token->UdpIo->RecvRequest = NULL;\r
+\r
+ UdpToken = &Token->UdpToken;\r
+ UdpRxData = UdpToken->Packet.RxData;\r
+\r
+ if (EFI_ERROR (UdpToken->Status) || (UdpRxData == NULL)) {\r
+ Token->CallBack (NULL, NULL, UdpToken->Status, Token->Context);\r
+ UdpIoFreeRxToken (Token);\r
+\r
+ goto ON_EXIT;\r
+ }\r
+\r
+ //\r
+ // Build a NET_BUF from the UDP receive data, then deliver it up.\r
+ //\r
+ Netbuf = NetbufFromExt (\r
+ (NET_FRAGMENT *) UdpRxData->FragmentTable,\r
+ UdpRxData->FragmentCount,\r
+ 0,\r
+ (UINT32) Token->HeadLen,\r
+ UdpIoRecycleDgram,\r
+ Token\r
+ );\r
+\r
+ if (Netbuf == NULL) {\r
+ gBS->SignalEvent (UdpRxData->RecycleSignal);\r
+ Token->CallBack (NULL, NULL, EFI_OUT_OF_RESOURCES, Token->Context);\r
+\r
+ UdpIoFreeRxToken (Token);\r
+ goto ON_EXIT;\r
+ }\r
+\r
+ UdpSession = &UdpRxData->UdpSession;\r
+ Points.LocalAddr = EFI_NTOHL (UdpSession->DestinationAddress);\r
+ Points.LocalPort = UdpSession->DestinationPort;\r
+ Points.RemoteAddr = EFI_NTOHL (UdpSession->SourceAddress);\r
+ Points.RemotePort = UdpSession->SourcePort;\r
+\r
+ Token->CallBack (Netbuf, &Points, EFI_SUCCESS, Token->Context);\r
+\r
+ON_EXIT:\r
+ return;\r
+}\r
+\r
+\r
+/**\r
+ Issue a receive request to the UDP IO port.\r
+\r
+ @param UdpIo The UDP IO port to recieve the packet from.\r
+ @param CallBack The call back function to execute when receive\r
+ finished.\r
+ @param Context The opque context to the call back\r
+ @param HeadLen The lenght of the application's header\r
+\r
+ @retval EFI_ALREADY_STARTED There is already a pending receive request. Only\r
+ one receive request is supported.\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource.\r
+ @retval EFI_SUCCESS The receive request is issued successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+UdpIoRecvDatagram (\r
+ IN UDP_IO_PORT *UdpIo,\r
+ IN UDP_IO_CALLBACK CallBack,\r
+ IN VOID *Context,\r
+ IN UINT32 HeadLen\r
+ )\r
+{\r
+ UDP_RX_TOKEN *Token;\r
+ EFI_STATUS Status;\r
+\r
+ if (UdpIo->RecvRequest != NULL) {\r
+ return EFI_ALREADY_STARTED;\r
+ }\r
+\r
+ Token = UdpIoCreateRxToken (UdpIo, CallBack, Context, HeadLen);\r
+\r
+ if (Token == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Status = UdpIo->Udp->Receive (UdpIo->Udp, &Token->UdpToken);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ UdpIoFreeRxToken (Token);\r
+ return Status;\r
+ }\r
+\r
+ UdpIo->RecvRequest = Token;\r
+ return EFI_SUCCESS;\r
+}\r
--- /dev/null
+#/** @file\r
+# Component name for module NetLib\r
+#\r
+# FIX ME!\r
+# Copyright (c) 2006, Intel Corporation. All right reserved.\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+[Defines]\r
+ INF_VERSION = 0x00010005\r
+ BASE_NAME = DxeUpdIoDxe\r
+ FILE_GUID = 7E615AA1-41EE-49d4-B7E9-1D7A60AA5C8D\r
+ MODULE_TYPE = DXE_DRIVER\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = UdpIoLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
+ EDK_RELEASE_VERSION = 0x00020000\r
+ EFI_SPECIFICATION_VERSION = 0x00020000\r
+\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+# VALID_ARCHITECTURES = IA32 X64 IPF EBC\r
+#\r
+\r
+[Sources.common]\r
+ DxeUdpIoLib.c\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
+ MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+[LibraryClasses]\r
+ MemoryAllocationLib\r
+ UefiLib\r
+ BaseLib\r
+ UefiBootServicesTableLib\r
+ UefiRuntimeServicesTableLib\r
+ BaseMemoryLib\r
+ DebugLib\r
+ PrintLib\r
+\r
+\r
+[Protocols]\r
+ gEfiIp4ProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverDiagnosticsProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiUdp4ServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiLoadedImageProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiIp4ServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverConfigurationProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiSimpleNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiDriverBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiUdp4ProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+ gEfiComponentNameProtocolGuid # PROTOCOL ALWAYS_CONSUMED\r
+\r
ScsiLib|$(WORKSPACE)/MdePkg/Library/UefiScsiLib/UefiScsiLib.inf\r
FrameworkHiiLib|$(WORKSPACE)/IntelFrameworkPkg/Library/FrameworkHiiLib/HiiLib.inf\r
UsbLib|$(WORKSPACE)/MdePkg/Library/UefiUsbLib/UefiUsbLib.inf\r
+ NetLib|$(WORKSPACE)/MdeModulePkg/Library/DxeNetLib/DxeNetLib.inf\r
+ IpIoLib|$(WORKSPACE)/MdeModulePkg/Library/DxeIpIoLib/DxeIpIoLib.inf\r
\r
[LibraryClasses.common.DXE_RUNTIME_DRIVER]\r
HobLib|$(WORKSPACE)/MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
$(WORKSPACE)/MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.inf\r
$(WORKSPACE)/MdeModulePkg/Library/EdkDxePrintLib/EdkDxePrintLib.inf\r
\r
+ $(WORKSPACE)/MdeModulePkg/Library/DxeNetLib/DxeNetLib.inf\r
+ $(WORKSPACE)/MdeModulePkg/Library/DxeIpIoLib/DxeIpIoLib.inf\r
+ $(WORKSPACE)/MdeModulePkg/Library/DxeUdpIoLib/DxeUdpIoLib.inf\r
+\r
+ $(WORKSPACE)/MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4ConfigDxe.inf\r
+\r
$(WORKSPACE)/MdeModulePkg/Application/HelloWorld/HelloWorld.inf\r
\r
$(WORKSPACE)/MdeModulePkg/Bus/Pci/AtapiPassThruDxe/AtapiPassThru.inf\r