xref: /freebsd/sys/geom/part/g_part.h (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006-2008 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _GEOM_PART_H_
30 #define	_GEOM_PART_H_
31 
32 #define	G_PART_TRACE(args)	g_trace args
33 
34 #define G_PART_PROBE_PRI_LOW	-10
35 #define	G_PART_PROBE_PRI_NORM	-5
36 #define	G_PART_PROBE_PRI_HIGH	0
37 
38 enum g_part_alias {
39 	G_PART_ALIAS_APPLE_APFS,	/* An Apple APFS partition. */
40 	G_PART_ALIAS_APPLE_BOOT,	/* An Apple boot partition entry. */
41 	G_PART_ALIAS_APPLE_CORE_STORAGE,/* An Apple Core Storage partition. */
42 	G_PART_ALIAS_APPLE_HFS,		/* An HFS+ file system entry. */
43 	G_PART_ALIAS_APPLE_LABEL,	/* An Apple label partition entry. */
44 	G_PART_ALIAS_APPLE_RAID,	/* An Apple RAID partition entry. */
45 	G_PART_ALIAS_APPLE_RAID_OFFLINE,/* An Apple RAID (offline) part entry.*/
46 	G_PART_ALIAS_APPLE_TV_RECOVERY,	/* An Apple TV recovery part entry. */
47 	G_PART_ALIAS_APPLE_UFS,		/* An Apple UFS partition entry. */
48 	G_PART_ALIAS_APPLE_ZFS,		/* An Apple ZFS partition entry.
49 					   Also used for Solaris /usr partition. */
50 	G_PART_ALIAS_BIOS_BOOT,		/* A GRUB 2 boot partition entry. */
51 	G_PART_ALIAS_CHROMEOS_FIRMWARE,	/* A ChromeOS firmware part. entry. */
52 	G_PART_ALIAS_CHROMEOS_KERNEL,	/* A ChromeOS Kernel part. entry. */
53 	G_PART_ALIAS_CHROMEOS_RESERVED,	/* ChromeOS. Reserved for future use. */
54 	G_PART_ALIAS_CHROMEOS_ROOT,	/* A ChromeOS root part. entry. */
55 	G_PART_ALIAS_DFBSD,		/* A DfBSD label32 partition entry */
56 	G_PART_ALIAS_DFBSD64,		/* A DfBSD label64 partition entry */
57 	G_PART_ALIAS_DFBSD_CCD,		/* A DfBSD CCD partition entry */
58 	G_PART_ALIAS_DFBSD_HAMMER,	/* A DfBSD HAMMER FS partition entry */
59 	G_PART_ALIAS_DFBSD_HAMMER2,	/* A DfBSD HAMMER2 FS partition entry */
60 	G_PART_ALIAS_DFBSD_LEGACY,	/* A DfBSD legacy partition entry */
61 	G_PART_ALIAS_DFBSD_SWAP,	/* A DfBSD swap partition entry */
62 	G_PART_ALIAS_DFBSD_UFS,		/* A DfBSD UFS partition entry */
63 	G_PART_ALIAS_DFBSD_VINUM,	/* A DfBSD Vinum partition entry */
64 	G_PART_ALIAS_EBR,		/* A EBR partition entry. */
65 	G_PART_ALIAS_EFI,		/* A EFI system partition entry. */
66 	G_PART_ALIAS_FREEBSD,		/* A BSD labeled partition entry. */
67 	G_PART_ALIAS_FREEBSD_BOOT,	/* A FreeBSD boot partition entry. */
68 	G_PART_ALIAS_FREEBSD_NANDFS,	/* A FreeBSD nandfs partition entry. */
69 	G_PART_ALIAS_FREEBSD_SWAP,	/* A swap partition entry. */
70 	G_PART_ALIAS_FREEBSD_UFS,	/* A UFS/UFS2 file system entry. */
71 	G_PART_ALIAS_FREEBSD_VINUM,	/* A Vinum partition entry. */
72 	G_PART_ALIAS_FREEBSD_ZFS,	/* A ZFS file system entry. */
73 	G_PART_ALIAS_HIFIVE_FSBL,	/* HiFive First Stage Bootloader */
74 	G_PART_ALIAS_HIFIVE_BBL,	/* HiFive Second Stage Bootloader */
75 	G_PART_ALIAS_LINUX_DATA,	/* A Linux data partition entry. */
76 	G_PART_ALIAS_LINUX_LVM,		/* A Linux LVM partition entry. */
77 	G_PART_ALIAS_LINUX_RAID,	/* A Linux RAID partition entry. */
78 	G_PART_ALIAS_LINUX_SWAP,	/* A Linux swap partition entry. */
79 	G_PART_ALIAS_MBR,		/* A MBR (extended) partition entry. */
80 	G_PART_ALIAS_MS_BASIC_DATA,	/* A Microsoft Data part. entry. */
81 	G_PART_ALIAS_MS_FAT16,		/* A Microsoft FAT16 partition entry. */
82 	G_PART_ALIAS_MS_FAT32,		/* A Microsoft FAT32 partition entry. */
83 	G_PART_ALIAS_MS_FAT32LBA,	/* A Microsoft FAT32 LBA partition entry */
84 	G_PART_ALIAS_MS_LDM_DATA,	/* A Microsoft LDM Data part. entry. */
85 	G_PART_ALIAS_MS_LDM_METADATA,	/* A Microsoft LDM Metadata entry. */
86 	G_PART_ALIAS_MS_NTFS,		/* A Microsoft NTFS partition entry */
87 	G_PART_ALIAS_MS_RECOVERY,	/* A Microsoft recovery part. entry. */
88 	G_PART_ALIAS_MS_RESERVED,	/* A Microsoft Reserved part. entry. */
89 	G_PART_ALIAS_MS_SPACES,		/* A Microsoft Spaces part. entry. */
90 	G_PART_ALIAS_NETBSD_CCD,	/* A NetBSD CCD partition entry. */
91 	G_PART_ALIAS_NETBSD_CGD,	/* A NetBSD CGD partition entry. */
92 	G_PART_ALIAS_NETBSD_FFS,	/* A NetBSD FFS partition entry. */
93 	G_PART_ALIAS_NETBSD_LFS,	/* A NetBSD LFS partition entry. */
94 	G_PART_ALIAS_NETBSD_RAID,	/* A NetBSD RAID partition entry. */
95 	G_PART_ALIAS_NETBSD_SWAP,	/* A NetBSD swap partition entry. */
96 	G_PART_ALIAS_OPENBSD_DATA,	/* An OpenBSD data partition entry. */
97 	G_PART_ALIAS_PREP_BOOT,		/* A PREP/CHRP boot partition entry. */
98 	G_PART_ALIAS_SOLARIS_BOOT,	/* A Solaris boot partition entry. */
99 	G_PART_ALIAS_SOLARIS_ROOT,	/* A Solaris root partition entry. */
100 	G_PART_ALIAS_SOLARIS_SWAP,	/* A Solaris swap partition entry. */
101 	G_PART_ALIAS_SOLARIS_BACKUP,	/* A Solaris backup partition entry. */
102 	G_PART_ALIAS_SOLARIS_VAR,	/* A Solaris /var partition entry. */
103 	G_PART_ALIAS_SOLARIS_HOME,	/* A Solaris /home partition entry. */
104 	G_PART_ALIAS_SOLARIS_ALTSEC,	/* A Solaris alternate sector partition entry. */
105 	G_PART_ALIAS_SOLARIS_RESERVED,	/* A Solaris reserved partition entry. */
106 	G_PART_ALIAS_VMFS,		/* A VMware VMFS partition entry */
107 	G_PART_ALIAS_VMKDIAG,		/* A VMware vmkDiagnostic partition entry */
108 	G_PART_ALIAS_VMRESERVED,	/* A VMware reserved partition entry */
109 	G_PART_ALIAS_VMVSANHDR,		/* A VMware vSAN header partition entry */
110 	/* Keep the following last */
111 	G_PART_ALIAS_COUNT
112 };
113 
114 const char *g_part_alias_name(enum g_part_alias);
115 
116 /* G_PART scheme (KOBJ class). */
117 struct g_part_scheme {
118 	KOBJ_CLASS_FIELDS;
119 	size_t		gps_entrysz;
120 	int		gps_minent;
121 	int		gps_maxent;
122 	int		gps_bootcodesz;
123 	TAILQ_ENTRY(g_part_scheme) scheme_list;
124 };
125 
126 struct g_part_entry {
127 	LIST_ENTRY(g_part_entry) gpe_entry;
128 	struct g_provider *gpe_pp;	/* Corresponding provider. */
129 	off_t		gpe_offset;	/* Byte offset. */
130 	quad_t		gpe_start;	/* First LBA of partition. */
131 	quad_t		gpe_end;	/* Last LBA of partition. */
132 	int		gpe_index;
133 	bool		gpe_created:1;	/* Entry is newly created. */
134 	bool		gpe_deleted:1;	/* Entry has been deleted. */
135 	bool		gpe_modified:1;	/* Entry has been modified. */
136 	bool		gpe_internal:1;	/* Entry is not a used entry. */
137 };
138 
139 /* G_PART table (KOBJ instance). */
140 struct g_part_table {
141 	KOBJ_FIELDS;
142 	struct g_part_scheme *gpt_scheme;
143 	struct g_geom	*gpt_gp;
144 	LIST_HEAD(, g_part_entry) gpt_entry;
145 	quad_t		gpt_first;	/* First allocatable LBA */
146 	quad_t		gpt_last;	/* Last allocatable LBA */
147 	int		gpt_entries;
148 	/*
149 	 * gpt_smhead and gpt_smtail are bitmaps representing the first
150 	 * 32 sectors on the disk (gpt_smhead) and the last 32 sectors
151 	 * on the disk (gpt_smtail). These maps are used by the commit
152 	 * verb to clear sectors previously used by a scheme after the
153 	 * partitioning scheme has been destroyed.
154 	 */
155 	uint32_t	gpt_smhead;
156 	uint32_t	gpt_smtail;
157 	/*
158 	 * gpt_sectors and gpt_heads are the fixed or synchesized number
159 	 * of sectors per track and heads (resp) that make up a disks
160 	 * geometry. This is to support partitioning schemes as well as
161 	 * file systems that work on a geometry. The MBR scheme and the
162 	 * MS-DOS (FAT) file system come to mind.
163 	 * We keep track of whether the geometry is fixed or synchesized
164 	 * so that a partitioning scheme can correct the synthesized
165 	 * geometry, based on the on-disk metadata.
166 	 */
167 	uint32_t	gpt_sectors;
168 	uint32_t	gpt_heads;
169 
170 	int		gpt_depth;	/* Sub-partitioning level. */
171 	bool		gpt_isleaf:1;	/* Cannot be sub-partitioned. */
172 	bool		gpt_created:1;	/* Newly created. */
173 	bool		gpt_modified:1;	/* Table changes have been made. */
174 	bool		gpt_opened:1;	/* Permissions obtained. */
175 	bool		gpt_fixgeom:1;	/* Geometry is fixed. */
176 	bool		gpt_corrupt:1;	/* Table is corrupt. */
177 };
178 
179 struct g_part_entry *g_part_new_entry(struct g_part_table *, int, quad_t,
180     quad_t);
181 
182 enum g_part_ctl {
183 	G_PART_CTL_NONE,
184 	G_PART_CTL_ADD,
185 	G_PART_CTL_BOOTCODE,
186 	G_PART_CTL_COMMIT,
187 	G_PART_CTL_CREATE,
188 	G_PART_CTL_DELETE,
189 	G_PART_CTL_DESTROY,
190 	G_PART_CTL_MODIFY,
191 	G_PART_CTL_MOVE,
192 	G_PART_CTL_RECOVER,
193 	G_PART_CTL_RESIZE,
194 	G_PART_CTL_SET,
195 	G_PART_CTL_UNDO,
196 	G_PART_CTL_UNSET
197 };
198 
199 /* G_PART ctlreq parameters. */
200 #define	G_PART_PARM_ENTRIES	0x0001
201 #define	G_PART_PARM_FLAGS	0x0002
202 #define	G_PART_PARM_GEOM	0x0004
203 #define	G_PART_PARM_INDEX	0x0008
204 #define	G_PART_PARM_LABEL	0x0010
205 #define	G_PART_PARM_OUTPUT	0x0020
206 #define	G_PART_PARM_PROVIDER	0x0040
207 #define	G_PART_PARM_SCHEME	0x0080
208 #define	G_PART_PARM_SIZE	0x0100
209 #define	G_PART_PARM_START	0x0200
210 #define	G_PART_PARM_TYPE	0x0400
211 #define	G_PART_PARM_VERSION	0x0800
212 #define	G_PART_PARM_BOOTCODE	0x1000
213 #define	G_PART_PARM_ATTRIB	0x2000
214 #define	G_PART_PARM_FORCE	0x4000
215 #define G_PART_PARM_SKIP_DSN	0x8000
216 
217 struct g_part_parms {
218 	unsigned int	gpp_parms;
219 	unsigned int	gpp_entries;
220 	const char	*gpp_flags;
221 	struct g_geom	*gpp_geom;
222 	unsigned int	gpp_index;
223 	const char	*gpp_label;
224 	struct g_provider *gpp_provider;
225 	struct g_part_scheme *gpp_scheme;
226 	quad_t		gpp_size;
227 	quad_t		gpp_start;
228 	const char	*gpp_type;
229 	unsigned int	gpp_version;
230 	const void	*gpp_codeptr;
231 	unsigned int	gpp_codesize;
232 	const char	*gpp_attrib;
233 	unsigned int	gpp_force;
234 	unsigned int	gpp_skip_dsn;
235 };
236 
237 void g_part_geometry_heads(off_t, u_int, off_t *, u_int *);
238 
239 int g_part_modevent(module_t, int, struct g_part_scheme *);
240 
241 extern char g_part_separator[];
242 
243 #define	G_PART_SCHEME_DECLARE(name)				\
244     static int name##_modevent(module_t mod, int tp, void *d)	\
245     {								\
246 	return (g_part_modevent(mod, tp, d));			\
247     }								\
248     static moduledata_t name##_mod = {				\
249 	#name,							\
250 	name##_modevent,					\
251 	&name##_scheme						\
252     };								\
253     DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \
254     MODULE_DEPEND(name, g_part, 0, 0, 0)
255 
256 #endif /* !_GEOM_PART_H_ */
257