xref: /freebsd/stand/common/part.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  */
26ca987d46SWarner Losh 
27ca987d46SWarner Losh #include <stand.h>
28ca987d46SWarner Losh #include <sys/param.h>
29ca987d46SWarner Losh #include <sys/diskmbr.h>
30ca987d46SWarner Losh #include <sys/disklabel.h>
31ca987d46SWarner Losh #include <sys/endian.h>
32ca987d46SWarner Losh #include <sys/gpt.h>
33ca987d46SWarner Losh #include <sys/stddef.h>
34ca987d46SWarner Losh #include <sys/queue.h>
35ca987d46SWarner Losh 
3648990fceSBenno Rice #include <fs/cd9660/iso.h>
3748990fceSBenno Rice 
3827bae615SToomas Soome #include <zlib.h>
39ca987d46SWarner Losh #include <part.h>
40ca987d46SWarner Losh #include <uuid.h>
41ca987d46SWarner Losh 
42ca987d46SWarner Losh #ifdef PART_DEBUG
437325df02SKyle Evans #define	DPRINTF(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
44ca987d46SWarner Losh #else
4582c29d4fSToomas Soome #define	DPRINTF(fmt, args...)	((void)0)
46ca987d46SWarner Losh #endif
47ca987d46SWarner Losh 
48ca987d46SWarner Losh #ifdef LOADER_GPT_SUPPORT
49ca987d46SWarner Losh #define	MAXTBLSZ	64
50ca987d46SWarner Losh static const uuid_t gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
51ca987d46SWarner Losh static const uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
52ca987d46SWarner Losh static const uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
53ca987d46SWarner Losh static const uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI;
54ca987d46SWarner Losh static const uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
55ca987d46SWarner Losh static const uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
56ca987d46SWarner Losh static const uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
57ca987d46SWarner Losh static const uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
58ca987d46SWarner Losh static const uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
59124003d5SToomas Soome static const uuid_t gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
60ca987d46SWarner Losh #endif
61ca987d46SWarner Losh 
62ca987d46SWarner Losh struct pentry {
63ca987d46SWarner Losh 	struct ptable_entry	part;
64ca987d46SWarner Losh 	uint64_t		flags;
65ca987d46SWarner Losh 	union {
66ca987d46SWarner Losh 		uint8_t bsd;
67ca987d46SWarner Losh 		uint8_t	mbr;
68ca987d46SWarner Losh 		uuid_t	gpt;
69ca987d46SWarner Losh 	} type;
70ca987d46SWarner Losh 	STAILQ_ENTRY(pentry)	entry;
71ca987d46SWarner Losh };
72ca987d46SWarner Losh 
73ca987d46SWarner Losh struct ptable {
74ca987d46SWarner Losh 	enum ptable_type	type;
75ca987d46SWarner Losh 	uint16_t		sectorsize;
76ca987d46SWarner Losh 	uint64_t		sectors;
77ca987d46SWarner Losh 
78ca987d46SWarner Losh 	STAILQ_HEAD(, pentry)	entries;
79ca987d46SWarner Losh };
80ca987d46SWarner Losh 
81ca987d46SWarner Losh static struct parttypes {
82ca987d46SWarner Losh 	enum partition_type	type;
83ca987d46SWarner Losh 	const char		*desc;
84ca987d46SWarner Losh } ptypes[] = {
85ca987d46SWarner Losh 	{ PART_UNKNOWN,		"Unknown" },
86ca987d46SWarner Losh 	{ PART_EFI,		"EFI" },
87ca987d46SWarner Losh 	{ PART_FREEBSD,		"FreeBSD" },
88ca987d46SWarner Losh 	{ PART_FREEBSD_BOOT,	"FreeBSD boot" },
89ca987d46SWarner Losh 	{ PART_FREEBSD_UFS,	"FreeBSD UFS" },
90ca987d46SWarner Losh 	{ PART_FREEBSD_ZFS,	"FreeBSD ZFS" },
91ca987d46SWarner Losh 	{ PART_FREEBSD_SWAP,	"FreeBSD swap" },
92ca987d46SWarner Losh 	{ PART_FREEBSD_VINUM,	"FreeBSD vinum" },
93ca987d46SWarner Losh 	{ PART_LINUX,		"Linux" },
94ca987d46SWarner Losh 	{ PART_LINUX_SWAP,	"Linux swap" },
95ca987d46SWarner Losh 	{ PART_DOS,		"DOS/Windows" },
9648990fceSBenno Rice 	{ PART_ISO9660,		"ISO9660" },
97124003d5SToomas Soome 	{ PART_APFS,		"APFS" },
98ca987d46SWarner Losh };
99ca987d46SWarner Losh 
100ca987d46SWarner Losh const char *
parttype2str(enum partition_type type)101ca987d46SWarner Losh parttype2str(enum partition_type type)
102ca987d46SWarner Losh {
103ca987d46SWarner Losh 	size_t i;
104ca987d46SWarner Losh 
105ca987d46SWarner Losh 	for (i = 0; i < nitems(ptypes); i++)
106ca987d46SWarner Losh 		if (ptypes[i].type == type)
107ca987d46SWarner Losh 			return (ptypes[i].desc);
108ca987d46SWarner Losh 	return (ptypes[0].desc);
109ca987d46SWarner Losh }
110ca987d46SWarner Losh 
111ca987d46SWarner Losh #ifdef LOADER_GPT_SUPPORT
112ca987d46SWarner Losh static void
uuid_letoh(uuid_t * uuid)113ca987d46SWarner Losh uuid_letoh(uuid_t *uuid)
114ca987d46SWarner Losh {
115ca987d46SWarner Losh 
116ca987d46SWarner Losh 	uuid->time_low = le32toh(uuid->time_low);
117ca987d46SWarner Losh 	uuid->time_mid = le16toh(uuid->time_mid);
118ca987d46SWarner Losh 	uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
119ca987d46SWarner Losh }
120ca987d46SWarner Losh 
121ca987d46SWarner Losh static enum partition_type
gpt_parttype(uuid_t type)122ca987d46SWarner Losh gpt_parttype(uuid_t type)
123ca987d46SWarner Losh {
124ca987d46SWarner Losh 
125ca987d46SWarner Losh 	if (uuid_equal(&type, &gpt_uuid_efi, NULL))
126ca987d46SWarner Losh 		return (PART_EFI);
127ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_ms_basic_data, NULL))
128ca987d46SWarner Losh 		return (PART_DOS);
129ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_freebsd_boot, NULL))
130ca987d46SWarner Losh 		return (PART_FREEBSD_BOOT);
131ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_freebsd_ufs, NULL))
132ca987d46SWarner Losh 		return (PART_FREEBSD_UFS);
133ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_freebsd_zfs, NULL))
134ca987d46SWarner Losh 		return (PART_FREEBSD_ZFS);
135ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_freebsd_swap, NULL))
136ca987d46SWarner Losh 		return (PART_FREEBSD_SWAP);
137ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_freebsd_vinum, NULL))
138ca987d46SWarner Losh 		return (PART_FREEBSD_VINUM);
139ca987d46SWarner Losh 	else if (uuid_equal(&type, &gpt_uuid_freebsd, NULL))
140ca987d46SWarner Losh 		return (PART_FREEBSD);
141124003d5SToomas Soome 	else if (uuid_equal(&type, &gpt_uuid_apple_apfs, NULL))
142124003d5SToomas Soome 		return (PART_APFS);
143ca987d46SWarner Losh 	return (PART_UNKNOWN);
144ca987d46SWarner Losh }
145ca987d46SWarner Losh 
146ca987d46SWarner Losh static struct gpt_hdr *
gpt_checkhdr(struct gpt_hdr * hdr,uint64_t lba_self,uint64_t lba_last,uint16_t sectorsize)147ca987d46SWarner Losh gpt_checkhdr(struct gpt_hdr *hdr, uint64_t lba_self, uint64_t lba_last,
148ca987d46SWarner Losh     uint16_t sectorsize)
149ca987d46SWarner Losh {
150ca987d46SWarner Losh 	uint32_t sz, crc;
151ca987d46SWarner Losh 
152ca987d46SWarner Losh 	if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0) {
1537325df02SKyle Evans 		DPRINTF("no GPT signature");
154ca987d46SWarner Losh 		return (NULL);
155ca987d46SWarner Losh 	}
156ca987d46SWarner Losh 	sz = le32toh(hdr->hdr_size);
157ca987d46SWarner Losh 	if (sz < 92 || sz > sectorsize) {
1587325df02SKyle Evans 		DPRINTF("invalid GPT header size: %d", sz);
159ca987d46SWarner Losh 		return (NULL);
160ca987d46SWarner Losh 	}
161ca987d46SWarner Losh 	crc = le32toh(hdr->hdr_crc_self);
16227bae615SToomas Soome 	hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
16327bae615SToomas Soome 	if (crc32(hdr->hdr_crc_self, (const Bytef *)hdr, sz) != crc) {
1647325df02SKyle Evans 		DPRINTF("GPT header's CRC doesn't match");
165ca987d46SWarner Losh 		return (NULL);
166ca987d46SWarner Losh 	}
167ca987d46SWarner Losh 	hdr->hdr_crc_self = crc;
168ca987d46SWarner Losh 	hdr->hdr_revision = le32toh(hdr->hdr_revision);
169ca987d46SWarner Losh 	if (hdr->hdr_revision < GPT_HDR_REVISION) {
1707325df02SKyle Evans 		DPRINTF("unsupported GPT revision %d", hdr->hdr_revision);
171ca987d46SWarner Losh 		return (NULL);
172ca987d46SWarner Losh 	}
173ca987d46SWarner Losh 	hdr->hdr_lba_self = le64toh(hdr->hdr_lba_self);
174ca987d46SWarner Losh 	if (hdr->hdr_lba_self != lba_self) {
1757325df02SKyle Evans 		DPRINTF("self LBA doesn't match");
176ca987d46SWarner Losh 		return (NULL);
177ca987d46SWarner Losh 	}
178ca987d46SWarner Losh 	hdr->hdr_lba_alt = le64toh(hdr->hdr_lba_alt);
179ca987d46SWarner Losh 	if (hdr->hdr_lba_alt == hdr->hdr_lba_self) {
1807325df02SKyle Evans 		DPRINTF("invalid alternate LBA");
181ca987d46SWarner Losh 		return (NULL);
182ca987d46SWarner Losh 	}
183ca987d46SWarner Losh 	hdr->hdr_entries = le32toh(hdr->hdr_entries);
184ca987d46SWarner Losh 	hdr->hdr_entsz = le32toh(hdr->hdr_entsz);
185ca987d46SWarner Losh 	if (hdr->hdr_entries == 0 ||
186ca987d46SWarner Losh 	    hdr->hdr_entsz < sizeof(struct gpt_ent) ||
187ca987d46SWarner Losh 	    sectorsize % hdr->hdr_entsz != 0) {
1887325df02SKyle Evans 		DPRINTF("invalid entry size or number of entries");
189ca987d46SWarner Losh 		return (NULL);
190ca987d46SWarner Losh 	}
191ca987d46SWarner Losh 	hdr->hdr_lba_start = le64toh(hdr->hdr_lba_start);
192ca987d46SWarner Losh 	hdr->hdr_lba_end = le64toh(hdr->hdr_lba_end);
193ca987d46SWarner Losh 	hdr->hdr_lba_table = le64toh(hdr->hdr_lba_table);
194ca987d46SWarner Losh 	hdr->hdr_crc_table = le32toh(hdr->hdr_crc_table);
195ca987d46SWarner Losh 	uuid_letoh(&hdr->hdr_uuid);
196ca987d46SWarner Losh 	return (hdr);
197ca987d46SWarner Losh }
198ca987d46SWarner Losh 
199ca987d46SWarner Losh static int
gpt_checktbl(const struct gpt_hdr * hdr,uint8_t * tbl,size_t size,uint64_t lba_last)200ca987d46SWarner Losh gpt_checktbl(const struct gpt_hdr *hdr, uint8_t *tbl, size_t size,
201ca987d46SWarner Losh     uint64_t lba_last)
202ca987d46SWarner Losh {
203ca987d46SWarner Losh 	struct gpt_ent *ent;
204ca987d46SWarner Losh 	uint32_t i, cnt;
205ca987d46SWarner Losh 
206ca987d46SWarner Losh 	cnt = size / hdr->hdr_entsz;
207ca987d46SWarner Losh 	if (hdr->hdr_entries <= cnt) {
208ca987d46SWarner Losh 		cnt = hdr->hdr_entries;
209ca987d46SWarner Losh 		/* Check CRC only when buffer size is enough for table. */
210ca987d46SWarner Losh 		if (hdr->hdr_crc_table !=
21127bae615SToomas Soome 		    crc32(0, tbl, hdr->hdr_entries * hdr->hdr_entsz)) {
2127325df02SKyle Evans 			DPRINTF("GPT table's CRC doesn't match");
213ca987d46SWarner Losh 			return (-1);
214ca987d46SWarner Losh 		}
215ca987d46SWarner Losh 	}
216ca987d46SWarner Losh 	for (i = 0; i < cnt; i++) {
217ca987d46SWarner Losh 		ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
218ca987d46SWarner Losh 		uuid_letoh(&ent->ent_type);
219ca987d46SWarner Losh 		if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
220ca987d46SWarner Losh 			continue;
221ca987d46SWarner Losh 		ent->ent_lba_start = le64toh(ent->ent_lba_start);
222ca987d46SWarner Losh 		ent->ent_lba_end = le64toh(ent->ent_lba_end);
223ca987d46SWarner Losh 	}
224ca987d46SWarner Losh 	return (0);
225ca987d46SWarner Losh }
226ca987d46SWarner Losh 
227ca987d46SWarner Losh static struct ptable *
ptable_gptread(struct ptable * table,void * dev,diskread_t dread)228ca987d46SWarner Losh ptable_gptread(struct ptable *table, void *dev, diskread_t dread)
229ca987d46SWarner Losh {
230ca987d46SWarner Losh 	struct pentry *entry;
231ca987d46SWarner Losh 	struct gpt_hdr *phdr, hdr;
232ca987d46SWarner Losh 	struct gpt_ent *ent;
233ca987d46SWarner Losh 	uint8_t *buf, *tbl;
234ca987d46SWarner Losh 	uint64_t offset;
235ca987d46SWarner Losh 	int pri, sec;
236ca987d46SWarner Losh 	size_t size, i;
237ca987d46SWarner Losh 
238ca987d46SWarner Losh 	buf = malloc(table->sectorsize);
239ca987d46SWarner Losh 	if (buf == NULL)
240ca987d46SWarner Losh 		return (NULL);
241ca987d46SWarner Losh 	tbl = malloc(table->sectorsize * MAXTBLSZ);
242ca987d46SWarner Losh 	if (tbl == NULL) {
243ca987d46SWarner Losh 		free(buf);
244ca987d46SWarner Losh 		return (NULL);
245ca987d46SWarner Losh 	}
246ca987d46SWarner Losh 	/* Read the primary GPT header. */
247ca987d46SWarner Losh 	if (dread(dev, buf, 1, 1) != 0) {
248ca987d46SWarner Losh 		ptable_close(table);
249ca987d46SWarner Losh 		table = NULL;
250ca987d46SWarner Losh 		goto out;
251ca987d46SWarner Losh 	}
252ca987d46SWarner Losh 	pri = sec = 0;
253ca987d46SWarner Losh 	/* Check the primary GPT header. */
254ca987d46SWarner Losh 	phdr = gpt_checkhdr((struct gpt_hdr *)buf, 1, table->sectors - 1,
255ca987d46SWarner Losh 	    table->sectorsize);
256ca987d46SWarner Losh 	if (phdr != NULL) {
257ca987d46SWarner Losh 		/* Read the primary GPT table. */
258ca987d46SWarner Losh 		size = MIN(MAXTBLSZ,
259ca987d46SWarner Losh 		    howmany(phdr->hdr_entries * phdr->hdr_entsz,
260ca987d46SWarner Losh 		        table->sectorsize));
261ca987d46SWarner Losh 		if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
262ca987d46SWarner Losh 		    gpt_checktbl(phdr, tbl, size * table->sectorsize,
263ca987d46SWarner Losh 		    table->sectors - 1) == 0) {
264ca987d46SWarner Losh 			memcpy(&hdr, phdr, sizeof(hdr));
265ca987d46SWarner Losh 			pri = 1;
266ca987d46SWarner Losh 		}
267ca987d46SWarner Losh 	}
268ca987d46SWarner Losh 	offset = pri ? hdr.hdr_lba_alt: table->sectors - 1;
269ca987d46SWarner Losh 	/* Read the backup GPT header. */
270ca987d46SWarner Losh 	if (dread(dev, buf, 1, offset) != 0)
271ca987d46SWarner Losh 		phdr = NULL;
272ca987d46SWarner Losh 	else
273ca987d46SWarner Losh 		phdr = gpt_checkhdr((struct gpt_hdr *)buf, offset,
274ca987d46SWarner Losh 		    table->sectors - 1, table->sectorsize);
275ca987d46SWarner Losh 	if (phdr != NULL) {
276ca987d46SWarner Losh 		/*
277ca987d46SWarner Losh 		 * Compare primary and backup headers.
278ca987d46SWarner Losh 		 * If they are equal, then we do not need to read backup
279ca987d46SWarner Losh 		 * table. If they are different, then prefer backup header
280ca987d46SWarner Losh 		 * and try to read backup table.
281ca987d46SWarner Losh 		 */
282ca987d46SWarner Losh 		if (pri == 0 ||
283ca987d46SWarner Losh 		    uuid_equal(&hdr.hdr_uuid, &phdr->hdr_uuid, NULL) == 0 ||
284ca987d46SWarner Losh 		    hdr.hdr_revision != phdr->hdr_revision ||
285ca987d46SWarner Losh 		    hdr.hdr_size != phdr->hdr_size ||
286ca987d46SWarner Losh 		    hdr.hdr_lba_start != phdr->hdr_lba_start ||
287ca987d46SWarner Losh 		    hdr.hdr_lba_end != phdr->hdr_lba_end ||
288ca987d46SWarner Losh 		    hdr.hdr_entries != phdr->hdr_entries ||
289ca987d46SWarner Losh 		    hdr.hdr_entsz != phdr->hdr_entsz ||
290ca987d46SWarner Losh 		    hdr.hdr_crc_table != phdr->hdr_crc_table) {
291ca987d46SWarner Losh 			/* Read the backup GPT table. */
292ca987d46SWarner Losh 			size = MIN(MAXTBLSZ,
293ca987d46SWarner Losh 				   howmany(phdr->hdr_entries * phdr->hdr_entsz,
294ca987d46SWarner Losh 				       table->sectorsize));
295ca987d46SWarner Losh 			if (dread(dev, tbl, size, phdr->hdr_lba_table) == 0 &&
296ca987d46SWarner Losh 			    gpt_checktbl(phdr, tbl, size * table->sectorsize,
297ca987d46SWarner Losh 			    table->sectors - 1) == 0) {
298ca987d46SWarner Losh 				memcpy(&hdr, phdr, sizeof(hdr));
299ca987d46SWarner Losh 				sec = 1;
300ca987d46SWarner Losh 			}
301ca987d46SWarner Losh 		}
302ca987d46SWarner Losh 	}
303ca987d46SWarner Losh 	if (pri == 0 && sec == 0) {
304ca987d46SWarner Losh 		/* Both primary and backup tables are invalid. */
305ca987d46SWarner Losh 		table->type = PTABLE_NONE;
306ca987d46SWarner Losh 		goto out;
307ca987d46SWarner Losh 	}
3087325df02SKyle Evans 	DPRINTF("GPT detected");
309ca987d46SWarner Losh 	size = MIN(hdr.hdr_entries * hdr.hdr_entsz,
310ca987d46SWarner Losh 	    MAXTBLSZ * table->sectorsize);
311ca987d46SWarner Losh 
312ca987d46SWarner Losh 	/*
313ca987d46SWarner Losh 	 * If the disk's sector count is smaller than the sector count recorded
314ca987d46SWarner Losh 	 * in the disk's GPT table header, set the table->sectors to the value
315ca987d46SWarner Losh 	 * recorded in GPT tables. This is done to work around buggy firmware
316ca987d46SWarner Losh 	 * that returns truncated disk sizes.
317ca987d46SWarner Losh 	 *
318ca987d46SWarner Losh 	 * Note, this is still not a foolproof way to get disk's size. For
319ca987d46SWarner Losh 	 * example, an image file can be truncated when copied to smaller media.
320ca987d46SWarner Losh 	 */
321ca987d46SWarner Losh 	table->sectors = hdr.hdr_lba_alt + 1;
322ca987d46SWarner Losh 
323ca987d46SWarner Losh 	for (i = 0; i < size / hdr.hdr_entsz; i++) {
324ca987d46SWarner Losh 		ent = (struct gpt_ent *)(tbl + i * hdr.hdr_entsz);
325ca987d46SWarner Losh 		if (uuid_equal(&ent->ent_type, &gpt_uuid_unused, NULL))
326ca987d46SWarner Losh 			continue;
327ca987d46SWarner Losh 
328ca987d46SWarner Losh 		/* Simple sanity checks. */
329ca987d46SWarner Losh 		if (ent->ent_lba_start < hdr.hdr_lba_start ||
330ca987d46SWarner Losh 		    ent->ent_lba_end > hdr.hdr_lba_end ||
331ca987d46SWarner Losh 		    ent->ent_lba_start > ent->ent_lba_end)
332ca987d46SWarner Losh 			continue;
333ca987d46SWarner Losh 
334ca987d46SWarner Losh 		entry = malloc(sizeof(*entry));
335ca987d46SWarner Losh 		if (entry == NULL)
336ca987d46SWarner Losh 			break;
337ca987d46SWarner Losh 		entry->part.start = ent->ent_lba_start;
338ca987d46SWarner Losh 		entry->part.end = ent->ent_lba_end;
339ca987d46SWarner Losh 		entry->part.index = i + 1;
340ca987d46SWarner Losh 		entry->part.type = gpt_parttype(ent->ent_type);
341ca987d46SWarner Losh 		entry->flags = le64toh(ent->ent_attr);
342ca987d46SWarner Losh 		memcpy(&entry->type.gpt, &ent->ent_type, sizeof(uuid_t));
343ca987d46SWarner Losh 		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
3447325df02SKyle Evans 		DPRINTF("new GPT partition added");
345ca987d46SWarner Losh 	}
346ca987d46SWarner Losh out:
347ca987d46SWarner Losh 	free(buf);
348ca987d46SWarner Losh 	free(tbl);
349ca987d46SWarner Losh 	return (table);
350ca987d46SWarner Losh }
351ca987d46SWarner Losh #endif /* LOADER_GPT_SUPPORT */
352ca987d46SWarner Losh 
353ca987d46SWarner Losh #ifdef LOADER_MBR_SUPPORT
354ca987d46SWarner Losh /* We do not need to support too many EBR partitions in the loader */
355ca987d46SWarner Losh #define	MAXEBRENTRIES		8
356ca987d46SWarner Losh static enum partition_type
mbr_parttype(uint8_t type)357ca987d46SWarner Losh mbr_parttype(uint8_t type)
358ca987d46SWarner Losh {
359ca987d46SWarner Losh 
360ca987d46SWarner Losh 	switch (type) {
361ca987d46SWarner Losh 	case DOSPTYP_386BSD:
362ca987d46SWarner Losh 		return (PART_FREEBSD);
363ca987d46SWarner Losh 	case DOSPTYP_LINSWP:
364ca987d46SWarner Losh 		return (PART_LINUX_SWAP);
365ca987d46SWarner Losh 	case DOSPTYP_LINUX:
366ca987d46SWarner Losh 		return (PART_LINUX);
367ca987d46SWarner Losh 	case 0x01:
368ca987d46SWarner Losh 	case 0x04:
369ca987d46SWarner Losh 	case 0x06:
370ca987d46SWarner Losh 	case 0x07:
371ca987d46SWarner Losh 	case 0x0b:
372ca987d46SWarner Losh 	case 0x0c:
373ca987d46SWarner Losh 	case 0x0e:
374ca987d46SWarner Losh 		return (PART_DOS);
375ca987d46SWarner Losh 	}
376ca987d46SWarner Losh 	return (PART_UNKNOWN);
377ca987d46SWarner Losh }
378ca987d46SWarner Losh 
379ca987d46SWarner Losh static struct ptable *
ptable_ebrread(struct ptable * table,void * dev,diskread_t dread)380ca987d46SWarner Losh ptable_ebrread(struct ptable *table, void *dev, diskread_t dread)
381ca987d46SWarner Losh {
382ca987d46SWarner Losh 	struct dos_partition *dp;
383ca987d46SWarner Losh 	struct pentry *e1, *entry;
384ca987d46SWarner Losh 	uint32_t start, end, offset;
385ca987d46SWarner Losh 	u_char *buf;
386ca987d46SWarner Losh 	int i, index;
387ca987d46SWarner Losh 
388ca987d46SWarner Losh 	STAILQ_FOREACH(e1, &table->entries, entry) {
389ca987d46SWarner Losh 		if (e1->type.mbr == DOSPTYP_EXT ||
390ca987d46SWarner Losh 		    e1->type.mbr == DOSPTYP_EXTLBA)
391ca987d46SWarner Losh 			break;
392ca987d46SWarner Losh 	}
393ca987d46SWarner Losh 	if (e1 == NULL)
394ca987d46SWarner Losh 		return (table);
395ca987d46SWarner Losh 	index = 5;
396ca987d46SWarner Losh 	offset = e1->part.start;
397ca987d46SWarner Losh 	buf = malloc(table->sectorsize);
398ca987d46SWarner Losh 	if (buf == NULL)
399ca987d46SWarner Losh 		return (table);
4007325df02SKyle Evans 	DPRINTF("EBR detected");
401ca987d46SWarner Losh 	for (i = 0; i < MAXEBRENTRIES; i++) {
402ca987d46SWarner Losh #if 0	/* Some BIOSes return an incorrect number of sectors */
403ca987d46SWarner Losh 		if (offset >= table->sectors)
404ca987d46SWarner Losh 			break;
405ca987d46SWarner Losh #endif
406ca987d46SWarner Losh 		if (dread(dev, buf, 1, offset) != 0)
407ca987d46SWarner Losh 			break;
408ca987d46SWarner Losh 		dp = (struct dos_partition *)(buf + DOSPARTOFF);
409ca987d46SWarner Losh 		if (dp[0].dp_typ == 0)
410ca987d46SWarner Losh 			break;
411ca987d46SWarner Losh 		start = le32toh(dp[0].dp_start);
412ca987d46SWarner Losh 		if (dp[0].dp_typ == DOSPTYP_EXT &&
413ca987d46SWarner Losh 		    dp[1].dp_typ == 0) {
414ca987d46SWarner Losh 			offset = e1->part.start + start;
415ca987d46SWarner Losh 			continue;
416ca987d46SWarner Losh 		}
417ca987d46SWarner Losh 		end = le32toh(dp[0].dp_size);
418ca987d46SWarner Losh 		entry = malloc(sizeof(*entry));
419ca987d46SWarner Losh 		if (entry == NULL)
420ca987d46SWarner Losh 			break;
421ca987d46SWarner Losh 		entry->part.start = offset + start;
422ca987d46SWarner Losh 		entry->part.end = entry->part.start + end - 1;
423ca987d46SWarner Losh 		entry->part.index = index++;
424ca987d46SWarner Losh 		entry->part.type = mbr_parttype(dp[0].dp_typ);
425ca987d46SWarner Losh 		entry->flags = dp[0].dp_flag;
426ca987d46SWarner Losh 		entry->type.mbr = dp[0].dp_typ;
427ca987d46SWarner Losh 		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
4287325df02SKyle Evans 		DPRINTF("new EBR partition added");
429ca987d46SWarner Losh 		if (dp[1].dp_typ == 0)
430ca987d46SWarner Losh 			break;
431ca987d46SWarner Losh 		offset = e1->part.start + le32toh(dp[1].dp_start);
432ca987d46SWarner Losh 	}
433ca987d46SWarner Losh 	free(buf);
434ca987d46SWarner Losh 	return (table);
435ca987d46SWarner Losh }
436ca987d46SWarner Losh #endif /* LOADER_MBR_SUPPORT */
437ca987d46SWarner Losh 
438ca987d46SWarner Losh static enum partition_type
bsd_parttype(uint8_t type)439ca987d46SWarner Losh bsd_parttype(uint8_t type)
440ca987d46SWarner Losh {
441ca987d46SWarner Losh 
442ca987d46SWarner Losh 	switch (type) {
443ca987d46SWarner Losh 	case FS_SWAP:
444ca987d46SWarner Losh 		return (PART_FREEBSD_SWAP);
445ca987d46SWarner Losh 	case FS_BSDFFS:
446ca987d46SWarner Losh 		return (PART_FREEBSD_UFS);
447ca987d46SWarner Losh 	case FS_VINUM:
448ca987d46SWarner Losh 		return (PART_FREEBSD_VINUM);
449ca987d46SWarner Losh 	case FS_ZFS:
450ca987d46SWarner Losh 		return (PART_FREEBSD_ZFS);
451ca987d46SWarner Losh 	}
452ca987d46SWarner Losh 	return (PART_UNKNOWN);
453ca987d46SWarner Losh }
454ca987d46SWarner Losh 
455ca987d46SWarner Losh static struct ptable *
ptable_bsdread(struct ptable * table,void * dev,diskread_t dread)456ca987d46SWarner Losh ptable_bsdread(struct ptable *table, void *dev, diskread_t dread)
457ca987d46SWarner Losh {
458ca987d46SWarner Losh 	struct disklabel *dl;
459ca987d46SWarner Losh 	struct partition *part;
460ca987d46SWarner Losh 	struct pentry *entry;
461ca987d46SWarner Losh 	uint8_t *buf;
462ca987d46SWarner Losh 	uint32_t raw_offset;
463ca987d46SWarner Losh 	int i;
464ca987d46SWarner Losh 
465ca987d46SWarner Losh 	if (table->sectorsize < sizeof(struct disklabel)) {
4667325df02SKyle Evans 		DPRINTF("Too small sectorsize");
467ca987d46SWarner Losh 		return (table);
468ca987d46SWarner Losh 	}
469ca987d46SWarner Losh 	buf = malloc(table->sectorsize);
470ca987d46SWarner Losh 	if (buf == NULL)
471ca987d46SWarner Losh 		return (table);
472ca987d46SWarner Losh 	if (dread(dev, buf, 1, 1) != 0) {
4737325df02SKyle Evans 		DPRINTF("read failed");
474ca987d46SWarner Losh 		ptable_close(table);
475ca987d46SWarner Losh 		table = NULL;
476ca987d46SWarner Losh 		goto out;
477ca987d46SWarner Losh 	}
478ca987d46SWarner Losh 	dl = (struct disklabel *)buf;
479ca987d46SWarner Losh 	if (le32toh(dl->d_magic) != DISKMAGIC &&
480ca987d46SWarner Losh 	    le32toh(dl->d_magic2) != DISKMAGIC)
481ca987d46SWarner Losh 		goto out;
482ca987d46SWarner Losh 	if (le32toh(dl->d_secsize) != table->sectorsize) {
4837325df02SKyle Evans 		DPRINTF("unsupported sector size");
484ca987d46SWarner Losh 		goto out;
485ca987d46SWarner Losh 	}
486ca987d46SWarner Losh 	dl->d_npartitions = le16toh(dl->d_npartitions);
487ca987d46SWarner Losh 	if (dl->d_npartitions > 20 || dl->d_npartitions < 8) {
4887325df02SKyle Evans 		DPRINTF("invalid number of partitions");
489ca987d46SWarner Losh 		goto out;
490ca987d46SWarner Losh 	}
4917325df02SKyle Evans 	DPRINTF("BSD detected");
492ca987d46SWarner Losh 	part = &dl->d_partitions[0];
493ca987d46SWarner Losh 	raw_offset = le32toh(part[RAW_PART].p_offset);
494ca987d46SWarner Losh 	for (i = 0; i < dl->d_npartitions; i++, part++) {
495ca987d46SWarner Losh 		if (i == RAW_PART)
496ca987d46SWarner Losh 			continue;
497ca987d46SWarner Losh 		if (part->p_size == 0)
498ca987d46SWarner Losh 			continue;
499ca987d46SWarner Losh 		entry = malloc(sizeof(*entry));
500ca987d46SWarner Losh 		if (entry == NULL)
501ca987d46SWarner Losh 			break;
502ca987d46SWarner Losh 		entry->part.start = le32toh(part->p_offset) - raw_offset;
503ca987d46SWarner Losh 		entry->part.end = entry->part.start +
504ca987d46SWarner Losh 		    le32toh(part->p_size) - 1;
505ca987d46SWarner Losh 		entry->part.type = bsd_parttype(part->p_fstype);
506ca987d46SWarner Losh 		entry->part.index = i; /* starts from zero */
507ca987d46SWarner Losh 		entry->type.bsd = part->p_fstype;
508ca987d46SWarner Losh 		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
5097325df02SKyle Evans 		DPRINTF("new BSD partition added");
510ca987d46SWarner Losh 	}
511ca987d46SWarner Losh 	table->type = PTABLE_BSD;
512ca987d46SWarner Losh out:
513ca987d46SWarner Losh 	free(buf);
514ca987d46SWarner Losh 	return (table);
515ca987d46SWarner Losh }
516ca987d46SWarner Losh 
51748990fceSBenno Rice #define cdb2devb(bno)   ((bno) * ISO_DEFAULT_BLOCK_SIZE / table->sectorsize)
51848990fceSBenno Rice 
51948990fceSBenno Rice static struct ptable *
ptable_iso9660read(struct ptable * table,void * dev,diskread_t dread)52048990fceSBenno Rice ptable_iso9660read(struct ptable *table, void *dev, diskread_t dread)
52148990fceSBenno Rice {
52248990fceSBenno Rice 	uint8_t *buf;
52348990fceSBenno Rice 	struct iso_primary_descriptor *vd;
52448990fceSBenno Rice 	struct pentry *entry;
52548990fceSBenno Rice 
52648990fceSBenno Rice 	buf = malloc(table->sectorsize);
52748990fceSBenno Rice 	if (buf == NULL)
52848990fceSBenno Rice 		return (table);
52948990fceSBenno Rice 
53048990fceSBenno Rice 	if (dread(dev, buf, 1, cdb2devb(16)) != 0) {
5317325df02SKyle Evans 		DPRINTF("read failed");
53248990fceSBenno Rice 		ptable_close(table);
53348990fceSBenno Rice 		table = NULL;
53448990fceSBenno Rice 		goto out;
53548990fceSBenno Rice 	}
53648990fceSBenno Rice 	vd = (struct iso_primary_descriptor *)buf;
53748990fceSBenno Rice 	if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
53848990fceSBenno Rice 		goto out;
53948990fceSBenno Rice 
54048990fceSBenno Rice 	entry = malloc(sizeof(*entry));
54148990fceSBenno Rice 	if (entry == NULL)
54248990fceSBenno Rice 		goto out;
54348990fceSBenno Rice 	entry->part.start = 0;
54448990fceSBenno Rice 	entry->part.end = table->sectors;
54548990fceSBenno Rice 	entry->part.type = PART_ISO9660;
54648990fceSBenno Rice 	entry->part.index = 0;
54748990fceSBenno Rice 	STAILQ_INSERT_TAIL(&table->entries, entry, entry);
54848990fceSBenno Rice 
54948990fceSBenno Rice 	table->type = PTABLE_ISO9660;
55048990fceSBenno Rice 
55148990fceSBenno Rice out:
55248990fceSBenno Rice 	free(buf);
55348990fceSBenno Rice 	return (table);
55448990fceSBenno Rice }
55548990fceSBenno Rice 
556ca987d46SWarner Losh struct ptable *
ptable_open(void * dev,uint64_t sectors,uint16_t sectorsize,diskread_t * dread)557ca987d46SWarner Losh ptable_open(void *dev, uint64_t sectors, uint16_t sectorsize,
558ca987d46SWarner Losh     diskread_t *dread)
559ca987d46SWarner Losh {
560ca987d46SWarner Losh 	struct dos_partition *dp;
561ca987d46SWarner Losh 	struct ptable *table;
562ca987d46SWarner Losh 	uint8_t *buf;
563ca987d46SWarner Losh #ifdef LOADER_MBR_SUPPORT
564ca987d46SWarner Losh 	struct pentry *entry;
565ca987d46SWarner Losh 	uint32_t start, end;
566ca987d46SWarner Losh 	int has_ext;
567ca987d46SWarner Losh #endif
568ca987d46SWarner Losh 	table = NULL;
56987d8d5eaSToomas Soome 	dp = NULL;
570ca987d46SWarner Losh 	buf = malloc(sectorsize);
571ca987d46SWarner Losh 	if (buf == NULL)
572ca987d46SWarner Losh 		return (NULL);
573ca987d46SWarner Losh 	/* First, read the MBR. */
574ca987d46SWarner Losh 	if (dread(dev, buf, 1, DOSBBSECTOR) != 0) {
5757325df02SKyle Evans 		DPRINTF("read failed");
576ca987d46SWarner Losh 		goto out;
577ca987d46SWarner Losh 	}
578ca987d46SWarner Losh 
579ca987d46SWarner Losh 	table = malloc(sizeof(*table));
580ca987d46SWarner Losh 	if (table == NULL)
581ca987d46SWarner Losh 		goto out;
582ca987d46SWarner Losh 	table->sectors = sectors;
583ca987d46SWarner Losh 	table->sectorsize = sectorsize;
584ca987d46SWarner Losh 	table->type = PTABLE_NONE;
585ca987d46SWarner Losh 	STAILQ_INIT(&table->entries);
586ca987d46SWarner Losh 
587a3fd276bSToomas Soome 	if (ptable_iso9660read(table, dev, dread) == NULL) {
588a3fd276bSToomas Soome 		/* Read error. */
589a3fd276bSToomas Soome 		table = NULL;
59048990fceSBenno Rice 		goto out;
591a3fd276bSToomas Soome 	} else if (table->type == PTABLE_ISO9660)
592a3fd276bSToomas Soome 		goto out;
59348990fceSBenno Rice 
594ca987d46SWarner Losh 	/* Check the BSD label. */
595ca987d46SWarner Losh 	if (ptable_bsdread(table, dev, dread) == NULL) { /* Read error. */
596ca987d46SWarner Losh 		table = NULL;
597ca987d46SWarner Losh 		goto out;
598ca987d46SWarner Losh 	} else if (table->type == PTABLE_BSD)
599ca987d46SWarner Losh 		goto out;
600ca987d46SWarner Losh 
601ca987d46SWarner Losh #if defined(LOADER_GPT_SUPPORT) || defined(LOADER_MBR_SUPPORT)
602ca987d46SWarner Losh 	/* Check the MBR magic. */
603ca987d46SWarner Losh 	if (buf[DOSMAGICOFFSET] != 0x55 ||
604ca987d46SWarner Losh 	    buf[DOSMAGICOFFSET + 1] != 0xaa) {
6057325df02SKyle Evans 		DPRINTF("magic sequence not found");
606ca987d46SWarner Losh #if defined(LOADER_GPT_SUPPORT)
607ca987d46SWarner Losh 		/* There is no PMBR, check that we have backup GPT */
608ca987d46SWarner Losh 		table->type = PTABLE_GPT;
609ca987d46SWarner Losh 		table = ptable_gptread(table, dev, dread);
610ca987d46SWarner Losh #endif
611ca987d46SWarner Losh 		goto out;
612ca987d46SWarner Losh 	}
613ca987d46SWarner Losh 	/* Check that we have PMBR. Also do some validation. */
61487d8d5eaSToomas Soome 	dp = malloc(NDOSPART * sizeof(struct dos_partition));
61587d8d5eaSToomas Soome 	if (dp == NULL)
61687d8d5eaSToomas Soome 		goto out;
61787d8d5eaSToomas Soome 	bcopy(buf + DOSPARTOFF, dp, NDOSPART * sizeof(struct dos_partition));
61887d8d5eaSToomas Soome 
6192e284464SToomas Soome 	/*
6202e284464SToomas Soome 	 * In mac we can have PMBR partition in hybrid MBR;
6212e284464SToomas Soome 	 * that is, MBR partition which has DOSPTYP_PMBR entry defined as
6222e284464SToomas Soome 	 * start sector 1. After DOSPTYP_PMBR, there may be other partitions.
6232e284464SToomas Soome 	 * UEFI compliant PMBR has no other partitions.
6242e284464SToomas Soome 	 */
625*f44c441eSToomas Soome 	for (int i = 0; i < NDOSPART; i++) {
626ca987d46SWarner Losh 		if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) {
6277325df02SKyle Evans 			DPRINTF("invalid partition flag %x", dp[i].dp_flag);
628ca987d46SWarner Losh 			goto out;
629ca987d46SWarner Losh 		}
630ca987d46SWarner Losh #ifdef LOADER_GPT_SUPPORT
6312e284464SToomas Soome 		if (dp[i].dp_typ == DOSPTYP_PMBR && dp[i].dp_start == 1) {
632ca987d46SWarner Losh 			table->type = PTABLE_GPT;
6337325df02SKyle Evans 			DPRINTF("PMBR detected");
634ca987d46SWarner Losh 		}
635ca987d46SWarner Losh #endif
636ca987d46SWarner Losh 	}
637ca987d46SWarner Losh #ifdef LOADER_GPT_SUPPORT
638ca987d46SWarner Losh 	if (table->type == PTABLE_GPT) {
639ca987d46SWarner Losh 		table = ptable_gptread(table, dev, dread);
640ca987d46SWarner Losh 		goto out;
641ca987d46SWarner Losh 	}
642ca987d46SWarner Losh #endif
643ca987d46SWarner Losh #ifdef LOADER_MBR_SUPPORT
644ca987d46SWarner Losh 	/* Read MBR. */
6457325df02SKyle Evans 	DPRINTF("MBR detected");
646ca987d46SWarner Losh 	table->type = PTABLE_MBR;
647*f44c441eSToomas Soome 	for (int i = has_ext = 0; i < NDOSPART; i++) {
648ca987d46SWarner Losh 		if (dp[i].dp_typ == 0)
649ca987d46SWarner Losh 			continue;
650ca987d46SWarner Losh 		start = le32dec(&(dp[i].dp_start));
651ca987d46SWarner Losh 		end = le32dec(&(dp[i].dp_size));
652ca987d46SWarner Losh 		if (start == 0 || end == 0)
653ca987d46SWarner Losh 			continue;
654ca987d46SWarner Losh #if 0	/* Some BIOSes return an incorrect number of sectors */
655ca987d46SWarner Losh 		if (start + end - 1 >= sectors)
656ca987d46SWarner Losh 			continue;	/* XXX: ignore */
657ca987d46SWarner Losh #endif
658ca987d46SWarner Losh 		if (dp[i].dp_typ == DOSPTYP_EXT ||
659ca987d46SWarner Losh 		    dp[i].dp_typ == DOSPTYP_EXTLBA)
660ca987d46SWarner Losh 			has_ext = 1;
661ca987d46SWarner Losh 		entry = malloc(sizeof(*entry));
662ca987d46SWarner Losh 		if (entry == NULL)
663ca987d46SWarner Losh 			break;
664ca987d46SWarner Losh 		entry->part.start = start;
665ca987d46SWarner Losh 		entry->part.end = start + end - 1;
666ca987d46SWarner Losh 		entry->part.index = i + 1;
667ca987d46SWarner Losh 		entry->part.type = mbr_parttype(dp[i].dp_typ);
668ca987d46SWarner Losh 		entry->flags = dp[i].dp_flag;
669ca987d46SWarner Losh 		entry->type.mbr = dp[i].dp_typ;
670ca987d46SWarner Losh 		STAILQ_INSERT_TAIL(&table->entries, entry, entry);
6717325df02SKyle Evans 		DPRINTF("new MBR partition added");
672ca987d46SWarner Losh 	}
673ca987d46SWarner Losh 	if (has_ext) {
674ca987d46SWarner Losh 		table = ptable_ebrread(table, dev, dread);
675ca987d46SWarner Losh 		/* FALLTHROUGH */
676ca987d46SWarner Losh 	}
677ca987d46SWarner Losh #endif /* LOADER_MBR_SUPPORT */
678ca987d46SWarner Losh #endif /* LOADER_MBR_SUPPORT || LOADER_GPT_SUPPORT */
679ca987d46SWarner Losh out:
68087d8d5eaSToomas Soome 	free(dp);
681ca987d46SWarner Losh 	free(buf);
682ca987d46SWarner Losh 	return (table);
683ca987d46SWarner Losh }
684ca987d46SWarner Losh 
685ca987d46SWarner Losh void
ptable_close(struct ptable * table)686ca987d46SWarner Losh ptable_close(struct ptable *table)
687ca987d46SWarner Losh {
688ca987d46SWarner Losh 	struct pentry *entry;
689ca987d46SWarner Losh 
69063f582e0SToomas Soome 	if (table == NULL)
69163f582e0SToomas Soome 		return;
69263f582e0SToomas Soome 
693ca987d46SWarner Losh 	while (!STAILQ_EMPTY(&table->entries)) {
694ca987d46SWarner Losh 		entry = STAILQ_FIRST(&table->entries);
695ca987d46SWarner Losh 		STAILQ_REMOVE_HEAD(&table->entries, entry);
696ca987d46SWarner Losh 		free(entry);
697ca987d46SWarner Losh 	}
698ca987d46SWarner Losh 	free(table);
699ca987d46SWarner Losh }
700ca987d46SWarner Losh 
701ca987d46SWarner Losh enum ptable_type
ptable_gettype(const struct ptable * table)702ca987d46SWarner Losh ptable_gettype(const struct ptable *table)
703ca987d46SWarner Losh {
704ca987d46SWarner Losh 
705ca987d46SWarner Losh 	return (table->type);
706ca987d46SWarner Losh }
707ca987d46SWarner Losh 
708ca987d46SWarner Losh int
ptable_getsize(const struct ptable * table,uint64_t * sizep)709ca987d46SWarner Losh ptable_getsize(const struct ptable *table, uint64_t *sizep)
710ca987d46SWarner Losh {
711ca987d46SWarner Losh 	uint64_t tmp = table->sectors * table->sectorsize;
712ca987d46SWarner Losh 
713ca987d46SWarner Losh 	if (tmp < table->sectors)
714ca987d46SWarner Losh 		return (EOVERFLOW);
715ca987d46SWarner Losh 
716ca987d46SWarner Losh 	if (sizep != NULL)
717ca987d46SWarner Losh 		*sizep = tmp;
718ca987d46SWarner Losh 	return (0);
719ca987d46SWarner Losh }
720ca987d46SWarner Losh 
721ca987d46SWarner Losh int
ptable_getpart(const struct ptable * table,struct ptable_entry * part,int index)722ca987d46SWarner Losh ptable_getpart(const struct ptable *table, struct ptable_entry *part, int index)
723ca987d46SWarner Losh {
724ca987d46SWarner Losh 	struct pentry *entry;
725ca987d46SWarner Losh 
726ca987d46SWarner Losh 	if (part == NULL || table == NULL)
727ca987d46SWarner Losh 		return (EINVAL);
728ca987d46SWarner Losh 
729ca987d46SWarner Losh 	STAILQ_FOREACH(entry, &table->entries, entry) {
730ca987d46SWarner Losh 		if (entry->part.index != index)
731ca987d46SWarner Losh 			continue;
732ca987d46SWarner Losh 		memcpy(part, &entry->part, sizeof(*part));
733ca987d46SWarner Losh 		return (0);
734ca987d46SWarner Losh 	}
735ca987d46SWarner Losh 	return (ENOENT);
736ca987d46SWarner Losh }
737ca987d46SWarner Losh 
738ca987d46SWarner Losh /*
739ca987d46SWarner Losh  * Search for a slice with the following preferences:
740ca987d46SWarner Losh  *
741ca987d46SWarner Losh  * 1: Active FreeBSD slice
742ca987d46SWarner Losh  * 2: Non-active FreeBSD slice
743ca987d46SWarner Losh  * 3: Active Linux slice
744ca987d46SWarner Losh  * 4: non-active Linux slice
745ca987d46SWarner Losh  * 5: Active FAT/FAT32 slice
746ca987d46SWarner Losh  * 6: non-active FAT/FAT32 slice
747ca987d46SWarner Losh  */
748ca987d46SWarner Losh #define	PREF_RAWDISK	0
749ca987d46SWarner Losh #define	PREF_FBSD_ACT	1
750ca987d46SWarner Losh #define	PREF_FBSD	2
751ca987d46SWarner Losh #define	PREF_LINUX_ACT	3
752ca987d46SWarner Losh #define	PREF_LINUX	4
753ca987d46SWarner Losh #define	PREF_DOS_ACT	5
754ca987d46SWarner Losh #define	PREF_DOS	6
755ca987d46SWarner Losh #define	PREF_NONE	7
756ca987d46SWarner Losh int
ptable_getbestpart(const struct ptable * table,struct ptable_entry * part)757ca987d46SWarner Losh ptable_getbestpart(const struct ptable *table, struct ptable_entry *part)
758ca987d46SWarner Losh {
759ca987d46SWarner Losh 	struct pentry *entry, *best;
760ca987d46SWarner Losh 	int pref, preflevel;
761ca987d46SWarner Losh 
762ca987d46SWarner Losh 	if (part == NULL || table == NULL)
763ca987d46SWarner Losh 		return (EINVAL);
764ca987d46SWarner Losh 
765ca987d46SWarner Losh 	best = NULL;
766ca987d46SWarner Losh 	preflevel = pref = PREF_NONE;
767ca987d46SWarner Losh 	STAILQ_FOREACH(entry, &table->entries, entry) {
768ca987d46SWarner Losh #ifdef LOADER_MBR_SUPPORT
769ca987d46SWarner Losh 		if (table->type == PTABLE_MBR) {
770ca987d46SWarner Losh 			switch (entry->type.mbr) {
771ca987d46SWarner Losh 			case DOSPTYP_386BSD:
772ca987d46SWarner Losh 				pref = entry->flags & 0x80 ? PREF_FBSD_ACT:
773ca987d46SWarner Losh 				    PREF_FBSD;
774ca987d46SWarner Losh 				break;
775ca987d46SWarner Losh 			case DOSPTYP_LINUX:
776ca987d46SWarner Losh 				pref = entry->flags & 0x80 ? PREF_LINUX_ACT:
777ca987d46SWarner Losh 				    PREF_LINUX;
778ca987d46SWarner Losh 				break;
779ca987d46SWarner Losh 			case 0x01:		/* DOS/Windows */
780ca987d46SWarner Losh 			case 0x04:
781ca987d46SWarner Losh 			case 0x06:
782ca987d46SWarner Losh 			case 0x0c:
783ca987d46SWarner Losh 			case 0x0e:
784ca987d46SWarner Losh 			case DOSPTYP_FAT32:
785ca987d46SWarner Losh 				pref = entry->flags & 0x80 ? PREF_DOS_ACT:
786ca987d46SWarner Losh 				    PREF_DOS;
787ca987d46SWarner Losh 				break;
788ca987d46SWarner Losh 			default:
789ca987d46SWarner Losh 				pref = PREF_NONE;
790ca987d46SWarner Losh 			}
791ca987d46SWarner Losh 		}
792ca987d46SWarner Losh #endif /* LOADER_MBR_SUPPORT */
793ca987d46SWarner Losh #ifdef LOADER_GPT_SUPPORT
794ca987d46SWarner Losh 		if (table->type == PTABLE_GPT) {
795ca987d46SWarner Losh 			if (entry->part.type == PART_DOS)
796ca987d46SWarner Losh 				pref = PREF_DOS;
797ca987d46SWarner Losh 			else if (entry->part.type == PART_FREEBSD_UFS ||
798ca987d46SWarner Losh 			    entry->part.type == PART_FREEBSD_ZFS)
799ca987d46SWarner Losh 				pref = PREF_FBSD;
800ca987d46SWarner Losh 			else
801ca987d46SWarner Losh 				pref = PREF_NONE;
802ca987d46SWarner Losh 		}
803ca987d46SWarner Losh #endif /* LOADER_GPT_SUPPORT */
804ca987d46SWarner Losh 		if (pref < preflevel) {
805ca987d46SWarner Losh 			preflevel = pref;
806ca987d46SWarner Losh 			best = entry;
807ca987d46SWarner Losh 		}
808ca987d46SWarner Losh 	}
809ca987d46SWarner Losh 	if (best != NULL) {
810ca987d46SWarner Losh 		memcpy(part, &best->part, sizeof(*part));
811ca987d46SWarner Losh 		return (0);
812ca987d46SWarner Losh 	}
813ca987d46SWarner Losh 	return (ENOENT);
814ca987d46SWarner Losh }
815ca987d46SWarner Losh 
816ca987d46SWarner Losh int
ptable_iterate(const struct ptable * table,void * arg,ptable_iterate_t * iter)817ca987d46SWarner Losh ptable_iterate(const struct ptable *table, void *arg, ptable_iterate_t *iter)
818ca987d46SWarner Losh {
819ca987d46SWarner Losh 	struct pentry *entry;
820ca987d46SWarner Losh 	char name[32];
821ca987d46SWarner Losh 	int ret = 0;
822ca987d46SWarner Losh 
823ca987d46SWarner Losh 	name[0] = '\0';
824ca987d46SWarner Losh 	STAILQ_FOREACH(entry, &table->entries, entry) {
825ca987d46SWarner Losh #ifdef LOADER_MBR_SUPPORT
826ca987d46SWarner Losh 		if (table->type == PTABLE_MBR)
827ca987d46SWarner Losh 			sprintf(name, "s%d", entry->part.index);
828ca987d46SWarner Losh 		else
829ca987d46SWarner Losh #endif
830ca987d46SWarner Losh #ifdef LOADER_GPT_SUPPORT
831ca987d46SWarner Losh 		if (table->type == PTABLE_GPT)
832ca987d46SWarner Losh 			sprintf(name, "p%d", entry->part.index);
833ca987d46SWarner Losh 		else
834ca987d46SWarner Losh #endif
835ca987d46SWarner Losh 		if (table->type == PTABLE_BSD)
836ca987d46SWarner Losh 			sprintf(name, "%c", (uint8_t) 'a' +
837ca987d46SWarner Losh 			    entry->part.index);
838ca987d46SWarner Losh 		if ((ret = iter(arg, name, &entry->part)) != 0)
839ca987d46SWarner Losh 			return (ret);
840ca987d46SWarner Losh 	}
841ca987d46SWarner Losh 	return (ret);
842ca987d46SWarner Losh }
843