efi_disk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. 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 *dp;
  29. /* partition # */
  30. unsigned int part;
  31. /* handle to filesys proto (for partition objects) */
  32. struct efi_simple_file_system_protocol *volume;
  33. /* Offset into disk for simple partitions */
  34. lbaint_t offset;
  35. /* Internal block device */
  36. struct blk_desc *desc;
  37. };
  38. static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
  39. char extended_verification)
  40. {
  41. EFI_ENTRY("%p, %x", this, extended_verification);
  42. return EFI_EXIT(EFI_DEVICE_ERROR);
  43. }
  44. enum efi_disk_direction {
  45. EFI_DISK_READ,
  46. EFI_DISK_WRITE,
  47. };
  48. static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
  49. u32 media_id, u64 lba, unsigned long buffer_size,
  50. void *buffer, enum efi_disk_direction direction)
  51. {
  52. struct efi_disk_obj *diskobj;
  53. struct blk_desc *desc;
  54. int blksz;
  55. int blocks;
  56. unsigned long n;
  57. diskobj = container_of(this, struct efi_disk_obj, ops);
  58. desc = (struct blk_desc *) diskobj->desc;
  59. blksz = desc->blksz;
  60. blocks = buffer_size / blksz;
  61. lba += diskobj->offset;
  62. debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
  63. __LINE__, blocks, lba, blksz, direction);
  64. /* We only support full block access */
  65. if (buffer_size & (blksz - 1))
  66. return EFI_DEVICE_ERROR;
  67. if (direction == EFI_DISK_READ)
  68. n = blk_dread(desc, lba, blocks, buffer);
  69. else
  70. n = blk_dwrite(desc, lba, blocks, buffer);
  71. /* We don't do interrupts, so check for timers cooperatively */
  72. efi_timer_check();
  73. debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
  74. if (n != blocks)
  75. return EFI_DEVICE_ERROR;
  76. return EFI_SUCCESS;
  77. }
  78. static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
  79. u32 media_id, u64 lba, efi_uintn_t buffer_size,
  80. void *buffer)
  81. {
  82. void *real_buffer = buffer;
  83. efi_status_t r;
  84. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  85. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  86. r = efi_disk_read_blocks(this, media_id, lba,
  87. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  88. if (r != EFI_SUCCESS)
  89. return r;
  90. return efi_disk_read_blocks(this, media_id, lba +
  91. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  92. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  93. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  94. }
  95. real_buffer = efi_bounce_buffer;
  96. #endif
  97. EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
  98. buffer_size, buffer);
  99. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  100. EFI_DISK_READ);
  101. /* Copy from bounce buffer to real buffer if necessary */
  102. if ((r == EFI_SUCCESS) && (real_buffer != buffer))
  103. memcpy(buffer, real_buffer, buffer_size);
  104. return EFI_EXIT(r);
  105. }
  106. static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
  107. u32 media_id, u64 lba, efi_uintn_t buffer_size,
  108. void *buffer)
  109. {
  110. void *real_buffer = buffer;
  111. efi_status_t r;
  112. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  113. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  114. r = efi_disk_write_blocks(this, media_id, lba,
  115. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  116. if (r != EFI_SUCCESS)
  117. return r;
  118. return efi_disk_write_blocks(this, media_id, lba +
  119. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  120. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  121. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  122. }
  123. real_buffer = efi_bounce_buffer;
  124. #endif
  125. EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
  126. buffer_size, buffer);
  127. /* Populate bounce buffer if necessary */
  128. if (real_buffer != buffer)
  129. memcpy(real_buffer, buffer, buffer_size);
  130. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  131. EFI_DISK_WRITE);
  132. return EFI_EXIT(r);
  133. }
  134. static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
  135. {
  136. /* We always write synchronously */
  137. EFI_ENTRY("%p", this);
  138. return EFI_EXIT(EFI_SUCCESS);
  139. }
  140. static const struct efi_block_io block_io_disk_template = {
  141. .reset = &efi_disk_reset,
  142. .read_blocks = &efi_disk_read_blocks,
  143. .write_blocks = &efi_disk_write_blocks,
  144. .flush_blocks = &efi_disk_flush_blocks,
  145. };
  146. /*
  147. * Get the simple file system protocol for a file device path.
  148. *
  149. * The full path provided is split into device part and into a file
  150. * part. The device part is used to find the handle on which the
  151. * simple file system protocol is installed.
  152. *
  153. * @full_path device path including device and file
  154. * @return simple file system protocol
  155. */
  156. struct efi_simple_file_system_protocol *
  157. efi_fs_from_path(struct efi_device_path *full_path)
  158. {
  159. struct efi_object *efiobj;
  160. struct efi_handler *handler;
  161. struct efi_device_path *device_path;
  162. struct efi_device_path *file_path;
  163. efi_status_t ret;
  164. /* Split the path into a device part and a file part */
  165. ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
  166. if (ret != EFI_SUCCESS)
  167. return NULL;
  168. efi_free_pool(file_path);
  169. /* Get the EFI object for the partition */
  170. efiobj = efi_dp_find_obj(device_path, NULL);
  171. efi_free_pool(device_path);
  172. if (!efiobj)
  173. return NULL;
  174. /* Find the simple file system protocol */
  175. ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
  176. &handler);
  177. if (ret != EFI_SUCCESS)
  178. return NULL;
  179. /* Return the simple file system protocol for the partition */
  180. return handler->protocol_interface;
  181. }
  182. /*
  183. * Create a handle for a partition or disk
  184. *
  185. * @parent parent handle
  186. * @dp_parent parent device path
  187. * @if_typename interface name for block device
  188. * @desc internal block device
  189. * @dev_index device index for block device
  190. * @offset offset into disk for simple partitions
  191. * @return disk object
  192. */
  193. static struct efi_disk_obj *efi_disk_add_dev(
  194. efi_handle_t parent,
  195. struct efi_device_path *dp_parent,
  196. const char *if_typename,
  197. struct blk_desc *desc,
  198. int dev_index,
  199. lbaint_t offset,
  200. unsigned int part)
  201. {
  202. struct efi_disk_obj *diskobj;
  203. efi_status_t ret;
  204. /* Don't add empty devices */
  205. if (!desc->lba)
  206. return NULL;
  207. diskobj = calloc(1, sizeof(*diskobj));
  208. if (!diskobj)
  209. goto out_of_memory;
  210. /* Hook up to the device list */
  211. efi_add_handle(&diskobj->parent);
  212. /* Fill in object data */
  213. if (part) {
  214. struct efi_device_path *node = efi_dp_part_node(desc, part);
  215. diskobj->dp = efi_dp_append_node(dp_parent, node);
  216. efi_free_pool(node);
  217. } else {
  218. diskobj->dp = efi_dp_from_part(desc, part);
  219. }
  220. diskobj->part = part;
  221. ret = efi_add_protocol(diskobj->parent.handle, &efi_block_io_guid,
  222. &diskobj->ops);
  223. if (ret != EFI_SUCCESS)
  224. goto out_of_memory;
  225. ret = efi_add_protocol(diskobj->parent.handle, &efi_guid_device_path,
  226. diskobj->dp);
  227. if (ret != EFI_SUCCESS)
  228. goto out_of_memory;
  229. if (part >= 1) {
  230. diskobj->volume = efi_simple_file_system(desc, part,
  231. diskobj->dp);
  232. ret = efi_add_protocol(diskobj->parent.handle,
  233. &efi_simple_file_system_protocol_guid,
  234. diskobj->volume);
  235. if (ret != EFI_SUCCESS)
  236. goto out_of_memory;
  237. }
  238. diskobj->ops = block_io_disk_template;
  239. diskobj->ifname = if_typename;
  240. diskobj->dev_index = dev_index;
  241. diskobj->offset = offset;
  242. diskobj->desc = desc;
  243. /* Fill in EFI IO Media info (for read/write callbacks) */
  244. diskobj->media.removable_media = desc->removable;
  245. diskobj->media.media_present = 1;
  246. diskobj->media.block_size = desc->blksz;
  247. diskobj->media.io_align = desc->blksz;
  248. diskobj->media.last_block = desc->lba - offset;
  249. if (part != 0)
  250. diskobj->media.logical_partition = 1;
  251. diskobj->ops.media = &diskobj->media;
  252. return diskobj;
  253. out_of_memory:
  254. printf("ERROR: Out of memory\n");
  255. return NULL;
  256. }
  257. /*
  258. * Create handles and protocols for the partitions of a block device
  259. *
  260. * @parent handle of the parent disk
  261. * @blk_desc block device
  262. * @if_typename interface type
  263. * @diskid device number
  264. * @pdevname device name
  265. * @return number of partitions created
  266. */
  267. int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
  268. const char *if_typename, int diskid,
  269. const char *pdevname)
  270. {
  271. int disks = 0;
  272. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  273. disk_partition_t info;
  274. int part;
  275. struct efi_device_path *dp = NULL;
  276. efi_status_t ret;
  277. struct efi_handler *handler;
  278. /* Get the device path of the parent */
  279. ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
  280. if (ret == EFI_SUCCESS)
  281. dp = handler->protocol_interface;
  282. /* Add devices for each partition */
  283. for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
  284. if (part_get_info(desc, part, &info))
  285. continue;
  286. snprintf(devname, sizeof(devname), "%s:%d", pdevname,
  287. part);
  288. efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
  289. info.start, part);
  290. disks++;
  291. }
  292. return disks;
  293. }
  294. /*
  295. * U-Boot doesn't have a list of all online disk devices. So when running our
  296. * EFI payload, we scan through all of the potentially available ones and
  297. * store them in our object pool.
  298. *
  299. * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
  300. * Consider converting the code to look up devices as needed. The EFI device
  301. * could be a child of the UCLASS_BLK block device, perhaps.
  302. *
  303. * This gets called from do_bootefi_exec().
  304. */
  305. int efi_disk_register(void)
  306. {
  307. struct efi_disk_obj *disk;
  308. int disks = 0;
  309. #ifdef CONFIG_BLK
  310. struct udevice *dev;
  311. for (uclass_first_device_check(UCLASS_BLK, &dev);
  312. dev;
  313. uclass_next_device_check(&dev)) {
  314. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  315. const char *if_typename = blk_get_if_type_name(desc->if_type);
  316. printf("Scanning disk %s...\n", dev->name);
  317. /* Add block device for the full device */
  318. disk = efi_disk_add_dev(NULL, NULL, if_typename,
  319. desc, desc->devnum, 0, 0);
  320. if (!disk)
  321. return -ENOMEM;
  322. disks++;
  323. /* Partitions show up as block devices in EFI */
  324. disks += efi_disk_create_partitions(
  325. disk->parent.handle, desc, if_typename,
  326. desc->devnum, dev->name);
  327. }
  328. #else
  329. int i, if_type;
  330. /* Search for all available disk devices */
  331. for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
  332. const struct blk_driver *cur_drvr;
  333. const char *if_typename;
  334. cur_drvr = blk_driver_lookup_type(if_type);
  335. if (!cur_drvr)
  336. continue;
  337. if_typename = cur_drvr->if_typename;
  338. printf("Scanning disks on %s...\n", if_typename);
  339. for (i = 0; i < 4; i++) {
  340. struct blk_desc *desc;
  341. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  342. desc = blk_get_devnum_by_type(if_type, i);
  343. if (!desc)
  344. continue;
  345. if (desc->type == DEV_TYPE_UNKNOWN)
  346. continue;
  347. snprintf(devname, sizeof(devname), "%s%d",
  348. if_typename, i);
  349. /* Add block device for the full device */
  350. disk = efi_disk_add_dev(NULL, NULL, if_typename, desc,
  351. i, 0, 0);
  352. if (!disk)
  353. return -ENOMEM;
  354. disks++;
  355. /* Partitions show up as block devices in EFI */
  356. disks += efi_disk_create_partitions(
  357. disk->parent.handle, desc,
  358. if_typename, i, devname);
  359. }
  360. }
  361. #endif
  362. printf("Found %d disks\n", disks);
  363. return 0;
  364. }