1 /* $OpenBSD: disklabel.h,v 1.39 2007/07/11 04:50:43 miod Exp $ */
2 /* $NetBSD: disklabel.h,v 1.41 1996/05/10 23:07:37 mark Exp $ */
3
4 /*
5 * Copyright (c) 1987, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)disklabel.h 8.2 (Berkeley) 7/10/94
33 */
34
35 /*
36 * Disk description table, see disktab(5)
37 */
38 #define _PATH_DISKTAB "/etc/disktab"
39 #define DISKTAB "/etc/disktab" /* deprecated */
40
41 /*
42 * Each disk has a label which includes information about the hardware
43 * disk geometry, filesystem partitions, and drive specific information.
44 * The location of the label, as well as the number of partitions the
45 * label can describe and the number of the "whole disk" (raw)
46 * paritition are machine dependent.
47 */
48 #include <machine/disklabel.h>
49
50 /*
51 * The absolute maximum number of disk partitions allowed.
52 * This is the maximum value of MAXPARTITIONS for which 'struct disklabel'
53 * is <= DEV_BSIZE bytes long. If MAXPARTITIONS is greater than this, beware.
54 */
55 #define MAXMAXPARTITIONS 22
56 #if MAXPARTITIONS > MAXMAXPARTITIONS
57 #warn beware: MAXPARTITIONS bigger than MAXMAXPARTITIONS
58 #endif
59
60 /*
61 * Translate between device numbers and major/disk unit/disk partition.
62 */
63 #define DISKUNIT(dev) (minor(dev) / MAXPARTITIONS)
64 #define DISKPART(dev) (minor(dev) % MAXPARTITIONS)
65 #define RAW_PART 2 /* 'c' partition */
66 #define DISKMINOR(unit, part) \
67 (((unit) * MAXPARTITIONS) + (part))
68 #define MAKEDISKDEV(maj, unit, part) \
69 (makedev((maj), DISKMINOR((unit), (part))))
70 #define DISKLABELDEV(dev) \
71 (MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART))
72
73 #define DISKMAGIC ((u_int32_t)0x82564557) /* The disk magic number */
74
75 #define MAXDISKSIZE 0x1fffffffffffLL /* 47 bits of reach */
76
77 #ifndef _LOCORE
78 struct disklabel {
79 u_int32_t d_magic; /* the magic number */
80 u_int16_t d_type; /* drive type */
81 u_int16_t d_subtype; /* controller/d_type specific */
82 char d_typename[16]; /* type name, e.g. "eagle" */
83
84 /*
85 * d_packname contains the pack identifier and is returned when
86 * the disklabel is read off the disk or in-core copy.
87 * d_boot0 and d_boot1 are the (optional) names of the
88 * primary (block 0) and secondary (block 1-15) bootstraps
89 * as found in /usr/mdec. These are returned when using
90 * getdiskbyname(3) to retrieve the values from /etc/disktab.
91 */
92 union {
93 char un_d_packname[16]; /* pack identifier */
94 struct {
95 char *un_d_boot0; /* primary bootstrap name */
96 char *un_d_boot1; /* secondary bootstrap name */
97 } un_b;
98 } d_un;
99 #define d_packname d_un.un_d_packname
100 #define d_boot0 d_un.un_b.un_d_boot0
101 #define d_boot1 d_un.un_b.un_d_boot1
102
103 /* disk geometry: */
104 u_int32_t d_secsize; /* # of bytes per sector */
105 u_int32_t d_nsectors; /* # of data sectors per track */
106 u_int32_t d_ntracks; /* # of tracks per cylinder */
107 u_int32_t d_ncylinders; /* # of data cylinders per unit */
108 u_int32_t d_secpercyl; /* # of data sectors per cylinder */
109 u_int32_t d_secperunit; /* # of data sectors per unit */
110
111 /*
112 * Spares (bad sector replacements) below are not counted in
113 * d_nsectors or d_secpercyl. Spare sectors are assumed to
114 * be physical sectors which occupy space at the end of each
115 * track and/or cylinder.
116 */
117 u_int16_t d_sparespertrack; /* # of spare sectors per track */
118 u_int16_t d_sparespercyl; /* # of spare sectors per cylinder */
119 /*
120 * Alternate cylinders include maintenance, replacement, configuration
121 * description areas, etc.
122 */
123 u_int32_t d_acylinders; /* # of alt. cylinders per unit */
124
125 /* hardware characteristics: */
126 /*
127 * d_interleave, d_trackskew and d_cylskew describe perturbations
128 * in the media format used to compensate for a slow controller.
129 * Interleave is physical sector interleave, set up by the
130 * formatter or controller when formatting. When interleaving is
131 * in use, logically adjacent sectors are not physically
132 * contiguous, but instead are separated by some number of
133 * sectors. It is specified as the ratio of physical sectors
134 * traversed per logical sector. Thus an interleave of 1:1
135 * implies contiguous layout, while 2:1 implies that logical
136 * sector 0 is separated by one sector from logical sector 1.
137 * d_trackskew is the offset of sector 0 on track N relative to
138 * sector 0 on track N-1 on the same cylinder. Finally, d_cylskew
139 * is the offset of sector 0 on cylinder N relative to sector 0
140 * on cylinder N-1.
141 */
142 u_int16_t d_rpm; /* rotational speed */
143 u_int16_t d_interleave; /* hardware sector interleave */
144 u_int16_t d_trackskew; /* sector 0 skew, per track */
145 u_int16_t d_cylskew; /* sector 0 skew, per cylinder */
146 u_int32_t d_headswitch; /* head switch time, usec */
147 u_int32_t d_trkseek; /* track-to-track seek, usec */
148 u_int32_t d_flags; /* generic flags */
149 #define NDDATA 5
150 u_int32_t d_drivedata[NDDATA]; /* drive-type specific information */
151 u_int16_t d_secperunith; /* # of data sectors (high part) */
152 u_int16_t d_version; /* version # (1=48 bit addressing) */
153 #define NSPARE 4
154 u_int32_t d_spare[NSPARE]; /* reserved for future use */
155 u_int32_t d_magic2; /* the magic number (again) */
156 u_int16_t d_checksum; /* xor of data incl. partitions */
157
158 /* filesystem and partition information: */
159 u_int16_t d_npartitions; /* number of partitions in following */
160 u_int32_t d_bbsize; /* size of boot area at sn0, bytes */
161 u_int32_t d_sbsize; /* max size of fs superblock, bytes */
162 struct partition { /* the partition table */
163 u_int32_t p_size; /* number of sectors in partition */
164 u_int32_t p_offset; /* starting sector */
165 u_int16_t p_offseth; /* starting sector (high part) */
166 u_int16_t p_sizeh; /* number of sectors (high part) */
167 u_int8_t p_fstype; /* filesystem type, see below */
168 u_int8_t p_fragblock; /* encoded filesystem frag/block */
169 u_int16_t p_cpg; /* UFS: FS cylinders per group */
170 } d_partitions[MAXPARTITIONS]; /* actually may be more */
171 };
172
173
174 struct __partitionv0 { /* the partition table */
175 u_int32_t p_size; /* number of sectors in partition */
176 u_int32_t p_offset; /* starting sector */
177 u_int32_t p_fsize; /* filesystem basic fragment size */
178 u_int8_t p_fstype; /* filesystem type, see below */
179 u_int8_t p_frag; /* filesystem fragments per block */
180 union {
181 u_int16_t cpg; /* UFS: FS cylinders per group */
182 u_int16_t sgs; /* LFS: FS segment shift */
183 } __partitionv0_u1;
184 };
185
186 #else /* _LOCORE */
187 /*
188 * offsets for asm boot files.
189 */
190 .set d_secsize,40
191 .set d_nsectors,44
192 .set d_ntracks,48
193 .set d_ncylinders,52
194 .set d_secpercyl,56
195 .set d_secperunit,60
196 .set d_end_,404 /* size of disk label */
197 #endif /* _LOCORE */
198
199
200 #define DISKLABELV1_FFS_FRAGBLOCK(fsize, frag) \
201 ((fsize) * (frag) == 0 ? 0 : \
202 (((ffs((fsize) * (frag)) - 13) << 3) | (ffs(frag))))
203
204 #define DISKLABELV1_FFS_BSIZE(i) ((i) == 0 ? 0 : (1 << (((i) >> 3) + 12)))
205 #define DISKLABELV1_FFS_FRAG(i) ((i) == 0 ? 0 : (1 << (((i) & 0x07) - 1)))
206 #define DISKLABELV1_FFS_FSIZE(i) (DISKLABELV1_FFS_FRAG(i) == 0 ? 0 : \
207 (DISKLABELV1_FFS_BSIZE(i) / DISKLABELV1_FFS_FRAG(i)))
208
209 #define DL_GETPSIZE(p) (((u_int64_t)(p)->p_sizeh << 32) + (p)->p_size)
210 #define DL_GETPOFFSET(p) (((u_int64_t)(p)->p_offseth << 32) + (p)->p_offset)
211 #define DL_GETDSIZE(d) (((u_int64_t)(d)->d_secperunith << 32) + \
212 (d)->d_secperunit)
213
214 #define DL_SETPSIZE(p, n) do { \
215 daddr64_t x = (n); \
216 (p)->p_sizeh = x >> 32; \
217 (p)->p_size = x; \
218 } while (0)
219 #define DL_SETPOFFSET(p, n) do { \
220 daddr64_t x = (n); \
221 (p)->p_offseth = x >> 32; \
222 (p)->p_offset = x; \
223 } while (0)
224 #define DL_SETDSIZE(d, n) do { \
225 daddr64_t x = (n); \
226 (d)->d_secperunith = x >> 32; \
227 (d)->d_secperunit = x; \
228 } while (0)
229
230 /* d_type values: */
231 #define DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */
232 #define DTYPE_MSCP 2 /* MSCP */
233 #define DTYPE_DEC 3 /* other DEC (rk, rl) */
234 #define DTYPE_SCSI 4 /* SCSI */
235 #define DTYPE_ESDI 5 /* ESDI interface */
236 #define DTYPE_ST506 6 /* ST506 etc. */
237 #define DTYPE_HPIB 7 /* CS/80 on HP-IB */
238 #define DTYPE_HPFL 8 /* HP Fiber-link */
239 #define DTYPE_FLOPPY 10 /* floppy */
240 #define DTYPE_CCD 11 /* concatenated disk device */
241 #define DTYPE_VND 12 /* vnode pseudo-disk */
242 #define DTYPE_ATAPI 13 /* ATAPI */
243 #define DTYPE_RAID 14 /* RAIDframe */
244
245 #ifdef DKTYPENAMES
246 static char *dktypenames[] = {
247 "unknown",
248 "SMD",
249 "MSCP",
250 "old DEC",
251 "SCSI",
252 "ESDI",
253 "ST506",
254 "HP-IB",
255 "HP-FL",
256 "type 9",
257 "floppy",
258 "ccd",
259 "vnd",
260 "ATAPI",
261 "RAID",
262 NULL
263 };
264 #define DKMAXTYPES (sizeof(dktypenames) / sizeof(dktypenames[0]) - 1)
265 #endif
266
267 /*
268 * Filesystem type and version.
269 * Used to interpret other filesystem-specific
270 * per-partition information.
271 */
272 #define FS_UNUSED 0 /* unused */
273 #define FS_SWAP 1 /* swap */
274 #define FS_V6 2 /* Sixth Edition */
275 #define FS_V7 3 /* Seventh Edition */
276 #define FS_SYSV 4 /* System V */
277 #define FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
278 #define FS_V8 6 /* Eighth Edition, 4K blocks */
279 #define FS_BSDFFS 7 /* 4.2BSD fast file system */
280 #define FS_MSDOS 8 /* MSDOS file system */
281 #define FS_BSDLFS 9 /* 4.4BSD log-structured file system */
282 #define FS_OTHER 10 /* in use, but unknown/unsupported */
283 #define FS_HPFS 11 /* OS/2 high-performance file system */
284 #define FS_ISO9660 12 /* ISO 9660, normally CD-ROM */
285 #define FS_BOOT 13 /* partition contains bootstrap */
286 #define FS_ADOS 14 /* AmigaDOS fast file system */
287 #define FS_HFS 15 /* Macintosh HFS */
288 #define FS_ADFS 16 /* Acorn Disk Filing System */
289 #define FS_EXT2FS 17 /* ext2fs */
290 #define FS_CCD 18 /* ccd component */
291 #define FS_RAID 19 /* RAIDframe */
292 #define FS_NTFS 20 /* Windows/NT file system */
293 #define FS_UDF 21 /* UDF (DVD) filesystem */
294
295 #ifdef DKTYPENAMES
296 static char *fstypenames[] = {
297 "unused",
298 "swap",
299 "Version6",
300 "Version7",
301 "SystemV",
302 "4.1BSD",
303 "Eighth-Edition",
304 "4.2BSD",
305 "MSDOS",
306 "4.4LFS",
307 "unknown",
308 "HPFS",
309 "ISO9660",
310 "boot",
311 "ADOS",
312 "HFS",
313 "ADFS",
314 "ext2fs",
315 "ccd",
316 "RAID",
317 "NTFS",
318 "UDF",
319 NULL
320 };
321
322 /* Similar to the above, but used for things like the mount command. */
323 static char *fstypesnames[] = {
324 "", /* 0 */
325 "", /* 1 */
326 "", /* 2 */
327 "", /* 3 */
328 "", /* 4 */
329 "", /* 5 */
330 "", /* 6 */
331 "ffs", /* 7 */
332 "msdos", /* 8 */
333 "lfs", /* 9 */
334 "", /* 10 */
335 "", /* 11 */
336 "cd9660", /* 12 */
337 "", /* 13 */
338 "ados", /* 14 */
339 "", /* 15 */
340 "", /* 16 */
341 "ext2fs", /* 17 */
342 "", /* 18 */
343 "", /* 19 */
344 "ntfs", /* 20 */
345 "udf", /* 21 */
346 NULL
347 };
348
349 #define FSMAXTYPES (sizeof(fstypenames) / sizeof(fstypenames[0]) - 1)
350 #endif
351
352 /*
353 * flags shared by various drives:
354 */
355 #define D_BADSECT 0x04 /* supports bad sector forw. */
356 #define D_VENDOR 0x08 /* vendor disklabel */
357
358 /*
359 * Drive data for SMD.
360 */
361 #define d_smdflags d_drivedata[0]
362 #define D_SSE 0x1 /* supports skip sectoring */
363 #define d_mindist d_drivedata[1]
364 #define d_maxdist d_drivedata[2]
365 #define d_sdist d_drivedata[3]
366
367 /*
368 * Drive data for ST506.
369 */
370 #define d_precompcyl d_drivedata[0]
371 #define d_gap3 d_drivedata[1] /* used only when formatting */
372
373 /*
374 * Drive data for SCSI.
375 */
376 #define d_blind d_drivedata[0]
377
378 #ifndef _LOCORE
379 /*
380 * Structure used to perform a format or other raw operation, returning
381 * data and/or register values. Register identification and format
382 * are device- and driver-dependent.
383 */
384 struct format_op {
385 char *df_buf;
386 int df_count; /* value-result */
387 daddr64_t df_startblk;
388 int df_reg[8]; /* result */
389 };
390
391 /*
392 * Structure used internally to retrieve information about a partition
393 * on a disk.
394 */
395 struct partinfo {
396 struct disklabel *disklab;
397 struct partition *part;
398 };
399
400 /* DOS partition table -- located at start of some disks. */
401 #define DOS_LABELSECTOR 1
402 #define DOSBBSECTOR 0 /* DOS boot block relative sector # */
403 #define DOSPARTOFF 446
404 #define DOSDISKOFF 444
405 #define NDOSPART 4
406 #define DOSACTIVE 0x80 /* active partition */
407
408 #define DOSMBR_SIGNATURE (0xaa55)
409 #define DOSMBR_SIGNATURE_OFF (0x1fe)
410
411 struct dos_partition {
412 u_int8_t dp_flag; /* bootstrap flags */
413 u_int8_t dp_shd; /* starting head */
414 u_int8_t dp_ssect; /* starting sector */
415 u_int8_t dp_scyl; /* starting cylinder */
416 u_int8_t dp_typ; /* partition type (see below) */
417 u_int8_t dp_ehd; /* end head */
418 u_int8_t dp_esect; /* end sector */
419 u_int8_t dp_ecyl; /* end cylinder */
420 u_int32_t dp_start; /* absolute starting sector number */
421 u_int32_t dp_size; /* partition size in sectors */
422 };
423
424 /* Isolate the relevant bits to get sector and cylinder. */
425 #define DPSECT(s) ((s) & 0x3f)
426 #define DPCYL(c, s) ((c) + (((s) & 0xc0) << 2))
427
428 /* Known DOS partition types. */
429 #define DOSPTYP_UNUSED 0x00 /* Unused partition */
430 #define DOSPTYP_FAT12 0x01 /* 12-bit FAT */
431 #define DOSPTYP_FAT16S 0x04 /* 16-bit FAT, less than 32M */
432 #define DOSPTYP_EXTEND 0x05 /* Extended; contains sub-partitions */
433 #define DOSPTYP_FAT16B 0x06 /* 16-bit FAT, more than 32M */
434 #define DOSPTYP_FAT32 0x0b /* 32-bit FAT */
435 #define DOSPTYP_FAT32L 0x0c /* 32-bit FAT, LBA-mapped */
436 #define DOSPTYP_FAT16L 0x0e /* 16-bit FAT, LBA-mapped */
437 #define DOSPTYP_EXTENDL 0x0f /* Extended, LBA-mapped; (sub-partitions) */
438 #define DOSPTYP_ONTRACK 0x54
439 #define DOSPTYP_LINUX 0x83 /* That other thing */
440 #define DOSPTYP_FREEBSD 0xa5 /* FreeBSD partition type */
441 #define DOSPTYP_OPENBSD 0xa6 /* OpenBSD partition type */
442 #define DOSPTYP_NETBSD 0xa9 /* NetBSD partition type */
443
444 struct dos_mbr {
445 u_int8_t dmbr_boot[DOSPARTOFF];
446 struct dos_partition dmbr_parts[NDOSPART];
447 u_int16_t dmbr_sign;
448 } __packed;
449
450 #ifdef _KERNEL
451 void diskerr(struct buf *, char *, char *, int, int, struct disklabel *);
452 void disksort(struct buf *, struct buf *);
453 u_int dkcksum(struct disklabel *);
454 char *initdisklabel(struct disklabel *);
455 char *checkdisklabel(void *, struct disklabel *);
456 int setdisklabel(struct disklabel *, struct disklabel *, u_int);
457 char *readdisklabel(dev_t, void (*)(struct buf *), struct disklabel *, int);
458 int writedisklabel(dev_t, void (*)(struct buf *), struct disklabel *);
459 int bounds_check_with_label(struct buf *, struct disklabel *, int);
460 char *readdoslabel(struct buf *, void (*)(struct buf *),
461 struct disklabel *, int *, int);
462 #ifdef CD9660
463 int iso_disklabelspoof(dev_t dev, void (*strat)(struct buf *),
464 struct disklabel *lp);
465 #endif
466 #ifdef UDF
467 int udf_disklabelspoof(dev_t dev, void (*strat)(struct buf *),
468 struct disklabel *lp);
469 #endif
470 #endif
471 #endif /* _LOCORE */
472
473 #if !defined(_KERNEL) && !defined(_LOCORE)
474
475 #include <sys/cdefs.h>
476
477 __BEGIN_DECLS
478 struct disklabel *getdiskbyname(const char *);
479 __END_DECLS
480
481 #endif