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