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