]> git.saurik.com Git - apple/libc.git/blame - gen/disklabel.c
Libc-320.tar.gz
[apple/libc.git] / gen / disklabel.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
e9ce8d39 7 *
734aad71
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
e9ce8d39
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
e9ce8d39
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1983, 1987, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58
59#include <sys/param.h>
60#include <sys/time.h>
61#define DKTYPENAMES
62#include <sys/disklabel.h>
63#include <ufs/ufs/dinode.h>
64#include <ufs/ffs/fs.h>
65
66#include <errno.h>
67#include <fcntl.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <unistd.h>
72#include <ctype.h>
73
9385eb3d
A
74#ifdef unused
75static int error(int);
76#endif // unused
77static int gettype(char *, char **);
e9ce8d39
A
78
79struct disklabel *
80getdiskbyname(name)
81 const char *name;
82{
83 static struct disklabel disk;
84 register struct disklabel *dp = &disk;
85 register struct partition *pp;
86 char *buf;
87 char *db_array[2] = { _PATH_DISKTAB, 0 };
88 char *cp, *cq; /* can't be register */
89 char p, max, psize[3], pbsize[3],
90 pfsize[3], poffset[3], ptype[3];
91 u_int32_t *dx;
92
93 if (cgetent(&buf, db_array, (char *) name) < 0)
94 return NULL;
95
96 bzero((char *)&disk, sizeof(disk));
97 /*
98 * typename
99 */
100 cq = dp->d_typename;
101 cp = buf;
102 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
103 (*cq = *cp) && *cq != '|' && *cq != ':')
104 cq++, cp++;
105 *cq = '\0';
106 /*
107 * boot name (optional) xxboot, bootxx
108 */
109 cgetstr(buf, "b0", &dp->d_boot0);
110 cgetstr(buf, "b1", &dp->d_boot1);
111
112 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
113 dp->d_flags |= D_REMOVABLE;
114 else if (cq && strcmp(cq, "simulated") == 0)
115 dp->d_flags |= D_RAMDISK;
116 if (cgetcap(buf, "sf", ':') != NULL)
117 dp->d_flags |= D_BADSECT;
118
119#define getnumdflt(field, dname, dflt) \
120 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
121
122 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
123 cgetnum(buf, "nt",(long *) &dp->d_ntracks);
124 cgetnum(buf, "ns",(long *) &dp->d_nsectors);
125 cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
126
127 if (cgetstr(buf, "dt", &cq) > 0)
128 dp->d_type = gettype(cq, dktypenames);
129 else
130 getnumdflt(dp->d_type, "dt", 0);
131 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
132 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
133 getnumdflt(dp->d_rpm, "rm", 3600);
134 getnumdflt(dp->d_interleave, "il", 1);
135 getnumdflt(dp->d_trackskew, "sk", 0);
136 getnumdflt(dp->d_cylskew, "cs", 0);
137 getnumdflt(dp->d_headswitch, "hs", 0);
138 getnumdflt(dp->d_trkseek, "ts", 0);
139 getnumdflt(dp->d_bbsize, "bs", BBSIZE);
140 getnumdflt(dp->d_sbsize, "sb", SBSIZE);
141 strcpy(psize, "px");
142 strcpy(pbsize, "bx");
143 strcpy(pfsize, "fx");
144 strcpy(poffset, "ox");
145 strcpy(ptype, "tx");
146 max = 'a' - 1;
147 pp = &dp->d_partitions[0];
148 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
149 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
150 if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
151 pp->p_size = 0;
152 else {
153 cgetnum(buf, poffset, (long *) &pp->p_offset);
154 getnumdflt(pp->p_fsize, pfsize, 0);
155 if (pp->p_fsize) {
156 long bsize;
157
158 if (cgetnum(buf, pbsize, &bsize) == 0)
159 pp->p_frag = bsize / pp->p_fsize;
160 else
161 pp->p_frag = 8;
162 }
163 getnumdflt(pp->p_fstype, ptype, 0);
164 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
165 pp->p_fstype = gettype(cq, fstypenames);
166 max = p;
167 }
168 }
169 dp->d_npartitions = max + 1 - 'a';
170 (void)strcpy(psize, "dx");
171 dx = dp->d_drivedata;
172 for (p = '0'; p < '0' + NDDATA; p++, dx++) {
173 psize[1] = p;
174 getnumdflt(*dx, psize, 0);
175 }
176 dp->d_magic = DISKMAGIC;
177 dp->d_magic2 = DISKMAGIC;
178 free(buf);
179 return (dp);
180}
181
182static int
183gettype(t, names)
184 char *t;
185 char **names;
186{
187 register char **nm;
188
189 for (nm = names; *nm; nm++)
190 if (strcasecmp(t, *nm) == 0)
191 return (nm - names);
192 if (isdigit(*t))
193 return (atoi(t));
194 return (0);
195}
196
9385eb3d 197#ifdef unused
e9ce8d39
A
198static int
199error(err)
200 int err;
201{
202 char *p;
203
204 (void)write(STDERR_FILENO, "disktab: ", 9);
205 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
206 (void)write(STDERR_FILENO, ": ", 2);
207 p = strerror(err);
208 (void)write(STDERR_FILENO, p, strlen(p));
209 (void)write(STDERR_FILENO, "\n", 1);
210}
9385eb3d 211#endif // unused