xref: /netbsd-src/lib/libc/gen/disklabel.c (revision 9e66e6d75e9910b3de5f4ef031995955f69a7dd1)
1*9e66e6d7Sabs /*	$NetBSD: disklabel.c,v 1.37 2012/06/25 22:32:43 abs Exp $	*/
25d6d5697Scgd 
361f28255Scgd /*
45d6d5697Scgd  * Copyright (c) 1983, 1987, 1993
55d6d5697Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32a37289dbSdyoung #if HAVE_NBTOOL_CONFIG_H
33a37289dbSdyoung #include "nbtool_config.h"
34a37289dbSdyoung #endif
35a37289dbSdyoung 
36eccdadf1Schristos #include <sys/cdefs.h>
371e1bba23Scgd #if defined(LIBC_SCCS) && !defined(lint)
385d6d5697Scgd #if 0
3908b0946fSperry static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
405d6d5697Scgd #else
41*9e66e6d7Sabs __RCSID("$NetBSD: disklabel.c,v 1.37 2012/06/25 22:32:43 abs Exp $");
425d6d5697Scgd #endif
431e1bba23Scgd #endif /* LIBC_SCCS and not lint */
4461f28255Scgd 
4543fa6fe3Sjtc #include "namespace.h"
4661f28255Scgd #include <sys/param.h>
4761f28255Scgd #define DKTYPENAMES
4829260d71Schristos #define FSTYPENAMES
4908b0946fSperry #include <ufs/ufs/dinode.h>
505d6d5697Scgd #include <ufs/ffs/fs.h>
515d6d5697Scgd 
52a37289dbSdyoung #if HAVE_NBTOOL_CONFIG_H
530ec45ddeShe #include <nbinclude/sys/disklabel.h>
540ec45ddeShe #include <nbinclude/disktab.h>
55a37289dbSdyoung #else
56a37289dbSdyoung #include <sys/disklabel.h>
57a37289dbSdyoung #include <disktab.h>
58a37289dbSdyoung #endif /* HAVE_NBTOOL_CONFIG_H */
59a37289dbSdyoung 
60b48252f3Slukem #include <assert.h>
613ba2c075Sjtc #include <ctype.h>
625d6d5697Scgd #include <errno.h>
635d6d5697Scgd #include <fcntl.h>
6461f28255Scgd #include <stdio.h>
6561f28255Scgd #include <stdlib.h>
665d6d5697Scgd #include <string.h>
6761f28255Scgd #include <unistd.h>
6861f28255Scgd 
6943fa6fe3Sjtc #ifdef __weak_alias
7060549036Smycroft __weak_alias(getdiskbyname,_getdiskbyname)
7143fa6fe3Sjtc #endif
7243fa6fe3Sjtc 
73eccdadf1Schristos #if 0
74cf884af3Smatt static void	error(int);
75eccdadf1Schristos #endif
76cf884af3Smatt static int	gettype(char *, const char *const *);
7761f28255Scgd 
7875f2f164Schristos static const char *db_array[2] = { _PATH_DISKTAB, 0 };
79593d6c65Sabs 
80593d6c65Sabs int
setdisktab(const char * name)81*9e66e6d7Sabs setdisktab(const char *name)
82593d6c65Sabs {
83593d6c65Sabs 	if (!name || !*name)
84593d6c65Sabs 		return -1;
85593d6c65Sabs 
86593d6c65Sabs 	db_array[0] = name;
87593d6c65Sabs 	return 0;
88593d6c65Sabs }
89593d6c65Sabs 
90593d6c65Sabs 
9161f28255Scgd struct disklabel *
getdiskbyname(const char * name)92*9e66e6d7Sabs getdiskbyname(const char *name)
9361f28255Scgd {
9461f28255Scgd 	static struct	disklabel disk;
954146d586Sperry 	struct	disklabel *dp = &disk;
964146d586Sperry 	struct partition *pp;
975d6d5697Scgd 	char	*buf;
984146d586Sperry 	char	*cp, *cq;	/* can't be */
9961f28255Scgd 	char	p, max, psize[3], pbsize[3],
10061f28255Scgd 		pfsize[3], poffset[3], ptype[3];
101db638fe1Scgd 	u_int32_t *dx;
10229260d71Schristos 	long f;
10361f28255Scgd 
104b48252f3Slukem 	_DIAGASSERT(name != NULL);
105b48252f3Slukem 
10629260d71Schristos 	if (cgetent(&buf, db_array, name) < 0)
1075d6d5697Scgd 		return NULL;
1085d6d5697Scgd 
109a843f0f8Sperry 	memset(&disk, 0, sizeof(disk));
11061f28255Scgd 	/*
11161f28255Scgd 	 * typename
11261f28255Scgd 	 */
11361f28255Scgd 	cq = dp->d_typename;
11461f28255Scgd 	cp = buf;
11561f28255Scgd 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
11661f28255Scgd 	    (*cq = *cp) && *cq != '|' && *cq != ':')
11761f28255Scgd 		cq++, cp++;
11861f28255Scgd 	*cq = '\0';
11961f28255Scgd 	/*
12061f28255Scgd 	 * boot name (optional)  xxboot, bootxx
12161f28255Scgd 	 */
1225d6d5697Scgd 	cgetstr(buf, "b0", &dp->d_boot0);
1235d6d5697Scgd 	cgetstr(buf, "b1", &dp->d_boot1);
1245d6d5697Scgd 
125a0357ac2Schristos 	if (cgetstr(buf, "ty", &cq) >= 0) {
126a0357ac2Schristos 		if (strcmp(cq, "removable") == 0)
12761f28255Scgd 			dp->d_flags |= D_REMOVABLE;
128a0357ac2Schristos 		else if (strcmp(cq, "simulated") == 0)
12961f28255Scgd 			dp->d_flags |= D_RAMDISK;
130a0357ac2Schristos 		free(cq);
131a0357ac2Schristos 	}
1325d6d5697Scgd 	if (cgetcap(buf, "sf", ':') != NULL)
13361f28255Scgd 		dp->d_flags |= D_BADSECT;
13461f28255Scgd 
13561f28255Scgd #define getnumdflt(field, dname, dflt) \
13637dfab8aSthorpej     (field) = ((cgetnum(buf, dname, &f) == -1) ? (dflt) : (u_int32_t) f)
13785ab28f7Scgd #define	getnum(field, dname) \
13829260d71Schristos 	if (cgetnum(buf, dname, &f) != -1) field = (u_int32_t)f
13961f28255Scgd 
14061f28255Scgd 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
14185ab28f7Scgd 	getnum(dp->d_ntracks, "nt");
14285ab28f7Scgd 	getnum(dp->d_nsectors, "ns");
14385ab28f7Scgd 	getnum(dp->d_ncylinders, "nc");
1445d6d5697Scgd 
145a0357ac2Schristos 	if (cgetstr(buf, "dt", &cq) >= 0) {
14661f28255Scgd 		dp->d_type = gettype(cq, dktypenames);
147a0357ac2Schristos 		free(cq);
148a0357ac2Schristos 	} else
14961f28255Scgd 		getnumdflt(dp->d_type, "dt", 0);
15061f28255Scgd 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
15161f28255Scgd 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
15261f28255Scgd 	getnumdflt(dp->d_rpm, "rm", 3600);
15361f28255Scgd 	getnumdflt(dp->d_interleave, "il", 1);
15461f28255Scgd 	getnumdflt(dp->d_trackskew, "sk", 0);
15561f28255Scgd 	getnumdflt(dp->d_cylskew, "cs", 0);
15661f28255Scgd 	getnumdflt(dp->d_headswitch, "hs", 0);
15761f28255Scgd 	getnumdflt(dp->d_trkseek, "ts", 0);
15861f28255Scgd 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
15942614ed3Sfvdl 	getnumdflt(dp->d_sbsize, "sb", SBLOCKSIZE);
16048b6b629Sscottr 	strcpy(psize, "px");	/* XXX: strcpy is safe */
16148b6b629Sscottr 	strcpy(pbsize, "bx");	/* XXX: strcpy is safe */
16248b6b629Sscottr 	strcpy(pfsize, "fx");	/* XXX: strcpy is safe */
16348b6b629Sscottr 	strcpy(poffset, "ox");	/* XXX: strcpy is safe */
16448b6b629Sscottr 	strcpy(ptype, "tx");	/* XXX: strcpy is safe */
16561f28255Scgd 	max = 'a' - 1;
16661f28255Scgd 	pp = &dp->d_partitions[0];
16761f28255Scgd 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
16829260d71Schristos 		long ff;
16985ab28f7Scgd 
17061f28255Scgd 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
17129260d71Schristos 		if (cgetnum(buf, psize, &ff) == -1)
17261f28255Scgd 			pp->p_size = 0;
17361f28255Scgd 		else {
17429260d71Schristos 			pp->p_size = (u_int32_t)ff;
17585ab28f7Scgd 			getnum(pp->p_offset, poffset);
17661f28255Scgd 			getnumdflt(pp->p_fsize, pfsize, 0);
1775d6d5697Scgd 			if (pp->p_fsize) {
1785d6d5697Scgd 				long bsize;
1795d6d5697Scgd 
18029260d71Schristos 				if (cgetnum(buf, pbsize, &bsize) == -1)
1815d6d5697Scgd 					pp->p_frag = 8;
18229260d71Schristos 				else
18329260d71Schristos 					pp->p_frag =
18429260d71Schristos 					    (u_int8_t)(bsize / pp->p_fsize);
1855d6d5697Scgd 			}
18661f28255Scgd 			getnumdflt(pp->p_fstype, ptype, 0);
187a0357ac2Schristos 			if (pp->p_fstype == 0)
188a0357ac2Schristos 				if (cgetstr(buf, ptype, &cq) >= 0) {
18961f28255Scgd 					pp->p_fstype = gettype(cq, fstypenames);
190a0357ac2Schristos 					free(cq);
191a0357ac2Schristos 				}
19261f28255Scgd 			max = p;
19361f28255Scgd 		}
19461f28255Scgd 	}
19561f28255Scgd 	dp->d_npartitions = max + 1 - 'a';
19648b6b629Sscottr 	strcpy(psize, "dx");	/* XXX: strcpy is safe */
19761f28255Scgd 	dx = dp->d_drivedata;
19861f28255Scgd 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
19961f28255Scgd 		psize[1] = p;
20061f28255Scgd 		getnumdflt(*dx, psize, 0);
20161f28255Scgd 	}
20261f28255Scgd 	dp->d_magic = DISKMAGIC;
20361f28255Scgd 	dp->d_magic2 = DISKMAGIC;
2045d6d5697Scgd 	free(buf);
20561f28255Scgd 	return (dp);
20661f28255Scgd }
20761f28255Scgd 
208a054c658Sjtc static int
gettype(char * t,const char * const * names)209cf884af3Smatt gettype(char *t, const char *const *names)
21061f28255Scgd {
2114024f736Smycroft 	const char *const *nm;
21261f28255Scgd 
213b48252f3Slukem 	_DIAGASSERT(t != NULL);
214b48252f3Slukem 	_DIAGASSERT(names != NULL);
215b48252f3Slukem 
21661f28255Scgd 	for (nm = names; *nm; nm++)
21761f28255Scgd 		if (strcasecmp(t, *nm) == 0)
218c5e820caSchristos 			return (int)(nm - names);
2197daefc5aSitohy 	if (isdigit((unsigned char) *t))
22061f28255Scgd 		return (atoi(t));
22161f28255Scgd 	return (0);
22261f28255Scgd }
22361f28255Scgd 
224eccdadf1Schristos #if 0
225a054c658Sjtc static void
22661f28255Scgd error(err)
22761f28255Scgd 	int err;
22861f28255Scgd {
22961f28255Scgd 	char *p;
23061f28255Scgd 
23161f28255Scgd 	(void)write(STDERR_FILENO, "disktab: ", 9);
23261f28255Scgd 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
2335d6d5697Scgd 	(void)write(STDERR_FILENO, ": ", 2);
23461f28255Scgd 	p = strerror(err);
23561f28255Scgd 	(void)write(STDERR_FILENO, p, strlen(p));
23661f28255Scgd 	(void)write(STDERR_FILENO, "\n", 1);
23761f28255Scgd }
238eccdadf1Schristos #endif
239