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