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