blob: c815711e5758fa5f2592d7c37437924e87eb657b [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>
Simon Glass487d7562016-05-14 14:03:05 -060011#include <dm.h>
Alexander Graf2a22d052016-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 */
Rob Clark884bcf62017-09-13 18:05:31 -040031 struct efi_device_path *dp;
32 /* partition # */
33 unsigned int part;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020034 /* Offset into disk for simple partitions */
35 lbaint_t offset;
Alexander Graff9d334b2016-08-05 14:49:53 +020036 /* Internal block device */
Rob Clark884bcf62017-09-13 18:05:31 -040037 struct blk_desc *desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010038};
39
Alexander Graf2a22d052016-03-04 01:10:02 +010040static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
41 char extended_verification)
42{
43 EFI_ENTRY("%p, %x", this, extended_verification);
44 return EFI_EXIT(EFI_DEVICE_ERROR);
45}
46
47enum efi_disk_direction {
48 EFI_DISK_READ,
49 EFI_DISK_WRITE,
50};
51
Heinrich Schuchardta80a2322017-08-26 22:33:13 +020052static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010053 u32 media_id, u64 lba, unsigned long buffer_size,
54 void *buffer, enum efi_disk_direction direction)
55{
56 struct efi_disk_obj *diskobj;
57 struct blk_desc *desc;
58 int blksz;
59 int blocks;
60 unsigned long n;
61
Alexander Graf2a22d052016-03-04 01:10:02 +010062 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graff9d334b2016-08-05 14:49:53 +020063 desc = (struct blk_desc *) diskobj->desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010064 blksz = desc->blksz;
65 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020066 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010067
Alexander Grafedcef3b2016-06-02 11:38:27 +020068 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
69 __LINE__, blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010070
71 /* We only support full block access */
72 if (buffer_size & (blksz - 1))
Rob Clark33049902017-07-25 20:28:29 -040073 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +010074
75 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -060076 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010077 else
Simon Glass487d7562016-05-14 14:03:05 -060078 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010079
80 /* We don't do interrupts, so check for timers cooperatively */
81 efi_timer_check();
82
Alexander Grafedcef3b2016-06-02 11:38:27 +020083 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
84
Alexander Graf2a22d052016-03-04 01:10:02 +010085 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -040086 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +010087
Rob Clark33049902017-07-25 20:28:29 -040088 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +010089}
90
Simon Glasse2754582016-09-25 15:27:32 -060091static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010092 u32 media_id, u64 lba, unsigned long buffer_size,
93 void *buffer)
94{
Alexander Graf51735ae2016-05-11 18:25:48 +020095 void *real_buffer = buffer;
96 efi_status_t r;
97
98#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
99 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
100 r = efi_disk_read_blocks(this, media_id, lba,
101 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
102 if (r != EFI_SUCCESS)
103 return r;
104 return efi_disk_read_blocks(this, media_id, lba +
105 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
106 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
107 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
108 }
109
110 real_buffer = efi_bounce_buffer;
111#endif
112
113 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
114 buffer_size, buffer);
115
116 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
117 EFI_DISK_READ);
118
119 /* Copy from bounce buffer to real buffer if necessary */
120 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
121 memcpy(buffer, real_buffer, buffer_size);
122
123 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100124}
125
Simon Glasse2754582016-09-25 15:27:32 -0600126static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +0100127 u32 media_id, u64 lba, unsigned long buffer_size,
128 void *buffer)
129{
Alexander Graf51735ae2016-05-11 18:25:48 +0200130 void *real_buffer = buffer;
131 efi_status_t r;
132
133#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
134 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
135 r = efi_disk_write_blocks(this, media_id, lba,
136 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
137 if (r != EFI_SUCCESS)
138 return r;
139 return efi_disk_write_blocks(this, media_id, lba +
140 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
141 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
142 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
143 }
144
145 real_buffer = efi_bounce_buffer;
146#endif
147
148 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
149 buffer_size, buffer);
150
151 /* Populate bounce buffer if necessary */
152 if (real_buffer != buffer)
153 memcpy(real_buffer, buffer, buffer_size);
154
155 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
156 EFI_DISK_WRITE);
157
158 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100159}
160
161static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
162{
163 /* We always write synchronously */
164 EFI_ENTRY("%p", this);
165 return EFI_EXIT(EFI_SUCCESS);
166}
167
168static const struct efi_block_io block_io_disk_template = {
169 .reset = &efi_disk_reset,
170 .read_blocks = &efi_disk_read_blocks,
171 .write_blocks = &efi_disk_write_blocks,
172 .flush_blocks = &efi_disk_flush_blocks,
173};
174
Simon Glass487d7562016-05-14 14:03:05 -0600175static void efi_disk_add_dev(const char *name,
176 const char *if_typename,
Rob Clark884bcf62017-09-13 18:05:31 -0400177 struct blk_desc *desc,
Alexander Graf4a12a972016-04-11 16:16:17 +0200178 int dev_index,
Rob Clark884bcf62017-09-13 18:05:31 -0400179 lbaint_t offset,
180 unsigned int part)
Alexander Graf4a12a972016-04-11 16:16:17 +0200181{
182 struct efi_disk_obj *diskobj;
Alexander Graf4a12a972016-04-11 16:16:17 +0200183
Alexander Graf0812d1a2016-08-05 14:51:47 +0200184 /* Don't add empty devices */
185 if (!desc->lba)
186 return;
187
Rob Clark884bcf62017-09-13 18:05:31 -0400188 diskobj = calloc(1, sizeof(*diskobj));
Alexander Graf4a12a972016-04-11 16:16:17 +0200189
190 /* Fill in object data */
Rob Clark884bcf62017-09-13 18:05:31 -0400191 diskobj->dp = efi_dp_from_part(desc, part);
192 diskobj->part = part;
Alexander Graf4a12a972016-04-11 16:16:17 +0200193 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200194 diskobj->parent.protocols[0].protocol_interface = &diskobj->ops;
Alexander Graf4a12a972016-04-11 16:16:17 +0200195 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
Rob Clark884bcf62017-09-13 18:05:31 -0400196 diskobj->parent.protocols[1].protocol_interface = diskobj->dp;
Alexander Graf4a12a972016-04-11 16:16:17 +0200197 diskobj->parent.handle = diskobj;
198 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600199 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200200 diskobj->dev_index = dev_index;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200201 diskobj->offset = offset;
Alexander Graff9d334b2016-08-05 14:49:53 +0200202 diskobj->desc = desc;
Alexander Graf4a12a972016-04-11 16:16:17 +0200203
204 /* Fill in EFI IO Media info (for read/write callbacks) */
205 diskobj->media.removable_media = desc->removable;
206 diskobj->media.media_present = 1;
207 diskobj->media.block_size = desc->blksz;
208 diskobj->media.io_align = desc->blksz;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200209 diskobj->media.last_block = desc->lba - offset;
Alexander Graf4a12a972016-04-11 16:16:17 +0200210 diskobj->ops.media = &diskobj->media;
211
Alexander Graf4a12a972016-04-11 16:16:17 +0200212 /* Hook up to the device list */
213 list_add_tail(&diskobj->parent.link, &efi_obj_list);
214}
215
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200216static int efi_disk_create_eltorito(struct blk_desc *desc,
Simon Glass487d7562016-05-14 14:03:05 -0600217 const char *if_typename,
Alexander Graff9d334b2016-08-05 14:49:53 +0200218 int diskid,
219 const char *pdevname)
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200220{
221 int disks = 0;
Patrick Delaunay1acc0082017-01-27 11:00:38 +0100222#if CONFIG_IS_ENABLED(ISO_PARTITION)
Alexander Grafecbe1a02016-04-11 16:16:20 +0200223 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200224 disk_partition_t info;
225 int part = 1;
226
227 if (desc->part_type != PART_TYPE_ISO)
228 return 0;
229
Rob Clark884bcf62017-09-13 18:05:31 -0400230 /* and devices for each partition: */
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200231 while (!part_get_info(desc, part, &info)) {
Alexander Graff9d334b2016-08-05 14:49:53 +0200232 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
233 part);
Simon Glass487d7562016-05-14 14:03:05 -0600234 efi_disk_add_dev(devname, if_typename, desc, diskid,
Rob Clark884bcf62017-09-13 18:05:31 -0400235 info.start, part);
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200236 part++;
237 disks++;
238 }
Rob Clark884bcf62017-09-13 18:05:31 -0400239
240 /* ... and add block device: */
241 efi_disk_add_dev(devname, if_typename, desc, diskid, 0, 0);
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200242#endif
243
244 return disks;
245}
246
Alexander Graf2a22d052016-03-04 01:10:02 +0100247/*
248 * U-Boot doesn't have a list of all online disk devices. So when running our
249 * EFI payload, we scan through all of the potentially available ones and
250 * store them in our object pool.
251 *
Simon Glass487d7562016-05-14 14:03:05 -0600252 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
253 * Consider converting the code to look up devices as needed. The EFI device
254 * could be a child of the UCLASS_BLK block device, perhaps.
255 *
Alexander Graf2a22d052016-03-04 01:10:02 +0100256 * This gets called from do_bootefi_exec().
257 */
258int efi_disk_register(void)
259{
Alexander Graf2a22d052016-03-04 01:10:02 +0100260 int disks = 0;
Simon Glass487d7562016-05-14 14:03:05 -0600261#ifdef CONFIG_BLK
262 struct udevice *dev;
263
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000264 for (uclass_first_device_check(UCLASS_BLK, &dev);
Simon Glass487d7562016-05-14 14:03:05 -0600265 dev;
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000266 uclass_next_device_check(&dev)) {
Simon Glass487d7562016-05-14 14:03:05 -0600267 struct blk_desc *desc = dev_get_uclass_platdata(dev);
268 const char *if_typename = dev->driver->name;
Rob Clark884bcf62017-09-13 18:05:31 -0400269 disk_partition_t info;
270 int part = 1;
Simon Glass487d7562016-05-14 14:03:05 -0600271
272 printf("Scanning disk %s...\n", dev->name);
Rob Clark884bcf62017-09-13 18:05:31 -0400273
274 /* add devices for each partition: */
275 while (!part_get_info(desc, part, &info)) {
276 efi_disk_add_dev(dev->name, if_typename, desc,
277 desc->devnum, 0, part);
278 part++;
279 }
280
281 /* ... and add block device: */
282 efi_disk_add_dev(dev->name, if_typename, desc,
283 desc->devnum, 0, 0);
284
Simon Glass487d7562016-05-14 14:03:05 -0600285 disks++;
286
287 /*
288 * El Torito images show up as block devices in an EFI world,
289 * so let's create them here
290 */
291 disks += efi_disk_create_eltorito(desc, if_typename,
Alexander Graff9d334b2016-08-05 14:49:53 +0200292 desc->devnum, dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600293 }
294#else
295 int i, if_type;
Alexander Graf2a22d052016-03-04 01:10:02 +0100296
297 /* Search for all available disk devices */
Simon Glass6dd9faf2016-05-01 13:52:32 -0600298 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass487d7562016-05-14 14:03:05 -0600299 const struct blk_driver *cur_drvr;
300 const char *if_typename;
301
Simon Glass6dd9faf2016-05-01 13:52:32 -0600302 cur_drvr = blk_driver_lookup_type(if_type);
303 if (!cur_drvr)
304 continue;
305
Simon Glass487d7562016-05-14 14:03:05 -0600306 if_typename = cur_drvr->if_typename;
307 printf("Scanning disks on %s...\n", if_typename);
Alexander Graf2a22d052016-03-04 01:10:02 +0100308 for (i = 0; i < 4; i++) {
309 struct blk_desc *desc;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200310 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf2a22d052016-03-04 01:10:02 +0100311
Simon Glass6dd9faf2016-05-01 13:52:32 -0600312 desc = blk_get_devnum_by_type(if_type, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100313 if (!desc)
314 continue;
315 if (desc->type == DEV_TYPE_UNKNOWN)
316 continue;
317
Alexander Graf2a22d052016-03-04 01:10:02 +0100318 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass487d7562016-05-14 14:03:05 -0600319 if_typename, i);
Rob Clark884bcf62017-09-13 18:05:31 -0400320 efi_disk_add_dev(devname, if_typename, desc, i, 0, 0);
Alexander Graf2a22d052016-03-04 01:10:02 +0100321 disks++;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200322
323 /*
324 * El Torito images show up as block devices
325 * in an EFI world, so let's create them here
326 */
Alexander Graff9d334b2016-08-05 14:49:53 +0200327 disks += efi_disk_create_eltorito(desc, if_typename,
328 i, devname);
Alexander Graf2a22d052016-03-04 01:10:02 +0100329 }
330 }
Simon Glass487d7562016-05-14 14:03:05 -0600331#endif
Alexander Graf2a22d052016-03-04 01:10:02 +0100332 printf("Found %d disks\n", disks);
333
334 return 0;
335}