1bf387fdbfab7e7408d7b580cbee0c0bfb13945b
[rrq/maintain_lilo.git] / src / geometry.h
1 /* geometry.h  -  Device and file geometry computation */
2 /*
3 Copyright 1992-1998 Werner Almesberger.
4 Copyright 1999-2005 John Coffman.
5 All rights reserved.
6
7 Licensed under the terms contained in the file 'COPYING' in the 
8 source directory.
9
10 */
11 #ifndef GEOMETRY_H
12 #define GEOMETRY_H
13
14 #define LINUX 1
15
16 #if LINUX
17 #include <linux/fd.h>
18 #include <linux/hdreg.h>
19 #else
20 struct hd_geometry {
21       unsigned char heads;
22       unsigned char sectors;
23       unsigned short cylinders;
24       unsigned long start;
25 };
26
27 /* hd/ide ctl's that pass (arg) ptrs to user space are numbered 0x030n/0x031n */
28 #define HDIO_GETGEO             0x0301  /* get device geometry */
29 struct floppy_struct {
30         unsigned int    size,           /* nr of sectors total */
31                         sect,           /* sectors per track */
32                         head,           /* nr of heads */
33                         track,          /* nr of tracks */
34                         stretch;        /* !=0 means double track steps */
35 #define FD_STRETCH 1
36 #define FD_SWAPSIDES 2
37 #define FD_ZEROBASED 4
38
39         unsigned char   gap,            /* gap1 size */
40
41                         rate,           /* data rate. |= 0x40 for perpendicular */
42 #define FD_2M 0x4
43 #define FD_SIZECODEMASK 0x38
44 #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
45 #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
46                              512 : 128 << FD_SIZECODE(floppy) )
47 #define FD_PERP 0x40
48
49                         spec1,          /* stepping rate, head unload time */
50                         fmt_gap;        /* gap2 size */
51         const char      * name; /* used only for predefined formats */
52 };
53 #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
54 #endif
55
56 #if 1
57 #define MAJOR(dev) (unsigned int)((((dev_t)(dev) >> 8) & 0xfff) | ((unsigned int) ((dev_t)(dev) >> 32) & ~0xfff))
58 #define MINOR(dev) (unsigned int)(((dev_t)(dev) & 0xff) | ((unsigned int) ((dev_t)(dev) >> 12) & ~0xff))
59 #define MKDEV(major,minor) (((minor & 0xff) | ((major & 0xfff) << 8) \
60           | (((unsigned long long int) (minor & ~0xff)) << 12) \
61           | (((unsigned long long int) (major & ~0xfff)) << 32)))
62 #else
63 #include <sys/sysmacros.h>
64 #ifdef major
65 #define MAJOR(dev) major(dev)
66 #define MINOR(dev) minor(dev)
67 #define MKDEV(maj,min) makedev(maj,min)
68 #else
69 /* from <linux/kdev_t.h>  */
70 /* These are for user-level "dev_t" */
71 #define MINORBITS       8
72 #define MINORMASK       ((1U << MINORBITS) - 1)
73 #error "Should not get here:  MAJOR/MINOR (geometry.h)"
74 #define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
75 #define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
76 #define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))
77 #endif
78 #endif
79
80 /*  from <linux/fs.h> */
81 #define BMAP_IOCTL 1            /* obsolete - kept for compatibility */
82 #define FIBMAP     _IO(0x00,1)  /* bmap access */
83 #define FIGETBSZ   _IO(0x00,2)  /* get the block size used for bmap */
84
85
86
87 typedef struct {
88     int device,heads;
89     int cylinders,sectors;
90     int start; /* partition offset */
91     int spb; /* sectors per block */
92     int fd,file;
93     int boot; /* non-zero after geo_open_boot */
94     int raid; /* file references require raid1 relocation */
95     dev_t dev, base_dev; /* real device if remapping (LVM, etc) */
96 } GEOMETRY;
97
98 typedef struct _dt_entry {
99     unsigned int device;
100     int bios;
101     int sectors;
102     int heads; /* 0 if inaccessible */
103     int cylinders;
104     int start;
105     struct _dt_entry *next;
106 } DT_ENTRY;
107
108 extern DT_ENTRY *disktab;
109
110
111 /* unsigned char max_partno[256]; */
112 /* index by major device number; entries must be 2**n-1 (7,15, or 63) */
113
114
115 int has_partitions(dev_t dev);
116 /* indicates that the specified device is a block hard disk device */
117 /* returns the partition mask or 0 */
118
119
120 void geo_init(char *name);
121 /* Loads the disk geometry table. */
122
123 int is_first(int device);
124 /* Returns a non-zero integer if the specified device could be the first (i.e.
125    boot) disk, zero otherwise. */
126
127
128 void geo_get(GEOMETRY *geo,int device,int user_device,int all);
129 /* Obtains geometry information of the specified device. Sets the BIOS device
130    number to user_device unless -1. If ALL is zero, only the BIOS device number
131    is retrieved and the other geometry information is undefined. */
132
133
134 int geo_open(GEOMETRY *geo,char *name,int flags);
135 /* Opens the specified file or block device, obtains the necessary geometry
136    information and returns the file descriptor. If the name contains a BIOS
137    device specification (xxx:yyy), it is removed and stored in the geometry
138    descriptor. Returns the file descriptor of the opened object. */
139
140
141 int geo_open_boot(GEOMETRY *geo,char *name);
142 /* Like get_open, but only the first sector of the device is accessed. This
143    way, no geometry information is needed. */
144
145
146 void geo_close(GEOMETRY *geo);
147 /* Closes a file or device that has previously been opened by geo_open. */
148
149
150 int geo_comp_addr(GEOMETRY *geo,int offset,SECTOR_ADDR *addr);
151 /* Determines the address of the disk sector that contains the offset'th
152    byte of the specified file or device. Returns a non-zero value if such
153    a sector exists, zero if it doesn't. */
154
155
156 int geo_find(GEOMETRY *geo,SECTOR_ADDR addr);
157 /* lseeks in the file associated with GEO for the sector at address ADDR.
158    Returns a non-zero integer on success, zero on failure. */
159
160
161 void geo_query_dev(GEOMETRY *geo,int device,int all);
162 /* opens the specified device and gets the geometry information.  That
163    information is then stored in *geo */
164
165
166 int is_dm_major(int major);
167 /* tell whether the specified major device number is one of the
168    device-mapper major devices */
169    
170
171 #if 0
172 int geo_devscan(int device);
173 /* called to fill in a disktab with arbitrary BIOS codes */
174 #endif
175
176
177 #endif