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 *dp;
34 /* handle to filesys proto (for partition objects) */
35 struct efi_simple_file_system_protocol *volume;
36 /* Offset into disk for simple partitions */
38 /* Internal block device */
39 struct blk_desc *desc;
42 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
43 char extended_verification)
45 EFI_ENTRY("%p, %x", this, extended_verification);
46 return EFI_EXIT(EFI_DEVICE_ERROR);
49 enum efi_disk_direction {
54 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
55 u32 media_id, u64 lba, unsigned long buffer_size,
56 void *buffer, enum efi_disk_direction direction)
58 struct efi_disk_obj *diskobj;
59 struct blk_desc *desc;
64 diskobj = container_of(this, struct efi_disk_obj, ops);
65 desc = (struct blk_desc *) diskobj->desc;
67 blocks = buffer_size / blksz;
68 lba += diskobj->offset;
70 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
71 __LINE__, blocks, lba, blksz, direction);
73 /* We only support full block access */
74 if (buffer_size & (blksz - 1))
75 return EFI_DEVICE_ERROR;
77 if (direction == EFI_DISK_READ)
78 n = blk_dread(desc, lba, blocks, buffer);
80 n = blk_dwrite(desc, lba, blocks, buffer);
82 /* We don't do interrupts, so check for timers cooperatively */
85 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
88 return EFI_DEVICE_ERROR;
93 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
94 u32 media_id, u64 lba, unsigned long buffer_size,
97 void *real_buffer = buffer;
100 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
101 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
102 r = efi_disk_read_blocks(this, media_id, lba,
103 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
104 if (r != EFI_SUCCESS)
106 return efi_disk_read_blocks(this, media_id, lba +
107 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
108 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
109 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
112 real_buffer = efi_bounce_buffer;
115 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
116 buffer_size, buffer);
118 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
121 /* Copy from bounce buffer to real buffer if necessary */
122 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
123 memcpy(buffer, real_buffer, buffer_size);
128 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
129 u32 media_id, u64 lba, unsigned long buffer_size,
132 void *real_buffer = buffer;
135 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
136 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
137 r = efi_disk_write_blocks(this, media_id, lba,
138 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
139 if (r != EFI_SUCCESS)
141 return efi_disk_write_blocks(this, media_id, lba +
142 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
143 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
144 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
147 real_buffer = efi_bounce_buffer;
150 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
151 buffer_size, buffer);
153 /* Populate bounce buffer if necessary */
154 if (real_buffer != buffer)
155 memcpy(real_buffer, buffer, buffer_size);
157 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
163 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
165 /* We always write synchronously */
166 EFI_ENTRY("%p", this);
167 return EFI_EXIT(EFI_SUCCESS);
170 static const struct efi_block_io block_io_disk_template = {
171 .reset = &efi_disk_reset,
172 .read_blocks = &efi_disk_read_blocks,
173 .write_blocks = &efi_disk_write_blocks,
174 .flush_blocks = &efi_disk_flush_blocks,
178 * Find filesystem from a device-path. The passed in path 'p' probably
179 * contains one or more /File(name) nodes, so the comparison stops at
180 * the first /File() node, and returns the pointer to that via 'rp'.
181 * This is mostly intended to be a helper to map a device-path to an
182 * efi_file_handle object.
184 struct efi_simple_file_system_protocol *
185 efi_fs_from_path(struct efi_device_path *fp)
187 struct efi_object *efiobj;
188 struct efi_disk_obj *diskobj;
190 efiobj = efi_dp_find_obj(fp, NULL);
194 diskobj = container_of(efiobj, struct efi_disk_obj, parent);
196 return diskobj->volume;
199 static void efi_disk_add_dev(const char *name,
200 const char *if_typename,
201 struct blk_desc *desc,
206 struct efi_disk_obj *diskobj;
208 /* Don't add empty devices */
212 diskobj = calloc(1, sizeof(*diskobj));
214 /* Fill in object data */
215 diskobj->dp = efi_dp_from_part(desc, part);
216 diskobj->part = part;
217 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
218 diskobj->parent.protocols[0].protocol_interface = &diskobj->ops;
219 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
220 diskobj->parent.protocols[1].protocol_interface = diskobj->dp;
222 diskobj->volume = efi_simple_file_system(desc, part,
224 diskobj->parent.protocols[2].guid =
225 &efi_simple_file_system_protocol_guid;
226 diskobj->parent.protocols[2].protocol_interface =
229 diskobj->parent.handle = diskobj;
230 diskobj->ops = block_io_disk_template;
231 diskobj->ifname = if_typename;
232 diskobj->dev_index = dev_index;
233 diskobj->offset = offset;
234 diskobj->desc = desc;
236 /* Fill in EFI IO Media info (for read/write callbacks) */
237 diskobj->media.removable_media = desc->removable;
238 diskobj->media.media_present = 1;
239 diskobj->media.block_size = desc->blksz;
240 diskobj->media.io_align = desc->blksz;
241 diskobj->media.last_block = desc->lba - offset;
242 diskobj->ops.media = &diskobj->media;
244 /* Hook up to the device list */
245 list_add_tail(&diskobj->parent.link, &efi_obj_list);
248 static int efi_disk_create_eltorito(struct blk_desc *desc,
249 const char *if_typename,
251 const char *pdevname)
254 #if CONFIG_IS_ENABLED(ISO_PARTITION)
255 char devname[32] = { 0 }; /* dp->str is u16[32] long */
256 disk_partition_t info;
259 if (desc->part_type != PART_TYPE_ISO)
262 /* and devices for each partition: */
263 while (!part_get_info(desc, part, &info)) {
264 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
266 efi_disk_add_dev(devname, if_typename, desc, diskid,
272 /* ... and add block device: */
273 efi_disk_add_dev(devname, if_typename, desc, diskid, 0, 0);
280 * U-Boot doesn't have a list of all online disk devices. So when running our
281 * EFI payload, we scan through all of the potentially available ones and
282 * store them in our object pool.
284 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
285 * Consider converting the code to look up devices as needed. The EFI device
286 * could be a child of the UCLASS_BLK block device, perhaps.
288 * This gets called from do_bootefi_exec().
290 int efi_disk_register(void)
296 for (uclass_first_device_check(UCLASS_BLK, &dev);
298 uclass_next_device_check(&dev)) {
299 struct blk_desc *desc = dev_get_uclass_platdata(dev);
300 const char *if_typename = dev->driver->name;
301 disk_partition_t info;
304 printf("Scanning disk %s...\n", dev->name);
306 /* add devices for each partition: */
307 while (!part_get_info(desc, part, &info)) {
308 efi_disk_add_dev(dev->name, if_typename, desc,
309 desc->devnum, 0, part);
313 /* ... and add block device: */
314 efi_disk_add_dev(dev->name, if_typename, desc,
320 * El Torito images show up as block devices in an EFI world,
321 * so let's create them here
323 disks += efi_disk_create_eltorito(desc, if_typename,
324 desc->devnum, dev->name);
329 /* Search for all available disk devices */
330 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
331 const struct blk_driver *cur_drvr;
332 const char *if_typename;
334 cur_drvr = blk_driver_lookup_type(if_type);
338 if_typename = cur_drvr->if_typename;
339 printf("Scanning disks on %s...\n", if_typename);
340 for (i = 0; i < 4; i++) {
341 struct blk_desc *desc;
342 char devname[32] = { 0 }; /* dp->str is u16[32] long */
344 desc = blk_get_devnum_by_type(if_type, i);
347 if (desc->type == DEV_TYPE_UNKNOWN)
350 snprintf(devname, sizeof(devname), "%s%d",
352 efi_disk_add_dev(devname, if_typename, desc, i, 0, 0);
356 * El Torito images show up as block devices
357 * in an EFI world, so let's create them here
359 disks += efi_disk_create_eltorito(desc, if_typename,
364 printf("Found %d disks\n", disks);