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