efi_disk.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 void ascii2unicode(u16 *unicode, char *ascii)
  29. {
  30. while (*ascii)
  31. *(unicode++) = *(ascii++);
  32. }
  33. static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
  34. void **protocol_interface, void *agent_handle,
  35. void *controller_handle, uint32_t attributes)
  36. {
  37. struct efi_disk_obj *diskobj = handle;
  38. *protocol_interface = &diskobj->ops;
  39. return EFI_SUCCESS;
  40. }
  41. static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
  42. void **protocol_interface, void *agent_handle,
  43. void *controller_handle, uint32_t attributes)
  44. {
  45. struct efi_disk_obj *diskobj = handle;
  46. *protocol_interface = diskobj->dp;
  47. return EFI_SUCCESS;
  48. }
  49. static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
  50. char extended_verification)
  51. {
  52. EFI_ENTRY("%p, %x", this, extended_verification);
  53. return EFI_EXIT(EFI_DEVICE_ERROR);
  54. }
  55. enum efi_disk_direction {
  56. EFI_DISK_READ,
  57. EFI_DISK_WRITE,
  58. };
  59. static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
  60. u32 media_id, u64 lba, unsigned long buffer_size,
  61. void *buffer, enum efi_disk_direction direction)
  62. {
  63. struct efi_disk_obj *diskobj;
  64. struct blk_desc *desc;
  65. int blksz;
  66. int blocks;
  67. unsigned long n;
  68. EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
  69. buffer_size, buffer);
  70. diskobj = container_of(this, struct efi_disk_obj, ops);
  71. if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index)))
  72. return EFI_EXIT(EFI_DEVICE_ERROR);
  73. blksz = desc->blksz;
  74. blocks = buffer_size / blksz;
  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 = desc->block_read(desc, lba, blocks, buffer);
  84. else
  85. n = desc->block_write(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. /*
  122. * U-Boot doesn't have a list of all online disk devices. So when running our
  123. * EFI payload, we scan through all of the potentially available ones and
  124. * store them in our object pool.
  125. *
  126. * This gets called from do_bootefi_exec().
  127. */
  128. int efi_disk_register(void)
  129. {
  130. const struct block_drvr *cur_drvr;
  131. int i;
  132. int disks = 0;
  133. /* Search for all available disk devices */
  134. for (cur_drvr = block_drvr; cur_drvr->name; cur_drvr++) {
  135. printf("Scanning disks on %s...\n", cur_drvr->name);
  136. for (i = 0; i < 4; i++) {
  137. struct blk_desc *desc;
  138. struct efi_disk_obj *diskobj;
  139. struct efi_device_path_file_path *dp;
  140. int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
  141. char devname[16] = { 0 }; /* dp->str is u16[16] long */
  142. desc = blk_get_dev(cur_drvr->name, i);
  143. if (!desc)
  144. continue;
  145. if (desc->type == DEV_TYPE_UNKNOWN)
  146. continue;
  147. diskobj = calloc(1, objlen);
  148. /* Fill in object data */
  149. diskobj->parent.protocols[0].guid = &efi_block_io_guid;
  150. diskobj->parent.protocols[0].open = efi_disk_open_block;
  151. diskobj->parent.protocols[1].guid = &efi_guid_device_path;
  152. diskobj->parent.protocols[1].open = efi_disk_open_dp;
  153. diskobj->parent.handle = diskobj;
  154. diskobj->ops = block_io_disk_template;
  155. diskobj->ifname = cur_drvr->name;
  156. diskobj->dev_index = i;
  157. /* Fill in EFI IO Media info (for read/write callbacks) */
  158. diskobj->media.removable_media = desc->removable;
  159. diskobj->media.media_present = 1;
  160. diskobj->media.block_size = desc->blksz;
  161. diskobj->media.io_align = desc->blksz;
  162. diskobj->media.last_block = desc->lba;
  163. diskobj->ops.media = &diskobj->media;
  164. /* Fill in device path */
  165. dp = (void*)&diskobj[1];
  166. diskobj->dp = dp;
  167. dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  168. dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  169. dp[0].dp.length = sizeof(*dp);
  170. snprintf(devname, sizeof(devname), "%s%d",
  171. cur_drvr->name, i);
  172. ascii2unicode(dp[0].str, devname);
  173. dp[1].dp.type = DEVICE_PATH_TYPE_END;
  174. dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
  175. dp[1].dp.length = sizeof(*dp);
  176. /* Hook up to the device list */
  177. list_add_tail(&diskobj->parent.link, &efi_obj_list);
  178. disks++;
  179. }
  180. }
  181. printf("Found %d disks\n", disks);
  182. return 0;
  183. }