]> Git Repo - J-u-boot.git/blame - lib/efi_loader/efi_disk.c
efi_loader: use EFI_PRINT() instead of debug()
[J-u-boot.git] / lib / efi_loader / efi_disk.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
2a22d05d
AG
2/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
2a22d05d
AG
6 */
7
8#include <common.h>
6dd9faf8 9#include <blk.h>
487d756f 10#include <dm.h>
2a22d05d 11#include <efi_loader.h>
2a22d05d
AG
12#include <part.h>
13#include <malloc.h>
14
dec88e41 15const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
2a22d05d 16
d39646a3
HS
17/**
18 * struct efi_disk_obj - EFI disk object
19 *
20 * @header: EFI object header
21 * @ops: EFI disk I/O protocol interface
22 * @ifname: interface name for block device
23 * @dev_index: device index of block device
24 * @media: block I/O media information
25 * @dp: device path to the block device
26 * @part: partition
27 * @volume: simple file system protocol of the partition
28 * @offset: offset into disk for simple partition
29 * @desc: internal block device descriptor
30 */
2a22d05d 31struct efi_disk_obj {
d39646a3 32 struct efi_object header;
2a22d05d 33 struct efi_block_io ops;
2a22d05d 34 const char *ifname;
2a22d05d 35 int dev_index;
2a22d05d 36 struct efi_block_io_media media;
884bcf6f 37 struct efi_device_path *dp;
884bcf6f 38 unsigned int part;
2a92080d 39 struct efi_simple_file_system_protocol *volume;
8c3df0bf 40 lbaint_t offset;
884bcf6f 41 struct blk_desc *desc;
2a22d05d
AG
42};
43
2a22d05d
AG
44static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
45 char extended_verification)
46{
47 EFI_ENTRY("%p, %x", this, extended_verification);
48 return EFI_EXIT(EFI_DEVICE_ERROR);
49}
50
51enum efi_disk_direction {
52 EFI_DISK_READ,
53 EFI_DISK_WRITE,
54};
55
a80a232e 56static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
2a22d05d
AG
57 u32 media_id, u64 lba, unsigned long buffer_size,
58 void *buffer, enum efi_disk_direction direction)
59{
60 struct efi_disk_obj *diskobj;
61 struct blk_desc *desc;
62 int blksz;
63 int blocks;
64 unsigned long n;
65
2a22d05d 66 diskobj = container_of(this, struct efi_disk_obj, ops);
f9d334bd 67 desc = (struct blk_desc *) diskobj->desc;
2a22d05d
AG
68 blksz = desc->blksz;
69 blocks = buffer_size / blksz;
8c3df0bf 70 lba += diskobj->offset;
2a22d05d 71
9d3f3398
HS
72 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
73 blocks, lba, blksz, direction);
2a22d05d
AG
74
75 /* We only support full block access */
76 if (buffer_size & (blksz - 1))
f59f0825 77 return EFI_BAD_BUFFER_SIZE;
2a22d05d
AG
78
79 if (direction == EFI_DISK_READ)
487d756f 80 n = blk_dread(desc, lba, blocks, buffer);
2a22d05d 81 else
487d756f 82 n = blk_dwrite(desc, lba, blocks, buffer);
2a22d05d
AG
83
84 /* We don't do interrupts, so check for timers cooperatively */
85 efi_timer_check();
86
9d3f3398 87 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
edcef3ba 88
2a22d05d 89 if (n != blocks)
3304990b 90 return EFI_DEVICE_ERROR;
2a22d05d 91
3304990b 92 return EFI_SUCCESS;
2a22d05d
AG
93}
94
e275458c 95static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
4f94865b 96 u32 media_id, u64 lba, efi_uintn_t buffer_size,
2a22d05d
AG
97 void *buffer)
98{
51735ae0
AG
99 void *real_buffer = buffer;
100 efi_status_t r;
101
f59f0825
HS
102 if (!this)
103 return EFI_INVALID_PARAMETER;
104 /* TODO: check for media changes */
105 if (media_id != this->media->media_id)
106 return EFI_MEDIA_CHANGED;
107 if (!this->media->media_present)
108 return EFI_NO_MEDIA;
109 /* media->io_align is a power of 2 */
110 if ((uintptr_t)buffer & (this->media->io_align - 1))
111 return EFI_INVALID_PARAMETER;
112 if (lba * this->media->block_size + buffer_size >
113 this->media->last_block * this->media->block_size)
114 return EFI_INVALID_PARAMETER;
115
51735ae0
AG
116#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
117 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
118 r = efi_disk_read_blocks(this, media_id, lba,
119 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
120 if (r != EFI_SUCCESS)
121 return r;
122 return efi_disk_read_blocks(this, media_id, lba +
123 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
124 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
125 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
126 }
127
128 real_buffer = efi_bounce_buffer;
129#endif
130
dee37fc9 131 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
51735ae0
AG
132 buffer_size, buffer);
133
134 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
135 EFI_DISK_READ);
136
137 /* Copy from bounce buffer to real buffer if necessary */
138 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
139 memcpy(buffer, real_buffer, buffer_size);
140
141 return EFI_EXIT(r);
2a22d05d
AG
142}
143
e275458c 144static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
4f94865b 145 u32 media_id, u64 lba, efi_uintn_t buffer_size,
2a22d05d
AG
146 void *buffer)
147{
51735ae0
AG
148 void *real_buffer = buffer;
149 efi_status_t r;
150
f59f0825
HS
151 if (!this)
152 return EFI_INVALID_PARAMETER;
153 if (this->media->read_only)
154 return EFI_WRITE_PROTECTED;
155 /* TODO: check for media changes */
156 if (media_id != this->media->media_id)
157 return EFI_MEDIA_CHANGED;
158 if (!this->media->media_present)
159 return EFI_NO_MEDIA;
160 /* media->io_align is a power of 2 */
161 if ((uintptr_t)buffer & (this->media->io_align - 1))
162 return EFI_INVALID_PARAMETER;
163 if (lba * this->media->block_size + buffer_size >
164 this->media->last_block * this->media->block_size)
165 return EFI_INVALID_PARAMETER;
166
51735ae0
AG
167#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
168 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
169 r = efi_disk_write_blocks(this, media_id, lba,
170 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
171 if (r != EFI_SUCCESS)
172 return r;
173 return efi_disk_write_blocks(this, media_id, lba +
174 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
175 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
176 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
177 }
178
179 real_buffer = efi_bounce_buffer;
180#endif
181
dee37fc9 182 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
51735ae0
AG
183 buffer_size, buffer);
184
185 /* Populate bounce buffer if necessary */
186 if (real_buffer != buffer)
187 memcpy(real_buffer, buffer, buffer_size);
188
189 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
190 EFI_DISK_WRITE);
191
192 return EFI_EXIT(r);
2a22d05d
AG
193}
194
195static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
196{
197 /* We always write synchronously */
198 EFI_ENTRY("%p", this);
199 return EFI_EXIT(EFI_SUCCESS);
200}
201
202static const struct efi_block_io block_io_disk_template = {
203 .reset = &efi_disk_reset,
204 .read_blocks = &efi_disk_read_blocks,
205 .write_blocks = &efi_disk_write_blocks,
206 .flush_blocks = &efi_disk_flush_blocks,
207};
208
2a92080d 209/*
110d80a1
HS
210 * Get the simple file system protocol for a file device path.
211 *
212 * The full path provided is split into device part and into a file
213 * part. The device part is used to find the handle on which the
214 * simple file system protocol is installed.
215 *
216 * @full_path device path including device and file
217 * @return simple file system protocol
2a92080d
RC
218 */
219struct efi_simple_file_system_protocol *
110d80a1 220efi_fs_from_path(struct efi_device_path *full_path)
2a92080d
RC
221{
222 struct efi_object *efiobj;
110d80a1
HS
223 struct efi_handler *handler;
224 struct efi_device_path *device_path;
225 struct efi_device_path *file_path;
226 efi_status_t ret;
2a92080d 227
110d80a1
HS
228 /* Split the path into a device part and a file part */
229 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
230 if (ret != EFI_SUCCESS)
231 return NULL;
232 efi_free_pool(file_path);
233
234 /* Get the EFI object for the partition */
235 efiobj = efi_dp_find_obj(device_path, NULL);
236 efi_free_pool(device_path);
2a92080d
RC
237 if (!efiobj)
238 return NULL;
239
110d80a1
HS
240 /* Find the simple file system protocol */
241 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
242 &handler);
243 if (ret != EFI_SUCCESS)
244 return NULL;
2a92080d 245
110d80a1
HS
246 /* Return the simple file system protocol for the partition */
247 return handler->protocol_interface;
2a92080d
RC
248}
249
93945f2c 250/*
64e4db0f 251 * Create a handle for a partition or disk
93945f2c 252 *
64e4db0f
HS
253 * @parent parent handle
254 * @dp_parent parent device path
93945f2c
HS
255 * @if_typename interface name for block device
256 * @desc internal block device
257 * @dev_index device index for block device
258 * @offset offset into disk for simple partitions
64e4db0f 259 * @return disk object
93945f2c 260 */
df9cf561 261static efi_status_t efi_disk_add_dev(
64e4db0f
HS
262 efi_handle_t parent,
263 struct efi_device_path *dp_parent,
264 const char *if_typename,
265 struct blk_desc *desc,
266 int dev_index,
267 lbaint_t offset,
df9cf561
HS
268 unsigned int part,
269 struct efi_disk_obj **disk)
4a12a97c
AG
270{
271 struct efi_disk_obj *diskobj;
4b9f7aaf 272 efi_status_t ret;
4a12a97c 273
0812d1a0
AG
274 /* Don't add empty devices */
275 if (!desc->lba)
df9cf561 276 return EFI_NOT_READY;
0812d1a0 277
884bcf6f 278 diskobj = calloc(1, sizeof(*diskobj));
4b9f7aaf 279 if (!diskobj)
df9cf561 280 return EFI_OUT_OF_RESOURCES;
4b9f7aaf
HS
281
282 /* Hook up to the device list */
d39646a3 283 efi_add_handle(&diskobj->header);
4a12a97c
AG
284
285 /* Fill in object data */
64e4db0f
HS
286 if (part) {
287 struct efi_device_path *node = efi_dp_part_node(desc, part);
288
289 diskobj->dp = efi_dp_append_node(dp_parent, node);
290 efi_free_pool(node);
291 } else {
292 diskobj->dp = efi_dp_from_part(desc, part);
293 }
884bcf6f 294 diskobj->part = part;
d39646a3 295 ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
4b9f7aaf
HS
296 &diskobj->ops);
297 if (ret != EFI_SUCCESS)
df9cf561 298 return ret;
d39646a3 299 ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
4b9f7aaf
HS
300 diskobj->dp);
301 if (ret != EFI_SUCCESS)
df9cf561 302 return ret;
2a92080d
RC
303 if (part >= 1) {
304 diskobj->volume = efi_simple_file_system(desc, part,
305 diskobj->dp);
d39646a3 306 ret = efi_add_protocol(&diskobj->header,
4b9f7aaf 307 &efi_simple_file_system_protocol_guid,
22de1de9 308 diskobj->volume);
4b9f7aaf 309 if (ret != EFI_SUCCESS)
df9cf561 310 return ret;
2a92080d 311 }
4a12a97c 312 diskobj->ops = block_io_disk_template;
487d756f 313 diskobj->ifname = if_typename;
4a12a97c 314 diskobj->dev_index = dev_index;
8c3df0bf 315 diskobj->offset = offset;
f9d334bd 316 diskobj->desc = desc;
4a12a97c
AG
317
318 /* Fill in EFI IO Media info (for read/write callbacks) */
319 diskobj->media.removable_media = desc->removable;
320 diskobj->media.media_present = 1;
f59f0825
HS
321 /*
322 * MediaID is just an arbitrary counter.
323 * We have to change it if the medium is removed or changed.
324 */
325 diskobj->media.media_id = 1;
4a12a97c
AG
326 diskobj->media.block_size = desc->blksz;
327 diskobj->media.io_align = desc->blksz;
0812d1a0 328 diskobj->media.last_block = desc->lba - offset;
5f770836
EV
329 if (part != 0)
330 diskobj->media.logical_partition = 1;
4a12a97c 331 diskobj->ops.media = &diskobj->media;
df9cf561
HS
332 if (disk)
333 *disk = diskobj;
334 return EFI_SUCCESS;
4a12a97c
AG
335}
336
64e4db0f
HS
337/*
338 * Create handles and protocols for the partitions of a block device
339 *
340 * @parent handle of the parent disk
341 * @blk_desc block device
342 * @if_typename interface type
343 * @diskid device number
344 * @pdevname device name
345 * @return number of partitions created
346 */
347int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
348 const char *if_typename, int diskid,
349 const char *pdevname)
8c3df0bf
AG
350{
351 int disks = 0;
ecbe1a07 352 char devname[32] = { 0 }; /* dp->str is u16[32] long */
8c3df0bf 353 disk_partition_t info;
16a73b24 354 int part;
64e4db0f
HS
355 struct efi_device_path *dp = NULL;
356 efi_status_t ret;
357 struct efi_handler *handler;
358
359 /* Get the device path of the parent */
360 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
361 if (ret == EFI_SUCCESS)
362 dp = handler->protocol_interface;
8c3df0bf 363
c034b7fd 364 /* Add devices for each partition */
16a73b24
JG
365 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
366 if (part_get_info(desc, part, &info))
367 continue;
f9d334bd
AG
368 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
369 part);
df9cf561
HS
370 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
371 info.start, part, NULL);
372 if (ret != EFI_SUCCESS) {
373 printf("Adding partition %s failed\n", pdevname);
374 continue;
375 }
8c3df0bf
AG
376 disks++;
377 }
884bcf6f 378
8c3df0bf
AG
379 return disks;
380}
381
2a22d05d
AG
382/*
383 * U-Boot doesn't have a list of all online disk devices. So when running our
384 * EFI payload, we scan through all of the potentially available ones and
385 * store them in our object pool.
386 *
487d756f
SG
387 * TODO([email protected]): Actually with CONFIG_BLK, U-Boot does have this.
388 * Consider converting the code to look up devices as needed. The EFI device
389 * could be a child of the UCLASS_BLK block device, perhaps.
390 *
2a22d05d
AG
391 * This gets called from do_bootefi_exec().
392 */
df9cf561 393efi_status_t efi_disk_register(void)
2a22d05d 394{
64e4db0f 395 struct efi_disk_obj *disk;
2a22d05d 396 int disks = 0;
df9cf561 397 efi_status_t ret;
487d756f
SG
398#ifdef CONFIG_BLK
399 struct udevice *dev;
400
df9cf561 401 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
70bfcdc6 402 uclass_next_device_check(&dev)) {
487d756f 403 struct blk_desc *desc = dev_get_uclass_platdata(dev);
9bfca9f9 404 const char *if_typename = blk_get_if_type_name(desc->if_type);
487d756f 405
c034b7fd 406 /* Add block device for the full device */
df9cf561
HS
407 printf("Scanning disk %s...\n", dev->name);
408 ret = efi_disk_add_dev(NULL, NULL, if_typename,
409 desc, desc->devnum, 0, 0, &disk);
410 if (ret == EFI_NOT_READY) {
411 printf("Disk %s not ready\n", dev->name);
412 continue;
413 }
414 if (ret) {
415 printf("ERROR: failure to add disk device %s, r = %lu\n",
416 dev->name, ret & ~EFI_ERROR_MASK);
417 return ret;
418 }
487d756f
SG
419 disks++;
420
c034b7fd 421 /* Partitions show up as block devices in EFI */
64e4db0f 422 disks += efi_disk_create_partitions(
d39646a3 423 &disk->header, desc, if_typename,
64e4db0f 424 desc->devnum, dev->name);
487d756f
SG
425 }
426#else
427 int i, if_type;
2a22d05d
AG
428
429 /* Search for all available disk devices */
6dd9faf8 430 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
487d756f
SG
431 const struct blk_driver *cur_drvr;
432 const char *if_typename;
433
6dd9faf8
SG
434 cur_drvr = blk_driver_lookup_type(if_type);
435 if (!cur_drvr)
436 continue;
437
487d756f
SG
438 if_typename = cur_drvr->if_typename;
439 printf("Scanning disks on %s...\n", if_typename);
2a22d05d
AG
440 for (i = 0; i < 4; i++) {
441 struct blk_desc *desc;
ecbe1a07 442 char devname[32] = { 0 }; /* dp->str is u16[32] long */
2a22d05d 443
6dd9faf8 444 desc = blk_get_devnum_by_type(if_type, i);
2a22d05d
AG
445 if (!desc)
446 continue;
447 if (desc->type == DEV_TYPE_UNKNOWN)
448 continue;
449
2a22d05d 450 snprintf(devname, sizeof(devname), "%s%d",
487d756f 451 if_typename, i);
77511b3b 452
c034b7fd 453 /* Add block device for the full device */
df9cf561
HS
454 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
455 i, 0, 0, &disk);
456 if (ret == EFI_NOT_READY) {
457 printf("Disk %s not ready\n", devname);
458 continue;
459 }
460 if (ret) {
461 printf("ERROR: failure to add disk device %s, r = %lu\n",
462 devname, ret & ~EFI_ERROR_MASK);
463 return ret;
464 }
2a22d05d 465 disks++;
8c3df0bf 466
c034b7fd 467 /* Partitions show up as block devices in EFI */
fae0118e 468 disks += efi_disk_create_partitions
d39646a3 469 (&disk->header, desc,
fae0118e 470 if_typename, i, devname);
2a22d05d
AG
471 }
472 }
487d756f 473#endif
2a22d05d
AG
474 printf("Found %d disks\n", disks);
475
df9cf561 476 return EFI_SUCCESS;
2a22d05d 477}
This page took 0.25719 seconds and 4 git commands to generate.