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