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