libdisk.h revision 8477
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $Id: libdisk.h,v 1.15 1995/05/10 05:57:03 phk Exp $
10 *
11 */
12
13#define MAX_NO_DISKS	20
14	/* Max # of disks Disk_Names() will return */
15
16typedef enum {
17	whole,
18	unknown,
19	fat,
20	freebsd,
21	extended,
22	part,
23	unused,
24	} chunk_e;
25
26extern char *chunk_n[];
27
28struct disk {
29	char		*name;
30	u_long		flags;
31#		define DISK_ON_TRACK	1
32	u_long		real_cyl;
33	u_long		real_hd;
34	u_long		real_sect;
35	u_long		bios_cyl;
36	u_long		bios_hd;
37	u_long		bios_sect;
38	u_char		*bootmgr;
39	u_char		*boot1;
40	u_char		*boot2;
41	struct chunk	*chunks;
42};
43
44struct chunk {
45	struct chunk	*next;
46	struct chunk	*part;
47	long		offset;
48	u_long		size;
49	u_long		end;
50	char		*name;
51	char		*oname;
52		/* Used during Fixup_Names() to avoid renaming more than
53		 * absolutely needed.
54		 */
55	chunk_e		type;
56	int		subtype;
57	u_long		flags;
58#		define CHUNK_PAST_1024		1
59			/* this chunk cannot be booted from because it
60			 * extends past cylinder 1024
61			 */
62#		define CHUNK_BSD_COMPAT	2
63			/* this chunk is in the BSD-compatibility, and has a
64			 * short name too, ie wd0s4f -> wd0f
65         		*/
66#		define CHUNK_BAD144		4
67			/* this chunk has bad144 mapping */
68#		define CHUNK_ALIGN		8
69			/* This chunk should be aligned */
70#		define CHUNK_IS_ROOT		16
71			/* This 'part' is a rootfs, allocate 'a' */
72#		define CHUNK_ACTIVE		32
73			/* This is the active slice in the MBR */
74
75	void		(*private_free)(void*);
76	void		*(*private_clone)(void*);
77	void		*private;
78		/* For data private to the application, and the management
79		 * thereof.  If the functions are not provided, no storage
80		 * management is done, Cloning will just copy the pointer
81		 * and freeing will just forget it.
82		 */
83};
84
85struct disk *
86Open_Disk(char *devname);
87	/* Will open the named disk, and return populated tree.
88	 */
89
90struct disk *
91Clone_Disk(struct disk *disk);
92	/* Clone a copy of a tree.  Useful for "Undo" functionality
93	 */
94
95void
96Free_Disk(struct disk *disk);
97	/* Free a tree made with Open_Disk() or Clone_Disk()
98	 */
99
100void
101Debug_Disk(struct disk *disk);
102	/* Print the content of the tree to stdout
103	 */
104
105struct disk *
106Set_Phys_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
107	/* Use a different physical geometry.  Makes sense for ST506 disks only.
108	 * The tree returned is read from the disk, using this geometry.
109	 */
110
111void
112Set_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
113	/* Set the geometry the bios uses.
114	 */
115
116int
117Delete_Chunk(struct disk *disk, struct chunk *);
118	/* Free a chunk of disk_space
119	 */
120
121void
122Collapse_Disk(struct disk *disk);
123	/* Experimental, do not use.
124	 */
125int
126Collapse_Chunk(struct disk *disk, struct chunk *chunk);
127	/* Experimental, do not use.
128	 */
129
130int
131Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int subtype, u_long flags);
132	/* Create a chunk with the specified paramters
133	 */
134
135void
136All_FreeBSD(struct disk *d);
137	/* Make one FreeBSD chunk covering the entire disk
138	 */
139
140char *
141CheckRules(struct disk *);
142	/* Return char* to warnings about broken design rules in this disklayout
143	 */
144
145char **
146Disk_Names();
147	/* Return char** with all disk's names (wd0, wd1 ...).  You must free
148	 * each pointer, as well as the array by hand
149	 */
150
151void
152Set_Boot_Mgr(struct disk *d, u_char *bootmgr);
153	/* Use this boot-manager on this disk.  Gets written when Write_Disk()
154	 * is called
155	 */
156
157void
158Set_Boot_Blocks(struct disk *d, u_char *boot1, u_char *boot2);
159	/* Use these boot-blocks on this disk.  Gets written when Write_Disk()
160	 * is called
161	 */
162
163int
164Write_Disk(struct disk *d);
165	/* Write all the MBRs, disklabels, bootblocks and boot managers
166	 */
167
168int
169Cyl_Aligned(struct disk *d, u_long offset);
170	/* Check if offset is aligned on a cylinder according to the
171	 * bios geometry
172	 */
173
174u_long
175Next_Cyl_Aligned(struct disk *d, u_long offset);
176	/* Round offset up to next cylinder according to the bios-geometry
177	 */
178
179u_long
180Prev_Cyl_Aligned(struct disk *d, u_long offset);
181	/* Round offset down to previous cylinder according to the bios-
182	 * geometry
183	 */
184
185int
186Track_Aligned(struct disk *d, u_long offset);
187	/* Check if offset is aligned on a track according to the
188	 * bios geometry
189	 */
190
191u_long
192Next_Track_Aligned(struct disk *d, u_long offset);
193	/* Round offset up to next track according to the bios-geometry
194	 */
195
196u_long
197Prev_Track_Aligned(struct disk *d, u_long offset);
198	/* Check if offset is aligned on a track according to the
199	 * bios geometry
200	 */
201
202struct chunk *
203Create_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size, chunk_e type, int subtype, u_long flags);
204	/* This one creates a partition inside the given parent of the given
205	 * size, and returns a pointer to it.  The first unused chunk big
206	 * enough is used.
207	 */
208
209void
210MakeDevDisk(struct disk *d,char *path);
211	/* Make device nodes for all chunks on this disk */
212
213/*
214 * Implementation details  >>> DO NOT USE <<<
215 */
216
217void Debug_Chunk(struct chunk *);
218void Free_Chunk(struct chunk *);
219struct chunk * Clone_Chunk(struct chunk *);
220int Add_Chunk(struct disk *, long , u_long , char *, chunk_e, int , u_long);
221void Bios_Limit_Chunk(struct chunk *, u_long);
222void * read_block(int, daddr_t );
223void write_block(int fd, daddr_t block, void *foo);
224struct disklabel * read_disklabel(int, daddr_t);
225u_short	dkcksum(struct disklabel *);
226struct chunk * Find_Mother_Chunk(struct chunk *, u_long , u_long , chunk_e);
227struct disk * Int_Open_Disk(char *name, u_long size);
228void Fixup_Names(struct disk *);
229
230#define dprintf	printf
231
232/* TODO
233 *
234 * Need a error string mechanism from the functions instead of warn()
235 *
236 * Make sure only FreeBSD start at offset==0
237 *
238 * Collapse must align.
239 *
240 * Make Write_Disk(struct disk*)
241 *
242 * Consider booting from OnTrack'ed disks.
243 *
244 * Get Bios-geom, ST506 & OnTrack from driver (or otherwise)
245 *
246 * Make Create_DWIM().
247 *
248 * Make Is_Unchanged(struct disk *d1, struct chunk *c1)
249 *
250 * don't rename slices unless we have to
251 *
252 *Sample output from tst01:
253 *
254 * Debug_Disk(wd0)  flags=0  real_geom=0/0/0  bios_geom=0/0/0
255 * >>        0x3d040          0    1411200    1411199 wd0      0 whole    0 0
256 * >>>>      0x3d080          0     960120     960119 wd0s1    3 freebsd  0 8
257 * >>>>>>    0x3d100          0      40960      40959 wd0s1a   5 part     0 0
258 * >>>>>>    0x3d180      40960     131072     172031 wd0s1b   5 part     0 0
259 * >>>>>>    0x3d1c0     172032     409600     581631 wd0s1e   5 part     0 0
260 * >>>>>>    0x3d200     581632     378488     960119 wd0s1f   5 part     0 0
261 * >>>>      0x3d140     960120       5670     965789 wd0s2    4 extended 0 8
262 * >>>>>>    0x3d2c0     960120         63     960182 -        6 unused   0 0
263 * >>>>>>    0x3d0c0     960183       5607     965789 wd0s5    2 fat      0 8
264 * >>>>      0x3d280     965790       1890     967679 wd0s3    1 foo      -2 8
265 * >>>>      0x3d300     967680     443520    1411199 wd0s4    3 freebsd  0 8
266 * >>>>>>    0x3d340     967680     443520    1411199 wd0s4a   5 part     0 0
267 *
268 * ^            ^           ^          ^          ^     ^      ^ ^        ^ ^
269 * level    chunkptr      start      size        end  name    type  subtype flags
270 *
271 * Underlying data structure:
272 *
273 *	Legend:
274 *		<struct chunk> --> part
275 *			|
276 *			v next
277 *
278 *	<wd0> --> <wd0s1> --> <wd0s1a>
279 *		     |           |
280 *		     |           v
281 *		     |        <wd0s1b>
282 *		     |           |
283 *		     |           v
284 *		     |        <wd0s1e>
285 *		     |           |
286 *		     |           v
287 *		     |        <wd0s1f>
288 *		     |
289 *		     v
290 *		  <wd0s2> --> <unused>
291 *		     |           |
292 *		     |           v
293 *		     |        <wd0s5>
294 *		     |
295 *		     v
296 *		  <wd0s3>
297 *		     |
298 *		     v
299 *		  <wd0s4> --> <wd0s4a>
300 *
301 *
302 */
303