1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application disk support
5 * Copyright (c) 2016 Alexander Graf
11 #include <efi_loader.h>
15 const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
18 * struct efi_disk_obj - EFI disk object
20 * @header: EFI object header
21 * @ops: EFI disk I/O protocol interface
22 * @ifname: interface name for block device
23 * @dev_index: device index of block device
24 * @media: block I/O media information
25 * @dp: device path to the block device
27 * @volume: simple file system protocol of the partition
28 * @offset: offset into disk for simple partition
29 * @desc: internal block device descriptor
32 struct efi_object header;
33 struct efi_block_io ops;
36 struct efi_block_io_media media;
37 struct efi_device_path *dp;
39 struct efi_simple_file_system_protocol *volume;
41 struct blk_desc *desc;
44 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
45 char extended_verification)
47 EFI_ENTRY("%p, %x", this, extended_verification);
48 return EFI_EXIT(EFI_DEVICE_ERROR);
51 enum efi_disk_direction {
56 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
57 u32 media_id, u64 lba, unsigned long buffer_size,
58 void *buffer, enum efi_disk_direction direction)
60 struct efi_disk_obj *diskobj;
61 struct blk_desc *desc;
66 diskobj = container_of(this, struct efi_disk_obj, ops);
67 desc = (struct blk_desc *) diskobj->desc;
69 blocks = buffer_size / blksz;
70 lba += diskobj->offset;
72 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
73 blocks, lba, blksz, direction);
75 /* We only support full block access */
76 if (buffer_size & (blksz - 1))
77 return EFI_BAD_BUFFER_SIZE;
79 if (direction == EFI_DISK_READ)
80 n = blk_dread(desc, lba, blocks, buffer);
82 n = blk_dwrite(desc, lba, blocks, buffer);
84 /* We don't do interrupts, so check for timers cooperatively */
87 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
90 return EFI_DEVICE_ERROR;
95 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
96 u32 media_id, u64 lba, efi_uintn_t buffer_size,
99 void *real_buffer = buffer;
103 return EFI_INVALID_PARAMETER;
104 /* TODO: check for media changes */
105 if (media_id != this->media->media_id)
106 return EFI_MEDIA_CHANGED;
107 if (!this->media->media_present)
109 /* media->io_align is a power of 2 */
110 if ((uintptr_t)buffer & (this->media->io_align - 1))
111 return EFI_INVALID_PARAMETER;
112 if (lba * this->media->block_size + buffer_size >
113 this->media->last_block * this->media->block_size)
114 return EFI_INVALID_PARAMETER;
116 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
117 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
118 r = efi_disk_read_blocks(this, media_id, lba,
119 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
120 if (r != EFI_SUCCESS)
122 return efi_disk_read_blocks(this, media_id, lba +
123 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
124 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
125 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
128 real_buffer = efi_bounce_buffer;
131 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
132 buffer_size, buffer);
134 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
137 /* Copy from bounce buffer to real buffer if necessary */
138 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
139 memcpy(buffer, real_buffer, buffer_size);
144 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
145 u32 media_id, u64 lba, efi_uintn_t buffer_size,
148 void *real_buffer = buffer;
152 return EFI_INVALID_PARAMETER;
153 if (this->media->read_only)
154 return EFI_WRITE_PROTECTED;
155 /* TODO: check for media changes */
156 if (media_id != this->media->media_id)
157 return EFI_MEDIA_CHANGED;
158 if (!this->media->media_present)
160 /* media->io_align is a power of 2 */
161 if ((uintptr_t)buffer & (this->media->io_align - 1))
162 return EFI_INVALID_PARAMETER;
163 if (lba * this->media->block_size + buffer_size >
164 this->media->last_block * this->media->block_size)
165 return EFI_INVALID_PARAMETER;
167 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
168 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
169 r = efi_disk_write_blocks(this, media_id, lba,
170 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
171 if (r != EFI_SUCCESS)
173 return efi_disk_write_blocks(this, media_id, lba +
174 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
175 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
176 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
179 real_buffer = efi_bounce_buffer;
182 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
183 buffer_size, buffer);
185 /* Populate bounce buffer if necessary */
186 if (real_buffer != buffer)
187 memcpy(real_buffer, buffer, buffer_size);
189 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
195 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
197 /* We always write synchronously */
198 EFI_ENTRY("%p", this);
199 return EFI_EXIT(EFI_SUCCESS);
202 static const struct efi_block_io block_io_disk_template = {
203 .reset = &efi_disk_reset,
204 .read_blocks = &efi_disk_read_blocks,
205 .write_blocks = &efi_disk_write_blocks,
206 .flush_blocks = &efi_disk_flush_blocks,
210 * Get the simple file system protocol for a file device path.
212 * The full path provided is split into device part and into a file
213 * part. The device part is used to find the handle on which the
214 * simple file system protocol is installed.
216 * @full_path device path including device and file
217 * @return simple file system protocol
219 struct efi_simple_file_system_protocol *
220 efi_fs_from_path(struct efi_device_path *full_path)
222 struct efi_object *efiobj;
223 struct efi_handler *handler;
224 struct efi_device_path *device_path;
225 struct efi_device_path *file_path;
228 /* Split the path into a device part and a file part */
229 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
230 if (ret != EFI_SUCCESS)
232 efi_free_pool(file_path);
234 /* Get the EFI object for the partition */
235 efiobj = efi_dp_find_obj(device_path, NULL);
236 efi_free_pool(device_path);
240 /* Find the simple file system protocol */
241 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
243 if (ret != EFI_SUCCESS)
246 /* Return the simple file system protocol for the partition */
247 return handler->protocol_interface;
251 * Create a handle for a partition or disk
253 * @parent parent handle
254 * @dp_parent parent device path
255 * @if_typename interface name for block device
256 * @desc internal block device
257 * @dev_index device index for block device
258 * @offset offset into disk for simple partitions
259 * @return disk object
261 static efi_status_t efi_disk_add_dev(
263 struct efi_device_path *dp_parent,
264 const char *if_typename,
265 struct blk_desc *desc,
269 struct efi_disk_obj **disk)
271 struct efi_disk_obj *diskobj;
274 /* Don't add empty devices */
276 return EFI_NOT_READY;
278 diskobj = calloc(1, sizeof(*diskobj));
280 return EFI_OUT_OF_RESOURCES;
282 /* Hook up to the device list */
283 efi_add_handle(&diskobj->header);
285 /* Fill in object data */
287 struct efi_device_path *node = efi_dp_part_node(desc, part);
289 diskobj->dp = efi_dp_append_node(dp_parent, node);
292 diskobj->dp = efi_dp_from_part(desc, part);
294 diskobj->part = part;
295 ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
297 if (ret != EFI_SUCCESS)
299 ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
301 if (ret != EFI_SUCCESS)
304 diskobj->volume = efi_simple_file_system(desc, part,
306 ret = efi_add_protocol(&diskobj->header,
307 &efi_simple_file_system_protocol_guid,
309 if (ret != EFI_SUCCESS)
312 diskobj->ops = block_io_disk_template;
313 diskobj->ifname = if_typename;
314 diskobj->dev_index = dev_index;
315 diskobj->offset = offset;
316 diskobj->desc = desc;
318 /* Fill in EFI IO Media info (for read/write callbacks) */
319 diskobj->media.removable_media = desc->removable;
320 diskobj->media.media_present = 1;
322 * MediaID is just an arbitrary counter.
323 * We have to change it if the medium is removed or changed.
325 diskobj->media.media_id = 1;
326 diskobj->media.block_size = desc->blksz;
327 diskobj->media.io_align = desc->blksz;
328 diskobj->media.last_block = desc->lba - offset;
330 diskobj->media.logical_partition = 1;
331 diskobj->ops.media = &diskobj->media;
338 * Create handles and protocols for the partitions of a block device
340 * @parent handle of the parent disk
341 * @blk_desc block device
342 * @if_typename interface type
343 * @diskid device number
344 * @pdevname device name
345 * @return number of partitions created
347 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
348 const char *if_typename, int diskid,
349 const char *pdevname)
352 char devname[32] = { 0 }; /* dp->str is u16[32] long */
353 disk_partition_t info;
355 struct efi_device_path *dp = NULL;
357 struct efi_handler *handler;
359 /* Get the device path of the parent */
360 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
361 if (ret == EFI_SUCCESS)
362 dp = handler->protocol_interface;
364 /* Add devices for each partition */
365 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
366 if (part_get_info(desc, part, &info))
368 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
370 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
371 info.start, part, NULL);
372 if (ret != EFI_SUCCESS) {
373 printf("Adding partition %s failed\n", pdevname);
383 * U-Boot doesn't have a list of all online disk devices. So when running our
384 * EFI payload, we scan through all of the potentially available ones and
385 * store them in our object pool.
387 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
388 * Consider converting the code to look up devices as needed. The EFI device
389 * could be a child of the UCLASS_BLK block device, perhaps.
391 * This gets called from do_bootefi_exec().
393 efi_status_t efi_disk_register(void)
395 struct efi_disk_obj *disk;
401 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
402 uclass_next_device_check(&dev)) {
403 struct blk_desc *desc = dev_get_uclass_platdata(dev);
404 const char *if_typename = blk_get_if_type_name(desc->if_type);
406 /* Add block device for the full device */
407 printf("Scanning disk %s...\n", dev->name);
408 ret = efi_disk_add_dev(NULL, NULL, if_typename,
409 desc, desc->devnum, 0, 0, &disk);
410 if (ret == EFI_NOT_READY) {
411 printf("Disk %s not ready\n", dev->name);
415 printf("ERROR: failure to add disk device %s, r = %lu\n",
416 dev->name, ret & ~EFI_ERROR_MASK);
421 /* Partitions show up as block devices in EFI */
422 disks += efi_disk_create_partitions(
423 &disk->header, desc, if_typename,
424 desc->devnum, dev->name);
429 /* Search for all available disk devices */
430 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
431 const struct blk_driver *cur_drvr;
432 const char *if_typename;
434 cur_drvr = blk_driver_lookup_type(if_type);
438 if_typename = cur_drvr->if_typename;
439 printf("Scanning disks on %s...\n", if_typename);
440 for (i = 0; i < 4; i++) {
441 struct blk_desc *desc;
442 char devname[32] = { 0 }; /* dp->str is u16[32] long */
444 desc = blk_get_devnum_by_type(if_type, i);
447 if (desc->type == DEV_TYPE_UNKNOWN)
450 snprintf(devname, sizeof(devname), "%s%d",
453 /* Add block device for the full device */
454 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
456 if (ret == EFI_NOT_READY) {
457 printf("Disk %s not ready\n", devname);
461 printf("ERROR: failure to add disk device %s, r = %lu\n",
462 devname, ret & ~EFI_ERROR_MASK);
467 /* Partitions show up as block devices in EFI */
468 disks += efi_disk_create_partitions
469 (&disk->header, desc,
470 if_typename, i, devname);
474 printf("Found %d disks\n", disks);