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 = GPT_ENT_TYPE_FREEBSD;
57 static const uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
58 static const uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
59 static const uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
60 static const uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
61 static const uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
65 struct ptable_entry part;
73 STAILQ_ENTRY(pentry) entry;
77 enum ptable_type type;
81 STAILQ_HEAD(, pentry) entries;
84 static struct parttypes {
85 enum partition_type type;
88 { PART_UNKNOWN, "Unknown" },
90 { PART_FREEBSD, "FreeBSD" },
91 { PART_FREEBSD_BOOT, "FreeBSD boot" },
92 { PART_FREEBSD_NANDFS, "FreeBSD nandfs" },
93 { PART_FREEBSD_UFS, "FreeBSD UFS" },
94 { PART_FREEBSD_ZFS, "FreeBSD ZFS" },
95 { PART_FREEBSD_SWAP, "FreeBSD swap" },
96 { PART_FREEBSD_VINUM, "FreeBSD vinum" },
97 { PART_LINUX, "Linux" },
98 { PART_LINUX_SWAP, "Linux swap" },
99 { PART_DOS, "DOS/Windows" },
103 parttype2str(enum partition_type type)
107 for (i = 0; i < nitems(ptypes); i++)
108 if (ptypes[i].type == type)
109 return (ptypes[i].desc);
110 return (ptypes[0].desc);
113 #ifdef LOADER_GPT_SUPPORT
115 uuid_letoh(uuid_t *uuid)
118 uuid->time_low = le32toh(uuid->time_low);
119 uuid->time_mid = le16toh(uuid->time_mid);
120 uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
123 static enum partition_type
124 gpt_parttype(uuid_t type)
127 if (uuid_equal(&type, &gpt_uuid_efi, NULL))
129 else if (uuid_equal(&type, &gpt_uuid_ms_basic_data, NULL))
131 else if (uuid_equal(&type, &gpt_uuid_freebsd_boot, NULL))
132 return (PART_FREEBSD_BOOT);
133 else if (uuid_equal(&type, &gpt_uuid_freebsd_ufs, NULL))
134 return (PART_FREEBSD_UFS);
135 else if (uuid_equal(&type, &gpt_uuid_freebsd_zfs, NULL))
136 return (PART_FREEBSD_ZFS);
137 else if (uuid_equal(&type, &gpt_uuid_freebsd_swap, NULL))
138 return (PART_FREEBSD_SWAP);
139 else if (uuid_equal(&type, &gpt_uuid_freebsd_vinum, NULL))
140 return (PART_FREEBSD_VINUM);
141 else if (uuid_equal(&type, &gpt_uuid_freebsd_nandfs, NULL))
142 return (PART_FREEBSD_NANDFS);
143 else if (uuid_equal(&type, &gpt_uuid_freebsd, NULL))
144 return (PART_FREEBSD);
145 return (PART_UNKNOWN);
148 static struct gpt_hdr *
149 gpt_checkhdr(struct gpt_hdr *hdr, uint64_t lba_self, uint64_t lba_last,
154 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0) {
155 DEBUG("no GPT signature");
158 sz = le32toh(hdr->hdr_size);
159 if (sz < 92 || sz > sectorsize) {
160 DEBUG("invalid GPT header size: %d", sz);
163 crc = le32toh(hdr->hdr_crc_self);
164 hdr->hdr_crc_self = 0;
165 if (crc32(hdr, sz) != crc) {
166 DEBUG("GPT header's CRC doesn't match");
169 hdr->hdr_crc_self = crc;
170 hdr->hdr_revision = le32toh(hdr->hdr_revision);
171 if (hdr->hdr_revision < GPT_HDR_REVISION) {
172 DEBUG("unsupported GPT revision %d", hdr->hdr_revision);
175 hdr->hdr_lba_self = le64toh(hdr->hdr_lba_self);
176 if (hdr->hdr_lba_self != lba_self) {
177 DEBUG("self LBA doesn't match");
180 hdr->hdr_lba_alt = le64toh(hdr->hdr_lba_alt);
181 if (hdr->hdr_lba_alt == hdr->hdr_lba_self) {
182 DEBUG("invalid alternate LBA");
185 hdr->hdr_entries = le32toh(hdr->hdr_entries);
186 hdr->hdr_entsz = le32toh(hdr->hdr_entsz);
187 if (hdr->hdr_entries == 0 ||
188 hdr->hdr_entsz < sizeof(struct gpt_ent) ||
189 sectorsize % hdr->hdr_entsz != 0) {
190 DEBUG("invalid entry size or number of entries");
193 hdr->hdr_lba_start = le64toh(hdr->hdr_lba_start);
194 hdr->hdr_lba_end = le64toh(hdr->hdr_lba_end);
195 hdr->hdr_lba_table = le64toh(hdr->hdr_lba_table);
196 hdr->hdr_crc_table = le32toh(hdr->hdr_crc_table);
197 uuid_letoh(&hdr->hdr_uuid);
202 gpt_checktbl(const struct gpt_hdr *hdr, uint8_t *tbl, size_t size,
208 cnt = size / hdr->hdr_entsz;
209 if (hdr->hdr_entries <= cnt) {
210 cnt = hdr->hdr_entries;
211 /* Check CRC only when buffer size is enough for table. */
212 if (hdr->hdr_crc_table !=
213 crc32(tbl, hdr->hdr_entries * hdr->hdr_entsz)) {
214 DEBUG("GPT table's CRC doesn't match");
218 for (i = 0; i < cnt; i++) {
219 ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
220 uuid_letoh(&ent->ent_type);
221 if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
223 ent->ent_lba_start = le64toh(ent->ent_lba_start);
224 ent->ent_lba_end = le64toh(ent->ent_lba_end);
229 static struct ptable *
230 ptable_gptread(struct ptable *table, void *dev, diskread_t dread)
232 struct pentry *entry;
233 struct gpt_hdr *phdr, hdr;
240 buf = malloc(table->sectorsize);
243 tbl = malloc(table->sectorsize * MAXTBLSZ);
248 /* Read the primary GPT header. */
249 if (dread(dev, buf, 1, 1) != 0) {
255 /* Check the primary GPT header. */
256 phdr = gpt_checkhdr((struct gpt_hdr *)buf, 1, table->sectors - 1,
259 /* Read the primary GPT table. */
261 howmany(phdr->hdr_entries * phdr->hdr_entsz,
263 if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
264 gpt_checktbl(phdr, tbl, size * table->sectorsize,
265 table->sectors - 1) == 0) {
266 memcpy(&hdr, phdr, sizeof(hdr));
270 offset = pri ? hdr.hdr_lba_alt: table->sectors - 1;
271 /* Read the backup GPT header. */
272 if (dread(dev, buf, 1, offset) != 0)
275 phdr = gpt_checkhdr((struct gpt_hdr *)buf, offset,
276 table->sectors - 1, table->sectorsize);
279 * Compare primary and backup headers.
280 * If they are equal, then we do not need to read backup
281 * table. If they are different, then prefer backup header
282 * and try to read backup table.
285 uuid_equal(&hdr.hdr_uuid, &phdr->hdr_uuid, NULL) == 0 ||
286 hdr.hdr_revision != phdr->hdr_revision ||
287 hdr.hdr_size != phdr->hdr_size ||
288 hdr.hdr_lba_start != phdr->hdr_lba_start ||
289 hdr.hdr_lba_end != phdr->hdr_lba_end ||
290 hdr.hdr_entries != phdr->hdr_entries ||
291 hdr.hdr_entsz != phdr->hdr_entsz ||
292 hdr.hdr_crc_table != phdr->hdr_crc_table) {
293 /* Read the backup GPT table. */
295 howmany(phdr->hdr_entries * phdr->hdr_entsz,
297 if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
298 gpt_checktbl(phdr, tbl, size * table->sectorsize,
299 table->sectors - 1) == 0) {
300 memcpy(&hdr, phdr, sizeof(hdr));
305 if (pri == 0 && sec == 0) {
306 /* Both primary and backup tables are invalid. */
307 table->type = PTABLE_NONE;
310 DEBUG("GPT detected");
311 size = MIN(hdr.hdr_entries * hdr.hdr_entsz,
312 MAXTBLSZ * table->sectorsize);
315 * If the disk's sector count is smaller than the sector count recorded
316 * in the disk's GPT table header, set the table->sectors to the value
317 * recorded in GPT tables. This is done to work around buggy firmware
318 * that returns truncated disk sizes.
320 * Note, this is still not a foolproof way to get disk's size. For
321 * example, an image file can be truncated when copied to smaller media.
323 if (hdr.hdr_lba_alt + 1 > table->sectors)
324 table->sectors = hdr.hdr_lba_alt + 1;
326 for (i = 0; i < size / hdr.hdr_entsz; i++) {
327 ent = (struct gpt_ent *)(tbl + i * hdr.hdr_entsz);
328 if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
331 /* Simple sanity checks. */
332 if (ent->ent_lba_start < hdr.hdr_lba_start ||
333 ent->ent_lba_end > hdr.hdr_lba_end ||
334 ent->ent_lba_start > ent->ent_lba_end)
337 entry = malloc(sizeof(*entry));
340 entry->part.start = ent->ent_lba_start;
341 entry->part.end = ent->ent_lba_end;
342 entry->part.index = i + 1;
343 entry->part.type = gpt_parttype(ent->ent_type);
344 entry->flags = le64toh(ent->ent_attr);
345 memcpy(&entry->type.gpt, &ent->ent_type, sizeof(uuid_t));
346 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
347 DEBUG("new GPT partition added");
354 #endif /* LOADER_GPT_SUPPORT */
356 #ifdef LOADER_MBR_SUPPORT
357 /* We do not need to support too many EBR partitions in the loader */
358 #define MAXEBRENTRIES 8
359 static enum partition_type
360 mbr_parttype(uint8_t type)
365 return (PART_FREEBSD);
367 return (PART_LINUX_SWAP);
379 return (PART_UNKNOWN);
382 static struct ptable *
383 ptable_ebrread(struct ptable *table, void *dev, diskread_t dread)
385 struct dos_partition *dp;
386 struct pentry *e1, *entry;
387 uint32_t start, end, offset;
391 STAILQ_FOREACH(e1, &table->entries, entry) {
392 if (e1->type.mbr == DOSPTYP_EXT ||
393 e1->type.mbr == DOSPTYP_EXTLBA)
399 offset = e1->part.start;
400 buf = malloc(table->sectorsize);
403 DEBUG("EBR detected");
404 for (i = 0; i < MAXEBRENTRIES; i++) {
405 #if 0 /* Some BIOSes return an incorrect number of sectors */
406 if (offset >= table->sectors)
409 if (dread(dev, buf, 1, offset) != 0)
411 dp = (struct dos_partition *)(buf + DOSPARTOFF);
412 if (dp[0].dp_typ == 0)
414 start = le32toh(dp[0].dp_start);
415 if (dp[0].dp_typ == DOSPTYP_EXT &&
417 offset = e1->part.start + start;
420 end = le32toh(dp[0].dp_size);
421 entry = malloc(sizeof(*entry));
424 entry->part.start = offset + start;
425 entry->part.end = entry->part.start + end - 1;
426 entry->part.index = index++;
427 entry->part.type = mbr_parttype(dp[0].dp_typ);
428 entry->flags = dp[0].dp_flag;
429 entry->type.mbr = dp[0].dp_typ;
430 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
431 DEBUG("new EBR partition added");
432 if (dp[1].dp_typ == 0)
434 offset = e1->part.start + le32toh(dp[1].dp_start);
439 #endif /* LOADER_MBR_SUPPORT */
441 static enum partition_type
442 bsd_parttype(uint8_t type)
447 return (PART_FREEBSD_NANDFS);
449 return (PART_FREEBSD_SWAP);
451 return (PART_FREEBSD_UFS);
453 return (PART_FREEBSD_VINUM);
455 return (PART_FREEBSD_ZFS);
457 return (PART_UNKNOWN);
460 static struct ptable *
461 ptable_bsdread(struct ptable *table, void *dev, diskread_t dread)
463 struct disklabel *dl;
464 struct partition *part;
465 struct pentry *entry;
470 if (table->sectorsize < sizeof(struct disklabel)) {
471 DEBUG("Too small sectorsize");
474 buf = malloc(table->sectorsize);
477 if (dread(dev, buf, 1, 1) != 0) {
478 DEBUG("read failed");
483 dl = (struct disklabel *)buf;
484 if (le32toh(dl->d_magic) != DISKMAGIC &&
485 le32toh(dl->d_magic2) != DISKMAGIC)
487 if (le32toh(dl->d_secsize) != table->sectorsize) {
488 DEBUG("unsupported sector size");
491 dl->d_npartitions = le16toh(dl->d_npartitions);
492 if (dl->d_npartitions > 20 || dl->d_npartitions < 8) {
493 DEBUG("invalid number of partitions");
496 DEBUG("BSD detected");
497 part = &dl->d_partitions[0];
498 raw_offset = le32toh(part[RAW_PART].p_offset);
499 for (i = 0; i < dl->d_npartitions; i++, part++) {
502 if (part->p_size == 0)
504 entry = malloc(sizeof(*entry));
507 entry->part.start = le32toh(part->p_offset) - raw_offset;
508 entry->part.end = entry->part.start +
509 le32toh(part->p_size) - 1;
510 entry->part.type = bsd_parttype(part->p_fstype);
511 entry->part.index = i; /* starts from zero */
512 entry->type.bsd = part->p_fstype;
513 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
514 DEBUG("new BSD partition added");
516 table->type = PTABLE_BSD;
522 #ifdef LOADER_VTOC8_SUPPORT
523 static enum partition_type
524 vtoc8_parttype(uint16_t type)
528 case VTOC_TAG_FREEBSD_NANDFS:
529 return (PART_FREEBSD_NANDFS);
530 case VTOC_TAG_FREEBSD_SWAP:
531 return (PART_FREEBSD_SWAP);
532 case VTOC_TAG_FREEBSD_UFS:
533 return (PART_FREEBSD_UFS);
534 case VTOC_TAG_FREEBSD_VINUM:
535 return (PART_FREEBSD_VINUM);
536 case VTOC_TAG_FREEBSD_ZFS:
537 return (PART_FREEBSD_ZFS);
539 return (PART_UNKNOWN);
542 static struct ptable *
543 ptable_vtoc8read(struct ptable *table, void *dev, diskread_t dread)
545 struct pentry *entry;
548 uint16_t sum, heads, sectors;
551 if (table->sectorsize != sizeof(struct vtoc8))
553 buf = malloc(table->sectorsize);
556 if (dread(dev, buf, 1, 0) != 0) {
557 DEBUG("read failed");
562 dl = (struct vtoc8 *)buf;
564 for (i = sum = 0; i < sizeof(struct vtoc8); i += sizeof(sum))
565 sum ^= be16dec(buf + i);
567 DEBUG("incorrect checksum");
570 if (be16toh(dl->nparts) != VTOC8_NPARTS) {
571 DEBUG("invalid number of entries");
574 sectors = be16toh(dl->nsecs);
575 heads = be16toh(dl->nheads);
576 if (sectors * heads == 0) {
577 DEBUG("invalid geometry");
580 DEBUG("VTOC8 detected");
581 for (i = 0; i < VTOC8_NPARTS; i++) {
582 dl->part[i].tag = be16toh(dl->part[i].tag);
583 if (i == VTOC_RAW_PART ||
584 dl->part[i].tag == VTOC_TAG_UNASSIGNED)
586 entry = malloc(sizeof(*entry));
589 entry->part.start = be32toh(dl->map[i].cyl) * heads * sectors;
590 entry->part.end = be32toh(dl->map[i].nblks) +
591 entry->part.start - 1;
592 entry->part.type = vtoc8_parttype(dl->part[i].tag);
593 entry->part.index = i; /* starts from zero */
594 entry->type.vtoc8 = dl->part[i].tag;
595 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
596 DEBUG("new VTOC8 partition added");
598 table->type = PTABLE_VTOC8;
604 #endif /* LOADER_VTOC8_SUPPORT */
607 ptable_open(void *dev, uint64_t sectors, uint16_t sectorsize,
610 struct dos_partition *dp;
611 struct ptable *table;
614 #ifdef LOADER_MBR_SUPPORT
615 struct pentry *entry;
620 buf = malloc(sectorsize);
623 /* First, read the MBR. */
624 if (dread(dev, buf, 1, DOSBBSECTOR) != 0) {
625 DEBUG("read failed");
629 table = malloc(sizeof(*table));
632 table->sectors = sectors;
633 table->sectorsize = sectorsize;
634 table->type = PTABLE_NONE;
635 STAILQ_INIT(&table->entries);
637 #ifdef LOADER_VTOC8_SUPPORT
638 if (be16dec(buf + offsetof(struct vtoc8, magic)) == VTOC_MAGIC) {
639 if (ptable_vtoc8read(table, dev, dread) == NULL) {
643 } else if (table->type == PTABLE_VTOC8)
647 /* Check the BSD label. */
648 if (ptable_bsdread(table, dev, dread) == NULL) { /* Read error. */
651 } else if (table->type == PTABLE_BSD)
654 #if defined(LOADER_GPT_SUPPORT) || defined(LOADER_MBR_SUPPORT)
655 /* Check the MBR magic. */
656 if (buf[DOSMAGICOFFSET] != 0x55 ||
657 buf[DOSMAGICOFFSET + 1] != 0xaa) {
658 DEBUG("magic sequence not found");
659 #if defined(LOADER_GPT_SUPPORT)
660 /* There is no PMBR, check that we have backup GPT */
661 table->type = PTABLE_GPT;
662 table = ptable_gptread(table, dev, dread);
666 /* Check that we have PMBR. Also do some validation. */
667 dp = (struct dos_partition *)(buf + DOSPARTOFF);
668 for (i = 0, count = 0; i < NDOSPART; i++) {
669 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) {
670 DEBUG("invalid partition flag %x", dp[i].dp_flag);
673 #ifdef LOADER_GPT_SUPPORT
674 if (dp[i].dp_typ == DOSPTYP_PMBR) {
675 table->type = PTABLE_GPT;
676 DEBUG("PMBR detected");
679 if (dp[i].dp_typ != 0)
682 /* Do we have some invalid values? */
683 if (table->type == PTABLE_GPT && count > 1) {
684 if (dp[1].dp_typ != DOSPTYP_HFS) {
685 table->type = PTABLE_NONE;
686 DEBUG("Incorrect PMBR, ignore it");
688 DEBUG("Bootcamp detected");
691 #ifdef LOADER_GPT_SUPPORT
692 if (table->type == PTABLE_GPT) {
693 table = ptable_gptread(table, dev, dread);
697 #ifdef LOADER_MBR_SUPPORT
699 DEBUG("MBR detected");
700 table->type = PTABLE_MBR;
701 for (i = has_ext = 0; i < NDOSPART; i++) {
702 if (dp[i].dp_typ == 0)
704 start = le32dec(&(dp[i].dp_start));
705 end = le32dec(&(dp[i].dp_size));
706 if (start == 0 || end == 0)
708 #if 0 /* Some BIOSes return an incorrect number of sectors */
709 if (start + end - 1 >= sectors)
710 continue; /* XXX: ignore */
712 if (dp[i].dp_typ == DOSPTYP_EXT ||
713 dp[i].dp_typ == DOSPTYP_EXTLBA)
715 entry = malloc(sizeof(*entry));
718 entry->part.start = start;
719 entry->part.end = start + end - 1;
720 entry->part.index = i + 1;
721 entry->part.type = mbr_parttype(dp[i].dp_typ);
722 entry->flags = dp[i].dp_flag;
723 entry->type.mbr = dp[i].dp_typ;
724 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
725 DEBUG("new MBR partition added");
728 table = ptable_ebrread(table, dev, dread);
731 #endif /* LOADER_MBR_SUPPORT */
732 #endif /* LOADER_MBR_SUPPORT || LOADER_GPT_SUPPORT */
739 ptable_close(struct ptable *table)
741 struct pentry *entry;
743 while (!STAILQ_EMPTY(&table->entries)) {
744 entry = STAILQ_FIRST(&table->entries);
745 STAILQ_REMOVE_HEAD(&table->entries, entry);
752 ptable_gettype(const struct ptable *table)
755 return (table->type);
759 ptable_getsize(const struct ptable *table, uint64_t *sizep)
761 uint64_t tmp = table->sectors * table->sectorsize;
763 if (tmp < table->sectors)
772 ptable_getpart(const struct ptable *table, struct ptable_entry *part, int index)
774 struct pentry *entry;
776 if (part == NULL || table == NULL)
779 STAILQ_FOREACH(entry, &table->entries, entry) {
780 if (entry->part.index != index)
782 memcpy(part, &entry->part, sizeof(*part));
789 * Search for a slice with the following preferences:
791 * 1: Active FreeBSD slice
792 * 2: Non-active FreeBSD slice
793 * 3: Active Linux slice
794 * 4: non-active Linux slice
795 * 5: Active FAT/FAT32 slice
796 * 6: non-active FAT/FAT32 slice
798 #define PREF_RAWDISK 0
799 #define PREF_FBSD_ACT 1
801 #define PREF_LINUX_ACT 3
803 #define PREF_DOS_ACT 5
807 ptable_getbestpart(const struct ptable *table, struct ptable_entry *part)
809 struct pentry *entry, *best;
812 if (part == NULL || table == NULL)
816 preflevel = pref = PREF_NONE;
817 STAILQ_FOREACH(entry, &table->entries, entry) {
818 #ifdef LOADER_MBR_SUPPORT
819 if (table->type == PTABLE_MBR) {
820 switch (entry->type.mbr) {
822 pref = entry->flags & 0x80 ? PREF_FBSD_ACT:
826 pref = entry->flags & 0x80 ? PREF_LINUX_ACT:
829 case 0x01: /* DOS/Windows */
835 pref = entry->flags & 0x80 ? PREF_DOS_ACT:
842 #endif /* LOADER_MBR_SUPPORT */
843 #ifdef LOADER_GPT_SUPPORT
844 if (table->type == PTABLE_GPT) {
845 if (entry->part.type == PART_DOS)
847 else if (entry->part.type == PART_FREEBSD_UFS ||
848 entry->part.type == PART_FREEBSD_ZFS)
853 #endif /* LOADER_GPT_SUPPORT */
854 if (pref < preflevel) {
860 memcpy(part, &best->part, sizeof(*part));
867 ptable_iterate(const struct ptable *table, void *arg, ptable_iterate_t *iter)
869 struct pentry *entry;
874 STAILQ_FOREACH(entry, &table->entries, entry) {
875 #ifdef LOADER_MBR_SUPPORT
876 if (table->type == PTABLE_MBR)
877 sprintf(name, "s%d", entry->part.index);
880 #ifdef LOADER_GPT_SUPPORT
881 if (table->type == PTABLE_GPT)
882 sprintf(name, "p%d", entry->part.index);
885 #ifdef LOADER_VTOC8_SUPPORT
886 if (table->type == PTABLE_VTOC8)
887 sprintf(name, "%c", (uint8_t) 'a' +
891 if (table->type == PTABLE_BSD)
892 sprintf(name, "%c", (uint8_t) 'a' +
894 if ((ret = iter(arg, name, &entry->part)) != 0)