efi_disk.c 6.8 KB

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