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>
40 #include <fs/cd9660/iso.h>
47 #define DPRINTF(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
49 #define DPRINTF(fmt, args...) ((void)0)
52 #ifdef LOADER_GPT_SUPPORT
54 static const uuid_t gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
55 static const uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
56 static const uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
57 static const uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI;
58 static const uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
59 static const uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
60 static const uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
61 static const uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
62 static const uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
63 static const uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
64 static const uuid_t gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
68 struct ptable_entry part;
76 STAILQ_ENTRY(pentry) entry;
80 enum ptable_type type;
84 STAILQ_HEAD(, pentry) entries;
87 static struct parttypes {
88 enum partition_type type;
91 { PART_UNKNOWN, "Unknown" },
93 { PART_FREEBSD, "FreeBSD" },
94 { PART_FREEBSD_BOOT, "FreeBSD boot" },
95 { PART_FREEBSD_NANDFS, "FreeBSD nandfs" },
96 { PART_FREEBSD_UFS, "FreeBSD UFS" },
97 { PART_FREEBSD_ZFS, "FreeBSD ZFS" },
98 { PART_FREEBSD_SWAP, "FreeBSD swap" },
99 { PART_FREEBSD_VINUM, "FreeBSD vinum" },
100 { PART_LINUX, "Linux" },
101 { PART_LINUX_SWAP, "Linux swap" },
102 { PART_DOS, "DOS/Windows" },
103 { PART_ISO9660, "ISO9660" },
104 { PART_APFS, "APFS" },
108 parttype2str(enum partition_type type)
112 for (i = 0; i < nitems(ptypes); i++)
113 if (ptypes[i].type == type)
114 return (ptypes[i].desc);
115 return (ptypes[0].desc);
118 #ifdef LOADER_GPT_SUPPORT
120 uuid_letoh(uuid_t *uuid)
123 uuid->time_low = le32toh(uuid->time_low);
124 uuid->time_mid = le16toh(uuid->time_mid);
125 uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
128 static enum partition_type
129 gpt_parttype(uuid_t type)
132 if (uuid_equal(&type, &gpt_uuid_efi, NULL))
134 else if (uuid_equal(&type, &gpt_uuid_ms_basic_data, NULL))
136 else if (uuid_equal(&type, &gpt_uuid_freebsd_boot, NULL))
137 return (PART_FREEBSD_BOOT);
138 else if (uuid_equal(&type, &gpt_uuid_freebsd_ufs, NULL))
139 return (PART_FREEBSD_UFS);
140 else if (uuid_equal(&type, &gpt_uuid_freebsd_zfs, NULL))
141 return (PART_FREEBSD_ZFS);
142 else if (uuid_equal(&type, &gpt_uuid_freebsd_swap, NULL))
143 return (PART_FREEBSD_SWAP);
144 else if (uuid_equal(&type, &gpt_uuid_freebsd_vinum, NULL))
145 return (PART_FREEBSD_VINUM);
146 else if (uuid_equal(&type, &gpt_uuid_freebsd_nandfs, NULL))
147 return (PART_FREEBSD_NANDFS);
148 else if (uuid_equal(&type, &gpt_uuid_freebsd, NULL))
149 return (PART_FREEBSD);
150 else if (uuid_equal(&type, &gpt_uuid_apple_apfs, NULL))
152 return (PART_UNKNOWN);
155 static struct gpt_hdr *
156 gpt_checkhdr(struct gpt_hdr *hdr, uint64_t lba_self, uint64_t lba_last,
161 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0) {
162 DPRINTF("no GPT signature");
165 sz = le32toh(hdr->hdr_size);
166 if (sz < 92 || sz > sectorsize) {
167 DPRINTF("invalid GPT header size: %d", sz);
170 crc = le32toh(hdr->hdr_crc_self);
171 hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
172 if (crc32(hdr->hdr_crc_self, (const Bytef *)hdr, sz) != crc) {
173 DPRINTF("GPT header's CRC doesn't match");
176 hdr->hdr_crc_self = crc;
177 hdr->hdr_revision = le32toh(hdr->hdr_revision);
178 if (hdr->hdr_revision < GPT_HDR_REVISION) {
179 DPRINTF("unsupported GPT revision %d", hdr->hdr_revision);
182 hdr->hdr_lba_self = le64toh(hdr->hdr_lba_self);
183 if (hdr->hdr_lba_self != lba_self) {
184 DPRINTF("self LBA doesn't match");
187 hdr->hdr_lba_alt = le64toh(hdr->hdr_lba_alt);
188 if (hdr->hdr_lba_alt == hdr->hdr_lba_self) {
189 DPRINTF("invalid alternate LBA");
192 hdr->hdr_entries = le32toh(hdr->hdr_entries);
193 hdr->hdr_entsz = le32toh(hdr->hdr_entsz);
194 if (hdr->hdr_entries == 0 ||
195 hdr->hdr_entsz < sizeof(struct gpt_ent) ||
196 sectorsize % hdr->hdr_entsz != 0) {
197 DPRINTF("invalid entry size or number of entries");
200 hdr->hdr_lba_start = le64toh(hdr->hdr_lba_start);
201 hdr->hdr_lba_end = le64toh(hdr->hdr_lba_end);
202 hdr->hdr_lba_table = le64toh(hdr->hdr_lba_table);
203 hdr->hdr_crc_table = le32toh(hdr->hdr_crc_table);
204 uuid_letoh(&hdr->hdr_uuid);
209 gpt_checktbl(const struct gpt_hdr *hdr, uint8_t *tbl, size_t size,
215 cnt = size / hdr->hdr_entsz;
216 if (hdr->hdr_entries <= cnt) {
217 cnt = hdr->hdr_entries;
218 /* Check CRC only when buffer size is enough for table. */
219 if (hdr->hdr_crc_table !=
220 crc32(0, tbl, hdr->hdr_entries * hdr->hdr_entsz)) {
221 DPRINTF("GPT table's CRC doesn't match");
225 for (i = 0; i < cnt; i++) {
226 ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
227 uuid_letoh(&ent->ent_type);
228 if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
230 ent->ent_lba_start = le64toh(ent->ent_lba_start);
231 ent->ent_lba_end = le64toh(ent->ent_lba_end);
236 static struct ptable *
237 ptable_gptread(struct ptable *table, void *dev, diskread_t dread)
239 struct pentry *entry;
240 struct gpt_hdr *phdr, hdr;
247 buf = malloc(table->sectorsize);
250 tbl = malloc(table->sectorsize * MAXTBLSZ);
255 /* Read the primary GPT header. */
256 if (dread(dev, buf, 1, 1) != 0) {
262 /* Check the primary GPT header. */
263 phdr = gpt_checkhdr((struct gpt_hdr *)buf, 1, table->sectors - 1,
266 /* Read the primary GPT table. */
268 howmany(phdr->hdr_entries * phdr->hdr_entsz,
270 if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
271 gpt_checktbl(phdr, tbl, size * table->sectorsize,
272 table->sectors - 1) == 0) {
273 memcpy(&hdr, phdr, sizeof(hdr));
277 offset = pri ? hdr.hdr_lba_alt: table->sectors - 1;
278 /* Read the backup GPT header. */
279 if (dread(dev, buf, 1, offset) != 0)
282 phdr = gpt_checkhdr((struct gpt_hdr *)buf, offset,
283 table->sectors - 1, table->sectorsize);
286 * Compare primary and backup headers.
287 * If they are equal, then we do not need to read backup
288 * table. If they are different, then prefer backup header
289 * and try to read backup table.
292 uuid_equal(&hdr.hdr_uuid, &phdr->hdr_uuid, NULL) == 0 ||
293 hdr.hdr_revision != phdr->hdr_revision ||
294 hdr.hdr_size != phdr->hdr_size ||
295 hdr.hdr_lba_start != phdr->hdr_lba_start ||
296 hdr.hdr_lba_end != phdr->hdr_lba_end ||
297 hdr.hdr_entries != phdr->hdr_entries ||
298 hdr.hdr_entsz != phdr->hdr_entsz ||
299 hdr.hdr_crc_table != phdr->hdr_crc_table) {
300 /* Read the backup GPT table. */
302 howmany(phdr->hdr_entries * phdr->hdr_entsz,
304 if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
305 gpt_checktbl(phdr, tbl, size * table->sectorsize,
306 table->sectors - 1) == 0) {
307 memcpy(&hdr, phdr, sizeof(hdr));
312 if (pri == 0 && sec == 0) {
313 /* Both primary and backup tables are invalid. */
314 table->type = PTABLE_NONE;
317 DPRINTF("GPT detected");
318 size = MIN(hdr.hdr_entries * hdr.hdr_entsz,
319 MAXTBLSZ * table->sectorsize);
322 * If the disk's sector count is smaller than the sector count recorded
323 * in the disk's GPT table header, set the table->sectors to the value
324 * recorded in GPT tables. This is done to work around buggy firmware
325 * that returns truncated disk sizes.
327 * Note, this is still not a foolproof way to get disk's size. For
328 * example, an image file can be truncated when copied to smaller media.
330 table->sectors = hdr.hdr_lba_alt + 1;
332 for (i = 0; i < size / hdr.hdr_entsz; i++) {
333 ent = (struct gpt_ent *)(tbl + i * hdr.hdr_entsz);
334 if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
337 /* Simple sanity checks. */
338 if (ent->ent_lba_start < hdr.hdr_lba_start ||
339 ent->ent_lba_end > hdr.hdr_lba_end ||
340 ent->ent_lba_start > ent->ent_lba_end)
343 entry = malloc(sizeof(*entry));
346 entry->part.start = ent->ent_lba_start;
347 entry->part.end = ent->ent_lba_end;
348 entry->part.index = i + 1;
349 entry->part.type = gpt_parttype(ent->ent_type);
350 entry->flags = le64toh(ent->ent_attr);
351 memcpy(&entry->type.gpt, &ent->ent_type, sizeof(uuid_t));
352 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
353 DPRINTF("new GPT partition added");
360 #endif /* LOADER_GPT_SUPPORT */
362 #ifdef LOADER_MBR_SUPPORT
363 /* We do not need to support too many EBR partitions in the loader */
364 #define MAXEBRENTRIES 8
365 static enum partition_type
366 mbr_parttype(uint8_t type)
371 return (PART_FREEBSD);
373 return (PART_LINUX_SWAP);
385 return (PART_UNKNOWN);
388 static struct ptable *
389 ptable_ebrread(struct ptable *table, void *dev, diskread_t dread)
391 struct dos_partition *dp;
392 struct pentry *e1, *entry;
393 uint32_t start, end, offset;
397 STAILQ_FOREACH(e1, &table->entries, entry) {
398 if (e1->type.mbr == DOSPTYP_EXT ||
399 e1->type.mbr == DOSPTYP_EXTLBA)
405 offset = e1->part.start;
406 buf = malloc(table->sectorsize);
409 DPRINTF("EBR detected");
410 for (i = 0; i < MAXEBRENTRIES; i++) {
411 #if 0 /* Some BIOSes return an incorrect number of sectors */
412 if (offset >= table->sectors)
415 if (dread(dev, buf, 1, offset) != 0)
417 dp = (struct dos_partition *)(buf + DOSPARTOFF);
418 if (dp[0].dp_typ == 0)
420 start = le32toh(dp[0].dp_start);
421 if (dp[0].dp_typ == DOSPTYP_EXT &&
423 offset = e1->part.start + start;
426 end = le32toh(dp[0].dp_size);
427 entry = malloc(sizeof(*entry));
430 entry->part.start = offset + start;
431 entry->part.end = entry->part.start + end - 1;
432 entry->part.index = index++;
433 entry->part.type = mbr_parttype(dp[0].dp_typ);
434 entry->flags = dp[0].dp_flag;
435 entry->type.mbr = dp[0].dp_typ;
436 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
437 DPRINTF("new EBR partition added");
438 if (dp[1].dp_typ == 0)
440 offset = e1->part.start + le32toh(dp[1].dp_start);
445 #endif /* LOADER_MBR_SUPPORT */
447 static enum partition_type
448 bsd_parttype(uint8_t type)
453 return (PART_FREEBSD_NANDFS);
455 return (PART_FREEBSD_SWAP);
457 return (PART_FREEBSD_UFS);
459 return (PART_FREEBSD_VINUM);
461 return (PART_FREEBSD_ZFS);
463 return (PART_UNKNOWN);
466 static struct ptable *
467 ptable_bsdread(struct ptable *table, void *dev, diskread_t dread)
469 struct disklabel *dl;
470 struct partition *part;
471 struct pentry *entry;
476 if (table->sectorsize < sizeof(struct disklabel)) {
477 DPRINTF("Too small sectorsize");
480 buf = malloc(table->sectorsize);
483 if (dread(dev, buf, 1, 1) != 0) {
484 DPRINTF("read failed");
489 dl = (struct disklabel *)buf;
490 if (le32toh(dl->d_magic) != DISKMAGIC &&
491 le32toh(dl->d_magic2) != DISKMAGIC)
493 if (le32toh(dl->d_secsize) != table->sectorsize) {
494 DPRINTF("unsupported sector size");
497 dl->d_npartitions = le16toh(dl->d_npartitions);
498 if (dl->d_npartitions > 20 || dl->d_npartitions < 8) {
499 DPRINTF("invalid number of partitions");
502 DPRINTF("BSD detected");
503 part = &dl->d_partitions[0];
504 raw_offset = le32toh(part[RAW_PART].p_offset);
505 for (i = 0; i < dl->d_npartitions; i++, part++) {
508 if (part->p_size == 0)
510 entry = malloc(sizeof(*entry));
513 entry->part.start = le32toh(part->p_offset) - raw_offset;
514 entry->part.end = entry->part.start +
515 le32toh(part->p_size) - 1;
516 entry->part.type = bsd_parttype(part->p_fstype);
517 entry->part.index = i; /* starts from zero */
518 entry->type.bsd = part->p_fstype;
519 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
520 DPRINTF("new BSD partition added");
522 table->type = PTABLE_BSD;
528 #ifdef LOADER_VTOC8_SUPPORT
529 static enum partition_type
530 vtoc8_parttype(uint16_t type)
534 case VTOC_TAG_FREEBSD_NANDFS:
535 return (PART_FREEBSD_NANDFS);
536 case VTOC_TAG_FREEBSD_SWAP:
537 return (PART_FREEBSD_SWAP);
538 case VTOC_TAG_FREEBSD_UFS:
539 return (PART_FREEBSD_UFS);
540 case VTOC_TAG_FREEBSD_VINUM:
541 return (PART_FREEBSD_VINUM);
542 case VTOC_TAG_FREEBSD_ZFS:
543 return (PART_FREEBSD_ZFS);
545 return (PART_UNKNOWN);
548 static struct ptable *
549 ptable_vtoc8read(struct ptable *table, void *dev, diskread_t dread)
551 struct pentry *entry;
554 uint16_t sum, heads, sectors;
557 if (table->sectorsize != sizeof(struct vtoc8))
559 buf = malloc(table->sectorsize);
562 if (dread(dev, buf, 1, 0) != 0) {
563 DPRINTF("read failed");
568 dl = (struct vtoc8 *)buf;
570 for (i = sum = 0; i < sizeof(struct vtoc8); i += sizeof(sum))
571 sum ^= be16dec(buf + i);
573 DPRINTF("incorrect checksum");
576 if (be16toh(dl->nparts) != VTOC8_NPARTS) {
577 DPRINTF("invalid number of entries");
580 sectors = be16toh(dl->nsecs);
581 heads = be16toh(dl->nheads);
582 if (sectors * heads == 0) {
583 DPRINTF("invalid geometry");
586 DPRINTF("VTOC8 detected");
587 for (i = 0; i < VTOC8_NPARTS; i++) {
588 dl->part[i].tag = be16toh(dl->part[i].tag);
589 if (i == VTOC_RAW_PART ||
590 dl->part[i].tag == VTOC_TAG_UNASSIGNED)
592 entry = malloc(sizeof(*entry));
595 entry->part.start = be32toh(dl->map[i].cyl) * heads * sectors;
596 entry->part.end = be32toh(dl->map[i].nblks) +
597 entry->part.start - 1;
598 entry->part.type = vtoc8_parttype(dl->part[i].tag);
599 entry->part.index = i; /* starts from zero */
600 entry->type.vtoc8 = dl->part[i].tag;
601 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
602 DPRINTF("new VTOC8 partition added");
604 table->type = PTABLE_VTOC8;
610 #endif /* LOADER_VTOC8_SUPPORT */
612 #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / table->sectorsize)
614 static struct ptable *
615 ptable_iso9660read(struct ptable *table, void *dev, diskread_t dread)
618 struct iso_primary_descriptor *vd;
619 struct pentry *entry;
621 buf = malloc(table->sectorsize);
625 if (dread(dev, buf, 1, cdb2devb(16)) != 0) {
626 DPRINTF("read failed");
631 vd = (struct iso_primary_descriptor *)buf;
632 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
635 entry = malloc(sizeof(*entry));
638 entry->part.start = 0;
639 entry->part.end = table->sectors;
640 entry->part.type = PART_ISO9660;
641 entry->part.index = 0;
642 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
644 table->type = PTABLE_ISO9660;
652 ptable_open(void *dev, uint64_t sectors, uint16_t sectorsize,
655 struct dos_partition *dp;
656 struct ptable *table;
659 #ifdef LOADER_MBR_SUPPORT
660 struct pentry *entry;
666 buf = malloc(sectorsize);
669 /* First, read the MBR. */
670 if (dread(dev, buf, 1, DOSBBSECTOR) != 0) {
671 DPRINTF("read failed");
675 table = malloc(sizeof(*table));
678 table->sectors = sectors;
679 table->sectorsize = sectorsize;
680 table->type = PTABLE_NONE;
681 STAILQ_INIT(&table->entries);
683 if (ptable_iso9660read(table, dev, dread) == NULL) {
687 } else if (table->type == PTABLE_ISO9660)
690 #ifdef LOADER_VTOC8_SUPPORT
691 if (be16dec(buf + offsetof(struct vtoc8, magic)) == VTOC_MAGIC) {
692 if (ptable_vtoc8read(table, dev, dread) == NULL) {
696 } else if (table->type == PTABLE_VTOC8)
700 /* Check the BSD label. */
701 if (ptable_bsdread(table, dev, dread) == NULL) { /* Read error. */
704 } else if (table->type == PTABLE_BSD)
707 #if defined(LOADER_GPT_SUPPORT) || defined(LOADER_MBR_SUPPORT)
708 /* Check the MBR magic. */
709 if (buf[DOSMAGICOFFSET] != 0x55 ||
710 buf[DOSMAGICOFFSET + 1] != 0xaa) {
711 DPRINTF("magic sequence not found");
712 #if defined(LOADER_GPT_SUPPORT)
713 /* There is no PMBR, check that we have backup GPT */
714 table->type = PTABLE_GPT;
715 table = ptable_gptread(table, dev, dread);
719 /* Check that we have PMBR. Also do some validation. */
720 dp = malloc(NDOSPART * sizeof(struct dos_partition));
723 bcopy(buf + DOSPARTOFF, dp, NDOSPART * sizeof(struct dos_partition));
726 * In mac we can have PMBR partition in hybrid MBR;
727 * that is, MBR partition which has DOSPTYP_PMBR entry defined as
728 * start sector 1. After DOSPTYP_PMBR, there may be other partitions.
729 * UEFI compliant PMBR has no other partitions.
731 for (i = 0; i < NDOSPART; i++) {
732 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) {
733 DPRINTF("invalid partition flag %x", dp[i].dp_flag);
736 #ifdef LOADER_GPT_SUPPORT
737 if (dp[i].dp_typ == DOSPTYP_PMBR && dp[i].dp_start == 1) {
738 table->type = PTABLE_GPT;
739 DPRINTF("PMBR detected");
743 #ifdef LOADER_GPT_SUPPORT
744 if (table->type == PTABLE_GPT) {
745 table = ptable_gptread(table, dev, dread);
749 #ifdef LOADER_MBR_SUPPORT
751 DPRINTF("MBR detected");
752 table->type = PTABLE_MBR;
753 for (i = has_ext = 0; i < NDOSPART; i++) {
754 if (dp[i].dp_typ == 0)
756 start = le32dec(&(dp[i].dp_start));
757 end = le32dec(&(dp[i].dp_size));
758 if (start == 0 || end == 0)
760 #if 0 /* Some BIOSes return an incorrect number of sectors */
761 if (start + end - 1 >= sectors)
762 continue; /* XXX: ignore */
764 if (dp[i].dp_typ == DOSPTYP_EXT ||
765 dp[i].dp_typ == DOSPTYP_EXTLBA)
767 entry = malloc(sizeof(*entry));
770 entry->part.start = start;
771 entry->part.end = start + end - 1;
772 entry->part.index = i + 1;
773 entry->part.type = mbr_parttype(dp[i].dp_typ);
774 entry->flags = dp[i].dp_flag;
775 entry->type.mbr = dp[i].dp_typ;
776 STAILQ_INSERT_TAIL(&table->entries, entry, entry);
777 DPRINTF("new MBR partition added");
780 table = ptable_ebrread(table, dev, dread);
783 #endif /* LOADER_MBR_SUPPORT */
784 #endif /* LOADER_MBR_SUPPORT || LOADER_GPT_SUPPORT */
792 ptable_close(struct ptable *table)
794 struct pentry *entry;
799 while (!STAILQ_EMPTY(&table->entries)) {
800 entry = STAILQ_FIRST(&table->entries);
801 STAILQ_REMOVE_HEAD(&table->entries, entry);
808 ptable_gettype(const struct ptable *table)
811 return (table->type);
815 ptable_getsize(const struct ptable *table, uint64_t *sizep)
817 uint64_t tmp = table->sectors * table->sectorsize;
819 if (tmp < table->sectors)
828 ptable_getpart(const struct ptable *table, struct ptable_entry *part, int index)
830 struct pentry *entry;
832 if (part == NULL || table == NULL)
835 STAILQ_FOREACH(entry, &table->entries, entry) {
836 if (entry->part.index != index)
838 memcpy(part, &entry->part, sizeof(*part));
845 * Search for a slice with the following preferences:
847 * 1: Active FreeBSD slice
848 * 2: Non-active FreeBSD slice
849 * 3: Active Linux slice
850 * 4: non-active Linux slice
851 * 5: Active FAT/FAT32 slice
852 * 6: non-active FAT/FAT32 slice
854 #define PREF_RAWDISK 0
855 #define PREF_FBSD_ACT 1
857 #define PREF_LINUX_ACT 3
859 #define PREF_DOS_ACT 5
863 ptable_getbestpart(const struct ptable *table, struct ptable_entry *part)
865 struct pentry *entry, *best;
868 if (part == NULL || table == NULL)
872 preflevel = pref = PREF_NONE;
873 STAILQ_FOREACH(entry, &table->entries, entry) {
874 #ifdef LOADER_MBR_SUPPORT
875 if (table->type == PTABLE_MBR) {
876 switch (entry->type.mbr) {
878 pref = entry->flags & 0x80 ? PREF_FBSD_ACT:
882 pref = entry->flags & 0x80 ? PREF_LINUX_ACT:
885 case 0x01: /* DOS/Windows */
891 pref = entry->flags & 0x80 ? PREF_DOS_ACT:
898 #endif /* LOADER_MBR_SUPPORT */
899 #ifdef LOADER_GPT_SUPPORT
900 if (table->type == PTABLE_GPT) {
901 if (entry->part.type == PART_DOS)
903 else if (entry->part.type == PART_FREEBSD_UFS ||
904 entry->part.type == PART_FREEBSD_ZFS)
909 #endif /* LOADER_GPT_SUPPORT */
910 if (pref < preflevel) {
916 memcpy(part, &best->part, sizeof(*part));
923 ptable_iterate(const struct ptable *table, void *arg, ptable_iterate_t *iter)
925 struct pentry *entry;
930 STAILQ_FOREACH(entry, &table->entries, entry) {
931 #ifdef LOADER_MBR_SUPPORT
932 if (table->type == PTABLE_MBR)
933 sprintf(name, "s%d", entry->part.index);
936 #ifdef LOADER_GPT_SUPPORT
937 if (table->type == PTABLE_GPT)
938 sprintf(name, "p%d", entry->part.index);
941 #ifdef LOADER_VTOC8_SUPPORT
942 if (table->type == PTABLE_VTOC8)
943 sprintf(name, "%c", (uint8_t) 'a' +
947 if (table->type == PTABLE_BSD)
948 sprintf(name, "%c", (uint8_t) 'a' +
950 if ((ret = iter(arg, name, &entry->part)) != 0)