efi_disk.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /*
  117. * U-Boot doesn't have a list of all online disk devices. So when running our
  118. * EFI payload, we scan through all of the potentially available ones and
  119. * store them in our object pool.
  120. *
  121. * This gets called from do_bootefi_exec().
  122. */
  123. int efi_disk_register(void)
  124. {
  125. const struct block_drvr *cur_drvr;
  126. int i;
  127. int disks = 0;
  128. /* Search for all available disk devices */
  129. for (cur_drvr = block_drvr; cur_drvr->name; cur_drvr++) {
  130. printf("Scanning disks on %s...\n", cur_drvr->name);
  131. for (i = 0; i < 4; i++) {
  132. struct blk_desc *desc;
  133. struct efi_disk_obj *diskobj;
  134. struct efi_device_path_file_path *dp;
  135. int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
  136. char devname[16] = { 0 }; /* dp->str is u16[16] long */
  137. desc = blk_get_dev(cur_drvr->name, i);
  138. if (!desc)
  139. continue;
  140. if (desc->type == DEV_TYPE_UNKNOWN)
  141. continue;
  142. diskobj = calloc(1, objlen);
  143. /* Fill in object data */
  144. diskobj->parent.protocols[0].guid = &efi_block_io_guid;
  145. diskobj->parent.protocols[0].open = efi_disk_open_block;
  146. diskobj->parent.protocols[1].guid = &efi_guid_device_path;
  147. diskobj->parent.protocols[1].open = efi_disk_open_dp;
  148. diskobj->parent.handle = diskobj;
  149. diskobj->ops = block_io_disk_template;
  150. diskobj->ifname = cur_drvr->name;
  151. diskobj->dev_index = i;
  152. /* Fill in EFI IO Media info (for read/write callbacks) */
  153. diskobj->media.removable_media = desc->removable;
  154. diskobj->media.media_present = 1;
  155. diskobj->media.block_size = desc->blksz;
  156. diskobj->media.io_align = desc->blksz;
  157. diskobj->media.last_block = desc->lba;
  158. diskobj->ops.media = &diskobj->media;
  159. /* Fill in device path */
  160. dp = (void*)&diskobj[1];
  161. diskobj->dp = dp;
  162. dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  163. dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  164. dp[0].dp.length = sizeof(*dp);
  165. snprintf(devname, sizeof(devname), "%s%d",
  166. cur_drvr->name, i);
  167. ascii2unicode(dp[0].str, devname);
  168. dp[1].dp.type = DEVICE_PATH_TYPE_END;
  169. dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
  170. dp[1].dp.length = sizeof(*dp);
  171. /* Hook up to the device list */
  172. list_add_tail(&diskobj->parent.link, &efi_obj_list);
  173. disks++;
  174. }
  175. }
  176. printf("Found %d disks\n", disks);
  177. return 0;
  178. }