Deleted Added
full compact
disklabel.c (22993) disklabel.c (23668)
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95";
37#endif
38static const char rcsid[] =
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95";
37#endif
38static const char rcsid[] =
39 "$Id$";
39 "$Id: disklabel.c,v 1.5 1997/02/22 14:58:01 peter Exp $";
40#endif /* not lint */
41
42#include <sys/param.h>
43#define DKTYPENAMES
44#include <sys/disklabel.h>
40#endif /* not lint */
41
42#include <sys/param.h>
43#define DKTYPENAMES
44#include <sys/disklabel.h>
45#include <ufs/ufs/dinode.h>
45#include <ufs/ffs/fs.h>
46
47#include <errno.h>
48#include <fcntl.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53#include <ctype.h>
54
55static int gettype __P((char *, char **));
56
57struct disklabel *
58getdiskbyname(name)
59 const char *name;
60{
61 static struct disklabel disk;
62 register struct disklabel *dp = &disk;
63 register struct partition *pp;
64 char *buf;
65 char *db_array[2] = { _PATH_DISKTAB, 0 };
66 char *cp, *cq; /* can't be register */
67 char p, max, psize[3], pbsize[3],
68 pfsize[3], poffset[3], ptype[3];
69 u_int32_t *dx;
70
71 if (cgetent(&buf, db_array, (char *) name) < 0)
72 return NULL;
73
74 bzero((char *)&disk, sizeof(disk));
75 /*
76 * typename
77 */
78 cq = dp->d_typename;
79 cp = buf;
80 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
81 (*cq = *cp) && *cq != '|' && *cq != ':')
82 cq++, cp++;
83 *cq = '\0';
84 /*
85 * boot name (optional) xxboot, bootxx
86 */
87 cgetstr(buf, "b0", &dp->d_boot0);
88 cgetstr(buf, "b1", &dp->d_boot1);
89
90 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
91 dp->d_flags |= D_REMOVABLE;
92 else if (cq && strcmp(cq, "simulated") == 0)
93 dp->d_flags |= D_RAMDISK;
94 if (cgetcap(buf, "sf", ':') != NULL)
95 dp->d_flags |= D_BADSECT;
96
97#define getnumdflt(field, dname, dflt) \
98 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
99
100 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
101 cgetnum(buf, "nt",(long *) &dp->d_ntracks);
102 cgetnum(buf, "ns",(long *) &dp->d_nsectors);
103 cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
104
105 if (cgetstr(buf, "dt", &cq) > 0)
106 dp->d_type = gettype(cq, dktypenames);
107 else
108 getnumdflt(dp->d_type, "dt", 0);
109 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
110 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
111 getnumdflt(dp->d_rpm, "rm", 3600);
112 getnumdflt(dp->d_interleave, "il", 1);
113 getnumdflt(dp->d_trackskew, "sk", 0);
114 getnumdflt(dp->d_cylskew, "cs", 0);
115 getnumdflt(dp->d_headswitch, "hs", 0);
116 getnumdflt(dp->d_trkseek, "ts", 0);
117 getnumdflt(dp->d_bbsize, "bs", BBSIZE);
118 getnumdflt(dp->d_sbsize, "sb", SBSIZE);
119 strcpy(psize, "px");
120 strcpy(pbsize, "bx");
121 strcpy(pfsize, "fx");
122 strcpy(poffset, "ox");
123 strcpy(ptype, "tx");
124 max = 'a' - 1;
125 pp = &dp->d_partitions[0];
126 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
127 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
128 if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
129 pp->p_size = 0;
130 else {
131 cgetnum(buf, poffset, (long *) &pp->p_offset);
132 getnumdflt(pp->p_fsize, pfsize, 0);
133 if (pp->p_fsize) {
134 long bsize;
135
136 if (cgetnum(buf, pbsize, &bsize) == 0)
137 pp->p_frag = bsize / pp->p_fsize;
138 else
139 pp->p_frag = 8;
140 }
141 getnumdflt(pp->p_fstype, ptype, 0);
142 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
143 pp->p_fstype = gettype(cq, fstypenames);
144 max = p;
145 }
146 }
147 dp->d_npartitions = max + 1 - 'a';
148 (void)strcpy(psize, "dx");
149 dx = dp->d_drivedata;
150 for (p = '0'; p < '0' + NDDATA; p++, dx++) {
151 psize[1] = p;
152 getnumdflt(*dx, psize, 0);
153 }
154 dp->d_magic = DISKMAGIC;
155 dp->d_magic2 = DISKMAGIC;
156 free(buf);
157 return (dp);
158}
159
160static int
161gettype(t, names)
162 char *t;
163 char **names;
164{
165 register char **nm;
166
167 for (nm = names; *nm; nm++)
168 if (strcasecmp(t, *nm) == 0)
169 return (nm - names);
170 if (isdigit(*t))
171 return (atoi(t));
172 return (0);
173}
46#include <ufs/ffs/fs.h>
47
48#include <errno.h>
49#include <fcntl.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54#include <ctype.h>
55
56static int gettype __P((char *, char **));
57
58struct disklabel *
59getdiskbyname(name)
60 const char *name;
61{
62 static struct disklabel disk;
63 register struct disklabel *dp = &disk;
64 register struct partition *pp;
65 char *buf;
66 char *db_array[2] = { _PATH_DISKTAB, 0 };
67 char *cp, *cq; /* can't be register */
68 char p, max, psize[3], pbsize[3],
69 pfsize[3], poffset[3], ptype[3];
70 u_int32_t *dx;
71
72 if (cgetent(&buf, db_array, (char *) name) < 0)
73 return NULL;
74
75 bzero((char *)&disk, sizeof(disk));
76 /*
77 * typename
78 */
79 cq = dp->d_typename;
80 cp = buf;
81 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
82 (*cq = *cp) && *cq != '|' && *cq != ':')
83 cq++, cp++;
84 *cq = '\0';
85 /*
86 * boot name (optional) xxboot, bootxx
87 */
88 cgetstr(buf, "b0", &dp->d_boot0);
89 cgetstr(buf, "b1", &dp->d_boot1);
90
91 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
92 dp->d_flags |= D_REMOVABLE;
93 else if (cq && strcmp(cq, "simulated") == 0)
94 dp->d_flags |= D_RAMDISK;
95 if (cgetcap(buf, "sf", ':') != NULL)
96 dp->d_flags |= D_BADSECT;
97
98#define getnumdflt(field, dname, dflt) \
99 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
100
101 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
102 cgetnum(buf, "nt",(long *) &dp->d_ntracks);
103 cgetnum(buf, "ns",(long *) &dp->d_nsectors);
104 cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
105
106 if (cgetstr(buf, "dt", &cq) > 0)
107 dp->d_type = gettype(cq, dktypenames);
108 else
109 getnumdflt(dp->d_type, "dt", 0);
110 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
111 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
112 getnumdflt(dp->d_rpm, "rm", 3600);
113 getnumdflt(dp->d_interleave, "il", 1);
114 getnumdflt(dp->d_trackskew, "sk", 0);
115 getnumdflt(dp->d_cylskew, "cs", 0);
116 getnumdflt(dp->d_headswitch, "hs", 0);
117 getnumdflt(dp->d_trkseek, "ts", 0);
118 getnumdflt(dp->d_bbsize, "bs", BBSIZE);
119 getnumdflt(dp->d_sbsize, "sb", SBSIZE);
120 strcpy(psize, "px");
121 strcpy(pbsize, "bx");
122 strcpy(pfsize, "fx");
123 strcpy(poffset, "ox");
124 strcpy(ptype, "tx");
125 max = 'a' - 1;
126 pp = &dp->d_partitions[0];
127 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
128 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
129 if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
130 pp->p_size = 0;
131 else {
132 cgetnum(buf, poffset, (long *) &pp->p_offset);
133 getnumdflt(pp->p_fsize, pfsize, 0);
134 if (pp->p_fsize) {
135 long bsize;
136
137 if (cgetnum(buf, pbsize, &bsize) == 0)
138 pp->p_frag = bsize / pp->p_fsize;
139 else
140 pp->p_frag = 8;
141 }
142 getnumdflt(pp->p_fstype, ptype, 0);
143 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
144 pp->p_fstype = gettype(cq, fstypenames);
145 max = p;
146 }
147 }
148 dp->d_npartitions = max + 1 - 'a';
149 (void)strcpy(psize, "dx");
150 dx = dp->d_drivedata;
151 for (p = '0'; p < '0' + NDDATA; p++, dx++) {
152 psize[1] = p;
153 getnumdflt(*dx, psize, 0);
154 }
155 dp->d_magic = DISKMAGIC;
156 dp->d_magic2 = DISKMAGIC;
157 free(buf);
158 return (dp);
159}
160
161static int
162gettype(t, names)
163 char *t;
164 char **names;
165{
166 register char **nm;
167
168 for (nm = names; *nm; nm++)
169 if (strcasecmp(t, *nm) == 0)
170 return (nm - names);
171 if (isdigit(*t))
172 return (atoi(t));
173 return (0);
174}