xref: /illumos-gate/usr/src/boot/common/disk.h (revision ce17336ed725d3b7fdff67bf0a0ee2b55018fec6)
1 /*
2  * Copyright (c) 2011 Google, Inc.
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 AUTHOR 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 AUTHOR 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 /*
28  * Device descriptor for partitioned disks. To use, set the
29  * d_slice and d_partition variables as follows:
30  *
31  * Whole disk access:
32  *
33  *	d_slice = D_SLICENONE
34  *	d_partition = <doesn't matter>
35  *
36  * Whole MBR slice:
37  *
38  *	d_slice = MBR slice number (typically 1..4)
39  *	d_partition = D_PARTNONE
40  *
41  * VTOC disklabel partition within an MBR slice:
42  *
43  *	d_slice = MBR slice number (typically 1..4)
44  *	d_partition = disklabel partition (typically 0..19 or D_PARTWILD)
45  *
46  * BSD disklabel partition within an MBR slice:
47  *
48  *	d_slice = MBR slice number (typically 1..4)
49  *	d_partition = disklabel partition (typically 0..19 or D_PARTWILD)
50  *
51  * BSD disklabel partition on the true dedicated disk:
52  *
53  *	d_slice = D_SLICENONE
54  *	d_partition = disklabel partition (typically 0..19 or D_PARTWILD)
55  *
56  * GPT partition:
57  *
58  *	d_slice = GPT partition number (typically 1..N)
59  *	d_partition = D_PARTISGPT
60  *
61  * For MBR, setting d_partition to D_PARTWILD will automatically use the first
62  * partition within the slice.
63  *
64  * For both MBR and GPT, to automatically find the 'best' slice and partition,
65  * set d_slice to D_SLICEWILD. This uses the partition type to decide which
66  * partition to use according to the following list of preferences:
67  *
68  *	Solaris2 (active)
69  *	Solaris2 (inactive)
70  *	Linux (active)
71  *	Linux (inactive)
72  *	DOS/Windows (active)
73  *	DOS/Windows (inactive)
74  *
75  * Active MBR slices (marked as bootable) are preferred over inactive. GPT
76  * doesn't have the concept of active/inactive partitions. In both MBR and GPT,
77  * if there are multiple slices/partitions of a given type, the first one
78  * is chosen.
79  *
80  * The low-level disk device will typically call disk_open() from its open
81  * method to interpret the disk partition tables according to the rules above.
82  * This will initialize d_offset to the block offset of the start of the
83  * selected partition - this offset should be added to the offset passed to
84  * the device's strategy method.
85  */
86 
87 #ifndef	_DISK_H
88 #define	_DISK_H
89 
90 #define	D_SLICENONE	-1
91 #define	D_SLICEWILD	0
92 #define	D_PARTNONE	-1
93 #define	D_PARTWILD	-2
94 #define	D_PARTISGPT	255
95 
96 struct disk_devdesc {
97 	struct devdesc	dd;		/* Must be first. */
98 	int		d_slice;
99 	int		d_partition;
100 	uint64_t	d_offset;
101 };
102 
103 enum disk_ioctl {
104 	IOCTL_GET_BLOCKS,
105 	IOCTL_GET_BLOCK_SIZE
106 };
107 
108 /*
109  * Parse disk metadata and initialise dev->d_offset.
110  */
111 extern int disk_open(struct disk_devdesc *, uint64_t, u_int);
112 extern int disk_close(struct disk_devdesc *);
113 extern int disk_ioctl(struct disk_devdesc *, u_long, void *);
114 extern int disk_read(struct disk_devdesc *, void *, uint64_t, u_int);
115 extern int disk_write(struct disk_devdesc *, void *, uint64_t, u_int);
116 
117 /*
118  * Print information about slices on a disk.
119  */
120 extern int disk_print(struct disk_devdesc *, char *, int);
121 extern char* disk_fmtdev(struct disk_devdesc *);
122 extern int disk_parsedev(struct disk_devdesc *, const char *, const char **);
123 
124 #endif	/* _DISK_H */
125