efi_block_device.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <common.h>
  31. #include <blk.h>
  32. #include <dm.h>
  33. #include <efi_driver.h>
  34. #include <malloc.h>
  35. #include <dm/device-internal.h>
  36. #include <dm/root.h>
  37. /*
  38. * EFI attributes of the udevice handled by this driver.
  39. *
  40. * handle handle of the controller on which this driver is installed
  41. * io block io protocol proxied by this driver
  42. */
  43. struct efi_blk_plat {
  44. efi_handle_t handle;
  45. struct efi_block_io *io;
  46. };
  47. /**
  48. * Read from block device
  49. *
  50. * @dev: device
  51. * @blknr: first block to be read
  52. * @blkcnt: number of blocks to read
  53. * @buffer: output buffer
  54. * Return: number of blocks transferred
  55. */
  56. static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  57. void *buffer)
  58. {
  59. struct efi_blk_plat *plat = dev_get_plat(dev);
  60. struct efi_block_io *io = plat->io;
  61. efi_status_t ret;
  62. EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
  63. __func__, dev->name, blknr, blkcnt);
  64. ret = EFI_CALL(io->read_blocks(
  65. io, io->media->media_id, (u64)blknr,
  66. (efi_uintn_t)blkcnt *
  67. (efi_uintn_t)io->media->block_size, buffer));
  68. EFI_PRINT("%s: r = %u\n", __func__,
  69. (unsigned int)(ret & ~EFI_ERROR_MASK));
  70. if (ret != EFI_SUCCESS)
  71. return 0;
  72. return blkcnt;
  73. }
  74. /**
  75. * Write to block device
  76. *
  77. * @dev: device
  78. * @blknr: first block to be write
  79. * @blkcnt: number of blocks to write
  80. * @buffer: input buffer
  81. * Return: number of blocks transferred
  82. */
  83. static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  84. const void *buffer)
  85. {
  86. struct efi_blk_plat *plat = dev_get_plat(dev);
  87. struct efi_block_io *io = plat->io;
  88. efi_status_t ret;
  89. EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
  90. __func__, dev->name, blknr, blkcnt);
  91. ret = EFI_CALL(io->write_blocks(
  92. io, io->media->media_id, (u64)blknr,
  93. (efi_uintn_t)blkcnt *
  94. (efi_uintn_t)io->media->block_size,
  95. (void *)buffer));
  96. EFI_PRINT("%s: r = %u\n", __func__,
  97. (unsigned int)(ret & ~EFI_ERROR_MASK));
  98. if (ret != EFI_SUCCESS)
  99. return 0;
  100. return blkcnt;
  101. }
  102. /**
  103. * Create partions for the block device.
  104. *
  105. * @handle: EFI handle of the block device
  106. * @dev: udevice of the block device
  107. * Return: number of partitions created
  108. */
  109. static int efi_bl_bind_partitions(efi_handle_t handle, struct udevice *dev)
  110. {
  111. struct blk_desc *desc;
  112. const char *if_typename;
  113. desc = dev_get_uclass_plat(dev);
  114. if_typename = blk_get_if_type_name(desc->if_type);
  115. return efi_disk_create_partitions(handle, desc, if_typename,
  116. desc->devnum, dev->name);
  117. }
  118. /**
  119. * Create a block device for a handle
  120. *
  121. * @handle: handle
  122. * @interface: block io protocol
  123. * Return: 0 = success
  124. */
  125. static int efi_bl_bind(efi_handle_t handle, void *interface)
  126. {
  127. struct udevice *bdev, *parent = dm_root();
  128. int ret, devnum;
  129. char *name;
  130. struct efi_object *obj = efi_search_obj(handle);
  131. struct efi_block_io *io = interface;
  132. int disks;
  133. struct efi_blk_plat *plat;
  134. EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io);
  135. if (!obj)
  136. return -ENOENT;
  137. devnum = blk_find_max_devnum(IF_TYPE_EFI);
  138. if (devnum == -ENODEV)
  139. devnum = 0;
  140. else if (devnum < 0)
  141. return devnum;
  142. name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */
  143. if (!name)
  144. return -ENOMEM;
  145. sprintf(name, "efiblk#%d", devnum);
  146. /* Create driver model udevice for the EFI block io device */
  147. ret = blk_create_device(parent, "efi_blk", name, IF_TYPE_EFI, devnum,
  148. io->media->block_size,
  149. (lbaint_t)io->media->last_block, &bdev);
  150. if (ret)
  151. return ret;
  152. if (!bdev)
  153. return -ENOENT;
  154. /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */
  155. device_set_name_alloced(bdev);
  156. plat = dev_get_plat(bdev);
  157. plat->handle = handle;
  158. plat->io = interface;
  159. ret = device_probe(bdev);
  160. if (ret)
  161. return ret;
  162. EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
  163. /* Create handles for the partions of the block device */
  164. disks = efi_bl_bind_partitions(handle, bdev);
  165. EFI_PRINT("Found %d partitions\n", disks);
  166. return 0;
  167. }
  168. /* Block device driver operators */
  169. static const struct blk_ops efi_blk_ops = {
  170. .read = efi_bl_read,
  171. .write = efi_bl_write,
  172. };
  173. /* Identify as block device driver */
  174. U_BOOT_DRIVER(efi_blk) = {
  175. .name = "efi_blk",
  176. .id = UCLASS_BLK,
  177. .ops = &efi_blk_ops,
  178. .plat_auto = sizeof(struct efi_blk_plat),
  179. };
  180. /* EFI driver operators */
  181. static const struct efi_driver_ops driver_ops = {
  182. .protocol = &efi_block_io_guid,
  183. .child_protocol = &efi_block_io_guid,
  184. .bind = efi_bl_bind,
  185. };
  186. /* Identify as EFI driver */
  187. U_BOOT_DRIVER(efi_block) = {
  188. .name = "EFI block driver",
  189. .id = UCLASS_EFI,
  190. .ops = &driver_ops,
  191. };