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