efi_block_device.c 5.5 KB

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