libdisk.h revision 12661
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 * 912661Speter * $Id: libdisk.h,v 1.19.2.2 1995/10/13 08:19:12 jkh 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 329202Srgrimes#if 0 338153Sphk u_long real_cyl; 348153Sphk u_long real_hd; 358153Sphk u_long real_sect; 369202Srgrimes#endif 378153Sphk u_long bios_cyl; 388153Sphk u_long bios_hd; 398153Sphk u_long bios_sect; 408158Sphk u_char *bootmgr; 418158Sphk u_char *boot1; 428158Sphk u_char *boot2; 438153Sphk struct chunk *chunks; 448153Sphk}; 458153Sphk 468153Sphkstruct chunk { 478153Sphk struct chunk *next; 488153Sphk struct chunk *part; 498745Sphk struct disk *disk; 508346Sphk long offset; 518153Sphk u_long size; 528153Sphk u_long end; 538153Sphk char *name; 548250Sphk char *oname; 558250Sphk /* Used during Fixup_Names() to avoid renaming more than 568250Sphk * absolutely needed. 578250Sphk */ 588153Sphk chunk_e type; 598153Sphk int subtype; 608153Sphk u_long flags; 618156Sphk# define CHUNK_PAST_1024 1 628881Srgrimes /* this chunk cannot be booted from because it 638264Sphk * extends past cylinder 1024 648264Sphk */ 658156Sphk# define CHUNK_BSD_COMPAT 2 668881Srgrimes /* this chunk is in the BSD-compatibility, and has a 678156Sphk * short name too, ie wd0s4f -> wd0f 688881Srgrimes */ 698156Sphk# define CHUNK_BAD144 4 708156Sphk /* this chunk has bad144 mapping */ 718156Sphk# define CHUNK_ALIGN 8 728264Sphk /* This chunk should be aligned */ 738233Sphk# define CHUNK_IS_ROOT 16 748233Sphk /* This 'part' is a rootfs, allocate 'a' */ 758264Sphk# define CHUNK_ACTIVE 32 768264Sphk /* This is the active slice in the MBR */ 7712661Speter# define CHUNK_FORCE_ALL 64 7812661Speter /* Force a dedicated disk for FreeBSD, bypassing 7912661Speter * all BIOS geometry considerations 8012661Speter */ 818250Sphk 828250Sphk void (*private_free)(void*); 838250Sphk void *(*private_clone)(void*); 848250Sphk void *private; 858250Sphk /* For data private to the application, and the management 868250Sphk * thereof. If the functions are not provided, no storage 878250Sphk * management is done, Cloning will just copy the pointer 888250Sphk * and freeing will just forget it. 898250Sphk */ 908153Sphk}; 918153Sphk 928156Sphkstruct disk * 938156SphkOpen_Disk(char *devname); 948156Sphk /* Will open the named disk, and return populated tree. 958881Srgrimes */ 968153Sphk 978156Sphkstruct disk * 988156SphkClone_Disk(struct disk *disk); 998156Sphk /* Clone a copy of a tree. Useful for "Undo" functionality 1008156Sphk */ 1018153Sphk 1028881Srgrimesvoid 1038156SphkFree_Disk(struct disk *disk); 1048156Sphk /* Free a tree made with Open_Disk() or Clone_Disk() 1058156Sphk */ 1068153Sphk 1078156Sphkvoid 1088156SphkDebug_Disk(struct disk *disk); 1098156Sphk /* Print the content of the tree to stdout 1108156Sphk */ 1118153Sphk 1129202Srgrimes#if 0 1138156Sphkstruct disk * 1148156SphkSet_Phys_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects); 1158156Sphk /* Use a different physical geometry. Makes sense for ST506 disks only. 1168156Sphk * The tree returned is read from the disk, using this geometry. 1178156Sphk */ 1189202Srgrimes#endif 1198153Sphk 1208881Srgrimesvoid 1218156SphkSet_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects); 1228156Sphk /* Set the geometry the bios uses. 1238156Sphk */ 1248156Sphk 1258881Srgrimesint 1268156SphkDelete_Chunk(struct disk *disk, struct chunk *); 1278881Srgrimes /* Free a chunk of disk_space 1288156Sphk */ 1298156Sphk 1308156Sphkvoid 1318156SphkCollapse_Disk(struct disk *disk); 1328156Sphk /* Experimental, do not use. 1338156Sphk */ 1348156Sphkint 1358156SphkCollapse_Chunk(struct disk *disk, struct chunk *chunk); 1368156Sphk /* Experimental, do not use. 1378156Sphk */ 1388156Sphk 1398881Srgrimesint 1408156SphkCreate_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int subtype, u_long flags); 1418156Sphk /* Create a chunk with the specified paramters 1428156Sphk */ 1438156Sphk 1448157Sphkvoid 14512661SpeterAll_FreeBSD(struct disk *d, int force_all); 14612661Speter /* Make one FreeBSD chunk covering the entire disk; 14712661Speter * if force_all is set, bypass all BIOS geometry 14812661Speter * considerations. 1498157Sphk */ 1508157Sphk 1518881Srgrimeschar * 1528156SphkCheckRules(struct disk *); 1538156Sphk /* Return char* to warnings about broken design rules in this disklayout 1548156Sphk */ 1558156Sphk 1568158Sphkchar ** 1578158SphkDisk_Names(); 1588158Sphk /* Return char** with all disk's names (wd0, wd1 ...). You must free 1598158Sphk * each pointer, as well as the array by hand 1608158Sphk */ 1618158Sphk 1628158Sphkvoid 1638158SphkSet_Boot_Mgr(struct disk *d, u_char *bootmgr); 1648158Sphk /* Use this boot-manager on this disk. Gets written when Write_Disk() 1658158Sphk * is called 1668158Sphk */ 1678158Sphk 1688158Sphkvoid 1698158SphkSet_Boot_Blocks(struct disk *d, u_char *boot1, u_char *boot2); 1708158Sphk /* Use these boot-blocks on this disk. Gets written when Write_Disk() 1718158Sphk * is called 1728158Sphk */ 1738158Sphk 1748160Sphkint 1758160SphkWrite_Disk(struct disk *d); 1768160Sphk /* Write all the MBRs, disklabels, bootblocks and boot managers 1778160Sphk */ 1788160Sphk 1798178Sphkint 1808178SphkCyl_Aligned(struct disk *d, u_long offset); 1818178Sphk /* Check if offset is aligned on a cylinder according to the 1828178Sphk * bios geometry 1838178Sphk */ 1848178Sphk 1858178Sphku_long 1868178SphkNext_Cyl_Aligned(struct disk *d, u_long offset); 1878178Sphk /* Round offset up to next cylinder according to the bios-geometry 1888178Sphk */ 1898178Sphk 1908178Sphku_long 1918178SphkPrev_Cyl_Aligned(struct disk *d, u_long offset); 1928178Sphk /* Round offset down to previous cylinder according to the bios- 1938178Sphk * geometry 1948178Sphk */ 1958178Sphk 1968178Sphkint 1978178SphkTrack_Aligned(struct disk *d, u_long offset); 1988178Sphk /* Check if offset is aligned on a track according to the 1998178Sphk * bios geometry 2008178Sphk */ 2018178Sphk 2028178Sphku_long 2038178SphkNext_Track_Aligned(struct disk *d, u_long offset); 2048178Sphk /* Round offset up to next track according to the bios-geometry 2058178Sphk */ 2068178Sphk 2078178Sphku_long 2088178SphkPrev_Track_Aligned(struct disk *d, u_long offset); 2098178Sphk /* Check if offset is aligned on a track according to the 2108178Sphk * bios geometry 2118178Sphk */ 2128178Sphk 2138404Sphkstruct chunk * 2148404SphkCreate_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size, chunk_e type, int subtype, u_long flags); 2158404Sphk /* This one creates a partition inside the given parent of the given 2168404Sphk * size, and returns a pointer to it. The first unused chunk big 2178404Sphk * enough is used. 2188404Sphk */ 2198404Sphk 22012661Speterint 22112661SpeterMakeDev(struct chunk *c, char *path); 22212661Speter 22312661Speterint 2248477SphkMakeDevDisk(struct disk *d,char *path); 2258477Sphk /* Make device nodes for all chunks on this disk */ 2268477Sphk 2279202Srgrimeschar * 2289202SrgrimesShowChunkFlags(struct chunk *c); 2299202Srgrimes /* Return string to show flags. */ 2309202Srgrimes 2319202Srgrimeschar * 2329202SrgrimesChunkCanBeRoot(struct chunk *c); 2339202Srgrimes /* Return NULL if chunk can be /, explanation otherwise */ 2349202Srgrimes 2358881Srgrimes/* 2368156Sphk * Implementation details >>> DO NOT USE <<< 2378156Sphk */ 2388156Sphk 2398153Sphkvoid Debug_Chunk(struct chunk *); 2408153Sphkvoid Free_Chunk(struct chunk *); 2418153Sphkstruct chunk * Clone_Chunk(struct chunk *); 2428346Sphkint Add_Chunk(struct disk *, long , u_long , char *, chunk_e, int , u_long); 2438153Sphkvoid Bios_Limit_Chunk(struct chunk *, u_long); 2448153Sphkvoid * read_block(int, daddr_t ); 2458178Sphkvoid write_block(int fd, daddr_t block, void *foo); 2468153Sphkstruct disklabel * read_disklabel(int, daddr_t); 2478153Sphku_short dkcksum(struct disklabel *); 2488153Sphkstruct chunk * Find_Mother_Chunk(struct chunk *, u_long , u_long , chunk_e); 2498158Sphkstruct disk * Int_Open_Disk(char *name, u_long size); 2508183Sphkvoid Fixup_Names(struct disk *); 2518153Sphk 2528153Sphk#define dprintf printf 2538158Sphk 2548158Sphk/* TODO 2558158Sphk * 2568158Sphk * Need a error string mechanism from the functions instead of warn() 2578881Srgrimes * 2588158Sphk * Make sure only FreeBSD start at offset==0 2598881Srgrimes * 2608158Sphk * Collapse must align. 2618881Srgrimes * 2628158Sphk * Make Write_Disk(struct disk*) 2638881Srgrimes * 2648158Sphk * Consider booting from OnTrack'ed disks. 2658158Sphk * 2668158Sphk * Get Bios-geom, ST506 & OnTrack from driver (or otherwise) 2678158Sphk * 2688158Sphk * Make Create_DWIM(). 2698158Sphk * 2708160Sphk * Make Is_Unchanged(struct disk *d1, struct chunk *c1) 2718881Srgrimes * 2728264Sphk * don't rename slices unless we have to 2738158Sphk * 2748158Sphk *Sample output from tst01: 2758158Sphk * 2768158Sphk * Debug_Disk(wd0) flags=0 real_geom=0/0/0 bios_geom=0/0/0 2778158Sphk * >> 0x3d040 0 1411200 1411199 wd0 0 whole 0 0 2788158Sphk * >>>> 0x3d080 0 960120 960119 wd0s1 3 freebsd 0 8 2798158Sphk * >>>>>> 0x3d100 0 40960 40959 wd0s1a 5 part 0 0 2808158Sphk * >>>>>> 0x3d180 40960 131072 172031 wd0s1b 5 part 0 0 2818158Sphk * >>>>>> 0x3d1c0 172032 409600 581631 wd0s1e 5 part 0 0 2828158Sphk * >>>>>> 0x3d200 581632 378488 960119 wd0s1f 5 part 0 0 2838158Sphk * >>>> 0x3d140 960120 5670 965789 wd0s2 4 extended 0 8 2848303Sphk * >>>>>> 0x3d2c0 960120 63 960182 - 6 unused 0 0 2858158Sphk * >>>>>> 0x3d0c0 960183 5607 965789 wd0s5 2 fat 0 8 2868158Sphk * >>>> 0x3d280 965790 1890 967679 wd0s3 1 foo -2 8 2878158Sphk * >>>> 0x3d300 967680 443520 1411199 wd0s4 3 freebsd 0 8 2888158Sphk * >>>>>> 0x3d340 967680 443520 1411199 wd0s4a 5 part 0 0 2898158Sphk * 2908158Sphk * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 2918158Sphk * level chunkptr start size end name type subtype flags 2928158Sphk * 2938158Sphk * Underlying data structure: 2948158Sphk * 2958158Sphk * Legend: 2968158Sphk * <struct chunk> --> part 2978158Sphk * | 2988158Sphk * v next 2998158Sphk * 3008158Sphk * <wd0> --> <wd0s1> --> <wd0s1a> 3018158Sphk * | | 3028158Sphk * | v 3038158Sphk * | <wd0s1b> 3048158Sphk * | | 3058158Sphk * | v 3068158Sphk * | <wd0s1e> 3078158Sphk * | | 3088158Sphk * | v 3098158Sphk * | <wd0s1f> 3108158Sphk * | 3118158Sphk * v 3128303Sphk * <wd0s2> --> <unused> 3138158Sphk * | | 3148158Sphk * | v 3158158Sphk * | <wd0s5> 3168158Sphk * | 3178158Sphk * v 3188881Srgrimes * <wd0s3> 3198158Sphk * | 3208158Sphk * v 3218158Sphk * <wd0s4> --> <wd0s4a> 3228158Sphk * 3238158Sphk * 3248158Sphk */ 325