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