2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/diskmbr.h>
33 #include <sys/disklabel.h>
34 #include <sys/endian.h>
36 #include <sys/stddef.h>
37 #include <sys/queue.h>
45 #define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
47 #define DEBUG(fmt, args...)
50 #ifdef LOADER_GPT_SUPPORT
52 static const uuid_t gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
53 static const uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
54 static const uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
55 static const uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI;
56 static const uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
57 static const uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
58 static const uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
59 static const uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
60 static const uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
64 struct ptable_entry part;
72 STAILQ_ENTRY(pentry) entry;
76 enum ptable_type type;
80 STAILQ_HEAD(, pentry) entries;
83 static struct parttypes {
84 enum partition_type type;
87 { PART_UNKNOWN, "Unknown" },
89 { PART_FREEBSD, "FreeBSD" },
90 { PART_FREEBSD_BOOT, "FreeBSD boot" },
91 { PART_FREEBSD_NANDFS, "FreeBSD nandfs" },
92 { PART_FREEBSD_UFS, "FreeBSD UFS" },
93 { PART_FREEBSD_ZFS, "FreeBSD ZFS" },
94 { PART_FREEBSD_SWAP, "FreeBSD swap" },
95 { PART_FREEBSD_VINUM, "FreeBSD vinum" },
96 { PART_LINUX, "Linux" },
97 { PART_LINUX_SWAP, "Linux swap" },
98 { PART_DOS, "DOS/Windows" },
102 parttype2str(enum partition_type type)
106 for (i = 0; i < sizeof(ptypes) / sizeof(ptypes[0]); i++)
107 if (ptypes[i].type == type)
108 return (ptypes[i].desc);
109 return (ptypes[0].desc);
112 #ifdef LOADER_GPT_SUPPORT
114 uuid_letoh(uuid_t *uuid)
117 uuid->time_low = le32toh(uuid->time_low);
118 uuid->time_mid = le16toh(uuid->time_mid);
119 uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
122 static enum partition_type
123 gpt_parttype(uuid_t type)
126 if (uuid_equal(&type, &gpt_uuid_efi, NULL))
128 else if (uuid_equal(&type, &gpt_uuid_ms_basic_data, NULL))
130 else if (uuid_equal(&type, &gpt_uuid_freebsd_boot, NULL))
131 return (PART_FREEBSD_BOOT);
132 else if (uuid_equal(&type, &gpt_uuid_freebsd_ufs, NULL))
133 return (PART_FREEBSD_UFS);
134 else if (uuid_equal(&type, &gpt_uuid_freebsd_zfs, NULL))
135 return (PART_FREEBSD_ZFS);
136 else if (uuid_equal(&type, &gpt_uuid_freebsd_swap, NULL))
137 return (PART_FREEBSD_SWAP);
138 else if (uuid_equal(&type, &gpt_uuid_freebsd_vinum, NULL))
139 return (PART_FREEBSD_VINUM);
140 else if (uuid_equal(&type, &gpt_uuid_freebsd_nandfs, NULL))
141 return (PART_FREEBSD_NANDFS);
142 return (PART_UNKNOWN);
145 static struct gpt_hdr*
146 gpt_checkhdr(struct gpt_hdr *hdr, uint64_t lba_self, uint64_t lba_last,
151 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0) {
152 DEBUG("no GPT signature");
155 sz = le32toh(hdr->hdr_size);
156 if (sz < 92 || sz > sectorsize) {
157 DEBUG("invalid GPT header size: %d", sz);
160 crc = le32toh(hdr->hdr_crc_self);
161 hdr->hdr_crc_self = 0;
162 if (crc32(hdr, sz) != crc) {
163 DEBUG("GPT header's CRC doesn't match");
166 hdr->hdr_crc_self = crc;
167 hdr->hdr_revision = le32toh(hdr->hdr_revision);
168 if (hdr->hdr_revision < GPT_HDR_REVISION) {
169 DEBUG("unsupported GPT revision %d", hdr->hdr_revision);
172 hdr->hdr_lba_self = le64toh(hdr->hdr_lba_self);
173 if (hdr->hdr_lba_self != lba_self) {
174 DEBUG("self LBA doesn't match");
177 hdr->hdr_lba_alt = le64toh(hdr->hdr_lba_alt);
178 if (hdr->hdr_lba_alt == hdr->hdr_lba_self) {
179 DEBUG("invalid alternate LBA");
182 hdr->hdr_entries = le32toh(hdr->hdr_entries);
183 hdr->hdr_entsz = le32toh(hdr->hdr_entsz);
184 if (hdr->hdr_entries == 0 ||
185 hdr->hdr_entsz < sizeof(struct gpt_ent) ||
186 sectorsize % hdr->hdr_entsz != 0) {
187 DEBUG("invalid entry size or number of entries");
190 hdr->hdr_lba_start = le64toh(hdr->hdr_lba_start);
191 hdr->hdr_lba_end = le64toh(hdr->hdr_lba_end);
192 hdr->hdr_lba_table = le64toh(hdr->hdr_lba_table);
193 hdr->hdr_crc_table = le32toh(hdr->hdr_crc_table);
194 uuid_letoh(&hdr->hdr_uuid);
199 gpt_checktbl(const struct gpt_hdr *hdr, u_char *tbl, size_t size,
205 cnt = size / hdr->hdr_entsz;
206 if (hdr->hdr_entries <= cnt) {
207 cnt = hdr->hdr_entries;
208 /* Check CRC only when buffer size is enough for table. */
209 if (hdr->hdr_crc_table !=
210 crc32(tbl, hdr->hdr_entries * hdr->hdr_entsz)) {
211 DEBUG("GPT table's CRC doesn't match");
215 ent = (struct gpt_ent *)tbl;
216 for (i = 0; i < cnt; i++, ent++) {
217 uuid_letoh(&ent->ent_type);
218 if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
220 ent->ent_lba_start = le64toh(ent->ent_lba_start);
221 ent->ent_lba_end = le64toh(ent->ent_lba_end);
226 static struct ptable*
227 ptable_gptread(struct ptable *table, void *dev, diskread_t dread)
229 struct pentry *entry;
230 struct gpt_hdr *phdr, hdr;
237 buf = malloc(table->sectorsize);
240 tbl = malloc(table->sectorsize * MAXTBLSZ);
245 /* Read the primary GPT header. */
246 if (dread(dev, buf, 1, 1) != 0) {
252 /* Check the primary GPT header. */
253 phdr = gpt_checkhdr((struct gpt_hdr *)buf, 1, table->sectors - 1,
256 /* Read the primary GPT table. */
258 phdr->hdr_entries * phdr->hdr_entsz / table->sectorsize);
259 if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
260 gpt_checktbl(phdr, tbl, size * table->sectorsize,
261 table->sectors - 1) == 0) {
262 memcpy(&hdr, phdr, sizeof(hdr));
266 offset = pri ? hdr.hdr_lba_alt: table->sectors - 1;
267 /* Read the backup GPT header. */
268 if (dread(dev, buf, 1, offset) != 0)
271 phdr = gpt_checkhdr((struct gpt_hdr *)buf, offset,
272 table->sectors - 1, table->sectorsize);
275 * Compare primary and backup headers.
276 * If they are equal, then we do not need to read backup
277 * table. If they are different, then prefer backup header
278 * and try to read backup table.
281 uuid_equal(&hdr.hdr_uuid, &phdr->hdr_uuid, NULL) == 0 ||
282 hdr.hdr_revision != phdr->hdr_revision ||
283 hdr.hdr_size != phdr->hdr_size ||
284 hdr.hdr_lba_start != phdr->hdr_lba_start ||
285 hdr.hdr_lba_end != phdr->hdr_lba_end ||
286 hdr.hdr_entries != phdr->hdr_entries ||
287 hdr.hdr_entsz != phdr->hdr_entsz ||
288 hdr.hdr_crc_table != phdr->hdr_crc_table) {
289 /* Read the backup GPT table. */
290 size = MIN(MAXTBLSZ, phdr->hdr_entries *
291 phdr->hdr_entsz / table->sectorsize);
292 if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
293 gpt_checktbl(phdr, tbl, size * table->sectorsize,
294 table->sectors - 1) == 0) {
295 memcpy(&hdr, phdr, sizeof(hdr));
300 if (pri == 0 && sec == 0) {
301 /* Both primary and backup tables are invalid. */
302 table->type = PTABLE_NONE;
305 ent = (struct gpt_ent *)tbl;
306 size = MIN(hdr.hdr_entries * hdr.hdr_entsz,
307 MAXTBLSZ * table->sectorsize);
308 for (i = 0; i < size / hdr.hdr_entsz; i++, ent++) {
309 if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
311 entry = malloc(sizeof(*entry));
314 entry->part.start = ent->ent_lba_start;
315 entry->part.end = ent->ent_lba_end;
316 entry->part.index = i + 1;
317 entry->part.type = gpt_parttype(ent->ent_type);
318 entry->flags = le64toh(ent->ent_attr);
319 memcpy(&entry->type.gpt, &ent->ent_type, sizeof(uuid_t));
320 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
321 DEBUG("new GPT partition added");
328 #endif /* LOADER_GPT_SUPPORT */
330 #ifdef LOADER_MBR_SUPPORT
331 /* We do not need to support too many EBR partitions in the loader */
332 #define MAXEBRENTRIES 8
333 static enum partition_type
334 mbr_parttype(uint8_t type)
339 return (PART_FREEBSD);
341 return (PART_LINUX_SWAP);
353 return (PART_UNKNOWN);
357 ptable_ebrread(struct ptable *table, void *dev, diskread_t dread)
359 struct dos_partition *dp;
360 struct pentry *e1, *entry;
361 uint32_t start, end, offset;
365 STAILQ_FOREACH(e1, &table->entries, entry) {
366 if (e1->type.mbr == DOSPTYP_EXT ||
367 e1->type.mbr == DOSPTYP_EXTLBA)
373 offset = e1->part.start;
374 buf = malloc(table->sectorsize);
377 for (i = 0; i < MAXEBRENTRIES; i++) {
378 #if 0 /* Some BIOSes return an incorrect number of sectors */
379 if (offset >= table->sectors)
382 if (dread(dev, buf, 1, offset) != 0)
384 dp = (struct dos_partition *)(buf + DOSPARTOFF);
385 if (dp[0].dp_typ == 0)
387 start = le32toh(dp[0].dp_start);
388 if (dp[0].dp_typ == DOSPTYP_EXT &&
390 offset = e1->part.start + start;
393 end = le32toh(dp[0].dp_size);
394 entry = malloc(sizeof(*entry));
397 entry->part.start = offset + start;
398 entry->part.end = entry->part.start + end - 1;
399 entry->part.index = index++;
400 entry->part.type = mbr_parttype(dp[0].dp_typ);
401 entry->flags = dp[0].dp_flag;
402 entry->type.mbr = dp[0].dp_typ;
403 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
404 DEBUG("new EBR partition added");
405 if (dp[1].dp_typ == 0)
407 offset = e1->part.start + le32toh(dp[1].dp_start);
412 #endif /* LOADER_MBR_SUPPORT */
414 static enum partition_type
415 bsd_parttype(uint8_t type)
420 return (PART_FREEBSD_NANDFS);
422 return (PART_FREEBSD_SWAP);
424 return (PART_FREEBSD_UFS);
426 return (PART_FREEBSD_VINUM);
428 return (PART_FREEBSD_ZFS);
430 return (PART_UNKNOWN);
434 ptable_bsdread(struct ptable *table, void *dev, diskread_t dread)
436 struct disklabel *dl;
437 struct partition *part;
438 struct pentry *entry;
443 if (table->sectorsize < sizeof(struct disklabel)) {
444 DEBUG("Too small sectorsize");
447 buf = malloc(table->sectorsize);
450 if (dread(dev, buf, 1, 1) != 0) {
451 DEBUG("read failed");
456 dl = (struct disklabel *)buf;
457 if (le32toh(dl->d_magic) != DISKMAGIC &&
458 le32toh(dl->d_magic2) != DISKMAGIC)
460 if (le32toh(dl->d_secsize) != table->sectorsize) {
461 DEBUG("unsupported sector size");
464 dl->d_npartitions = le16toh(dl->d_npartitions);
465 if (dl->d_npartitions > 20 || dl->d_npartitions < 8) {
466 DEBUG("invalid number of partitions");
469 part = &dl->d_partitions[0];
470 raw_offset = le32toh(part[RAW_PART].p_offset);
471 for (i = 0; i < dl->d_npartitions; i++, part++) {
474 if (part->p_size == 0)
476 entry = malloc(sizeof(*entry));
479 entry->part.start = le32toh(part->p_offset) - raw_offset;
480 entry->part.end = entry->part.start +
481 le32toh(part->p_size) + 1;
482 entry->part.type = bsd_parttype(part->p_fstype);
483 entry->part.index = i; /* starts from zero */
484 entry->type.bsd = part->p_fstype;
485 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
486 DEBUG("new BSD partition added");
488 table->type = PTABLE_BSD;
494 #ifdef LOADER_VTOC8_SUPPORT
495 static enum partition_type
496 vtoc8_parttype(uint16_t type)
500 case VTOC_TAG_FREEBSD_NANDFS:
501 return (PART_FREEBSD_NANDFS);
502 case VTOC_TAG_FREEBSD_SWAP:
503 return (PART_FREEBSD_SWAP);
504 case VTOC_TAG_FREEBSD_UFS:
505 return (PART_FREEBSD_UFS);
506 case VTOC_TAG_FREEBSD_VINUM:
507 return (PART_FREEBSD_VINUM);
508 case VTOC_TAG_FREEBSD_ZFS:
509 return (PART_FREEBSD_ZFS);
511 return (PART_UNKNOWN);
514 static struct ptable*
515 ptable_vtoc8read(struct ptable *table, void *dev, diskread_t dread)
517 struct pentry *entry;
520 uint16_t sum, heads, sectors;
523 if (table->sectorsize != sizeof(struct vtoc8))
525 buf = malloc(table->sectorsize);
528 if (dread(dev, buf, 1, 0) != 0) {
529 DEBUG("read failed");
534 dl = (struct vtoc8 *)buf;
536 for (i = sum = 0; i < sizeof(struct vtoc8); i += sizeof(sum))
537 sum ^= be16dec(buf + i);
539 DEBUG("incorrect checksum");
542 if (be16toh(dl->nparts) != VTOC8_NPARTS) {
543 DEBUG("invalid number of entries");
546 sectors = be16toh(dl->nsecs);
547 heads = be16toh(dl->nheads);
548 if (sectors * heads == 0) {
549 DEBUG("invalid geometry");
552 for (i = 0; i < VTOC8_NPARTS; i++) {
553 dl->part[i].tag = be16toh(dl->part[i].tag);
554 if (i == VTOC_RAW_PART ||
555 dl->part[i].tag == VTOC_TAG_UNASSIGNED)
557 entry = malloc(sizeof(*entry));
560 entry->part.start = be32toh(dl->map[i].cyl) * heads * sectors;
561 entry->part.end = be32toh(dl->map[i].nblks) +
562 entry->part.start - 1;
563 entry->part.type = vtoc8_parttype(dl->part[i].tag);
564 entry->part.index = i; /* starts from zero */
565 entry->type.vtoc8 = dl->part[i].tag;
566 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
567 DEBUG("new VTOC8 partition added");
569 table->type = PTABLE_VTOC8;
575 #endif /* LOADER_VTOC8_SUPPORT */
578 ptable_open(void *dev, off_t sectors, uint16_t sectorsize,
581 struct dos_partition *dp;
582 struct ptable *table;
585 #ifdef LOADER_MBR_SUPPORT
586 struct pentry *entry;
591 buf = malloc(sectorsize);
594 /* First, read the MBR. */
595 if (dread(dev, buf, 1, DOSBBSECTOR) != 0) {
596 DEBUG("read failed");
600 table = malloc(sizeof(*table));
603 table->sectors = sectors;
604 table->sectorsize = sectorsize;
605 table->type = PTABLE_NONE;
606 STAILQ_INIT(&table->entries);
608 #ifdef LOADER_VTOC8_SUPPORT
609 if (be16dec(buf + offsetof(struct vtoc8, magic)) == VTOC_MAGIC) {
610 if (ptable_vtoc8read(table, dev, dread) == NULL) {
614 } else if (table->type == PTABLE_VTOC8)
618 /* Check the BSD label. */
619 if (ptable_bsdread(table, dev, dread) == NULL) { /* Read error. */
622 } else if (table->type == PTABLE_BSD)
625 #if defined(LOADER_GPT_SUPPORT) || defined(LOADER_MBR_SUPPORT)
626 /* Check the MBR magic. */
627 if (buf[DOSMAGICOFFSET] != 0x55 ||
628 buf[DOSMAGICOFFSET + 1] != 0xaa) {
629 DEBUG("magic sequence not found");
632 /* Check that we have PMBR. Also do some validation. */
633 dp = (struct dos_partition *)(buf + DOSPARTOFF);
634 for (i = 0, count = 0; i < NDOSPART; i++) {
635 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) {
636 DEBUG("invalid partition flag %x", dp[i].dp_flag);
639 #ifdef LOADER_GPT_SUPPORT
640 if (dp[i].dp_typ == DOSPTYP_PMBR) {
641 table->type = PTABLE_GPT;
642 DEBUG("PMBR detected");
645 if (dp[i].dp_typ != 0)
648 /* Do we have some invalid values? */
650 (table->type == PTABLE_GPT && count > 1)) {
651 if (dp[1].dp_typ != DOSPTYP_HFS) {
652 table->type = PTABLE_NONE;
653 DEBUG("invalid values detected, ignore "
657 DEBUG("Bootcamp detected");
659 #ifdef LOADER_GPT_SUPPORT
660 if (table->type == PTABLE_GPT) {
661 table = ptable_gptread(table, dev, dread);
665 #ifdef LOADER_MBR_SUPPORT
667 table->type = PTABLE_MBR;
668 for (i = has_ext = 0; i < NDOSPART; i++) {
669 if (dp[i].dp_typ == 0)
671 start = le32dec(&(dp[i].dp_start));
672 end = le32dec(&(dp[i].dp_size));
673 if (start == 0 || end == 0)
675 #if 0 /* Some BIOSes return an incorrect number of sectors */
676 if (start + end - 1 >= sectors)
677 continue; /* XXX: ignore */
679 if (dp[i].dp_typ == DOSPTYP_EXT ||
680 dp[i].dp_typ == DOSPTYP_EXTLBA)
682 entry = malloc(sizeof(*entry));
685 entry->part.start = start;
686 entry->part.end = start + end - 1;
687 entry->part.index = i + 1;
688 entry->part.type = mbr_parttype(dp[i].dp_typ);
689 entry->flags = dp[i].dp_flag;
690 entry->type.mbr = dp[i].dp_typ;
691 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
692 DEBUG("new MBR partition added");
695 table = ptable_ebrread(table, dev, dread);
698 #endif /* LOADER_MBR_SUPPORT */
699 #endif /* LOADER_MBR_SUPPORT || LOADER_GPT_SUPPORT */
706 ptable_close(struct ptable *table)
708 struct pentry *entry;
710 while (!STAILQ_EMPTY(&table->entries)) {
711 entry = STAILQ_FIRST(&table->entries);
712 STAILQ_REMOVE_HEAD(&table->entries, entry);
719 ptable_gettype(const struct ptable *table)
722 return (table->type);
726 ptable_getpart(const struct ptable *table, struct ptable_entry *part, int index)
728 struct pentry *entry;
730 if (part == NULL || table == NULL)
733 STAILQ_FOREACH(entry, &table->entries, entry) {
734 if (entry->part.index != index)
736 memcpy(part, &entry->part, sizeof(*part));
743 * Search for a slice with the following preferences:
745 * 1: Active FreeBSD slice
746 * 2: Non-active FreeBSD slice
747 * 3: Active Linux slice
748 * 4: non-active Linux slice
749 * 5: Active FAT/FAT32 slice
750 * 6: non-active FAT/FAT32 slice
752 #define PREF_RAWDISK 0
753 #define PREF_FBSD_ACT 1
755 #define PREF_LINUX_ACT 3
757 #define PREF_DOS_ACT 5
761 ptable_getbestpart(const struct ptable *table, struct ptable_entry *part)
763 struct pentry *entry, *best;
766 if (part == NULL || table == NULL)
770 preflevel = pref = PREF_NONE;
771 STAILQ_FOREACH(entry, &table->entries, entry) {
772 #ifdef LOADER_MBR_SUPPORT
773 if (table->type == PTABLE_MBR) {
774 switch (entry->type.mbr) {
776 pref = entry->flags & 0x80 ? PREF_FBSD_ACT:
780 pref = entry->flags & 0x80 ? PREF_LINUX_ACT:
783 case 0x01: /* DOS/Windows */
789 pref = entry->flags & 0x80 ? PREF_DOS_ACT:
796 #endif /* LOADER_MBR_SUPPORT */
797 #ifdef LOADER_GPT_SUPPORT
798 if (table->type == PTABLE_GPT) {
799 if (entry->part.type == PART_DOS)
801 else if (entry->part.type == PART_FREEBSD_UFS ||
802 entry->part.type == PART_FREEBSD_ZFS)
807 #endif /* LOADER_GPT_SUPPORT */
808 if (pref < preflevel) {
814 memcpy(part, &best->part, sizeof(*part));
821 ptable_iterate(const struct ptable *table, void *arg, ptable_iterate_t *iter)
823 struct pentry *entry;
827 STAILQ_FOREACH(entry, &table->entries, entry) {
828 #ifdef LOADER_MBR_SUPPORT
829 if (table->type == PTABLE_MBR)
830 sprintf(name, "s%d", entry->part.index);
833 #ifdef LOADER_GPT_SUPPORT
834 if (table->type == PTABLE_GPT)
835 sprintf(name, "p%d", entry->part.index);
838 #ifdef LOADER_VTOC8_SUPPORT
839 if (table->type == PTABLE_VTOC8)
840 sprintf(name, "%c", (u_char) 'a' +
844 if (table->type == PTABLE_BSD)
845 sprintf(name, "%c", (u_char) 'a' +
847 iter(arg, name, &entry->part);