xref: /minix3/lib/libc/gen/disklabel.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: disklabel.c,v 1.37 2012/06/25 22:32:43 abs Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1983, 1987, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
332fe8fb19SBen Gras #include "nbtool_config.h"
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras 
362fe8fb19SBen Gras #include <sys/cdefs.h>
372fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
382fe8fb19SBen Gras #if 0
392fe8fb19SBen Gras static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
402fe8fb19SBen Gras #else
41*f14fb602SLionel Sambuc __RCSID("$NetBSD: disklabel.c,v 1.37 2012/06/25 22:32:43 abs Exp $");
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
442fe8fb19SBen Gras 
452fe8fb19SBen Gras #include "namespace.h"
462fe8fb19SBen Gras #include <sys/param.h>
472fe8fb19SBen Gras #define DKTYPENAMES
482fe8fb19SBen Gras #define FSTYPENAMES
492fe8fb19SBen Gras #include <ufs/ufs/dinode.h>
502fe8fb19SBen Gras #include <ufs/ffs/fs.h>
512fe8fb19SBen Gras 
522fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
532fe8fb19SBen Gras #include <nbinclude/sys/disklabel.h>
542fe8fb19SBen Gras #include <nbinclude/disktab.h>
552fe8fb19SBen Gras #else
562fe8fb19SBen Gras #include <sys/disklabel.h>
572fe8fb19SBen Gras #include <disktab.h>
582fe8fb19SBen Gras #endif /* HAVE_NBTOOL_CONFIG_H */
592fe8fb19SBen Gras 
602fe8fb19SBen Gras #include <assert.h>
612fe8fb19SBen Gras #include <ctype.h>
622fe8fb19SBen Gras #include <errno.h>
632fe8fb19SBen Gras #include <fcntl.h>
642fe8fb19SBen Gras #include <stdio.h>
652fe8fb19SBen Gras #include <stdlib.h>
662fe8fb19SBen Gras #include <string.h>
672fe8fb19SBen Gras #include <unistd.h>
682fe8fb19SBen Gras 
692fe8fb19SBen Gras #ifdef __weak_alias
702fe8fb19SBen Gras __weak_alias(getdiskbyname,_getdiskbyname)
712fe8fb19SBen Gras #endif
722fe8fb19SBen Gras 
732fe8fb19SBen Gras #if 0
74*f14fb602SLionel Sambuc static void	error(int);
752fe8fb19SBen Gras #endif
76*f14fb602SLionel Sambuc static int	gettype(char *, const char *const *);
772fe8fb19SBen Gras 
782fe8fb19SBen Gras static const char *db_array[2] = { _PATH_DISKTAB, 0 };
792fe8fb19SBen Gras 
802fe8fb19SBen Gras int
setdisktab(const char * name)81*f14fb602SLionel Sambuc setdisktab(const char *name)
822fe8fb19SBen Gras {
832fe8fb19SBen Gras 	if (!name || !*name)
842fe8fb19SBen Gras 		return -1;
852fe8fb19SBen Gras 
862fe8fb19SBen Gras 	db_array[0] = name;
872fe8fb19SBen Gras 	return 0;
882fe8fb19SBen Gras }
892fe8fb19SBen Gras 
902fe8fb19SBen Gras 
912fe8fb19SBen Gras struct disklabel *
getdiskbyname(const char * name)92*f14fb602SLionel Sambuc getdiskbyname(const char *name)
932fe8fb19SBen Gras {
942fe8fb19SBen Gras 	static struct	disklabel disk;
952fe8fb19SBen Gras 	struct	disklabel *dp = &disk;
962fe8fb19SBen Gras 	struct partition *pp;
972fe8fb19SBen Gras 	char	*buf;
982fe8fb19SBen Gras 	char	*cp, *cq;	/* can't be */
992fe8fb19SBen Gras 	char	p, max, psize[3], pbsize[3],
1002fe8fb19SBen Gras 		pfsize[3], poffset[3], ptype[3];
1012fe8fb19SBen Gras 	u_int32_t *dx;
1022fe8fb19SBen Gras 	long f;
1032fe8fb19SBen Gras 
1042fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras 	if (cgetent(&buf, db_array, name) < 0)
1072fe8fb19SBen Gras 		return NULL;
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras 	memset(&disk, 0, sizeof(disk));
1102fe8fb19SBen Gras 	/*
1112fe8fb19SBen Gras 	 * typename
1122fe8fb19SBen Gras 	 */
1132fe8fb19SBen Gras 	cq = dp->d_typename;
1142fe8fb19SBen Gras 	cp = buf;
1152fe8fb19SBen Gras 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
1162fe8fb19SBen Gras 	    (*cq = *cp) && *cq != '|' && *cq != ':')
1172fe8fb19SBen Gras 		cq++, cp++;
1182fe8fb19SBen Gras 	*cq = '\0';
1192fe8fb19SBen Gras 	/*
1202fe8fb19SBen Gras 	 * boot name (optional)  xxboot, bootxx
1212fe8fb19SBen Gras 	 */
1222fe8fb19SBen Gras 	cgetstr(buf, "b0", &dp->d_boot0);
1232fe8fb19SBen Gras 	cgetstr(buf, "b1", &dp->d_boot1);
1242fe8fb19SBen Gras 
1252fe8fb19SBen Gras 	if (cgetstr(buf, "ty", &cq) >= 0) {
1262fe8fb19SBen Gras 		if (strcmp(cq, "removable") == 0)
1272fe8fb19SBen Gras 			dp->d_flags |= D_REMOVABLE;
1282fe8fb19SBen Gras 		else if (strcmp(cq, "simulated") == 0)
1292fe8fb19SBen Gras 			dp->d_flags |= D_RAMDISK;
1302fe8fb19SBen Gras 		free(cq);
1312fe8fb19SBen Gras 	}
1322fe8fb19SBen Gras 	if (cgetcap(buf, "sf", ':') != NULL)
1332fe8fb19SBen Gras 		dp->d_flags |= D_BADSECT;
1342fe8fb19SBen Gras 
1352fe8fb19SBen Gras #define getnumdflt(field, dname, dflt) \
1362fe8fb19SBen Gras     (field) = ((cgetnum(buf, dname, &f) == -1) ? (dflt) : (u_int32_t) f)
1372fe8fb19SBen Gras #define	getnum(field, dname) \
1382fe8fb19SBen Gras 	if (cgetnum(buf, dname, &f) != -1) field = (u_int32_t)f
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
1412fe8fb19SBen Gras 	getnum(dp->d_ntracks, "nt");
1422fe8fb19SBen Gras 	getnum(dp->d_nsectors, "ns");
1432fe8fb19SBen Gras 	getnum(dp->d_ncylinders, "nc");
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras 	if (cgetstr(buf, "dt", &cq) >= 0) {
1462fe8fb19SBen Gras 		dp->d_type = gettype(cq, dktypenames);
1472fe8fb19SBen Gras 		free(cq);
1482fe8fb19SBen Gras 	} else
1492fe8fb19SBen Gras 		getnumdflt(dp->d_type, "dt", 0);
1502fe8fb19SBen Gras 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
1512fe8fb19SBen Gras 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
1522fe8fb19SBen Gras 	getnumdflt(dp->d_rpm, "rm", 3600);
1532fe8fb19SBen Gras 	getnumdflt(dp->d_interleave, "il", 1);
1542fe8fb19SBen Gras 	getnumdflt(dp->d_trackskew, "sk", 0);
1552fe8fb19SBen Gras 	getnumdflt(dp->d_cylskew, "cs", 0);
1562fe8fb19SBen Gras 	getnumdflt(dp->d_headswitch, "hs", 0);
1572fe8fb19SBen Gras 	getnumdflt(dp->d_trkseek, "ts", 0);
1582fe8fb19SBen Gras 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
1592fe8fb19SBen Gras 	getnumdflt(dp->d_sbsize, "sb", SBLOCKSIZE);
1602fe8fb19SBen Gras 	strcpy(psize, "px");	/* XXX: strcpy is safe */
1612fe8fb19SBen Gras 	strcpy(pbsize, "bx");	/* XXX: strcpy is safe */
1622fe8fb19SBen Gras 	strcpy(pfsize, "fx");	/* XXX: strcpy is safe */
1632fe8fb19SBen Gras 	strcpy(poffset, "ox");	/* XXX: strcpy is safe */
1642fe8fb19SBen Gras 	strcpy(ptype, "tx");	/* XXX: strcpy is safe */
1652fe8fb19SBen Gras 	max = 'a' - 1;
1662fe8fb19SBen Gras 	pp = &dp->d_partitions[0];
1672fe8fb19SBen Gras 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
1682fe8fb19SBen Gras 		long ff;
1692fe8fb19SBen Gras 
1702fe8fb19SBen Gras 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
1712fe8fb19SBen Gras 		if (cgetnum(buf, psize, &ff) == -1)
1722fe8fb19SBen Gras 			pp->p_size = 0;
1732fe8fb19SBen Gras 		else {
1742fe8fb19SBen Gras 			pp->p_size = (u_int32_t)ff;
1752fe8fb19SBen Gras 			getnum(pp->p_offset, poffset);
1762fe8fb19SBen Gras 			getnumdflt(pp->p_fsize, pfsize, 0);
1772fe8fb19SBen Gras 			if (pp->p_fsize) {
1782fe8fb19SBen Gras 				long bsize;
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras 				if (cgetnum(buf, pbsize, &bsize) == -1)
1812fe8fb19SBen Gras 					pp->p_frag = 8;
1822fe8fb19SBen Gras 				else
1832fe8fb19SBen Gras 					pp->p_frag =
1842fe8fb19SBen Gras 					    (u_int8_t)(bsize / pp->p_fsize);
1852fe8fb19SBen Gras 			}
1862fe8fb19SBen Gras 			getnumdflt(pp->p_fstype, ptype, 0);
1872fe8fb19SBen Gras 			if (pp->p_fstype == 0)
1882fe8fb19SBen Gras 				if (cgetstr(buf, ptype, &cq) >= 0) {
1892fe8fb19SBen Gras 					pp->p_fstype = gettype(cq, fstypenames);
1902fe8fb19SBen Gras 					free(cq);
1912fe8fb19SBen Gras 				}
1922fe8fb19SBen Gras 			max = p;
1932fe8fb19SBen Gras 		}
1942fe8fb19SBen Gras 	}
1952fe8fb19SBen Gras 	dp->d_npartitions = max + 1 - 'a';
1962fe8fb19SBen Gras 	strcpy(psize, "dx");	/* XXX: strcpy is safe */
1972fe8fb19SBen Gras 	dx = dp->d_drivedata;
1982fe8fb19SBen Gras 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
1992fe8fb19SBen Gras 		psize[1] = p;
2002fe8fb19SBen Gras 		getnumdflt(*dx, psize, 0);
2012fe8fb19SBen Gras 	}
2022fe8fb19SBen Gras 	dp->d_magic = DISKMAGIC;
2032fe8fb19SBen Gras 	dp->d_magic2 = DISKMAGIC;
2042fe8fb19SBen Gras 	free(buf);
2052fe8fb19SBen Gras 	return (dp);
2062fe8fb19SBen Gras }
2072fe8fb19SBen Gras 
2082fe8fb19SBen Gras static int
gettype(char * t,const char * const * names)209*f14fb602SLionel Sambuc gettype(char *t, const char *const *names)
2102fe8fb19SBen Gras {
2112fe8fb19SBen Gras 	const char *const *nm;
2122fe8fb19SBen Gras 
2132fe8fb19SBen Gras 	_DIAGASSERT(t != NULL);
2142fe8fb19SBen Gras 	_DIAGASSERT(names != NULL);
2152fe8fb19SBen Gras 
2162fe8fb19SBen Gras 	for (nm = names; *nm; nm++)
2172fe8fb19SBen Gras 		if (strcasecmp(t, *nm) == 0)
218*f14fb602SLionel Sambuc 			return (int)(nm - names);
2192fe8fb19SBen Gras 	if (isdigit((unsigned char) *t))
2202fe8fb19SBen Gras 		return (atoi(t));
2212fe8fb19SBen Gras 	return (0);
2222fe8fb19SBen Gras }
2232fe8fb19SBen Gras 
2242fe8fb19SBen Gras #if 0
2252fe8fb19SBen Gras static void
2262fe8fb19SBen Gras error(err)
2272fe8fb19SBen Gras 	int err;
2282fe8fb19SBen Gras {
2292fe8fb19SBen Gras 	char *p;
2302fe8fb19SBen Gras 
2312fe8fb19SBen Gras 	(void)write(STDERR_FILENO, "disktab: ", 9);
2322fe8fb19SBen Gras 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
2332fe8fb19SBen Gras 	(void)write(STDERR_FILENO, ": ", 2);
2342fe8fb19SBen Gras 	p = strerror(err);
2352fe8fb19SBen Gras 	(void)write(STDERR_FILENO, p, strlen(p));
2362fe8fb19SBen Gras 	(void)write(STDERR_FILENO, "\n", 1);
2372fe8fb19SBen Gras }
2382fe8fb19SBen Gras #endif
239