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