blob: e61dbc80580b61ae6e0351be68d77fd8eeca0eeb [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 */
Rob Clarka83272f2017-09-13 18:05:31 -040031 struct efi_device_path *dp;
32 /* partition # */
33 unsigned int part;
Rob Clark53142032017-09-13 18:05:34 -040034 /* handle to filesys proto (for partition objects) */
35 struct efi_simple_file_system_protocol *volume;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020036 /* Offset into disk for simple partitions */
37 lbaint_t offset;
Alexander Graf34118e72016-08-05 14:49:53 +020038 /* Internal block device */
Rob Clarka83272f2017-09-13 18:05:31 -040039 struct blk_desc *desc;
Alexander Grafe83be452016-03-04 01:10:02 +010040};
41
Alexander Grafe83be452016-03-04 01:10:02 +010042static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
43 char extended_verification)
44{
45 EFI_ENTRY("%p, %x", this, extended_verification);
46 return EFI_EXIT(EFI_DEVICE_ERROR);
47}
48
49enum efi_disk_direction {
50 EFI_DISK_READ,
51 EFI_DISK_WRITE,
52};
53
Heinrich Schuchardt9ce06172017-08-26 22:33:13 +020054static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +010055 u32 media_id, u64 lba, unsigned long buffer_size,
56 void *buffer, enum efi_disk_direction direction)
57{
58 struct efi_disk_obj *diskobj;
59 struct blk_desc *desc;
60 int blksz;
61 int blocks;
62 unsigned long n;
63
Alexander Grafe83be452016-03-04 01:10:02 +010064 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graf34118e72016-08-05 14:49:53 +020065 desc = (struct blk_desc *) diskobj->desc;
Alexander Grafe83be452016-03-04 01:10:02 +010066 blksz = desc->blksz;
67 blocks = buffer_size / blksz;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020068 lba += diskobj->offset;
Alexander Grafe83be452016-03-04 01:10:02 +010069
Alexander Graf62a67482016-06-02 11:38:27 +020070 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
71 __LINE__, blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +010072
73 /* We only support full block access */
74 if (buffer_size & (blksz - 1))
Rob Clark99c43872017-07-25 20:28:29 -040075 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +010076
77 if (direction == EFI_DISK_READ)
Simon Glass302d4f82016-05-14 14:03:05 -060078 n = blk_dread(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010079 else
Simon Glass302d4f82016-05-14 14:03:05 -060080 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010081
82 /* We don't do interrupts, so check for timers cooperatively */
83 efi_timer_check();
84
Alexander Graf62a67482016-06-02 11:38:27 +020085 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
86
Alexander Grafe83be452016-03-04 01:10:02 +010087 if (n != blocks)
Rob Clark99c43872017-07-25 20:28:29 -040088 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +010089
Rob Clark99c43872017-07-25 20:28:29 -040090 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +010091}
92
Simon Glassc2194bf2016-09-25 15:27:32 -060093static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +010094 u32 media_id, u64 lba, unsigned long buffer_size,
95 void *buffer)
96{
Alexander Graf7c00a3c2016-05-11 18:25:48 +020097 void *real_buffer = buffer;
98 efi_status_t r;
99
100#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
101 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
102 r = efi_disk_read_blocks(this, media_id, lba,
103 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
104 if (r != EFI_SUCCESS)
105 return r;
106 return efi_disk_read_blocks(this, media_id, lba +
107 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
108 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
109 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
110 }
111
112 real_buffer = efi_bounce_buffer;
113#endif
114
115 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
116 buffer_size, buffer);
117
118 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
119 EFI_DISK_READ);
120
121 /* Copy from bounce buffer to real buffer if necessary */
122 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
123 memcpy(buffer, real_buffer, buffer_size);
124
125 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100126}
127
Simon Glassc2194bf2016-09-25 15:27:32 -0600128static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +0100129 u32 media_id, u64 lba, unsigned long buffer_size,
130 void *buffer)
131{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200132 void *real_buffer = buffer;
133 efi_status_t r;
134
135#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
136 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
137 r = efi_disk_write_blocks(this, media_id, lba,
138 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
139 if (r != EFI_SUCCESS)
140 return r;
141 return efi_disk_write_blocks(this, media_id, lba +
142 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
143 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
144 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
145 }
146
147 real_buffer = efi_bounce_buffer;
148#endif
149
150 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
151 buffer_size, buffer);
152
153 /* Populate bounce buffer if necessary */
154 if (real_buffer != buffer)
155 memcpy(real_buffer, buffer, buffer_size);
156
157 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
158 EFI_DISK_WRITE);
159
160 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100161}
162
163static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
164{
165 /* We always write synchronously */
166 EFI_ENTRY("%p", this);
167 return EFI_EXIT(EFI_SUCCESS);
168}
169
170static const struct efi_block_io block_io_disk_template = {
171 .reset = &efi_disk_reset,
172 .read_blocks = &efi_disk_read_blocks,
173 .write_blocks = &efi_disk_write_blocks,
174 .flush_blocks = &efi_disk_flush_blocks,
175};
176
Rob Clark53142032017-09-13 18:05:34 -0400177/*
178 * Find filesystem from a device-path. The passed in path 'p' probably
179 * contains one or more /File(name) nodes, so the comparison stops at
180 * the first /File() node, and returns the pointer to that via 'rp'.
181 * This is mostly intended to be a helper to map a device-path to an
182 * efi_file_handle object.
183 */
184struct efi_simple_file_system_protocol *
185efi_fs_from_path(struct efi_device_path *fp)
186{
187 struct efi_object *efiobj;
188 struct efi_disk_obj *diskobj;
189
190 efiobj = efi_dp_find_obj(fp, NULL);
191 if (!efiobj)
192 return NULL;
193
194 diskobj = container_of(efiobj, struct efi_disk_obj, parent);
195
196 return diskobj->volume;
197}
198
Simon Glass302d4f82016-05-14 14:03:05 -0600199static void efi_disk_add_dev(const char *name,
200 const char *if_typename,
Rob Clarka83272f2017-09-13 18:05:31 -0400201 struct blk_desc *desc,
Alexander Graf06fe57b2016-04-11 16:16:17 +0200202 int dev_index,
Rob Clarka83272f2017-09-13 18:05:31 -0400203 lbaint_t offset,
204 unsigned int part)
Alexander Graf06fe57b2016-04-11 16:16:17 +0200205{
206 struct efi_disk_obj *diskobj;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200207
Alexander Graf601db1f2016-08-05 14:51:47 +0200208 /* Don't add empty devices */
209 if (!desc->lba)
210 return;
211
Rob Clarka83272f2017-09-13 18:05:31 -0400212 diskobj = calloc(1, sizeof(*diskobj));
Alexander Graf06fe57b2016-04-11 16:16:17 +0200213
214 /* Fill in object data */
Rob Clarka83272f2017-09-13 18:05:31 -0400215 diskobj->dp = efi_dp_from_part(desc, part);
216 diskobj->part = part;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200217 diskobj->parent.protocols[0].guid = &efi_block_io_guid;
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +0200218 diskobj->parent.protocols[0].protocol_interface = &diskobj->ops;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200219 diskobj->parent.protocols[1].guid = &efi_guid_device_path;
Rob Clarka83272f2017-09-13 18:05:31 -0400220 diskobj->parent.protocols[1].protocol_interface = diskobj->dp;
Rob Clark53142032017-09-13 18:05:34 -0400221 if (part >= 1) {
222 diskobj->volume = efi_simple_file_system(desc, part,
223 diskobj->dp);
224 diskobj->parent.protocols[2].guid =
225 &efi_simple_file_system_protocol_guid;
226 diskobj->parent.protocols[2].protocol_interface =
227 diskobj->volume;
228 }
Alexander Graf06fe57b2016-04-11 16:16:17 +0200229 diskobj->parent.handle = diskobj;
230 diskobj->ops = block_io_disk_template;
Simon Glass302d4f82016-05-14 14:03:05 -0600231 diskobj->ifname = if_typename;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200232 diskobj->dev_index = dev_index;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200233 diskobj->offset = offset;
Alexander Graf34118e72016-08-05 14:49:53 +0200234 diskobj->desc = desc;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200235
236 /* Fill in EFI IO Media info (for read/write callbacks) */
237 diskobj->media.removable_media = desc->removable;
238 diskobj->media.media_present = 1;
239 diskobj->media.block_size = desc->blksz;
240 diskobj->media.io_align = desc->blksz;
Alexander Graf601db1f2016-08-05 14:51:47 +0200241 diskobj->media.last_block = desc->lba - offset;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200242 diskobj->ops.media = &diskobj->media;
243
Alexander Graf06fe57b2016-04-11 16:16:17 +0200244 /* Hook up to the device list */
245 list_add_tail(&diskobj->parent.link, &efi_obj_list);
246}
247
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200248static int efi_disk_create_eltorito(struct blk_desc *desc,
Simon Glass302d4f82016-05-14 14:03:05 -0600249 const char *if_typename,
Alexander Graf34118e72016-08-05 14:49:53 +0200250 int diskid,
251 const char *pdevname)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200252{
253 int disks = 0;
Patrick Delaunay21d3bce2017-01-27 11:00:38 +0100254#if CONFIG_IS_ENABLED(ISO_PARTITION)
Alexander Graf3491ee72016-04-11 16:16:20 +0200255 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200256 disk_partition_t info;
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100257 int part;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200258
259 if (desc->part_type != PART_TYPE_ISO)
260 return 0;
261
Rob Clarka83272f2017-09-13 18:05:31 -0400262 /* and devices for each partition: */
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100263 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
264 if (part_get_info(desc, part, &info))
265 continue;
Alexander Graf34118e72016-08-05 14:49:53 +0200266 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
267 part);
Simon Glass302d4f82016-05-14 14:03:05 -0600268 efi_disk_add_dev(devname, if_typename, desc, diskid,
Rob Clarka83272f2017-09-13 18:05:31 -0400269 info.start, part);
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200270 disks++;
271 }
Rob Clarka83272f2017-09-13 18:05:31 -0400272
273 /* ... and add block device: */
274 efi_disk_add_dev(devname, if_typename, desc, diskid, 0, 0);
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200275#endif
276
277 return disks;
278}
279
Alexander Grafe83be452016-03-04 01:10:02 +0100280/*
281 * U-Boot doesn't have a list of all online disk devices. So when running our
282 * EFI payload, we scan through all of the potentially available ones and
283 * store them in our object pool.
284 *
Simon Glass302d4f82016-05-14 14:03:05 -0600285 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
286 * Consider converting the code to look up devices as needed. The EFI device
287 * could be a child of the UCLASS_BLK block device, perhaps.
288 *
Alexander Grafe83be452016-03-04 01:10:02 +0100289 * This gets called from do_bootefi_exec().
290 */
291int efi_disk_register(void)
292{
Alexander Grafe83be452016-03-04 01:10:02 +0100293 int disks = 0;
Simon Glass302d4f82016-05-14 14:03:05 -0600294#ifdef CONFIG_BLK
295 struct udevice *dev;
296
xypron.glpk@gmx.de308cd8e2017-06-20 19:10:27 +0000297 for (uclass_first_device_check(UCLASS_BLK, &dev);
Simon Glass302d4f82016-05-14 14:03:05 -0600298 dev;
xypron.glpk@gmx.de308cd8e2017-06-20 19:10:27 +0000299 uclass_next_device_check(&dev)) {
Simon Glass302d4f82016-05-14 14:03:05 -0600300 struct blk_desc *desc = dev_get_uclass_platdata(dev);
301 const char *if_typename = dev->driver->name;
Rob Clarka83272f2017-09-13 18:05:31 -0400302 disk_partition_t info;
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100303 int part;
Simon Glass302d4f82016-05-14 14:03:05 -0600304
305 printf("Scanning disk %s...\n", dev->name);
Rob Clarka83272f2017-09-13 18:05:31 -0400306
307 /* add devices for each partition: */
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100308 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
309 if (part_get_info(desc, part, &info))
310 continue;
Rob Clarka83272f2017-09-13 18:05:31 -0400311 efi_disk_add_dev(dev->name, if_typename, desc,
312 desc->devnum, 0, part);
Rob Clarka83272f2017-09-13 18:05:31 -0400313 }
314
315 /* ... and add block device: */
316 efi_disk_add_dev(dev->name, if_typename, desc,
317 desc->devnum, 0, 0);
318
Simon Glass302d4f82016-05-14 14:03:05 -0600319 disks++;
320
321 /*
322 * El Torito images show up as block devices in an EFI world,
323 * so let's create them here
324 */
325 disks += efi_disk_create_eltorito(desc, if_typename,
Alexander Graf34118e72016-08-05 14:49:53 +0200326 desc->devnum, dev->name);
Simon Glass302d4f82016-05-14 14:03:05 -0600327 }
328#else
329 int i, if_type;
Alexander Grafe83be452016-03-04 01:10:02 +0100330
331 /* Search for all available disk devices */
Simon Glasse3dfa912016-05-01 13:52:32 -0600332 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass302d4f82016-05-14 14:03:05 -0600333 const struct blk_driver *cur_drvr;
334 const char *if_typename;
335
Simon Glasse3dfa912016-05-01 13:52:32 -0600336 cur_drvr = blk_driver_lookup_type(if_type);
337 if (!cur_drvr)
338 continue;
339
Simon Glass302d4f82016-05-14 14:03:05 -0600340 if_typename = cur_drvr->if_typename;
341 printf("Scanning disks on %s...\n", if_typename);
Alexander Grafe83be452016-03-04 01:10:02 +0100342 for (i = 0; i < 4; i++) {
343 struct blk_desc *desc;
Alexander Graf3491ee72016-04-11 16:16:20 +0200344 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400345 disk_partition_t info;
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100346 int part;
Alexander Grafe83be452016-03-04 01:10:02 +0100347
Simon Glasse3dfa912016-05-01 13:52:32 -0600348 desc = blk_get_devnum_by_type(if_type, i);
Alexander Grafe83be452016-03-04 01:10:02 +0100349 if (!desc)
350 continue;
351 if (desc->type == DEV_TYPE_UNKNOWN)
352 continue;
353
Alexander Grafe83be452016-03-04 01:10:02 +0100354 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass302d4f82016-05-14 14:03:05 -0600355 if_typename, i);
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400356
357 /* add devices for each partition: */
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100358 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
359 if (part_get_info(desc, part, &info))
360 continue;
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400361 efi_disk_add_dev(devname, if_typename, desc,
362 i, 0, part);
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400363 }
364
365 /* ... and add block device: */
Rob Clarka83272f2017-09-13 18:05:31 -0400366 efi_disk_add_dev(devname, if_typename, desc, i, 0, 0);
Alexander Grafe83be452016-03-04 01:10:02 +0100367 disks++;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200368
369 /*
370 * El Torito images show up as block devices
371 * in an EFI world, so let's create them here
372 */
Alexander Graf34118e72016-08-05 14:49:53 +0200373 disks += efi_disk_create_eltorito(desc, if_typename,
374 i, devname);
Alexander Grafe83be452016-03-04 01:10:02 +0100375 }
376 }
Simon Glass302d4f82016-05-14 14:03:05 -0600377#endif
Alexander Grafe83be452016-03-04 01:10:02 +0100378 printf("Found %d disks\n", disks);
379
380 return 0;
381}