xref: /original-bsd/lib/libc/gen/disklabel.c (revision 7dd1926f)
11e80345cSdist /*
272f9a726Skarels  * Copyright (c) 1983, 1987 Regents of the University of California.
3da55c904Sbostic  * All rights reserved.
4da55c904Sbostic  *
5*7dd1926fSelan  * Redistribution and use in source and binary forms, with or without
6*7dd1926fSelan  * modification, are permitted provided that the following conditions
7*7dd1926fSelan  * are met:
8*7dd1926fSelan  * 1. Redistributions of source code must retain the above copyright
9*7dd1926fSelan  *    notice, this list of conditions and the following disclaimer.
10*7dd1926fSelan  * 2. Redistributions in binary form must reproduce the above copyright
11*7dd1926fSelan  *    notice, this list of conditions and the following disclaimer in the
12*7dd1926fSelan  *    documentation and/or other materials provided with the distribution.
13*7dd1926fSelan  * 3. All advertising materials mentioning features or use of this software
14*7dd1926fSelan  *    must display the following acknowledgement:
15*7dd1926fSelan  *	This product includes software developed by the University of
16*7dd1926fSelan  *	California, Berkeley and its contributors.
17*7dd1926fSelan  * 4. Neither the name of the University nor the names of its contributors
18*7dd1926fSelan  *    may be used to endorse or promote products derived from this software
19*7dd1926fSelan  *    without specific prior written permission.
20*7dd1926fSelan  *
21*7dd1926fSelan  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*7dd1926fSelan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*7dd1926fSelan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*7dd1926fSelan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*7dd1926fSelan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*7dd1926fSelan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*7dd1926fSelan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*7dd1926fSelan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*7dd1926fSelan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*7dd1926fSelan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*7dd1926fSelan  * SUCH DAMAGE.
321e80345cSdist  */
331e80345cSdist 
3487176300Sdonn #if defined(LIBC_SCCS) && !defined(lint)
35*7dd1926fSelan static char sccsid[] = "@(#)disklabel.c	5.19 (Berkeley) 3/19/92";
36da55c904Sbostic #endif /* LIBC_SCCS and not lint */
379876a848Ssam 
3872f9a726Skarels #include <sys/param.h>
39d5da809dSbostic #include <sys/errno.h>
4072f9a726Skarels #include <sys/file.h>
4172f9a726Skarels #define DKTYPENAMES
4272f9a726Skarels #include <sys/disklabel.h>
43ec9487d9Sbostic #include <ufs/ffs/fs.h>
449876a848Ssam #include <stdio.h>
4572a6a662Sbostic #include <string.h>
46ae227ee3Sdonn #include <stdlib.h>
47ae227ee3Sdonn #include <unistd.h>
489876a848Ssam 
49*7dd1926fSelan static int	error __P((int));
50*7dd1926fSelan static int	gettype __P((char *, char **));
519876a848Ssam 
5272f9a726Skarels struct disklabel *
539876a848Ssam getdiskbyname(name)
54ae227ee3Sdonn 	const char *name;
559876a848Ssam {
5672f9a726Skarels 	static struct	disklabel disk;
5772f9a726Skarels 	register struct	disklabel *dp = &disk;
58ef743ec4Ssam 	register struct partition *pp;
59*7dd1926fSelan 	char	*buf;
60*7dd1926fSelan 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
61*7dd1926fSelan 	char	*cp, *cq;	/* can't be register */
62af6dc062Smarc 	char	p, max, psize[3], pbsize[3],
63af6dc062Smarc 		pfsize[3], poffset[3], ptype[3];
6472f9a726Skarels 	u_long	*dx;
659876a848Ssam 
66*7dd1926fSelan 	if (cgetent(&buf, db_array, (char *) name) < 0)
67*7dd1926fSelan 		return NULL;
68*7dd1926fSelan 
6972f9a726Skarels 	bzero((char *)&disk, sizeof(disk));
70af6dc062Smarc 	/*
71af6dc062Smarc 	 * typename
72af6dc062Smarc 	 */
7372f9a726Skarels 	cq = dp->d_typename;
7472f9a726Skarels 	cp = buf;
7572f9a726Skarels 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
7672f9a726Skarels 	    (*cq = *cp) && *cq != '|' && *cq != ':')
7772f9a726Skarels 		cq++, cp++;
7872f9a726Skarels 	*cq = '\0';
79af6dc062Smarc 	/*
80af6dc062Smarc 	 * boot name (optional)  xxboot, bootxx
81af6dc062Smarc 	 */
82*7dd1926fSelan 	cgetstr(buf, "b0", &dp->d_boot0);
83*7dd1926fSelan 	cgetstr(buf, "b1", &dp->d_boot1);
84*7dd1926fSelan 
85*7dd1926fSelan 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
8672f9a726Skarels 		dp->d_flags |= D_REMOVABLE;
8772f9a726Skarels 	else  if (cq && strcmp(cq, "simulated") == 0)
8872f9a726Skarels 		dp->d_flags |= D_RAMDISK;
89*7dd1926fSelan 	if (cgetcap(buf, "sf", ':') != NULL)
9072f9a726Skarels 		dp->d_flags |= D_BADSECT;
91af6dc062Smarc 
9272f9a726Skarels #define getnumdflt(field, dname, dflt) \
93*7dd1926fSelan         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
9472f9a726Skarels 
9572f9a726Skarels 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
96*7dd1926fSelan 	cgetnum(buf, "nt",(long *) &dp->d_ntracks);
97*7dd1926fSelan 	cgetnum(buf, "ns",(long *) &dp->d_nsectors);
98*7dd1926fSelan 	cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
99*7dd1926fSelan 
100*7dd1926fSelan 	if (cgetstr(buf, "dt", &cq) > 0)
10172f9a726Skarels 		dp->d_type = gettype(cq, dktypenames);
10272f9a726Skarels 	else
10372f9a726Skarels 		getnumdflt(dp->d_type, "dt", 0);
10472f9a726Skarels 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
10572f9a726Skarels 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
10672f9a726Skarels 	getnumdflt(dp->d_rpm, "rm", 3600);
10772f9a726Skarels 	getnumdflt(dp->d_interleave, "il", 1);
10872f9a726Skarels 	getnumdflt(dp->d_trackskew, "sk", 0);
10972f9a726Skarels 	getnumdflt(dp->d_cylskew, "cs", 0);
11072f9a726Skarels 	getnumdflt(dp->d_headswitch, "hs", 0);
11172f9a726Skarels 	getnumdflt(dp->d_trkseek, "ts", 0);
11272f9a726Skarels 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
11372f9a726Skarels 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
114ef743ec4Ssam 	strcpy(psize, "px");
115ef743ec4Ssam 	strcpy(pbsize, "bx");
116ef743ec4Ssam 	strcpy(pfsize, "fx");
11772f9a726Skarels 	strcpy(poffset, "ox");
11872f9a726Skarels 	strcpy(ptype, "tx");
11972f9a726Skarels 	max = 'a' - 1;
12072f9a726Skarels 	pp = &dp->d_partitions[0];
12172f9a726Skarels 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
12272f9a726Skarels 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
123*7dd1926fSelan 		if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
12472f9a726Skarels 			pp->p_size = 0;
12572f9a726Skarels 		else {
126*7dd1926fSelan 			cgetnum(buf, poffset, (long *) &pp->p_offset);
12772f9a726Skarels 			getnumdflt(pp->p_fsize, pfsize, 0);
128*7dd1926fSelan 			if (pp->p_fsize) {
129*7dd1926fSelan 			        cgetnum(buf, pbsize, (long *) &pp->p_frag);
130*7dd1926fSelan 				pp->p_frag /= pp->p_fsize;
131*7dd1926fSelan 			}
13272f9a726Skarels 			getnumdflt(pp->p_fstype, ptype, 0);
133*7dd1926fSelan 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
13472f9a726Skarels 				pp->p_fstype = gettype(cq, fstypenames);
13572f9a726Skarels 			max = p;
1369876a848Ssam 		}
13772f9a726Skarels 	}
13872f9a726Skarels 	dp->d_npartitions = max + 1 - 'a';
1390a8a25dfSbostic 	(void)strcpy(psize, "dx");
14072f9a726Skarels 	dx = dp->d_drivedata;
14172f9a726Skarels 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
14272f9a726Skarels 		psize[1] = p;
14372f9a726Skarels 		getnumdflt(*dx, psize, 0);
14472f9a726Skarels 	}
14572f9a726Skarels 	dp->d_magic = DISKMAGIC;
14672f9a726Skarels 	dp->d_magic2 = DISKMAGIC;
147*7dd1926fSelan 	free(buf);
1489876a848Ssam 	return (dp);
1499876a848Ssam }
1509876a848Ssam 
151*7dd1926fSelan static int
15272f9a726Skarels gettype(t, names)
15372f9a726Skarels 	char *t;
15472f9a726Skarels 	char **names;
15572f9a726Skarels {
15672f9a726Skarels 	register char **nm;
15772f9a726Skarels 
15872f9a726Skarels 	for (nm = names; *nm; nm++)
159c32170efSmckusick 		if (strcasecmp(t, *nm) == 0)
16072f9a726Skarels 			return (nm - names);
16172f9a726Skarels 	if (isdigit(*t))
16272f9a726Skarels 		return (atoi(t));
16372f9a726Skarels 	return (0);
16472f9a726Skarels }
16572f9a726Skarels 
166*7dd1926fSelan static int
167d5da809dSbostic error(err)
168d5da809dSbostic 	int err;
169d5da809dSbostic {
170d5da809dSbostic 	char *p;
171d5da809dSbostic 
172d5da809dSbostic 	(void)write(STDERR_FILENO, "disktab: ", 9);
173d5da809dSbostic 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
1747f83c103Sbostic 	(void)write(STDERR_FILENO, ": ", 2);
175d5da809dSbostic 	p = strerror(err);
176d5da809dSbostic 	(void)write(STDERR_FILENO, p, strlen(p));
177d5da809dSbostic 	(void)write(STDERR_FILENO, "\n", 1);
178d5da809dSbostic }
179