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