efi_disk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 <fs.h>
  12. #include <part.h>
  13. #include <malloc.h>
  14. struct efi_system_partition efi_system_partition;
  15. const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
  16. /**
  17. * struct efi_disk_obj - EFI disk object
  18. *
  19. * @header: EFI object header
  20. * @ops: EFI disk I/O protocol interface
  21. * @ifname: interface name for block device
  22. * @dev_index: device index of block device
  23. * @media: block I/O media information
  24. * @dp: device path to the block device
  25. * @part: partition
  26. * @volume: simple file system protocol of the partition
  27. * @offset: offset into disk for simple partition
  28. * @desc: internal block device descriptor
  29. */
  30. struct efi_disk_obj {
  31. struct efi_object header;
  32. struct efi_block_io ops;
  33. const char *ifname;
  34. int dev_index;
  35. struct efi_block_io_media media;
  36. struct efi_device_path *dp;
  37. unsigned int part;
  38. struct efi_simple_file_system_protocol *volume;
  39. lbaint_t offset;
  40. struct blk_desc *desc;
  41. };
  42. /**
  43. * efi_disk_reset() - reset block device
  44. *
  45. * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
  46. *
  47. * As U-Boot's block devices do not have a reset function simply return
  48. * EFI_SUCCESS.
  49. *
  50. * See the Unified Extensible Firmware Interface (UEFI) specification for
  51. * details.
  52. *
  53. * @this: pointer to the BLOCK_IO_PROTOCOL
  54. * @extended_verification: extended verification
  55. * Return: status code
  56. */
  57. static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
  58. char extended_verification)
  59. {
  60. EFI_ENTRY("%p, %x", this, extended_verification);
  61. return EFI_EXIT(EFI_SUCCESS);
  62. }
  63. enum efi_disk_direction {
  64. EFI_DISK_READ,
  65. EFI_DISK_WRITE,
  66. };
  67. static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
  68. u32 media_id, u64 lba, unsigned long buffer_size,
  69. void *buffer, enum efi_disk_direction direction)
  70. {
  71. struct efi_disk_obj *diskobj;
  72. struct blk_desc *desc;
  73. int blksz;
  74. int blocks;
  75. unsigned long n;
  76. diskobj = container_of(this, struct efi_disk_obj, ops);
  77. desc = (struct blk_desc *) diskobj->desc;
  78. blksz = desc->blksz;
  79. blocks = buffer_size / blksz;
  80. lba += diskobj->offset;
  81. EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
  82. blocks, lba, blksz, direction);
  83. /* We only support full block access */
  84. if (buffer_size & (blksz - 1))
  85. return EFI_BAD_BUFFER_SIZE;
  86. if (direction == EFI_DISK_READ)
  87. n = blk_dread(desc, lba, blocks, buffer);
  88. else
  89. n = blk_dwrite(desc, lba, blocks, buffer);
  90. /* We don't do interrupts, so check for timers cooperatively */
  91. efi_timer_check();
  92. EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
  93. if (n != blocks)
  94. return EFI_DEVICE_ERROR;
  95. return EFI_SUCCESS;
  96. }
  97. /**
  98. * efi_disk_read_blocks() - reads blocks from device
  99. *
  100. * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
  101. *
  102. * See the Unified Extensible Firmware Interface (UEFI) specification for
  103. * details.
  104. *
  105. * @this: pointer to the BLOCK_IO_PROTOCOL
  106. * @media_id: id of the medium to be read from
  107. * @lba: starting logical block for reading
  108. * @buffer_size: size of the read buffer
  109. * @buffer: pointer to the destination buffer
  110. * Return: status code
  111. */
  112. static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
  113. u32 media_id, u64 lba, efi_uintn_t buffer_size,
  114. void *buffer)
  115. {
  116. void *real_buffer = buffer;
  117. efi_status_t r;
  118. if (!this)
  119. return EFI_INVALID_PARAMETER;
  120. /* TODO: check for media changes */
  121. if (media_id != this->media->media_id)
  122. return EFI_MEDIA_CHANGED;
  123. if (!this->media->media_present)
  124. return EFI_NO_MEDIA;
  125. /* media->io_align is a power of 2 */
  126. if ((uintptr_t)buffer & (this->media->io_align - 1))
  127. return EFI_INVALID_PARAMETER;
  128. if (lba * this->media->block_size + buffer_size >
  129. this->media->last_block * this->media->block_size)
  130. return EFI_INVALID_PARAMETER;
  131. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  132. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  133. r = efi_disk_read_blocks(this, media_id, lba,
  134. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  135. if (r != EFI_SUCCESS)
  136. return r;
  137. return efi_disk_read_blocks(this, media_id, lba +
  138. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  139. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  140. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  141. }
  142. real_buffer = efi_bounce_buffer;
  143. #endif
  144. EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
  145. buffer_size, buffer);
  146. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  147. EFI_DISK_READ);
  148. /* Copy from bounce buffer to real buffer if necessary */
  149. if ((r == EFI_SUCCESS) && (real_buffer != buffer))
  150. memcpy(buffer, real_buffer, buffer_size);
  151. return EFI_EXIT(r);
  152. }
  153. /**
  154. * efi_disk_write_blocks() - writes blocks to device
  155. *
  156. * This function implements the WriteBlocks service of the
  157. * EFI_BLOCK_IO_PROTOCOL.
  158. *
  159. * See the Unified Extensible Firmware Interface (UEFI) specification for
  160. * details.
  161. *
  162. * @this: pointer to the BLOCK_IO_PROTOCOL
  163. * @media_id: id of the medium to be written to
  164. * @lba: starting logical block for writing
  165. * @buffer_size: size of the write buffer
  166. * @buffer: pointer to the source buffer
  167. * Return: status code
  168. */
  169. static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
  170. u32 media_id, u64 lba, efi_uintn_t buffer_size,
  171. void *buffer)
  172. {
  173. void *real_buffer = buffer;
  174. efi_status_t r;
  175. if (!this)
  176. return EFI_INVALID_PARAMETER;
  177. if (this->media->read_only)
  178. return EFI_WRITE_PROTECTED;
  179. /* TODO: check for media changes */
  180. if (media_id != this->media->media_id)
  181. return EFI_MEDIA_CHANGED;
  182. if (!this->media->media_present)
  183. return EFI_NO_MEDIA;
  184. /* media->io_align is a power of 2 */
  185. if ((uintptr_t)buffer & (this->media->io_align - 1))
  186. return EFI_INVALID_PARAMETER;
  187. if (lba * this->media->block_size + buffer_size >
  188. this->media->last_block * this->media->block_size)
  189. return EFI_INVALID_PARAMETER;
  190. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  191. if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
  192. r = efi_disk_write_blocks(this, media_id, lba,
  193. EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
  194. if (r != EFI_SUCCESS)
  195. return r;
  196. return efi_disk_write_blocks(this, media_id, lba +
  197. EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
  198. buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
  199. buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
  200. }
  201. real_buffer = efi_bounce_buffer;
  202. #endif
  203. EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
  204. buffer_size, buffer);
  205. /* Populate bounce buffer if necessary */
  206. if (real_buffer != buffer)
  207. memcpy(real_buffer, buffer, buffer_size);
  208. r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
  209. EFI_DISK_WRITE);
  210. return EFI_EXIT(r);
  211. }
  212. /**
  213. * efi_disk_flush_blocks() - flushes modified data to the device
  214. *
  215. * This function implements the FlushBlocks service of the
  216. * EFI_BLOCK_IO_PROTOCOL.
  217. *
  218. * As we always write synchronously nothing is done here.
  219. *
  220. * See the Unified Extensible Firmware Interface (UEFI) specification for
  221. * details.
  222. *
  223. * @this: pointer to the BLOCK_IO_PROTOCOL
  224. * Return: status code
  225. */
  226. static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
  227. {
  228. EFI_ENTRY("%p", this);
  229. return EFI_EXIT(EFI_SUCCESS);
  230. }
  231. static const struct efi_block_io block_io_disk_template = {
  232. .reset = &efi_disk_reset,
  233. .read_blocks = &efi_disk_read_blocks,
  234. .write_blocks = &efi_disk_write_blocks,
  235. .flush_blocks = &efi_disk_flush_blocks,
  236. };
  237. /**
  238. * efi_fs_from_path() - retrieve simple file system protocol
  239. *
  240. * Gets the simple file system protocol for a file device path.
  241. *
  242. * The full path provided is split into device part and into a file
  243. * part. The device part is used to find the handle on which the
  244. * simple file system protocol is installed.
  245. *
  246. * @full_path: device path including device and file
  247. * Return: simple file system protocol
  248. */
  249. struct efi_simple_file_system_protocol *
  250. efi_fs_from_path(struct efi_device_path *full_path)
  251. {
  252. struct efi_object *efiobj;
  253. struct efi_handler *handler;
  254. struct efi_device_path *device_path;
  255. struct efi_device_path *file_path;
  256. efi_status_t ret;
  257. /* Split the path into a device part and a file part */
  258. ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
  259. if (ret != EFI_SUCCESS)
  260. return NULL;
  261. efi_free_pool(file_path);
  262. /* Get the EFI object for the partition */
  263. efiobj = efi_dp_find_obj(device_path, NULL);
  264. efi_free_pool(device_path);
  265. if (!efiobj)
  266. return NULL;
  267. /* Find the simple file system protocol */
  268. ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
  269. &handler);
  270. if (ret != EFI_SUCCESS)
  271. return NULL;
  272. /* Return the simple file system protocol for the partition */
  273. return handler->protocol_interface;
  274. }
  275. /**
  276. * efi_fs_exists() - check if a partition bears a file system
  277. *
  278. * @desc: block device descriptor
  279. * @part: partition number
  280. * Return: 1 if a file system exists on the partition
  281. * 0 otherwise
  282. */
  283. static int efi_fs_exists(struct blk_desc *desc, int part)
  284. {
  285. if (fs_set_blk_dev_with_part(desc, part))
  286. return 0;
  287. if (fs_get_type() == FS_TYPE_ANY)
  288. return 0;
  289. fs_close();
  290. return 1;
  291. }
  292. /**
  293. * efi_disk_add_dev() - create a handle for a partition or disk
  294. *
  295. * @parent: parent handle
  296. * @dp_parent: parent device path
  297. * @if_typename: interface name for block device
  298. * @desc: internal block device
  299. * @dev_index: device index for block device
  300. * @offset: offset into disk for simple partitions
  301. * @part: partition
  302. * @disk: pointer to receive the created handle
  303. * Return: disk object
  304. */
  305. static efi_status_t efi_disk_add_dev(
  306. efi_handle_t parent,
  307. struct efi_device_path *dp_parent,
  308. const char *if_typename,
  309. struct blk_desc *desc,
  310. int dev_index,
  311. lbaint_t offset,
  312. unsigned int part,
  313. struct efi_disk_obj **disk)
  314. {
  315. struct efi_disk_obj *diskobj;
  316. efi_status_t ret;
  317. /* Don't add empty devices */
  318. if (!desc->lba)
  319. return EFI_NOT_READY;
  320. diskobj = calloc(1, sizeof(*diskobj));
  321. if (!diskobj)
  322. return EFI_OUT_OF_RESOURCES;
  323. /* Hook up to the device list */
  324. efi_add_handle(&diskobj->header);
  325. /* Fill in object data */
  326. if (part) {
  327. struct efi_device_path *node = efi_dp_part_node(desc, part);
  328. diskobj->dp = efi_dp_append_node(dp_parent, node);
  329. efi_free_pool(node);
  330. } else {
  331. diskobj->dp = efi_dp_from_part(desc, part);
  332. }
  333. diskobj->part = part;
  334. ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
  335. &diskobj->ops);
  336. if (ret != EFI_SUCCESS)
  337. return ret;
  338. ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
  339. diskobj->dp);
  340. if (ret != EFI_SUCCESS)
  341. return ret;
  342. /* partitions or whole disk without partitions */
  343. if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
  344. efi_fs_exists(desc, part)) {
  345. diskobj->volume = efi_simple_file_system(desc, part,
  346. diskobj->dp);
  347. ret = efi_add_protocol(&diskobj->header,
  348. &efi_simple_file_system_protocol_guid,
  349. diskobj->volume);
  350. if (ret != EFI_SUCCESS)
  351. return ret;
  352. }
  353. diskobj->ops = block_io_disk_template;
  354. diskobj->ifname = if_typename;
  355. diskobj->dev_index = dev_index;
  356. diskobj->offset = offset;
  357. diskobj->desc = desc;
  358. /* Fill in EFI IO Media info (for read/write callbacks) */
  359. diskobj->media.removable_media = desc->removable;
  360. diskobj->media.media_present = 1;
  361. /*
  362. * MediaID is just an arbitrary counter.
  363. * We have to change it if the medium is removed or changed.
  364. */
  365. diskobj->media.media_id = 1;
  366. diskobj->media.block_size = desc->blksz;
  367. diskobj->media.io_align = desc->blksz;
  368. diskobj->media.last_block = desc->lba - offset;
  369. if (part)
  370. diskobj->media.logical_partition = 1;
  371. diskobj->ops.media = &diskobj->media;
  372. if (disk)
  373. *disk = diskobj;
  374. /* Store first EFI system partition */
  375. if (part && !efi_system_partition.if_type) {
  376. int r;
  377. struct disk_partition info;
  378. r = part_get_info(desc, part, &info);
  379. if (r)
  380. return EFI_DEVICE_ERROR;
  381. if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
  382. efi_system_partition.if_type = desc->if_type;
  383. efi_system_partition.devnum = desc->devnum;
  384. efi_system_partition.part = part;
  385. EFI_PRINT("EFI system partition: %s %d:%d\n",
  386. blk_get_if_type_name(desc->if_type),
  387. desc->devnum, part);
  388. }
  389. }
  390. return EFI_SUCCESS;
  391. }
  392. /**
  393. * efi_disk_create_partitions() - create handles and protocols for partitions
  394. *
  395. * Create handles and protocols for the partitions of a block device.
  396. *
  397. * @parent: handle of the parent disk
  398. * @desc: block device
  399. * @if_typename: interface type
  400. * @diskid: device number
  401. * @pdevname: device name
  402. * Return: number of partitions created
  403. */
  404. int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
  405. const char *if_typename, int diskid,
  406. const char *pdevname)
  407. {
  408. int disks = 0;
  409. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  410. struct disk_partition info;
  411. int part;
  412. struct efi_device_path *dp = NULL;
  413. efi_status_t ret;
  414. struct efi_handler *handler;
  415. /* Get the device path of the parent */
  416. ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
  417. if (ret == EFI_SUCCESS)
  418. dp = handler->protocol_interface;
  419. /* Add devices for each partition */
  420. for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
  421. if (part_get_info(desc, part, &info))
  422. continue;
  423. snprintf(devname, sizeof(devname), "%s:%d", pdevname,
  424. part);
  425. ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
  426. info.start, part, NULL);
  427. if (ret != EFI_SUCCESS) {
  428. printf("Adding partition %s failed\n", pdevname);
  429. continue;
  430. }
  431. disks++;
  432. }
  433. return disks;
  434. }
  435. /**
  436. * efi_disk_register() - register block devices
  437. *
  438. * U-Boot doesn't have a list of all online disk devices. So when running our
  439. * EFI payload, we scan through all of the potentially available ones and
  440. * store them in our object pool.
  441. *
  442. * This function is called in efi_init_obj_list().
  443. *
  444. * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
  445. * Consider converting the code to look up devices as needed. The EFI device
  446. * could be a child of the UCLASS_BLK block device, perhaps.
  447. *
  448. * Return: status code
  449. */
  450. efi_status_t efi_disk_register(void)
  451. {
  452. struct efi_disk_obj *disk;
  453. int disks = 0;
  454. efi_status_t ret;
  455. #ifdef CONFIG_BLK
  456. struct udevice *dev;
  457. for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
  458. uclass_next_device_check(&dev)) {
  459. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  460. const char *if_typename = blk_get_if_type_name(desc->if_type);
  461. /* Add block device for the full device */
  462. printf("Scanning disk %s...\n", dev->name);
  463. ret = efi_disk_add_dev(NULL, NULL, if_typename,
  464. desc, desc->devnum, 0, 0, &disk);
  465. if (ret == EFI_NOT_READY) {
  466. printf("Disk %s not ready\n", dev->name);
  467. continue;
  468. }
  469. if (ret) {
  470. printf("ERROR: failure to add disk device %s, r = %lu\n",
  471. dev->name, ret & ~EFI_ERROR_MASK);
  472. return ret;
  473. }
  474. disks++;
  475. /* Partitions show up as block devices in EFI */
  476. disks += efi_disk_create_partitions(
  477. &disk->header, desc, if_typename,
  478. desc->devnum, dev->name);
  479. }
  480. #else
  481. int i, if_type;
  482. /* Search for all available disk devices */
  483. for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
  484. const struct blk_driver *cur_drvr;
  485. const char *if_typename;
  486. cur_drvr = blk_driver_lookup_type(if_type);
  487. if (!cur_drvr)
  488. continue;
  489. if_typename = cur_drvr->if_typename;
  490. printf("Scanning disks on %s...\n", if_typename);
  491. for (i = 0; i < 4; i++) {
  492. struct blk_desc *desc;
  493. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  494. desc = blk_get_devnum_by_type(if_type, i);
  495. if (!desc)
  496. continue;
  497. if (desc->type == DEV_TYPE_UNKNOWN)
  498. continue;
  499. snprintf(devname, sizeof(devname), "%s%d",
  500. if_typename, i);
  501. /* Add block device for the full device */
  502. ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
  503. i, 0, 0, &disk);
  504. if (ret == EFI_NOT_READY) {
  505. printf("Disk %s not ready\n", devname);
  506. continue;
  507. }
  508. if (ret) {
  509. printf("ERROR: failure to add disk device %s, r = %lu\n",
  510. devname, ret & ~EFI_ERROR_MASK);
  511. return ret;
  512. }
  513. disks++;
  514. /* Partitions show up as block devices in EFI */
  515. disks += efi_disk_create_partitions
  516. (&disk->header, desc,
  517. if_typename, i, devname);
  518. }
  519. }
  520. #endif
  521. printf("Found %d disks\n", disks);
  522. return EFI_SUCCESS;
  523. }
  524. /**
  525. * efi_disk_is_system_part() - check if handle refers to an EFI system partition
  526. *
  527. * @handle: handle of partition
  528. *
  529. * Return: true if handle refers to an EFI system partition
  530. */
  531. bool efi_disk_is_system_part(efi_handle_t handle)
  532. {
  533. struct efi_handler *handler;
  534. struct efi_disk_obj *diskobj;
  535. struct disk_partition info;
  536. efi_status_t ret;
  537. int r;
  538. /* check if this is a block device */
  539. ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
  540. if (ret != EFI_SUCCESS)
  541. return false;
  542. diskobj = container_of(handle, struct efi_disk_obj, header);
  543. r = part_get_info(diskobj->desc, diskobj->part, &info);
  544. if (r)
  545. return false;
  546. return !!(info.bootable & PART_EFI_SYSTEM_PARTITION);
  547. }