blob: ed06485e334b771b2e2e8872615952b19735c0f2 [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 */
31 struct efi_device_path_file_path *dp;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020032 /* Offset into disk for simple partitions */
33 lbaint_t offset;
Alexander Graff9d334b2016-08-05 14:49:53 +020034 /* Internal block device */
35 const struct blk_desc *desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010036};
37
Alexander Graf2a22d052016-03-04 01:10:02 +010038static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
39 char extended_verification)
40{
41 EFI_ENTRY("%p, %x", this, extended_verification);
42 return EFI_EXIT(EFI_DEVICE_ERROR);
43}
44
45enum efi_disk_direction {
46 EFI_DISK_READ,
47 EFI_DISK_WRITE,
48};
49
50static 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)
53{
54 struct efi_disk_obj *diskobj;
55 struct blk_desc *desc;
56 int blksz;
57 int blocks;
58 unsigned long n;
59
Alexander Graf2a22d052016-03-04 01:10:02 +010060 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graff9d334b2016-08-05 14:49:53 +020061 desc = (struct blk_desc *) diskobj->desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010062 blksz = desc->blksz;
63 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020064 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010065
Alexander Grafedcef3b2016-06-02 11:38:27 +020066 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
67 __LINE__, blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010068
69 /* We only support full block access */
70 if (buffer_size & (blksz - 1))
Rob Clark33049902017-07-25 20:28:29 -040071 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +010072
73 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -060074 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010075 else
Simon Glass487d7562016-05-14 14:03:05 -060076 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010077
78 /* We don't do interrupts, so check for timers cooperatively */
79 efi_timer_check();
80
Alexander Grafedcef3b2016-06-02 11:38:27 +020081 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
82
Alexander Graf2a22d052016-03-04 01:10:02 +010083 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -040084 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +010085
Rob Clark33049902017-07-25 20:28:29 -040086 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +010087}
88
Simon Glasse2754582016-09-25 15:27:32 -060089static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010090 u32 media_id, u64 lba, unsigned long buffer_size,
91 void *buffer)
92{
Alexander Graf51735ae2016-05-11 18:25:48 +020093 void *real_buffer = buffer;
94 efi_status_t r;
95
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)
101 return r;
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);
106 }
107
108 real_buffer = efi_bounce_buffer;
109#endif
110
111 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
112 buffer_size, buffer);
113
114 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
115 EFI_DISK_READ);
116
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);
120
121 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100122}
123
Simon Glasse2754582016-09-25 15:27:32 -0600124static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +0100125 u32 media_id, u64 lba, unsigned long buffer_size,
126 void *buffer)
127{
Alexander Graf51735ae2016-05-11 18:25:48 +0200128 void *real_buffer = buffer;
129 efi_status_t r;
130
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)
136 return r;
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);
141 }
142
143 real_buffer = efi_bounce_buffer;
144#endif
145
146 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
147 buffer_size, buffer);
148
149 /* Populate bounce buffer if necessary */
150 if (real_buffer != buffer)
151 memcpy(real_buffer, buffer, buffer_size);
152
153 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
154 EFI_DISK_WRITE);
155
156 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100157}
158
159static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
160{
161 /* We always write synchronously */
162 EFI_ENTRY("%p", this);
163 return EFI_EXIT(EFI_SUCCESS);
164}
165
166static 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,
171};
172
Simon Glass487d7562016-05-14 14:03:05 -0600173static void efi_disk_add_dev(const char *name,
174 const char *if_typename,
Alexander Graf4a12a972016-04-11 16:16:17 +0200175 const struct blk_desc *desc,
176 int dev_index,
177 lbaint_t offset)
178{
179 struct efi_disk_obj *diskobj;
180 struct efi_device_path_file_path *dp;
181 int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
182
Alexander Graf0812d1a2016-08-05 14:51:47 +0200183 /* Don't add empty devices */
184 if (!desc->lba)
185 return;
186
Alexander Graf4a12a972016-04-11 16:16:17 +0200187 diskobj = calloc(1, objlen);
188
189 /* Fill in object data */
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200190 dp = (void *)&diskobj[1];
Alexander Graf4a12a972016-04-11 16:16:17 +0200191 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200192 diskobj->parent.protocols[0].protocol_interface = &diskobj->ops;
Alexander Graf4a12a972016-04-11 16:16:17 +0200193 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200194 diskobj->parent.protocols[1].protocol_interface = dp;
Alexander Graf4a12a972016-04-11 16:16:17 +0200195 diskobj->parent.handle = diskobj;
196 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600197 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200198 diskobj->dev_index = dev_index;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200199 diskobj->offset = offset;
Alexander Graff9d334b2016-08-05 14:49:53 +0200200 diskobj->desc = desc;
Alexander Graf4a12a972016-04-11 16:16:17 +0200201
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;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200207 diskobj->media.last_block = desc->lba - offset;
Alexander Graf4a12a972016-04-11 16:16:17 +0200208 diskobj->ops.media = &diskobj->media;
209
210 /* Fill in device path */
Alexander Graf4a12a972016-04-11 16:16:17 +0200211 diskobj->dp = dp;
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);
216
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);
220
221 /* Hook up to the device list */
222 list_add_tail(&diskobj->parent.link, &efi_obj_list);
223}
224
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200225static int efi_disk_create_eltorito(struct blk_desc *desc,
Simon Glass487d7562016-05-14 14:03:05 -0600226 const char *if_typename,
Alexander Graff9d334b2016-08-05 14:49:53 +0200227 int diskid,
228 const char *pdevname)
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200229{
230 int disks = 0;
Patrick Delaunay1acc0082017-01-27 11:00:38 +0100231#if CONFIG_IS_ENABLED(ISO_PARTITION)
Alexander Grafecbe1a02016-04-11 16:16:20 +0200232 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200233 disk_partition_t info;
234 int part = 1;
235
236 if (desc->part_type != PART_TYPE_ISO)
237 return 0;
238
239 while (!part_get_info(desc, part, &info)) {
Alexander Graff9d334b2016-08-05 14:49:53 +0200240 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
241 part);
Simon Glass487d7562016-05-14 14:03:05 -0600242 efi_disk_add_dev(devname, if_typename, desc, diskid,
243 info.start);
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200244 part++;
245 disks++;
246 }
247#endif
248
249 return disks;
250}
251
Alexander Graf2a22d052016-03-04 01:10:02 +0100252/*
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.
256 *
Simon Glass487d7562016-05-14 14:03:05 -0600257 * 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.
260 *
Alexander Graf2a22d052016-03-04 01:10:02 +0100261 * This gets called from do_bootefi_exec().
262 */
263int efi_disk_register(void)
264{
Alexander Graf2a22d052016-03-04 01:10:02 +0100265 int disks = 0;
Simon Glass487d7562016-05-14 14:03:05 -0600266#ifdef CONFIG_BLK
267 struct udevice *dev;
268
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000269 for (uclass_first_device_check(UCLASS_BLK, &dev);
Simon Glass487d7562016-05-14 14:03:05 -0600270 dev;
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000271 uclass_next_device_check(&dev)) {
Simon Glass487d7562016-05-14 14:03:05 -0600272 struct blk_desc *desc = dev_get_uclass_platdata(dev);
273 const char *if_typename = dev->driver->name;
274
275 printf("Scanning disk %s...\n", dev->name);
276 efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0);
277 disks++;
278
279 /*
280 * El Torito images show up as block devices in an EFI world,
281 * so let's create them here
282 */
283 disks += efi_disk_create_eltorito(desc, if_typename,
Alexander Graff9d334b2016-08-05 14:49:53 +0200284 desc->devnum, dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600285 }
286#else
287 int i, if_type;
Alexander Graf2a22d052016-03-04 01:10:02 +0100288
289 /* Search for all available disk devices */
Simon Glass6dd9faf2016-05-01 13:52:32 -0600290 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass487d7562016-05-14 14:03:05 -0600291 const struct blk_driver *cur_drvr;
292 const char *if_typename;
293
Simon Glass6dd9faf2016-05-01 13:52:32 -0600294 cur_drvr = blk_driver_lookup_type(if_type);
295 if (!cur_drvr)
296 continue;
297
Simon Glass487d7562016-05-14 14:03:05 -0600298 if_typename = cur_drvr->if_typename;
299 printf("Scanning disks on %s...\n", if_typename);
Alexander Graf2a22d052016-03-04 01:10:02 +0100300 for (i = 0; i < 4; i++) {
301 struct blk_desc *desc;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200302 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf2a22d052016-03-04 01:10:02 +0100303
Simon Glass6dd9faf2016-05-01 13:52:32 -0600304 desc = blk_get_devnum_by_type(if_type, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100305 if (!desc)
306 continue;
307 if (desc->type == DEV_TYPE_UNKNOWN)
308 continue;
309
Alexander Graf2a22d052016-03-04 01:10:02 +0100310 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass487d7562016-05-14 14:03:05 -0600311 if_typename, i);
312 efi_disk_add_dev(devname, if_typename, desc, i, 0);
Alexander Graf2a22d052016-03-04 01:10:02 +0100313 disks++;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200314
315 /*
316 * El Torito images show up as block devices
317 * in an EFI world, so let's create them here
318 */
Alexander Graff9d334b2016-08-05 14:49:53 +0200319 disks += efi_disk_create_eltorito(desc, if_typename,
320 i, devname);
Alexander Graf2a22d052016-03-04 01:10:02 +0100321 }
322 }
Simon Glass487d7562016-05-14 14:03:05 -0600323#endif
Alexander Graf2a22d052016-03-04 01:10:02 +0100324 printf("Found %d disks\n", disks);
325
326 return 0;
327}