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