efi_disk.c 9.2 KB

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