]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_device_path.c
c1ba54e6bd54c38fe77dae03358c94ffaa28c46f
[u-boot] / lib / efi_loader / efi_device_path.c
1 /*
2  * EFI device path from u-boot device-model mapping
3  *
4  * (C) Copyright 2017 Rob Clark
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <blk.h>
11 #include <dm.h>
12 #include <usb.h>
13 #include <mmc.h>
14 #include <efi_loader.h>
15 #include <inttypes.h>
16 #include <part.h>
17
18 /* template END node: */
19 static const struct efi_device_path END = {
20         .type     = DEVICE_PATH_TYPE_END,
21         .sub_type = DEVICE_PATH_SUB_TYPE_END,
22         .length   = sizeof(END),
23 };
24
25 #define U_BOOT_GUID \
26         EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
27                  0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
28
29 /* template ROOT node: */
30 static const struct efi_device_path_vendor ROOT = {
31         .dp = {
32                 .type     = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
33                 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
34                 .length   = sizeof(ROOT),
35         },
36         .guid = U_BOOT_GUID,
37 };
38
39 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
40 /*
41  * Determine if an MMC device is an SD card.
42  *
43  * @desc        block device descriptor
44  * @return      true if the device is an SD card
45  */
46 static bool is_sd(struct blk_desc *desc)
47 {
48         struct mmc *mmc = find_mmc_device(desc->devnum);
49
50         if (!mmc)
51                 return false;
52
53         return IS_SD(mmc) != 0U;
54 }
55 #endif
56
57 static void *dp_alloc(size_t sz)
58 {
59         void *buf;
60
61         if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
62             EFI_SUCCESS) {
63                 debug("EFI: ERROR: out of memory in %s\n", __func__);
64                 return NULL;
65         }
66
67         return buf;
68 }
69
70 /*
71  * Iterate to next block in device-path, terminating (returning NULL)
72  * at /End* node.
73  */
74 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
75 {
76         if (dp == NULL)
77                 return NULL;
78         if (dp->type == DEVICE_PATH_TYPE_END)
79                 return NULL;
80         dp = ((void *)dp) + dp->length;
81         if (dp->type == DEVICE_PATH_TYPE_END)
82                 return NULL;
83         return (struct efi_device_path *)dp;
84 }
85
86 /*
87  * Compare two device-paths, stopping when the shorter of the two hits
88  * an End* node.  This is useful to, for example, compare a device-path
89  * representing a device with one representing a file on the device, or
90  * a device with a parent device.
91  */
92 int efi_dp_match(const struct efi_device_path *a,
93                  const struct efi_device_path *b)
94 {
95         while (1) {
96                 int ret;
97
98                 ret = memcmp(&a->length, &b->length, sizeof(a->length));
99                 if (ret)
100                         return ret;
101
102                 ret = memcmp(a, b, a->length);
103                 if (ret)
104                         return ret;
105
106                 a = efi_dp_next(a);
107                 b = efi_dp_next(b);
108
109                 if (!a || !b)
110                         return 0;
111         }
112 }
113
114
115 /*
116  * See UEFI spec (section 3.1.2, about short-form device-paths..
117  * tl;dr: we can have a device-path that starts with a USB WWID
118  * or USB Class node, and a few other cases which don't encode
119  * the full device path with bus hierarchy:
120  *
121  *   - MESSAGING:USB_WWID
122  *   - MESSAGING:USB_CLASS
123  *   - MEDIA:FILE_PATH
124  *   - MEDIA:HARD_DRIVE
125  *   - MESSAGING:URI
126  */
127 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
128 {
129         while (dp) {
130                 /*
131                  * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
132                  * in practice fallback.efi just uses MEDIA:HARD_DRIVE
133                  * so not sure when we would see these other cases.
134                  */
135                 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
136                     EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
137                     EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
138                         return dp;
139
140                 dp = efi_dp_next(dp);
141         }
142
143         return dp;
144 }
145
146 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
147                                    struct efi_device_path **rem)
148 {
149         struct efi_object *efiobj;
150         unsigned int dp_size = efi_dp_size(dp);
151
152         list_for_each_entry(efiobj, &efi_obj_list, link) {
153                 struct efi_handler *handler;
154                 struct efi_device_path *obj_dp;
155                 efi_status_t ret;
156
157                 ret = efi_search_protocol(efiobj->handle,
158                                           &efi_guid_device_path, &handler);
159                 if (ret != EFI_SUCCESS)
160                         continue;
161                 obj_dp = handler->protocol_interface;
162
163                 do {
164                         if (efi_dp_match(dp, obj_dp) == 0) {
165                                 if (rem) {
166                                         /*
167                                          * Allow partial matches, but inform
168                                          * the caller.
169                                          */
170                                         *rem = ((void *)dp) +
171                                                 efi_dp_size(obj_dp);
172                                         return efiobj;
173                                 } else {
174                                         /* Only return on exact matches */
175                                         if (efi_dp_size(obj_dp) == dp_size)
176                                                 return efiobj;
177                                 }
178                         }
179
180                         obj_dp = shorten_path(efi_dp_next(obj_dp));
181                 } while (short_path && obj_dp);
182         }
183
184         return NULL;
185 }
186
187
188 /*
189  * Find an efiobj from device-path, if 'rem' is not NULL, returns the
190  * remaining part of the device path after the matched object.
191  */
192 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
193                                    struct efi_device_path **rem)
194 {
195         struct efi_object *efiobj;
196
197         /* Search for an exact match first */
198         efiobj = find_obj(dp, false, NULL);
199
200         /* Then for a fuzzy match */
201         if (!efiobj)
202                 efiobj = find_obj(dp, false, rem);
203
204         /* And now for a fuzzy short match */
205         if (!efiobj)
206                 efiobj = find_obj(dp, true, rem);
207
208         return efiobj;
209 }
210
211 /* return size not including End node: */
212 unsigned efi_dp_size(const struct efi_device_path *dp)
213 {
214         unsigned sz = 0;
215
216         while (dp) {
217                 sz += dp->length;
218                 dp = efi_dp_next(dp);
219         }
220
221         return sz;
222 }
223
224 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
225 {
226         struct efi_device_path *ndp;
227         unsigned sz = efi_dp_size(dp) + sizeof(END);
228
229         if (!dp)
230                 return NULL;
231
232         ndp = dp_alloc(sz);
233         if (!ndp)
234                 return NULL;
235         memcpy(ndp, dp, sz);
236
237         return ndp;
238 }
239
240 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
241                                       const struct efi_device_path *dp2)
242 {
243         struct efi_device_path *ret;
244
245         if (!dp1) {
246                 ret = efi_dp_dup(dp2);
247         } else if (!dp2) {
248                 ret = efi_dp_dup(dp1);
249         } else {
250                 /* both dp1 and dp2 are non-null */
251                 unsigned sz1 = efi_dp_size(dp1);
252                 unsigned sz2 = efi_dp_size(dp2);
253                 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
254                 if (!p)
255                         return NULL;
256                 memcpy(p, dp1, sz1);
257                 memcpy(p + sz1, dp2, sz2);
258                 memcpy(p + sz1 + sz2, &END, sizeof(END));
259                 ret = p;
260         }
261
262         return ret;
263 }
264
265 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
266                                            const struct efi_device_path *node)
267 {
268         struct efi_device_path *ret;
269
270         if (!node && !dp) {
271                 ret = efi_dp_dup(&END);
272         } else if (!node) {
273                 ret = efi_dp_dup(dp);
274         } else if (!dp) {
275                 unsigned sz = node->length;
276                 void *p = dp_alloc(sz + sizeof(END));
277                 if (!p)
278                         return NULL;
279                 memcpy(p, node, sz);
280                 memcpy(p + sz, &END, sizeof(END));
281                 ret = p;
282         } else {
283                 /* both dp and node are non-null */
284                 unsigned sz = efi_dp_size(dp);
285                 void *p = dp_alloc(sz + node->length + sizeof(END));
286                 if (!p)
287                         return NULL;
288                 memcpy(p, dp, sz);
289                 memcpy(p + sz, node, node->length);
290                 memcpy(p + sz + node->length, &END, sizeof(END));
291                 ret = p;
292         }
293
294         return ret;
295 }
296
297 #ifdef CONFIG_DM
298 /* size of device-path not including END node for device and all parents
299  * up to the root device.
300  */
301 static unsigned dp_size(struct udevice *dev)
302 {
303         if (!dev || !dev->driver)
304                 return sizeof(ROOT);
305
306         switch (dev->driver->id) {
307         case UCLASS_ROOT:
308         case UCLASS_SIMPLE_BUS:
309                 /* stop traversing parents at this point: */
310                 return sizeof(ROOT);
311 #ifdef CONFIG_BLK
312         case UCLASS_BLK:
313                 switch (dev->parent->uclass->uc_drv->id) {
314 #ifdef CONFIG_IDE
315                 case UCLASS_IDE:
316                         return dp_size(dev->parent) +
317                                 sizeof(struct efi_device_path_atapi);
318 #endif
319 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
320                 case UCLASS_SCSI:
321                         return dp_size(dev->parent) +
322                                 sizeof(struct efi_device_path_scsi);
323 #endif
324                 default:
325                         return dp_size(dev->parent);
326                 }
327 #endif
328         case UCLASS_MMC:
329                 return dp_size(dev->parent) +
330                         sizeof(struct efi_device_path_sd_mmc_path);
331         case UCLASS_MASS_STORAGE:
332         case UCLASS_USB_HUB:
333                 return dp_size(dev->parent) +
334                         sizeof(struct efi_device_path_usb_class);
335         default:
336                 /* just skip over unknown classes: */
337                 return dp_size(dev->parent);
338         }
339 }
340
341 /*
342  * Recursively build a device path.
343  *
344  * @buf         pointer to the end of the device path
345  * @dev         device
346  * @return      pointer to the end of the device path
347  */
348 static void *dp_fill(void *buf, struct udevice *dev)
349 {
350         if (!dev || !dev->driver)
351                 return buf;
352
353         switch (dev->driver->id) {
354         case UCLASS_ROOT:
355         case UCLASS_SIMPLE_BUS: {
356                 /* stop traversing parents at this point: */
357                 struct efi_device_path_vendor *vdp = buf;
358                 *vdp = ROOT;
359                 return &vdp[1];
360         }
361 #ifdef CONFIG_BLK
362         case UCLASS_BLK:
363                 switch (dev->parent->uclass->uc_drv->id) {
364 #ifdef CONFIG_IDE
365                 case UCLASS_IDE: {
366                         struct efi_device_path_atapi *dp =
367                         dp_fill(buf, dev->parent);
368                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
369
370                         dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
371                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
372                         dp->dp.length = sizeof(*dp);
373                         dp->logical_unit_number = desc->devnum;
374                         dp->primary_secondary = IDE_BUS(desc->devnum);
375                         dp->slave_master = desc->devnum %
376                                 (CONFIG_SYS_IDE_MAXDEVICE /
377                                  CONFIG_SYS_IDE_MAXBUS);
378                         return &dp[1];
379                         }
380 #endif
381 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
382                 case UCLASS_SCSI: {
383                         struct efi_device_path_scsi *dp =
384                                 dp_fill(buf, dev->parent);
385                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
386
387                         dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
388                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
389                         dp->dp.length = sizeof(*dp);
390                         dp->logical_unit_number = desc->lun;
391                         dp->target_id = desc->target;
392                         return &dp[1];
393                         }
394 #endif
395                 default:
396                         printf("unhandled parent class: %s (%u)\n",
397                                dev->name, dev->driver->id);
398                         return dp_fill(buf, dev->parent);
399                 }
400 #endif
401 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
402         case UCLASS_MMC: {
403                 struct efi_device_path_sd_mmc_path *sddp =
404                         dp_fill(buf, dev->parent);
405                 struct mmc *mmc = mmc_get_mmc_dev(dev);
406                 struct blk_desc *desc = mmc_get_blk_desc(mmc);
407
408                 sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
409                 sddp->dp.sub_type = is_sd(desc) ?
410                         DEVICE_PATH_SUB_TYPE_MSG_SD :
411                         DEVICE_PATH_SUB_TYPE_MSG_MMC;
412                 sddp->dp.length   = sizeof(*sddp);
413                 sddp->slot_number = dev->seq;
414
415                 return &sddp[1];
416         }
417 #endif
418         case UCLASS_MASS_STORAGE:
419         case UCLASS_USB_HUB: {
420                 struct efi_device_path_usb_class *udp =
421                         dp_fill(buf, dev->parent);
422                 struct usb_device *udev = dev_get_parent_priv(dev);
423                 struct usb_device_descriptor *desc = &udev->descriptor;
424
425                 udp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
426                 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
427                 udp->dp.length   = sizeof(*udp);
428                 udp->vendor_id   = desc->idVendor;
429                 udp->product_id  = desc->idProduct;
430                 udp->device_class    = desc->bDeviceClass;
431                 udp->device_subclass = desc->bDeviceSubClass;
432                 udp->device_protocol = desc->bDeviceProtocol;
433
434                 return &udp[1];
435         }
436         default:
437                 debug("unhandled device class: %s (%u)\n",
438                       dev->name, dev->driver->id);
439                 return dp_fill(buf, dev->parent);
440         }
441 }
442
443 /* Construct a device-path from a device: */
444 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
445 {
446         void *buf, *start;
447
448         start = buf = dp_alloc(dp_size(dev) + sizeof(END));
449         if (!buf)
450                 return NULL;
451         buf = dp_fill(buf, dev);
452         *((struct efi_device_path *)buf) = END;
453
454         return start;
455 }
456 #endif
457
458 static unsigned dp_part_size(struct blk_desc *desc, int part)
459 {
460         unsigned dpsize;
461
462 #ifdef CONFIG_BLK
463         {
464                 struct udevice *dev;
465                 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
466
467                 if (ret)
468                         dev = desc->bdev->parent;
469                 dpsize = dp_size(dev);
470         }
471 #else
472         dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
473 #endif
474
475         if (part == 0) /* the actual disk, not a partition */
476                 return dpsize;
477
478         if (desc->part_type == PART_TYPE_ISO)
479                 dpsize += sizeof(struct efi_device_path_cdrom_path);
480         else
481                 dpsize += sizeof(struct efi_device_path_hard_drive_path);
482
483         return dpsize;
484 }
485
486 /*
487  * Create a device path for a block device or one of its partitions.
488  *
489  * @buf         buffer to which the device path is wirtten
490  * @desc        block device descriptor
491  * @part        partition number, 0 identifies a block device
492  */
493 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
494 {
495         disk_partition_t info;
496
497 #ifdef CONFIG_BLK
498         {
499                 struct udevice *dev;
500                 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
501
502                 if (ret)
503                         dev = desc->bdev->parent;
504                 buf = dp_fill(buf, dev);
505         }
506 #else
507         /*
508          * We *could* make a more accurate path, by looking at if_type
509          * and handling all the different cases like we do for non-
510          * legacy (ie CONFIG_BLK=y) case.  But most important thing
511          * is just to have a unique device-path for if_type+devnum.
512          * So map things to a fictitious USB device.
513          */
514         struct efi_device_path_usb *udp;
515
516         memcpy(buf, &ROOT, sizeof(ROOT));
517         buf += sizeof(ROOT);
518
519         udp = buf;
520         udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
521         udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
522         udp->dp.length = sizeof(*udp);
523         udp->parent_port_number = desc->if_type;
524         udp->usb_interface = desc->devnum;
525         buf = &udp[1];
526 #endif
527
528         if (part == 0) /* the actual disk, not a partition */
529                 return buf;
530
531         part_get_info(desc, part, &info);
532
533         if (desc->part_type == PART_TYPE_ISO) {
534                 struct efi_device_path_cdrom_path *cddp = buf;
535
536                 cddp->boot_entry = part;
537                 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
538                 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
539                 cddp->dp.length = sizeof(*cddp);
540                 cddp->partition_start = info.start;
541                 cddp->partition_end = info.size;
542
543                 buf = &cddp[1];
544         } else {
545                 struct efi_device_path_hard_drive_path *hddp = buf;
546
547                 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
548                 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
549                 hddp->dp.length = sizeof(*hddp);
550                 hddp->partition_number = part;
551                 hddp->partition_start = info.start;
552                 hddp->partition_end = info.size;
553                 if (desc->part_type == PART_TYPE_EFI)
554                         hddp->partmap_type = 2;
555                 else
556                         hddp->partmap_type = 1;
557
558                 switch (desc->sig_type) {
559                 case SIG_TYPE_NONE:
560                 default:
561                         hddp->signature_type = 0;
562                         memset(hddp->partition_signature, 0,
563                                sizeof(hddp->partition_signature));
564                         break;
565                 case SIG_TYPE_MBR:
566                         hddp->signature_type = 1;
567                         memset(hddp->partition_signature, 0,
568                                sizeof(hddp->partition_signature));
569                         memcpy(hddp->partition_signature, &desc->mbr_sig,
570                                sizeof(desc->mbr_sig));
571                         break;
572                 case SIG_TYPE_GUID:
573                         hddp->signature_type = 2;
574                         memcpy(hddp->partition_signature, &desc->guid_sig,
575                                sizeof(hddp->partition_signature));
576                         break;
577                 }
578
579                 buf = &hddp[1];
580         }
581
582         return buf;
583 }
584
585
586 /* Construct a device-path from a partition on a blk device: */
587 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
588 {
589         void *buf, *start;
590
591         start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
592         if (!buf)
593                 return NULL;
594
595         buf = dp_part_fill(buf, desc, part);
596
597         *((struct efi_device_path *)buf) = END;
598
599         return start;
600 }
601
602 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
603 static void path_to_uefi(u16 *uefi, const char *path)
604 {
605         while (*path) {
606                 char c = *(path++);
607                 if (c == '/')
608                         c = '\\';
609                 *(uefi++) = c;
610         }
611         *uefi = '\0';
612 }
613
614 /*
615  * If desc is NULL, this creates a path with only the file component,
616  * otherwise it creates a full path with both device and file components
617  */
618 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
619                 const char *path)
620 {
621         struct efi_device_path_file_path *fp;
622         void *buf, *start;
623         unsigned dpsize = 0, fpsize;
624
625         if (desc)
626                 dpsize = dp_part_size(desc, part);
627
628         fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
629         dpsize += fpsize;
630
631         start = buf = dp_alloc(dpsize + sizeof(END));
632         if (!buf)
633                 return NULL;
634
635         if (desc)
636                 buf = dp_part_fill(buf, desc, part);
637
638         /* add file-path: */
639         fp = buf;
640         fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
641         fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
642         fp->dp.length = fpsize;
643         path_to_uefi(fp->str, path);
644         buf += fpsize;
645
646         *((struct efi_device_path *)buf) = END;
647
648         return start;
649 }
650
651 #ifdef CONFIG_NET
652 struct efi_device_path *efi_dp_from_eth(void)
653 {
654         struct efi_device_path_mac_addr *ndp;
655         void *buf, *start;
656         unsigned dpsize = 0;
657
658         assert(eth_get_dev());
659
660 #ifdef CONFIG_DM_ETH
661         dpsize += dp_size(eth_get_dev());
662 #else
663         dpsize += sizeof(ROOT);
664 #endif
665         dpsize += sizeof(*ndp);
666
667         start = buf = dp_alloc(dpsize + sizeof(END));
668         if (!buf)
669                 return NULL;
670
671 #ifdef CONFIG_DM_ETH
672         buf = dp_fill(buf, eth_get_dev());
673 #else
674         memcpy(buf, &ROOT, sizeof(ROOT));
675         buf += sizeof(ROOT);
676 #endif
677
678         ndp = buf;
679         ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
680         ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
681         ndp->dp.length = sizeof(*ndp);
682         memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
683         buf = &ndp[1];
684
685         *((struct efi_device_path *)buf) = END;
686
687         return start;
688 }
689 #endif
690
691 /* Construct a device-path for memory-mapped image */
692 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
693                                         uint64_t start_address,
694                                         uint64_t end_address)
695 {
696         struct efi_device_path_memory *mdp;
697         void *buf, *start;
698
699         start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
700         if (!buf)
701                 return NULL;
702
703         mdp = buf;
704         mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
705         mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
706         mdp->dp.length = sizeof(*mdp);
707         mdp->memory_type = memory_type;
708         mdp->start_address = start_address;
709         mdp->end_address = end_address;
710         buf = &mdp[1];
711
712         *((struct efi_device_path *)buf) = END;
713
714         return start;
715 }
716
717 /*
718  * Helper to split a full device path (containing both device and file
719  * parts) into it's constituent parts.
720  */
721 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
722                                     struct efi_device_path **device_path,
723                                     struct efi_device_path **file_path)
724 {
725         struct efi_device_path *p, *dp, *fp;
726
727         *device_path = NULL;
728         *file_path = NULL;
729         dp = efi_dp_dup(full_path);
730         if (!dp)
731                 return EFI_OUT_OF_RESOURCES;
732         p = dp;
733         while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
734                 p = efi_dp_next(p);
735                 if (!p)
736                         return EFI_OUT_OF_RESOURCES;
737         }
738         fp = efi_dp_dup(p);
739         if (!fp)
740                 return EFI_OUT_OF_RESOURCES;
741         p->type = DEVICE_PATH_TYPE_END;
742         p->sub_type = DEVICE_PATH_SUB_TYPE_END;
743         p->length = sizeof(*p);
744
745         *device_path = dp;
746         *file_path = fp;
747         return EFI_SUCCESS;
748 }