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 <efi_loader.h> 11 #include <inttypes.h> 12 #include <part.h> 13 #include <malloc.h> 14 15 static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID; 16 17 struct efi_disk_obj { 18 /* Generic EFI object parent class data */ 19 struct efi_object parent; 20 /* EFI Interface callback struct for block I/O */ 21 struct efi_block_io ops; 22 /* U-Boot ifname for block device */ 23 const char *ifname; 24 /* U-Boot dev_index for block device */ 25 int dev_index; 26 /* EFI Interface Media descriptor struct, referenced by ops */ 27 struct efi_block_io_media media; 28 /* EFI device path to this block device */ 29 struct efi_device_path_file_path *dp; 30 }; 31 32 static void ascii2unicode(u16 *unicode, char *ascii) 33 { 34 while (*ascii) 35 *(unicode++) = *(ascii++); 36 } 37 38 static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol, 39 void **protocol_interface, void *agent_handle, 40 void *controller_handle, uint32_t attributes) 41 { 42 struct efi_disk_obj *diskobj = handle; 43 44 *protocol_interface = &diskobj->ops; 45 46 return EFI_SUCCESS; 47 } 48 49 static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol, 50 void **protocol_interface, void *agent_handle, 51 void *controller_handle, uint32_t attributes) 52 { 53 struct efi_disk_obj *diskobj = handle; 54 55 *protocol_interface = diskobj->dp; 56 57 return EFI_SUCCESS; 58 } 59 60 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this, 61 char extended_verification) 62 { 63 EFI_ENTRY("%p, %x", this, extended_verification); 64 return EFI_EXIT(EFI_DEVICE_ERROR); 65 } 66 67 enum efi_disk_direction { 68 EFI_DISK_READ, 69 EFI_DISK_WRITE, 70 }; 71 72 static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this, 73 u32 media_id, u64 lba, unsigned long buffer_size, 74 void *buffer, enum efi_disk_direction direction) 75 { 76 struct efi_disk_obj *diskobj; 77 struct blk_desc *desc; 78 int blksz; 79 int blocks; 80 unsigned long n; 81 82 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba, 83 buffer_size, buffer); 84 85 diskobj = container_of(this, struct efi_disk_obj, ops); 86 if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index))) 87 return EFI_EXIT(EFI_DEVICE_ERROR); 88 blksz = desc->blksz; 89 blocks = buffer_size / blksz; 90 91 #ifdef DEBUG_EFI 92 printf("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__, 93 __LINE__, blocks, lba, blksz, direction); 94 #endif 95 96 /* We only support full block access */ 97 if (buffer_size & (blksz - 1)) 98 return EFI_EXIT(EFI_DEVICE_ERROR); 99 100 if (direction == EFI_DISK_READ) 101 n = desc->block_read(desc, lba, blocks, buffer); 102 else 103 n = desc->block_write(desc, lba, blocks, buffer); 104 105 /* We don't do interrupts, so check for timers cooperatively */ 106 efi_timer_check(); 107 108 #ifdef DEBUG_EFI 109 printf("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks); 110 #endif 111 if (n != blocks) 112 return EFI_EXIT(EFI_DEVICE_ERROR); 113 114 return EFI_EXIT(EFI_SUCCESS); 115 } 116 117 static efi_status_t efi_disk_read_blocks(struct efi_block_io *this, 118 u32 media_id, u64 lba, unsigned long buffer_size, 119 void *buffer) 120 { 121 return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer, 122 EFI_DISK_READ); 123 } 124 125 static efi_status_t efi_disk_write_blocks(struct efi_block_io *this, 126 u32 media_id, u64 lba, unsigned long buffer_size, 127 void *buffer) 128 { 129 return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer, 130 EFI_DISK_WRITE); 131 } 132 133 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this) 134 { 135 /* We always write synchronously */ 136 EFI_ENTRY("%p", this); 137 return EFI_EXIT(EFI_SUCCESS); 138 } 139 140 static const struct efi_block_io block_io_disk_template = { 141 .reset = &efi_disk_reset, 142 .read_blocks = &efi_disk_read_blocks, 143 .write_blocks = &efi_disk_write_blocks, 144 .flush_blocks = &efi_disk_flush_blocks, 145 }; 146 147 /* 148 * U-Boot doesn't have a list of all online disk devices. So when running our 149 * EFI payload, we scan through all of the potentially available ones and 150 * store them in our object pool. 151 * 152 * This gets called from do_bootefi_exec(). 153 */ 154 int efi_disk_register(void) 155 { 156 const struct block_drvr *cur_drvr; 157 int i; 158 int disks = 0; 159 160 /* Search for all available disk devices */ 161 for (cur_drvr = block_drvr; cur_drvr->name; cur_drvr++) { 162 printf("Scanning disks on %s...\n", cur_drvr->name); 163 for (i = 0; i < 4; i++) { 164 struct blk_desc *desc; 165 struct efi_disk_obj *diskobj; 166 struct efi_device_path_file_path *dp; 167 int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2); 168 char devname[16] = { 0 }; /* dp->str is u16[16] long */ 169 170 desc = blk_get_dev(cur_drvr->name, i); 171 if (!desc) 172 continue; 173 if (desc->type == DEV_TYPE_UNKNOWN) 174 continue; 175 176 diskobj = calloc(1, objlen); 177 178 /* Fill in object data */ 179 diskobj->parent.protocols[0].guid = &efi_block_io_guid; 180 diskobj->parent.protocols[0].open = efi_disk_open_block; 181 diskobj->parent.protocols[1].guid = &efi_guid_device_path; 182 diskobj->parent.protocols[1].open = efi_disk_open_dp; 183 diskobj->parent.handle = diskobj; 184 diskobj->ops = block_io_disk_template; 185 diskobj->ifname = cur_drvr->name; 186 diskobj->dev_index = i; 187 188 /* Fill in EFI IO Media info (for read/write callbacks) */ 189 diskobj->media.removable_media = desc->removable; 190 diskobj->media.media_present = 1; 191 diskobj->media.block_size = desc->blksz; 192 diskobj->media.io_align = desc->blksz; 193 diskobj->media.last_block = desc->lba; 194 diskobj->ops.media = &diskobj->media; 195 196 /* Fill in device path */ 197 dp = (void*)&diskobj[1]; 198 diskobj->dp = dp; 199 dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; 200 dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; 201 dp[0].dp.length = sizeof(*dp); 202 snprintf(devname, sizeof(devname), "%s%d", 203 cur_drvr->name, i); 204 ascii2unicode(dp[0].str, devname); 205 206 dp[1].dp.type = DEVICE_PATH_TYPE_END; 207 dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END; 208 dp[1].dp.length = sizeof(*dp); 209 210 /* Hook up to the device list */ 211 list_add_tail(&diskobj->parent.link, &efi_obj_list); 212 disks++; 213 } 214 } 215 printf("Found %d disks\n", disks); 216 217 return 0; 218 } 219