]> git.ipfire.org Git - people/ms/u-boot.git/blob - lib/efi_loader/efi_disk.c
cccfc6dac5c3ca178393c88ed93aea54b218ad35
[people/ms/u-boot.git] / lib / efi_loader / efi_disk.c
1 /*
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>
10 #include <blk.h>
11 #include <dm.h>
12 #include <efi_loader.h>
13 #include <inttypes.h>
14 #include <part.h>
15 #include <malloc.h>
16
17 const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
18
19 struct 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 *dp;
32 /* partition # */
33 unsigned int part;
34 /* handle to filesys proto (for partition objects) */
35 struct efi_simple_file_system_protocol *volume;
36 /* Offset into disk for simple partitions */
37 lbaint_t offset;
38 /* Internal block device */
39 struct blk_desc *desc;
40 };
41
42 static 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
49 enum efi_disk_direction {
50 EFI_DISK_READ,
51 EFI_DISK_WRITE,
52 };
53
54 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
55 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
64 diskobj = container_of(this, struct efi_disk_obj, ops);
65 desc = (struct blk_desc *) diskobj->desc;
66 blksz = desc->blksz;
67 blocks = buffer_size / blksz;
68 lba += diskobj->offset;
69
70 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
71 __LINE__, blocks, lba, blksz, direction);
72
73 /* We only support full block access */
74 if (buffer_size & (blksz - 1))
75 return EFI_DEVICE_ERROR;
76
77 if (direction == EFI_DISK_READ)
78 n = blk_dread(desc, lba, blocks, buffer);
79 else
80 n = blk_dwrite(desc, lba, blocks, buffer);
81
82 /* We don't do interrupts, so check for timers cooperatively */
83 efi_timer_check();
84
85 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
86
87 if (n != blocks)
88 return EFI_DEVICE_ERROR;
89
90 return EFI_SUCCESS;
91 }
92
93 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
94 u32 media_id, u64 lba, unsigned long buffer_size,
95 void *buffer)
96 {
97 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);
126 }
127
128 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
129 u32 media_id, u64 lba, unsigned long buffer_size,
130 void *buffer)
131 {
132 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);
161 }
162
163 static 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
170 static 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
177 /*
178 * Get the simple file system protocol for a file device path.
179 *
180 * The full path provided is split into device part and into a file
181 * part. The device part is used to find the handle on which the
182 * simple file system protocol is installed.
183 *
184 * @full_path device path including device and file
185 * @return simple file system protocol
186 */
187 struct efi_simple_file_system_protocol *
188 efi_fs_from_path(struct efi_device_path *full_path)
189 {
190 struct efi_object *efiobj;
191 struct efi_handler *handler;
192 struct efi_device_path *device_path;
193 struct efi_device_path *file_path;
194 efi_status_t ret;
195
196 /* Split the path into a device part and a file part */
197 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
198 if (ret != EFI_SUCCESS)
199 return NULL;
200 efi_free_pool(file_path);
201
202 /* Get the EFI object for the partition */
203 efiobj = efi_dp_find_obj(device_path, NULL);
204 efi_free_pool(device_path);
205 if (!efiobj)
206 return NULL;
207
208 /* Find the simple file system protocol */
209 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
210 &handler);
211 if (ret != EFI_SUCCESS)
212 return NULL;
213
214 /* Return the simple file system protocol for the partition */
215 return handler->protocol_interface;
216 }
217
218 /*
219 * Create a device for a disk
220 *
221 * @name not used
222 * @if_typename interface name for block device
223 * @desc internal block device
224 * @dev_index device index for block device
225 * @offset offset into disk for simple partitions
226 */
227 static void efi_disk_add_dev(const char *name,
228 const char *if_typename,
229 struct blk_desc *desc,
230 int dev_index,
231 lbaint_t offset,
232 unsigned int part)
233 {
234 struct efi_disk_obj *diskobj;
235 efi_status_t ret;
236
237 /* Don't add empty devices */
238 if (!desc->lba)
239 return;
240
241 diskobj = calloc(1, sizeof(*diskobj));
242 if (!diskobj)
243 goto out_of_memory;
244
245 /* Hook up to the device list */
246 efi_add_handle(&diskobj->parent);
247
248 /* Fill in object data */
249 diskobj->dp = efi_dp_from_part(desc, part);
250 diskobj->part = part;
251 ret = efi_add_protocol(diskobj->parent.handle, &efi_block_io_guid,
252 &diskobj->ops);
253 if (ret != EFI_SUCCESS)
254 goto out_of_memory;
255 ret = efi_add_protocol(diskobj->parent.handle, &efi_guid_device_path,
256 diskobj->dp);
257 if (ret != EFI_SUCCESS)
258 goto out_of_memory;
259 if (part >= 1) {
260 diskobj->volume = efi_simple_file_system(desc, part,
261 diskobj->dp);
262 ret = efi_add_protocol(diskobj->parent.handle,
263 &efi_simple_file_system_protocol_guid,
264 diskobj->volume);
265 if (ret != EFI_SUCCESS)
266 goto out_of_memory;
267 }
268 diskobj->ops = block_io_disk_template;
269 diskobj->ifname = if_typename;
270 diskobj->dev_index = dev_index;
271 diskobj->offset = offset;
272 diskobj->desc = desc;
273
274 /* Fill in EFI IO Media info (for read/write callbacks) */
275 diskobj->media.removable_media = desc->removable;
276 diskobj->media.media_present = 1;
277 diskobj->media.block_size = desc->blksz;
278 diskobj->media.io_align = desc->blksz;
279 diskobj->media.last_block = desc->lba - offset;
280 if (part != 0)
281 diskobj->media.logical_partition = 1;
282 diskobj->ops.media = &diskobj->media;
283 return;
284 out_of_memory:
285 printf("ERROR: Out of memory\n");
286 }
287
288 static int efi_disk_create_partitions(struct blk_desc *desc,
289 const char *if_typename,
290 int diskid,
291 const char *pdevname)
292 {
293 int disks = 0;
294 char devname[32] = { 0 }; /* dp->str is u16[32] long */
295 disk_partition_t info;
296 int part;
297
298 /* Add devices for each partition */
299 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
300 if (part_get_info(desc, part, &info))
301 continue;
302 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
303 part);
304 efi_disk_add_dev(devname, if_typename, desc, diskid,
305 info.start, part);
306 disks++;
307 }
308
309 return disks;
310 }
311
312 /*
313 * U-Boot doesn't have a list of all online disk devices. So when running our
314 * EFI payload, we scan through all of the potentially available ones and
315 * store them in our object pool.
316 *
317 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
318 * Consider converting the code to look up devices as needed. The EFI device
319 * could be a child of the UCLASS_BLK block device, perhaps.
320 *
321 * This gets called from do_bootefi_exec().
322 */
323 int efi_disk_register(void)
324 {
325 int disks = 0;
326 #ifdef CONFIG_BLK
327 struct udevice *dev;
328
329 for (uclass_first_device_check(UCLASS_BLK, &dev);
330 dev;
331 uclass_next_device_check(&dev)) {
332 struct blk_desc *desc = dev_get_uclass_platdata(dev);
333 const char *if_typename = blk_get_if_type_name(desc->if_type);
334
335 printf("Scanning disk %s...\n", dev->name);
336
337 /* Add block device for the full device */
338 efi_disk_add_dev(dev->name, if_typename, desc,
339 desc->devnum, 0, 0);
340
341 disks++;
342
343 /* Partitions show up as block devices in EFI */
344 disks += efi_disk_create_partitions(desc, if_typename,
345 desc->devnum, dev->name);
346 }
347 #else
348 int i, if_type;
349
350 /* Search for all available disk devices */
351 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
352 const struct blk_driver *cur_drvr;
353 const char *if_typename;
354
355 cur_drvr = blk_driver_lookup_type(if_type);
356 if (!cur_drvr)
357 continue;
358
359 if_typename = cur_drvr->if_typename;
360 printf("Scanning disks on %s...\n", if_typename);
361 for (i = 0; i < 4; i++) {
362 struct blk_desc *desc;
363 char devname[32] = { 0 }; /* dp->str is u16[32] long */
364
365 desc = blk_get_devnum_by_type(if_type, i);
366 if (!desc)
367 continue;
368 if (desc->type == DEV_TYPE_UNKNOWN)
369 continue;
370
371 snprintf(devname, sizeof(devname), "%s%d",
372 if_typename, i);
373
374 /* Add block device for the full device */
375 efi_disk_add_dev(devname, if_typename, desc, i, 0, 0);
376 disks++;
377
378 /* Partitions show up as block devices in EFI */
379 disks += efi_disk_create_partitions(desc, if_typename,
380 i, devname);
381 }
382 }
383 #endif
384 printf("Found %d disks\n", disks);
385
386 return 0;
387 }