xref: /freebsd/lib/libc/gen/disklabel.c (revision cc0ea3465d0b6a60e7d2d7e3a528366f791051e0)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1983, 1987, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #ifndef lint
35cc0ea346SBruce Evans #if 0
36cc0ea346SBruce Evans static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
37cc0ea346SBruce Evans #endif
38cc0ea346SBruce Evans static const char rcsid[] =
39cc0ea346SBruce Evans 	"$Id$";
4058f0484fSRodney W. Grimes #endif /* not lint */
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <sys/param.h>
4358f0484fSRodney W. Grimes #define DKTYPENAMES
4458f0484fSRodney W. Grimes #include <sys/disklabel.h>
4558f0484fSRodney W. Grimes #include <ufs/ffs/fs.h>
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <errno.h>
4858f0484fSRodney W. Grimes #include <fcntl.h>
4958f0484fSRodney W. Grimes #include <stdio.h>
5058f0484fSRodney W. Grimes #include <stdlib.h>
5158f0484fSRodney W. Grimes #include <string.h>
5258f0484fSRodney W. Grimes #include <unistd.h>
538b102407SPoul-Henning Kamp #include <ctype.h>
5458f0484fSRodney W. Grimes 
5558f0484fSRodney W. Grimes static int	gettype __P((char *, char **));
5658f0484fSRodney W. Grimes 
5758f0484fSRodney W. Grimes struct disklabel *
5858f0484fSRodney W. Grimes getdiskbyname(name)
5958f0484fSRodney W. Grimes 	const char *name;
6058f0484fSRodney W. Grimes {
6158f0484fSRodney W. Grimes 	static struct	disklabel disk;
6258f0484fSRodney W. Grimes 	register struct	disklabel *dp = &disk;
6358f0484fSRodney W. Grimes 	register struct partition *pp;
6458f0484fSRodney W. Grimes 	char	*buf;
6558f0484fSRodney W. Grimes 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
6658f0484fSRodney W. Grimes 	char	*cp, *cq;	/* can't be register */
6758f0484fSRodney W. Grimes 	char	p, max, psize[3], pbsize[3],
6858f0484fSRodney W. Grimes 		pfsize[3], poffset[3], ptype[3];
69cc0ea346SBruce Evans 	u_int32_t *dx;
7058f0484fSRodney W. Grimes 
7158f0484fSRodney W. Grimes 	if (cgetent(&buf, db_array, (char *) name) < 0)
7258f0484fSRodney W. Grimes 		return NULL;
7358f0484fSRodney W. Grimes 
7458f0484fSRodney W. Grimes 	bzero((char *)&disk, sizeof(disk));
7558f0484fSRodney W. Grimes 	/*
7658f0484fSRodney W. Grimes 	 * typename
7758f0484fSRodney W. Grimes 	 */
7858f0484fSRodney W. Grimes 	cq = dp->d_typename;
7958f0484fSRodney W. Grimes 	cp = buf;
8058f0484fSRodney W. Grimes 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
8158f0484fSRodney W. Grimes 	    (*cq = *cp) && *cq != '|' && *cq != ':')
8258f0484fSRodney W. Grimes 		cq++, cp++;
8358f0484fSRodney W. Grimes 	*cq = '\0';
8458f0484fSRodney W. Grimes 	/*
8558f0484fSRodney W. Grimes 	 * boot name (optional)  xxboot, bootxx
8658f0484fSRodney W. Grimes 	 */
8758f0484fSRodney W. Grimes 	cgetstr(buf, "b0", &dp->d_boot0);
8858f0484fSRodney W. Grimes 	cgetstr(buf, "b1", &dp->d_boot1);
8958f0484fSRodney W. Grimes 
9058f0484fSRodney W. Grimes 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
9158f0484fSRodney W. Grimes 		dp->d_flags |= D_REMOVABLE;
9258f0484fSRodney W. Grimes 	else  if (cq && strcmp(cq, "simulated") == 0)
9358f0484fSRodney W. Grimes 		dp->d_flags |= D_RAMDISK;
9458f0484fSRodney W. Grimes 	if (cgetcap(buf, "sf", ':') != NULL)
9558f0484fSRodney W. Grimes 		dp->d_flags |= D_BADSECT;
9658f0484fSRodney W. Grimes 
9758f0484fSRodney W. Grimes #define getnumdflt(field, dname, dflt) \
9858f0484fSRodney W. Grimes         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
9958f0484fSRodney W. Grimes 
10058f0484fSRodney W. Grimes 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
10158f0484fSRodney W. Grimes 	cgetnum(buf, "nt",(long *) &dp->d_ntracks);
10258f0484fSRodney W. Grimes 	cgetnum(buf, "ns",(long *) &dp->d_nsectors);
10358f0484fSRodney W. Grimes 	cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
10458f0484fSRodney W. Grimes 
10558f0484fSRodney W. Grimes 	if (cgetstr(buf, "dt", &cq) > 0)
10658f0484fSRodney W. Grimes 		dp->d_type = gettype(cq, dktypenames);
10758f0484fSRodney W. Grimes 	else
10858f0484fSRodney W. Grimes 		getnumdflt(dp->d_type, "dt", 0);
10958f0484fSRodney W. Grimes 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
11058f0484fSRodney W. Grimes 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
11158f0484fSRodney W. Grimes 	getnumdflt(dp->d_rpm, "rm", 3600);
11258f0484fSRodney W. Grimes 	getnumdflt(dp->d_interleave, "il", 1);
11358f0484fSRodney W. Grimes 	getnumdflt(dp->d_trackskew, "sk", 0);
11458f0484fSRodney W. Grimes 	getnumdflt(dp->d_cylskew, "cs", 0);
11558f0484fSRodney W. Grimes 	getnumdflt(dp->d_headswitch, "hs", 0);
11658f0484fSRodney W. Grimes 	getnumdflt(dp->d_trkseek, "ts", 0);
11758f0484fSRodney W. Grimes 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
11858f0484fSRodney W. Grimes 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
11958f0484fSRodney W. Grimes 	strcpy(psize, "px");
12058f0484fSRodney W. Grimes 	strcpy(pbsize, "bx");
12158f0484fSRodney W. Grimes 	strcpy(pfsize, "fx");
12258f0484fSRodney W. Grimes 	strcpy(poffset, "ox");
12358f0484fSRodney W. Grimes 	strcpy(ptype, "tx");
12458f0484fSRodney W. Grimes 	max = 'a' - 1;
12558f0484fSRodney W. Grimes 	pp = &dp->d_partitions[0];
12658f0484fSRodney W. Grimes 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
12758f0484fSRodney W. Grimes 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
12858f0484fSRodney W. Grimes 		if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
12958f0484fSRodney W. Grimes 			pp->p_size = 0;
13058f0484fSRodney W. Grimes 		else {
13158f0484fSRodney W. Grimes 			cgetnum(buf, poffset, (long *) &pp->p_offset);
13258f0484fSRodney W. Grimes 			getnumdflt(pp->p_fsize, pfsize, 0);
13358f0484fSRodney W. Grimes 			if (pp->p_fsize) {
13458f0484fSRodney W. Grimes 				long bsize;
13558f0484fSRodney W. Grimes 
13658f0484fSRodney W. Grimes 				if (cgetnum(buf, pbsize, &bsize) == 0)
13758f0484fSRodney W. Grimes 					pp->p_frag = bsize / pp->p_fsize;
13858f0484fSRodney W. Grimes 				else
13958f0484fSRodney W. Grimes 					pp->p_frag = 8;
14058f0484fSRodney W. Grimes 			}
14158f0484fSRodney W. Grimes 			getnumdflt(pp->p_fstype, ptype, 0);
14258f0484fSRodney W. Grimes 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
14358f0484fSRodney W. Grimes 				pp->p_fstype = gettype(cq, fstypenames);
14458f0484fSRodney W. Grimes 			max = p;
14558f0484fSRodney W. Grimes 		}
14658f0484fSRodney W. Grimes 	}
14758f0484fSRodney W. Grimes 	dp->d_npartitions = max + 1 - 'a';
14858f0484fSRodney W. Grimes 	(void)strcpy(psize, "dx");
14958f0484fSRodney W. Grimes 	dx = dp->d_drivedata;
15058f0484fSRodney W. Grimes 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
15158f0484fSRodney W. Grimes 		psize[1] = p;
15258f0484fSRodney W. Grimes 		getnumdflt(*dx, psize, 0);
15358f0484fSRodney W. Grimes 	}
15458f0484fSRodney W. Grimes 	dp->d_magic = DISKMAGIC;
15558f0484fSRodney W. Grimes 	dp->d_magic2 = DISKMAGIC;
15658f0484fSRodney W. Grimes 	free(buf);
15758f0484fSRodney W. Grimes 	return (dp);
15858f0484fSRodney W. Grimes }
15958f0484fSRodney W. Grimes 
16058f0484fSRodney W. Grimes static int
16158f0484fSRodney W. Grimes gettype(t, names)
16258f0484fSRodney W. Grimes 	char *t;
16358f0484fSRodney W. Grimes 	char **names;
16458f0484fSRodney W. Grimes {
16558f0484fSRodney W. Grimes 	register char **nm;
16658f0484fSRodney W. Grimes 
16758f0484fSRodney W. Grimes 	for (nm = names; *nm; nm++)
16858f0484fSRodney W. Grimes 		if (strcasecmp(t, *nm) == 0)
16958f0484fSRodney W. Grimes 			return (nm - names);
17058f0484fSRodney W. Grimes 	if (isdigit(*t))
17158f0484fSRodney W. Grimes 		return (atoi(t));
17258f0484fSRodney W. Grimes 	return (0);
17358f0484fSRodney W. Grimes }
174