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 */
36 static efi_status_t
efi_disk_open_block(void *handle
, efi_guid_t
*protocol
,
37 void **protocol_interface
, void *agent_handle
,
38 void *controller_handle
, uint32_t attributes
)
40 struct efi_disk_obj
*diskobj
= handle
;
42 *protocol_interface
= &diskobj
->ops
;
47 static efi_status_t
efi_disk_open_dp(void *handle
, efi_guid_t
*protocol
,
48 void **protocol_interface
, void *agent_handle
,
49 void *controller_handle
, uint32_t attributes
)
51 struct efi_disk_obj
*diskobj
= handle
;
53 *protocol_interface
= diskobj
->dp
;
58 static efi_status_t EFIAPI
efi_disk_reset(struct efi_block_io
*this,
59 char extended_verification
)
61 EFI_ENTRY("%p, %x", this, extended_verification
);
62 return EFI_EXIT(EFI_DEVICE_ERROR
);
65 enum efi_disk_direction
{
70 static efi_status_t EFIAPI
efi_disk_rw_blocks(struct efi_block_io
*this,
71 u32 media_id
, u64 lba
, unsigned long buffer_size
,
72 void *buffer
, enum efi_disk_direction direction
)
74 struct efi_disk_obj
*diskobj
;
75 struct blk_desc
*desc
;
80 diskobj
= container_of(this, struct efi_disk_obj
, ops
);
81 if (!(desc
= blk_get_dev(diskobj
->ifname
, diskobj
->dev_index
)))
82 return EFI_EXIT(EFI_DEVICE_ERROR
);
84 blocks
= buffer_size
/ blksz
;
85 lba
+= diskobj
->offset
;
88 printf("EFI: %s:%d blocks=%x lba=%"PRIx64
" blksz=%x dir=%d\n", __func__
,
89 __LINE__
, blocks
, lba
, blksz
, direction
);
92 /* We only support full block access */
93 if (buffer_size
& (blksz
- 1))
94 return EFI_EXIT(EFI_DEVICE_ERROR
);
96 if (direction
== EFI_DISK_READ
)
97 n
= blk_dread(desc
, lba
, blocks
, buffer
);
99 n
= blk_dwrite(desc
, lba
, blocks
, buffer
);
101 /* We don't do interrupts, so check for timers cooperatively */
105 printf("EFI: %s:%d n=%lx blocks=%x\n", __func__
, __LINE__
, n
, blocks
);
108 return EFI_EXIT(EFI_DEVICE_ERROR
);
110 return EFI_EXIT(EFI_SUCCESS
);
113 static efi_status_t
efi_disk_read_blocks(struct efi_block_io
*this,
114 u32 media_id
, u64 lba
, unsigned long buffer_size
,
117 void *real_buffer
= buffer
;
120 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
121 if (buffer_size
> EFI_LOADER_BOUNCE_BUFFER_SIZE
) {
122 r
= efi_disk_read_blocks(this, media_id
, lba
,
123 EFI_LOADER_BOUNCE_BUFFER_SIZE
, buffer
);
124 if (r
!= EFI_SUCCESS
)
126 return efi_disk_read_blocks(this, media_id
, lba
+
127 EFI_LOADER_BOUNCE_BUFFER_SIZE
/ this->media
->block_size
,
128 buffer_size
- EFI_LOADER_BOUNCE_BUFFER_SIZE
,
129 buffer
+ EFI_LOADER_BOUNCE_BUFFER_SIZE
);
132 real_buffer
= efi_bounce_buffer
;
135 EFI_ENTRY("%p, %x, %"PRIx64
", %lx, %p", this, media_id
, lba
,
136 buffer_size
, buffer
);
138 r
= efi_disk_rw_blocks(this, media_id
, lba
, buffer_size
, real_buffer
,
141 /* Copy from bounce buffer to real buffer if necessary */
142 if ((r
== EFI_SUCCESS
) && (real_buffer
!= buffer
))
143 memcpy(buffer
, real_buffer
, buffer_size
);
148 static efi_status_t
efi_disk_write_blocks(struct efi_block_io
*this,
149 u32 media_id
, u64 lba
, unsigned long buffer_size
,
152 void *real_buffer
= buffer
;
155 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
156 if (buffer_size
> EFI_LOADER_BOUNCE_BUFFER_SIZE
) {
157 r
= efi_disk_write_blocks(this, media_id
, lba
,
158 EFI_LOADER_BOUNCE_BUFFER_SIZE
, buffer
);
159 if (r
!= EFI_SUCCESS
)
161 return efi_disk_write_blocks(this, media_id
, lba
+
162 EFI_LOADER_BOUNCE_BUFFER_SIZE
/ this->media
->block_size
,
163 buffer_size
- EFI_LOADER_BOUNCE_BUFFER_SIZE
,
164 buffer
+ EFI_LOADER_BOUNCE_BUFFER_SIZE
);
167 real_buffer
= efi_bounce_buffer
;
170 EFI_ENTRY("%p, %x, %"PRIx64
", %lx, %p", this, media_id
, lba
,
171 buffer_size
, buffer
);
173 /* Populate bounce buffer if necessary */
174 if (real_buffer
!= buffer
)
175 memcpy(real_buffer
, buffer
, buffer_size
);
177 r
= efi_disk_rw_blocks(this, media_id
, lba
, buffer_size
, real_buffer
,
183 static efi_status_t EFIAPI
efi_disk_flush_blocks(struct efi_block_io
*this)
185 /* We always write synchronously */
186 EFI_ENTRY("%p", this);
187 return EFI_EXIT(EFI_SUCCESS
);
190 static const struct efi_block_io block_io_disk_template
= {
191 .reset
= &efi_disk_reset
,
192 .read_blocks
= &efi_disk_read_blocks
,
193 .write_blocks
= &efi_disk_write_blocks
,
194 .flush_blocks
= &efi_disk_flush_blocks
,
197 static void efi_disk_add_dev(const char *name
,
198 const char *if_typename
,
199 const struct blk_desc
*desc
,
203 struct efi_disk_obj
*diskobj
;
204 struct efi_device_path_file_path
*dp
;
205 int objlen
= sizeof(*diskobj
) + (sizeof(*dp
) * 2);
207 diskobj
= calloc(1, objlen
);
209 /* Fill in object data */
210 diskobj
->parent
.protocols
[0].guid
= &efi_block_io_guid
;
211 diskobj
->parent
.protocols
[0].open
= efi_disk_open_block
;
212 diskobj
->parent
.protocols
[1].guid
= &efi_guid_device_path
;
213 diskobj
->parent
.protocols
[1].open
= efi_disk_open_dp
;
214 diskobj
->parent
.handle
= diskobj
;
215 diskobj
->ops
= block_io_disk_template
;
216 diskobj
->ifname
= if_typename
;
217 diskobj
->dev_index
= dev_index
;
218 diskobj
->offset
= offset
;
220 /* Fill in EFI IO Media info (for read/write callbacks) */
221 diskobj
->media
.removable_media
= desc
->removable
;
222 diskobj
->media
.media_present
= 1;
223 diskobj
->media
.block_size
= desc
->blksz
;
224 diskobj
->media
.io_align
= desc
->blksz
;
225 diskobj
->media
.last_block
= desc
->lba
;
226 diskobj
->ops
.media
= &diskobj
->media
;
228 /* Fill in device path */
229 dp
= (void*)&diskobj
[1];
231 dp
[0].dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
232 dp
[0].dp
.sub_type
= DEVICE_PATH_SUB_TYPE_FILE_PATH
;
233 dp
[0].dp
.length
= sizeof(*dp
);
234 ascii2unicode(dp
[0].str
, name
);
236 dp
[1].dp
.type
= DEVICE_PATH_TYPE_END
;
237 dp
[1].dp
.sub_type
= DEVICE_PATH_SUB_TYPE_END
;
238 dp
[1].dp
.length
= sizeof(*dp
);
240 /* Hook up to the device list */
241 list_add_tail(&diskobj
->parent
.link
, &efi_obj_list
);
244 static int efi_disk_create_eltorito(struct blk_desc
*desc
,
245 const char *if_typename
,
249 #ifdef CONFIG_ISO_PARTITION
250 char devname
[32] = { 0 }; /* dp->str is u16[32] long */
251 disk_partition_t info
;
254 if (desc
->part_type
!= PART_TYPE_ISO
)
257 while (!part_get_info(desc
, part
, &info
)) {
258 snprintf(devname
, sizeof(devname
), "%s%d:%d", if_typename
,
260 efi_disk_add_dev(devname
, if_typename
, desc
, diskid
,
271 * U-Boot doesn't have a list of all online disk devices. So when running our
272 * EFI payload, we scan through all of the potentially available ones and
273 * store them in our object pool.
275 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
276 * Consider converting the code to look up devices as needed. The EFI device
277 * could be a child of the UCLASS_BLK block device, perhaps.
279 * This gets called from do_bootefi_exec().
281 int efi_disk_register(void)
287 for (uclass_first_device(UCLASS_BLK
, &dev
);
289 uclass_next_device(&dev
)) {
290 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
291 const char *if_typename
= dev
->driver
->name
;
293 printf("Scanning disk %s...\n", dev
->name
);
294 efi_disk_add_dev(dev
->name
, if_typename
, desc
, desc
->devnum
, 0);
298 * El Torito images show up as block devices in an EFI world,
299 * so let's create them here
301 disks
+= efi_disk_create_eltorito(desc
, if_typename
,
307 /* Search for all available disk devices */
308 for (if_type
= 0; if_type
< IF_TYPE_COUNT
; if_type
++) {
309 const struct blk_driver
*cur_drvr
;
310 const char *if_typename
;
312 cur_drvr
= blk_driver_lookup_type(if_type
);
316 if_typename
= cur_drvr
->if_typename
;
317 printf("Scanning disks on %s...\n", if_typename
);
318 for (i
= 0; i
< 4; i
++) {
319 struct blk_desc
*desc
;
320 char devname
[32] = { 0 }; /* dp->str is u16[32] long */
322 desc
= blk_get_devnum_by_type(if_type
, i
);
325 if (desc
->type
== DEV_TYPE_UNKNOWN
)
328 snprintf(devname
, sizeof(devname
), "%s%d",
330 efi_disk_add_dev(devname
, if_typename
, desc
, i
, 0);
334 * El Torito images show up as block devices
335 * in an EFI world, so let's create them here
337 disks
+= efi_disk_create_eltorito(desc
, if_typename
, i
);
341 printf("Found %d disks\n", disks
);