len = read (Private->fd, Buffer, BufferSize);

/**
  Read BufferSize bytes from Lba into Buffer.

  This function reads the requested number of blocks from the device. All the
  blocks are read, or an error is returned.
  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
  non-blocking I/O is being used, the Event associated with this request will
  not be signaled.

  @param[in]       This       Indicates a pointer to the calling context.
  @param[in]       MediaId    Id of the media, changes every time the media is
                              replaced.
  @param[in]       Lba        The starting Logical Block Address to read from.
  @param[in, out]  Token      A pointer to the token associated with the transaction.
  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.
  @param[out]      Buffer     A pointer to the destination buffer for the data. The
                              caller is responsible for either having implicit or
                              explicit ownership of the buffer.

  @retval EFI_SUCCESS           The read request was queued if Token->Event is
                                not NULL.The data was read correctly from the
                                device if the Token->Event is NULL.
  @retval EFI_DEVICE_ERROR      The device reported an error while performing
                                the read.
  @retval EFI_NO_MEDIA          There is no media in the device.
  @retval EFI_MEDIA_CHANGED     The MediaId is not for the current media.
  @retval EFI_BAD_BUFFER_SIZE   The BufferSize parameter is not a multiple of the
                                intrinsic block size of the device.
  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
                                or the buffer is not on proper alignment.
  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a lack
                                of resources.
**/
EFI_STATUS
EmuBlockIoReadBlocks (
  IN     EMU_BLOCK_IO_PROTOCOL  *This,
  IN     UINT32                 MediaId,
  IN     EFI_LBA                LBA,
  IN OUT EFI_BLOCK_IO2_TOKEN    *Token,
  IN     UINTN                  BufferSize,
     OUT VOID                   *Buffer
  )
{
  EFI_STATUS              Status;
  EMU_BLOCK_IO_PRIVATE    *Private;
  ssize_t                 len;

  Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);

  Status  = EmuBlockIoReadWriteCommon (Private, MediaId, LBA, BufferSize, Buffer, "UnixReadBlocks");
  if (EFI_ERROR (Status)) {
    goto Done;
  }

  len = read (Private->fd, Buffer, BufferSize);
  if (len != BufferSize) {
    DEBUG ((EFI_D_INIT, "ReadBlocks: ReadFile failed.\n"));
    Status = EmuBlockIoError (Private);
    goto Done;
  }

  //
  // If we read then media is present.
  //
  Private->Media->MediaPresent = TRUE;
  Status = EFI_SUCCESS;

Done:
  if (Token != NULL) {
    if (Token->Event != NULL) {
      // Caller is responsible for signaling EFI Event
      Token->TransactionStatus = Status;
      return EFI_SUCCESS;
    }
  }
  return Status;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是完整的队列操作代码,包括驱动的初始化和退出函数: ``` #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/slab.h> struct msg { int data; int complete; }; struct ring_buffer { int head; int tail; struct msg *data; int size; unsigned int capacity; }; struct ring_buffer *ring_buffer_init(unsigned int capacity) { struct ring_buffer *rbuf = kmalloc(sizeof(struct ring_buffer), GFP_KERNEL); rbuf->capacity = capacity; rbuf->head = rbuf->size = 0; rbuf->tail = capacity - 1; rbuf->data = kmalloc(rbuf->capacity * sizeof(struct msg), GFP_KERNEL); printk(KERN_DEBUG "ring_buffer create successfully!\n"); return rbuf; } int ring_buffer_is_empty(struct ring_buffer *rbuf) { return (rbuf->size == 0); } int ring_buffer_is_full(struct ring_buffer *rbuf) { return (rbuf->size == rbuf->capacity); } void ring_buffer_in(struct ring_buffer *rbuf, struct msg msg) { if (ring_buffer_is_full(rbuf)) { return; } rbuf->tail = (rbuf->tail + 1) % rbuf->capacity; rbuf->data[rbuf->tail] = msg; rbuf->size = rbuf->size + 1; } struct msg ring_buffer_out(struct ring_buffer *rbuf) { struct msg rsv_msg; if (ring_buffer_is_empty(rbuf)) { pr_info("buffer is empty!\n"); rsv_msg.complete = -1; return rsv_msg; } rsv_msg = rbuf->data[rbuf->head]; rbuf->head = (rbuf->head + 1) % rbuf->capacity; rbuf->size = rbuf->size - 1; return rsv_msg; } void destroy_ring_buffer(struct ring_buffer *rbuf) { kfree(rbuf->data); kfree(rbuf); } static int __init my_driver_init(void) { struct ring_buffer *rbuf = ring_buffer_init(10); struct msg msg1 = {1, 0}; struct msg msg2 = {2, 0}; struct msg msg3 = {3, 0}; ring_buffer_in(rbuf, msg1); ring_buffer_in(rbuf, msg2); ring_buffer_in(rbuf, msg3); struct msg out1 = ring_buffer_out(rbuf); struct msg out2 = ring_buffer_out(rbuf); struct msg out3 = ring_buffer_out(rbuf); printk(KERN_DEBUG "out1: %d, out2: %d, out3: %d\n", out1.data, out2.data, out3.data); destroy_ring_buffer(rbuf); return 0; } static void __exit my_driver_exit(void) { printk(KERN_DEBUG "my_driver exit\n"); } module_init(my_driver_init); module_exit(my_driver_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple example Linux module."); MODULE_VERSION("0.01"); ``` 上面的代码在驱动初始化函数中创建了一个容量为 10 的环形队列,然后向队列中插入了三个元素,接着连续地从队列中取出三个元素并打印,最后在驱动退出函数中销毁了队列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值