devicepathtotext.c

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

C
1,553
字号
/*++

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:

  DevicePathToText.c

Abstract:

  DevicePathToText protocol as defined in the UEFI 2.0 specification.

--*/

#include "DevicePathDriver.h"

EFI_DEVICE_PATH_PROTOCOL *
UnpackDevicePath (
  IN EFI_DEVICE_PATH_PROTOCOL  *DevPath
  )
/*++

  Routine Description:
    Function unpacks a device path data structure so that all the nodes of a device path 
    are naturally aligned.

  Arguments:
    DevPath        - A pointer to a device path data structure

  Returns:
    If the memory for the device path is successfully allocated, then a pointer to the 
    new device path is returned.  Otherwise, NULL is returned.

--*/
{
  EFI_DEVICE_PATH_PROTOCOL  *Src;
  EFI_DEVICE_PATH_PROTOCOL  *Dest;
  EFI_DEVICE_PATH_PROTOCOL  *NewPath;
  UINTN                     Size;

  if (DevPath == NULL) {
    return NULL;
  }
  //
  // Walk device path and round sizes to valid boundries
  //
  Src   = DevPath;
  Size  = 0;
  for (;;) {
    Size += DevicePathNodeLength (Src);
    Size += ALIGN_SIZE (Size);

    if (IsDevicePathEnd (Src)) {
      break;
    }

    Src = (EFI_DEVICE_PATH_PROTOCOL *) NextDevicePathNode (Src);
  }
  //
  // Allocate space for the unpacked path
  //
  NewPath = EfiLibAllocateZeroPool (Size);
  if (NewPath != NULL) {

    ASSERT (((UINTN) NewPath) % MIN_ALIGNMENT_SIZE == 0);

    //
    // Copy each node
    //
    Src   = DevPath;
    Dest  = NewPath;
    for (;;) {
      Size = DevicePathNodeLength (Src);
      EfiCopyMem (Dest, Src, Size);
      Size += ALIGN_SIZE (Size);
      SetDevicePathNodeLength (Dest, Size);
      Dest->Type |= EFI_DP_TYPE_UNPACKED;
      Dest = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *) Dest) + Size);

      if (IsDevicePathEnd (Src)) {
        break;
      }

      Src = (EFI_DEVICE_PATH_PROTOCOL *) NextDevicePathNode (Src);
    }
  }

  return NewPath;
}

VOID *
ReallocatePool (
  IN VOID                 *OldPool,
  IN UINTN                OldSize,
  IN UINTN                NewSize
  )
/*++

  Routine Description:
    Adjusts the size of a previously allocated buffer.

  Arguments:
    OldPool               - A pointer to the buffer whose size is being adjusted.
    OldSize               - The size of the current buffer.
    NewSize               - The size of the new buffer.

  Returns:
    EFI_SUCEESS           - The requested number of bytes were allocated.
    EFI_OUT_OF_RESOURCES  - The pool requested could not be allocated.
    EFI_INVALID_PARAMETER - The buffer was invalid.

--*/
{
  VOID  *NewPool;

  NewPool = NULL;
  if (NewSize) {
    NewPool = EfiLibAllocateZeroPool (NewSize);
  }

  if (OldPool) {
    if (NewPool) {
      EfiCopyMem (NewPool, OldPool, OldSize < NewSize ? OldSize : NewSize);
    }

    gBS->FreePool (OldPool);
  }

  return NewPool;
}

CHAR16 *
CatPrint (
  IN OUT POOL_PRINT   *Str,
  IN CHAR16           *Fmt,
  ...
  )
/*++

  Routine Description:
    Concatenates a formatted unicode string to allocated pool.  
    The caller must free the resulting buffer.

  Arguments:
    Str         - Tracks the allocated pool, size in use, and 
                  amount of pool allocated.
    Fmt         - The format string

  Returns:
    Allocated buffer with the formatted string printed in it.  
    The caller must free the allocated buffer.   The buffer
    allocation is not packed.

--*/
{
  UINT16  *AppendStr;
  VA_LIST Args;
  UINTN   StrSize;

  AppendStr = EfiLibAllocateZeroPool (0x1000);
  if (AppendStr == NULL) {
    return Str->Str;
  }

  VA_START (Args, Fmt);
  VSPrint (AppendStr, 0x1000, Fmt, Args);
  VA_END (Args);
  if (NULL == Str->Str) {
    StrSize   = EfiStrSize (AppendStr);
    Str->Str  = EfiLibAllocateZeroPool (StrSize);
    ASSERT (Str->Str != NULL);
  } else {
    StrSize = EfiStrSize (AppendStr) + EfiStrSize (Str->Str) - sizeof (UINT16);
    Str->Str = ReallocatePool (
                Str->Str,
                EfiStrSize (Str->Str),
                StrSize
                );
    ASSERT (Str->Str != NULL);
  }

  Str->MaxLen = MAX_CHAR * sizeof (UINT16);
  if (StrSize < Str->MaxLen) {
    EfiStrCat (Str->Str, AppendStr);
    Str->Len = StrSize - sizeof (UINT16);
  }

  gBS->FreePool (AppendStr);
  return Str->Str;
}

