efi_block_device.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI block driver
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt
  6. *
  7. * The EFI uclass creates a handle for this driver and installs the
  8. * driver binding protocol on it.
  9. *
  10. * The EFI block driver binds to controllers implementing the block io
  11. * protocol.
  12. *
  13. * When the bind function of the EFI block driver is called it creates a
  14. * new U-Boot block device. It installs child handles for all partitions and
  15. * installs the simple file protocol on these.
  16. *
  17. * The read and write functions of the EFI block driver delegate calls to the
  18. * controller that it is bound to.
  19. *
  20. * A usage example is as following:
  21. *
  22. * U-Boot loads the iPXE snp.efi executable. iPXE connects an iSCSI drive and
  23. * exposes a handle with the block IO protocol. It calls ConnectController.
  24. *
  25. * Now the EFI block driver installs the partitions with the simple file
  26. * protocol.
  27. *
  28. * iPXE uses the simple file protocol to load Grub or the Linux Kernel.
  29. */
  30. #include <efi_driver.h>
  31. #include <dm/device-internal.h>
  32. #include <dm/root.h>
  33. /*
  34. * EFI attributes of the udevice handled by this driver.
  35. *
  36. * handle handle of the controller on which this driver is installed
  37. * io block io protocol proxied by this driver
  38. */
  39. struct efi_blk_priv {
  40. efi_handle_t handle;
  41. struct efi_block_io *io;
  42. };
  43. /*
  44. * Read from block device
  45. *
  46. * @dev device
  47. * @blknr first block to be read
  48. * @blkcnt number of blocks to read
  49. * @buffer output buffer
  50. * @return number of blocks transferred
  51. */
  52. static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  53. void *buffer)
  54. {
  55. struct efi_blk_priv *priv = dev->priv;
  56. struct efi_block_io *io = priv->io;
  57. efi_status_t ret;
  58. EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
  59. __func__, dev->name, blknr, blkcnt);
  60. ret = EFI_CALL(io->read_blocks(
  61. io, io->media->media_id, (u64)blknr,
  62. (efi_uintn_t)blkcnt *
  63. (efi_uintn_t)io->media->block_size, buffer));
  64. EFI_PRINT("%s: r = %u\n", __func__,
  65. (unsigned int)(ret & ~EFI_ERROR_MASK));
  66. if (ret != EFI_SUCCESS)
  67. return 0;
  68. return blkcnt;
  69. }
  70. /*
  71. * Write to block device
  72. *
  73. * @dev device
  74. * @blknr first block to be write
  75. * @blkcnt number of blocks to write
  76. * @buffer input buffer
  77. * @return number of blocks transferred
  78. */
  79. static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  80. const void *buffer)
  81. {
  82. struct efi_blk_priv *priv = dev->priv;
  83. struct efi_block_io *io = priv->io;
  84. efi_status_t ret;
  85. EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
  86. __func__, dev->name, blknr, blkcnt);
  87. ret = EFI_CALL(io->write_blocks(
  88. io, io->media->media_id, (u64)blknr,
  89. (efi_uintn_t)blkcnt *
  90. (efi_uintn_t)io->media->block_size,
  91. (void *)buffer));
  92. EFI_PRINT("%s: r = %u\n", __func__,
  93. (unsigned int)(ret & ~EFI_ERROR_MASK));
  94. if (ret != EFI_SUCCESS)
  95. return 0;
  96. return blkcnt;
  97. }
  98. /*
  99. * Create partions for the block device.
  100. *
  101. * @handle EFI handle of the block device
  102. * @dev udevice of the block device
  103. */
  104. static int efi_bl_bind_partitions(efi_handle_t handle, struct udevice *dev)
  105. {
  106. struct blk_desc *desc;
  107. const char *if_typename;
  108. desc = dev_get_uclass_platdata(dev);
  109. if_typename = blk_get_if_type_name(desc->if_type);
  110. return efi_disk_create_partitions(handle, desc, if_typename,
  111. desc->devnum, dev->name);
  112. }
  113. /*
  114. * Create a block device for a handle
  115. *
  116. * @handle handle
  117. * @interface block io protocol
  118. * @return 0 = success
  119. */
  120. static int efi_bl_bind(efi_handle_t handle, void *interface)
  121. {
  122. struct udevice *bdev, *parent = dm_root();
  123. int ret, devnum;
  124. char *name;
  125. struct efi_object *obj = efi_search_obj(handle);
  126. struct efi_block_io *io = interface;
  127. int disks;
  128. struct efi_blk_priv *priv;
  129. EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io);
  130. if (!obj)
  131. return -ENOENT;
  132. devnum = blk_find_max_devnum(IF_TYPE_EFI);
  133. if (devnum == -ENODEV)
  134. devnum = 0;
  135. else if (devnum < 0)
  136. return devnum;
  137. name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */
  138. if (!name)
  139. return -ENOMEM;
  140. sprintf(name, "efiblk#%d", devnum);
  141. /* Create driver model udevice for the EFI block io device */
  142. ret = blk_create_device(parent, "efi_blk", name, IF_TYPE_EFI, devnum,
  143. io->media->block_size,
  144. (lbaint_t)io->media->last_block, &bdev);
  145. if (ret)
  146. return ret;
  147. if (!bdev)
  148. return -ENOENT;
  149. /* Allocate priv */
  150. ret = device_probe(bdev);
  151. if (ret)
  152. return ret;
  153. EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
  154. priv = bdev->priv;
  155. priv->handle = handle;
  156. priv->io = interface;
  157. ret = blk_prepare_device(bdev);
  158. /* Create handles for the partions of the block device */
  159. disks = efi_bl_bind_partitions(handle, bdev);
  160. EFI_PRINT("Found %d partitions\n", disks);
  161. return 0;
  162. }
  163. /* Block device driver operators */
  164. static const struct blk_ops efi_blk_ops = {
  165. .read = efi_bl_read,
  166. .write = efi_bl_write,
  167. };
  168. /* Identify as block device driver */
  169. U_BOOT_DRIVER(efi_blk) = {
  170. .name = "efi_blk",
  171. .id = UCLASS_BLK,
  172. .ops = &efi_blk_ops,
  173. .priv_auto_alloc_size = sizeof(struct efi_blk_priv),
  174. };
  175. /* EFI driver operators */
  176. static const struct efi_driver_ops driver_ops = {
  177. .protocol = &efi_block_io_guid,
  178. .child_protocol = &efi_block_io_guid,
  179. .bind = efi_bl_bind,
  180. };
  181. /* Identify as EFI driver */
  182. U_BOOT_DRIVER(efi_block) = {
  183. .name = "EFI block driver",
  184. .id = UCLASS_EFI,
  185. .ops = &driver_ops,
  186. };