2 * EFI application disk support
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <efi_loader.h>
17 static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
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 */
26 /* U-Boot dev_index for block device */
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 */
34 /* Internal block device */
35 const struct blk_desc *desc;
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)
42 struct efi_disk_obj *diskobj = handle;
44 *protocol_interface = &diskobj->ops;
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)
53 struct efi_disk_obj *diskobj = handle;
55 *protocol_interface = diskobj->dp;
60 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
61 char extended_verification)
63 EFI_ENTRY("%p, %x", this, extended_verification);
64 return EFI_EXIT(EFI_DEVICE_ERROR);
67 enum efi_disk_direction {
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)
76 struct efi_disk_obj *diskobj;
77 struct blk_desc *desc;
82 diskobj = container_of(this, struct efi_disk_obj, ops);
83 desc = (struct blk_desc *) diskobj->desc;
85 blocks = buffer_size / blksz;
86 lba += diskobj->offset;
88 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
89 __LINE__, blocks, lba, blksz, direction);
91 /* We only support full block access */
92 if (buffer_size & (blksz - 1))
93 return EFI_EXIT(EFI_DEVICE_ERROR);
95 if (direction == EFI_DISK_READ)
96 n = blk_dread(desc, lba, blocks, buffer);
98 n = blk_dwrite(desc, lba, blocks, buffer);
100 /* We don't do interrupts, so check for timers cooperatively */
103 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
106 return EFI_EXIT(EFI_DEVICE_ERROR);
108 return EFI_EXIT(EFI_SUCCESS);
111 static efi_status_t efi_disk_read_blocks(struct efi_block_io *this,
112 u32 media_id, u64 lba, unsigned long buffer_size,
115 void *real_buffer = buffer;
118 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
119 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
120 r = efi_disk_read_blocks(this, media_id, lba,
121 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
122 if (r != EFI_SUCCESS)
124 return efi_disk_read_blocks(this, media_id, lba +
125 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
126 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
127 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
130 real_buffer = efi_bounce_buffer;
133 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
134 buffer_size, buffer);
136 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
139 /* Copy from bounce buffer to real buffer if necessary */
140 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
141 memcpy(buffer, real_buffer, buffer_size);
146 static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
147 u32 media_id, u64 lba, unsigned long buffer_size,
150 void *real_buffer = buffer;
153 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
154 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
155 r = efi_disk_write_blocks(this, media_id, lba,
156 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
157 if (r != EFI_SUCCESS)
159 return efi_disk_write_blocks(this, media_id, lba +
160 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
161 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
162 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
165 real_buffer = efi_bounce_buffer;
168 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
169 buffer_size, buffer);
171 /* Populate bounce buffer if necessary */
172 if (real_buffer != buffer)
173 memcpy(real_buffer, buffer, buffer_size);
175 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
181 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
183 /* We always write synchronously */
184 EFI_ENTRY("%p", this);
185 return EFI_EXIT(EFI_SUCCESS);
188 static const struct efi_block_io block_io_disk_template = {
189 .reset = &efi_disk_reset,
190 .read_blocks = &efi_disk_read_blocks,
191 .write_blocks = &efi_disk_write_blocks,
192 .flush_blocks = &efi_disk_flush_blocks,
195 static void efi_disk_add_dev(const char *name,
196 const char *if_typename,
197 const struct blk_desc *desc,
201 struct efi_disk_obj *diskobj;
202 struct efi_device_path_file_path *dp;
203 int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
205 /* Don't add empty devices */
209 diskobj = calloc(1, objlen);
211 /* Fill in object data */
212 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
213 diskobj->parent.protocols[0].open = efi_disk_open_block;
214 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
215 diskobj->parent.protocols[1].open = efi_disk_open_dp;
216 diskobj->parent.handle = diskobj;
217 diskobj->ops = block_io_disk_template;
218 diskobj->ifname = if_typename;
219 diskobj->dev_index = dev_index;
220 diskobj->offset = offset;
221 diskobj->desc = desc;
223 /* Fill in EFI IO Media info (for read/write callbacks) */
224 diskobj->media.removable_media = desc->removable;
225 diskobj->media.media_present = 1;
226 diskobj->media.block_size = desc->blksz;
227 diskobj->media.io_align = desc->blksz;
228 diskobj->media.last_block = desc->lba - offset;
229 diskobj->ops.media = &diskobj->media;
231 /* Fill in device path */
232 dp = (void*)&diskobj[1];
234 dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
235 dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
236 dp[0].dp.length = sizeof(*dp);
237 ascii2unicode(dp[0].str, name);
239 dp[1].dp.type = DEVICE_PATH_TYPE_END;
240 dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
241 dp[1].dp.length = sizeof(*dp);
243 /* Hook up to the device list */
244 list_add_tail(&diskobj->parent.link, &efi_obj_list);
247 static int efi_disk_create_eltorito(struct blk_desc *desc,
248 const char *if_typename,
250 const char *pdevname)
253 #ifdef CONFIG_ISO_PARTITION
254 char devname[32] = { 0 }; /* dp->str is u16[32] long */
255 disk_partition_t info;
258 if (desc->part_type != PART_TYPE_ISO)
261 while (!part_get_info(desc, part, &info)) {
262 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
264 efi_disk_add_dev(devname, if_typename, desc, diskid,
275 * U-Boot doesn't have a list of all online disk devices. So when running our
276 * EFI payload, we scan through all of the potentially available ones and
277 * store them in our object pool.
280 * Consider converting the code to look up devices as needed. The EFI device
281 * could be a child of the UCLASS_BLK block device, perhaps.
283 * This gets called from do_bootefi_exec().
285 int efi_disk_register(void)
291 for (uclass_first_device(UCLASS_BLK, &dev);
293 uclass_next_device(&dev)) {
294 struct blk_desc *desc = dev_get_uclass_platdata(dev);
295 const char *if_typename = dev->driver->name;
297 printf("Scanning disk %s...\n", dev->name);
298 efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
302 * El Torito images show up as block devices in an EFI world,
303 * so let's create them here
305 disks += efi_disk_create_eltorito(desc, if_typename,
306 desc->devnum, dev->name);
311 /* Search for all available disk devices */
312 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
313 const struct blk_driver *cur_drvr;
314 const char *if_typename;
316 cur_drvr = blk_driver_lookup_type(if_type);
320 if_typename = cur_drvr->if_typename;
321 printf("Scanning disks on %s...\n", if_typename);
322 for (i = 0; i < 4; i++) {
323 struct blk_desc *desc;
324 char devname[32] = { 0 }; /* dp->str is u16[32] long */
326 desc = blk_get_devnum_by_type(if_type, i);
329 if (desc->type == DEV_TYPE_UNKNOWN)
332 snprintf(devname, sizeof(devname), "%s%d",
334 efi_disk_add_dev(devname, if_typename, desc, i, 0);
338 * El Torito images show up as block devices
339 * in an EFI world, so let's create them here
341 disks += efi_disk_create_eltorito(desc, if_typename,
346 printf("Found %d disks\n", disks);