efi_disk.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /* Don't add empty devices */
  168. if (!desc->lba)
  169. return;
  170. diskobj = calloc(1, objlen);
  171. /* Fill in object data */
  172. diskobj->parent.protocols[0].guid = &efi_block_io_guid;
  173. diskobj->parent.protocols[0].open = efi_disk_open_block;
  174. diskobj->parent.protocols[1].guid = &efi_guid_device_path;
  175. diskobj->parent.protocols[1].open = efi_disk_open_dp;
  176. diskobj->parent.handle = diskobj;
  177. diskobj->ops = block_io_disk_template;
  178. diskobj->ifname = if_typename;
  179. diskobj->dev_index = dev_index;
  180. diskobj->offset = offset;
  181. diskobj->desc = desc;
  182. /* Fill in EFI IO Media info (for read/write callbacks) */
  183. diskobj->media.removable_media = desc->removable;
  184. diskobj->media.media_present = 1;
  185. diskobj->media.block_size = desc->blksz;
  186. diskobj->media.io_align = desc->blksz;
  187. diskobj->media.last_block = desc->lba - offset;
  188. diskobj->ops.media = &diskobj->media;
  189. /* Fill in device path */
  190. dp = (void*)&diskobj[1];
  191. diskobj->dp = dp;
  192. dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  193. dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  194. dp[0].dp.length = sizeof(*dp);
  195. ascii2unicode(dp[0].str, name);
  196. dp[1].dp.type = DEVICE_PATH_TYPE_END;
  197. dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
  198. dp[1].dp.length = sizeof(*dp);
  199. /* Hook up to the device list */
  200. list_add_tail(&diskobj->parent.link, &efi_obj_list);
  201. }
  202. static int efi_disk_create_eltorito(struct blk_desc *desc,
  203. const char *if_typename,
  204. int diskid,
  205. const char *pdevname)
  206. {
  207. int disks = 0;
  208. #ifdef CONFIG_ISO_PARTITION
  209. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  210. disk_partition_t info;
  211. int part = 1;
  212. if (desc->part_type != PART_TYPE_ISO)
  213. return 0;
  214. while (!part_get_info(desc, part, &info)) {
  215. snprintf(devname, sizeof(devname), "%s:%d", pdevname,
  216. part);
  217. efi_disk_add_dev(devname, if_typename, desc, diskid,
  218. info.start);
  219. part++;
  220. disks++;
  221. }
  222. #endif
  223. return disks;
  224. }
  225. /*
  226. * U-Boot doesn't have a list of all online disk devices. So when running our
  227. * EFI payload, we scan through all of the potentially available ones and
  228. * store them in our object pool.
  229. *
  230. * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
  231. * Consider converting the code to look up devices as needed. The EFI device
  232. * could be a child of the UCLASS_BLK block device, perhaps.
  233. *
  234. * This gets called from do_bootefi_exec().
  235. */
  236. int efi_disk_register(void)
  237. {
  238. int disks = 0;
  239. #ifdef CONFIG_BLK
  240. struct udevice *dev;
  241. for (uclass_first_device(UCLASS_BLK, &dev);
  242. dev;
  243. uclass_next_device(&dev)) {
  244. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  245. const char *if_typename = dev->driver->name;
  246. printf("Scanning disk %s...\n", dev->name);
  247. efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
  248. disks++;
  249. /*
  250. * El Torito images show up as block devices in an EFI world,
  251. * so let's create them here
  252. */
  253. disks += efi_disk_create_eltorito(desc, if_typename,
  254. desc->devnum, dev->name);
  255. }
  256. #else
  257. int i, if_type;
  258. /* Search for all available disk devices */
  259. for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
  260. const struct blk_driver *cur_drvr;
  261. const char *if_typename;
  262. cur_drvr = blk_driver_lookup_type(if_type);
  263. if (!cur_drvr)
  264. continue;
  265. if_typename = cur_drvr->if_typename;
  266. printf("Scanning disks on %s...\n", if_typename);
  267. for (i = 0; i < 4; i++) {
  268. struct blk_desc *desc;
  269. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  270. desc = blk_get_devnum_by_type(if_type, i);
  271. if (!desc)
  272. continue;
  273. if (desc->type == DEV_TYPE_UNKNOWN)
  274. continue;
  275. snprintf(devname, sizeof(devname), "%s%d",
  276. if_typename, i);
  277. efi_disk_add_dev(devname, if_typename, desc, i, 0);
  278. disks++;
  279. /*
  280. * El Torito images show up as block devices
  281. * in an EFI world, so let's create them here
  282. */
  283. disks += efi_disk_create_eltorito(desc, if_typename,
  284. i, devname);
  285. }
  286. }
  287. #endif
  288. printf("Found %d disks\n", disks);
  289. return 0;
  290. }