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