VOID
DevPathToTextPci (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  PCI_DEVICE_PATH *Pci;

  Pci = DevPath;
  CatPrint (Str, L"Pci(%x,%x)", Pci->Function, Pci->Device);
}

VOID
DevPathToTextPccard (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  PCCARD_DEVICE_PATH  *Pccard;

  Pccard = DevPath;
  CatPrint (Str, L"PcCard(%x)", Pccard->FunctionNumber);
}

VOID
DevPathToTextMemMap (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  MEMMAP_DEVICE_PATH  *MemMap;

  MemMap = DevPath;
  CatPrint (
    Str,
    L"MemoryMapped(%lx,%lx)",
    MemMap->StartingAddress,
    MemMap->EndingAddress
    );
}

VOID
DevPathToTextVendor (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  VENDOR_DEVICE_PATH  *Vendor;
  CHAR16              *Type;
  UINTN               Index;
  UINT32              FlowControlMap;
  UINT16              Info;

  Vendor = (VENDOR_DEVICE_PATH *) DevPath;
  switch (DevicePathType (&Vendor->Header)) {
  case HARDWARE_DEVICE_PATH:
    Type = L"Hw";
    break;

  case MESSAGING_DEVICE_PATH:
    Type = L"Msg";
    if (AllowShortcuts) {
      if (EfiCompareGuid (&Vendor->Guid, &gEfiPcAnsiGuid)) {
        CatPrint (Str, L"VenPcAnsi()");
        return ;
      } else if (EfiCompareGuid (&Vendor->Guid, &gEfiVT100Guid)) {
        CatPrint (Str, L"VenVt100()");
        return ;
      } else if (EfiCompareGuid (&Vendor->Guid, &gEfiVT100PlusGuid)) {
        CatPrint (Str, L"VenVt100Plus()");
        return ;
      } else if (EfiCompareGuid (&Vendor->Guid, &gEfiVTUTF8Guid)) {
        CatPrint (Str, L"VenUft8()");
        return ;
      } else if (EfiCompareGuid (&Vendor->Guid, &mEfiDevicePathMessagingUartFlowControlGuid)) {
        FlowControlMap = (((UART_FLOW_CONTROL_DEVICE_PATH *) Vendor)->FlowControlMap);
        switch (FlowControlMap & 0x00000003) {
        case 0:
          CatPrint (Str, L"UartFlowCtrl(%s)", L"None");
          break;

        case 1:
          CatPrint (Str, L"UartFlowCtrl(%s)", L"Hardware");
          break;

        case 2:
          CatPrint (Str, L"UartFlowCtrl(%s)", L"XonXoff");
          break;

        default:
          break;
        }

        return ;
      } else if (EfiCompareGuid (&Vendor->Guid, &mEfiDevicePathMessagingSASGuid)) {
        CatPrint (
          Str,
          L"SAS(%lx,%lx,%x,",
          ((SAS_DEVICE_PATH *) Vendor)->SasAddress,
          ((SAS_DEVICE_PATH *) Vendor)->Lun,
          ((SAS_DEVICE_PATH *) Vendor)->RelativeTargetPort
          );
        Info = (((SAS_DEVICE_PATH *) Vendor)->DeviceTopology);
        if ((Info & 0x0f) == 0) {
          CatPrint (Str, L"NoTopology,0,0,0,");
        } else if (((Info & 0x0f) == 1) || ((Info & 0x0f) == 2)) {
          CatPrint (
            Str,
            L"%s,%s,%s,",
            (Info & (0x1 << 4)) ? L"SATA" : L"SAS",
            (Info & (0x1 << 5)) ? L"External" : L"Internal",
            (Info & (0x1 << 6)) ? L"Expanded" : L"Direct"
            );
          if ((Info & 0x0f) == 1) {
            CatPrint (Str, L"0,");
          } else {
            CatPrint (Str, L"%x,", (Info >> 8) & 0xff);
          }
        } else {
          CatPrint (Str, L"0,0,0,0,");
        }

        CatPrint (Str, L"%x)", ((SAS_DEVICE_PATH *) Vendor)->Reserved);
        return ;
      } else if (EfiCompareGuid (&Vendor->Guid, &gEfiDebugPortProtocolGuid)) {
        CatPrint (Str, L"DebugPort()");
        return ;
      } else {
        return ;
        //
        // reserved
        //
      }
    }
    break;

  case MEDIA_DEVICE_PATH:
    Type = L"Media";
    break;

  default:
    Type = L"?";
    break;
  }

  CatPrint (Str, L"Ven%s(%g,", Type, &Vendor->Guid);
  for (Index = 0; Index < DevicePathNodeLength (&Vendor->Header) - sizeof (VENDOR_DEVICE_PATH); Index++) {
    CatPrint (Str, L"%02x", ((VENDOR_DEVICE_PATH_WITH_DATA *) Vendor)->VendorDefinedData[Index]);
  }

  CatPrint (Str, L")");
}

