Home | History | Annotate | Line # | Download | only in gen
disklabel.c revision 1.13
      1 /*	$NetBSD: disklabel.c,v 1.13 1997/02/14 08:30:32 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1987, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      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 
     36 #if defined(LIBC_SCCS) && !defined(lint)
     37 #if 0
     38 static char sccsid[] = "@(#)disklabel.c	8.1 (Berkeley) 6/4/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: disklabel.c,v 1.13 1997/02/14 08:30:32 scottr Exp $";
     41 #endif
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #include <sys/param.h>
     45 #define DKTYPENAMES
     46 #include <sys/disklabel.h>
     47 #include <ufs/ffs/fs.h>
     48 
     49 #include <ctype.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 static void	error __P((int));
     58 static int	gettype __P((char *, char **));
     59 
     60 struct disklabel *
     61 getdiskbyname(name)
     62 	const char *name;
     63 {
     64 	static struct	disklabel disk;
     65 	register struct	disklabel *dp = &disk;
     66 	register struct partition *pp;
     67 	char	*buf;
     68 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
     69 	char	*cp, *cq;	/* can't be register */
     70 	char	p, max, psize[3], pbsize[3],
     71 		pfsize[3], poffset[3], ptype[3];
     72 	u_int32_t *dx;
     73 
     74 	if (cgetent(&buf, db_array, (char *) name) < 0)
     75 		return NULL;
     76 
     77 	bzero((char *)&disk, sizeof(disk));
     78 	/*
     79 	 * typename
     80 	 */
     81 	cq = dp->d_typename;
     82 	cp = buf;
     83 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
     84 	    (*cq = *cp) && *cq != '|' && *cq != ':')
     85 		cq++, cp++;
     86 	*cq = '\0';
     87 	/*
     88 	 * boot name (optional)  xxboot, bootxx
     89 	 */
     90 	cgetstr(buf, "b0", &dp->d_boot0);
     91 	cgetstr(buf, "b1", &dp->d_boot1);
     92 
     93 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
     94 		dp->d_flags |= D_REMOVABLE;
     95 	else  if (cq && strcmp(cq, "simulated") == 0)
     96 		dp->d_flags |= D_RAMDISK;
     97 	if (cgetcap(buf, "sf", ':') != NULL)
     98 		dp->d_flags |= D_BADSECT;
     99 
    100 #define getnumdflt(field, dname, dflt) \
    101         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
    102 #define	getnum(field, dname) \
    103 	{ long f; cgetnum(buf, dname, &f); field = f; }
    104 
    105 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
    106 	getnum(dp->d_ntracks, "nt");
    107 	getnum(dp->d_nsectors, "ns");
    108 	getnum(dp->d_ncylinders, "nc");
    109 
    110 	if (cgetstr(buf, "dt", &cq) > 0)
    111 		dp->d_type = gettype(cq, dktypenames);
    112 	else
    113 		getnumdflt(dp->d_type, "dt", 0);
    114 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
    115 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
    116 	getnumdflt(dp->d_rpm, "rm", 3600);
    117 	getnumdflt(dp->d_interleave, "il", 1);
    118 	getnumdflt(dp->d_trackskew, "sk", 0);
    119 	getnumdflt(dp->d_cylskew, "cs", 0);
    120 	getnumdflt(dp->d_headswitch, "hs", 0);
    121 	getnumdflt(dp->d_trkseek, "ts", 0);
    122 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
    123 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
    124 	strcpy(psize, "px");	/* XXX: strcpy is safe */
    125 	strcpy(pbsize, "bx");	/* XXX: strcpy is safe */
    126 	strcpy(pfsize, "fx");	/* XXX: strcpy is safe */
    127 	strcpy(poffset, "ox");	/* XXX: strcpy is safe */
    128 	strcpy(ptype, "tx");	/* XXX: strcpy is safe */
    129 	max = 'a' - 1;
    130 	pp = &dp->d_partitions[0];
    131 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
    132 		long f;
    133 
    134 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
    135 		if (cgetnum(buf, psize, &f) == -1)
    136 			pp->p_size = 0;
    137 		else {
    138 			pp->p_size = f;
    139 			getnum(pp->p_offset, poffset);
    140 			getnumdflt(pp->p_fsize, pfsize, 0);
    141 			if (pp->p_fsize) {
    142 				long bsize;
    143 
    144 				if (cgetnum(buf, pbsize, &bsize) == 0)
    145 					pp->p_frag = bsize / pp->p_fsize;
    146 				else
    147 					pp->p_frag = 8;
    148 			}
    149 			getnumdflt(pp->p_fstype, ptype, 0);
    150 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
    151 				pp->p_fstype = gettype(cq, fstypenames);
    152 			max = p;
    153 		}
    154 	}
    155 	dp->d_npartitions = max + 1 - 'a';
    156 	strcpy(psize, "dx");	/* XXX: strcpy is safe */
    157 	dx = dp->d_drivedata;
    158 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
    159 		psize[1] = p;
    160 		getnumdflt(*dx, psize, 0);
    161 	}
    162 	dp->d_magic = DISKMAGIC;
    163 	dp->d_magic2 = DISKMAGIC;
    164 	free(buf);
    165 	return (dp);
    166 }
    167 
    168 static int
    169 gettype(t, names)
    170 	char *t;
    171 	char **names;
    172 {
    173 	register char **nm;
    174 
    175 	for (nm = names; *nm; nm++)
    176 		if (strcasecmp(t, *nm) == 0)
    177 			return (nm - names);
    178 	if (isdigit(*t))
    179 		return (atoi(t));
    180 	return (0);
    181 }
    182 
    183 static void
    184 error(err)
    185 	int err;
    186 {
    187 	char *p;
    188 
    189 	(void)write(STDERR_FILENO, "disktab: ", 9);
    190 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
    191 	(void)write(STDERR_FILENO, ": ", 2);
    192 	p = strerror(err);
    193 	(void)write(STDERR_FILENO, p, strlen(p));
    194 	(void)write(STDERR_FILENO, "\n", 1);
    195 }
    196