Merge branch 'vendor/BZIP'
[dragonfly.git] / lib / libc / gen / disklabel.c
1 /*
2  * Copyright (c) 1983, 1987, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)disklabel.c      8.2 (Berkeley) 5/3/95
30  * $FreeBSD: src/lib/libc/gen/disklabel.c,v 1.9.2.1 2001/03/05 08:40:47 obrien Exp $
31  * $DragonFly: src/lib/libc/gen/disklabel.c,v 1.13 2007/06/18 05:13:34 dillon Exp $
32  */
33
34 #include <sys/param.h>
35 #define DKTYPENAMES
36 #include <sys/disklabel32.h>
37 #include <sys/dtype.h>
38 #include <vfs/ufs/dinode.h>
39 #include <vfs/ufs/fs.h>
40
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <ctype.h>
47 #include <disktab.h>
48
49 static int      gettype (const char *, const char **);
50
51 struct disklabel32 *
52 getdiskbyname(const char *name)
53 {
54         static struct disklabel32 disk;
55         struct  disklabel32 *dp = &disk;
56         struct partition32 *pp;
57         char    *buf;
58         char    *db_array[2] = { _PATH_DISKTAB, 0 };
59         char    *cp, *cq;       /* can't be register */
60         char    p, max, psize[3], pbsize[3],
61                 pfsize[3], poffset[3], ptype[3];
62         u_int32_t *dx;
63
64         if (cgetent(&buf, db_array, (char *)name) < 0)
65                 return NULL;
66
67         bzero((char *)&disk, sizeof(disk));
68         /*
69          * typename
70          */
71         cq = dp->d_typename;
72         cp = buf;
73         while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
74             (*cq = *cp) && *cq != '|' && *cq != ':')
75                 cq++, cp++;
76         *cq = '\0';
77
78 #define getnumdflt(field, dname, dflt) \
79         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
80
81         getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
82         getnumdflt(dp->d_ntracks, "nt", 0);
83         getnumdflt(dp->d_nsectors, "ns", 0);
84         getnumdflt(dp->d_ncylinders, "nc", 0);
85
86         if (cgetstr(buf, "dt", &cq) > 0)
87                 dp->d_type = gettype(cq, dktypenames);
88         else
89                 getnumdflt(dp->d_type, "dt", 0);
90         getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
91         getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
92         getnumdflt(dp->d_rpm, "rm", 3600);
93         getnumdflt(dp->d_interleave, "il", 1);
94         getnumdflt(dp->d_trackskew, "sk", 0);
95         getnumdflt(dp->d_cylskew, "cs", 0);
96         getnumdflt(dp->d_headswitch, "hs", 0);
97         getnumdflt(dp->d_trkseek, "ts", 0);
98         getnumdflt(dp->d_bbsize, "bs", BBSIZE);
99         getnumdflt(dp->d_sbsize, "sb", SBSIZE);
100         strcpy(psize, "px");
101         strcpy(pbsize, "bx");
102         strcpy(pfsize, "fx");
103         strcpy(poffset, "ox");
104         strcpy(ptype, "tx");
105         max = 'a' - 1;
106         pp = &dp->d_partitions[0];
107         for (p = 'a'; p < 'a' + MAXPARTITIONS32; p++, pp++) {
108                 long l;
109                 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
110                 if (cgetnum(buf, psize, &l) == -1)
111                         pp->p_size = 0;
112                 else {
113                         pp->p_size = l;
114                         cgetnum(buf, poffset, &l);
115                         pp->p_offset = l;
116                         getnumdflt(pp->p_fsize, pfsize, 0);
117                         if (pp->p_fsize) {
118                                 long bsize;
119
120                                 if (cgetnum(buf, pbsize, &bsize) == 0)
121                                         pp->p_frag = bsize / pp->p_fsize;
122                                 else
123                                         pp->p_frag = 8;
124                         }
125                         getnumdflt(pp->p_fstype, ptype, 0);
126                         if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
127                                 pp->p_fstype = gettype(cq, fstypenames);
128                         max = p;
129                 }
130         }
131         dp->d_npartitions = max + 1 - 'a';
132         strcpy(psize, "dx");
133         dx = dp->d_drivedata;
134         for (p = '0'; p < '0' + NDDATA32; p++, dx++) {
135                 psize[1] = p;
136                 getnumdflt(*dx, psize, 0);
137         }
138         dp->d_magic = DISKMAGIC32;
139         dp->d_magic2 = DISKMAGIC32;
140         free(buf);
141         return (dp);
142 }
143
144 static int
145 gettype(const char *t, const char **names)
146 {
147         const char **nm;
148
149         for (nm = names; *nm; nm++)
150                 if (strcasecmp(t, *nm) == 0)
151                         return (nm - names);
152         if (isdigit((unsigned char)*t))
153                 return (atoi(t));
154         return (0);
155 }