VOID
DevPathToTextController (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  CONTROLLER_DEVICE_PATH  *Controller;

  Controller = DevPath;
  CatPrint (
    Str,
    L"Ctrl(%x)",
    Controller->Controller
    );
}

VOID
DevPathToTextAcpi (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  ACPI_HID_DEVICE_PATH  *Acpi;

  Acpi = DevPath;
  if ((Acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
    if (AllowShortcuts) {
      switch (EISA_ID_TO_NUM (Acpi->HID)) {
      case 0x0a03:
        CatPrint (Str, L"PciRoot(%x)", Acpi->UID);
        break;

      case 0x0604:
        CatPrint (Str, L"Floppy(%x)", Acpi->UID);
        break;

      case 0x0301:
        CatPrint (Str, L"Keyboard(%x)", Acpi->UID);
        break;

      case 0x0501:
        CatPrint (Str, L"Serial(%x)", Acpi->UID);
        break;

      case 0x0401:
        CatPrint (Str, L"ParallelPort(%x)", Acpi->UID);
        break;

      default:
        break;
      }

      return ;
    }

    CatPrint (Str, L"Acpi(PNP%04x,%x)", EISA_ID_TO_NUM (Acpi->HID), Acpi->UID);
  } else {
    CatPrint (Str, L"Acpi(%08x,%x)", Acpi->HID, Acpi->UID);
  }
}

#define NextStrA(a) ((UINT8 *) (((UINT8 *) (a)) + EfiAsciiStrLen (a) + 1))

VOID
DevPathToTextExtAcpi (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  ACPI_EXTENDED_HID_DEVICE_PATH_WITH_STR  *AcpiExt;

  AcpiExt = DevPath;

  if (AllowShortcuts) {
    if ((*(AcpiExt->HidUidCidStr) == '\0') &&
        (*(NextStrA (NextStrA (AcpiExt->HidUidCidStr))) == '\0') &&
        (AcpiExt->UID == 0)
        ) {
      if ((AcpiExt->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
        CatPrint (
          Str,
          L"AcpiExp(PNP%04x,%x,%a)",
          EISA_ID_TO_NUM (AcpiExt->HID),
          AcpiExt->CID,
          NextStrA (AcpiExt->HidUidCidStr)
          );
      } else {
        CatPrint (
          Str,
          L"AcpiExp(%08x,%x,%a)",
          AcpiExt->HID,
          AcpiExt->CID,
          NextStrA (AcpiExt->HidUidCidStr)
          );
      }
    }
    return ;
  }

  if ((AcpiExt->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
    CatPrint (
      Str,
      L"AcpiEx(PNP%04x,%x,%x,%a,%a,%a)",
      EISA_ID_TO_NUM (AcpiExt->HID),
      AcpiExt->CID,
      AcpiExt->UID,
      AcpiExt->HidUidCidStr,
      NextStrA (NextStrA (AcpiExt->HidUidCidStr)),
      NextStrA (AcpiExt->HidUidCidStr)
      );
  } else {
    CatPrint (
      Str,
      L"AcpiEx(%08x,%x,%x,%a,%a,%a)",
      AcpiExt->HID,
      AcpiExt->CID,
      AcpiExt->UID,
      AcpiExt->HidUidCidStr,
      NextStrA (NextStrA (AcpiExt->HidUidCidStr)),
      NextStrA (AcpiExt->HidUidCidStr)
      );
  }
}

VOID
DevPathToTextAdrAcpi (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,
  IN BOOLEAN         AllowShortcuts
  )
{
  ACPI_ADR_DEVICE_PATH    *AcpiAdr;
  UINT16                  Index;
  UINT16                  Length;
  UINT16                  AdditionalAdrCount;

  AcpiAdr            = DevPath;
  Length             = DevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *) AcpiAdr);
  AdditionalAdrCount = (Length - 8) / 4;

  CatPrint (Str, L"AcpiAdr(%x", AcpiAdr->ADR);
  for (Index = 0; Index < AdditionalAdrCount; Index++) {
    CatPrint (Str, L",%x", *(UINT32 *) ((UINT8 *) AcpiAdr + 8 + Index * 4));
  }
  CatPrint (Str, L")");
}

VOID
DevPathToTextAtapi (
  IN OUT POOL_PRINT  *Str,
  IN VOID            *DevPath,
  IN BOOLEAN         DisplayOnly,

⌨️ 快捷键说明

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