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> |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
AKASHI Takahiro | 120a01a | 2019-10-07 14:59:38 +0900 | [diff] [blame] | 14 | #include <fs.h> |
Heinrich Schuchardt | 3724057 | 2020-07-17 20:33:05 +0200 | [diff] [blame] | 15 | #include <log.h> |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 16 | #include <part.h> |
| 17 | #include <malloc.h> |
| 18 | |
Heinrich Schuchardt | a993173 | 2020-03-19 15:16:31 +0100 | [diff] [blame] | 19 | struct efi_system_partition efi_system_partition; |
| 20 | |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 21 | 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] | 22 | const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 23 | |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 24 | /** |
| 25 | * struct efi_disk_obj - EFI disk object |
| 26 | * |
| 27 | * @header: EFI object header |
| 28 | * @ops: EFI disk I/O protocol interface |
| 29 | * @ifname: interface name for block device |
| 30 | * @dev_index: device index of block device |
| 31 | * @media: block I/O media information |
| 32 | * @dp: device path to the block device |
| 33 | * @part: partition |
| 34 | * @volume: simple file system protocol of the partition |
| 35 | * @offset: offset into disk for simple partition |
| 36 | * @desc: internal block device descriptor |
| 37 | */ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 38 | struct efi_disk_obj { |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 39 | struct efi_object header; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 40 | struct efi_block_io ops; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 41 | const char *ifname; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 42 | int dev_index; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 43 | struct efi_block_io_media media; |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 44 | struct efi_device_path *dp; |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 45 | unsigned int part; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 46 | struct efi_simple_file_system_protocol *volume; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 47 | lbaint_t offset; |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 48 | struct blk_desc *desc; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Heinrich Schuchardt | 670198c | 2019-09-05 20:13:46 +0200 | [diff] [blame] | 51 | /** |
| 52 | * efi_disk_reset() - reset block device |
| 53 | * |
| 54 | * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL. |
| 55 | * |
| 56 | * As U-Boot's block devices do not have a reset function simply return |
| 57 | * EFI_SUCCESS. |
| 58 | * |
| 59 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 60 | * details. |
| 61 | * |
| 62 | * @this: pointer to the BLOCK_IO_PROTOCOL |
| 63 | * @extended_verification: extended verification |
| 64 | * Return: status code |
| 65 | */ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 66 | static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this, |
| 67 | char extended_verification) |
| 68 | { |
| 69 | EFI_ENTRY("%p, %x", this, extended_verification); |
Heinrich Schuchardt | 670198c | 2019-09-05 20:13:46 +0200 | [diff] [blame] | 70 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | enum efi_disk_direction { |
| 74 | EFI_DISK_READ, |
| 75 | EFI_DISK_WRITE, |
| 76 | }; |
| 77 | |
Heinrich Schuchardt | 9ce0617 | 2017-08-26 22:33:13 +0200 | [diff] [blame] | 78 | 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] | 79 | u32 media_id, u64 lba, unsigned long buffer_size, |
| 80 | void *buffer, enum efi_disk_direction direction) |
| 81 | { |
| 82 | struct efi_disk_obj *diskobj; |
| 83 | struct blk_desc *desc; |
| 84 | int blksz; |
| 85 | int blocks; |
| 86 | unsigned long n; |
| 87 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 88 | diskobj = container_of(this, struct efi_disk_obj, ops); |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 89 | desc = (struct blk_desc *) diskobj->desc; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 90 | blksz = desc->blksz; |
| 91 | blocks = buffer_size / blksz; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 92 | lba += diskobj->offset; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 93 | |
Heinrich Schuchardt | ee6ef8b | 2019-09-05 19:43:17 +0200 | [diff] [blame] | 94 | EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n", |
| 95 | blocks, lba, blksz, direction); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 96 | |
| 97 | /* We only support full block access */ |
| 98 | if (buffer_size & (blksz - 1)) |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 99 | return EFI_BAD_BUFFER_SIZE; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 100 | |
| 101 | if (direction == EFI_DISK_READ) |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 102 | n = blk_dread(desc, lba, blocks, buffer); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 103 | else |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 104 | n = blk_dwrite(desc, lba, blocks, buffer); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 105 | |
| 106 | /* We don't do interrupts, so check for timers cooperatively */ |
| 107 | efi_timer_check(); |
| 108 | |
Heinrich Schuchardt | ee6ef8b | 2019-09-05 19:43:17 +0200 | [diff] [blame] | 109 | EFI_PRINT("n=%lx blocks=%x\n", n, blocks); |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 110 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 111 | if (n != blocks) |
Rob Clark | 99c4387 | 2017-07-25 20:28:29 -0400 | [diff] [blame] | 112 | return EFI_DEVICE_ERROR; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 113 | |
Rob Clark | 99c4387 | 2017-07-25 20:28:29 -0400 | [diff] [blame] | 114 | return EFI_SUCCESS; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Heinrich Schuchardt | 76f5789 | 2020-04-10 17:10:34 +0200 | [diff] [blame] | 117 | /** |
| 118 | * efi_disk_read_blocks() - reads blocks from device |
| 119 | * |
| 120 | * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL. |
| 121 | * |
| 122 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 123 | * details. |
| 124 | * |
| 125 | * @this: pointer to the BLOCK_IO_PROTOCOL |
| 126 | * @media_id: id of the medium to be read from |
| 127 | * @lba: starting logical block for reading |
| 128 | * @buffer_size: size of the read buffer |
| 129 | * @buffer: pointer to the destination buffer |
| 130 | * Return: status code |
| 131 | */ |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 132 | 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] | 133 | u32 media_id, u64 lba, efi_uintn_t buffer_size, |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 134 | void *buffer) |
| 135 | { |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 136 | void *real_buffer = buffer; |
| 137 | efi_status_t r; |
| 138 | |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 139 | if (!this) |
| 140 | return EFI_INVALID_PARAMETER; |
| 141 | /* TODO: check for media changes */ |
| 142 | if (media_id != this->media->media_id) |
| 143 | return EFI_MEDIA_CHANGED; |
| 144 | if (!this->media->media_present) |
| 145 | return EFI_NO_MEDIA; |
Heinrich Schuchardt | 0bb7389 | 2021-01-23 19:33:11 +0100 | [diff] [blame] | 146 | /* media->io_align is a power of 2 or 0 */ |
| 147 | if (this->media->io_align && |
| 148 | (uintptr_t)buffer & (this->media->io_align - 1)) |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 149 | return EFI_INVALID_PARAMETER; |
| 150 | if (lba * this->media->block_size + buffer_size > |
Jesper Schmitz Mouridsen | f142493 | 2021-02-09 17:17:17 +0100 | [diff] [blame] | 151 | (this->media->last_block + 1) * this->media->block_size) |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 152 | return EFI_INVALID_PARAMETER; |
| 153 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 154 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 155 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { |
| 156 | r = efi_disk_read_blocks(this, media_id, lba, |
| 157 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); |
| 158 | if (r != EFI_SUCCESS) |
| 159 | return r; |
| 160 | return efi_disk_read_blocks(this, media_id, lba + |
| 161 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, |
| 162 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, |
| 163 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); |
| 164 | } |
| 165 | |
| 166 | real_buffer = efi_bounce_buffer; |
| 167 | #endif |
| 168 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 169 | EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 170 | buffer_size, buffer); |
| 171 | |
| 172 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, |
| 173 | EFI_DISK_READ); |
| 174 | |
| 175 | /* Copy from bounce buffer to real buffer if necessary */ |
| 176 | if ((r == EFI_SUCCESS) && (real_buffer != buffer)) |
| 177 | memcpy(buffer, real_buffer, buffer_size); |
| 178 | |
| 179 | return EFI_EXIT(r); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Heinrich Schuchardt | 76f5789 | 2020-04-10 17:10:34 +0200 | [diff] [blame] | 182 | /** |
| 183 | * efi_disk_write_blocks() - writes blocks to device |
| 184 | * |
| 185 | * This function implements the WriteBlocks service of the |
| 186 | * EFI_BLOCK_IO_PROTOCOL. |
| 187 | * |
| 188 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 189 | * details. |
| 190 | * |
| 191 | * @this: pointer to the BLOCK_IO_PROTOCOL |
| 192 | * @media_id: id of the medium to be written to |
| 193 | * @lba: starting logical block for writing |
| 194 | * @buffer_size: size of the write buffer |
| 195 | * @buffer: pointer to the source buffer |
| 196 | * Return: status code |
| 197 | */ |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 198 | 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] | 199 | u32 media_id, u64 lba, efi_uintn_t buffer_size, |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 200 | void *buffer) |
| 201 | { |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 202 | void *real_buffer = buffer; |
| 203 | efi_status_t r; |
| 204 | |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 205 | if (!this) |
| 206 | return EFI_INVALID_PARAMETER; |
| 207 | if (this->media->read_only) |
| 208 | return EFI_WRITE_PROTECTED; |
| 209 | /* TODO: check for media changes */ |
| 210 | if (media_id != this->media->media_id) |
| 211 | return EFI_MEDIA_CHANGED; |
| 212 | if (!this->media->media_present) |
| 213 | return EFI_NO_MEDIA; |
Heinrich Schuchardt | 0bb7389 | 2021-01-23 19:33:11 +0100 | [diff] [blame] | 214 | /* media->io_align is a power of 2 or 0 */ |
| 215 | if (this->media->io_align && |
| 216 | (uintptr_t)buffer & (this->media->io_align - 1)) |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 217 | return EFI_INVALID_PARAMETER; |
| 218 | if (lba * this->media->block_size + buffer_size > |
Jesper Schmitz Mouridsen | f142493 | 2021-02-09 17:17:17 +0100 | [diff] [blame] | 219 | (this->media->last_block + 1) * this->media->block_size) |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 220 | return EFI_INVALID_PARAMETER; |
| 221 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 222 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 223 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { |
| 224 | r = efi_disk_write_blocks(this, media_id, lba, |
| 225 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); |
| 226 | if (r != EFI_SUCCESS) |
| 227 | return r; |
| 228 | return efi_disk_write_blocks(this, media_id, lba + |
| 229 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, |
| 230 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, |
| 231 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); |
| 232 | } |
| 233 | |
| 234 | real_buffer = efi_bounce_buffer; |
| 235 | #endif |
| 236 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 237 | EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 238 | buffer_size, buffer); |
| 239 | |
| 240 | /* Populate bounce buffer if necessary */ |
| 241 | if (real_buffer != buffer) |
| 242 | memcpy(real_buffer, buffer, buffer_size); |
| 243 | |
| 244 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, |
| 245 | EFI_DISK_WRITE); |
| 246 | |
| 247 | return EFI_EXIT(r); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Heinrich Schuchardt | 76f5789 | 2020-04-10 17:10:34 +0200 | [diff] [blame] | 250 | /** |
| 251 | * efi_disk_flush_blocks() - flushes modified data to the device |
| 252 | * |
| 253 | * This function implements the FlushBlocks service of the |
| 254 | * EFI_BLOCK_IO_PROTOCOL. |
| 255 | * |
| 256 | * As we always write synchronously nothing is done here. |
| 257 | * |
| 258 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 259 | * details. |
| 260 | * |
| 261 | * @this: pointer to the BLOCK_IO_PROTOCOL |
| 262 | * Return: status code |
| 263 | */ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 264 | static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this) |
| 265 | { |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 266 | EFI_ENTRY("%p", this); |
| 267 | return EFI_EXIT(EFI_SUCCESS); |
| 268 | } |
| 269 | |
| 270 | static const struct efi_block_io block_io_disk_template = { |
| 271 | .reset = &efi_disk_reset, |
| 272 | .read_blocks = &efi_disk_read_blocks, |
| 273 | .write_blocks = &efi_disk_write_blocks, |
| 274 | .flush_blocks = &efi_disk_flush_blocks, |
| 275 | }; |
| 276 | |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 277 | /** |
| 278 | * efi_fs_from_path() - retrieve simple file system protocol |
| 279 | * |
| 280 | * Gets the simple file system protocol for a file device path. |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 281 | * |
| 282 | * The full path provided is split into device part and into a file |
| 283 | * part. The device part is used to find the handle on which the |
| 284 | * simple file system protocol is installed. |
| 285 | * |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 286 | * @full_path: device path including device and file |
| 287 | * Return: simple file system protocol |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 288 | */ |
| 289 | struct efi_simple_file_system_protocol * |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 290 | efi_fs_from_path(struct efi_device_path *full_path) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 291 | { |
| 292 | struct efi_object *efiobj; |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 293 | struct efi_handler *handler; |
| 294 | struct efi_device_path *device_path; |
| 295 | struct efi_device_path *file_path; |
| 296 | efi_status_t ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 297 | |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 298 | /* Split the path into a device part and a file part */ |
| 299 | ret = efi_dp_split_file_path(full_path, &device_path, &file_path); |
| 300 | if (ret != EFI_SUCCESS) |
| 301 | return NULL; |
| 302 | efi_free_pool(file_path); |
| 303 | |
| 304 | /* Get the EFI object for the partition */ |
| 305 | efiobj = efi_dp_find_obj(device_path, NULL); |
| 306 | efi_free_pool(device_path); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 307 | if (!efiobj) |
| 308 | return NULL; |
| 309 | |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 310 | /* Find the simple file system protocol */ |
| 311 | ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid, |
| 312 | &handler); |
| 313 | if (ret != EFI_SUCCESS) |
| 314 | return NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 315 | |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 316 | /* Return the simple file system protocol for the partition */ |
| 317 | return handler->protocol_interface; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 318 | } |
| 319 | |
AKASHI Takahiro | 120a01a | 2019-10-07 14:59:38 +0900 | [diff] [blame] | 320 | /** |
| 321 | * efi_fs_exists() - check if a partition bears a file system |
| 322 | * |
| 323 | * @desc: block device descriptor |
| 324 | * @part: partition number |
| 325 | * Return: 1 if a file system exists on the partition |
| 326 | * 0 otherwise |
| 327 | */ |
| 328 | static int efi_fs_exists(struct blk_desc *desc, int part) |
| 329 | { |
| 330 | if (fs_set_blk_dev_with_part(desc, part)) |
| 331 | return 0; |
| 332 | |
| 333 | if (fs_get_type() == FS_TYPE_ANY) |
| 334 | return 0; |
| 335 | |
| 336 | fs_close(); |
| 337 | |
| 338 | return 1; |
| 339 | } |
| 340 | |
Heinrich Schuchardt | 76f5789 | 2020-04-10 17:10:34 +0200 | [diff] [blame] | 341 | /** |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 342 | * efi_disk_add_dev() - create a handle for a partition or disk |
Heinrich Schuchardt | 34f5183 | 2017-10-26 19:25:46 +0200 | [diff] [blame] | 343 | * |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 344 | * @parent: parent handle |
| 345 | * @dp_parent: parent device path |
| 346 | * @if_typename: interface name for block device |
| 347 | * @desc: internal block device |
| 348 | * @dev_index: device index for block device |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 349 | * @part_info: partition info |
Heinrich Schuchardt | 76f5789 | 2020-04-10 17:10:34 +0200 | [diff] [blame] | 350 | * @part: partition |
| 351 | * @disk: pointer to receive the created handle |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 352 | * Return: disk object |
Heinrich Schuchardt | 34f5183 | 2017-10-26 19:25:46 +0200 | [diff] [blame] | 353 | */ |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 354 | static efi_status_t efi_disk_add_dev( |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 355 | efi_handle_t parent, |
| 356 | struct efi_device_path *dp_parent, |
| 357 | const char *if_typename, |
| 358 | struct blk_desc *desc, |
| 359 | int dev_index, |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 360 | struct disk_partition *part_info, |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 361 | unsigned int part, |
| 362 | struct efi_disk_obj **disk) |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 363 | { |
| 364 | struct efi_disk_obj *diskobj; |
Heinrich Schuchardt | 2321b55 | 2020-05-21 09:22:06 +0200 | [diff] [blame] | 365 | struct efi_object *handle; |
Heinrich Schuchardt | 2639762 | 2021-02-02 17:53:14 +0100 | [diff] [blame] | 366 | const efi_guid_t *guid = NULL; |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 367 | efi_status_t ret; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 368 | |
Alexander Graf | 601db1f | 2016-08-05 14:51:47 +0200 | [diff] [blame] | 369 | /* Don't add empty devices */ |
| 370 | if (!desc->lba) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 371 | return EFI_NOT_READY; |
Alexander Graf | 601db1f | 2016-08-05 14:51:47 +0200 | [diff] [blame] | 372 | |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 373 | diskobj = calloc(1, sizeof(*diskobj)); |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 374 | if (!diskobj) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 375 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 376 | |
| 377 | /* Hook up to the device list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 378 | efi_add_handle(&diskobj->header); |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 379 | |
| 380 | /* Fill in object data */ |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 381 | if (part_info) { |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 382 | struct efi_device_path *node = efi_dp_part_node(desc, part); |
Heinrich Schuchardt | cb9ff4e | 2020-01-10 12:36:01 +0100 | [diff] [blame] | 383 | struct efi_handler *handler; |
| 384 | void *protocol_interface; |
| 385 | |
| 386 | /* Parent must expose EFI_BLOCK_IO_PROTOCOL */ |
| 387 | ret = efi_search_protocol(parent, &efi_block_io_guid, &handler); |
| 388 | if (ret != EFI_SUCCESS) |
| 389 | goto error; |
| 390 | |
| 391 | /* |
| 392 | * Link the partition (child controller) to the block device |
| 393 | * (controller). |
| 394 | */ |
| 395 | ret = efi_protocol_open(handler, &protocol_interface, NULL, |
| 396 | &diskobj->header, |
| 397 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER); |
| 398 | if (ret != EFI_SUCCESS) |
| 399 | goto error; |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 400 | |
| 401 | diskobj->dp = efi_dp_append_node(dp_parent, node); |
| 402 | efi_free_pool(node); |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 403 | diskobj->offset = part_info->start; |
| 404 | diskobj->media.last_block = part_info->size - 1; |
Heinrich Schuchardt | 2639762 | 2021-02-02 17:53:14 +0100 | [diff] [blame] | 405 | if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) |
| 406 | guid = &efi_system_partition_guid; |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 407 | } else { |
| 408 | diskobj->dp = efi_dp_from_part(desc, part); |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 409 | diskobj->offset = 0; |
| 410 | diskobj->media.last_block = desc->lba - 1; |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 411 | } |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 412 | diskobj->part = part; |
Heinrich Schuchardt | 2321b55 | 2020-05-21 09:22:06 +0200 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * Install the device path and the block IO protocol. |
| 416 | * |
| 417 | * InstallMultipleProtocolInterfaces() checks if the device path is |
| 418 | * already installed on an other handle and returns EFI_ALREADY_STARTED |
| 419 | * in this case. |
| 420 | */ |
| 421 | handle = &diskobj->header; |
| 422 | ret = EFI_CALL(efi_install_multiple_protocol_interfaces( |
| 423 | &handle, &efi_guid_device_path, diskobj->dp, |
Heinrich Schuchardt | 2639762 | 2021-02-02 17:53:14 +0100 | [diff] [blame] | 424 | &efi_block_io_guid, &diskobj->ops, |
| 425 | guid, NULL, NULL)); |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 426 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e47b68b | 2021-11-20 13:56:02 +0100 | [diff] [blame^] | 427 | goto error; |
Heinrich Schuchardt | 2321b55 | 2020-05-21 09:22:06 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * On partitions or whole disks without partitions install the |
| 431 | * simple file system protocol if a file system is available. |
| 432 | */ |
AKASHI Takahiro | 356ef90 | 2019-10-07 14:59:39 +0900 | [diff] [blame] | 433 | if ((part || desc->part_type == PART_TYPE_UNKNOWN) && |
| 434 | efi_fs_exists(desc, part)) { |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 435 | diskobj->volume = efi_simple_file_system(desc, part, |
| 436 | diskobj->dp); |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 437 | ret = efi_add_protocol(&diskobj->header, |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 438 | &efi_simple_file_system_protocol_guid, |
Heinrich Schuchardt | ce721a8 | 2018-01-19 20:24:38 +0100 | [diff] [blame] | 439 | diskobj->volume); |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 440 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 441 | return ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 442 | } |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 443 | diskobj->ops = block_io_disk_template; |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 444 | diskobj->ifname = if_typename; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 445 | diskobj->dev_index = dev_index; |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 446 | diskobj->desc = desc; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 447 | |
| 448 | /* Fill in EFI IO Media info (for read/write callbacks) */ |
| 449 | diskobj->media.removable_media = desc->removable; |
| 450 | diskobj->media.media_present = 1; |
Heinrich Schuchardt | 3592252 | 2019-09-05 19:31:23 +0200 | [diff] [blame] | 451 | /* |
| 452 | * MediaID is just an arbitrary counter. |
| 453 | * We have to change it if the medium is removed or changed. |
| 454 | */ |
| 455 | diskobj->media.media_id = 1; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 456 | diskobj->media.block_size = desc->blksz; |
| 457 | diskobj->media.io_align = desc->blksz; |
Heinrich Schuchardt | af8c5b6 | 2020-03-19 15:45:52 +0100 | [diff] [blame] | 458 | if (part) |
Emmanuel Vadot | 5bf2964 | 2017-12-11 19:22:33 +0100 | [diff] [blame] | 459 | diskobj->media.logical_partition = 1; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 460 | diskobj->ops.media = &diskobj->media; |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 461 | if (disk) |
| 462 | *disk = diskobj; |
Heinrich Schuchardt | a993173 | 2020-03-19 15:16:31 +0100 | [diff] [blame] | 463 | |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 464 | EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d" |
| 465 | ", offset " LBAF ", last_block %llu\n", |
| 466 | diskobj->part, |
| 467 | diskobj->media.media_present, |
| 468 | diskobj->media.logical_partition, |
| 469 | diskobj->media.removable_media, |
| 470 | diskobj->offset, |
| 471 | diskobj->media.last_block); |
| 472 | |
Heinrich Schuchardt | a993173 | 2020-03-19 15:16:31 +0100 | [diff] [blame] | 473 | /* Store first EFI system partition */ |
| 474 | if (part && !efi_system_partition.if_type) { |
Heinrich Schuchardt | 2639762 | 2021-02-02 17:53:14 +0100 | [diff] [blame] | 475 | if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) { |
Heinrich Schuchardt | a993173 | 2020-03-19 15:16:31 +0100 | [diff] [blame] | 476 | efi_system_partition.if_type = desc->if_type; |
| 477 | efi_system_partition.devnum = desc->devnum; |
| 478 | efi_system_partition.part = part; |
Heinrich Schuchardt | 54f180d | 2021-05-25 18:00:13 +0200 | [diff] [blame] | 479 | EFI_PRINT("EFI system partition: %s %x:%x\n", |
Heinrich Schuchardt | a993173 | 2020-03-19 15:16:31 +0100 | [diff] [blame] | 480 | blk_get_if_type_name(desc->if_type), |
| 481 | desc->devnum, part); |
| 482 | } |
| 483 | } |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 484 | return EFI_SUCCESS; |
Heinrich Schuchardt | cb9ff4e | 2020-01-10 12:36:01 +0100 | [diff] [blame] | 485 | error: |
| 486 | efi_delete_handle(&diskobj->header); |
| 487 | return ret; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 490 | /** |
| 491 | * efi_disk_create_partitions() - create handles and protocols for partitions |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 492 | * |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 493 | * Create handles and protocols for the partitions of a block device. |
| 494 | * |
| 495 | * @parent: handle of the parent disk |
Heinrich Schuchardt | 76f5789 | 2020-04-10 17:10:34 +0200 | [diff] [blame] | 496 | * @desc: block device |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 497 | * @if_typename: interface type |
| 498 | * @diskid: device number |
| 499 | * @pdevname: device name |
| 500 | * Return: number of partitions created |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 501 | */ |
| 502 | int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc, |
| 503 | const char *if_typename, int diskid, |
| 504 | const char *pdevname) |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 505 | { |
| 506 | int disks = 0; |
Alexander Graf | 3491ee7 | 2016-04-11 16:16:20 +0200 | [diff] [blame] | 507 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
Jonathan Gray | d35cbd5 | 2017-10-10 13:55:26 +1100 | [diff] [blame] | 508 | int part; |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 509 | struct efi_device_path *dp = NULL; |
| 510 | efi_status_t ret; |
| 511 | struct efi_handler *handler; |
| 512 | |
| 513 | /* Get the device path of the parent */ |
| 514 | ret = efi_search_protocol(parent, &efi_guid_device_path, &handler); |
| 515 | if (ret == EFI_SUCCESS) |
| 516 | dp = handler->protocol_interface; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 517 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 518 | /* Add devices for each partition */ |
Jonathan Gray | d35cbd5 | 2017-10-10 13:55:26 +1100 | [diff] [blame] | 519 | for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) { |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 520 | struct disk_partition info; |
| 521 | |
Jonathan Gray | d35cbd5 | 2017-10-10 13:55:26 +1100 | [diff] [blame] | 522 | if (part_get_info(desc, part, &info)) |
| 523 | continue; |
Heinrich Schuchardt | 54f180d | 2021-05-25 18:00:13 +0200 | [diff] [blame] | 524 | snprintf(devname, sizeof(devname), "%s:%x", pdevname, |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 525 | part); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 526 | ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid, |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 527 | &info, part, NULL); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 528 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 3724057 | 2020-07-17 20:33:05 +0200 | [diff] [blame] | 529 | log_err("Adding partition %s failed\n", pdevname); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 530 | continue; |
| 531 | } |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 532 | disks++; |
| 533 | } |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 534 | |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 535 | return disks; |
| 536 | } |
| 537 | |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 538 | /** |
| 539 | * efi_disk_register() - register block devices |
| 540 | * |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 541 | * U-Boot doesn't have a list of all online disk devices. So when running our |
| 542 | * EFI payload, we scan through all of the potentially available ones and |
| 543 | * store them in our object pool. |
| 544 | * |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 545 | * This function is called in efi_init_obj_list(). |
| 546 | * |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 547 | * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this. |
| 548 | * Consider converting the code to look up devices as needed. The EFI device |
| 549 | * could be a child of the UCLASS_BLK block device, perhaps. |
| 550 | * |
Heinrich Schuchardt | 6203606 | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 551 | * Return: status code |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 552 | */ |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 553 | efi_status_t efi_disk_register(void) |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 554 | { |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 555 | struct efi_disk_obj *disk; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 556 | int disks = 0; |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 557 | efi_status_t ret; |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 558 | struct udevice *dev; |
| 559 | |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 560 | for (uclass_first_device_check(UCLASS_BLK, &dev); dev; |
xypron.glpk@gmx.de | 308cd8e | 2017-06-20 19:10:27 +0000 | [diff] [blame] | 561 | uclass_next_device_check(&dev)) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 562 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | 6a18bb3 | 2018-01-19 20:24:44 +0100 | [diff] [blame] | 563 | const char *if_typename = blk_get_if_type_name(desc->if_type); |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 564 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 565 | /* Add block device for the full device */ |
Heinrich Schuchardt | 3724057 | 2020-07-17 20:33:05 +0200 | [diff] [blame] | 566 | log_info("Scanning disk %s...\n", dev->name); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 567 | ret = efi_disk_add_dev(NULL, NULL, if_typename, |
Heinrich Schuchardt | b56e719 | 2021-01-23 06:52:21 +0100 | [diff] [blame] | 568 | desc, desc->devnum, NULL, 0, &disk); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 569 | if (ret == EFI_NOT_READY) { |
Heinrich Schuchardt | 3724057 | 2020-07-17 20:33:05 +0200 | [diff] [blame] | 570 | log_notice("Disk %s not ready\n", dev->name); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 571 | continue; |
| 572 | } |
| 573 | if (ret) { |
Heinrich Schuchardt | 3724057 | 2020-07-17 20:33:05 +0200 | [diff] [blame] | 574 | log_err("ERROR: failure to add disk device %s, r = %lu\n", |
| 575 | dev->name, ret & ~EFI_ERROR_MASK); |
Heinrich Schuchardt | e47b68b | 2021-11-20 13:56:02 +0100 | [diff] [blame^] | 576 | continue; |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 577 | } |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 578 | disks++; |
| 579 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 580 | /* Partitions show up as block devices in EFI */ |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 581 | disks += efi_disk_create_partitions( |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 582 | &disk->header, desc, if_typename, |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 583 | desc->devnum, dev->name); |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 584 | } |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 585 | |
Heinrich Schuchardt | 3724057 | 2020-07-17 20:33:05 +0200 | [diff] [blame] | 586 | log_info("Found %d disks\n", disks); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 587 | |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 588 | return EFI_SUCCESS; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 589 | } |
AKASHI Takahiro | 911df64 | 2020-04-27 18:48:20 +0900 | [diff] [blame] | 590 | |
| 591 | /** |
| 592 | * efi_disk_is_system_part() - check if handle refers to an EFI system partition |
| 593 | * |
| 594 | * @handle: handle of partition |
| 595 | * |
| 596 | * Return: true if handle refers to an EFI system partition |
| 597 | */ |
| 598 | bool efi_disk_is_system_part(efi_handle_t handle) |
| 599 | { |
| 600 | struct efi_handler *handler; |
| 601 | struct efi_disk_obj *diskobj; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 602 | struct disk_partition info; |
AKASHI Takahiro | 911df64 | 2020-04-27 18:48:20 +0900 | [diff] [blame] | 603 | efi_status_t ret; |
| 604 | int r; |
| 605 | |
| 606 | /* check if this is a block device */ |
| 607 | ret = efi_search_protocol(handle, &efi_block_io_guid, &handler); |
| 608 | if (ret != EFI_SUCCESS) |
| 609 | return false; |
| 610 | |
| 611 | diskobj = container_of(handle, struct efi_disk_obj, header); |
| 612 | |
| 613 | r = part_get_info(diskobj->desc, diskobj->part, &info); |
| 614 | if (r) |
| 615 | return false; |
| 616 | |
| 617 | return !!(info.bootable & PART_EFI_SYSTEM_PARTITION); |
| 618 | } |