callback.c

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

C
613
字号
  if (MicroSeconds != 0) {
    gBS->Stall ((UINTN) MicroSeconds);
  }
}

/*
 *  IO routine for UNDI start CPB.
 */
VOID
snp_undi32_callback_memio (
  UINT64 UniqueId,
  UINT8  ReadOrWrite,
  UINT8  NumBytes,
  UINT64 Address,
  UINT64 BufferAddr
  )
/*++

Routine Description:
 This is a callback routine supplied to UNDI at undi_start time.
 This is the IO routine for UNDI3.1.

Arguments:
  ReadOrWrite - indicates read or write, IO or Memory
  NumBytes    - number of bytes to read or write
  Address     - IO or memory address to read from or write to
  BufferAddr  - memory location to read into or that contains the bytes
                to write

Returns:

--*/
{
  SNP_DRIVER                *snp;
  EFI_PCI_IO_PROTOCOL_WIDTH Width;

  snp   = (SNP_DRIVER *) (UINTN) UniqueId;

  Width = 0;
  switch (NumBytes) {
  case 2:
    Width = 1;
    break;

  case 4:
    Width = 2;
    break;

  case 8:
    Width = 3;
    break;
  }

  switch (ReadOrWrite) {
  case PXE_IO_READ:
    snp->IoFncs->Io.Read (
                      snp->IoFncs,
                      Width,
                      snp->IoBarIndex,      // BAR 1 (for 32bit regs), IO base address
                      Address,
                      1,                    // count
                      (VOID *) (UINTN) BufferAddr
                      );
    break;

  case PXE_IO_WRITE:
    snp->IoFncs->Io.Write (
                      snp->IoFncs,
                      Width,
                      snp->IoBarIndex,      // BAR 1 (for 32bit regs), IO base address
                      Address,
                      1,                    // count
                      (VOID *) (UINTN) BufferAddr
                      );
    break;

  case PXE_MEM_READ:
    snp->IoFncs->Mem.Read (
                      snp->IoFncs,
                      Width,
                      snp->MemoryBarIndex,  // BAR 0, Memory base address
                      Address,
                      1,                    // count
                      (VOID *) (UINTN) BufferAddr
                      );
    break;

  case PXE_MEM_WRITE:
    snp->IoFncs->Mem.Write (
                      snp->IoFncs,
                      Width,
                      snp->MemoryBarIndex,  // BAR 0, Memory base address
                      Address,
                      1,                    // count
                      (VOID *) (UINTN) BufferAddr
                      );
    break;
  }

  return ;
}

VOID
snp_undi32_callback_map (
  IN UINT64     UniqueId,
  IN UINT64     CpuAddr,
  IN UINT32     NumBytes,
  IN UINT32     Direction,
  IN OUT UINT64 DeviceAddrPtr
  )
