-extern IP4_ADDR mIp4AllMasks [IP4_MASK_NUM];
-
-
-extern EFI_IPv4_ADDRESS mZeroIp4Addr;
-
-#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))
-//
-// 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) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)
-
-
-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 CHAR16 **MacString
- );
-
-VOID
-NetLibCreateIPv4DPathNode (
- IN OUT IPv4_DEVICE_PATH *Node,
- IN EFI_HANDLE Controller,
- IN IP4_ADDR LocalIp,
- IN UINT16 LocalPort,
- IN IP4_ADDR RemoteIp,
- IN UINT16 RemotePort,
- IN UINT16 Protocol,
- IN BOOLEAN UseDefaultAddress
- );
-
-EFI_HANDLE
-NetLibGetNicHandle (
- IN EFI_HANDLE Controller,
- IN EFI_GUID *ProtocolGuid
- );
-
-EFI_STATUS
-EFIAPI
-NetLibDefaultUnload (
- IN EFI_HANDLE ImageHandle
- );
-
-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
- );
-
-//\r
-// The debug level definition. This value is also used as the\r
-// syslog's servity level. Don't change it.\r
-//\r
-enum {\r
- NETDEBUG_LEVEL_TRACE = 5,\r
- NETDEBUG_LEVEL_WARNING = 4,\r
- NETDEBUG_LEVEL_ERROR = 3,\r
-};\r
-\r
-#ifdef EFI_NETWORK_STACK_DEBUG\r
-\r
-//\r
-// The debug output expects the ASCII format string, Use %a to print ASCII\r
-// string, and %s to print UNICODE string. PrintArg must be enclosed in ().\r
-// For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name));\r
-//\r
-#define NET_DEBUG_TRACE(Module, PrintArg) \\r
- NetDebugOutput ( \\r
- NETDEBUG_LEVEL_TRACE, \\r
- Module, \\r
- __FILE__, \\r
- __LINE__, \\r
- NetDebugASPrint PrintArg \\r
- )\r
-\r
-#define NET_DEBUG_WARNING(Module, PrintArg) \\r
- NetDebugOutput ( \\r
- NETDEBUG_LEVEL_WARNING, \\r
- Module, \\r
- __FILE__, \\r
- __LINE__, \\r
- NetDebugASPrint PrintArg \\r
- )\r
-\r
-#define NET_DEBUG_ERROR(Module, PrintArg) \\r
- NetDebugOutput ( \\r
- NETDEBUG_LEVEL_ERROR, \\r
- Module, \\r
- __FILE__, \\r
- __LINE__, \\r
- NetDebugASPrint PrintArg \\r
- )\r
-\r
-#else\r
-#define NET_DEBUG_TRACE(Module, PrintString)\r
-#define NET_DEBUG_WARNING(Module, PrintString)\r
-#define NET_DEBUG_ERROR(Module, PrintString)\r
-#endif\r
+extern IP4_ADDR gIp4AllMasks [IP4_MASK_NUM];\r
+\r
+\r
+extern EFI_IPv4_ADDRESS mZeroIp4Addr;\r
+\r
+#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))\r
+#define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))\r
+#define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))\r
+#define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))\r
+\r
+#define TICKS_PER_MS 10000U\r
+#define TICKS_PER_SECOND 10000000U\r
+\r
+#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)\r
+\r
+/**\r
+ Extract a UINT32 from a byte stream, then convert it to host\r
+ byte order. Use this function to avoid alignment error.\r
+\r
+ @param[in] Buf The buffer to extract the UINT32.\r
+\r
+ @return The UINT32 extracted.\r
+\r
+**/\r
+UINT32\r
+EFIAPI\r
+NetGetUint32 (\r
+ IN UINT8 *Buf\r
+ );\r
+\r
+/**\r
+ Put a UINT32 to the byte stream. Convert it from host byte order\r
+ to network byte order before putting.\r
+\r
+ @param[in, out] Buf The buffer to put the UINT32.\r
+ @param[in] Data The data to put.\r
+ \r
+**/\r
+VOID\r
+EFIAPI\r
+NetPutUint32 (\r
+ IN OUT UINT8 *Buf,\r
+ IN UINT32 Data\r
+ );\r
+\r
+/**\r
+ Initialize a random seed using current time.\r
+\r
+ @return The random seed initialized with current time.\r
+\r
+**/\r
+UINT32\r
+EFIAPI\r
+NetRandomInitSeed (\r
+ VOID\r
+ );\r
+\r
+\r
+#define NET_LIST_USER_STRUCT(Entry, Type, Field) \\r
+ BASE_CR(Entry, Type, Field)\r
+\r
+#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \\r
+ CR(Entry, Type, Field, Sig)\r
+\r
+//\r
+// Iterate through the doule linked list. It is NOT delete safe\r
+//\r
+#define NET_LIST_FOR_EACH(Entry, ListHead) \\r
+ for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)\r
+\r
+//\r
+// Iterate through the doule linked list. This is delete-safe.\r
+// Don't touch NextEntry. Also, don't use this macro if list\r
+// entries other than the Entry may be deleted when processing\r
+// the current Entry.\r
+//\r
+#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \\r
+ for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \\r
+ Entry != (ListHead); \\r
+ Entry = NextEntry, NextEntry = Entry->ForwardLink \\r
+ )\r
+\r
+//\r
+// Make sure the list isn't empty before get the frist/last record.\r
+//\r
+#define NET_LIST_HEAD(ListHead, Type, Field) \\r
+ NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)\r
+\r
+#define NET_LIST_TAIL(ListHead, Type, Field) \\r
+ NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)\r
+\r
+\r
+/**\r
+ Remove the first entry on the list.\r
+\r
+ @param[in, out] Head The list header.\r
+\r
+ @return The entry that is removed from the list, NULL if the list is empty.\r
+\r
+**/\r
+LIST_ENTRY *\r
+EFIAPI\r
+NetListRemoveHead (\r
+ IN OUT LIST_ENTRY *Head\r
+ );\r
+\r
+/**\r
+ Remove the last entry on the list.\r
+\r
+ @param[in, out] Head The list head.\r
+\r
+ @return The entry that is removed from the list, NULL if the list is empty.\r
+\r
+**/\r
+LIST_ENTRY *\r
+EFIAPI\r
+NetListRemoveTail (\r
+ IN OUT LIST_ENTRY *Head\r
+ );\r
+\r
+/**\r
+ Insert the NewEntry after the PrevEntry.\r
+\r
+ @param[in, out] PrevEntry The previous entry to insert after.\r
+ @param[in, out] NewEntry The new entry to insert.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+NetListInsertAfter (\r
+ IN OUT LIST_ENTRY *PrevEntry,\r
+ IN OUT LIST_ENTRY *NewEntry\r
+ );\r
+\r
+/**\r
+ Insert the NewEntry before the PostEntry.\r
+\r
+ @param[in, out] PostEntry The entry to insert before.\r
+ @param[in, out] NewEntry The new entry to insert.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+NetListInsertBefore (\r
+ IN OUT LIST_ENTRY *PostEntry,\r
+ IN OUT LIST_ENTRY *NewEntry\r
+ );\r
+\r
+\r
+//\r
+// Object container: EFI network stack spec defines various kinds of\r
+// tokens. The drivers can share code to manage those objects.\r
+//\r
+typedef struct {\r
+ LIST_ENTRY Link;\r
+ VOID *Key;\r
+ VOID *Value;\r
+} NET_MAP_ITEM;\r
+\r
+typedef struct {\r
+ LIST_ENTRY Used;\r
+ LIST_ENTRY Recycled;\r
+ UINTN Count;\r
+} NET_MAP;\r
+\r
+#define NET_MAP_INCREAMENT 64\r
+\r
+/**\r
+ Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
+\r
+ @param[in, out] Map The netmap to initialize.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+NetMapInit (\r
+ IN OUT NET_MAP *Map\r
+ );\r
+\r
+/**\r
+ To clean up the netmap, that is, release allocated memories.\r
+\r
+ @param[in, out] Map The netmap to clean up.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+NetMapClean (\r
+ IN OUT NET_MAP *Map\r
+ );\r
+\r
+/**\r
+ Test whether the netmap is empty.\r
+\r
+ @param[in] Map The net map to test.\r
+\r
+ @return TRUE if the netmap is empty, otherwise FALSE.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+NetMapIsEmpty (\r
+ IN NET_MAP *Map\r
+ );\r
+\r
+/**\r
+ Return the number of the <Key, Value> pairs in the netmap.\r
+\r
+ @param[in] Map The netmap to get the entry number.\r
+\r
+ @return The entry number in the netmap.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+NetMapGetCount (\r
+ IN NET_MAP *Map\r
+ );\r
+\r
+/**\r
+ Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
+\r
+ @param[in, out] Map The netmap to insert into.\r
+ @param[in] Key The user's key.\r
+ @param[in] Value The user's value for the key.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
+ @retval EFI_SUCCESS The item is inserted to the head.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetMapInsertHead (\r
+ IN OUT NET_MAP *Map,\r
+ IN VOID *Key,\r
+ IN VOID *Value OPTIONAL\r
+ );\r
+\r
+/**\r
+ Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
+\r
+ @param[in, out] Map The netmap to insert into.\r
+ @param[in] Key The user's key.\r
+ @param[in] Value The user's value for the key.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
+ @retval EFI_SUCCESS The item is inserted to the tail.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetMapInsertTail (\r
+ IN OUT NET_MAP *Map,\r
+ IN VOID *Key,\r
+ IN VOID *Value OPTIONAL\r
+ );\r
+\r
+/**\r
+ Find the key in the netmap.\r
+\r
+ @param[in] Map The netmap to search within.\r
+ @param[in] Key The key to search.\r
+\r
+ @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
+\r
+**/\r
+NET_MAP_ITEM *\r
+EFIAPI\r
+NetMapFindKey (\r
+ IN NET_MAP *Map,\r
+ IN VOID *Key\r
+ );\r
+\r
+/**\r
+ Remove the item from the netmap.\r
+\r
+ @param[in, out] Map The netmap to remove the item from.\r
+ @param[in, out] Item The item to remove.\r
+ @param[out] Value The variable to receive the value if not NULL.\r
+\r
+ @return The key of the removed item.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+NetMapRemoveItem (\r
+ IN OUT NET_MAP *Map,\r
+ IN OUT NET_MAP_ITEM *Item,\r
+ OUT VOID **Value OPTIONAL\r
+ );\r
+\r
+/**\r
+ Remove the first entry on the netmap.\r
+\r
+ @param[in, out] Map The netmap to remove the head from.\r
+ @param[out] Value The variable to receive the value if not NULL.\r
+\r
+ @return The key of the item removed.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+NetMapRemoveHead (\r
+ IN OUT NET_MAP *Map,\r
+ OUT VOID **Value OPTIONAL\r
+ );\r
+\r
+/**\r
+ Remove the last entry on the netmap.\r
+\r
+ @param[in, out] Map The netmap to remove the tail from.\r
+ @param[out] Value The variable to receive the value if not NULL.\r
+\r
+ @return The key of the item removed.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+NetMapRemoveTail (\r
+ IN OUT NET_MAP *Map,\r
+ OUT VOID **Value OPTIONAL\r
+ );\r
+\r
+typedef\r
+EFI_STATUS\r
+(*NET_MAP_CALLBACK) (\r
+ IN NET_MAP *Map,\r
+ IN NET_MAP_ITEM *Item,\r
+ IN VOID *Arg\r
+ );\r
+\r
+/**\r
+ Iterate through the netmap and call CallBack for each item. It will\r
+ contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
+ from the loop. It returns the CallBack's last return value. This\r
+ function is delete safe for the current item.\r
+\r
+ @param[in] Map The Map to iterate through.\r
+ @param[in] CallBack The callback function to call for each item.\r
+ @param[in] Arg The opaque parameter to the callback.\r
+\r
+ @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item\r
+ return EFI_SUCCESS.\r
+ @retval Others It returns the CallBack's last return value.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetMapIterate (\r
+ IN NET_MAP *Map,\r
+ IN NET_MAP_CALLBACK CallBack,\r
+ IN VOID *Arg\r
+ );\r
+\r
+\r
+//\r
+// Helper functions to implement driver binding and service binding protocols.\r
+//\r
+/**\r
+ Create a child of the service that is identified by ServiceBindingGuid.\r
+\r
+ @param[in] Controller The controller which has the service installed.\r
+ @param[in] Image The image handle used to open service.\r
+ @param[in] ServiceBindingGuid The service's Guid.\r
+ @param[in, out] ChildHandle The handle to receive the create child\r
+\r
+ @retval EFI_SUCCESS The child is successfully created.\r
+ @retval Others Failed to create the child.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetLibCreateServiceChild (\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_HANDLE Image,\r
+ IN EFI_GUID *ServiceBindingGuid,\r
+ IN OUT EFI_HANDLE *ChildHandle\r
+ );\r
+\r
+/**\r
+ Destory a child of the service that is identified by ServiceBindingGuid.\r
+\r
+ @param[in] Controller The controller which has the service installed.\r
+ @param[in] Image The image handle used to open service.\r
+ @param[in] ServiceBindingGuid The service's Guid.\r
+ @param[in] ChildHandle The child to destory\r
+\r
+ @retval EFI_SUCCESS The child is successfully destoried.\r
+ @retval Others Failed to destory the child.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetLibDestroyServiceChild (\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_HANDLE Image,\r
+ IN EFI_GUID *ServiceBindingGuid,\r
+ IN EFI_HANDLE ChildHandle\r
+ );\r
+\r
+/**\r
+ Convert the mac address of the simple network protocol installed on\r
+ SnpHandle to a unicode string. Callers are responsible for freeing the\r
+ string storage.\r
+\r
+ @param[in] SnpHandle The handle where the simple network protocol is\r
+ installed on.\r
+ @param[in] ImageHandle The image handle used to act as the agent handle to\r
+ get the simple network protocol.\r
+ @param[out] MacString The pointer to store the address of the string\r
+ representation of the mac address.\r
+ \r
+ @retval EFI_SUCCESS Convert the mac address a unicode string successfully.\r
+ @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r
+ @retval Others Failed to open the simple network protocol.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetLibGetMacString (\r
+ IN EFI_HANDLE SnpHandle,\r
+ IN EFI_HANDLE ImageHandle,\r
+ OUT CHAR16 **MacString\r
+ );\r
+\r
+/**\r
+ Create an IPv4 device path node.\r
+\r
+ @param[in, out] Node Pointer to the IPv4 device path node.\r
+ @param[in] Controller The handle where the NIC IP4 config protocol resides.\r
+ @param[in] LocalIp The local IPv4 address.\r
+ @param[in] LocalPort The local port.\r
+ @param[in] RemoteIp The remote IPv4 address.\r
+ @param[in] RemotePort The remote port.\r
+ @param[in] Protocol The protocol type in the IP header.\r
+ @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+NetLibCreateIPv4DPathNode (\r
+ IN OUT IPv4_DEVICE_PATH *Node,\r
+ IN EFI_HANDLE Controller,\r
+ IN IP4_ADDR LocalIp,\r
+ IN UINT16 LocalPort,\r
+ IN IP4_ADDR RemoteIp,\r
+ IN UINT16 RemotePort,\r
+ IN UINT16 Protocol,\r
+ IN BOOLEAN UseDefaultAddress\r
+ );\r
+\r
+/**\r
+ Find the UNDI/SNP handle from controller and protocol GUID.\r
+ For example, IP will open a MNP child to transmit/receive\r
+ packets, when MNP is stopped, IP should also be stopped. IP\r
+ needs to find its own private data which is related the IP's\r
+ service binding instance that is install on UNDI/SNP handle.\r
+ Now, the controller is either a MNP or ARP child handle. But\r
+ IP opens these handle BY_DRIVER, use that info, we can get the\r
+ UNDI/SNP handle.\r
+\r
+ @param[in] Controller Then protocol handle to check.\r
+ @param[in] ProtocolGuid The protocol that is related with the handle.\r
+\r
+ @return The UNDI/SNP handle or NULL for errors.\r
+\r
+**/\r
+EFI_HANDLE\r
+EFIAPI\r
+NetLibGetNicHandle (\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_GUID *ProtocolGuid\r
+ );\r
+\r
+/**\r
+ Add a Deferred Procedure Call to the end of the DPC queue.\r
+\r
+ @param[in] DpcTpl The EFI_TPL that the DPC should be invoked.\r
+ @param[in] DpcProcedure Pointer to the DPC's function.\r
+ @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure\r
+ when DpcProcedure is invoked.\r
+\r
+ @retval EFI_SUCCESS The DPC was queued.\r
+ @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL, or DpcProcedure\r
+ is NULL.\r
+ @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
+ add the DPC to the queue.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetLibQueueDpc (\r
+ IN EFI_TPL DpcTpl,\r
+ IN EFI_DPC_PROCEDURE DpcProcedure,\r
+ IN VOID *DpcContext OPTIONAL\r
+ );\r
+\r
+/**\r
+ Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl\r
+ value greater than or equal to the current TPL are invoked in the order that\r
+ they were queued. DPCs with higher DpcTpl values are invoked before DPCs with\r
+ lower DpcTpl values.\r