efi_disk.c 8.7 KB

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