/*++

Routine Description:
  This is a callback routine supplied to UNDI at undi_start time.
  UNDI call this routine when it has to map a CPU address to a device
  address.

Arguments:
  UniqueId      - This was supplied to UNDI at Undi_Start, SNP uses this to store
                  Undi interface context (Undi does not read or write this variable)
  CpuAddr       - Virtual address to be mapped!
  NumBytes      - size of memory to be mapped
  Direction     - direction of data flow for this memory's usage:
                  cpu->device, device->cpu or both ways
  DeviceAddrPtr - pointer to return the mapped device address

Returns:
  None

--*/
{
  EFI_PHYSICAL_ADDRESS          *DevAddrPtr;
  EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;
  UINTN                         BuffSize;
  SNP_DRIVER                    *snp;
  UINTN                         Index;
  EFI_STATUS                    Status;

  BuffSize    = (UINTN) NumBytes;
  snp         = (SNP_DRIVER *) (UINTN) UniqueId;
  DevAddrPtr  = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr;

  if (CpuAddr == 0) {
    *DevAddrPtr = 0;
    return ;
  }

  switch (Direction) {
  case TO_AND_FROM_DEVICE:
    DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;
    break;

  case FROM_DEVICE:
    DirectionFlag = EfiPciIoOperationBusMasterWrite;
    break;

  case TO_DEVICE:
    DirectionFlag = EfiPciIoOperationBusMasterRead;
    break;

  default:
    *DevAddrPtr = 0;
    //
    // any non zero indicates error!
    //
    return ;
  }
  //
  // find an unused map_list entry
  //
  for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
    if (snp->map_list[Index].virt == 0) {
      break;
    }
  }

  if (Index >= MAX_MAP_LENGTH) {
    SNP_PRINT (L"SNP maplist is FULL\n");
    *DevAddrPtr = 0;
    return ;
  }

  snp->map_list[Index].virt = (EFI_PHYSICAL_ADDRESS) CpuAddr;

  Status = snp->IoFncs->Map (
                          snp->IoFncs,
                          DirectionFlag,
                          (VOID *) (UINTN) CpuAddr,
                          &BuffSize,
                          DevAddrPtr,
                          &(snp->map_list[Index].map_cookie)
                          );
  if (Status != EFI_SUCCESS) {
    *DevAddrPtr               = 0;
    snp->map_list[Index].virt = 0;
  }

  return ;
}

VOID
snp_undi32_callback_unmap (
  IN UINT64 UniqueId,
  IN UINT64 CpuAddr,
  IN UINT32 NumBytes,
  IN UINT32 Direction,
  IN UINT64 DeviceAddr
  )
/*++

Routine Description:
 This is a callback routine supplied to UNDI at undi_start time.
 UNDI call this routine when it wants to unmap an address that was previously
 mapped using map callback

Arguments:
 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
            Undi interface context (Undi does not read or write this variable)
 CpuAddr  - Virtual address that was mapped!
 NumBytes - size of memory mapped
 Direction- direction of data flow for this memory's usage:
            cpu->device, device->cpu or both ways
 DeviceAddr - the mapped device address

Returns:

--*/
{
  SNP_DRIVER  *snp;
  UINT16      Index;

  snp = (SNP_DRIVER *) (UINTN) UniqueId;

  for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
    if (snp->map_list[Index].virt == CpuAddr) {
      break;
    }
  }

  if (Index >= MAX_MAP_LENGTH)
  {
#if SNP_DEBUG
    Print (L"SNP could not find a mapping, failed to unmap.\n");
#endif
    return ;
  }

  snp->IoFncs->Unmap (snp->IoFncs, snp->map_list[Index].map_cookie);
  snp->map_list[Index].virt       = 0;
  snp->map_list[Index].map_cookie = NULL;
  return ;
}

VOID
snp_undi32_callback_sync (
  UINT64 UniqueId,
  UINT64 CpuAddr,
  UINT32 NumBytes,
  UINT32 Direction,
  UINT64 DeviceAddr
  )
/*++

Routine Description:
 This is a callback routine supplied to UNDI at undi_start time.
 UNDI call this routine when it wants synchronize the virtual buffer contents
 with the mapped buffer contents. The virtual and mapped buffers need not
 correspond to the same physical memory (especially if the virtual address is
 > 4GB). Depending on the direction for which the buffer is mapped, undi will
 need to synchronize their contents whenever it writes to/reads from the buffer
 using either the cpu address or the device address.

 EFI does not provide a sync call, since virt=physical, we sould just do
 the synchronization ourself here!

Arguments:
 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
            Undi interface context (Undi does not read or write this variable)
 CpuAddr  - Virtual address that was mapped!
 NumBytes - size of memory mapped
 Direction- direction of data flow for this memory's usage:
            cpu->device, device->cpu or both ways
 DeviceAddr - the mapped device address

Returns:

--*/
{
  if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {
    return ;

  }

  switch (Direction) {
  case FROM_DEVICE:
    EfiCopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes);
    break;

  case TO_DEVICE:
    EfiCopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes);
    break;
  }

  return ;
}

⌨️ 快捷键说明

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