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 EFIAPI efi_disk_reset(struct efi_block_io *this,
39 char extended_verification)
41 EFI_ENTRY("%p, %x", this, extended_verification);
42 return EFI_EXIT(EFI_DEVICE_ERROR);
45 enum efi_disk_direction {
50 static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
51 u32 media_id, u64 lba, unsigned long buffer_size,
52 void *buffer, enum efi_disk_direction direction)
54 struct efi_disk_obj *diskobj;
55 struct blk_desc *desc;
60 diskobj = container_of(this, struct efi_disk_obj, ops);
61 desc = (struct blk_desc *) diskobj->desc;
63 blocks = buffer_size / blksz;
64 lba += diskobj->offset;
66 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
67 __LINE__, blocks, lba, blksz, direction);
69 /* We only support full block access */
70 if (buffer_size & (blksz - 1))
71 return EFI_DEVICE_ERROR;
73 if (direction == EFI_DISK_READ)
74 n = blk_dread(desc, lba, blocks, buffer);
76 n = blk_dwrite(desc, lba, blocks, buffer);
78 /* We don't do interrupts, so check for timers cooperatively */
81 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
84 return EFI_DEVICE_ERROR;
89 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
90 u32 media_id, u64 lba, unsigned long buffer_size,
93 void *real_buffer = buffer;
96 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
97 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
98 r = efi_disk_read_blocks(this, media_id, lba,
99 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
100 if (r != EFI_SUCCESS)
102 return efi_disk_read_blocks(this, media_id, lba +
103 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
104 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
105 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
108 real_buffer = efi_bounce_buffer;
111 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
112 buffer_size, buffer);
114 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
117 /* Copy from bounce buffer to real buffer if necessary */
118 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
119 memcpy(buffer, real_buffer, buffer_size);
124 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
125 u32 media_id, u64 lba, unsigned long buffer_size,
128 void *real_buffer = buffer;
131 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
132 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
133 r = efi_disk_write_blocks(this, media_id, lba,
134 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
135 if (r != EFI_SUCCESS)
137 return efi_disk_write_blocks(this, media_id, lba +
138 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
139 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
140 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
143 real_buffer = efi_bounce_buffer;
146 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
147 buffer_size, buffer);
149 /* Populate bounce buffer if necessary */
150 if (real_buffer != buffer)
151 memcpy(real_buffer, buffer, buffer_size);
153 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
159 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
161 /* We always write synchronously */
162 EFI_ENTRY("%p", this);
163 return EFI_EXIT(EFI_SUCCESS);
166 static const struct efi_block_io block_io_disk_template = {
167 .reset = &efi_disk_reset,
168 .read_blocks = &efi_disk_read_blocks,
169 .write_blocks = &efi_disk_write_blocks,
170 .flush_blocks = &efi_disk_flush_blocks,
173 static void efi_disk_add_dev(const char *name,
174 const char *if_typename,
175 const struct blk_desc *desc,
179 struct efi_disk_obj *diskobj;
180 struct efi_device_path_file_path *dp;
181 int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
183 /* Don't add empty devices */
187 diskobj = calloc(1, objlen);
189 /* Fill in object data */
190 dp = (void *)&diskobj[1];
191 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
192 diskobj->parent.protocols[0].protocol_interface = &diskobj->ops;
193 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
194 diskobj->parent.protocols[1].protocol_interface = dp;
195 diskobj->parent.handle = diskobj;
196 diskobj->ops = block_io_disk_template;
197 diskobj->ifname = if_typename;
198 diskobj->dev_index = dev_index;
199 diskobj->offset = offset;
200 diskobj->desc = desc;
202 /* Fill in EFI IO Media info (for read/write callbacks) */
203 diskobj->media.removable_media = desc->removable;
204 diskobj->media.media_present = 1;
205 diskobj->media.block_size = desc->blksz;
206 diskobj->media.io_align = desc->blksz;
207 diskobj->media.last_block = desc->lba - offset;
208 diskobj->ops.media = &diskobj->media;
210 /* Fill in device path */
212 dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
213 dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
214 dp[0].dp.length = sizeof(*dp);
215 ascii2unicode(dp[0].str, name);
217 dp[1].dp.type = DEVICE_PATH_TYPE_END;
218 dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
219 dp[1].dp.length = sizeof(*dp);
221 /* Hook up to the device list */
222 list_add_tail(&diskobj->parent.link, &efi_obj_list);
225 static int efi_disk_create_eltorito(struct blk_desc *desc,
226 const char *if_typename,
228 const char *pdevname)
231 #if CONFIG_IS_ENABLED(ISO_PARTITION)
232 char devname[32] = { 0 }; /* dp->str is u16[32] long */
233 disk_partition_t info;
236 if (desc->part_type != PART_TYPE_ISO)
239 while (!part_get_info(desc, part, &info)) {
240 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
242 efi_disk_add_dev(devname, if_typename, desc, diskid,
253 * U-Boot doesn't have a list of all online disk devices. So when running our
254 * EFI payload, we scan through all of the potentially available ones and
255 * store them in our object pool.
257 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
258 * Consider converting the code to look up devices as needed. The EFI device
259 * could be a child of the UCLASS_BLK block device, perhaps.
261 * This gets called from do_bootefi_exec().
263 int efi_disk_register(void)
269 for (uclass_first_device_check(UCLASS_BLK, &dev);
271 uclass_next_device_check(&dev)) {
272 struct blk_desc *desc = dev_get_uclass_platdata(dev);
273 const char *if_typename = dev->driver->name;
275 printf("Scanning disk %s...\n", dev->name);
276 efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
280 * El Torito images show up as block devices in an EFI world,
281 * so let's create them here
283 disks += efi_disk_create_eltorito(desc, if_typename,
284 desc->devnum, dev->name);
289 /* Search for all available disk devices */
290 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
291 const struct blk_driver *cur_drvr;
292 const char *if_typename;
294 cur_drvr = blk_driver_lookup_type(if_type);
298 if_typename = cur_drvr->if_typename;
299 printf("Scanning disks on %s...\n", if_typename);
300 for (i = 0; i < 4; i++) {
301 struct blk_desc *desc;
302 char devname[32] = { 0 }; /* dp->str is u16[32] long */
304 desc = blk_get_devnum_by_type(if_type, i);
307 if (desc->type == DEV_TYPE_UNKNOWN)
310 snprintf(devname, sizeof(devname), "%s%d",
312 efi_disk_add_dev(devname, if_typename, desc, i, 0);
316 * El Torito images show up as block devices
317 * in an EFI world, so let's create them here
319 disks += efi_disk_create_eltorito(desc, if_typename,
324 printf("Found %d disks\n", disks);