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