efi_block_device.c 5.3 KB

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