1
2
3
4
5
6
7
8#define LOG_CATEGORY LOGC_EFI
9
10#include <common.h>
11#include <blk.h>
12#include <dm.h>
13#include <efi_loader.h>
14#include <fs.h>
15#include <log.h>
16#include <part.h>
17#include <malloc.h>
18
19struct efi_system_partition efi_system_partition;
20
21const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37struct efi_disk_obj {
38 struct efi_object header;
39 struct efi_block_io ops;
40 const char *ifname;
41 int dev_index;
42 struct efi_block_io_media media;
43 struct efi_device_path *dp;
44 unsigned int part;
45 struct efi_simple_file_system_protocol *volume;
46 lbaint_t offset;
47 struct blk_desc *desc;
48};
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
66 char extended_verification)
67{
68 EFI_ENTRY("%p, %x", this, extended_verification);
69 return EFI_EXIT(EFI_SUCCESS);
70}
71
72enum efi_disk_direction {
73 EFI_DISK_READ,
74 EFI_DISK_WRITE,
75};
76
77static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
78 u32 media_id, u64 lba, unsigned long buffer_size,
79 void *buffer, enum efi_disk_direction direction)
80{
81 struct efi_disk_obj *diskobj;
82 struct blk_desc *desc;
83 int blksz;
84 int blocks;
85 unsigned long n;
86
87 diskobj = container_of(this, struct efi_disk_obj, ops);
88 desc = (struct blk_desc *) diskobj->desc;
89 blksz = desc->blksz;
90 blocks = buffer_size / blksz;
91 lba += diskobj->offset;
92
93 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
94 blocks, lba, blksz, direction);
95
96
97 if (buffer_size & (blksz - 1))
98 return EFI_BAD_BUFFER_SIZE;
99
100 if (direction == EFI_DISK_READ)
101 n = blk_dread(desc, lba, blocks, buffer);
102 else
103 n = blk_dwrite(desc, lba, blocks, buffer);
104
105
106 efi_timer_check();
107
108 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
109
110 if (n != blocks)
111 return EFI_DEVICE_ERROR;
112
113 return EFI_SUCCESS;
114}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
132 u32 media_id, u64 lba, efi_uintn_t buffer_size,
133 void *buffer)
134{
135 void *real_buffer = buffer;
136 efi_status_t r;
137
138 if (!this)
139 return EFI_INVALID_PARAMETER;
140
141 if (media_id != this->media->media_id)
142 return EFI_MEDIA_CHANGED;
143 if (!this->media->media_present)
144 return EFI_NO_MEDIA;
145
146 if ((uintptr_t)buffer & (this->media->io_align - 1))
147 return EFI_INVALID_PARAMETER;
148 if (lba * this->media->block_size + buffer_size >
149 this->media->last_block * this->media->block_size)
150 return EFI_INVALID_PARAMETER;
151
152#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
153 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
154 r = efi_disk_read_blocks(this, media_id, lba,
155 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
156 if (r != EFI_SUCCESS)
157 return r;
158 return efi_disk_read_blocks(this, media_id, lba +
159 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
160 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
161 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
162 }
163
164 real_buffer = efi_bounce_buffer;
165#endif
166
167 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
168 buffer_size, buffer);
169
170 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
171 EFI_DISK_READ);
172
173
174 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
175 memcpy(buffer, real_buffer, buffer_size);
176
177 return EFI_EXIT(r);
178}
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
197 u32 media_id, u64 lba, efi_uintn_t buffer_size,
198 void *buffer)
199{
200 void *real_buffer = buffer;
201 efi_status_t r;
202
203 if (!this)
204 return EFI_INVALID_PARAMETER;
205 if (this->media->read_only)
206 return EFI_WRITE_PROTECTED;
207
208 if (media_id != this->media->media_id)
209 return EFI_MEDIA_CHANGED;
210 if (!this->media->media_present)
211 return EFI_NO_MEDIA;
212
213 if ((uintptr_t)buffer & (this->media->io_align - 1))
214 return EFI_INVALID_PARAMETER;
215 if (lba * this->media->block_size + buffer_size >
216 this->media->last_block * this->media->block_size)
217 return EFI_INVALID_PARAMETER;
218
219#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
220 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
221 r = efi_disk_write_blocks(this, media_id, lba,
222 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
223 if (r != EFI_SUCCESS)
224 return r;
225 return efi_disk_write_blocks(this, media_id, lba +
226 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
227 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
228 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
229 }
230
231 real_buffer = efi_bounce_buffer;
232#endif
233
234 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
235 buffer_size, buffer);
236
237
238 if (real_buffer != buffer)
239 memcpy(real_buffer, buffer, buffer_size);
240
241 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
242 EFI_DISK_WRITE);
243
244 return EFI_EXIT(r);
245}
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
262{
263 EFI_ENTRY("%p", this);
264 return EFI_EXIT(EFI_SUCCESS);
265}
266
267static const struct efi_block_io block_io_disk_template = {
268 .reset = &efi_disk_reset,
269 .read_blocks = &efi_disk_read_blocks,
270 .write_blocks = &efi_disk_write_blocks,
271 .flush_blocks = &efi_disk_flush_blocks,
272};
273
274
275
276
277
278
279
280
281
282
283
284
285
286struct efi_simple_file_system_protocol *
287efi_fs_from_path(struct efi_device_path *full_path)
288{
289 struct efi_object *efiobj;
290 struct efi_handler *handler;
291 struct efi_device_path *device_path;
292 struct efi_device_path *file_path;
293 efi_status_t ret;
294
295
296 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
297 if (ret != EFI_SUCCESS)
298 return NULL;
299 efi_free_pool(file_path);
300
301
302 efiobj = efi_dp_find_obj(device_path, NULL);
303 efi_free_pool(device_path);
304 if (!efiobj)
305 return NULL;
306
307
308 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
309 &handler);
310 if (ret != EFI_SUCCESS)
311 return NULL;
312
313
314 return handler->protocol_interface;
315}
316
317
318
319
320
321
322
323
324
325static int efi_fs_exists(struct blk_desc *desc, int part)
326{
327 if (fs_set_blk_dev_with_part(desc, part))
328 return 0;
329
330 if (fs_get_type() == FS_TYPE_ANY)
331 return 0;
332
333 fs_close();
334
335 return 1;
336}
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351static efi_status_t efi_disk_add_dev(
352 efi_handle_t parent,
353 struct efi_device_path *dp_parent,
354 const char *if_typename,
355 struct blk_desc *desc,
356 int dev_index,
357 lbaint_t offset,
358 unsigned int part,
359 struct efi_disk_obj **disk)
360{
361 struct efi_disk_obj *diskobj;
362 struct efi_object *handle;
363 efi_status_t ret;
364
365
366 if (!desc->lba)
367 return EFI_NOT_READY;
368
369 diskobj = calloc(1, sizeof(*diskobj));
370 if (!diskobj)
371 return EFI_OUT_OF_RESOURCES;
372
373
374 efi_add_handle(&diskobj->header);
375
376
377 if (part) {
378 struct efi_device_path *node = efi_dp_part_node(desc, part);
379
380 diskobj->dp = efi_dp_append_node(dp_parent, node);
381 efi_free_pool(node);
382 } else {
383 diskobj->dp = efi_dp_from_part(desc, part);
384 }
385 diskobj->part = part;
386
387
388
389
390
391
392
393
394 handle = &diskobj->header;
395 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
396 &handle, &efi_guid_device_path, diskobj->dp,
397 &efi_block_io_guid, &diskobj->ops, NULL));
398 if (ret != EFI_SUCCESS)
399 return ret;
400
401
402
403
404
405 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
406 efi_fs_exists(desc, part)) {
407 diskobj->volume = efi_simple_file_system(desc, part,
408 diskobj->dp);
409 ret = efi_add_protocol(&diskobj->header,
410 &efi_simple_file_system_protocol_guid,
411 diskobj->volume);
412 if (ret != EFI_SUCCESS)
413 return ret;
414 }
415 diskobj->ops = block_io_disk_template;
416 diskobj->ifname = if_typename;
417 diskobj->dev_index = dev_index;
418 diskobj->offset = offset;
419 diskobj->desc = desc;
420
421
422 diskobj->media.removable_media = desc->removable;
423 diskobj->media.media_present = 1;
424
425
426
427
428 diskobj->media.media_id = 1;
429 diskobj->media.block_size = desc->blksz;
430 diskobj->media.io_align = desc->blksz;
431 diskobj->media.last_block = desc->lba - offset;
432 if (part)
433 diskobj->media.logical_partition = 1;
434 diskobj->ops.media = &diskobj->media;
435 if (disk)
436 *disk = diskobj;
437
438
439 if (part && !efi_system_partition.if_type) {
440 int r;
441 struct disk_partition info;
442
443 r = part_get_info(desc, part, &info);
444 if (r)
445 return EFI_DEVICE_ERROR;
446 if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
447 efi_system_partition.if_type = desc->if_type;
448 efi_system_partition.devnum = desc->devnum;
449 efi_system_partition.part = part;
450 EFI_PRINT("EFI system partition: %s %d:%d\n",
451 blk_get_if_type_name(desc->if_type),
452 desc->devnum, part);
453 }
454 }
455 return EFI_SUCCESS;
456}
457
458
459
460
461
462
463
464
465
466
467
468
469
470int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
471 const char *if_typename, int diskid,
472 const char *pdevname)
473{
474 int disks = 0;
475 char devname[32] = { 0 };
476 struct disk_partition info;
477 int part;
478 struct efi_device_path *dp = NULL;
479 efi_status_t ret;
480 struct efi_handler *handler;
481
482
483 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
484 if (ret == EFI_SUCCESS)
485 dp = handler->protocol_interface;
486
487
488 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
489 if (part_get_info(desc, part, &info))
490 continue;
491 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
492 part);
493 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
494 info.start, part, NULL);
495 if (ret != EFI_SUCCESS) {
496 log_err("Adding partition %s failed\n", pdevname);
497 continue;
498 }
499 disks++;
500 }
501
502 return disks;
503}
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520efi_status_t efi_disk_register(void)
521{
522 struct efi_disk_obj *disk;
523 int disks = 0;
524 efi_status_t ret;
525#ifdef CONFIG_BLK
526 struct udevice *dev;
527
528 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
529 uclass_next_device_check(&dev)) {
530 struct blk_desc *desc = dev_get_uclass_platdata(dev);
531 const char *if_typename = blk_get_if_type_name(desc->if_type);
532
533
534 log_info("Scanning disk %s...\n", dev->name);
535 ret = efi_disk_add_dev(NULL, NULL, if_typename,
536 desc, desc->devnum, 0, 0, &disk);
537 if (ret == EFI_NOT_READY) {
538 log_notice("Disk %s not ready\n", dev->name);
539 continue;
540 }
541 if (ret) {
542 log_err("ERROR: failure to add disk device %s, r = %lu\n",
543 dev->name, ret & ~EFI_ERROR_MASK);
544 return ret;
545 }
546 disks++;
547
548
549 disks += efi_disk_create_partitions(
550 &disk->header, desc, if_typename,
551 desc->devnum, dev->name);
552 }
553#else
554 int i, if_type;
555
556
557 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
558 const struct blk_driver *cur_drvr;
559 const char *if_typename;
560
561 cur_drvr = blk_driver_lookup_type(if_type);
562 if (!cur_drvr)
563 continue;
564
565 if_typename = cur_drvr->if_typename;
566 log_info("Scanning disks on %s...\n", if_typename);
567 for (i = 0; i < 4; i++) {
568 struct blk_desc *desc;
569 char devname[32] = { 0 };
570
571 desc = blk_get_devnum_by_type(if_type, i);
572 if (!desc)
573 continue;
574 if (desc->type == DEV_TYPE_UNKNOWN)
575 continue;
576
577 snprintf(devname, sizeof(devname), "%s%d",
578 if_typename, i);
579
580
581 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
582 i, 0, 0, &disk);
583 if (ret == EFI_NOT_READY) {
584 log_notice("Disk %s not ready\n", devname);
585 continue;
586 }
587 if (ret) {
588 log_err("ERROR: failure to add disk device %s, r = %lu\n",
589 devname, ret & ~EFI_ERROR_MASK);
590 return ret;
591 }
592 disks++;
593
594
595 disks += efi_disk_create_partitions
596 (&disk->header, desc,
597 if_typename, i, devname);
598 }
599 }
600#endif
601 log_info("Found %d disks\n", disks);
602
603 return EFI_SUCCESS;
604}
605
606
607
608
609
610
611
612
613bool efi_disk_is_system_part(efi_handle_t handle)
614{
615 struct efi_handler *handler;
616 struct efi_disk_obj *diskobj;
617 struct disk_partition info;
618 efi_status_t ret;
619 int r;
620
621
622 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
623 if (ret != EFI_SUCCESS)
624 return false;
625
626 diskobj = container_of(handle, struct efi_disk_obj, header);
627
628 r = part_get_info(diskobj->desc, diskobj->part, &info);
629 if (r)
630 return false;
631
632 return !!(info.bootable & PART_EFI_SYSTEM_PARTITION);
633}
634