xref: /freebsd/lib/libc/gen/disklabel.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
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 
3258f0484fSRodney W. Grimes #include <sys/param.h>
3358f0484fSRodney W. Grimes #define DKTYPENAMES
34b8606fe6SBosko Milekic #define FSTYPENAMES
3558f0484fSRodney W. Grimes #include <sys/disklabel.h>
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #include <fcntl.h>
3858f0484fSRodney W. Grimes #include <stdio.h>
3958f0484fSRodney W. Grimes #include <stdlib.h>
4058f0484fSRodney W. Grimes #include <string.h>
4158f0484fSRodney W. Grimes #include <unistd.h>
428b102407SPoul-Henning Kamp #include <ctype.h>
4358f0484fSRodney W. Grimes 
44674a5ae3SPoul-Henning Kamp static int
gettype(char * t,const char ** names)45674a5ae3SPoul-Henning Kamp gettype(char *t, const char **names)
46674a5ae3SPoul-Henning Kamp {
47674a5ae3SPoul-Henning Kamp 	const char **nm;
48674a5ae3SPoul-Henning Kamp 
49674a5ae3SPoul-Henning Kamp 	for (nm = names; *nm; nm++)
50674a5ae3SPoul-Henning Kamp 		if (strcasecmp(t, *nm) == 0)
51674a5ae3SPoul-Henning Kamp 			return (nm - names);
52674a5ae3SPoul-Henning Kamp 	if (isdigit((unsigned char)*t))
53674a5ae3SPoul-Henning Kamp 		return (atoi(t));
54674a5ae3SPoul-Henning Kamp 	return (0);
55674a5ae3SPoul-Henning Kamp }
5658f0484fSRodney W. Grimes 
5758f0484fSRodney W. Grimes struct disklabel *
getdiskbyname(const char * name)58674a5ae3SPoul-Henning Kamp getdiskbyname(const char *name)
5958f0484fSRodney W. Grimes {
6058f0484fSRodney W. Grimes 	static struct	disklabel disk;
61b231cb39SDavid E. O'Brien 	struct	disklabel *dp = &disk;
62b231cb39SDavid E. O'Brien 	struct partition *pp;
6358f0484fSRodney W. Grimes 	char	*buf;
6458f0484fSRodney W. Grimes 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
6558f0484fSRodney W. Grimes 	char	*cp, *cq;	/* can't be register */
6658f0484fSRodney W. Grimes 	char	p, max, psize[3], pbsize[3],
6758f0484fSRodney W. Grimes 		pfsize[3], poffset[3], ptype[3];
68cc0ea346SBruce Evans 	u_int32_t *dx;
6958f0484fSRodney W. Grimes 
7058f0484fSRodney W. Grimes 	if (cgetent(&buf, db_array, (char *) name) < 0)
7158f0484fSRodney W. Grimes 		return NULL;
7258f0484fSRodney W. Grimes 
7358f0484fSRodney W. Grimes 	bzero((char *)&disk, sizeof(disk));
7458f0484fSRodney W. Grimes 	/*
7558f0484fSRodney W. Grimes 	 * typename
7658f0484fSRodney W. Grimes 	 */
7758f0484fSRodney W. Grimes 	cq = dp->d_typename;
7858f0484fSRodney W. Grimes 	cp = buf;
7958f0484fSRodney W. Grimes 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
8058f0484fSRodney W. Grimes 	    (*cq = *cp) && *cq != '|' && *cq != ':')
8158f0484fSRodney W. Grimes 		cq++, cp++;
8258f0484fSRodney W. Grimes 	*cq = '\0';
8358f0484fSRodney W. Grimes 
845262b957SPedro F. Giffuni 	if (cgetstr(buf, "ty", &cq) > 0) {
855262b957SPedro F. Giffuni 		if (strcmp(cq, "removable") == 0)
8658f0484fSRodney W. Grimes 			dp->d_flags |= D_REMOVABLE;
8758f0484fSRodney W. Grimes 		else  if (cq && strcmp(cq, "simulated") == 0)
8858f0484fSRodney W. Grimes 			dp->d_flags |= D_RAMDISK;
895262b957SPedro F. Giffuni 		free(cq);
905262b957SPedro F. Giffuni 	}
9158f0484fSRodney W. Grimes 	if (cgetcap(buf, "sf", ':') != NULL)
9258f0484fSRodney W. Grimes 		dp->d_flags |= D_BADSECT;
9358f0484fSRodney W. Grimes 
9458f0484fSRodney W. Grimes #define getnumdflt(field, dname, dflt) \
9558f0484fSRodney W. Grimes         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
9658f0484fSRodney W. Grimes 
9758f0484fSRodney W. Grimes 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
98f0a36920SDoug Rabson 	getnumdflt(dp->d_ntracks, "nt", 0);
99f0a36920SDoug Rabson 	getnumdflt(dp->d_nsectors, "ns", 0);
100f0a36920SDoug Rabson 	getnumdflt(dp->d_ncylinders, "nc", 0);
10158f0484fSRodney W. Grimes 
1025262b957SPedro F. Giffuni 	if (cgetstr(buf, "dt", &cq) > 0) {
10358f0484fSRodney W. Grimes 		dp->d_type = gettype(cq, dktypenames);
1045262b957SPedro F. Giffuni 		free(cq);
1055262b957SPedro F. Giffuni 	} else
10658f0484fSRodney W. Grimes 		getnumdflt(dp->d_type, "dt", 0);
10758f0484fSRodney W. Grimes 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
10858f0484fSRodney W. Grimes 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
10958f0484fSRodney W. Grimes 	getnumdflt(dp->d_rpm, "rm", 3600);
11058f0484fSRodney W. Grimes 	getnumdflt(dp->d_interleave, "il", 1);
11158f0484fSRodney W. Grimes 	getnumdflt(dp->d_trackskew, "sk", 0);
11258f0484fSRodney W. Grimes 	getnumdflt(dp->d_cylskew, "cs", 0);
11358f0484fSRodney W. Grimes 	getnumdflt(dp->d_headswitch, "hs", 0);
11458f0484fSRodney W. Grimes 	getnumdflt(dp->d_trkseek, "ts", 0);
11558f0484fSRodney W. Grimes 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
11677068a7fSPoul-Henning Kamp 	getnumdflt(dp->d_sbsize, "sb", 0);
11758f0484fSRodney W. Grimes 	strcpy(psize, "px");
11858f0484fSRodney W. Grimes 	strcpy(pbsize, "bx");
11958f0484fSRodney W. Grimes 	strcpy(pfsize, "fx");
12058f0484fSRodney W. Grimes 	strcpy(poffset, "ox");
12158f0484fSRodney W. Grimes 	strcpy(ptype, "tx");
12258f0484fSRodney W. Grimes 	max = 'a' - 1;
12358f0484fSRodney W. Grimes 	pp = &dp->d_partitions[0];
12458f0484fSRodney W. Grimes 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
125f0a36920SDoug Rabson 		long l;
12658f0484fSRodney W. Grimes 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
127f0a36920SDoug Rabson 		if (cgetnum(buf, psize, &l) == -1)
12858f0484fSRodney W. Grimes 			pp->p_size = 0;
12958f0484fSRodney W. Grimes 		else {
130f0a36920SDoug Rabson 			pp->p_size = l;
131f0a36920SDoug Rabson 			cgetnum(buf, poffset, &l);
132f0a36920SDoug Rabson 			pp->p_offset = l;
13358f0484fSRodney W. Grimes 			getnumdflt(pp->p_fsize, pfsize, 0);
13458f0484fSRodney W. Grimes 			if (pp->p_fsize) {
13558f0484fSRodney W. Grimes 				long bsize;
13658f0484fSRodney W. Grimes 
13758f0484fSRodney W. Grimes 				if (cgetnum(buf, pbsize, &bsize) == 0)
13858f0484fSRodney W. Grimes 					pp->p_frag = bsize / pp->p_fsize;
13958f0484fSRodney W. Grimes 				else
14058f0484fSRodney W. Grimes 					pp->p_frag = 8;
14158f0484fSRodney W. Grimes 			}
14258f0484fSRodney W. Grimes 			getnumdflt(pp->p_fstype, ptype, 0);
1435262b957SPedro F. Giffuni 			if (pp->p_fstype == 0)
1445262b957SPedro F. Giffuni 				if (cgetstr(buf, ptype, &cq) >= 0) {
14558f0484fSRodney W. Grimes 					pp->p_fstype = gettype(cq, fstypenames);
1465262b957SPedro F. Giffuni 					free(cq);
1475262b957SPedro F. Giffuni 				}
14858f0484fSRodney W. Grimes 			max = p;
14958f0484fSRodney W. Grimes 		}
15058f0484fSRodney W. Grimes 	}
15158f0484fSRodney W. Grimes 	dp->d_npartitions = max + 1 - 'a';
15258f0484fSRodney W. Grimes 	(void)strcpy(psize, "dx");
15358f0484fSRodney W. Grimes 	dx = dp->d_drivedata;
15458f0484fSRodney W. Grimes 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
15558f0484fSRodney W. Grimes 		psize[1] = p;
15658f0484fSRodney W. Grimes 		getnumdflt(*dx, psize, 0);
15758f0484fSRodney W. Grimes 	}
15858f0484fSRodney W. Grimes 	dp->d_magic = DISKMAGIC;
15958f0484fSRodney W. Grimes 	dp->d_magic2 = DISKMAGIC;
16058f0484fSRodney W. Grimes 	free(buf);
16158f0484fSRodney W. Grimes 	return (dp);
16258f0484fSRodney W. Grimes }
163