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 | |
| 17 | static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID; |
| 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 */ |
| 31 | struct efi_device_path_file_path *dp; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 32 | /* Offset into disk for simple partitions */ |
| 33 | lbaint_t offset; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 36 | static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol, |
| 37 | void **protocol_interface, void *agent_handle, |
| 38 | void *controller_handle, uint32_t attributes) |
| 39 | { |
| 40 | struct efi_disk_obj *diskobj = handle; |
| 41 | |
| 42 | *protocol_interface = &diskobj->ops; |
| 43 | |
| 44 | return EFI_SUCCESS; |
| 45 | } |
| 46 | |
| 47 | static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol, |
| 48 | void **protocol_interface, void *agent_handle, |
| 49 | void *controller_handle, uint32_t attributes) |
| 50 | { |
| 51 | struct efi_disk_obj *diskobj = handle; |
| 52 | |
| 53 | *protocol_interface = diskobj->dp; |
| 54 | |
| 55 | return EFI_SUCCESS; |
| 56 | } |
| 57 | |
| 58 | static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this, |
| 59 | char extended_verification) |
| 60 | { |
| 61 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 62 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 63 | } |
| 64 | |
| 65 | enum efi_disk_direction { |
| 66 | EFI_DISK_READ, |
| 67 | EFI_DISK_WRITE, |
| 68 | }; |
| 69 | |
| 70 | static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this, |
| 71 | u32 media_id, u64 lba, unsigned long buffer_size, |
| 72 | void *buffer, enum efi_disk_direction direction) |
| 73 | { |
| 74 | struct efi_disk_obj *diskobj; |
| 75 | struct blk_desc *desc; |
| 76 | int blksz; |
| 77 | int blocks; |
| 78 | unsigned long n; |
| 79 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 80 | diskobj = container_of(this, struct efi_disk_obj, ops); |
| 81 | if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index))) |
| 82 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 83 | blksz = desc->blksz; |
| 84 | blocks = buffer_size / blksz; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 85 | lba += diskobj->offset; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 86 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 87 | debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__, |
| 88 | __LINE__, blocks, lba, blksz, direction); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 89 | |
| 90 | /* We only support full block access */ |
| 91 | if (buffer_size & (blksz - 1)) |
| 92 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 93 | |
| 94 | if (direction == EFI_DISK_READ) |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 95 | n = blk_dread(desc, lba, blocks, buffer); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 96 | else |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 97 | n = blk_dwrite(desc, lba, blocks, buffer); |
Alexander Graf | e83be45 | 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 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 102 | debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks); |
| 103 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 104 | if (n != blocks) |
| 105 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 106 | |
| 107 | return EFI_EXIT(EFI_SUCCESS); |
| 108 | } |
| 109 | |
| 110 | static efi_status_t efi_disk_read_blocks(struct efi_block_io *this, |
| 111 | u32 media_id, u64 lba, unsigned long buffer_size, |
| 112 | void *buffer) |
| 113 | { |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 114 | void *real_buffer = buffer; |
| 115 | efi_status_t r; |
| 116 | |
| 117 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 118 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { |
| 119 | r = efi_disk_read_blocks(this, media_id, lba, |
| 120 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); |
| 121 | if (r != EFI_SUCCESS) |
| 122 | return r; |
| 123 | return efi_disk_read_blocks(this, media_id, lba + |
| 124 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, |
| 125 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, |
| 126 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); |
| 127 | } |
| 128 | |
| 129 | real_buffer = efi_bounce_buffer; |
| 130 | #endif |
| 131 | |
| 132 | EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba, |
| 133 | buffer_size, buffer); |
| 134 | |
| 135 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, |
| 136 | EFI_DISK_READ); |
| 137 | |
| 138 | /* Copy from bounce buffer to real buffer if necessary */ |
| 139 | if ((r == EFI_SUCCESS) && (real_buffer != buffer)) |
| 140 | memcpy(buffer, real_buffer, buffer_size); |
| 141 | |
| 142 | return EFI_EXIT(r); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static efi_status_t efi_disk_write_blocks(struct efi_block_io *this, |
| 146 | u32 media_id, u64 lba, unsigned long buffer_size, |
| 147 | void *buffer) |
| 148 | { |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 149 | void *real_buffer = buffer; |
| 150 | efi_status_t r; |
| 151 | |
| 152 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 153 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { |
| 154 | r = efi_disk_write_blocks(this, media_id, lba, |
| 155 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); |
| 156 | if (r != EFI_SUCCESS) |
| 157 | return r; |
| 158 | return efi_disk_write_blocks(this, media_id, lba + |
| 159 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, |
| 160 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, |
| 161 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); |
| 162 | } |
| 163 | |
| 164 | real_buffer = efi_bounce_buffer; |
| 165 | #endif |
| 166 | |
| 167 | EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba, |
| 168 | buffer_size, buffer); |
| 169 | |
| 170 | /* Populate bounce buffer if necessary */ |
| 171 | if (real_buffer != buffer) |
| 172 | memcpy(real_buffer, buffer, buffer_size); |
| 173 | |
| 174 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, |
| 175 | EFI_DISK_WRITE); |
| 176 | |
| 177 | return EFI_EXIT(r); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this) |
| 181 | { |
| 182 | /* We always write synchronously */ |
| 183 | EFI_ENTRY("%p", this); |
| 184 | return EFI_EXIT(EFI_SUCCESS); |
| 185 | } |
| 186 | |
| 187 | static const struct efi_block_io block_io_disk_template = { |
| 188 | .reset = &efi_disk_reset, |
| 189 | .read_blocks = &efi_disk_read_blocks, |
| 190 | .write_blocks = &efi_disk_write_blocks, |
| 191 | .flush_blocks = &efi_disk_flush_blocks, |
| 192 | }; |
| 193 | |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 194 | static void efi_disk_add_dev(const char *name, |
| 195 | const char *if_typename, |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 196 | const struct blk_desc *desc, |
| 197 | int dev_index, |
| 198 | lbaint_t offset) |
| 199 | { |
| 200 | struct efi_disk_obj *diskobj; |
| 201 | struct efi_device_path_file_path *dp; |
| 202 | int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2); |
| 203 | |
| 204 | diskobj = calloc(1, objlen); |
| 205 | |
| 206 | /* Fill in object data */ |
| 207 | diskobj->parent.protocols[0].guid = &efi_block_io_guid; |
| 208 | diskobj->parent.protocols[0].open = efi_disk_open_block; |
| 209 | diskobj->parent.protocols[1].guid = &efi_guid_device_path; |
| 210 | diskobj->parent.protocols[1].open = efi_disk_open_dp; |
| 211 | diskobj->parent.handle = diskobj; |
| 212 | diskobj->ops = block_io_disk_template; |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 213 | diskobj->ifname = if_typename; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 214 | diskobj->dev_index = dev_index; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 215 | diskobj->offset = offset; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 216 | |
| 217 | /* Fill in EFI IO Media info (for read/write callbacks) */ |
| 218 | diskobj->media.removable_media = desc->removable; |
| 219 | diskobj->media.media_present = 1; |
| 220 | diskobj->media.block_size = desc->blksz; |
| 221 | diskobj->media.io_align = desc->blksz; |
| 222 | diskobj->media.last_block = desc->lba; |
| 223 | diskobj->ops.media = &diskobj->media; |
| 224 | |
| 225 | /* Fill in device path */ |
| 226 | dp = (void*)&diskobj[1]; |
| 227 | diskobj->dp = dp; |
| 228 | dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 229 | dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; |
| 230 | dp[0].dp.length = sizeof(*dp); |
| 231 | ascii2unicode(dp[0].str, name); |
| 232 | |
| 233 | dp[1].dp.type = DEVICE_PATH_TYPE_END; |
| 234 | dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 235 | dp[1].dp.length = sizeof(*dp); |
| 236 | |
| 237 | /* Hook up to the device list */ |
| 238 | list_add_tail(&diskobj->parent.link, &efi_obj_list); |
| 239 | } |
| 240 | |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 241 | static int efi_disk_create_eltorito(struct blk_desc *desc, |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 242 | const char *if_typename, |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 243 | int diskid) |
| 244 | { |
| 245 | int disks = 0; |
| 246 | #ifdef CONFIG_ISO_PARTITION |
Alexander Graf | 3491ee7 | 2016-04-11 16:16:20 +0200 | [diff] [blame] | 247 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 248 | disk_partition_t info; |
| 249 | int part = 1; |
| 250 | |
| 251 | if (desc->part_type != PART_TYPE_ISO) |
| 252 | return 0; |
| 253 | |
| 254 | while (!part_get_info(desc, part, &info)) { |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 255 | snprintf(devname, sizeof(devname), "%s%d:%d", if_typename, |
| 256 | diskid, part); |
| 257 | efi_disk_add_dev(devname, if_typename, desc, diskid, |
| 258 | info.start); |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 259 | part++; |
| 260 | disks++; |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | return disks; |
| 265 | } |
| 266 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 267 | /* |
| 268 | * U-Boot doesn't have a list of all online disk devices. So when running our |
| 269 | * EFI payload, we scan through all of the potentially available ones and |
| 270 | * store them in our object pool. |
| 271 | * |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 272 | * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this. |
| 273 | * Consider converting the code to look up devices as needed. The EFI device |
| 274 | * could be a child of the UCLASS_BLK block device, perhaps. |
| 275 | * |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 276 | * This gets called from do_bootefi_exec(). |
| 277 | */ |
| 278 | int efi_disk_register(void) |
| 279 | { |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 280 | int disks = 0; |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 281 | #ifdef CONFIG_BLK |
| 282 | struct udevice *dev; |
| 283 | |
| 284 | for (uclass_first_device(UCLASS_BLK, &dev); |
| 285 | dev; |
| 286 | uclass_next_device(&dev)) { |
| 287 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 288 | const char *if_typename = dev->driver->name; |
| 289 | |
| 290 | printf("Scanning disk %s...\n", dev->name); |
| 291 | efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0); |
| 292 | disks++; |
| 293 | |
| 294 | /* |
| 295 | * El Torito images show up as block devices in an EFI world, |
| 296 | * so let's create them here |
| 297 | */ |
| 298 | disks += efi_disk_create_eltorito(desc, if_typename, |
| 299 | desc->devnum); |
| 300 | } |
| 301 | #else |
| 302 | int i, if_type; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 303 | |
| 304 | /* Search for all available disk devices */ |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 305 | for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) { |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 306 | const struct blk_driver *cur_drvr; |
| 307 | const char *if_typename; |
| 308 | |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 309 | cur_drvr = blk_driver_lookup_type(if_type); |
| 310 | if (!cur_drvr) |
| 311 | continue; |
| 312 | |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 313 | if_typename = cur_drvr->if_typename; |
| 314 | printf("Scanning disks on %s...\n", if_typename); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 315 | for (i = 0; i < 4; i++) { |
| 316 | struct blk_desc *desc; |
Alexander Graf | 3491ee7 | 2016-04-11 16:16:20 +0200 | [diff] [blame] | 317 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 318 | |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 319 | desc = blk_get_devnum_by_type(if_type, i); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 320 | if (!desc) |
| 321 | continue; |
| 322 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 323 | continue; |
| 324 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 325 | snprintf(devname, sizeof(devname), "%s%d", |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 326 | if_typename, i); |
| 327 | efi_disk_add_dev(devname, if_typename, desc, i, 0); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 328 | disks++; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * El Torito images show up as block devices |
| 332 | * in an EFI world, so let's create them here |
| 333 | */ |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 334 | disks += efi_disk_create_eltorito(desc, if_typename, i); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 337 | #endif |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 338 | printf("Found %d disks\n", disks); |
| 339 | |
| 340 | return 0; |
| 341 | } |