efi_disk.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 <dm.h>
  11. #include <efi_loader.h>
  12. #include <inttypes.h>
  13. #include <part.h>
  14. #include <malloc.h>
  15. static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
  16. struct efi_disk_obj {
  17. /* Generic EFI object parent class data */
  18. struct efi_object parent;
  19. /* EFI Interface callback struct for block I/O */
  20. struct efi_block_io ops;
  21. /* U-Boot ifname for block device */
  22. const char *ifname;
  23. /* U-Boot dev_index for block device */
  24. int dev_index;
  25. /* EFI Interface Media descriptor struct, referenced by ops */
  26. struct efi_block_io_media media;
  27. /* EFI device path to this block device */
  28. struct efi_device_path_file_path *dp;
  29. /* Offset into disk for simple partitions */
  30. lbaint_t offset;
  31. /* Internal block device */
  32. const struct blk_desc *desc;
  33. };
  34. static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
  35. void **protocol_interface, void *agent_handle,
  36. void *controller_handle, uint32_t attributes)
  37. {
  38. struct efi_disk_obj *diskobj = handle;
  39. *protocol_interface = &diskobj->ops;
  40. return EFI_SUCCESS;
  41. }
  42. static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
  43. void **protocol_interface, void *agent_handle,
  44. void *controller_handle, uint32_t attributes)
  45. {
  46. struct efi_disk_obj *diskobj = handle;
  47. *protocol_interface = diskobj->dp;
  48. return EFI_SUCCESS;
  49. }
  50. static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
  51. char extended_verification)
  52. {
  53. EFI_ENTRY("%p, %x", this, extended_verification);
  54. return EFI_EXIT(EFI_DEVICE_ERROR);
  55. }
  56. enum efi_disk_direction {
  57. EFI_DISK_READ,
  58. EFI_DISK_WRITE,
  59. };
  60. static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
  61. u32 media_id, u64 lba, unsigned long buffer_size,
  62. void *buffer, enum efi_disk_direction direction)
  63. {
  64. struct efi_disk_obj *diskobj;
  65. struct blk_desc *desc;
  66. int blksz;
  67. int blocks;
  68. unsigned long n;
  69. diskobj = container_of(this, struct efi_disk_obj, ops);
  70. desc = (struct blk_desc *) diskobj->desc;
  71. blksz = desc->blksz;
  72. blocks = buffer_size / blksz;
  73. lba += diskobj->offset;
  74. debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
  75. __LINE__, blocks, lba, blksz, direction);
  76. /* We only support full block access */
  77. if (buffer_size & (blksz - 1))
  78. return EFI_EXIT(EFI_DEVICE_ERROR);
  79. if (direction == EFI_DISK_READ)
  80. n = blk_dread(desc, lba, blocks, buffer);
  81. else
  82. n = blk_dwrite(desc, lba, blocks, buffer);
  83. /* We don't do interrupts, so check for timers cooperatively */
  84. efi_timer_check();
  85. debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
  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. void *real_buffer = buffer;
  95. efi_status_t r;
  96. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  97. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  98. r = efi_disk_read_blocks(this, media_id, lba,
  99. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  100. if (r != EFI_SUCCESS)
  101. return r;
  102. return efi_disk_read_blocks(this, media_id, lba +
  103. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  104. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  105. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  106. }
  107. real_buffer = efi_bounce_buffer;
  108. #endif
  109. EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
  110. buffer_size, buffer);
  111. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  112. EFI_DISK_READ);
  113. /* Copy from bounce buffer to real buffer if necessary */
  114. if ((r == EFI_SUCCESS) && (real_buffer != buffer))
  115. memcpy(buffer, real_buffer, buffer_size);
  116. return EFI_EXIT(r);
  117. }
  118. static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
  119. u32 media_id, u64 lba, unsigned long buffer_size,
  120. void *buffer)
  121. {
  122. void *real_buffer = buffer;
  123. efi_status_t r;
  124. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  125. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  126. r = efi_disk_write_blocks(this, media_id, lba,
  127. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  128. if (r != EFI_SUCCESS)
  129. return r;
  130. return efi_disk_write_blocks(this, media_id, lba +
  131. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  132. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  133. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  134. }
  135. real_buffer = efi_bounce_buffer;
  136. #endif
  137. EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
  138. buffer_size, buffer);
  139. /* Populate bounce buffer if necessary */
  140. if (real_buffer != buffer)
  141. memcpy(real_buffer, buffer, buffer_size);
  142. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  143. EFI_DISK_WRITE);
  144. return EFI_EXIT(r);
  145. }
  146. static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
  147. {
  148. /* We always write synchronously */
  149. EFI_ENTRY("%p", this);
  150. return EFI_EXIT(EFI_SUCCESS);
  151. }
  152. static const struct efi_block_io block_io_disk_template = {
  153. .reset = &efi_disk_reset,
  154. .read_blocks = &efi_disk_read_blocks,
  155. .write_blocks = &efi_disk_write_blocks,
  156. .flush_blocks = &efi_disk_flush_blocks,
  157. };
  158. static void efi_disk_add_dev(const char *name,
  159. const char *if_typename,
  160. const struct blk_desc *desc,
  161. int dev_index,
  162. lbaint_t offset)
  163. {
  164. struct efi_disk_obj *diskobj;
  165. struct efi_device_path_file_path *dp;
  166. int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
  167. diskobj = calloc(1, objlen);
  168. /* Fill in object data */
  169. diskobj->parent.protocols[0].guid = &efi_block_io_guid;
  170. diskobj->parent.protocols[0].open = efi_disk_open_block;
  171. diskobj->parent.protocols[1].guid = &efi_guid_device_path;
  172. diskobj->parent.protocols[1].open = efi_disk_open_dp;
  173. diskobj->parent.handle = diskobj;
  174. diskobj->ops = block_io_disk_template;
  175. diskobj->ifname = if_typename;
  176. diskobj->dev_index = dev_index;
  177. diskobj->offset = offset;
  178. diskobj->desc = desc;
  179. /* Fill in EFI IO Media info (for read/write callbacks) */
  180. diskobj->media.removable_media = desc->removable;
  181. diskobj->media.media_present = 1;
  182. diskobj->media.block_size = desc->blksz;
  183. diskobj->media.io_align = desc->blksz;
  184. diskobj->media.last_block = desc->lba;
  185. diskobj->ops.media = &diskobj->media;
  186. /* Fill in device path */
  187. dp = (void*)&diskobj[1];
  188. diskobj->dp = dp;
  189. dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  190. dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  191. dp[0].dp.length = sizeof(*dp);
  192. ascii2unicode(dp[0].str, name);
  193. dp[1].dp.type = DEVICE_PATH_TYPE_END;
  194. dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
  195. dp[1].dp.length = sizeof(*dp);
  196. /* Hook up to the device list */
  197. list_add_tail(&diskobj->parent.link, &efi_obj_list);
  198. }
  199. static int efi_disk_create_eltorito(struct blk_desc *desc,
  200. const char *if_typename,
  201. int diskid,
  202. const char *pdevname)
  203. {
  204. int disks = 0;
  205. #ifdef CONFIG_ISO_PARTITION
  206. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  207. disk_partition_t info;
  208. int part = 1;
  209. if (desc->part_type != PART_TYPE_ISO)
  210. return 0;
  211. while (!part_get_info(desc, part, &info)) {
  212. snprintf(devname, sizeof(devname), "%s:%d", pdevname,
  213. part);
  214. efi_disk_add_dev(devname, if_typename, desc, diskid,
  215. info.start);
  216. part++;
  217. disks++;
  218. }
  219. #endif
  220. return disks;
  221. }
  222. /*
  223. * U-Boot doesn't have a list of all online disk devices. So when running our
  224. * EFI payload, we scan through all of the potentially available ones and
  225. * store them in our object pool.
  226. *
  227. * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
  228. * Consider converting the code to look up devices as needed. The EFI device
  229. * could be a child of the UCLASS_BLK block device, perhaps.
  230. *
  231. * This gets called from do_bootefi_exec().
  232. */
  233. int efi_disk_register(void)
  234. {
  235. int disks = 0;
  236. #ifdef CONFIG_BLK
  237. struct udevice *dev;
  238. for (uclass_first_device(UCLASS_BLK, &dev);
  239. dev;
  240. uclass_next_device(&dev)) {
  241. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  242. const char *if_typename = dev->driver->name;
  243. printf("Scanning disk %s...\n", dev->name);
  244. efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
  245. disks++;
  246. /*
  247. * El Torito images show up as block devices in an EFI world,
  248. * so let's create them here
  249. */
  250. disks += efi_disk_create_eltorito(desc, if_typename,
  251. desc->devnum, dev->name);
  252. }
  253. #else
  254. int i, if_type;
  255. /* Search for all available disk devices */
  256. for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
  257. const struct blk_driver *cur_drvr;
  258. const char *if_typename;
  259. cur_drvr = blk_driver_lookup_type(if_type);
  260. if (!cur_drvr)
  261. continue;
  262. if_typename = cur_drvr->if_typename;
  263. printf("Scanning disks on %s...\n", if_typename);
  264. for (i = 0; i < 4; i++) {
  265. struct blk_desc *desc;
  266. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  267. desc = blk_get_devnum_by_type(if_type, i);
  268. if (!desc)
  269. continue;
  270. if (desc->type == DEV_TYPE_UNKNOWN)
  271. continue;
  272. snprintf(devname, sizeof(devname), "%s%d",
  273. if_typename, i);
  274. efi_disk_add_dev(devname, if_typename, desc, i, 0);
  275. disks++;
  276. /*
  277. * El Torito images show up as block devices
  278. * in an EFI world, so let's create them here
  279. */
  280. disks += efi_disk_create_eltorito(desc, if_typename,
  281. i, devname);
  282. }
  283. }
  284. #endif
  285. printf("Found %d disks\n", disks);
  286. return 0;
  287. }