Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI block driver |
| 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt |
| 6 | * |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 7 | * The EFI uclass creates a handle for this driver and installs the |
| 8 | * driver binding protocol on it. |
| 9 | * |
| 10 | * The EFI block driver binds to controllers implementing the block io |
| 11 | * protocol. |
| 12 | * |
| 13 | * When the bind function of the EFI block driver is called it creates a |
| 14 | * new U-Boot block device. It installs child handles for all partitions and |
| 15 | * installs the simple file protocol on these. |
| 16 | * |
| 17 | * The read and write functions of the EFI block driver delegate calls to the |
| 18 | * controller that it is bound to. |
| 19 | * |
| 20 | * A usage example is as following: |
| 21 | * |
| 22 | * U-Boot loads the iPXE snp.efi executable. iPXE connects an iSCSI drive and |
| 23 | * exposes a handle with the block IO protocol. It calls ConnectController. |
| 24 | * |
| 25 | * Now the EFI block driver installs the partitions with the simple file |
| 26 | * protocol. |
| 27 | * |
| 28 | * iPXE uses the simple file protocol to load Grub or the Linux Kernel. |
| 29 | */ |
| 30 | |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 31 | #include <common.h> |
| 32 | #include <blk.h> |
Simon Glass | e1e10f2 | 2020-07-19 10:15:43 -0600 | [diff] [blame] | 33 | #include <dm.h> |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 34 | #include <efi_driver.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 35 | #include <malloc.h> |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 36 | #include <dm/device-internal.h> |
| 37 | #include <dm/root.h> |
AKASHI Takahiro | a9bf024 | 2022-04-19 10:05:12 +0900 | [diff] [blame] | 38 | #include <dm/tag.h> |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * EFI attributes of the udevice handled by this driver. |
| 42 | * |
| 43 | * handle handle of the controller on which this driver is installed |
| 44 | * io block io protocol proxied by this driver |
| 45 | */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 46 | struct efi_blk_plat { |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 47 | efi_handle_t handle; |
| 48 | struct efi_block_io *io; |
| 49 | }; |
| 50 | |
Heinrich Schuchardt | 3f33f30 | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 51 | /** |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 52 | * Read from block device |
| 53 | * |
Heinrich Schuchardt | 3f33f30 | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 54 | * @dev: device |
| 55 | * @blknr: first block to be read |
| 56 | * @blkcnt: number of blocks to read |
| 57 | * @buffer: output buffer |
| 58 | * Return: number of blocks transferred |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 59 | */ |
| 60 | static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 61 | void *buffer) |
| 62 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 63 | struct efi_blk_plat *plat = dev_get_plat(dev); |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 64 | struct efi_block_io *io = plat->io; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 65 | efi_status_t ret; |
| 66 | |
| 67 | EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n", |
| 68 | __func__, dev->name, blknr, blkcnt); |
| 69 | ret = EFI_CALL(io->read_blocks( |
| 70 | io, io->media->media_id, (u64)blknr, |
| 71 | (efi_uintn_t)blkcnt * |
| 72 | (efi_uintn_t)io->media->block_size, buffer)); |
| 73 | EFI_PRINT("%s: r = %u\n", __func__, |
| 74 | (unsigned int)(ret & ~EFI_ERROR_MASK)); |
| 75 | if (ret != EFI_SUCCESS) |
| 76 | return 0; |
| 77 | return blkcnt; |
| 78 | } |
| 79 | |
Heinrich Schuchardt | 3f33f30 | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 80 | /** |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 81 | * Write to block device |
| 82 | * |
Heinrich Schuchardt | 3f33f30 | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 83 | * @dev: device |
| 84 | * @blknr: first block to be write |
| 85 | * @blkcnt: number of blocks to write |
| 86 | * @buffer: input buffer |
| 87 | * Return: number of blocks transferred |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 88 | */ |
| 89 | static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 90 | const void *buffer) |
| 91 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 92 | struct efi_blk_plat *plat = dev_get_plat(dev); |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 93 | struct efi_block_io *io = plat->io; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 94 | efi_status_t ret; |
| 95 | |
| 96 | EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n", |
| 97 | __func__, dev->name, blknr, blkcnt); |
| 98 | ret = EFI_CALL(io->write_blocks( |
| 99 | io, io->media->media_id, (u64)blknr, |
| 100 | (efi_uintn_t)blkcnt * |
| 101 | (efi_uintn_t)io->media->block_size, |
| 102 | (void *)buffer)); |
| 103 | EFI_PRINT("%s: r = %u\n", __func__, |
| 104 | (unsigned int)(ret & ~EFI_ERROR_MASK)); |
| 105 | if (ret != EFI_SUCCESS) |
| 106 | return 0; |
| 107 | return blkcnt; |
| 108 | } |
| 109 | |
Heinrich Schuchardt | 3f33f30 | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 110 | /** |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 111 | * Create a block device for a handle |
| 112 | * |
Heinrich Schuchardt | 3f33f30 | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 113 | * @handle: handle |
| 114 | * @interface: block io protocol |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 115 | * Return: status code |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 116 | */ |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 117 | static efi_status_t efi_bl_bind(efi_handle_t handle, void *interface) |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 118 | { |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 119 | struct udevice *bdev = NULL, *parent = dm_root(); |
| 120 | efi_status_t ret; |
| 121 | int devnum; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 122 | char *name; |
| 123 | struct efi_object *obj = efi_search_obj(handle); |
| 124 | struct efi_block_io *io = interface; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 125 | struct efi_blk_plat *plat; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 126 | |
| 127 | EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io); |
| 128 | |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 129 | if (!obj || !interface) |
| 130 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 131 | |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 132 | devnum = blk_find_max_devnum(UCLASS_EFI_LOADER); |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 133 | if (devnum == -ENODEV) |
| 134 | devnum = 0; |
| 135 | else if (devnum < 0) |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 136 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 137 | |
| 138 | name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */ |
| 139 | if (!name) |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 140 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 141 | sprintf(name, "efiblk#%d", devnum); |
| 142 | |
| 143 | /* Create driver model udevice for the EFI block io device */ |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 144 | if (blk_create_device(parent, "efi_blk", name, UCLASS_EFI_LOADER, |
| 145 | devnum, io->media->block_size, |
| 146 | (lbaint_t)io->media->last_block, &bdev)) { |
| 147 | ret = EFI_OUT_OF_RESOURCES; |
| 148 | free(name); |
| 149 | goto err; |
| 150 | } |
Heinrich Schuchardt | df76431 | 2018-06-30 07:11:32 +0200 | [diff] [blame] | 151 | /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */ |
| 152 | device_set_name_alloced(bdev); |
Bin Meng | f26ce03 | 2018-10-15 02:21:06 -0700 | [diff] [blame] | 153 | |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 154 | plat = dev_get_plat(bdev); |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 155 | plat->handle = handle; |
| 156 | plat->io = interface; |
Bin Meng | f26ce03 | 2018-10-15 02:21:06 -0700 | [diff] [blame] | 157 | |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 158 | if (efi_link_dev(handle, bdev)) { |
| 159 | ret = EFI_OUT_OF_RESOURCES; |
| 160 | goto err; |
| 161 | } |
AKASHI Takahiro | a9bf024 | 2022-04-19 10:05:12 +0900 | [diff] [blame] | 162 | |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 163 | if (device_probe(bdev)) { |
| 164 | ret = EFI_DEVICE_ERROR; |
| 165 | goto err; |
| 166 | } |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 167 | EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name); |
| 168 | |
Heinrich Schuchardt | 43a5891 | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 169 | return EFI_SUCCESS; |
| 170 | |
| 171 | err: |
| 172 | efi_unlink_dev(handle); |
| 173 | if (bdev) |
| 174 | device_unbind(bdev); |
| 175 | |
| 176 | return ret; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Block device driver operators */ |
| 180 | static const struct blk_ops efi_blk_ops = { |
| 181 | .read = efi_bl_read, |
| 182 | .write = efi_bl_write, |
| 183 | }; |
| 184 | |
| 185 | /* Identify as block device driver */ |
| 186 | U_BOOT_DRIVER(efi_blk) = { |
| 187 | .name = "efi_blk", |
| 188 | .id = UCLASS_BLK, |
| 189 | .ops = &efi_blk_ops, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 190 | .plat_auto = sizeof(struct efi_blk_plat), |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | /* EFI driver operators */ |
| 194 | static const struct efi_driver_ops driver_ops = { |
| 195 | .protocol = &efi_block_io_guid, |
| 196 | .child_protocol = &efi_block_io_guid, |
| 197 | .bind = efi_bl_bind, |
| 198 | }; |
| 199 | |
| 200 | /* Identify as EFI driver */ |
| 201 | U_BOOT_DRIVER(efi_block) = { |
| 202 | .name = "EFI block driver", |
Simon Glass | 2abd8d1 | 2021-12-04 08:56:30 -0700 | [diff] [blame] | 203 | .id = UCLASS_EFI_LOADER, |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 204 | .ops = &driver_ops, |
| 205 | }; |