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