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 = BLOCK_IO_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 debug("EFI: %s:%d blocks=%x lba=%llx blksz=%x dir=%d\n", __func__,
73 __LINE__, blocks, lba, blksz, direction);
75 /* We only support full block access */
76 if (buffer_size & (blksz - 1))
77 return EFI_DEVICE_ERROR;
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 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, 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;
102 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
103 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
104 r = efi_disk_read_blocks(this, media_id, lba,
105 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
106 if (r != EFI_SUCCESS)
108 return efi_disk_read_blocks(this, media_id, lba +
109 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
110 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
111 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
114 real_buffer = efi_bounce_buffer;
117 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
118 buffer_size, buffer);
120 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
123 /* Copy from bounce buffer to real buffer if necessary */
124 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
125 memcpy(buffer, real_buffer, buffer_size);
130 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
131 u32 media_id, u64 lba, efi_uintn_t buffer_size,
134 void *real_buffer = buffer;
137 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
138 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
139 r = efi_disk_write_blocks(this, media_id, lba,
140 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
141 if (r != EFI_SUCCESS)
143 return efi_disk_write_blocks(this, media_id, lba +
144 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
145 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
146 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
149 real_buffer = efi_bounce_buffer;
152 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
153 buffer_size, buffer);
155 /* Populate bounce buffer if necessary */
156 if (real_buffer != buffer)
157 memcpy(real_buffer, buffer, buffer_size);
159 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
165 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
167 /* We always write synchronously */
168 EFI_ENTRY("%p", this);
169 return EFI_EXIT(EFI_SUCCESS);
172 static const struct efi_block_io block_io_disk_template = {
173 .reset = &efi_disk_reset,
174 .read_blocks = &efi_disk_read_blocks,
175 .write_blocks = &efi_disk_write_blocks,
176 .flush_blocks = &efi_disk_flush_blocks,
180 * Get the simple file system protocol for a file device path.
182 * The full path provided is split into device part and into a file
183 * part. The device part is used to find the handle on which the
184 * simple file system protocol is installed.
186 * @full_path device path including device and file
187 * @return simple file system protocol
189 struct efi_simple_file_system_protocol *
190 efi_fs_from_path(struct efi_device_path *full_path)
192 struct efi_object *efiobj;
193 struct efi_handler *handler;
194 struct efi_device_path *device_path;
195 struct efi_device_path *file_path;
198 /* Split the path into a device part and a file part */
199 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
200 if (ret != EFI_SUCCESS)
202 efi_free_pool(file_path);
204 /* Get the EFI object for the partition */
205 efiobj = efi_dp_find_obj(device_path, NULL);
206 efi_free_pool(device_path);
210 /* Find the simple file system protocol */
211 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
213 if (ret != EFI_SUCCESS)
216 /* Return the simple file system protocol for the partition */
217 return handler->protocol_interface;
221 * Create a handle for a partition or disk
223 * @parent parent handle
224 * @dp_parent parent device path
225 * @if_typename interface name for block device
226 * @desc internal block device
227 * @dev_index device index for block device
228 * @offset offset into disk for simple partitions
229 * @return disk object
231 static efi_status_t efi_disk_add_dev(
233 struct efi_device_path *dp_parent,
234 const char *if_typename,
235 struct blk_desc *desc,
239 struct efi_disk_obj **disk)
241 struct efi_disk_obj *diskobj;
244 /* Don't add empty devices */
246 return EFI_NOT_READY;
248 diskobj = calloc(1, sizeof(*diskobj));
250 return EFI_OUT_OF_RESOURCES;
252 /* Hook up to the device list */
253 efi_add_handle(&diskobj->header);
255 /* Fill in object data */
257 struct efi_device_path *node = efi_dp_part_node(desc, part);
259 diskobj->dp = efi_dp_append_node(dp_parent, node);
262 diskobj->dp = efi_dp_from_part(desc, part);
264 diskobj->part = part;
265 ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
267 if (ret != EFI_SUCCESS)
269 ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
271 if (ret != EFI_SUCCESS)
274 diskobj->volume = efi_simple_file_system(desc, part,
276 ret = efi_add_protocol(&diskobj->header,
277 &efi_simple_file_system_protocol_guid,
279 if (ret != EFI_SUCCESS)
282 diskobj->ops = block_io_disk_template;
283 diskobj->ifname = if_typename;
284 diskobj->dev_index = dev_index;
285 diskobj->offset = offset;
286 diskobj->desc = desc;
288 /* Fill in EFI IO Media info (for read/write callbacks) */
289 diskobj->media.removable_media = desc->removable;
290 diskobj->media.media_present = 1;
291 diskobj->media.block_size = desc->blksz;
292 diskobj->media.io_align = desc->blksz;
293 diskobj->media.last_block = desc->lba - offset;
295 diskobj->media.logical_partition = 1;
296 diskobj->ops.media = &diskobj->media;
303 * Create handles and protocols for the partitions of a block device
305 * @parent handle of the parent disk
306 * @blk_desc block device
307 * @if_typename interface type
308 * @diskid device number
309 * @pdevname device name
310 * @return number of partitions created
312 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
313 const char *if_typename, int diskid,
314 const char *pdevname)
317 char devname[32] = { 0 }; /* dp->str is u16[32] long */
318 disk_partition_t info;
320 struct efi_device_path *dp = NULL;
322 struct efi_handler *handler;
324 /* Get the device path of the parent */
325 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
326 if (ret == EFI_SUCCESS)
327 dp = handler->protocol_interface;
329 /* Add devices for each partition */
330 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
331 if (part_get_info(desc, part, &info))
333 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
335 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
336 info.start, part, NULL);
337 if (ret != EFI_SUCCESS) {
338 printf("Adding partition %s failed\n", pdevname);
348 * U-Boot doesn't have a list of all online disk devices. So when running our
349 * EFI payload, we scan through all of the potentially available ones and
350 * store them in our object pool.
352 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
353 * Consider converting the code to look up devices as needed. The EFI device
354 * could be a child of the UCLASS_BLK block device, perhaps.
356 * This gets called from do_bootefi_exec().
358 efi_status_t efi_disk_register(void)
360 struct efi_disk_obj *disk;
366 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
367 uclass_next_device_check(&dev)) {
368 struct blk_desc *desc = dev_get_uclass_platdata(dev);
369 const char *if_typename = blk_get_if_type_name(desc->if_type);
371 /* Add block device for the full device */
372 printf("Scanning disk %s...\n", dev->name);
373 ret = efi_disk_add_dev(NULL, NULL, if_typename,
374 desc, desc->devnum, 0, 0, &disk);
375 if (ret == EFI_NOT_READY) {
376 printf("Disk %s not ready\n", dev->name);
380 printf("ERROR: failure to add disk device %s, r = %lu\n",
381 dev->name, ret & ~EFI_ERROR_MASK);
386 /* Partitions show up as block devices in EFI */
387 disks += efi_disk_create_partitions(
388 &disk->header, desc, if_typename,
389 desc->devnum, dev->name);
394 /* Search for all available disk devices */
395 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
396 const struct blk_driver *cur_drvr;
397 const char *if_typename;
399 cur_drvr = blk_driver_lookup_type(if_type);
403 if_typename = cur_drvr->if_typename;
404 printf("Scanning disks on %s...\n", if_typename);
405 for (i = 0; i < 4; i++) {
406 struct blk_desc *desc;
407 char devname[32] = { 0 }; /* dp->str is u16[32] long */
409 desc = blk_get_devnum_by_type(if_type, i);
412 if (desc->type == DEV_TYPE_UNKNOWN)
415 snprintf(devname, sizeof(devname), "%s%d",
418 /* Add block device for the full device */
419 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
421 if (ret == EFI_NOT_READY) {
422 printf("Disk %s not ready\n", devname);
426 printf("ERROR: failure to add disk device %s, r = %lu\n",
427 devname, ret & ~EFI_ERROR_MASK);
432 /* Partitions show up as block devices in EFI */
433 disks += efi_disk_create_partitions
434 (&disk->header, desc,
435 if_typename, i, devname);
439 printf("Found %d disks\n", disks);