xref: /freebsd/lib/libc/gen/disklabel.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1983, 1987, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3226a2df73SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
33cc0ea346SBruce Evans static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
3426a2df73SDavid E. O'Brien #endif /* LIBC_SCCS and not lint */
35b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
36b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$");
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #include <sys/param.h>
3958f0484fSRodney W. Grimes #define DKTYPENAMES
40b8606fe6SBosko Milekic #define FSTYPENAMES
4158f0484fSRodney W. Grimes #include <sys/disklabel.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <fcntl.h>
4458f0484fSRodney W. Grimes #include <stdio.h>
4558f0484fSRodney W. Grimes #include <stdlib.h>
4658f0484fSRodney W. Grimes #include <string.h>
4758f0484fSRodney W. Grimes #include <unistd.h>
488b102407SPoul-Henning Kamp #include <ctype.h>
4958f0484fSRodney W. Grimes 
50674a5ae3SPoul-Henning Kamp static int
51674a5ae3SPoul-Henning Kamp gettype(char *t, const char **names)
52674a5ae3SPoul-Henning Kamp {
53674a5ae3SPoul-Henning Kamp 	const char **nm;
54674a5ae3SPoul-Henning Kamp 
55674a5ae3SPoul-Henning Kamp 	for (nm = names; *nm; nm++)
56674a5ae3SPoul-Henning Kamp 		if (strcasecmp(t, *nm) == 0)
57674a5ae3SPoul-Henning Kamp 			return (nm - names);
58674a5ae3SPoul-Henning Kamp 	if (isdigit((unsigned char)*t))
59674a5ae3SPoul-Henning Kamp 		return (atoi(t));
60674a5ae3SPoul-Henning Kamp 	return (0);
61674a5ae3SPoul-Henning Kamp }
6258f0484fSRodney W. Grimes 
6358f0484fSRodney W. Grimes struct disklabel *
64674a5ae3SPoul-Henning Kamp getdiskbyname(const char *name)
6558f0484fSRodney W. Grimes {
6658f0484fSRodney W. Grimes 	static struct	disklabel disk;
67b231cb39SDavid E. O'Brien 	struct	disklabel *dp = &disk;
68b231cb39SDavid E. O'Brien 	struct partition *pp;
6958f0484fSRodney W. Grimes 	char	*buf;
7058f0484fSRodney W. Grimes 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
7158f0484fSRodney W. Grimes 	char	*cp, *cq;	/* can't be register */
7258f0484fSRodney W. Grimes 	char	p, max, psize[3], pbsize[3],
7358f0484fSRodney W. Grimes 		pfsize[3], poffset[3], ptype[3];
74cc0ea346SBruce Evans 	u_int32_t *dx;
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes 	if (cgetent(&buf, db_array, (char *) name) < 0)
7758f0484fSRodney W. Grimes 		return NULL;
7858f0484fSRodney W. Grimes 
7958f0484fSRodney W. Grimes 	bzero((char *)&disk, sizeof(disk));
8058f0484fSRodney W. Grimes 	/*
8158f0484fSRodney W. Grimes 	 * typename
8258f0484fSRodney W. Grimes 	 */
8358f0484fSRodney W. Grimes 	cq = dp->d_typename;
8458f0484fSRodney W. Grimes 	cp = buf;
8558f0484fSRodney W. Grimes 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
8658f0484fSRodney W. Grimes 	    (*cq = *cp) && *cq != '|' && *cq != ':')
8758f0484fSRodney W. Grimes 		cq++, cp++;
8858f0484fSRodney W. Grimes 	*cq = '\0';
8958f0484fSRodney W. Grimes 
905262b957SPedro F. Giffuni 	if (cgetstr(buf, "ty", &cq) > 0) {
915262b957SPedro F. Giffuni 		if (strcmp(cq, "removable") == 0)
9258f0484fSRodney W. Grimes 			dp->d_flags |= D_REMOVABLE;
9358f0484fSRodney W. Grimes 		else  if (cq && strcmp(cq, "simulated") == 0)
9458f0484fSRodney W. Grimes 			dp->d_flags |= D_RAMDISK;
955262b957SPedro F. Giffuni 		free(cq);
965262b957SPedro F. Giffuni 	}
9758f0484fSRodney W. Grimes 	if (cgetcap(buf, "sf", ':') != NULL)
9858f0484fSRodney W. Grimes 		dp->d_flags |= D_BADSECT;
9958f0484fSRodney W. Grimes 
10058f0484fSRodney W. Grimes #define getnumdflt(field, dname, dflt) \
10158f0484fSRodney W. Grimes         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
10258f0484fSRodney W. Grimes 
10358f0484fSRodney W. Grimes 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
104f0a36920SDoug Rabson 	getnumdflt(dp->d_ntracks, "nt", 0);
105f0a36920SDoug Rabson 	getnumdflt(dp->d_nsectors, "ns", 0);
106f0a36920SDoug Rabson 	getnumdflt(dp->d_ncylinders, "nc", 0);
10758f0484fSRodney W. Grimes 
1085262b957SPedro F. Giffuni 	if (cgetstr(buf, "dt", &cq) > 0) {
10958f0484fSRodney W. Grimes 		dp->d_type = gettype(cq, dktypenames);
1105262b957SPedro F. Giffuni 		free(cq);
1115262b957SPedro F. Giffuni 	} else
11258f0484fSRodney W. Grimes 		getnumdflt(dp->d_type, "dt", 0);
11358f0484fSRodney W. Grimes 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
11458f0484fSRodney W. Grimes 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
11558f0484fSRodney W. Grimes 	getnumdflt(dp->d_rpm, "rm", 3600);
11658f0484fSRodney W. Grimes 	getnumdflt(dp->d_interleave, "il", 1);
11758f0484fSRodney W. Grimes 	getnumdflt(dp->d_trackskew, "sk", 0);
11858f0484fSRodney W. Grimes 	getnumdflt(dp->d_cylskew, "cs", 0);
11958f0484fSRodney W. Grimes 	getnumdflt(dp->d_headswitch, "hs", 0);
12058f0484fSRodney W. Grimes 	getnumdflt(dp->d_trkseek, "ts", 0);
12158f0484fSRodney W. Grimes 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
12277068a7fSPoul-Henning Kamp 	getnumdflt(dp->d_sbsize, "sb", 0);
12358f0484fSRodney W. Grimes 	strcpy(psize, "px");
12458f0484fSRodney W. Grimes 	strcpy(pbsize, "bx");
12558f0484fSRodney W. Grimes 	strcpy(pfsize, "fx");
12658f0484fSRodney W. Grimes 	strcpy(poffset, "ox");
12758f0484fSRodney W. Grimes 	strcpy(ptype, "tx");
12858f0484fSRodney W. Grimes 	max = 'a' - 1;
12958f0484fSRodney W. Grimes 	pp = &dp->d_partitions[0];
13058f0484fSRodney W. Grimes 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
131f0a36920SDoug Rabson 		long l;
13258f0484fSRodney W. Grimes 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
133f0a36920SDoug Rabson 		if (cgetnum(buf, psize, &l) == -1)
13458f0484fSRodney W. Grimes 			pp->p_size = 0;
13558f0484fSRodney W. Grimes 		else {
136f0a36920SDoug Rabson 			pp->p_size = l;
137f0a36920SDoug Rabson 			cgetnum(buf, poffset, &l);
138f0a36920SDoug Rabson 			pp->p_offset = l;
13958f0484fSRodney W. Grimes 			getnumdflt(pp->p_fsize, pfsize, 0);
14058f0484fSRodney W. Grimes 			if (pp->p_fsize) {
14158f0484fSRodney W. Grimes 				long bsize;
14258f0484fSRodney W. Grimes 
14358f0484fSRodney W. Grimes 				if (cgetnum(buf, pbsize, &bsize) == 0)
14458f0484fSRodney W. Grimes 					pp->p_frag = bsize / pp->p_fsize;
14558f0484fSRodney W. Grimes 				else
14658f0484fSRodney W. Grimes 					pp->p_frag = 8;
14758f0484fSRodney W. Grimes 			}
14858f0484fSRodney W. Grimes 			getnumdflt(pp->p_fstype, ptype, 0);
1495262b957SPedro F. Giffuni 			if (pp->p_fstype == 0)
1505262b957SPedro F. Giffuni 				if (cgetstr(buf, ptype, &cq) >= 0) {
15158f0484fSRodney W. Grimes 					pp->p_fstype = gettype(cq, fstypenames);
1525262b957SPedro F. Giffuni 					free(cq);
1535262b957SPedro F. Giffuni 				}
15458f0484fSRodney W. Grimes 			max = p;
15558f0484fSRodney W. Grimes 		}
15658f0484fSRodney W. Grimes 	}
15758f0484fSRodney W. Grimes 	dp->d_npartitions = max + 1 - 'a';
15858f0484fSRodney W. Grimes 	(void)strcpy(psize, "dx");
15958f0484fSRodney W. Grimes 	dx = dp->d_drivedata;
16058f0484fSRodney W. Grimes 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
16158f0484fSRodney W. Grimes 		psize[1] = p;
16258f0484fSRodney W. Grimes 		getnumdflt(*dx, psize, 0);
16358f0484fSRodney W. Grimes 	}
16458f0484fSRodney W. Grimes 	dp->d_magic = DISKMAGIC;
16558f0484fSRodney W. Grimes 	dp->d_magic2 = DISKMAGIC;
16658f0484fSRodney W. Grimes 	free(buf);
16758f0484fSRodney W. Grimes 	return (dp);
16858f0484fSRodney W. Grimes }
169