Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI block driver |
| 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt |
| 6 | * |
Heinrich Schuchardt | 11206f4 | 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 | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 31 | #include <blk.h> |
Simon Glass | f6977c1 | 2020-07-19 10:15:43 -0600 | [diff] [blame] | 32 | #include <dm.h> |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 33 | #include <efi_driver.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 34 | #include <malloc.h> |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 35 | #include <dm/device-internal.h> |
| 36 | #include <dm/root.h> |
AKASHI Takahiro | 2381f2e | 2022-04-19 10:05:12 +0900 | [diff] [blame] | 37 | #include <dm/tag.h> |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 38 | |
Heinrich Schuchardt | 35679a9 | 2022-10-04 18:53:34 +0200 | [diff] [blame] | 39 | /** |
| 40 | * struct efi_blk_plat - attributes of a block device |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 41 | * |
Heinrich Schuchardt | 35679a9 | 2022-10-04 18:53:34 +0200 | [diff] [blame] | 42 | * @handle: handle of the controller on which this driver is installed |
| 43 | * @io: block io protocol proxied by this driver |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 44 | */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 45 | struct efi_blk_plat { |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 46 | efi_handle_t handle; |
| 47 | struct efi_block_io *io; |
| 48 | }; |
| 49 | |
Heinrich Schuchardt | 6492dad | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 50 | /** |
Heinrich Schuchardt | 35679a9 | 2022-10-04 18:53:34 +0200 | [diff] [blame] | 51 | * efi_bl_read() - read from block device |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 52 | * |
Heinrich Schuchardt | 6492dad | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 53 | * @dev: device |
| 54 | * @blknr: first block to be read |
| 55 | * @blkcnt: number of blocks to read |
| 56 | * @buffer: output buffer |
| 57 | * Return: number of blocks transferred |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 58 | */ |
| 59 | static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 60 | void *buffer) |
| 61 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 62 | struct efi_blk_plat *plat = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 63 | struct efi_block_io *io = plat->io; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 64 | efi_status_t ret; |
| 65 | |
| 66 | EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n", |
| 67 | __func__, dev->name, blknr, blkcnt); |
| 68 | ret = EFI_CALL(io->read_blocks( |
| 69 | io, io->media->media_id, (u64)blknr, |
| 70 | (efi_uintn_t)blkcnt * |
| 71 | (efi_uintn_t)io->media->block_size, buffer)); |
| 72 | EFI_PRINT("%s: r = %u\n", __func__, |
| 73 | (unsigned int)(ret & ~EFI_ERROR_MASK)); |
| 74 | if (ret != EFI_SUCCESS) |
| 75 | return 0; |
| 76 | return blkcnt; |
| 77 | } |
| 78 | |
Heinrich Schuchardt | 6492dad | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 79 | /** |
Heinrich Schuchardt | 35679a9 | 2022-10-04 18:53:34 +0200 | [diff] [blame] | 80 | * efi_bl_write() - write to block device |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 81 | * |
Heinrich Schuchardt | 6492dad | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 82 | * @dev: device |
| 83 | * @blknr: first block to be write |
| 84 | * @blkcnt: number of blocks to write |
| 85 | * @buffer: input buffer |
| 86 | * Return: number of blocks transferred |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 87 | */ |
| 88 | static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 89 | const void *buffer) |
| 90 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 91 | struct efi_blk_plat *plat = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 92 | struct efi_block_io *io = plat->io; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 93 | efi_status_t ret; |
| 94 | |
| 95 | EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n", |
| 96 | __func__, dev->name, blknr, blkcnt); |
| 97 | ret = EFI_CALL(io->write_blocks( |
| 98 | io, io->media->media_id, (u64)blknr, |
| 99 | (efi_uintn_t)blkcnt * |
| 100 | (efi_uintn_t)io->media->block_size, |
| 101 | (void *)buffer)); |
| 102 | EFI_PRINT("%s: r = %u\n", __func__, |
| 103 | (unsigned int)(ret & ~EFI_ERROR_MASK)); |
| 104 | if (ret != EFI_SUCCESS) |
| 105 | return 0; |
| 106 | return blkcnt; |
| 107 | } |
| 108 | |
Heinrich Schuchardt | 6492dad | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 109 | /** |
Heinrich Schuchardt | 35679a9 | 2022-10-04 18:53:34 +0200 | [diff] [blame] | 110 | * efi_bl_create_block_device() - create a block device for a handle |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 111 | * |
Heinrich Schuchardt | 6492dad | 2019-07-14 11:52:33 +0200 | [diff] [blame] | 112 | * @handle: handle |
| 113 | * @interface: block io protocol |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 114 | * Return: status code |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 115 | */ |
Heinrich Schuchardt | cc00eb4 | 2022-10-04 16:19:30 +0200 | [diff] [blame] | 116 | static efi_status_t |
| 117 | efi_bl_create_block_device(efi_handle_t handle, void *interface) |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 118 | { |
Heinrich Schuchardt | 3f712db | 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 | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 122 | char *name; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 123 | struct efi_block_io *io = interface; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 124 | struct efi_blk_plat *plat; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 125 | |
Masahisa Kojima | 676c274 | 2023-07-03 15:08:45 +0900 | [diff] [blame] | 126 | devnum = blk_next_free_devnum(UCLASS_EFI_LOADER); |
| 127 | if (devnum < 0) |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 128 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 129 | |
| 130 | name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */ |
| 131 | if (!name) |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 132 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 133 | sprintf(name, "efiblk#%d", devnum); |
| 134 | |
| 135 | /* Create driver model udevice for the EFI block io device */ |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 136 | if (blk_create_device(parent, "efi_blk", name, UCLASS_EFI_LOADER, |
| 137 | devnum, io->media->block_size, |
| 138 | (lbaint_t)io->media->last_block, &bdev)) { |
| 139 | ret = EFI_OUT_OF_RESOURCES; |
| 140 | free(name); |
| 141 | goto err; |
| 142 | } |
Heinrich Schuchardt | 25ac13e | 2018-06-30 07:11:32 +0200 | [diff] [blame] | 143 | /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */ |
| 144 | device_set_name_alloced(bdev); |
Bin Meng | 9193863 | 2018-10-15 02:21:06 -0700 | [diff] [blame] | 145 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 146 | plat = dev_get_plat(bdev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 147 | plat->handle = handle; |
| 148 | plat->io = interface; |
Bin Meng | 9193863 | 2018-10-15 02:21:06 -0700 | [diff] [blame] | 149 | |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 150 | if (efi_link_dev(handle, bdev)) { |
| 151 | ret = EFI_OUT_OF_RESOURCES; |
| 152 | goto err; |
| 153 | } |
AKASHI Takahiro | 2381f2e | 2022-04-19 10:05:12 +0900 | [diff] [blame] | 154 | |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 155 | if (device_probe(bdev)) { |
| 156 | ret = EFI_DEVICE_ERROR; |
| 157 | goto err; |
| 158 | } |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 159 | EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name); |
| 160 | |
Heinrich Schuchardt | 3f712db | 2022-10-03 10:35:35 +0200 | [diff] [blame] | 161 | return EFI_SUCCESS; |
| 162 | |
| 163 | err: |
| 164 | efi_unlink_dev(handle); |
| 165 | if (bdev) |
| 166 | device_unbind(bdev); |
| 167 | |
| 168 | return ret; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Heinrich Schuchardt | cc00eb4 | 2022-10-04 16:19:30 +0200 | [diff] [blame] | 171 | /** |
| 172 | * efi_bl_bind() - bind to a block io protocol |
| 173 | * |
Heinrich Schuchardt | ff1d50b | 2022-10-04 19:12:59 +0200 | [diff] [blame] | 174 | * @this: driver binding protocol |
Heinrich Schuchardt | cc00eb4 | 2022-10-04 16:19:30 +0200 | [diff] [blame] | 175 | * @handle: handle |
| 176 | * @interface: block io protocol |
| 177 | * Return: status code |
| 178 | */ |
Heinrich Schuchardt | ff1d50b | 2022-10-04 19:12:59 +0200 | [diff] [blame] | 179 | static efi_status_t efi_bl_bind( |
| 180 | struct efi_driver_binding_extended_protocol *this, |
| 181 | efi_handle_t handle, void *interface) |
Heinrich Schuchardt | cc00eb4 | 2022-10-04 16:19:30 +0200 | [diff] [blame] | 182 | { |
| 183 | efi_status_t ret = EFI_SUCCESS; |
| 184 | struct efi_object *obj = efi_search_obj(handle); |
| 185 | |
| 186 | EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, interface); |
| 187 | |
| 188 | if (!obj || !interface) |
| 189 | return EFI_INVALID_PARAMETER; |
| 190 | |
| 191 | if (!handle->dev) |
| 192 | ret = efi_bl_create_block_device(handle, interface); |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
Heinrich Schuchardt | 6c5fd21 | 2022-10-05 11:28:47 +0200 | [diff] [blame] | 197 | /** |
| 198 | * efi_bl_init() - initialize block device driver |
| 199 | * |
| 200 | * @this: extended driver binding protocol |
| 201 | */ |
| 202 | static efi_status_t |
| 203 | efi_bl_init(struct efi_driver_binding_extended_protocol *this) |
| 204 | { |
Heinrich Schuchardt | da620cf | 2022-10-06 07:29:41 +0200 | [diff] [blame] | 205 | int ret; |
| 206 | |
| 207 | ret = event_register("efi_disk add", EVT_DM_POST_PROBE, |
| 208 | efi_disk_probe, this); |
| 209 | if (ret) { |
| 210 | log_err("Event registration for efi_disk add failed\n"); |
| 211 | return EFI_OUT_OF_RESOURCES; |
| 212 | } |
| 213 | |
| 214 | ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE, |
| 215 | efi_disk_remove, this); |
| 216 | if (ret) { |
| 217 | log_err("Event registration for efi_disk del failed\n"); |
| 218 | return EFI_OUT_OF_RESOURCES; |
| 219 | } |
| 220 | |
Heinrich Schuchardt | 6c5fd21 | 2022-10-05 11:28:47 +0200 | [diff] [blame] | 221 | return EFI_SUCCESS; |
| 222 | } |
| 223 | |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 224 | /* Block device driver operators */ |
| 225 | static const struct blk_ops efi_blk_ops = { |
| 226 | .read = efi_bl_read, |
| 227 | .write = efi_bl_write, |
| 228 | }; |
| 229 | |
| 230 | /* Identify as block device driver */ |
| 231 | U_BOOT_DRIVER(efi_blk) = { |
Heinrich Schuchardt | 35679a9 | 2022-10-04 18:53:34 +0200 | [diff] [blame] | 232 | .name = "efi_blk", |
| 233 | .id = UCLASS_BLK, |
| 234 | .ops = &efi_blk_ops, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 235 | .plat_auto = sizeof(struct efi_blk_plat), |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | /* EFI driver operators */ |
| 239 | static const struct efi_driver_ops driver_ops = { |
| 240 | .protocol = &efi_block_io_guid, |
| 241 | .child_protocol = &efi_block_io_guid, |
Heinrich Schuchardt | 6c5fd21 | 2022-10-05 11:28:47 +0200 | [diff] [blame] | 242 | .init = efi_bl_init, |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 243 | .bind = efi_bl_bind, |
| 244 | }; |
| 245 | |
| 246 | /* Identify as EFI driver */ |
| 247 | U_BOOT_DRIVER(efi_block) = { |
| 248 | .name = "EFI block driver", |
Simon Glass | 15c4d67 | 2021-12-04 08:56:30 -0700 | [diff] [blame] | 249 | .id = UCLASS_EFI_LOADER, |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 250 | .ops = &driver_ops, |
| 251 | }; |