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> 10 #include <blk.h> 11 #include <dm.h> 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; 32 /* Offset into disk for simple partitions */ 33 lbaint_t offset; 34 /* Internal block device */ 35 const struct blk_desc *desc; 36 }; 37 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 60 diskobj = container_of(this, struct efi_disk_obj, ops); 61 desc = (struct blk_desc *) diskobj->desc; 62 blksz = desc->blksz; 63 blocks = buffer_size / blksz; 64 lba += diskobj->offset; 65 66 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__, 67 __LINE__, blocks, lba, blksz, direction); 68 69 /* We only support full block access */ 70 if (buffer_size & (blksz - 1)) 71 return EFI_DEVICE_ERROR; 72 73 if (direction == EFI_DISK_READ) 74 n = blk_dread(desc, lba, blocks, buffer); 75 else 76 n = blk_dwrite(desc, lba, blocks, buffer); 77 78 /* We don't do interrupts, so check for timers cooperatively */ 79 efi_timer_check(); 80 81 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks); 82 83 if (n != blocks) 84 return EFI_DEVICE_ERROR; 85 86 return EFI_SUCCESS; 87 } 88 89 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this, 90 u32 media_id, u64 lba, unsigned long buffer_size, 91 void *buffer) 92 { 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); 122 } 123 124 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this, 125 u32 media_id, u64 lba, unsigned long buffer_size, 126 void *buffer) 127 { 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); 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 173 static void efi_disk_add_dev(const char *name, 174 const char *if_typename, 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 183 /* Don't add empty devices */ 184 if (!desc->lba) 185 return; 186 187 diskobj = calloc(1, objlen); 188 189 /* Fill in object data */ 190 dp = (void *)&diskobj[1]; 191 diskobj->parent.protocols[0].guid = &efi_block_io_guid; 192 diskobj->parent.protocols[0].protocol_interface = &diskobj->ops; 193 diskobj->parent.protocols[1].guid = &efi_guid_device_path; 194 diskobj->parent.protocols[1].protocol_interface = dp; 195 diskobj->parent.handle = diskobj; 196 diskobj->ops = block_io_disk_template; 197 diskobj->ifname = if_typename; 198 diskobj->dev_index = dev_index; 199 diskobj->offset = offset; 200 diskobj->desc = desc; 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; 207 diskobj->media.last_block = desc->lba - offset; 208 diskobj->ops.media = &diskobj->media; 209 210 /* Fill in device path */ 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 225 static int efi_disk_create_eltorito(struct blk_desc *desc, 226 const char *if_typename, 227 int diskid, 228 const char *pdevname) 229 { 230 int disks = 0; 231 #if CONFIG_IS_ENABLED(ISO_PARTITION) 232 char devname[32] = { 0 }; /* dp->str is u16[32] long */ 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)) { 240 snprintf(devname, sizeof(devname), "%s:%d", pdevname, 241 part); 242 efi_disk_add_dev(devname, if_typename, desc, diskid, 243 info.start); 244 part++; 245 disks++; 246 } 247 #endif 248 249 return disks; 250 } 251 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 * 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 * 261 * This gets called from do_bootefi_exec(). 262 */ 263 int efi_disk_register(void) 264 { 265 int disks = 0; 266 #ifdef CONFIG_BLK 267 struct udevice *dev; 268 269 for (uclass_first_device_check(UCLASS_BLK, &dev); 270 dev; 271 uclass_next_device_check(&dev)) { 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, 284 desc->devnum, dev->name); 285 } 286 #else 287 int i, if_type; 288 289 /* Search for all available disk devices */ 290 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) { 291 const struct blk_driver *cur_drvr; 292 const char *if_typename; 293 294 cur_drvr = blk_driver_lookup_type(if_type); 295 if (!cur_drvr) 296 continue; 297 298 if_typename = cur_drvr->if_typename; 299 printf("Scanning disks on %s...\n", if_typename); 300 for (i = 0; i < 4; i++) { 301 struct blk_desc *desc; 302 char devname[32] = { 0 }; /* dp->str is u16[32] long */ 303 304 desc = blk_get_devnum_by_type(if_type, i); 305 if (!desc) 306 continue; 307 if (desc->type == DEV_TYPE_UNKNOWN) 308 continue; 309 310 snprintf(devname, sizeof(devname), "%s%d", 311 if_typename, i); 312 efi_disk_add_dev(devname, if_typename, desc, i, 0); 313 disks++; 314 315 /* 316 * El Torito images show up as block devices 317 * in an EFI world, so let's create them here 318 */ 319 disks += efi_disk_create_eltorito(desc, if_typename, 320 i, devname); 321 } 322 } 323 #endif 324 printf("Found %d disks\n", disks); 325 326 return 0; 327 } 328