xref: /freebsd/stand/common/disk.h (revision b3e76948)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2011 Google, Inc.
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 AUTHOR 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 AUTHOR 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 /*
28ca987d46SWarner Losh  * Device descriptor for partitioned disks. To use, set the
29ca987d46SWarner Losh  * d_slice and d_partition variables as follows:
30ca987d46SWarner Losh  *
31ca987d46SWarner Losh  * Whole disk access:
32ca987d46SWarner Losh  *
3314243f8dSIan Lepore  * 	d_slice = D_SLICENONE
3414243f8dSIan Lepore  * 	d_partition = <doesn't matter>
35ca987d46SWarner Losh  *
36ca987d46SWarner Losh  * Whole MBR slice:
37ca987d46SWarner Losh  *
38ca987d46SWarner Losh  * 	d_slice = MBR slice number (typically 1..4)
3914243f8dSIan Lepore  * 	d_partition = D_PARTNONE
40ca987d46SWarner Losh  *
41ca987d46SWarner Losh  * BSD disklabel partition within an MBR slice:
42ca987d46SWarner Losh  *
43ca987d46SWarner Losh  * 	d_slice = MBR slice number (typically 1..4)
4414243f8dSIan Lepore  * 	d_partition = disklabel partition (typically 0..19 or D_PARTWILD)
45ca987d46SWarner Losh  *
46ca987d46SWarner Losh  * BSD disklabel partition on the true dedicated disk:
47ca987d46SWarner Losh  *
4814243f8dSIan Lepore  * 	d_slice = D_SLICENONE
4914243f8dSIan Lepore  * 	d_partition = disklabel partition (typically 0..19 or D_PARTWILD)
50ca987d46SWarner Losh  *
51ca987d46SWarner Losh  * GPT partition:
52ca987d46SWarner Losh  *
53ca987d46SWarner Losh  * 	d_slice = GPT partition number (typically 1..N)
5414243f8dSIan Lepore  * 	d_partition = D_PARTISGPT
55ca987d46SWarner Losh  *
5614243f8dSIan Lepore  * For MBR, setting d_partition to D_PARTWILD will automatically use the first
5714243f8dSIan Lepore  * partition within the slice.
5814243f8dSIan Lepore  *
5914243f8dSIan Lepore  * For both MBR and GPT, to automatically find the 'best' slice and partition,
6014243f8dSIan Lepore  * set d_slice to D_SLICEWILD. This uses the partition type to decide which
6114243f8dSIan Lepore  * partition to use according to the following list of preferences:
62ca987d46SWarner Losh  *
63ca987d46SWarner Losh  * 	FreeBSD (active)
64ca987d46SWarner Losh  * 	FreeBSD (inactive)
65ca987d46SWarner Losh  * 	Linux (active)
66ca987d46SWarner Losh  * 	Linux (inactive)
67ca987d46SWarner Losh  * 	DOS/Windows (active)
68ca987d46SWarner Losh  * 	DOS/Windows (inactive)
69ca987d46SWarner Losh  *
70ca987d46SWarner Losh  * Active MBR slices (marked as bootable) are preferred over inactive. GPT
71ca987d46SWarner Losh  * doesn't have the concept of active/inactive partitions. In both MBR and GPT,
72ca987d46SWarner Losh  * if there are multiple slices/partitions of a given type, the first one
73ca987d46SWarner Losh  * is chosen.
74ca987d46SWarner Losh  *
75ca987d46SWarner Losh  * The low-level disk device will typically call disk_open() from its open
76ca987d46SWarner Losh  * method to interpret the disk partition tables according to the rules above.
77ca987d46SWarner Losh  * This will initialize d_offset to the block offset of the start of the
78ca987d46SWarner Losh  * selected partition - this offset should be added to the offset passed to
79ca987d46SWarner Losh  * the device's strategy method.
80ca987d46SWarner Losh  */
81ca987d46SWarner Losh 
82ca987d46SWarner Losh #ifndef	_DISK_H
83ca987d46SWarner Losh #define	_DISK_H
84ca987d46SWarner Losh 
8514243f8dSIan Lepore #define	D_SLICENONE	-1
8614243f8dSIan Lepore #define	D_SLICEWILD	 0
8714243f8dSIan Lepore #define	D_PARTNONE	-1
8814243f8dSIan Lepore #define	D_PARTWILD	-2
8914243f8dSIan Lepore #define	D_PARTISGPT	255
9014243f8dSIan Lepore 
91b3a2aad1SWarner Losh struct disk_devdesc {
929f6fd839SIan Lepore 	struct devdesc	dd;		/* Must be first. */
93ca987d46SWarner Losh 	int		d_slice;
94ca987d46SWarner Losh 	int		d_partition;
95ca987d46SWarner Losh 	uint64_t	d_offset;
96ca987d46SWarner Losh };
97ca987d46SWarner Losh 
98ca987d46SWarner Losh /*
99ca987d46SWarner Losh  * Parse disk metadata and initialise dev->d_offset.
100ca987d46SWarner Losh  */
101ca987d46SWarner Losh extern int disk_open(struct disk_devdesc *, uint64_t, u_int);
102ca987d46SWarner Losh extern int disk_close(struct disk_devdesc *);
103ca987d46SWarner Losh extern int disk_ioctl(struct disk_devdesc *, u_long, void *);
104ca987d46SWarner Losh extern int disk_read(struct disk_devdesc *, void *, uint64_t, u_int);
105ca987d46SWarner Losh extern int disk_write(struct disk_devdesc *, void *, uint64_t, u_int);
106ca987d46SWarner Losh extern int ptblread(void *, void *, size_t, uint64_t);
107ca987d46SWarner Losh 
108ca987d46SWarner Losh /*
109ca987d46SWarner Losh  * Print information about slices on a disk.
110ca987d46SWarner Losh  */
111ca987d46SWarner Losh extern int disk_print(struct disk_devdesc *, char *, int);
11217276525SWarner Losh extern int disk_parsedev(struct devdesc **, const char *, const char **);
113ca987d46SWarner Losh 
114c32dde31SWarner Losh char *disk_fmtdev(struct devdesc *vdev);
115c32dde31SWarner Losh 
116ca987d46SWarner Losh #endif	/* _DISK_H */
117