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