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