Annotation of src/lib/libc/gen/disklabel.c, Revision 1.26
1.26 ! mycroft 1: /* $NetBSD: disklabel.c,v 1.25 1999/09/20 04:38:58 lukem Exp $ */
1.7 cgd 2:
1.1 cgd 3: /*
1.7 cgd 4: * Copyright (c) 1983, 1987, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.14 christos 36: #include <sys/cdefs.h>
1.9 cgd 37: #if defined(LIBC_SCCS) && !defined(lint)
1.7 cgd 38: #if 0
1.16 perry 39: static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95";
1.7 cgd 40: #else
1.26 ! mycroft 41: __RCSID("$NetBSD: disklabel.c,v 1.25 1999/09/20 04:38:58 lukem Exp $");
1.7 cgd 42: #endif
1.9 cgd 43: #endif /* LIBC_SCCS and not lint */
1.1 cgd 44:
1.15 jtc 45: #include "namespace.h"
1.1 cgd 46: #include <sys/param.h>
47: #define DKTYPENAMES
1.22 christos 48: #define FSTYPENAMES
1.1 cgd 49: #include <sys/disklabel.h>
1.16 perry 50: #include <ufs/ufs/dinode.h>
1.7 cgd 51: #include <ufs/ffs/fs.h>
52:
1.24 lukem 53: #include <assert.h>
1.10 jtc 54: #include <ctype.h>
1.7 cgd 55: #include <errno.h>
56: #include <fcntl.h>
1.1 cgd 57: #include <stdio.h>
1.7 cgd 58: #include <stdlib.h>
1.1 cgd 59: #include <string.h>
60: #include <unistd.h>
1.15 jtc 61:
1.23 abs 62: #include <disktab.h>
63:
1.15 jtc 64: #ifdef __weak_alias
1.26 ! mycroft 65: __weak_alias(getdiskbyname,_getdiskbyname)
1.15 jtc 66: #endif
1.1 cgd 67:
1.14 christos 68: #if 0
1.7 cgd 69: static void error __P((int));
1.14 christos 70: #endif
1.20 mycroft 71: static int gettype __P((char *, const char *const *));
1.1 cgd 72:
1.23 abs 73: static char *db_array[2] = { _PATH_DISKTAB, 0 };
74:
75: int
76: setdisktab(name)
77: char *name;
78: {
79: if (!name || !*name)
80: return -1;
81:
82: db_array[0] = name;
83: return 0;
84: }
85:
86:
1.1 cgd 87: struct disklabel *
88: getdiskbyname(name)
89: const char *name;
90: {
91: static struct disklabel disk;
1.17 perry 92: struct disklabel *dp = &disk;
93: struct partition *pp;
1.7 cgd 94: char *buf;
1.17 perry 95: char *cp, *cq; /* can't be */
1.1 cgd 96: char p, max, psize[3], pbsize[3],
97: pfsize[3], poffset[3], ptype[3];
1.8 cgd 98: u_int32_t *dx;
1.22 christos 99: long f;
1.7 cgd 100:
1.24 lukem 101: _DIAGASSERT(name != NULL);
102:
1.22 christos 103: if (cgetent(&buf, db_array, name) < 0)
1.7 cgd 104: return NULL;
1.1 cgd 105:
1.21 perry 106: memset(&disk, 0, sizeof(disk));
1.1 cgd 107: /*
108: * typename
109: */
110: cq = dp->d_typename;
111: cp = buf;
112: while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
113: (*cq = *cp) && *cq != '|' && *cq != ':')
114: cq++, cp++;
115: *cq = '\0';
116: /*
117: * boot name (optional) xxboot, bootxx
118: */
1.7 cgd 119: cgetstr(buf, "b0", &dp->d_boot0);
120: cgetstr(buf, "b1", &dp->d_boot1);
121:
122: if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
1.1 cgd 123: dp->d_flags |= D_REMOVABLE;
124: else if (cq && strcmp(cq, "simulated") == 0)
125: dp->d_flags |= D_RAMDISK;
1.7 cgd 126: if (cgetcap(buf, "sf", ':') != NULL)
1.1 cgd 127: dp->d_flags |= D_BADSECT;
128:
129: #define getnumdflt(field, dname, dflt) \
1.22 christos 130: (field) = (u_int32_t) ((cgetnum(buf, dname, &f) == -1) ? (dflt) : f)
1.11 cgd 131: #define getnum(field, dname) \
1.22 christos 132: if (cgetnum(buf, dname, &f) != -1) field = (u_int32_t)f
1.1 cgd 133:
134: getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
1.11 cgd 135: getnum(dp->d_ntracks, "nt");
136: getnum(dp->d_nsectors, "ns");
137: getnum(dp->d_ncylinders, "nc");
1.7 cgd 138:
139: if (cgetstr(buf, "dt", &cq) > 0)
1.1 cgd 140: dp->d_type = gettype(cq, dktypenames);
141: else
142: getnumdflt(dp->d_type, "dt", 0);
143: getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
144: getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
145: getnumdflt(dp->d_rpm, "rm", 3600);
146: getnumdflt(dp->d_interleave, "il", 1);
147: getnumdflt(dp->d_trackskew, "sk", 0);
148: getnumdflt(dp->d_cylskew, "cs", 0);
149: getnumdflt(dp->d_headswitch, "hs", 0);
150: getnumdflt(dp->d_trkseek, "ts", 0);
151: getnumdflt(dp->d_bbsize, "bs", BBSIZE);
152: getnumdflt(dp->d_sbsize, "sb", SBSIZE);
1.13 scottr 153: strcpy(psize, "px"); /* XXX: strcpy is safe */
154: strcpy(pbsize, "bx"); /* XXX: strcpy is safe */
155: strcpy(pfsize, "fx"); /* XXX: strcpy is safe */
156: strcpy(poffset, "ox"); /* XXX: strcpy is safe */
157: strcpy(ptype, "tx"); /* XXX: strcpy is safe */
1.1 cgd 158: max = 'a' - 1;
159: pp = &dp->d_partitions[0];
160: for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
1.22 christos 161: long ff;
1.11 cgd 162:
1.1 cgd 163: psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
1.22 christos 164: if (cgetnum(buf, psize, &ff) == -1)
1.1 cgd 165: pp->p_size = 0;
166: else {
1.22 christos 167: pp->p_size = (u_int32_t)ff;
1.11 cgd 168: getnum(pp->p_offset, poffset);
1.1 cgd 169: getnumdflt(pp->p_fsize, pfsize, 0);
1.7 cgd 170: if (pp->p_fsize) {
171: long bsize;
172:
1.22 christos 173: if (cgetnum(buf, pbsize, &bsize) == -1)
174: pp->p_frag = 8;
1.7 cgd 175: else
1.22 christos 176: pp->p_frag =
177: (u_int8_t)(bsize / pp->p_fsize);
1.7 cgd 178: }
1.1 cgd 179: getnumdflt(pp->p_fstype, ptype, 0);
1.7 cgd 180: if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
1.1 cgd 181: pp->p_fstype = gettype(cq, fstypenames);
182: max = p;
183: }
184: }
185: dp->d_npartitions = max + 1 - 'a';
1.13 scottr 186: strcpy(psize, "dx"); /* XXX: strcpy is safe */
1.1 cgd 187: dx = dp->d_drivedata;
188: for (p = '0'; p < '0' + NDDATA; p++, dx++) {
189: psize[1] = p;
190: getnumdflt(*dx, psize, 0);
191: }
192: dp->d_magic = DISKMAGIC;
193: dp->d_magic2 = DISKMAGIC;
1.7 cgd 194: free(buf);
1.1 cgd 195: return (dp);
196: }
197:
1.4 jtc 198: static int
1.1 cgd 199: gettype(t, names)
200: char *t;
1.20 mycroft 201: const char *const *names;
1.1 cgd 202: {
1.20 mycroft 203: const char *const *nm;
1.24 lukem 204:
205: _DIAGASSERT(t != NULL);
206: _DIAGASSERT(names != NULL);
1.1 cgd 207:
208: for (nm = names; *nm; nm++)
209: if (strcasecmp(t, *nm) == 0)
210: return (nm - names);
211: if (isdigit(*t))
212: return (atoi(t));
213: return (0);
214: }
215:
1.14 christos 216: #if 0
1.4 jtc 217: static void
1.1 cgd 218: error(err)
219: int err;
220: {
221: char *p;
222:
223: (void)write(STDERR_FILENO, "disktab: ", 9);
224: (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
1.7 cgd 225: (void)write(STDERR_FILENO, ": ", 2);
1.1 cgd 226: p = strerror(err);
227: (void)write(STDERR_FILENO, p, strlen(p));
228: (void)write(STDERR_FILENO, "\n", 1);
229: }
1.14 christos 230: #endif
CVSweb <webmaster@jp.NetBSD.org>