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