efi_disk.c 5.9 KB

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