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