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