18156Sphk/*
214792Sjoerg* ----------------------------------------------------------------------------
314792Sjoerg* "THE BEER-WARE LICENSE" (Revision 42):
493149Sphk* <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
514792Sjoerg* can do whatever you want with this stuff. If we meet some day, and you think
614792Sjoerg* this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
714792Sjoerg* ----------------------------------------------------------------------------
814792Sjoerg*
950476Speter* $FreeBSD$
1014792Sjoerg*
1114792Sjoerg*/
128153Sphk
13173791Smtm#ifndef _LIBDISK_H_
14173791Smtm#define _LIBDISK_H_
15173791Smtm
16120522Srwatson/* #define DEBUG 1 */
17106135Sphk/* You can define a particular architecture here if you are debugging. */
18106135Sphk/* #define P_DEBUG p_sparc64 */
19106135Sphk
2076551Sjkh#define MAX_NO_DISKS	32
2114792Sjoerg/* Max # of disks Disk_Names() will return */
228158Sphk
2376551Sjkh#define MAX_SEC_SIZE    2048  /* maximum sector size that is supported */
2476551Sjkh#define MIN_SEC_SIZE	512   /* the sector size to end sensing at */
2576551Sjkh
26114300Sobrienenum platform {
27106135Sphk	p_any,			/* for debugging ! */
28106135Sphk	p_alpha,
29106135Sphk	p_i386,
30106135Sphk	p_pc98,
31106135Sphk	p_sparc64,
32106135Sphk	p_ia64,
33114329Speter	p_ppc,
34154814Scognet	p_amd64,
35178765Sgonzo	p_arm,
36178765Sgonzo	p_mips
37114300Sobrien};
38114300Sobrienextern const enum platform platform;
39106135Sphk
408303Sphktypedef enum {
418303Sphk	whole,
428303Sphk	unknown,
43106135Sphk
44106135Sphk	sun,
45106135Sphk	pc98,
46106135Sphk	mbr,
47106135Sphk	gpt,
48106135Sphk
49106742Smarcel	efi,
508303Sphk	fat,
518303Sphk	freebsd,
528303Sphk	extended,
538303Sphk	part,
54106135Sphk	spare,
55128541Sgrehan	unused,
56128541Sgrehan
57128541Sgrehan	apple
5814792Sjoerg} chunk_e;
598156Sphk
6014792Sjoerg__BEGIN_DECLS
61121888Smarcel#ifndef __ia64__
628153Sphkstruct disk {
638153Sphk	char		*name;
648153Sphk	u_long		bios_cyl;
658153Sphk	u_long		bios_hd;
668153Sphk	u_long		bios_sect;
6764555Snyan#ifdef PC98
6864555Snyan	u_char		*bootipl;
6964555Snyan	size_t		bootipl_size;
7064555Snyan	u_char		*bootmenu;
7164555Snyan	size_t		bootmenu_size;
7264555Snyan#else
738158Sphk	u_char		*bootmgr;
7463030Sjhb	size_t		bootmgr_size;
7564555Snyan#endif
768158Sphk	u_char		*boot1;
77114329Speter#if defined(__i386__) || defined(__amd64__) /* the i386 needs extra help... */
788158Sphk	u_char		*boot2;
7940000Sdfr#endif
808153Sphk	struct chunk	*chunks;
8176551Sjkh	u_long		sector_size; /* media sector size, a power of 2 */
828153Sphk};
83121888Smarcel#else	/* !__ia64__ */
84121888Smarcelstruct disk {
85121888Smarcel	char		*name;
86121888Smarcel	struct chunk	*chunks;
87121888Smarcel	u_long		media_size;
88121888Smarcel	u_long		sector_size;
89121888Smarcel	u_long		lba_start;
90121888Smarcel	u_long		lba_end;
91121888Smarcel	u_int		gpt_size;	/* Number of entries */
92121888Smarcel};
93121888Smarcel#endif
948153Sphk
958153Sphkstruct chunk {
968153Sphk	struct chunk	*next;
978153Sphk	struct chunk	*part;
988745Sphk	struct disk	*disk;
99127081Sjhb	daddr_t		offset;
100127081Sjhb	daddr_t		size;
101127081Sjhb	daddr_t		end;
102105685Sphk	char		*sname;		/* PC98 field */
1038153Sphk	char		*name;
1048250Sphk	char		*oname;
10514792Sjoerg	/* Used during Fixup_Names() to avoid renaming more than
10614792Sjoerg	 * absolutely needed.
10714792Sjoerg	 */
1088153Sphk	chunk_e		type;
1098153Sphk	int		subtype;
1108153Sphk	u_long		flags;
1118250Sphk	void		(*private_free)(void*);
1128250Sphk	void		*(*private_clone)(void*);
11314792Sjoerg	void		*private_data;
11414792Sjoerg	/* For data private to the application, and the management
11514792Sjoerg	 * thereof.  If the functions are not provided, no storage
11614792Sjoerg	 * management is done, Cloning will just copy the pointer
11714792Sjoerg	 * and freeing will just forget it.
11814792Sjoerg	 */
1198153Sphk};
1208153Sphk
12187581Sdillon/*
12287581Sdillon * flags:
12387581Sdillon *
12487581Sdillon * ALIGN	-	This chunk should be aligned
12587581Sdillon * IS_ROOT	-	This 'part' is a rootfs, allocate 'a'
12687581Sdillon * ACTIVE	-	This is the active slice in the MBR
12787581Sdillon * FORCE_ALL	-	Force a dedicated disk for FreeBSD, bypassing
12887581Sdillon *			all BIOS geometry considerations
12987581Sdillon * AUTO_SIZE	-	This chunk was auto-sized and can fill-out a
13087581Sdillon *			following chunk if the following chunk is deleted.
13187581Sdillon * NEWFS	-	newfs pending, used to enable auto-resizing on
13287581Sdillon *			delete (along with AUTO_SIZE).
13387581Sdillon */
13487581Sdillon
13587581Sdillon#define CHUNK_ALIGN		0x0008
13687581Sdillon#define CHUNK_IS_ROOT		0x0010
13787581Sdillon#define CHUNK_ACTIVE		0x0020
13887581Sdillon#define CHUNK_FORCE_ALL		0x0040
13987581Sdillon#define CHUNK_AUTO_SIZE		0x0080
14087581Sdillon#define CHUNK_NEWFS		0x0100
141121888Smarcel#define	CHUNK_HAS_INDEX		0x0200
142121888Smarcel#define	CHUNK_ITOF(i)		((i & 0xFFFF) << 16)
143121888Smarcel#define	CHUNK_FTOI(f)		((f >> 16) & 0xFFFF)
14487581Sdillon
14588995Sdillon#define DELCHUNK_NORMAL		0x0000
14688995Sdillon#define DELCHUNK_RECOVER	0x0001
14787581Sdillon
148106783Sdavidcconst char *chunk_name(chunk_e);
14914792Sjoerg
15021958Sobrienconst char *
151106783Sdavidcslice_type_name(int, int);
152106949Snyan/* "chunk_n" for subtypes too */
15321958Sobrien
1548156Sphkstruct disk *
155106783SdavidcOpen_Disk(const char *);
156106949Snyan/* Will open the named disk, and return populated tree.  */
1578153Sphk
1588881Srgrimesvoid
159106783SdavidcFree_Disk(struct disk *);
160106949Snyan/* Free a tree made with Open_Disk() or Clone_Disk() */
1618153Sphk
1628156Sphkvoid
163106783SdavidcDebug_Disk(struct disk *);
164106949Snyan/* Print the content of the tree to stdout */
1658153Sphk
1668881Srgrimesvoid
167106783SdavidcSet_Bios_Geom(struct disk *, u_long, u_long, u_long);
168106949Snyan/* Set the geometry the bios uses. */
1698156Sphk
17019989Sphkvoid
171106783SdavidcSanitize_Bios_Geom(struct disk *);
172106949Snyan/* Set the bios geometry to something sane */
17319989Sphk
1748881Srgrimesint
175127081SjhbInsert_Chunk(struct chunk *, daddr_t, daddr_t, const char *, chunk_e, int,
176106783Sdavidc	u_long, const char *);
177106135Sphk
178106135Sphkint
179106783SdavidcDelete_Chunk2(struct disk *, struct chunk *, int);
180106949Snyan/* Free a chunk of disk_space modified by the passed flags. */
18188995Sdillon
18288995Sdillonint
183106783SdavidcDelete_Chunk(struct disk *, struct chunk *);
184106949Snyan/* Free a chunk of disk_space */
1858156Sphk
1868156Sphkvoid
187106783SdavidcCollapse_Disk(struct disk *);
188106949Snyan/* Experimental, do not use. */
189106949Snyan
1908156Sphkint
191106783SdavidcCollapse_Chunk(struct disk *, struct chunk *);
192106949Snyan/* Experimental, do not use. */
1938156Sphk
1948881Srgrimesint
195127081SjhbCreate_Chunk(struct disk *, daddr_t, daddr_t, chunk_e, int, u_long, const char *);
196106949Snyan/* Create a chunk with the specified paramters */
1978156Sphk
1988157Sphkvoid
199106783SdavidcAll_FreeBSD(struct disk *, int);
200106949Snyan/*
201106949Snyan * Make one FreeBSD chunk covering the entire disk;
20214792Sjoerg * if force_all is set, bypass all BIOS geometry
20314792Sjoerg * considerations.
20414792Sjoerg */
2058157Sphk
2068881Srgrimeschar *
207105681SphkCheckRules(const struct disk *);
208106949Snyan/* Return char* to warnings about broken design rules in this disklayout */
2098156Sphk
2108158Sphkchar **
211105816SphkDisk_Names(void);
212106949Snyan/*
213106949Snyan * Return char** with all disk's names (wd0, wd1 ...).  You must free
21414792Sjoerg * each pointer, as well as the array by hand
21514792Sjoerg */
2168158Sphk
21764555Snyan#ifdef PC98
2188158Sphkvoid
219106783SdavidcSet_Boot_Mgr(struct disk *, const u_char *, const size_t, const u_char *,
220106783Sdavidc	const size_t);
22164555Snyan#else
22264555Snyanvoid
223106783SdavidcSet_Boot_Mgr(struct disk *, const u_char *, const size_t);
22464555Snyan#endif
225106949Snyan/*
226106949Snyan * Use this boot-manager on this disk.  Gets written when Write_Disk()
22714792Sjoerg * is called
22814792Sjoerg */
2298158Sphk
23076551Sjkhint
231106783SdavidcSet_Boot_Blocks(struct disk *, const u_char *, const u_char *);
232106949Snyan/*
233106949Snyan * Use these boot-blocks on this disk.  Gets written when Write_Disk()
23476551Sjkh * is called. Returns nonzero upon failure.
23514792Sjoerg */
2368158Sphk
2378160Sphkint
238106783SdavidcWrite_Disk(const struct disk *);
239106949Snyan/* Write all the MBRs, disklabels, bootblocks and boot managers */
2408160Sphk
241127081Sjhbdaddr_t
242127081SjhbNext_Cyl_Aligned(const struct disk *, daddr_t);
243106949Snyan/* Round offset up to next cylinder according to the bios-geometry */
2448178Sphk
245127081Sjhbdaddr_t
246127081SjhbPrev_Cyl_Aligned(const struct disk *, daddr_t);
247106949Snyan/* Round offset down to previous cylinder according to the bios-geometry */
2488178Sphk
2498178Sphkint
250127081SjhbTrack_Aligned(const struct disk *, daddr_t);
251106949Snyan/* Check if offset is aligned on a track according to the bios geometry */
2528178Sphk
253127081Sjhbdaddr_t
254127081SjhbNext_Track_Aligned(const struct disk *, daddr_t);
255106949Snyan/* Round offset up to next track according to the bios-geometry */
2568178Sphk
257127081Sjhbdaddr_t
258127081SjhbPrev_Track_Aligned(const struct disk *, daddr_t);
259106949Snyan/* Check if offset is aligned on a track according to the bios geometry */
2608178Sphk
2618404Sphkstruct chunk *
262127081SjhbCreate_Chunk_DWIM(struct disk *, struct chunk *, daddr_t, chunk_e, int,
263106783Sdavidc	u_long);
264106949Snyan/*
265106949Snyan * This one creates a partition inside the given parent of the given
26614792Sjoerg * size, and returns a pointer to it.  The first unused chunk big
26714792Sjoerg * enough is used.
26814792Sjoerg */
2698404Sphk
2709202Srgrimeschar *
271106783SdavidcShowChunkFlags(struct chunk *);
27214792Sjoerg/* Return string to show flags. */
2739202Srgrimes
2748881Srgrimes/*
2758156Sphk * Implementation details  >>> DO NOT USE <<<
2768156Sphk */
2778156Sphk
278105816Sphkstruct disklabel;
279121888Smarcel
280106783Sdavidcvoid Fill_Disklabel(struct disklabel *, const struct disk *,
281110339Sjhb	const struct chunk *);
2828153Sphkvoid Debug_Chunk(struct chunk *);
283127595Skuriyamastruct chunk *New_Chunk(void);
2848153Sphkvoid Free_Chunk(struct chunk *);
285106783Sdavidcstruct chunk *Clone_Chunk(const struct chunk *);
286127081Sjhbint Add_Chunk(struct disk *, daddr_t, daddr_t, const char *, chunk_e, int,
287127081Sjhb	u_long, const char *);
288106783Sdavidcvoid *read_block(int, daddr_t, u_long);
289105681Sphkint write_block(int, daddr_t, const void *, u_long);
290106783Sdavidcstruct disklabel *read_disklabel(int, daddr_t, u_long);
291121888Smarcelstruct disk *Int_Open_Disk(const char *, char *);
29276551Sjkhint Fixup_Names(struct disk *);
293106783Sdavidcint MakeDevChunk(const struct chunk *, const char *);
29414792Sjoerg__END_DECLS
2958153Sphk
2968153Sphk#define dprintf	printf
2978158Sphk
2988158Sphk/* TODO
2998158Sphk *
300108533Sschweikh * Need an error string mechanism from the functions instead of warn()
3018881Srgrimes *
3028158Sphk * Make sure only FreeBSD start at offset==0
3038881Srgrimes *
3048158Sphk * Collapse must align.
3058881Srgrimes *
3068158Sphk * Make Write_Disk(struct disk*)
3078881Srgrimes *
3088158Sphk * Consider booting from OnTrack'ed disks.
3098158Sphk *
3108158Sphk * Get Bios-geom, ST506 & OnTrack from driver (or otherwise)
3118158Sphk *
3128158Sphk * Make Create_DWIM().
3138158Sphk *
3148160Sphk * Make Is_Unchanged(struct disk *d1, struct chunk *c1)
3158881Srgrimes *
3168264Sphk * don't rename slices unless we have to
3178158Sphk *
3188158Sphk *Sample output from tst01:
3198158Sphk *
32019989Sphk * Debug_Disk(wd0)  flags=0  bios_geom=0/0/0
3218158Sphk * >>        0x3d040          0    1411200    1411199 wd0      0 whole    0 0
3228158Sphk * >>>>      0x3d080          0     960120     960119 wd0s1    3 freebsd  0 8
3238158Sphk * >>>>>>    0x3d100          0      40960      40959 wd0s1a   5 part     0 0
3248158Sphk * >>>>>>    0x3d180      40960     131072     172031 wd0s1b   5 part     0 0
3258158Sphk * >>>>>>    0x3d1c0     172032     409600     581631 wd0s1e   5 part     0 0
3268158Sphk * >>>>>>    0x3d200     581632     378488     960119 wd0s1f   5 part     0 0
3278158Sphk * >>>>      0x3d140     960120       5670     965789 wd0s2    4 extended 0 8
3288303Sphk * >>>>>>    0x3d2c0     960120         63     960182 -        6 unused   0 0
3298158Sphk * >>>>>>    0x3d0c0     960183       5607     965789 wd0s5    2 fat      0 8
3308158Sphk * >>>>      0x3d280     965790       1890     967679 wd0s3    1 foo      -2 8
3318158Sphk * >>>>      0x3d300     967680     443520    1411199 wd0s4    3 freebsd  0 8
3328158Sphk * >>>>>>    0x3d340     967680     443520    1411199 wd0s4a   5 part     0 0
3338158Sphk *
3348158Sphk * ^            ^           ^          ^          ^     ^      ^ ^        ^ ^
3358158Sphk * level    chunkptr      start      size        end  name    type  subtype flags
3368158Sphk *
3378158Sphk * Underlying data structure:
3388158Sphk *
3398158Sphk *	Legend:
3408158Sphk *		<struct chunk> --> part
3418158Sphk *			|
3428158Sphk *			v next
3438158Sphk *
3448158Sphk *	<wd0> --> <wd0s1> --> <wd0s1a>
3458158Sphk *		     |           |
3468158Sphk *		     |           v
3478158Sphk *		     |        <wd0s1b>
3488158Sphk *		     |           |
3498158Sphk *		     |           v
3508158Sphk *		     |        <wd0s1e>
3518158Sphk *		     |           |
3528158Sphk *		     |           v
3538158Sphk *		     |        <wd0s1f>
3548158Sphk *		     |
3558158Sphk *		     v
3568303Sphk *		  <wd0s2> --> <unused>
3578158Sphk *		     |           |
3588158Sphk *		     |           v
3598158Sphk *		     |        <wd0s5>
3608158Sphk *		     |
3618158Sphk *		     v
3628881Srgrimes *		  <wd0s3>
3638158Sphk *		     |
3648158Sphk *		     v
3658158Sphk *		  <wd0s4> --> <wd0s4a>
3668158Sphk *
3678158Sphk *
3688158Sphk */
369173791Smtm
370173791Smtm#endif /* _LIBDISK_H_ */
371