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