]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libdisk/libdisk.h
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libdisk / libdisk.h
1 /*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD$
10 *
11 */
12
13 /* #define DEBUG 1 */
14 /* You can define a particular architecture here if you are debugging. */
15 /* #define P_DEBUG p_sparc64 */
16
17 #define MAX_NO_DISKS    32
18 /* Max # of disks Disk_Names() will return */
19
20 #define MAX_SEC_SIZE    2048  /* maximum sector size that is supported */
21 #define MIN_SEC_SIZE    512   /* the sector size to end sensing at */
22
23 enum platform {
24         p_any,                  /* for debugging ! */
25         p_alpha,
26         p_i386,
27         p_pc98,
28         p_sparc64,
29         p_ia64,
30         p_ppc,
31         p_amd64,
32         p_arm
33 };
34 extern const enum platform platform;
35
36 typedef enum {
37         whole,
38         unknown,
39
40         sun,
41         pc98,
42         mbr,
43         gpt,
44
45         efi,
46         fat,
47         freebsd,
48         extended,
49         part,
50         spare,
51         unused,
52
53         apple
54 } chunk_e;
55
56 __BEGIN_DECLS
57 #ifndef __ia64__
58 struct disk {
59         char            *name;
60         u_long          bios_cyl;
61         u_long          bios_hd;
62         u_long          bios_sect;
63 #ifdef PC98
64         u_char          *bootipl;
65         size_t          bootipl_size;
66         u_char          *bootmenu;
67         size_t          bootmenu_size;
68 #else
69         u_char          *bootmgr;
70         size_t          bootmgr_size;
71 #endif
72         u_char          *boot1;
73 #if defined(__i386__) || defined(__amd64__) /* the i386 needs extra help... */
74         u_char          *boot2;
75 #endif
76         struct chunk    *chunks;
77         u_long          sector_size; /* media sector size, a power of 2 */
78 };
79 #else   /* !__ia64__ */
80 struct disk {
81         char            *name;
82         struct chunk    *chunks;
83         u_long          media_size;
84         u_long          sector_size;
85         u_long          lba_start;
86         u_long          lba_end;
87         u_int           gpt_size;       /* Number of entries */
88 };
89 #endif
90
91 struct chunk {
92         struct chunk    *next;
93         struct chunk    *part;
94         struct disk     *disk;
95         daddr_t         offset;
96         daddr_t         size;
97         daddr_t         end;
98         char            *sname;         /* PC98 field */
99         char            *name;
100         char            *oname;
101         /* Used during Fixup_Names() to avoid renaming more than
102          * absolutely needed.
103          */
104         chunk_e         type;
105         int             subtype;
106         u_long          flags;
107         void            (*private_free)(void*);
108         void            *(*private_clone)(void*);
109         void            *private_data;
110         /* For data private to the application, and the management
111          * thereof.  If the functions are not provided, no storage
112          * management is done, Cloning will just copy the pointer
113          * and freeing will just forget it.
114          */
115 };
116
117 /*
118  * flags:
119  *
120  * ALIGN        -       This chunk should be aligned
121  * IS_ROOT      -       This 'part' is a rootfs, allocate 'a'
122  * ACTIVE       -       This is the active slice in the MBR
123  * FORCE_ALL    -       Force a dedicated disk for FreeBSD, bypassing
124  *                      all BIOS geometry considerations
125  * AUTO_SIZE    -       This chunk was auto-sized and can fill-out a
126  *                      following chunk if the following chunk is deleted.
127  * NEWFS        -       newfs pending, used to enable auto-resizing on
128  *                      delete (along with AUTO_SIZE).
129  */
130
131 #define CHUNK_ALIGN             0x0008
132 #define CHUNK_IS_ROOT           0x0010
133 #define CHUNK_ACTIVE            0x0020
134 #define CHUNK_FORCE_ALL         0x0040  
135 #define CHUNK_AUTO_SIZE         0x0080
136 #define CHUNK_NEWFS             0x0100
137 #define CHUNK_HAS_INDEX         0x0200
138 #define CHUNK_ITOF(i)           ((i & 0xFFFF) << 16)
139 #define CHUNK_FTOI(f)           ((f >> 16) & 0xFFFF)
140
141 #define DELCHUNK_NORMAL         0x0000
142 #define DELCHUNK_RECOVER        0x0001
143
144 const char *chunk_name(chunk_e);
145
146 const char *
147 slice_type_name(int, int);
148 /* "chunk_n" for subtypes too */
149
150 struct disk *
151 Open_Disk(const char *);
152 /* Will open the named disk, and return populated tree.  */
153
154 void
155 Free_Disk(struct disk *);
156 /* Free a tree made with Open_Disk() or Clone_Disk() */
157
158 void
159 Debug_Disk(struct disk *);
160 /* Print the content of the tree to stdout */
161
162 void
163 Set_Bios_Geom(struct disk *, u_long, u_long, u_long);
164 /* Set the geometry the bios uses. */
165
166 void
167 Sanitize_Bios_Geom(struct disk *);
168 /* Set the bios geometry to something sane */
169
170 int
171 Insert_Chunk(struct chunk *, daddr_t, daddr_t, const char *, chunk_e, int,
172         u_long, const char *);
173
174 int
175 Delete_Chunk2(struct disk *, struct chunk *, int);
176 /* Free a chunk of disk_space modified by the passed flags. */
177
178 int
179 Delete_Chunk(struct disk *, struct chunk *);
180 /* Free a chunk of disk_space */
181
182 void
183 Collapse_Disk(struct disk *);
184 /* Experimental, do not use. */
185
186 int
187 Collapse_Chunk(struct disk *, struct chunk *);
188 /* Experimental, do not use. */
189
190 int
191 Create_Chunk(struct disk *, daddr_t, daddr_t, chunk_e, int, u_long, const char *);
192 /* Create a chunk with the specified paramters */
193
194 void
195 All_FreeBSD(struct disk *, int);
196 /*
197  * Make one FreeBSD chunk covering the entire disk;
198  * if force_all is set, bypass all BIOS geometry
199  * considerations.
200  */
201
202 char *
203 CheckRules(const struct disk *);
204 /* Return char* to warnings about broken design rules in this disklayout */
205
206 char **
207 Disk_Names(void);
208 /*
209  * Return char** with all disk's names (wd0, wd1 ...).  You must free
210  * each pointer, as well as the array by hand
211  */
212
213 #ifdef PC98
214 void
215 Set_Boot_Mgr(struct disk *, const u_char *, const size_t, const u_char *,
216         const size_t);
217 #else
218 void
219 Set_Boot_Mgr(struct disk *, const u_char *, const size_t);
220 #endif
221 /*
222  * Use this boot-manager on this disk.  Gets written when Write_Disk()
223  * is called
224  */
225
226 int
227 Set_Boot_Blocks(struct disk *, const u_char *, const u_char *);
228 /*
229  * Use these boot-blocks on this disk.  Gets written when Write_Disk()
230  * is called. Returns nonzero upon failure.
231  */
232
233 int
234 Write_Disk(const struct disk *);
235 /* Write all the MBRs, disklabels, bootblocks and boot managers */
236
237 daddr_t
238 Next_Cyl_Aligned(const struct disk *, daddr_t);
239 /* Round offset up to next cylinder according to the bios-geometry */
240
241 daddr_t
242 Prev_Cyl_Aligned(const struct disk *, daddr_t);
243 /* Round offset down to previous cylinder according to the bios-geometry */
244
245 int
246 Track_Aligned(const struct disk *, daddr_t);
247 /* Check if offset is aligned on a track according to the bios geometry */
248
249 daddr_t
250 Next_Track_Aligned(const struct disk *, daddr_t);
251 /* Round offset up to next track according to the bios-geometry */
252
253 daddr_t
254 Prev_Track_Aligned(const struct disk *, daddr_t);
255 /* Check if offset is aligned on a track according to the bios geometry */
256
257 struct chunk *
258 Create_Chunk_DWIM(struct disk *, struct chunk *, daddr_t, chunk_e, int,
259         u_long);
260 /*
261  * This one creates a partition inside the given parent of the given
262  * size, and returns a pointer to it.  The first unused chunk big
263  * enough is used.
264  */
265
266 char *
267 ShowChunkFlags(struct chunk *);
268 /* Return string to show flags. */
269
270 /*
271  * Implementation details  >>> DO NOT USE <<<
272  */
273
274 struct disklabel;
275
276 void Fill_Disklabel(struct disklabel *, const struct disk *,
277         const struct chunk *);
278 void Debug_Chunk(struct chunk *);
279 struct chunk *New_Chunk(void);
280 void Free_Chunk(struct chunk *);
281 struct chunk *Clone_Chunk(const struct chunk *);
282 int Add_Chunk(struct disk *, daddr_t, daddr_t, const char *, chunk_e, int,
283         u_long, const char *);
284 void *read_block(int, daddr_t, u_long);
285 int write_block(int, daddr_t, const void *, u_long);
286 struct disklabel *read_disklabel(int, daddr_t, u_long);
287 struct disk *Int_Open_Disk(const char *, char *);
288 int Fixup_Names(struct disk *);
289 int MakeDevChunk(const struct chunk *, const char *);
290 __END_DECLS
291
292 #define dprintf printf
293
294 /* TODO
295  *
296  * Need an error string mechanism from the functions instead of warn()
297  *
298  * Make sure only FreeBSD start at offset==0
299  *
300  * Collapse must align.
301  *
302  * Make Write_Disk(struct disk*)
303  *
304  * Consider booting from OnTrack'ed disks.
305  *
306  * Get Bios-geom, ST506 & OnTrack from driver (or otherwise)
307  *
308  * Make Create_DWIM().
309  *
310  * Make Is_Unchanged(struct disk *d1, struct chunk *c1)
311  *
312  * don't rename slices unless we have to
313  *
314  *Sample output from tst01:
315  *
316  * Debug_Disk(wd0)  flags=0  bios_geom=0/0/0
317  * >>        0x3d040          0    1411200    1411199 wd0      0 whole    0 0
318  * >>>>      0x3d080          0     960120     960119 wd0s1    3 freebsd  0 8
319  * >>>>>>    0x3d100          0      40960      40959 wd0s1a   5 part     0 0
320  * >>>>>>    0x3d180      40960     131072     172031 wd0s1b   5 part     0 0
321  * >>>>>>    0x3d1c0     172032     409600     581631 wd0s1e   5 part     0 0
322  * >>>>>>    0x3d200     581632     378488     960119 wd0s1f   5 part     0 0
323  * >>>>      0x3d140     960120       5670     965789 wd0s2    4 extended 0 8
324  * >>>>>>    0x3d2c0     960120         63     960182 -        6 unused   0 0
325  * >>>>>>    0x3d0c0     960183       5607     965789 wd0s5    2 fat      0 8
326  * >>>>      0x3d280     965790       1890     967679 wd0s3    1 foo      -2 8
327  * >>>>      0x3d300     967680     443520    1411199 wd0s4    3 freebsd  0 8
328  * >>>>>>    0x3d340     967680     443520    1411199 wd0s4a   5 part     0 0
329  *
330  * ^            ^           ^          ^          ^     ^      ^ ^        ^ ^
331  * level    chunkptr      start      size        end  name    type  subtype flags
332  *
333  * Underlying data structure:
334  *
335  *      Legend:
336  *              <struct chunk> --> part
337  *                      |
338  *                      v next
339  *
340  *      <wd0> --> <wd0s1> --> <wd0s1a>
341  *                   |           |
342  *                   |           v
343  *                   |        <wd0s1b>
344  *                   |           |
345  *                   |           v
346  *                   |        <wd0s1e>
347  *                   |           |
348  *                   |           v
349  *                   |        <wd0s1f>
350  *                   |
351  *                   v
352  *                <wd0s2> --> <unused>
353  *                   |           |
354  *                   |           v
355  *                   |        <wd0s5>
356  *                   |
357  *                   v
358  *                <wd0s3>
359  *                   |
360  *                   v
361  *                <wd0s4> --> <wd0s4a>
362  *
363  *
364  */