netlib.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,442 行 · 第 1/3 页

C
1,442
字号
NetMapIsEmpty (
  IN NET_MAP                *Map
  )
/*++

Routine Description:

  Test whether the netmap is empty

Arguments:

  Map - The net map to test

Returns:

  TRUE if the netmap is empty, otherwise FALSE.

--*/
{
  ASSERT (Map != NULL);
  return (BOOLEAN) (Map->Count == 0);
}

UINTN
NetMapGetCount (
  IN NET_MAP                *Map
  )
/*++

Routine Description:

  Return the number of the <Key, Value> pairs in the netmap.

Arguments:

  Map - The netmap to get the entry number

Returns:

  The entry number in the netmap.

--*/
{
  return Map->Count;
}

STATIC
NET_MAP_ITEM *
NetMapAllocItem (
  IN NET_MAP                *Map
  )
/*++

Routine Description:

  Allocate an item for the netmap. It will try to allocate
  a batch of items and return one.

Arguments:

  Map - The netmap to allocate item for

Returns:

  The allocated item or NULL

--*/
{
  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;
}

EFI_STATUS
NetMapInsertHead (
  IN NET_MAP                *Map,
  IN VOID                   *Key,
  IN VOID                   *Value    OPTIONAL
  )
/*++

Routine Description:

  Allocate an item to save the <Key, Value> pair to the head of the netmap.

Arguments:

  Map   - The netmap to insert into
  Key   - The user's key 
  Value - The user's value for the key

Returns:

  EFI_OUT_OF_RESOURCES - Failed to allocate the memory for the item
  EFI_SUCCESS          - The item is inserted to the head

--*/
{
  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;
}

EFI_STATUS
NetMapInsertTail (
  IN NET_MAP                *Map,
  IN VOID                   *Key,
  IN VOID                   *Value    OPTIONAL
  )
/*++

Routine Description:

  Allocate an item to save the <Key, Value> pair to the tail of the netmap.

Arguments:

  Map   - The netmap to insert into
  Key   - The user's key 
  Value - The user's value for the key

Returns:

  EFI_OUT_OF_RESOURCES - Failed to allocate the memory for the item
  EFI_SUCCESS          - The item is inserted to the tail

--*/
{
  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;
}

STATIC
BOOLEAN
NetItemInMap (
  IN NET_MAP                *Map,
  IN NET_MAP_ITEM           *Item
  )
/*++

Routine Description:

  Check whther the item is in the Map

Arguments:

  Map   - The netmap to search within
  Item  - The item to search

Returns:

  TRUE if the item is in the netmap, otherwise FALSE.

--*/
{
  NET_LIST_ENTRY            *ListEntry;

  NET_LIST_FOR_EACH (ListEntry, &Map->Used) {
    if (ListEntry == &Item->Link) {
      return TRUE;
    }
  }

  return FALSE;
}

NET_MAP_ITEM *
NetMapFindKey (
  IN  NET_MAP               *Map,
  IN  VOID                  *Key
  )
/*++

Routine Description:

  Find the key in the netmap

Arguments:

  Map - The netmap to search within
  Key - The key to search

Returns:

  The point to the item contains the Key, or NULL if Key isn't in the map.

--*/
{
  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;
}

VOID *
NetMapRemoveItem (
  IN  NET_MAP             *Map,
  IN  NET_MAP_ITEM        *Item,
  OUT VOID                **Value           OPTIONAL
  )
/*++

Routine Description:

  Remove the item from the netmap

Arguments:

  Map   - The netmap to remove the item from
  Item  - The item to remove
  Value - The variable to receive the value if not NULL

Returns:

  The key of the removed item.

--*/
{
  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;
}

VOID *
NetMapRemoveHead (
  IN  NET_MAP               *Map,
  OUT VOID                  **Value         OPTIONAL
  )
/*++

Routine Description:

  Remove the first entry on the netmap

Arguments:

  Map   - The netmap to remove the head from
  Value - The variable to receive the value if not NULL
  
Returns:

  The key of the item removed

--*/
{
  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;
}

VOID *
NetMapRemoveTail (
  IN  NET_MAP               *Map,
  OUT VOID                  **Value       OPTIONAL
  )
/*++

Routine Description:

  Remove the last entry on the netmap

Arguments:

  Map   - The netmap to remove the tail from
  Value - The variable to receive the value if not NULL
  
Returns:

  The key of the item removed


--*/
{
  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;
}

EFI_STATUS
NetMapIterate (
  IN NET_MAP                *Map,
  IN NET_MAP_CALLBACK       CallBack,
  IN VOID                   *Arg
  )
/*++

Routine Description:

  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.

Arguments:

  Map       - The Map to iterate through
  CallBack  - The callback function to call for each item.
  Arg       - The opaque parameter to the callback

Returns:

  It returns the CallBack's last return value.

--*/
{

  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;
}

EFI_STATUS
EFIAPI
NetLibDefaultUnload (
  IN EFI_HANDLE             ImageHandle
  )
/*++

Routine Description:

  This is the default unload handle for all the network drivers.

Arguments:

  ImageHandle - The drivers' driver image.

Returns:

  EFI_SUCCESS - The image is unloaded.
  Others      - Failed to unload the image.
  
--*/
{
  EFI_STATUS                        Status;
  EFI_HANDLE                        *DeviceHandleBuffer;
  UINTN                             DeviceHandleCount;
  UINTN                             Index;
  EFI_DRIVER_BINDING_PROTOCOL       *DriverBinding;
  EFI_COMPONENT_NAME_PROTOCOL       *ComponentName;
  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;
  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?