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