efi_disk.c 6.9 KB

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