blob: c7d4515321ea431b3ccc5184d76da6d3494f0ba8 [file] [log] [blame]
Alexander Graf2a22d052016-03-04 01:10:02 +01001/*
2 * EFI application disk support
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
Simon Glass6dd9faf2016-05-01 13:52:32 -060010#include <blk.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010011#include <efi_loader.h>
12#include <inttypes.h>
13#include <part.h>
14#include <malloc.h>
15
16static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
17
18struct efi_disk_obj {
19 /* Generic EFI object parent class data */
20 struct efi_object parent;
21 /* EFI Interface callback struct for block I/O */
22 struct efi_block_io ops;
23 /* U-Boot ifname for block device */
24 const char *ifname;
25 /* U-Boot dev_index for block device */
26 int dev_index;
27 /* EFI Interface Media descriptor struct, referenced by ops */
28 struct efi_block_io_media media;
29 /* EFI device path to this block device */
30 struct efi_device_path_file_path *dp;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020031 /* Offset into disk for simple partitions */
32 lbaint_t offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010033};
34
Alexander Graf2a22d052016-03-04 01:10:02 +010035static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
36 void **protocol_interface, void *agent_handle,
37 void *controller_handle, uint32_t attributes)
38{
39 struct efi_disk_obj *diskobj = handle;
40
41 *protocol_interface = &diskobj->ops;
42
43 return EFI_SUCCESS;
44}
45
46static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
47 void **protocol_interface, void *agent_handle,
48 void *controller_handle, uint32_t attributes)
49{
50 struct efi_disk_obj *diskobj = handle;
51
52 *protocol_interface = diskobj->dp;
53
54 return EFI_SUCCESS;
55}
56
57static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
58 char extended_verification)
59{
60 EFI_ENTRY("%p, %x", this, extended_verification);
61 return EFI_EXIT(EFI_DEVICE_ERROR);
62}
63
64enum efi_disk_direction {
65 EFI_DISK_READ,
66 EFI_DISK_WRITE,
67};
68
69static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
70 u32 media_id, u64 lba, unsigned long buffer_size,
71 void *buffer, enum efi_disk_direction direction)
72{
73 struct efi_disk_obj *diskobj;
74 struct blk_desc *desc;
75 int blksz;
76 int blocks;
77 unsigned long n;
78
Alexander Graf2a22d052016-03-04 01:10:02 +010079 diskobj = container_of(this, struct efi_disk_obj, ops);
80 if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index)))
81 return EFI_EXIT(EFI_DEVICE_ERROR);
82 blksz = desc->blksz;
83 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020084 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010085
86#ifdef DEBUG_EFI
87 printf("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
88 __LINE__, blocks, lba, blksz, direction);
89#endif
90
91 /* We only support full block access */
92 if (buffer_size & (blksz - 1))
93 return EFI_EXIT(EFI_DEVICE_ERROR);
94
Alexander Graf51735ae2016-05-11 18:25:48 +020095 if (direction == EFI_DISK_READ) {
Alexander Graf2a22d052016-03-04 01:10:02 +010096 n = desc->block_read(desc, lba, blocks, buffer);
Alexander Graf51735ae2016-05-11 18:25:48 +020097 } else {
Alexander Graf2a22d052016-03-04 01:10:02 +010098 n = desc->block_write(desc, lba, blocks, buffer);
Alexander Graf51735ae2016-05-11 18:25:48 +020099 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100100
101 /* We don't do interrupts, so check for timers cooperatively */
102 efi_timer_check();
103
104#ifdef DEBUG_EFI
105 printf("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
106#endif
107 if (n != blocks)
108 return EFI_EXIT(EFI_DEVICE_ERROR);
109
110 return EFI_EXIT(EFI_SUCCESS);
111}
112
113static efi_status_t efi_disk_read_blocks(struct efi_block_io *this,
114 u32 media_id, u64 lba, unsigned long buffer_size,
115 void *buffer)
116{
Alexander Graf51735ae2016-05-11 18:25:48 +0200117 void *real_buffer = buffer;
118 efi_status_t r;
119
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)
125 return r;
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);
130 }
131
132 real_buffer = efi_bounce_buffer;
133#endif
134
135 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
136 buffer_size, buffer);
137
138 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
139 EFI_DISK_READ);
140
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);
144
145 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100146}
147
148static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
149 u32 media_id, u64 lba, unsigned long buffer_size,
150 void *buffer)
151{
Alexander Graf51735ae2016-05-11 18:25:48 +0200152 void *real_buffer = buffer;
153 efi_status_t r;
154
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)
160 return r;
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);
165 }
166
167 real_buffer = efi_bounce_buffer;
168#endif
169
170 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
171 buffer_size, buffer);
172
173 /* Populate bounce buffer if necessary */
174 if (real_buffer != buffer)
175 memcpy(real_buffer, buffer, buffer_size);
176
177 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
178 EFI_DISK_WRITE);
179
180 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100181}
182
183static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
184{
185 /* We always write synchronously */
186 EFI_ENTRY("%p", this);
187 return EFI_EXIT(EFI_SUCCESS);
188}
189
190static 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,
195};
196
Alexander Graf4a12a972016-04-11 16:16:17 +0200197static void efi_disk_add_dev(char *name,
Simon Glass6dd9faf2016-05-01 13:52:32 -0600198 const struct blk_driver *cur_drvr,
Alexander Graf4a12a972016-04-11 16:16:17 +0200199 const struct blk_desc *desc,
200 int dev_index,
201 lbaint_t offset)
202{
203 struct efi_disk_obj *diskobj;
204 struct efi_device_path_file_path *dp;
205 int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
206
207 diskobj = calloc(1, objlen);
208
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;
Simon Glass6dd9faf2016-05-01 13:52:32 -0600216 diskobj->ifname = cur_drvr->if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200217 diskobj->dev_index = dev_index;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200218 diskobj->offset = offset;
Alexander Graf4a12a972016-04-11 16:16:17 +0200219
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;
227
228 /* Fill in device path */
229 dp = (void*)&diskobj[1];
230 diskobj->dp = dp;
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);
235
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);
239
240 /* Hook up to the device list */
241 list_add_tail(&diskobj->parent.link, &efi_obj_list);
242}
243
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200244static int efi_disk_create_eltorito(struct blk_desc *desc,
Simon Glass6dd9faf2016-05-01 13:52:32 -0600245 const struct blk_driver *cur_drvr,
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200246 int diskid)
247{
248 int disks = 0;
249#ifdef CONFIG_ISO_PARTITION
Alexander Grafecbe1a02016-04-11 16:16:20 +0200250 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200251 disk_partition_t info;
252 int part = 1;
253
254 if (desc->part_type != PART_TYPE_ISO)
255 return 0;
256
257 while (!part_get_info(desc, part, &info)) {
Simon Glass6dd9faf2016-05-01 13:52:32 -0600258 snprintf(devname, sizeof(devname), "%s%d:%d",
259 cur_drvr->if_typename, diskid, part);
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200260 efi_disk_add_dev(devname, cur_drvr, desc, diskid, info.start);
261 part++;
262 disks++;
263 }
264#endif
265
266 return disks;
267}
268
Alexander Graf2a22d052016-03-04 01:10:02 +0100269/*
270 * U-Boot doesn't have a list of all online disk devices. So when running our
271 * EFI payload, we scan through all of the potentially available ones and
272 * store them in our object pool.
273 *
274 * This gets called from do_bootefi_exec().
275 */
276int efi_disk_register(void)
277{
Simon Glass6dd9faf2016-05-01 13:52:32 -0600278 const struct blk_driver *cur_drvr;
279 int i, if_type;
Alexander Graf2a22d052016-03-04 01:10:02 +0100280 int disks = 0;
281
282 /* Search for all available disk devices */
Simon Glass6dd9faf2016-05-01 13:52:32 -0600283 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
284 cur_drvr = blk_driver_lookup_type(if_type);
285 if (!cur_drvr)
286 continue;
287
288 printf("Scanning disks on %s...\n", cur_drvr->if_typename);
Alexander Graf2a22d052016-03-04 01:10:02 +0100289 for (i = 0; i < 4; i++) {
290 struct blk_desc *desc;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200291 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf2a22d052016-03-04 01:10:02 +0100292
Simon Glass6dd9faf2016-05-01 13:52:32 -0600293 desc = blk_get_devnum_by_type(if_type, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100294 if (!desc)
295 continue;
296 if (desc->type == DEV_TYPE_UNKNOWN)
297 continue;
298
Alexander Graf2a22d052016-03-04 01:10:02 +0100299 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass6dd9faf2016-05-01 13:52:32 -0600300 cur_drvr->if_typename, i);
Alexander Graf4a12a972016-04-11 16:16:17 +0200301 efi_disk_add_dev(devname, cur_drvr, desc, i, 0);
Alexander Graf2a22d052016-03-04 01:10:02 +0100302 disks++;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200303
304 /*
305 * El Torito images show up as block devices
306 * in an EFI world, so let's create them here
307 */
308 disks += efi_disk_create_eltorito(desc, cur_drvr, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100309 }
310 }
311 printf("Found %d disks\n", disks);
312
313 return 0;
314}