efi_disk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application disk support
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #define LOG_CATEGORY LOGC_EFI
  8. #include <common.h>
  9. #include <blk.h>
  10. #include <dm.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/tag.h>
  13. #include <event.h>
  14. #include <efi_driver.h>
  15. #include <efi_loader.h>
  16. #include <fs.h>
  17. #include <log.h>
  18. #include <part.h>
  19. #include <malloc.h>
  20. struct efi_system_partition efi_system_partition = {
  21. .uclass_id = UCLASS_INVALID,
  22. };
  23. const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
  24. const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
  25. /**
  26. * struct efi_disk_obj - EFI disk object
  27. *
  28. * @header: EFI object header
  29. * @ops: EFI disk I/O protocol interface
  30. * @dev_index: device index of block device
  31. * @media: block I/O media information
  32. * @dp: device path to the block device
  33. * @part: partition
  34. * @volume: simple file system protocol of the partition
  35. * @dev: associated DM device
  36. */
  37. struct efi_disk_obj {
  38. struct efi_object header;
  39. struct efi_block_io ops;
  40. int dev_index;
  41. struct efi_block_io_media media;
  42. struct efi_device_path *dp;
  43. unsigned int part;
  44. struct efi_simple_file_system_protocol *volume;
  45. };
  46. /**
  47. * efi_disk_reset() - reset block device
  48. *
  49. * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
  50. *
  51. * As U-Boot's block devices do not have a reset function simply return
  52. * EFI_SUCCESS.
  53. *
  54. * See the Unified Extensible Firmware Interface (UEFI) specification for
  55. * details.
  56. *
  57. * @this: pointer to the BLOCK_IO_PROTOCOL
  58. * @extended_verification: extended verification
  59. * Return: status code
  60. */
  61. static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
  62. char extended_verification)
  63. {
  64. EFI_ENTRY("%p, %x", this, extended_verification);
  65. return EFI_EXIT(EFI_SUCCESS);
  66. }
  67. /**
  68. * efi_disk_is_removable() - check if the device is removable media
  69. * @handle: efi object handle;
  70. *
  71. * Examine the device and determine if the device is a local block device
  72. * and removable media.
  73. *
  74. * Return: true if removable, false otherwise
  75. */
  76. bool efi_disk_is_removable(efi_handle_t handle)
  77. {
  78. struct efi_handler *handler;
  79. struct efi_block_io *io;
  80. efi_status_t ret;
  81. ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
  82. if (ret != EFI_SUCCESS)
  83. return false;
  84. io = handler->protocol_interface;
  85. if (!io || !io->media)
  86. return false;
  87. return (bool)io->media->removable_media;
  88. }
  89. enum efi_disk_direction {
  90. EFI_DISK_READ,
  91. EFI_DISK_WRITE,
  92. };
  93. static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
  94. u32 media_id, u64 lba, unsigned long buffer_size,
  95. void *buffer, enum efi_disk_direction direction)
  96. {
  97. struct efi_disk_obj *diskobj;
  98. int blksz;
  99. int blocks;
  100. unsigned long n;
  101. diskobj = container_of(this, struct efi_disk_obj, ops);
  102. blksz = diskobj->media.block_size;
  103. blocks = buffer_size / blksz;
  104. EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
  105. blocks, lba, blksz, direction);
  106. /* We only support full block access */
  107. if (buffer_size & (blksz - 1))
  108. return EFI_BAD_BUFFER_SIZE;
  109. if (CONFIG_IS_ENABLED(PARTITIONS) &&
  110. device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
  111. if (direction == EFI_DISK_READ)
  112. n = disk_blk_read(diskobj->header.dev, lba, blocks,
  113. buffer);
  114. else
  115. n = disk_blk_write(diskobj->header.dev, lba, blocks,
  116. buffer);
  117. } else {
  118. /* dev is a block device (UCLASS_BLK) */
  119. struct blk_desc *desc;
  120. desc = dev_get_uclass_plat(diskobj->header.dev);
  121. if (direction == EFI_DISK_READ)
  122. n = blk_dread(desc, lba, blocks, buffer);
  123. else
  124. n = blk_dwrite(desc, lba, blocks, buffer);
  125. }
  126. /* We don't do interrupts, so check for timers cooperatively */
  127. efi_timer_check();
  128. EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
  129. if (n != blocks)
  130. return EFI_DEVICE_ERROR;
  131. return EFI_SUCCESS;
  132. }
  133. /**
  134. * efi_disk_read_blocks() - reads blocks from device
  135. *
  136. * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
  137. *
  138. * See the Unified Extensible Firmware Interface (UEFI) specification for
  139. * details.
  140. *
  141. * @this: pointer to the BLOCK_IO_PROTOCOL
  142. * @media_id: id of the medium to be read from
  143. * @lba: starting logical block for reading
  144. * @buffer_size: size of the read buffer
  145. * @buffer: pointer to the destination buffer
  146. * Return: status code
  147. */
  148. static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
  149. u32 media_id, u64 lba, efi_uintn_t buffer_size,
  150. void *buffer)
  151. {
  152. void *real_buffer = buffer;
  153. efi_status_t r;
  154. if (!this)
  155. return EFI_INVALID_PARAMETER;
  156. /* TODO: check for media changes */
  157. if (media_id != this->media->media_id)
  158. return EFI_MEDIA_CHANGED;
  159. if (!this->media->media_present)
  160. return EFI_NO_MEDIA;
  161. /* media->io_align is a power of 2 or 0 */
  162. if (this->media->io_align &&
  163. (uintptr_t)buffer & (this->media->io_align - 1))
  164. return EFI_INVALID_PARAMETER;
  165. if (lba * this->media->block_size + buffer_size >
  166. (this->media->last_block + 1) * this->media->block_size)
  167. return EFI_INVALID_PARAMETER;
  168. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  169. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  170. r = efi_disk_read_blocks(this, media_id, lba,
  171. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  172. if (r != EFI_SUCCESS)
  173. return r;
  174. return efi_disk_read_blocks(this, media_id, lba +
  175. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  176. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  177. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  178. }
  179. real_buffer = efi_bounce_buffer;
  180. #endif
  181. EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
  182. buffer_size, buffer);
  183. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  184. EFI_DISK_READ);
  185. /* Copy from bounce buffer to real buffer if necessary */
  186. if ((r == EFI_SUCCESS) && (real_buffer != buffer))
  187. memcpy(buffer, real_buffer, buffer_size);
  188. return EFI_EXIT(r);
  189. }
  190. /**
  191. * efi_disk_write_blocks() - writes blocks to device
  192. *
  193. * This function implements the WriteBlocks service of the
  194. * EFI_BLOCK_IO_PROTOCOL.
  195. *
  196. * See the Unified Extensible Firmware Interface (UEFI) specification for
  197. * details.
  198. *
  199. * @this: pointer to the BLOCK_IO_PROTOCOL
  200. * @media_id: id of the medium to be written to
  201. * @lba: starting logical block for writing
  202. * @buffer_size: size of the write buffer
  203. * @buffer: pointer to the source buffer
  204. * Return: status code
  205. */
  206. static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
  207. u32 media_id, u64 lba, efi_uintn_t buffer_size,
  208. void *buffer)
  209. {
  210. void *real_buffer = buffer;
  211. efi_status_t r;
  212. if (!this)
  213. return EFI_INVALID_PARAMETER;
  214. if (this->media->read_only)
  215. return EFI_WRITE_PROTECTED;
  216. /* TODO: check for media changes */
  217. if (media_id != this->media->media_id)
  218. return EFI_MEDIA_CHANGED;
  219. if (!this->media->media_present)
  220. return EFI_NO_MEDIA;
  221. /* media->io_align is a power of 2 or 0 */
  222. if (this->media->io_align &&
  223. (uintptr_t)buffer & (this->media->io_align - 1))
  224. return EFI_INVALID_PARAMETER;
  225. if (lba * this->media->block_size + buffer_size >
  226. (this->media->last_block + 1) * this->media->block_size)
  227. return EFI_INVALID_PARAMETER;
  228. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  229. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  230. r = efi_disk_write_blocks(this, media_id, lba,
  231. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  232. if (r != EFI_SUCCESS)
  233. return r;
  234. return efi_disk_write_blocks(this, media_id, lba +
  235. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  236. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  237. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  238. }
  239. real_buffer = efi_bounce_buffer;
  240. #endif
  241. EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
  242. buffer_size, buffer);
  243. /* Populate bounce buffer if necessary */
  244. if (real_buffer != buffer)
  245. memcpy(real_buffer, buffer, buffer_size);
  246. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  247. EFI_DISK_WRITE);
  248. return EFI_EXIT(r);
  249. }
  250. /**
  251. * efi_disk_flush_blocks() - flushes modified data to the device
  252. *
  253. * This function implements the FlushBlocks service of the
  254. * EFI_BLOCK_IO_PROTOCOL.
  255. *
  256. * As we always write synchronously nothing is done here.
  257. *
  258. * See the Unified Extensible Firmware Interface (UEFI) specification for
  259. * details.
  260. *
  261. * @this: pointer to the BLOCK_IO_PROTOCOL
  262. * Return: status code
  263. */
  264. static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
  265. {
  266. EFI_ENTRY("%p", this);
  267. return EFI_EXIT(EFI_SUCCESS);
  268. }
  269. static const struct efi_block_io block_io_disk_template = {
  270. .reset = &efi_disk_reset,
  271. .read_blocks = &efi_disk_read_blocks,
  272. .write_blocks = &efi_disk_write_blocks,
  273. .flush_blocks = &efi_disk_flush_blocks,
  274. };
  275. /**
  276. * efi_fs_from_path() - retrieve simple file system protocol
  277. *
  278. * Gets the simple file system protocol for a file device path.
  279. *
  280. * The full path provided is split into device part and into a file
  281. * part. The device part is used to find the handle on which the
  282. * simple file system protocol is installed.
  283. *
  284. * @full_path: device path including device and file
  285. * Return: simple file system protocol
  286. */
  287. struct efi_simple_file_system_protocol *
  288. efi_fs_from_path(struct efi_device_path *full_path)
  289. {
  290. struct efi_object *efiobj;
  291. struct efi_handler *handler;
  292. struct efi_device_path *device_path;
  293. struct efi_device_path *file_path;
  294. efi_status_t ret;
  295. /* Split the path into a device part and a file part */
  296. ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
  297. if (ret != EFI_SUCCESS)
  298. return NULL;
  299. efi_free_pool(file_path);
  300. /* Get the EFI object for the partition */
  301. efiobj = efi_dp_find_obj(device_path, NULL, NULL);
  302. efi_free_pool(device_path);
  303. if (!efiobj)
  304. return NULL;
  305. /* Find the simple file system protocol */
  306. ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
  307. &handler);
  308. if (ret != EFI_SUCCESS)
  309. return NULL;
  310. /* Return the simple file system protocol for the partition */
  311. return handler->protocol_interface;
  312. }
  313. /**
  314. * efi_fs_exists() - check if a partition bears a file system
  315. *
  316. * @desc: block device descriptor
  317. * @part: partition number
  318. * Return: 1 if a file system exists on the partition
  319. * 0 otherwise
  320. */
  321. static int efi_fs_exists(struct blk_desc *desc, int part)
  322. {
  323. if (fs_set_blk_dev_with_part(desc, part))
  324. return 0;
  325. if (fs_get_type() == FS_TYPE_ANY)
  326. return 0;
  327. fs_close();
  328. return 1;
  329. }
  330. /**
  331. * efi_disk_add_dev() - create a handle for a partition or disk
  332. *
  333. * @parent: parent handle
  334. * @dp_parent: parent device path
  335. * @desc: internal block device
  336. * @dev_index: device index for block device
  337. * @part_info: partition info
  338. * @part: partition
  339. * @disk: pointer to receive the created handle
  340. * @agent_handle: handle of the EFI block driver
  341. * Return: disk object
  342. */
  343. static efi_status_t efi_disk_add_dev(
  344. efi_handle_t parent,
  345. struct efi_device_path *dp_parent,
  346. struct blk_desc *desc,
  347. int dev_index,
  348. struct disk_partition *part_info,
  349. unsigned int part,
  350. struct efi_disk_obj **disk,
  351. efi_handle_t agent_handle)
  352. {
  353. struct efi_disk_obj *diskobj;
  354. struct efi_object *handle;
  355. const efi_guid_t *esp_guid = NULL;
  356. efi_status_t ret;
  357. /* Don't add empty devices */
  358. if (!desc->lba)
  359. return EFI_NOT_READY;
  360. diskobj = calloc(1, sizeof(*diskobj));
  361. if (!diskobj)
  362. return EFI_OUT_OF_RESOURCES;
  363. /* Hook up to the device list */
  364. efi_add_handle(&diskobj->header);
  365. /* Fill in object data */
  366. if (part_info) {
  367. struct efi_device_path *node = efi_dp_part_node(desc, part);
  368. struct efi_handler *handler;
  369. void *protocol_interface;
  370. if (!node) {
  371. ret = EFI_OUT_OF_RESOURCES;
  372. log_debug("no node\n");
  373. goto error;
  374. }
  375. /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
  376. ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
  377. if (ret != EFI_SUCCESS) {
  378. log_debug("search failed\n");
  379. goto error;
  380. }
  381. /*
  382. * Link the partition (child controller) to the block device
  383. * (controller).
  384. */
  385. ret = efi_protocol_open(handler, &protocol_interface, NULL,
  386. &diskobj->header,
  387. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
  388. if (ret != EFI_SUCCESS) {
  389. log_debug("prot open failed\n");
  390. goto error;
  391. }
  392. diskobj->dp = efi_dp_append_node(dp_parent, node);
  393. efi_free_pool(node);
  394. diskobj->media.last_block = part_info->size - 1;
  395. if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
  396. esp_guid = &efi_system_partition_guid;
  397. } else {
  398. diskobj->dp = efi_dp_from_part(desc, part);
  399. diskobj->media.last_block = desc->lba - 1;
  400. }
  401. diskobj->part = part;
  402. /*
  403. * Install the device path and the block IO protocol.
  404. *
  405. * InstallMultipleProtocolInterfaces() checks if the device path is
  406. * already installed on an other handle and returns EFI_ALREADY_STARTED
  407. * in this case.
  408. */
  409. handle = &diskobj->header;
  410. ret = efi_install_multiple_protocol_interfaces(
  411. &handle,
  412. &efi_guid_device_path, diskobj->dp,
  413. &efi_block_io_guid, &diskobj->ops,
  414. /*
  415. * esp_guid must be last entry as it
  416. * can be NULL. Its interface is NULL.
  417. */
  418. esp_guid, NULL,
  419. NULL);
  420. if (ret != EFI_SUCCESS) {
  421. log_debug("install failed %lx\n", ret);
  422. goto error;
  423. }
  424. /*
  425. * On partitions or whole disks without partitions install the
  426. * simple file system protocol if a file system is available.
  427. */
  428. if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
  429. efi_fs_exists(desc, part)) {
  430. ret = efi_create_simple_file_system(desc, part, diskobj->dp,
  431. &diskobj->volume);
  432. if (ret != EFI_SUCCESS)
  433. goto error;
  434. ret = efi_add_protocol(&diskobj->header,
  435. &efi_simple_file_system_protocol_guid,
  436. diskobj->volume);
  437. if (ret != EFI_SUCCESS)
  438. goto error;
  439. }
  440. diskobj->ops = block_io_disk_template;
  441. diskobj->dev_index = dev_index;
  442. /* Fill in EFI IO Media info (for read/write callbacks) */
  443. diskobj->media.removable_media = desc->removable;
  444. diskobj->media.media_present = 1;
  445. /*
  446. * MediaID is just an arbitrary counter.
  447. * We have to change it if the medium is removed or changed.
  448. */
  449. diskobj->media.media_id = 1;
  450. diskobj->media.block_size = desc->blksz;
  451. diskobj->media.io_align = desc->blksz;
  452. if (part)
  453. diskobj->media.logical_partition = 1;
  454. diskobj->ops.media = &diskobj->media;
  455. if (disk)
  456. *disk = diskobj;
  457. EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
  458. ", last_block %llu\n",
  459. diskobj->part,
  460. diskobj->media.media_present,
  461. diskobj->media.logical_partition,
  462. diskobj->media.removable_media,
  463. diskobj->media.last_block);
  464. /* Store first EFI system partition */
  465. if (part && efi_system_partition.uclass_id == UCLASS_INVALID) {
  466. if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
  467. efi_system_partition.uclass_id = desc->uclass_id;
  468. efi_system_partition.devnum = desc->devnum;
  469. efi_system_partition.part = part;
  470. EFI_PRINT("EFI system partition: %s %x:%x\n",
  471. blk_get_uclass_name(desc->uclass_id),
  472. desc->devnum, part);
  473. }
  474. }
  475. return EFI_SUCCESS;
  476. error:
  477. efi_delete_handle(&diskobj->header);
  478. free(diskobj->volume);
  479. free(diskobj);
  480. return ret;
  481. }
  482. /**
  483. * efi_disk_create_raw() - create a handle for a whole raw disk
  484. *
  485. * @dev: udevice (UCLASS_BLK)
  486. * @agent_handle: handle of the EFI block driver
  487. *
  488. * Create an efi_disk object which is associated with @dev.
  489. * The type of @dev must be UCLASS_BLK.
  490. *
  491. * Return: 0 on success, -1 otherwise
  492. */
  493. static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
  494. {
  495. struct efi_disk_obj *disk;
  496. struct blk_desc *desc;
  497. int diskid;
  498. efi_status_t ret;
  499. desc = dev_get_uclass_plat(dev);
  500. diskid = desc->devnum;
  501. ret = efi_disk_add_dev(NULL, NULL, desc,
  502. diskid, NULL, 0, &disk, agent_handle);
  503. if (ret != EFI_SUCCESS) {
  504. if (ret == EFI_NOT_READY) {
  505. log_notice("Disk %s not ready\n", dev->name);
  506. ret = -EBUSY;
  507. } else {
  508. log_err("Adding disk for %s failed (err=%ld/%#lx)\n", dev->name, ret, ret);
  509. ret = -ENOENT;
  510. }
  511. return ret;
  512. }
  513. if (efi_link_dev(&disk->header, dev)) {
  514. efi_free_pool(disk->dp);
  515. efi_delete_handle(&disk->header);
  516. return -EINVAL;
  517. }
  518. return 0;
  519. }
  520. /**
  521. * efi_disk_create_part() - create a handle for a disk partition
  522. *
  523. * @dev: udevice (UCLASS_PARTITION)
  524. * @agent_handle: handle of the EFI block driver
  525. *
  526. * Create an efi_disk object which is associated with @dev.
  527. * The type of @dev must be UCLASS_PARTITION.
  528. *
  529. * Return: 0 on success, -1 otherwise
  530. */
  531. static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
  532. {
  533. efi_handle_t parent;
  534. struct blk_desc *desc;
  535. struct disk_part *part_data;
  536. struct disk_partition *info;
  537. unsigned int part;
  538. int diskid;
  539. struct efi_handler *handler;
  540. struct efi_device_path *dp_parent;
  541. struct efi_disk_obj *disk;
  542. efi_status_t ret;
  543. if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
  544. return -1;
  545. desc = dev_get_uclass_plat(dev_get_parent(dev));
  546. diskid = desc->devnum;
  547. part_data = dev_get_uclass_plat(dev);
  548. part = part_data->partnum;
  549. info = &part_data->gpt_part_info;
  550. ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
  551. if (ret != EFI_SUCCESS)
  552. return -1;
  553. dp_parent = (struct efi_device_path *)handler->protocol_interface;
  554. ret = efi_disk_add_dev(parent, dp_parent, desc, diskid,
  555. info, part, &disk, agent_handle);
  556. if (ret != EFI_SUCCESS) {
  557. log_err("Adding partition for %s failed\n", dev->name);
  558. return -1;
  559. }
  560. if (efi_link_dev(&disk->header, dev)) {
  561. efi_free_pool(disk->dp);
  562. efi_delete_handle(&disk->header);
  563. return -1;
  564. }
  565. return 0;
  566. }
  567. /**
  568. * efi_disk_probe() - create efi_disk objects for a block device
  569. *
  570. * @ctx: event context - driver binding protocol
  571. * @event: EV_PM_POST_PROBE event
  572. *
  573. * Create efi_disk objects for partitions as well as a raw disk
  574. * which is associated with @dev.
  575. * The type of @dev must be UCLASS_BLK.
  576. * This function is expected to be called at EV_PM_POST_PROBE.
  577. *
  578. * Return: 0 on success, -1 otherwise
  579. */
  580. int efi_disk_probe(void *ctx, struct event *event)
  581. {
  582. struct udevice *dev;
  583. enum uclass_id id;
  584. struct blk_desc *desc;
  585. struct udevice *child;
  586. struct efi_driver_binding_extended_protocol *db_prot = ctx;
  587. efi_handle_t agent_handle = db_prot->bp.driver_binding_handle;
  588. int ret;
  589. dev = event->data.dm.dev;
  590. id = device_get_uclass_id(dev);
  591. /* We won't support partitions in a partition */
  592. if (id != UCLASS_BLK)
  593. return 0;
  594. /*
  595. * Avoid creating duplicated objects now that efi_driver
  596. * has already created an efi_disk at this moment.
  597. */
  598. desc = dev_get_uclass_plat(dev);
  599. if (desc->uclass_id != UCLASS_EFI_LOADER) {
  600. ret = efi_disk_create_raw(dev, agent_handle);
  601. if (ret)
  602. return -1;
  603. }
  604. device_foreach_child(child, dev) {
  605. ret = efi_disk_create_part(child, agent_handle);
  606. if (ret)
  607. return -1;
  608. }
  609. return 0;
  610. }
  611. /**
  612. * efi_disk_remove - delete an efi_disk object for a block device or partition
  613. *
  614. * @ctx: event context: driver binding protocol
  615. * @event: EV_PM_PRE_REMOVE event
  616. *
  617. * Delete an efi_disk object which is associated with the UCLASS_BLK or
  618. * UCLASS_PARTITION device for which the EV_PM_PRE_REMOVE event is raised.
  619. *
  620. * Return: 0 on success, -1 otherwise
  621. */
  622. int efi_disk_remove(void *ctx, struct event *event)
  623. {
  624. enum uclass_id id;
  625. struct udevice *dev = event->data.dm.dev;
  626. efi_handle_t handle;
  627. struct blk_desc *desc;
  628. struct efi_disk_obj *diskobj = NULL;
  629. efi_status_t ret;
  630. if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
  631. return 0;
  632. id = device_get_uclass_id(dev);
  633. switch (id) {
  634. case UCLASS_BLK:
  635. desc = dev_get_uclass_plat(dev);
  636. if (desc && desc->uclass_id != UCLASS_EFI_LOADER)
  637. diskobj = container_of(handle, struct efi_disk_obj,
  638. header);
  639. break;
  640. case UCLASS_PARTITION:
  641. diskobj = container_of(handle, struct efi_disk_obj, header);
  642. break;
  643. default:
  644. return 0;
  645. }
  646. ret = efi_delete_handle(handle);
  647. /* Do not delete DM device if there are still EFI drivers attached. */
  648. if (ret != EFI_SUCCESS)
  649. return -1;
  650. if (diskobj)
  651. efi_free_pool(diskobj->dp);
  652. dev_tag_del(dev, DM_TAG_EFI);
  653. return 0;
  654. }
  655. /**
  656. * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
  657. *
  658. * @handle: pointer to the EFI handle
  659. * @buf: pointer to the buffer to store the string
  660. * @size: size of buffer
  661. * Return: status code
  662. */
  663. efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
  664. {
  665. int count;
  666. int diskid;
  667. enum uclass_id id;
  668. unsigned int part;
  669. struct udevice *dev;
  670. struct blk_desc *desc;
  671. const char *if_typename;
  672. bool is_partition = false;
  673. struct disk_part *part_data;
  674. if (!handle || !buf || !size)
  675. return EFI_INVALID_PARAMETER;
  676. dev = handle->dev;
  677. id = device_get_uclass_id(dev);
  678. if (id == UCLASS_BLK) {
  679. desc = dev_get_uclass_plat(dev);
  680. } else if (id == UCLASS_PARTITION) {
  681. desc = dev_get_uclass_plat(dev_get_parent(dev));
  682. is_partition = true;
  683. } else {
  684. return EFI_INVALID_PARAMETER;
  685. }
  686. if_typename = blk_get_uclass_name(desc->uclass_id);
  687. diskid = desc->devnum;
  688. if (is_partition) {
  689. part_data = dev_get_uclass_plat(dev);
  690. part = part_data->partnum;
  691. count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
  692. part);
  693. } else {
  694. count = snprintf(buf, size, "%s %d", if_typename, diskid);
  695. }
  696. if (count < 0 || (count + 1) > size)
  697. return EFI_INVALID_PARAMETER;
  698. return EFI_SUCCESS;
  699. }
  700. /**
  701. * efi_disks_register() - ensure all block devices are available in UEFI
  702. *
  703. * The function probes all block devices. As we store UEFI variables on the
  704. * EFI system partition this function has to be called before enabling
  705. * variable services.
  706. */
  707. efi_status_t efi_disks_register(void)
  708. {
  709. struct udevice *dev;
  710. uclass_foreach_dev_probe(UCLASS_BLK, dev) {
  711. }
  712. return EFI_SUCCESS;
  713. }