efi_disk.c 12 KB

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