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