1/*-
2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <stand.h>
31#include <sys/param.h>
32#include <sys/diskmbr.h>
33#include <sys/disklabel.h>
34#include <sys/endian.h>
35#include <sys/gpt.h>
36#include <sys/stddef.h>
37#include <sys/queue.h>
38#include <sys/vtoc.h>
39
40#include <crc32.h>
41#include <part.h>
42#include <uuid.h>
43
44#ifdef PART_DEBUG
45#define	DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
46#else
47#define	DEBUG(fmt, args...)
48#endif
49
50#ifdef LOADER_GPT_SUPPORT
51#define	MAXTBLSZ	64
52static const uuid_t gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
53static const uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
54static const uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
55static const uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI;
56static const uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
57static const uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
58static const uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
59static const uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
60static const uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
61#endif
62
63struct pentry {
64	struct ptable_entry	part;
65	uint64_t		flags;
66	union {
67		uint8_t bsd;
68		uint8_t	mbr;
69		uuid_t	gpt;
70		uint16_t vtoc8;
71	} type;
72	STAILQ_ENTRY(pentry)	entry;
73};
74
75struct ptable {
76	enum ptable_type	type;
77	uint16_t		sectorsize;
78	uint64_t		sectors;
79
80	STAILQ_HEAD(, pentry)	entries;
81};
82
83static struct parttypes {
84	enum partition_type	type;
85	const char		*desc;
86} ptypes[] = {
87	{ PART_UNKNOWN,		"Unknown" },
88	{ PART_EFI,		"EFI" },
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" },
99};
100
101const char *
102parttype2str(enum partition_type type)
103{
104	int i;
105
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);
110}
111
112#ifdef LOADER_GPT_SUPPORT
113static void
114uuid_letoh(uuid_t *uuid)
115{
116
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);
120}
121
122static enum partition_type
123gpt_parttype(uuid_t type)
124{
125
126	if (uuid_equal(&type, &gpt_uuid_efi, NULL))
127		return (PART_EFI);
128	else if (uuid_equal(&type, &gpt_uuid_ms_basic_data, NULL))
129		return (PART_DOS);
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);
143}
144
145static struct gpt_hdr*
146gpt_checkhdr(struct gpt_hdr *hdr, uint64_t lba_self, uint64_t lba_last,
147    uint16_t sectorsize)
148{
149	uint32_t sz, crc;
150
151	if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0) {
152		DEBUG("no GPT signature");
153		return (NULL);
154	}
155	sz = le32toh(hdr->hdr_size);
156	if (sz < 92 || sz > sectorsize) {
157		DEBUG("invalid GPT header size: %d", sz);
158		return (NULL);
159	}
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");
164		return (NULL);
165	}
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);
170		return (NULL);
171	}
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");
175		return (NULL);
176	}
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");
180		return (NULL);
181	}
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");
188		return (NULL);
189	}
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);
195	return (hdr);
196}
197
198static int
199gpt_checktbl(const struct gpt_hdr *hdr, u_char *tbl, size_t size,
200    uint64_t lba_last)
201{
202	struct gpt_ent *ent;
203	int i, cnt;
204
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");
212			return (-1);
213		}
214	}
215	for (i = 0; i < cnt; i++) {
216		ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
217		uuid_letoh(&ent->ent_type);
218		if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
219			continue;
220		ent->ent_lba_start = le64toh(ent->ent_lba_start);
221		ent->ent_lba_end = le64toh(ent->ent_lba_end);
222	}
223	return (0);
224}
225
226static struct ptable*
227ptable_gptread(struct ptable *table, void *dev, diskread_t dread)
228{
229	struct pentry *entry;
230	struct gpt_hdr *phdr, hdr;
231	struct gpt_ent *ent;
232	u_char *buf, *tbl;
233	uint64_t offset;
234	int pri, sec, i;
235	size_t size;
236
237	buf = malloc(table->sectorsize);
238	if (buf == NULL)
239		return (NULL);
240	tbl = malloc(table->sectorsize * MAXTBLSZ);
241	if (tbl == NULL) {
242		free(buf);
243		return (NULL);
244	}
245	/* Read the primary GPT header. */
246	if (dread(dev, buf, 1, 1) != 0) {
247		ptable_close(table);
248		table = NULL;
249		goto out;
250	}
251	pri = sec = 0;
252	/* Check the primary GPT header. */
253	phdr = gpt_checkhdr((struct gpt_hdr *)buf, 1, table->sectors - 1,
254	    table->sectorsize);
255	if (phdr != NULL) {
256		/* Read the primary GPT table. */
257		size = MIN(MAXTBLSZ, (phdr->hdr_entries * phdr->hdr_entsz +
258		    table->sectorsize - 1) / 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));
263			pri = 1;
264		}
265	}
266	offset = pri ? hdr.hdr_lba_alt: table->sectors - 1;
267	/* Read the backup GPT header. */
268	if (dread(dev, buf, 1, offset) != 0)
269		phdr = NULL;
270	else
271		phdr = gpt_checkhdr((struct gpt_hdr *)buf, offset,
272		    table->sectors - 1, table->sectorsize);
273	if (phdr != NULL) {
274		/*
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.
279		 */
280		if (pri == 0 ||
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 - 1) /
292			    table->sectorsize);
293			if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
294			    gpt_checktbl(phdr, tbl, size * table->sectorsize,
295			    table->sectors - 1) == 0) {
296				memcpy(&hdr, phdr, sizeof(hdr));
297				sec = 1;
298			}
299		}
300	}
301	if (pri == 0 && sec == 0) {
302		/* Both primary and backup tables are invalid. */
303		table->type = PTABLE_NONE;
304		goto out;
305	}
306	size = MIN(hdr.hdr_entries * hdr.hdr_entsz,
307	    MAXTBLSZ * table->sectorsize);
308	for (i = 0; i < size / hdr.hdr_entsz; i++) {
309		ent = (struct gpt_ent *)(tbl + i * hdr.hdr_entsz);
310		if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
311			continue;
312		entry = malloc(sizeof(*entry));
313		if (entry == NULL)
314			break;
315		entry->part.start = ent->ent_lba_start;
316		entry->part.end = ent->ent_lba_end;
317		entry->part.index = i + 1;
318		entry->part.type = gpt_parttype(ent->ent_type);
319		entry->flags = le64toh(ent->ent_attr);
320		memcpy(&entry->type.gpt, &ent->ent_type, sizeof(uuid_t));
321		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
322		DEBUG("new GPT partition added");
323	}
324out:
325	free(buf);
326	free(tbl);
327	return (table);
328}
329#endif /* LOADER_GPT_SUPPORT */
330
331#ifdef LOADER_MBR_SUPPORT
332/* We do not need to support too many EBR partitions in the loader */
333#define	MAXEBRENTRIES		8
334static enum partition_type
335mbr_parttype(uint8_t type)
336{
337
338	switch (type) {
339	case DOSPTYP_386BSD:
340		return (PART_FREEBSD);
341	case DOSPTYP_LINSWP:
342		return (PART_LINUX_SWAP);
343	case DOSPTYP_LINUX:
344		return (PART_LINUX);
345	case 0x01:
346	case 0x04:
347	case 0x06:
348	case 0x07:
349	case 0x0b:
350	case 0x0c:
351	case 0x0e:
352		return (PART_DOS);
353	}
354	return (PART_UNKNOWN);
355}
356
357struct ptable*
358ptable_ebrread(struct ptable *table, void *dev, diskread_t dread)
359{
360	struct dos_partition *dp;
361	struct pentry *e1, *entry;
362	uint32_t start, end, offset;
363	u_char *buf;
364	int i, index;
365
366	STAILQ_FOREACH(e1, &table->entries, entry) {
367		if (e1->type.mbr == DOSPTYP_EXT ||
368		    e1->type.mbr == DOSPTYP_EXTLBA)
369			break;
370	}
371	if (e1 == NULL)
372		return (table);
373	index = 5;
374	offset = e1->part.start;
375	buf = malloc(table->sectorsize);
376	if (buf == NULL)
377		return (table);
378	for (i = 0; i < MAXEBRENTRIES; i++) {
379#if 0	/* Some BIOSes return an incorrect number of sectors */
380		if (offset >= table->sectors)
381			break;
382#endif
383		if (dread(dev, buf, 1, offset) != 0)
384			break;
385		dp = (struct dos_partition *)(buf + DOSPARTOFF);
386		if (dp[0].dp_typ == 0)
387			break;
388		start = le32toh(dp[0].dp_start);
389		if (dp[0].dp_typ == DOSPTYP_EXT &&
390		    dp[1].dp_typ == 0) {
391			offset = e1->part.start + start;
392			continue;
393		}
394		end = le32toh(dp[0].dp_size);
395		entry = malloc(sizeof(*entry));
396		if (entry == NULL)
397			break;
398		entry->part.start = offset + start;
399		entry->part.end = entry->part.start + end - 1;
400		entry->part.index = index++;
401		entry->part.type = mbr_parttype(dp[0].dp_typ);
402		entry->flags = dp[0].dp_flag;
403		entry->type.mbr = dp[0].dp_typ;
404		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
405		DEBUG("new EBR partition added");
406		if (dp[1].dp_typ == 0)
407			break;
408		offset = e1->part.start + le32toh(dp[1].dp_start);
409	}
410	free(buf);
411	return (table);
412}
413#endif /* LOADER_MBR_SUPPORT */
414
415static enum partition_type
416bsd_parttype(uint8_t type)
417{
418
419	switch (type) {
420	case FS_NANDFS:
421		return (PART_FREEBSD_NANDFS);
422	case FS_SWAP:
423		return (PART_FREEBSD_SWAP);
424	case FS_BSDFFS:
425		return (PART_FREEBSD_UFS);
426	case FS_VINUM:
427		return (PART_FREEBSD_VINUM);
428	case FS_ZFS:
429		return (PART_FREEBSD_ZFS);
430	}
431	return (PART_UNKNOWN);
432}
433
434struct ptable*
435ptable_bsdread(struct ptable *table, void *dev, diskread_t dread)
436{
437	struct disklabel *dl;
438	struct partition *part;
439	struct pentry *entry;
440	u_char *buf;
441	uint32_t raw_offset;
442	int i;
443
444	if (table->sectorsize < sizeof(struct disklabel)) {
445		DEBUG("Too small sectorsize");
446		return (table);
447	}
448	buf = malloc(table->sectorsize);
449	if (buf == NULL)
450		return (table);
451	if (dread(dev, buf, 1, 1) != 0) {
452		DEBUG("read failed");
453		ptable_close(table);
454		table = NULL;
455		goto out;
456	}
457	dl = (struct disklabel *)buf;
458	if (le32toh(dl->d_magic) != DISKMAGIC &&
459	    le32toh(dl->d_magic2) != DISKMAGIC)
460		goto out;
461	if (le32toh(dl->d_secsize) != table->sectorsize) {
462		DEBUG("unsupported sector size");
463		goto out;
464	}
465	dl->d_npartitions = le16toh(dl->d_npartitions);
466	if (dl->d_npartitions > 20 || dl->d_npartitions < 8) {
467		DEBUG("invalid number of partitions");
468		goto out;
469	}
470	part = &dl->d_partitions[0];
471	raw_offset = le32toh(part[RAW_PART].p_offset);
472	for (i = 0; i < dl->d_npartitions; i++, part++) {
473		if (i == RAW_PART)
474			continue;
475		if (part->p_size == 0)
476			continue;
477		entry = malloc(sizeof(*entry));
478		if (entry == NULL)
479			break;
480		entry->part.start = le32toh(part->p_offset) - raw_offset;
481		entry->part.end = entry->part.start +
482		    le32toh(part->p_size) + 1;
483		entry->part.type = bsd_parttype(part->p_fstype);
484		entry->part.index = i; /* starts from zero */
485		entry->type.bsd = part->p_fstype;
486		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
487		DEBUG("new BSD partition added");
488	}
489	table->type = PTABLE_BSD;
490out:
491	free(buf);
492	return (table);
493}
494
495#ifdef LOADER_VTOC8_SUPPORT
496static enum partition_type
497vtoc8_parttype(uint16_t type)
498{
499
500	switch (type) {
501	case VTOC_TAG_FREEBSD_NANDFS:
502		return (PART_FREEBSD_NANDFS);
503	case VTOC_TAG_FREEBSD_SWAP:
504		return (PART_FREEBSD_SWAP);
505	case VTOC_TAG_FREEBSD_UFS:
506		return (PART_FREEBSD_UFS);
507	case VTOC_TAG_FREEBSD_VINUM:
508		return (PART_FREEBSD_VINUM);
509	case VTOC_TAG_FREEBSD_ZFS:
510		return (PART_FREEBSD_ZFS);
511	};
512	return (PART_UNKNOWN);
513}
514
515static struct ptable*
516ptable_vtoc8read(struct ptable *table, void *dev, diskread_t dread)
517{
518	struct pentry *entry;
519	struct vtoc8 *dl;
520	u_char *buf;
521	uint16_t sum, heads, sectors;
522	int i;
523
524	if (table->sectorsize != sizeof(struct vtoc8))
525		return (table);
526	buf = malloc(table->sectorsize);
527	if (buf == NULL)
528		return (table);
529	if (dread(dev, buf, 1, 0) != 0) {
530		DEBUG("read failed");
531		ptable_close(table);
532		table = NULL;
533		goto out;
534	}
535	dl = (struct vtoc8 *)buf;
536	/* Check the sum */
537	for (i = sum = 0; i < sizeof(struct vtoc8); i += sizeof(sum))
538		sum ^= be16dec(buf + i);
539	if (sum != 0) {
540		DEBUG("incorrect checksum");
541		goto out;
542	}
543	if (be16toh(dl->nparts) != VTOC8_NPARTS) {
544		DEBUG("invalid number of entries");
545		goto out;
546	}
547	sectors = be16toh(dl->nsecs);
548	heads = be16toh(dl->nheads);
549	if (sectors * heads == 0) {
550		DEBUG("invalid geometry");
551		goto out;
552	}
553	for (i = 0; i < VTOC8_NPARTS; i++) {
554		dl->part[i].tag = be16toh(dl->part[i].tag);
555		if (i == VTOC_RAW_PART ||
556		    dl->part[i].tag == VTOC_TAG_UNASSIGNED)
557			continue;
558		entry = malloc(sizeof(*entry));
559		if (entry == NULL)
560			break;
561		entry->part.start = be32toh(dl->map[i].cyl) * heads * sectors;
562		entry->part.end = be32toh(dl->map[i].nblks) +
563		    entry->part.start - 1;
564		entry->part.type = vtoc8_parttype(dl->part[i].tag);
565		entry->part.index = i; /* starts from zero */
566		entry->type.vtoc8 = dl->part[i].tag;
567		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
568		DEBUG("new VTOC8 partition added");
569	}
570	table->type = PTABLE_VTOC8;
571out:
572	free(buf);
573	return (table);
574
575}
576#endif /* LOADER_VTOC8_SUPPORT */
577
578struct ptable*
579ptable_open(void *dev, off_t sectors, uint16_t sectorsize,
580    diskread_t *dread)
581{
582	struct dos_partition *dp;
583	struct ptable *table;
584	u_char *buf;
585	int i, count;
586#ifdef LOADER_MBR_SUPPORT
587	struct pentry *entry;
588	uint32_t start, end;
589	int has_ext;
590#endif
591	table = NULL;
592	buf = malloc(sectorsize);
593	if (buf == NULL)
594		return (NULL);
595	/* First, read the MBR. */
596	if (dread(dev, buf, 1, DOSBBSECTOR) != 0) {
597		DEBUG("read failed");
598		goto out;
599	}
600
601	table = malloc(sizeof(*table));
602	if (table == NULL)
603		goto out;
604	table->sectors = sectors;
605	table->sectorsize = sectorsize;
606	table->type = PTABLE_NONE;
607	STAILQ_INIT(&table->entries);
608
609#ifdef LOADER_VTOC8_SUPPORT
610	if (be16dec(buf + offsetof(struct vtoc8, magic)) == VTOC_MAGIC) {
611		if (ptable_vtoc8read(table, dev, dread) == NULL) {
612			/* Read error. */
613			table = NULL;
614			goto out;
615		} else if (table->type == PTABLE_VTOC8)
616			goto out;
617	}
618#endif
619	/* Check the BSD label. */
620	if (ptable_bsdread(table, dev, dread) == NULL) { /* Read error. */
621		table = NULL;
622		goto out;
623	} else if (table->type == PTABLE_BSD)
624		goto out;
625
626#if defined(LOADER_GPT_SUPPORT) || defined(LOADER_MBR_SUPPORT)
627	/* Check the MBR magic. */
628	if (buf[DOSMAGICOFFSET] != 0x55 ||
629	    buf[DOSMAGICOFFSET + 1] != 0xaa) {
630		DEBUG("magic sequence not found");
631		goto out;
632	}
633	/* Check that we have PMBR. Also do some validation. */
634	dp = (struct dos_partition *)(buf + DOSPARTOFF);
635	for (i = 0, count = 0; i < NDOSPART; i++) {
636		if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) {
637			DEBUG("invalid partition flag %x", dp[i].dp_flag);
638			goto out;
639		}
640#ifdef LOADER_GPT_SUPPORT
641		if (dp[i].dp_typ == DOSPTYP_PMBR) {
642			table->type = PTABLE_GPT;
643			DEBUG("PMBR detected");
644		}
645#endif
646		if (dp[i].dp_typ != 0)
647			count++;
648	}
649	/* Do we have some invalid values? */
650	if (table->type == PTABLE_GPT && count > 1) {
651		if (dp[1].dp_typ != DOSPTYP_HFS) {
652			table->type = PTABLE_NONE;
653			DEBUG("Incorrect PMBR, ignore it");
654		} else
655			DEBUG("Bootcamp detected");
656	}
657#ifdef LOADER_GPT_SUPPORT
658	if (table->type == PTABLE_GPT) {
659		table = ptable_gptread(table, dev, dread);
660		goto out;
661	}
662#endif
663#ifdef LOADER_MBR_SUPPORT
664	/* Read MBR. */
665	table->type = PTABLE_MBR;
666	for (i = has_ext = 0; i < NDOSPART; i++) {
667		if (dp[i].dp_typ == 0)
668			continue;
669		start = le32dec(&(dp[i].dp_start));
670		end = le32dec(&(dp[i].dp_size));
671		if (start == 0 || end == 0)
672			continue;
673#if 0	/* Some BIOSes return an incorrect number of sectors */
674		if (start + end - 1 >= sectors)
675			continue;	/* XXX: ignore */
676#endif
677		if (dp[i].dp_typ == DOSPTYP_EXT ||
678		    dp[i].dp_typ == DOSPTYP_EXTLBA)
679			has_ext = 1;
680		entry = malloc(sizeof(*entry));
681		if (entry == NULL)
682			break;
683		entry->part.start = start;
684		entry->part.end = start + end - 1;
685		entry->part.index = i + 1;
686		entry->part.type = mbr_parttype(dp[i].dp_typ);
687		entry->flags = dp[i].dp_flag;
688		entry->type.mbr = dp[i].dp_typ;
689		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
690		DEBUG("new MBR partition added");
691	}
692	if (has_ext) {
693		table = ptable_ebrread(table, dev, dread);
694		/* FALLTHROUGH */
695	}
696#endif /* LOADER_MBR_SUPPORT */
697#endif /* LOADER_MBR_SUPPORT || LOADER_GPT_SUPPORT */
698out:
699	free(buf);
700	return (table);
701}
702
703void
704ptable_close(struct ptable *table)
705{
706	struct pentry *entry;
707
708	while (!STAILQ_EMPTY(&table->entries)) {
709		entry = STAILQ_FIRST(&table->entries);
710		STAILQ_REMOVE_HEAD(&table->entries, entry);
711		free(entry);
712	}
713	free(table);
714}
715
716enum ptable_type
717ptable_gettype(const struct ptable *table)
718{
719
720	return (table->type);
721}
722
723int
724ptable_getpart(const struct ptable *table, struct ptable_entry *part, int index)
725{
726	struct pentry *entry;
727
728	if (part == NULL || table == NULL)
729		return (EINVAL);
730
731	STAILQ_FOREACH(entry, &table->entries, entry) {
732		if (entry->part.index != index)
733			continue;
734		memcpy(part, &entry->part, sizeof(*part));
735		return (0);
736	}
737	return (ENOENT);
738}
739
740/*
741 * Search for a slice with the following preferences:
742 *
743 * 1: Active FreeBSD slice
744 * 2: Non-active FreeBSD slice
745 * 3: Active Linux slice
746 * 4: non-active Linux slice
747 * 5: Active FAT/FAT32 slice
748 * 6: non-active FAT/FAT32 slice
749 */
750#define PREF_RAWDISK	0
751#define PREF_FBSD_ACT	1
752#define PREF_FBSD	2
753#define PREF_LINUX_ACT	3
754#define PREF_LINUX	4
755#define PREF_DOS_ACT	5
756#define PREF_DOS	6
757#define PREF_NONE	7
758int
759ptable_getbestpart(const struct ptable *table, struct ptable_entry *part)
760{
761	struct pentry *entry, *best;
762	int pref, preflevel;
763
764	if (part == NULL || table == NULL)
765		return (EINVAL);
766
767	best = NULL;
768	preflevel = pref = PREF_NONE;
769	STAILQ_FOREACH(entry, &table->entries, entry) {
770#ifdef LOADER_MBR_SUPPORT
771		if (table->type == PTABLE_MBR) {
772			switch (entry->type.mbr) {
773			case DOSPTYP_386BSD:
774				pref = entry->flags & 0x80 ? PREF_FBSD_ACT:
775				    PREF_FBSD;
776				break;
777			case DOSPTYP_LINUX:
778				pref = entry->flags & 0x80 ? PREF_LINUX_ACT:
779				    PREF_LINUX;
780				break;
781			case 0x01:		/* DOS/Windows */
782			case 0x04:
783			case 0x06:
784			case 0x0c:
785			case 0x0e:
786			case DOSPTYP_FAT32:
787				pref = entry->flags & 0x80 ? PREF_DOS_ACT:
788				    PREF_DOS;
789				break;
790			default:
791				pref = PREF_NONE;
792			}
793		}
794#endif /* LOADER_MBR_SUPPORT */
795#ifdef LOADER_GPT_SUPPORT
796		if (table->type == PTABLE_GPT) {
797			if (entry->part.type == PART_DOS)
798				pref = PREF_DOS;
799			else if (entry->part.type == PART_FREEBSD_UFS ||
800			    entry->part.type == PART_FREEBSD_ZFS)
801				pref = PREF_FBSD;
802			else
803				pref = PREF_NONE;
804		}
805#endif /* LOADER_GPT_SUPPORT */
806		if (pref < preflevel) {
807			preflevel = pref;
808			best = entry;
809		}
810	}
811	if (best != NULL) {
812		memcpy(part, &best->part, sizeof(*part));
813		return (0);
814	}
815	return (ENOENT);
816}
817
818void
819ptable_iterate(const struct ptable *table, void *arg, ptable_iterate_t *iter)
820{
821	struct pentry *entry;
822	char name[32];
823
824	name[0] = '\0';
825	STAILQ_FOREACH(entry, &table->entries, entry) {
826#ifdef LOADER_MBR_SUPPORT
827		if (table->type == PTABLE_MBR)
828			sprintf(name, "s%d", entry->part.index);
829		else
830#endif
831#ifdef LOADER_GPT_SUPPORT
832		if (table->type == PTABLE_GPT)
833			sprintf(name, "p%d", entry->part.index);
834		else
835#endif
836#ifdef LOADER_VTOC8_SUPPORT
837		if (table->type == PTABLE_VTOC8)
838			sprintf(name, "%c", (u_char) 'a' +
839			    entry->part.index);
840		else
841#endif
842		if (table->type == PTABLE_BSD)
843			sprintf(name, "%c", (u_char) 'a' +
844			    entry->part.index);
845		iter(arg, name, &entry->part);
846	}
847}
848
849