blob: d8ddcc9b423f0feecd0a7503a481776234b46b43 [file] [log] [blame]
Alexander Grafe83be452016-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 Glasse3dfa912016-05-01 13:52:32 -060010#include <blk.h>
Simon Glass302d4f82016-05-14 14:03:05 -060011#include <dm.h>
Alexander Grafe83be452016-03-04 01:10:02 +010012#include <efi_loader.h>
13#include <inttypes.h>
14#include <part.h>
15#include <malloc.h>
16
17static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
18
19struct efi_disk_obj {
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 */
25 const char *ifname;
26 /* U-Boot dev_index for block device */
27 int dev_index;
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;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020032 /* Offset into disk for simple partitions */
33 lbaint_t offset;
Alexander Graf34118e72016-08-05 14:49:53 +020034 /* Internal block device */
35 const struct blk_desc *desc;
Alexander Grafe83be452016-03-04 01:10:02 +010036};
37
Alexander Grafe83be452016-03-04 01:10:02 +010038static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
39 void **protocol_interface, void *agent_handle,
40 void *controller_handle, uint32_t attributes)
41{
42 struct efi_disk_obj *diskobj = handle;
43
44 *protocol_interface = &diskobj->ops;
45
46 return EFI_SUCCESS;
47}
48
49static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
50 void **protocol_interface, void *agent_handle,
51 void *controller_handle, uint32_t attributes)
52{
53 struct efi_disk_obj *diskobj = handle;
54
55 *protocol_interface = diskobj->dp;
56
57 return EFI_SUCCESS;
58}
59
60static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
61 char extended_verification)
62{
63 EFI_ENTRY("%p, %x", this, extended_verification);
64 return EFI_EXIT(EFI_DEVICE_ERROR);
65}
66
67enum efi_disk_direction {
68 EFI_DISK_READ,
69 EFI_DISK_WRITE,
70};
71
72static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
73 u32 media_id, u64 lba, unsigned long buffer_size,
74 void *buffer, enum efi_disk_direction direction)
75{
76 struct efi_disk_obj *diskobj;
77 struct blk_desc *desc;
78 int blksz;
79 int blocks;
80 unsigned long n;
81
Alexander Grafe83be452016-03-04 01:10:02 +010082 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graf34118e72016-08-05 14:49:53 +020083 desc = (struct blk_desc *) diskobj->desc;
Alexander Grafe83be452016-03-04 01:10:02 +010084 blksz = desc->blksz;
85 blocks = buffer_size / blksz;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020086 lba += diskobj->offset;
Alexander Grafe83be452016-03-04 01:10:02 +010087
Alexander Graf62a67482016-06-02 11:38:27 +020088 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
89 __LINE__, blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +010090
91 /* We only support full block access */
92 if (buffer_size & (blksz - 1))
93 return EFI_EXIT(EFI_DEVICE_ERROR);
94
95 if (direction == EFI_DISK_READ)
Simon Glass302d4f82016-05-14 14:03:05 -060096 n = blk_dread(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010097 else
Simon Glass302d4f82016-05-14 14:03:05 -060098 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010099
100 /* We don't do interrupts, so check for timers cooperatively */
101 efi_timer_check();
102
Alexander Graf62a67482016-06-02 11:38:27 +0200103 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
104
Alexander Grafe83be452016-03-04 01:10:02 +0100105 if (n != blocks)
106 return EFI_EXIT(EFI_DEVICE_ERROR);
107
108 return EFI_EXIT(EFI_SUCCESS);
109}
110
111static efi_status_t efi_disk_read_blocks(struct efi_block_io *this,
112 u32 media_id, u64 lba, unsigned long buffer_size,
113 void *buffer)
114{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200115 void *real_buffer = buffer;
116 efi_status_t r;
117
118#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
119 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
120 r = efi_disk_read_blocks(this, media_id, lba,
121 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
122 if (r != EFI_SUCCESS)
123 return r;
124 return efi_disk_read_blocks(this, media_id, lba +
125 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
126 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
127 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
128 }
129
130 real_buffer = efi_bounce_buffer;
131#endif
132
133 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
134 buffer_size, buffer);
135
136 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
137 EFI_DISK_READ);
138
139 /* Copy from bounce buffer to real buffer if necessary */
140 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
141 memcpy(buffer, real_buffer, buffer_size);
142
143 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100144}
145
146static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
147 u32 media_id, u64 lba, unsigned long buffer_size,
148 void *buffer)
149{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200150 void *real_buffer = buffer;
151 efi_status_t r;
152
153#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
154 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
155 r = efi_disk_write_blocks(this, media_id, lba,
156 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
157 if (r != EFI_SUCCESS)
158 return r;
159 return efi_disk_write_blocks(this, media_id, lba +
160 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
161 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
162 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
163 }
164
165 real_buffer = efi_bounce_buffer;
166#endif
167
168 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
169 buffer_size, buffer);
170
171 /* Populate bounce buffer if necessary */
172 if (real_buffer != buffer)
173 memcpy(real_buffer, buffer, buffer_size);
174
175 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
176 EFI_DISK_WRITE);
177
178 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100179}
180
181static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
182{
183 /* We always write synchronously */
184 EFI_ENTRY("%p", this);
185 return EFI_EXIT(EFI_SUCCESS);
186}
187
188static const struct efi_block_io block_io_disk_template = {
189 .reset = &efi_disk_reset,
190 .read_blocks = &efi_disk_read_blocks,
191 .write_blocks = &efi_disk_write_blocks,
192 .flush_blocks = &efi_disk_flush_blocks,
193};
194
Simon Glass302d4f82016-05-14 14:03:05 -0600195static void efi_disk_add_dev(const char *name,
196 const char *if_typename,
Alexander Graf06fe57b2016-04-11 16:16:17 +0200197 const struct blk_desc *desc,
198 int dev_index,
199 lbaint_t offset)
200{
201 struct efi_disk_obj *diskobj;
202 struct efi_device_path_file_path *dp;
203 int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
204
Alexander Graf601db1f2016-08-05 14:51:47 +0200205 /* Don't add empty devices */
206 if (!desc->lba)
207 return;
208
Alexander Graf06fe57b2016-04-11 16:16:17 +0200209 diskobj = calloc(1, objlen);
210
211 /* Fill in object data */
212 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
213 diskobj->parent.protocols[0].open = efi_disk_open_block;
214 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
215 diskobj->parent.protocols[1].open = efi_disk_open_dp;
216 diskobj->parent.handle = diskobj;
217 diskobj->ops = block_io_disk_template;
Simon Glass302d4f82016-05-14 14:03:05 -0600218 diskobj->ifname = if_typename;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200219 diskobj->dev_index = dev_index;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200220 diskobj->offset = offset;
Alexander Graf34118e72016-08-05 14:49:53 +0200221 diskobj->desc = desc;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200222
223 /* Fill in EFI IO Media info (for read/write callbacks) */
224 diskobj->media.removable_media = desc->removable;
225 diskobj->media.media_present = 1;
226 diskobj->media.block_size = desc->blksz;
227 diskobj->media.io_align = desc->blksz;
Alexander Graf601db1f2016-08-05 14:51:47 +0200228 diskobj->media.last_block = desc->lba - offset;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200229 diskobj->ops.media = &diskobj->media;
230
231 /* Fill in device path */
232 dp = (void*)&diskobj[1];
233 diskobj->dp = dp;
234 dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
235 dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
236 dp[0].dp.length = sizeof(*dp);
237 ascii2unicode(dp[0].str, name);
238
239 dp[1].dp.type = DEVICE_PATH_TYPE_END;
240 dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
241 dp[1].dp.length = sizeof(*dp);
242
243 /* Hook up to the device list */
244 list_add_tail(&diskobj->parent.link, &efi_obj_list);
245}
246
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200247static int efi_disk_create_eltorito(struct blk_desc *desc,
Simon Glass302d4f82016-05-14 14:03:05 -0600248 const char *if_typename,
Alexander Graf34118e72016-08-05 14:49:53 +0200249 int diskid,
250 const char *pdevname)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200251{
252 int disks = 0;
253#ifdef CONFIG_ISO_PARTITION
Alexander Graf3491ee72016-04-11 16:16:20 +0200254 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200255 disk_partition_t info;
256 int part = 1;
257
258 if (desc->part_type != PART_TYPE_ISO)
259 return 0;
260
261 while (!part_get_info(desc, part, &info)) {
Alexander Graf34118e72016-08-05 14:49:53 +0200262 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
263 part);
Simon Glass302d4f82016-05-14 14:03:05 -0600264 efi_disk_add_dev(devname, if_typename, desc, diskid,
265 info.start);
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200266 part++;
267 disks++;
268 }
269#endif
270
271 return disks;
272}
273
Alexander Grafe83be452016-03-04 01:10:02 +0100274/*
275 * U-Boot doesn't have a list of all online disk devices. So when running our
276 * EFI payload, we scan through all of the potentially available ones and
277 * store them in our object pool.
278 *
Simon Glass302d4f82016-05-14 14:03:05 -0600279 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
280 * Consider converting the code to look up devices as needed. The EFI device
281 * could be a child of the UCLASS_BLK block device, perhaps.
282 *
Alexander Grafe83be452016-03-04 01:10:02 +0100283 * This gets called from do_bootefi_exec().
284 */
285int efi_disk_register(void)
286{
Alexander Grafe83be452016-03-04 01:10:02 +0100287 int disks = 0;
Simon Glass302d4f82016-05-14 14:03:05 -0600288#ifdef CONFIG_BLK
289 struct udevice *dev;
290
291 for (uclass_first_device(UCLASS_BLK, &dev);
292 dev;
293 uclass_next_device(&dev)) {
294 struct blk_desc *desc = dev_get_uclass_platdata(dev);
295 const char *if_typename = dev->driver->name;
296
297 printf("Scanning disk %s...\n", dev->name);
298 efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
299 disks++;
300
301 /*
302 * El Torito images show up as block devices in an EFI world,
303 * so let's create them here
304 */
305 disks += efi_disk_create_eltorito(desc, if_typename,
Alexander Graf34118e72016-08-05 14:49:53 +0200306 desc->devnum, dev->name);
Simon Glass302d4f82016-05-14 14:03:05 -0600307 }
308#else
309 int i, if_type;
Alexander Grafe83be452016-03-04 01:10:02 +0100310
311 /* Search for all available disk devices */
Simon Glasse3dfa912016-05-01 13:52:32 -0600312 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass302d4f82016-05-14 14:03:05 -0600313 const struct blk_driver *cur_drvr;
314 const char *if_typename;
315
Simon Glasse3dfa912016-05-01 13:52:32 -0600316 cur_drvr = blk_driver_lookup_type(if_type);
317 if (!cur_drvr)
318 continue;
319
Simon Glass302d4f82016-05-14 14:03:05 -0600320 if_typename = cur_drvr->if_typename;
321 printf("Scanning disks on %s...\n", if_typename);
Alexander Grafe83be452016-03-04 01:10:02 +0100322 for (i = 0; i < 4; i++) {
323 struct blk_desc *desc;
Alexander Graf3491ee72016-04-11 16:16:20 +0200324 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafe83be452016-03-04 01:10:02 +0100325
Simon Glasse3dfa912016-05-01 13:52:32 -0600326 desc = blk_get_devnum_by_type(if_type, i);
Alexander Grafe83be452016-03-04 01:10:02 +0100327 if (!desc)
328 continue;
329 if (desc->type == DEV_TYPE_UNKNOWN)
330 continue;
331
Alexander Grafe83be452016-03-04 01:10:02 +0100332 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass302d4f82016-05-14 14:03:05 -0600333 if_typename, i);
334 efi_disk_add_dev(devname, if_typename, desc, i, 0);
Alexander Grafe83be452016-03-04 01:10:02 +0100335 disks++;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200336
337 /*
338 * El Torito images show up as block devices
339 * in an EFI world, so let's create them here
340 */
Alexander Graf34118e72016-08-05 14:49:53 +0200341 disks += efi_disk_create_eltorito(desc, if_typename,
342 i, devname);
Alexander Grafe83be452016-03-04 01:10:02 +0100343 }
344 }
Simon Glass302d4f82016-05-14 14:03:05 -0600345#endif
Alexander Grafe83be452016-03-04 01:10:02 +0100346 printf("Found %d disks\n", disks);
347
348 return 0;
349}