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
30 #include <sys/param.h> /* DEV_BSIZE */
31 #include <sys/types.h>
32 #define DKTYPENAMES
33 #include <sys/disklabel.h>
34 #include <ufs/ffs/fs.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <limits.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 static u_int gettype(char *, const char * const *);
46
47 struct disklabel *
getdiskbyname(const char * name)48 getdiskbyname(const char *name)
49 {
50 static struct disklabel disk;
51 struct disklabel *dp = &disk;
52 struct partition *pp;
53 char *buf;
54 char *db_array[2] = { _PATH_DISKTAB, 0 };
55 char *cp, *cq;
56 char p, max, psize[3], pbsize[3],
57 pfsize[3], poffset[3], ptype[3];
58
59 if (cgetent(&buf, db_array, (char *) name) < 0)
60 return NULL;
61
62 bzero((char *)&disk, sizeof(disk));
63 /*
64 * typename
65 */
66 cq = dp->d_typename;
67 cp = buf;
68 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
69 (*cq = *cp) && *cq != '|' && *cq != ':')
70 cq++, cp++;
71 *cq = '\0';
72
73 #define getnumdflt(field, dname, dflt) \
74 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
75 #define getnum(field, dname) \
76 { long f; cgetnum(buf, dname, &f); field = f; }
77
78 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
79 getnum(dp->d_ntracks, "nt");
80 getnum(dp->d_nsectors, "ns");
81 getnum(dp->d_ncylinders, "nc");
82
83 if (cgetstr(buf, "dt", &cq) > 0)
84 dp->d_type = (u_short)gettype(cq, dktypenames);
85 else
86 getnumdflt(dp->d_type, "dt", 0);
87 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
88 /* XXX */
89 dp->d_secperunith = 0;
90 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
91 strlcpy(psize, "px", sizeof psize);
92 strlcpy(pbsize, "bx", sizeof pbsize);
93 strlcpy(pfsize, "fx", sizeof pfsize);
94 strlcpy(poffset, "ox", sizeof poffset);
95 strlcpy(ptype, "tx", sizeof ptype);
96 max = 'a' - 1;
97 pp = &dp->d_partitions[0];
98 dp->d_version = 1;
99 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
100 long f;
101
102 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
103 /* XXX */
104 if (cgetnum(buf, psize, &f) == -1)
105 DL_SETPSIZE(pp, 0);
106 else {
107 u_int32_t fsize, frag = 8;
108
109 DL_SETPSIZE(pp, f);
110 /* XXX */
111 pp->p_offseth = 0;
112 getnum(pp->p_offset, poffset);
113 getnumdflt(fsize, pfsize, 0);
114 if (fsize) {
115 long bsize;
116
117 if (cgetnum(buf, pbsize, &bsize) == 0)
118 frag = bsize / fsize;
119 pp->p_fragblock =
120 DISKLABELV1_FFS_FRAGBLOCK(fsize, frag);
121 }
122 getnumdflt(pp->p_fstype, ptype, 0);
123 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
124 pp->p_fstype = (u_char)gettype(cq, fstypenames);
125 max = p;
126 }
127 }
128 dp->d_npartitions = max + 1 - 'a';
129 dp->d_magic = DISKMAGIC;
130 dp->d_magic2 = DISKMAGIC;
131 free(buf);
132 return (dp);
133 }
134
135 static u_int
gettype(char * t,const char * const * names)136 gettype(char *t, const char * const *names)
137 {
138 const char * const *nm;
139
140 for (nm = names; *nm; nm++)
141 if (strcasecmp(t, *nm) == 0)
142 return (nm - names);
143 if (isdigit((u_char)*t))
144 return ((u_int)strtonum(t, 0, USHRT_MAX, NULL));
145 return (0);
146 }
147