libdisk.h revision 8745
18156Sphk/*
28156Sphk * ----------------------------------------------------------------------------
38156Sphk * "THE BEER-WARE LICENSE" (Revision 42):
48156Sphk * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
58156Sphk * can do whatever you want with this stuff. If we meet some day, and you think
68156Sphk * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
78156Sphk * ----------------------------------------------------------------------------
88156Sphk *
98745Sphk * $Id: libdisk.h,v 1.16 1995/05/12 18:49:58 phk Exp $
108156Sphk *
118156Sphk */
128153Sphk
138158Sphk#define MAX_NO_DISKS	20
148158Sphk	/* Max # of disks Disk_Names() will return */
158158Sphk
168303Sphktypedef enum {
178303Sphk	whole,
188303Sphk	unknown,
198303Sphk	fat,
208303Sphk	freebsd,
218303Sphk	extended,
228303Sphk	part,
238303Sphk	unused,
248303Sphk	} chunk_e;
258156Sphk
268241Sjkhextern char *chunk_n[];
278153Sphk
288153Sphkstruct disk {
298153Sphk	char		*name;
308153Sphk	u_long		flags;
318156Sphk#		define DISK_ON_TRACK	1
328153Sphk	u_long		real_cyl;
338153Sphk	u_long		real_hd;
348153Sphk	u_long		real_sect;
358153Sphk	u_long		bios_cyl;
368153Sphk	u_long		bios_hd;
378153Sphk	u_long		bios_sect;
388158Sphk	u_char		*bootmgr;
398158Sphk	u_char		*boot1;
408158Sphk	u_char		*boot2;
418153Sphk	struct chunk	*chunks;
428153Sphk};
438153Sphk
448153Sphkstruct chunk {
458153Sphk	struct chunk	*next;
468153Sphk	struct chunk	*part;
478745Sphk	struct disk	*disk;
488346Sphk	long		offset;
498153Sphk	u_long		size;
508153Sphk	u_long		end;
518153Sphk	char		*name;
528250Sphk	char		*oname;
538250Sphk		/* Used during Fixup_Names() to avoid renaming more than
548250Sphk		 * absolutely needed.
558250Sphk		 */
568153Sphk	chunk_e		type;
578153Sphk	int		subtype;
588153Sphk	u_long		flags;
598156Sphk#		define CHUNK_PAST_1024		1
608264Sphk			/* this chunk cannot be booted from because it
618264Sphk			 * extends past cylinder 1024
628264Sphk			 */
638156Sphk#		define CHUNK_BSD_COMPAT	2
648156Sphk			/* this chunk is in the BSD-compatibility, and has a
658156Sphk			 * short name too, ie wd0s4f -> wd0f
668156Sphk         		*/
678156Sphk#		define CHUNK_BAD144		4
688156Sphk			/* this chunk has bad144 mapping */
698156Sphk#		define CHUNK_ALIGN		8
708264Sphk			/* This chunk should be aligned */
718233Sphk#		define CHUNK_IS_ROOT		16
728233Sphk			/* This 'part' is a rootfs, allocate 'a' */
738264Sphk#		define CHUNK_ACTIVE		32
748264Sphk			/* This is the active slice in the MBR */
758250Sphk
768250Sphk	void		(*private_free)(void*);
778250Sphk	void		*(*private_clone)(void*);
788250Sphk	void		*private;
798250Sphk		/* For data private to the application, and the management
808250Sphk		 * thereof.  If the functions are not provided, no storage
818250Sphk		 * management is done, Cloning will just copy the pointer
828250Sphk		 * and freeing will just forget it.
838250Sphk		 */
848153Sphk};
858153Sphk
868156Sphkstruct disk *
878156SphkOpen_Disk(char *devname);
888156Sphk	/* Will open the named disk, and return populated tree.
898156Sphk	 */
908153Sphk
918156Sphkstruct disk *
928156SphkClone_Disk(struct disk *disk);
938156Sphk	/* Clone a copy of a tree.  Useful for "Undo" functionality
948156Sphk	 */
958153Sphk
968156Sphkvoid
978156SphkFree_Disk(struct disk *disk);
988156Sphk	/* Free a tree made with Open_Disk() or Clone_Disk()
998156Sphk	 */
1008153Sphk
1018156Sphkvoid
1028156SphkDebug_Disk(struct disk *disk);
1038156Sphk	/* Print the content of the tree to stdout
1048156Sphk	 */
1058153Sphk
1068156Sphkstruct disk *
1078156SphkSet_Phys_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
1088156Sphk	/* Use a different physical geometry.  Makes sense for ST506 disks only.
1098156Sphk	 * The tree returned is read from the disk, using this geometry.
1108156Sphk	 */
1118153Sphk
1128156Sphkvoid
1138156SphkSet_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects);
1148156Sphk	/* Set the geometry the bios uses.
1158156Sphk	 */
1168156Sphk
1178156Sphkint
1188156SphkDelete_Chunk(struct disk *disk, struct chunk *);
1198156Sphk	/* Free a chunk of disk_space
1208156Sphk	 */
1218156Sphk
1228156Sphkvoid
1238156SphkCollapse_Disk(struct disk *disk);
1248156Sphk	/* Experimental, do not use.
1258156Sphk	 */
1268156Sphkint
1278156SphkCollapse_Chunk(struct disk *disk, struct chunk *chunk);
1288156Sphk	/* Experimental, do not use.
1298156Sphk	 */
1308156Sphk
1318156Sphkint
1328156SphkCreate_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int subtype, u_long flags);
1338156Sphk	/* Create a chunk with the specified paramters
1348156Sphk	 */
1358156Sphk
1368157Sphkvoid
1378157SphkAll_FreeBSD(struct disk *d);
1388157Sphk	/* Make one FreeBSD chunk covering the entire disk
1398157Sphk	 */
1408157Sphk
1418156Sphkchar *
1428156SphkCheckRules(struct disk *);
1438156Sphk	/* Return char* to warnings about broken design rules in this disklayout
1448156Sphk	 */
1458156Sphk
1468158Sphkchar **
1478158SphkDisk_Names();
1488158Sphk	/* Return char** with all disk's names (wd0, wd1 ...).  You must free
1498158Sphk	 * each pointer, as well as the array by hand
1508158Sphk	 */
1518158Sphk
1528158Sphkvoid
1538158SphkSet_Boot_Mgr(struct disk *d, u_char *bootmgr);
1548158Sphk	/* Use this boot-manager on this disk.  Gets written when Write_Disk()
1558158Sphk	 * is called
1568158Sphk	 */
1578158Sphk
1588158Sphkvoid
1598158SphkSet_Boot_Blocks(struct disk *d, u_char *boot1, u_char *boot2);
1608158Sphk	/* Use these boot-blocks on this disk.  Gets written when Write_Disk()
1618158Sphk	 * is called
1628158Sphk	 */
1638158Sphk
1648160Sphkint
1658160SphkWrite_Disk(struct disk *d);
1668160Sphk	/* Write all the MBRs, disklabels, bootblocks and boot managers
1678160Sphk	 */
1688160Sphk
1698178Sphkint
1708178SphkCyl_Aligned(struct disk *d, u_long offset);
1718178Sphk	/* Check if offset is aligned on a cylinder according to the
1728178Sphk	 * bios geometry
1738178Sphk	 */
1748178Sphk
1758178Sphku_long
1768178SphkNext_Cyl_Aligned(struct disk *d, u_long offset);
1778178Sphk	/* Round offset up to next cylinder according to the bios-geometry
1788178Sphk	 */
1798178Sphk
1808178Sphku_long
1818178SphkPrev_Cyl_Aligned(struct disk *d, u_long offset);
1828178Sphk	/* Round offset down to previous cylinder according to the bios-
1838178Sphk	 * geometry
1848178Sphk	 */
1858178Sphk
1868178Sphkint
1878178SphkTrack_Aligned(struct disk *d, u_long offset);
1888178Sphk	/* Check if offset is aligned on a track according to the
1898178Sphk	 * bios geometry
1908178Sphk	 */
1918178Sphk
1928178Sphku_long
1938178SphkNext_Track_Aligned(struct disk *d, u_long offset);
1948178Sphk	/* Round offset up to next track according to the bios-geometry
1958178Sphk	 */
1968178Sphk
1978178Sphku_long
1988178SphkPrev_Track_Aligned(struct disk *d, u_long offset);
1998178Sphk	/* Check if offset is aligned on a track according to the
2008178Sphk	 * bios geometry
2018178Sphk	 */
2028178Sphk
2038404Sphkstruct chunk *
2048404SphkCreate_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size, chunk_e type, int subtype, u_long flags);
2058404Sphk	/* This one creates a partition inside the given parent of the given
2068404Sphk	 * size, and returns a pointer to it.  The first unused chunk big
2078404Sphk	 * enough is used.
2088404Sphk	 */
2098404Sphk
2108477Sphkvoid
2118477SphkMakeDevDisk(struct disk *d,char *path);
2128477Sphk	/* Make device nodes for all chunks on this disk */
2138477Sphk
2148156Sphk/*
2158156Sphk * Implementation details  >>> DO NOT USE <<<
2168156Sphk */
2178156Sphk
2188153Sphkvoid Debug_Chunk(struct chunk *);
2198153Sphkvoid Free_Chunk(struct chunk *);
2208153Sphkstruct chunk * Clone_Chunk(struct chunk *);
2218346Sphkint Add_Chunk(struct disk *, long , u_long , char *, chunk_e, int , u_long);
2228153Sphkvoid Bios_Limit_Chunk(struct chunk *, u_long);
2238153Sphkvoid * read_block(int, daddr_t );
2248178Sphkvoid write_block(int fd, daddr_t block, void *foo);
2258153Sphkstruct disklabel * read_disklabel(int, daddr_t);
2268153Sphku_short	dkcksum(struct disklabel *);
2278153Sphkstruct chunk * Find_Mother_Chunk(struct chunk *, u_long , u_long , chunk_e);
2288158Sphkstruct disk * Int_Open_Disk(char *name, u_long size);
2298183Sphkvoid Fixup_Names(struct disk *);
2308153Sphk
2318153Sphk#define dprintf	printf
2328158Sphk
2338158Sphk/* TODO
2348158Sphk *
2358158Sphk * Need a error string mechanism from the functions instead of warn()
2368158Sphk *
2378158Sphk * Make sure only FreeBSD start at offset==0
2388158Sphk *
2398158Sphk * Collapse must align.
2408158Sphk *
2418158Sphk * Make Write_Disk(struct disk*)
2428158Sphk *
2438158Sphk * Consider booting from OnTrack'ed disks.
2448158Sphk *
2458158Sphk * Get Bios-geom, ST506 & OnTrack from driver (or otherwise)
2468158Sphk *
2478158Sphk * Make Create_DWIM().
2488158Sphk *
2498160Sphk * Make Is_Unchanged(struct disk *d1, struct chunk *c1)
2508178Sphk *
2518264Sphk * don't rename slices unless we have to
2528158Sphk *
2538158Sphk *Sample output from tst01:
2548158Sphk *
2558158Sphk * Debug_Disk(wd0)  flags=0  real_geom=0/0/0  bios_geom=0/0/0
2568158Sphk * >>        0x3d040          0    1411200    1411199 wd0      0 whole    0 0
2578158Sphk * >>>>      0x3d080          0     960120     960119 wd0s1    3 freebsd  0 8
2588158Sphk * >>>>>>    0x3d100          0      40960      40959 wd0s1a   5 part     0 0
2598158Sphk * >>>>>>    0x3d180      40960     131072     172031 wd0s1b   5 part     0 0
2608158Sphk * >>>>>>    0x3d1c0     172032     409600     581631 wd0s1e   5 part     0 0
2618158Sphk * >>>>>>    0x3d200     581632     378488     960119 wd0s1f   5 part     0 0
2628158Sphk * >>>>      0x3d140     960120       5670     965789 wd0s2    4 extended 0 8
2638303Sphk * >>>>>>    0x3d2c0     960120         63     960182 -        6 unused   0 0
2648158Sphk * >>>>>>    0x3d0c0     960183       5607     965789 wd0s5    2 fat      0 8
2658158Sphk * >>>>      0x3d280     965790       1890     967679 wd0s3    1 foo      -2 8
2668158Sphk * >>>>      0x3d300     967680     443520    1411199 wd0s4    3 freebsd  0 8
2678158Sphk * >>>>>>    0x3d340     967680     443520    1411199 wd0s4a   5 part     0 0
2688158Sphk *
2698158Sphk * ^            ^           ^          ^          ^     ^      ^ ^        ^ ^
2708158Sphk * level    chunkptr      start      size        end  name    type  subtype flags
2718158Sphk *
2728158Sphk * Underlying data structure:
2738158Sphk *
2748158Sphk *	Legend:
2758158Sphk *		<struct chunk> --> part
2768158Sphk *			|
2778158Sphk *			v next
2788158Sphk *
2798158Sphk *	<wd0> --> <wd0s1> --> <wd0s1a>
2808158Sphk *		     |           |
2818158Sphk *		     |           v
2828158Sphk *		     |        <wd0s1b>
2838158Sphk *		     |           |
2848158Sphk *		     |           v
2858158Sphk *		     |        <wd0s1e>
2868158Sphk *		     |           |
2878158Sphk *		     |           v
2888158Sphk *		     |        <wd0s1f>
2898158Sphk *		     |
2908158Sphk *		     v
2918303Sphk *		  <wd0s2> --> <unused>
2928158Sphk *		     |           |
2938158Sphk *		     |           v
2948158Sphk *		     |        <wd0s5>
2958158Sphk *		     |
2968158Sphk *		     v
2978158Sphk *		  <wd0s3>
2988158Sphk *		     |
2998158Sphk *		     v
3008158Sphk *		  <wd0s4> --> <wd0s4a>
3018158Sphk *
3028158Sphk *
3038158Sphk */
304