efi_disk.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * EFI application disk support
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <blk.h>
  10. #include <dm.h>
  11. #include <efi_loader.h>
  12. #include <inttypes.h>
  13. #include <part.h>
  14. #include <malloc.h>
  15. static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
  16. struct efi_disk_obj {
  17. /* Generic EFI object parent class data */
  18. struct efi_object parent;
  19. /* EFI Interface callback struct for block I/O */
  20. struct efi_block_io ops;
  21. /* U-Boot ifname for block device */
  22. const char *ifname;
  23. /* U-Boot dev_index for block device */
  24. int dev_index;
  25. /* EFI Interface Media descriptor struct, referenced by ops */
  26. struct efi_block_io_media media;
  27. /* EFI device path to this block device */
  28. struct efi_device_path_file_path *dp;
  29. /* Offset into disk for simple partitions */
  30. lbaint_t offset;
  31. };
  32. static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
  33. void **protocol_interface, void *agent_handle,
  34. void *controller_handle, uint32_t attributes)
  35. {
  36. struct efi_disk_obj *diskobj = handle;
  37. *protocol_interface = &diskobj->ops;
  38. return EFI_SUCCESS;
  39. }
  40. static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
  41. void **protocol_interface, void *agent_handle,
  42. void *controller_handle, uint32_t attributes)
  43. {
  44. struct efi_disk_obj *diskobj = handle;
  45. *protocol_interface = diskobj->dp;
  46. return EFI_SUCCESS;
  47. }
  48. static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
  49. char extended_verification)
  50. {
  51. EFI_ENTRY("%p, %x", this, extended_verification);
  52. return EFI_EXIT(EFI_DEVICE_ERROR);
  53. }
  54. enum efi_disk_direction {
  55. EFI_DISK_READ,
  56. EFI_DISK_WRITE,
  57. };
  58. static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
  59. u32 media_id, u64 lba, unsigned long buffer_size,
  60. void *buffer, enum efi_disk_direction direction)
  61. {
  62. struct efi_disk_obj *diskobj;
  63. struct blk_desc *desc;
  64. int blksz;
  65. int blocks;
  66. unsigned long n;
  67. EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
  68. buffer_size, buffer);
  69. diskobj = container_of(this, struct efi_disk_obj, ops);
  70. if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index)))
  71. return EFI_EXIT(EFI_DEVICE_ERROR);
  72. blksz = desc->blksz;
  73. blocks = buffer_size / blksz;
  74. lba += diskobj->offset;
  75. #ifdef DEBUG_EFI
  76. printf("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
  77. __LINE__, blocks, lba, blksz, direction);
  78. #endif
  79. /* We only support full block access */
  80. if (buffer_size & (blksz - 1))
  81. return EFI_EXIT(EFI_DEVICE_ERROR);
  82. if (direction == EFI_DISK_READ)
  83. n = blk_dread(desc, lba, blocks, buffer);
  84. else
  85. n = blk_dwrite(desc, lba, blocks, buffer);
  86. /* We don't do interrupts, so check for timers cooperatively */
  87. efi_timer_check();
  88. #ifdef DEBUG_EFI
  89. printf("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
  90. #endif
  91. if (n != blocks)
  92. return EFI_EXIT(EFI_DEVICE_ERROR);
  93. return EFI_EXIT(EFI_SUCCESS);
  94. }
  95. static efi_status_t efi_disk_read_blocks(struct efi_block_io *this,
  96. u32 media_id, u64 lba, unsigned long buffer_size,
  97. void *buffer)
  98. {
  99. return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer,
  100. EFI_DISK_READ);
  101. }
  102. static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
  103. u32 media_id, u64 lba, unsigned long buffer_size,
  104. void *buffer)
  105. {
  106. return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer,
  107. EFI_DISK_WRITE);
  108. }
  109. static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
  110. {
  111. /* We always write synchronously */
  112. EFI_ENTRY("%p", this);
  113. return EFI_EXIT(EFI_SUCCESS);
  114. }
  115. static const struct efi_block_io block_io_disk_template = {
  116. .reset = &efi_disk_reset,
  117. .read_blocks = &efi_disk_read_blocks,
  118. .write_blocks = &efi_disk_write_blocks,
  119. .flush_blocks = &efi_disk_flush_blocks,
  120. };
  121. static void efi_disk_add_dev(const char *name,
  122. const char *if_typename,
  123. const struct blk_desc *desc,
  124. int dev_index,
  125. lbaint_t offset)
  126. {
  127. struct efi_disk_obj *diskobj;
  128. struct efi_device_path_file_path *dp;
  129. int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
  130. diskobj = calloc(1, objlen);
  131. /* Fill in object data */
  132. diskobj->parent.protocols[0].guid = &efi_block_io_guid;
  133. diskobj->parent.protocols[0].open = efi_disk_open_block;
  134. diskobj->parent.protocols[1].guid = &efi_guid_device_path;
  135. diskobj->parent.protocols[1].open = efi_disk_open_dp;
  136. diskobj->parent.handle = diskobj;
  137. diskobj->ops = block_io_disk_template;
  138. diskobj->ifname = if_typename;
  139. diskobj->dev_index = dev_index;
  140. diskobj->offset = offset;
  141. /* Fill in EFI IO Media info (for read/write callbacks) */
  142. diskobj->media.removable_media = desc->removable;
  143. diskobj->media.media_present = 1;
  144. diskobj->media.block_size = desc->blksz;
  145. diskobj->media.io_align = desc->blksz;
  146. diskobj->media.last_block = desc->lba;
  147. diskobj->ops.media = &diskobj->media;
  148. /* Fill in device path */
  149. dp = (void*)&diskobj[1];
  150. diskobj->dp = dp;
  151. dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  152. dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  153. dp[0].dp.length = sizeof(*dp);
  154. ascii2unicode(dp[0].str, name);
  155. dp[1].dp.type = DEVICE_PATH_TYPE_END;
  156. dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
  157. dp[1].dp.length = sizeof(*dp);
  158. /* Hook up to the device list */
  159. list_add_tail(&diskobj->parent.link, &efi_obj_list);
  160. }
  161. static int efi_disk_create_eltorito(struct blk_desc *desc,
  162. const char *if_typename,
  163. int diskid)
  164. {
  165. int disks = 0;
  166. #ifdef CONFIG_ISO_PARTITION
  167. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  168. disk_partition_t info;
  169. int part = 1;
  170. if (desc->part_type != PART_TYPE_ISO)
  171. return 0;
  172. while (!part_get_info(desc, part, &info)) {
  173. snprintf(devname, sizeof(devname), "%s%d:%d", if_typename,
  174. diskid, part);
  175. efi_disk_add_dev(devname, if_typename, desc, diskid,
  176. info.start);
  177. part++;
  178. disks++;
  179. }
  180. #endif
  181. return disks;
  182. }
  183. /*
  184. * U-Boot doesn't have a list of all online disk devices. So when running our
  185. * EFI payload, we scan through all of the potentially available ones and
  186. * store them in our object pool.
  187. *
  188. * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
  189. * Consider converting the code to look up devices as needed. The EFI device
  190. * could be a child of the UCLASS_BLK block device, perhaps.
  191. *
  192. * This gets called from do_bootefi_exec().
  193. */
  194. int efi_disk_register(void)
  195. {
  196. int disks = 0;
  197. #ifdef CONFIG_BLK
  198. struct udevice *dev;
  199. for (uclass_first_device(UCLASS_BLK, &dev);
  200. dev;
  201. uclass_next_device(&dev)) {
  202. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  203. const char *if_typename = dev->driver->name;
  204. printf("Scanning disk %s...\n", dev->name);
  205. efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
  206. disks++;
  207. /*
  208. * El Torito images show up as block devices in an EFI world,
  209. * so let's create them here
  210. */
  211. disks += efi_disk_create_eltorito(desc, if_typename,
  212. desc->devnum);
  213. }
  214. #else
  215. int i, if_type;
  216. /* Search for all available disk devices */
  217. for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
  218. const struct blk_driver *cur_drvr;
  219. const char *if_typename;
  220. cur_drvr = blk_driver_lookup_type(if_type);
  221. if (!cur_drvr)
  222. continue;
  223. if_typename = cur_drvr->if_typename;
  224. printf("Scanning disks on %s...\n", if_typename);
  225. for (i = 0; i < 4; i++) {
  226. struct blk_desc *desc;
  227. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  228. desc = blk_get_devnum_by_type(if_type, i);
  229. if (!desc)
  230. continue;
  231. if (desc->type == DEV_TYPE_UNKNOWN)
  232. continue;
  233. snprintf(devname, sizeof(devname), "%s%d",
  234. if_typename, i);
  235. efi_disk_add_dev(devname, if_typename, desc, i, 0);
  236. disks++;
  237. /*
  238. * El Torito images show up as block devices
  239. * in an EFI world, so let's create them here
  240. */
  241. disks += efi_disk_create_eltorito(desc, if_typename, i);
  242. }
  243. }
  244. #endif
  245. printf("Found %d disks\n", disks);
  246. return 0;
  247. }