libdisk.h revision 14792
18156Sphk/*
214792Sjoerg* ----------------------------------------------------------------------------
314792Sjoerg* "THE BEER-WARE LICENSE" (Revision 42):
414792Sjoerg* <phk@login.dknet.dk> 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*
914792Sjoerg* $Id: libdisk.h,v 1.20 1995/12/07 10:33:21 peter Exp $
1014792Sjoerg*
1114792Sjoerg*/
128153Sphk
138158Sphk#define MAX_NO_DISKS	20
1414792Sjoerg/* Max # of disks Disk_Names() will return */
158158Sphk
168303Sphktypedef enum {
178303Sphk	whole,
188303Sphk	unknown,
198303Sphk	fat,
208303Sphk	freebsd,
218303Sphk	extended,
228303Sphk	part,
238303Sphk	unused,
2414792Sjoerg} chunk_e;
258156Sphk
2614792Sjoerg__BEGIN_DECLS
278153Sphkstruct disk {
288153Sphk	char		*name;
298153Sphk	u_long		flags;
308156Sphk#		define DISK_ON_TRACK	1
319202Srgrimes#if 0
328153Sphk	u_long		real_cyl;
338153Sphk	u_long		real_hd;
348153Sphk	u_long		real_sect;
359202Srgrimes#endif
368153Sphk	u_long		bios_cyl;
378153Sphk	u_long		bios_hd;
388153Sphk	u_long		bios_sect;
398158Sphk	u_char		*bootmgr;
408158Sphk	u_char		*boot1;
418158Sphk	u_char		*boot2;
428153Sphk	struct chunk	*chunks;
438153Sphk};
448153Sphk
458153Sphkstruct chunk {
468153Sphk	struct chunk	*next;
478153Sphk	struct chunk	*part;
488745Sphk	struct disk	*disk;
498346Sphk	long		offset;
508153Sphk	u_long		size;
518153Sphk	u_long		end;
528153Sphk	char		*name;
538250Sphk	char		*oname;
5414792Sjoerg	/* Used during Fixup_Names() to avoid renaming more than
5514792Sjoerg	 * absolutely needed.
5614792Sjoerg	 */
578153Sphk	chunk_e		type;
588153Sphk	int		subtype;
598153Sphk	u_long		flags;
608156Sphk#		define CHUNK_PAST_1024		1
6114792Sjoerg		/* this chunk cannot be booted from because it
6214792Sjoerg		 * extends past cylinder 1024
6314792Sjoerg		 */
648156Sphk#		define CHUNK_BSD_COMPAT	2
6514792Sjoerg		/* this chunk is in the BSD-compatibility, and has a
6614792Sjoerg		 * short name too, ie wd0s4f -> wd0f
6714792Sjoerg		*/
688156Sphk#		define CHUNK_BAD144		4
6914792Sjoerg		/* this chunk has bad144 mapping */
708156Sphk#		define CHUNK_ALIGN		8
7114792Sjoerg		/* This chunk should be aligned */
728233Sphk#		define CHUNK_IS_ROOT		16
7314792Sjoerg		/* This 'part' is a rootfs, allocate 'a' */
748264Sphk#		define CHUNK_ACTIVE		32
7514792Sjoerg		/* This is the active slice in the MBR */
7612661Speter#		define CHUNK_FORCE_ALL		64
7714792Sjoerg		/* Force a dedicated disk for FreeBSD, bypassing
7814792Sjoerg		 * all BIOS geometry considerations
7914792Sjoerg		 */
808250Sphk
818250Sphk	void		(*private_free)(void*);
828250Sphk	void		*(*private_clone)(void*);
8314792Sjoerg	void		*private_data;
8414792Sjoerg	/* For data private to the application, and the management
8514792Sjoerg	 * thereof.  If the functions are not provided, no storage
8614792Sjoerg	 * management is done, Cloning will just copy the pointer
8714792Sjoerg	 * and freeing will just forget it.
8814792Sjoerg	 */
898153Sphk};
908153Sphk
9114792Sjoergextern const char *chunk_n[];
9214792Sjoergextern const u_char *boot1, *boot2;
9314792Sjoerg
948156Sphkstruct disk *
9514792SjoergOpen_Disk(const char *devname);
9614792Sjoerg/* Will open the named disk, and return populated tree.
9714792Sjoerg */
988153Sphk
998156Sphkstruct disk *
1008156SphkClone_Disk(struct disk *disk);
10114792Sjoerg/* Clone a copy of a tree.  Useful for "Undo" functionality
10214792Sjoerg */
1038153Sphk
1048881Srgrimesvoid
1058156SphkFree_Disk(struct disk *disk);
10614792Sjoerg/* Free a tree made with Open_Disk() or Clone_Disk()
10714792Sjoerg */
1088153Sphk
1098156Sphkvoid
1108156SphkDebug_Disk(struct disk *disk);
11114792Sjoerg/* Print the content of the tree to stdout
11214792Sjoerg */
1138153Sphk
1149202Srgrimes#if 0
1158156Sphkstruct disk *
1168156SphkSet_Phys_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
11714792Sjoerg/* Use a different physical geometry.  Makes sense for ST506 disks only.
11814792Sjoerg * The tree returned is read from the disk, using this geometry.
11914792Sjoerg */
1209202Srgrimes#endif
1218153Sphk
1228881Srgrimesvoid
1238156SphkSet_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
12414792Sjoerg/* Set the geometry the bios uses.
12514792Sjoerg */
1268156Sphk
1278881Srgrimesint
1288156SphkDelete_Chunk(struct disk *disk, struct chunk *);
12914792Sjoerg/* Free a chunk of disk_space
13014792Sjoerg */
1318156Sphk
1328156Sphkvoid
1338156SphkCollapse_Disk(struct disk *disk);
13414792Sjoerg/* Experimental, do not use.
13514792Sjoerg */
1368156Sphkint
1378156SphkCollapse_Chunk(struct disk *disk, struct chunk *chunk);
13814792Sjoerg/* Experimental, do not use.
13914792Sjoerg */
1408156Sphk
1418881Srgrimesint
14214792SjoergCreate_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type,
14314792Sjoerg	int subtype, u_long flags);
14414792Sjoerg/* Create a chunk with the specified paramters
14514792Sjoerg */
1468156Sphk
1478157Sphkvoid
14812661SpeterAll_FreeBSD(struct disk *d, int force_all);
14914792Sjoerg/* Make one FreeBSD chunk covering the entire disk;
15014792Sjoerg * if force_all is set, bypass all BIOS geometry
15114792Sjoerg * considerations.
15214792Sjoerg */
1538157Sphk
1548881Srgrimeschar *
1558156SphkCheckRules(struct disk *);
15614792Sjoerg/* Return char* to warnings about broken design rules in this disklayout
15714792Sjoerg */
1588156Sphk
1598158Sphkchar **
1608158SphkDisk_Names();
16114792Sjoerg/* Return char** with all disk's names (wd0, wd1 ...).  You must free
16214792Sjoerg * each pointer, as well as the array by hand
16314792Sjoerg */
1648158Sphk
1658158Sphkvoid
16614792SjoergSet_Boot_Mgr(struct disk *d, const u_char *bootmgr);
16714792Sjoerg/* Use this boot-manager on this disk.  Gets written when Write_Disk()
16814792Sjoerg * is called
16914792Sjoerg */
1708158Sphk
1718158Sphkvoid
17214792SjoergSet_Boot_Blocks(struct disk *d, const u_char *boot1, const u_char *boot2);
17314792Sjoerg/* Use these boot-blocks on this disk.  Gets written when Write_Disk()
17414792Sjoerg * is called
17514792Sjoerg */
1768158Sphk
1778160Sphkint
1788160SphkWrite_Disk(struct disk *d);
17914792Sjoerg/* Write all the MBRs, disklabels, bootblocks and boot managers
18014792Sjoerg */
1818160Sphk
1828178Sphkint
1838178SphkCyl_Aligned(struct disk *d, u_long offset);
18414792Sjoerg/* Check if offset is aligned on a cylinder according to the
18514792Sjoerg * bios geometry
18614792Sjoerg */
1878178Sphk
1888178Sphku_long
1898178SphkNext_Cyl_Aligned(struct disk *d, u_long offset);
19014792Sjoerg/* Round offset up to next cylinder according to the bios-geometry
19114792Sjoerg */
1928178Sphk
1938178Sphku_long
1948178SphkPrev_Cyl_Aligned(struct disk *d, u_long offset);
19514792Sjoerg/* Round offset down to previous cylinder according to the bios-
19614792Sjoerg * geometry
19714792Sjoerg */
1988178Sphk
1998178Sphkint
2008178SphkTrack_Aligned(struct disk *d, u_long offset);
20114792Sjoerg/* Check if offset is aligned on a track according to the
20214792Sjoerg * bios geometry
20314792Sjoerg */
2048178Sphk
2058178Sphku_long
2068178SphkNext_Track_Aligned(struct disk *d, u_long offset);
20714792Sjoerg/* Round offset up to next track according to the bios-geometry
20814792Sjoerg */
2098178Sphk
2108178Sphku_long
2118178SphkPrev_Track_Aligned(struct disk *d, u_long offset);
21214792Sjoerg/* Check if offset is aligned on a track according to the
21314792Sjoerg * bios geometry
21414792Sjoerg */
2158178Sphk
2168404Sphkstruct chunk *
21714792SjoergCreate_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size,
21814792Sjoerg	chunk_e type, int subtype, u_long flags);
21914792Sjoerg/* This one creates a partition inside the given parent of the given
22014792Sjoerg * size, and returns a pointer to it.  The first unused chunk big
22114792Sjoerg * enough is used.
22214792Sjoerg */
2238404Sphk
22412661Speterint
22514792SjoergMakeDev(struct chunk *c, const char *path);
22612661Speter
22712661Speterint
22814792SjoergMakeDevDisk(struct disk *d, const char *path);
22914792Sjoerg/* Make device nodes for all chunks on this disk */
2308477Sphk
2319202Srgrimeschar *
2329202SrgrimesShowChunkFlags(struct chunk *c);
23314792Sjoerg/* Return string to show flags. */
2349202Srgrimes
2359202Srgrimeschar *
2369202SrgrimesChunkCanBeRoot(struct chunk *c);
23714792Sjoerg/* Return NULL if chunk can be /, explanation otherwise */
2389202Srgrimes
2398881Srgrimes/*
2408156Sphk * Implementation details  >>> DO NOT USE <<<
2418156Sphk */
2428156Sphk
2438153Sphkvoid Debug_Chunk(struct chunk *);
2448153Sphkvoid Free_Chunk(struct chunk *);
2458153Sphkstruct chunk * Clone_Chunk(struct chunk *);
24614792Sjoergint Add_Chunk(struct disk *, long, u_long, const char *, chunk_e, int, u_long);
2478153Sphkvoid Bios_Limit_Chunk(struct chunk *, u_long);
24814792Sjoergvoid * read_block(int, daddr_t);
2498178Sphkvoid write_block(int fd, daddr_t block, void *foo);
2508153Sphkstruct disklabel * read_disklabel(int, daddr_t);
2518153Sphku_short	dkcksum(struct disklabel *);
25214792Sjoergstruct chunk * Find_Mother_Chunk(struct chunk *, u_long, u_long, chunk_e);
25314792Sjoergstruct disk * Int_Open_Disk(const char *name, u_long size);
2548183Sphkvoid Fixup_Names(struct disk *);
25514792Sjoerg__END_DECLS
2568153Sphk
2578153Sphk#define dprintf	printf
2588158Sphk
2598158Sphk/* TODO
2608158Sphk *
2618158Sphk * Need a error string mechanism from the functions instead of warn()
2628881Srgrimes *
2638158Sphk * Make sure only FreeBSD start at offset==0
2648881Srgrimes *
2658158Sphk * Collapse must align.
2668881Srgrimes *
2678158Sphk * Make Write_Disk(struct disk*)
2688881Srgrimes *
2698158Sphk * Consider booting from OnTrack'ed disks.
2708158Sphk *
2718158Sphk * Get Bios-geom, ST506 & OnTrack from driver (or otherwise)
2728158Sphk *
2738158Sphk * Make Create_DWIM().
2748158Sphk *
2758160Sphk * Make Is_Unchanged(struct disk *d1, struct chunk *c1)
2768881Srgrimes *
2778264Sphk * don't rename slices unless we have to
2788158Sphk *
2798158Sphk *Sample output from tst01:
2808158Sphk *
2818158Sphk * Debug_Disk(wd0)  flags=0  real_geom=0/0/0  bios_geom=0/0/0
2828158Sphk * >>        0x3d040          0    1411200    1411199 wd0      0 whole    0 0
2838158Sphk * >>>>      0x3d080          0     960120     960119 wd0s1    3 freebsd  0 8
2848158Sphk * >>>>>>    0x3d100          0      40960      40959 wd0s1a   5 part     0 0
2858158Sphk * >>>>>>    0x3d180      40960     131072     172031 wd0s1b   5 part     0 0
2868158Sphk * >>>>>>    0x3d1c0     172032     409600     581631 wd0s1e   5 part     0 0
2878158Sphk * >>>>>>    0x3d200     581632     378488     960119 wd0s1f   5 part     0 0
2888158Sphk * >>>>      0x3d140     960120       5670     965789 wd0s2    4 extended 0 8
2898303Sphk * >>>>>>    0x3d2c0     960120         63     960182 -        6 unused   0 0
2908158Sphk * >>>>>>    0x3d0c0     960183       5607     965789 wd0s5    2 fat      0 8
2918158Sphk * >>>>      0x3d280     965790       1890     967679 wd0s3    1 foo      -2 8
2928158Sphk * >>>>      0x3d300     967680     443520    1411199 wd0s4    3 freebsd  0 8
2938158Sphk * >>>>>>    0x3d340     967680     443520    1411199 wd0s4a   5 part     0 0
2948158Sphk *
2958158Sphk * ^            ^           ^          ^          ^     ^      ^ ^        ^ ^
2968158Sphk * level    chunkptr      start      size        end  name    type  subtype flags
2978158Sphk *
2988158Sphk * Underlying data structure:
2998158Sphk *
3008158Sphk *	Legend:
3018158Sphk *		<struct chunk> --> part
3028158Sphk *			|
3038158Sphk *			v next
3048158Sphk *
3058158Sphk *	<wd0> --> <wd0s1> --> <wd0s1a>
3068158Sphk *		     |           |
3078158Sphk *		     |           v
3088158Sphk *		     |        <wd0s1b>
3098158Sphk *		     |           |
3108158Sphk *		     |           v
3118158Sphk *		     |        <wd0s1e>
3128158Sphk *		     |           |
3138158Sphk *		     |           v
3148158Sphk *		     |        <wd0s1f>
3158158Sphk *		     |
3168158Sphk *		     v
3178303Sphk *		  <wd0s2> --> <unused>
3188158Sphk *		     |           |
3198158Sphk *		     |           v
3208158Sphk *		     |        <wd0s5>
3218158Sphk *		     |
3228158Sphk *		     v
3238881Srgrimes *		  <wd0s3>
3248158Sphk *		     |
3258158Sphk *		     v
3268158Sphk *		  <wd0s4> --> <wd0s4a>
3278158Sphk *
3288158Sphk *
3298158Sphk */
330