xref: /freebsd/lib/libc/gen/disklabel.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
1 /*
2  * Copyright (c) 1983, 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #define DKTYPENAMES
42 #include <sys/disklabel.h>
43 
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 
51 static int
52 gettype(char *t, const char **names)
53 {
54 	const char **nm;
55 
56 	for (nm = names; *nm; nm++)
57 		if (strcasecmp(t, *nm) == 0)
58 			return (nm - names);
59 	if (isdigit((unsigned char)*t))
60 		return (atoi(t));
61 	return (0);
62 }
63 
64 struct disklabel *
65 getdiskbyname(const char *name)
66 {
67 	static struct	disklabel disk;
68 	struct	disklabel *dp = &disk;
69 	struct partition *pp;
70 	char	*buf;
71 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
72 	char	*cp, *cq;	/* can't be register */
73 	char	p, max, psize[3], pbsize[3],
74 		pfsize[3], poffset[3], ptype[3];
75 	u_int32_t *dx;
76 
77 	if (cgetent(&buf, db_array, (char *) name) < 0)
78 		return NULL;
79 
80 	bzero((char *)&disk, sizeof(disk));
81 	/*
82 	 * typename
83 	 */
84 	cq = dp->d_typename;
85 	cp = buf;
86 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
87 	    (*cq = *cp) && *cq != '|' && *cq != ':')
88 		cq++, cp++;
89 	*cq = '\0';
90 
91 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
92 		dp->d_flags |= D_REMOVABLE;
93 	else  if (cq && strcmp(cq, "simulated") == 0)
94 		dp->d_flags |= D_RAMDISK;
95 	if (cgetcap(buf, "sf", ':') != NULL)
96 		dp->d_flags |= D_BADSECT;
97 
98 #define getnumdflt(field, dname, dflt) \
99         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
100 
101 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
102 	getnumdflt(dp->d_ntracks, "nt", 0);
103 	getnumdflt(dp->d_nsectors, "ns", 0);
104 	getnumdflt(dp->d_ncylinders, "nc", 0);
105 
106 	if (cgetstr(buf, "dt", &cq) > 0)
107 		dp->d_type = gettype(cq, dktypenames);
108 	else
109 		getnumdflt(dp->d_type, "dt", 0);
110 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
111 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
112 	getnumdflt(dp->d_rpm, "rm", 3600);
113 	getnumdflt(dp->d_interleave, "il", 1);
114 	getnumdflt(dp->d_trackskew, "sk", 0);
115 	getnumdflt(dp->d_cylskew, "cs", 0);
116 	getnumdflt(dp->d_headswitch, "hs", 0);
117 	getnumdflt(dp->d_trkseek, "ts", 0);
118 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
119 	getnumdflt(dp->d_sbsize, "sb", 0);
120 	strcpy(psize, "px");
121 	strcpy(pbsize, "bx");
122 	strcpy(pfsize, "fx");
123 	strcpy(poffset, "ox");
124 	strcpy(ptype, "tx");
125 	max = 'a' - 1;
126 	pp = &dp->d_partitions[0];
127 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
128 		long l;
129 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
130 		if (cgetnum(buf, psize, &l) == -1)
131 			pp->p_size = 0;
132 		else {
133 			pp->p_size = l;
134 			cgetnum(buf, poffset, &l);
135 			pp->p_offset = l;
136 			getnumdflt(pp->p_fsize, pfsize, 0);
137 			if (pp->p_fsize) {
138 				long bsize;
139 
140 				if (cgetnum(buf, pbsize, &bsize) == 0)
141 					pp->p_frag = bsize / pp->p_fsize;
142 				else
143 					pp->p_frag = 8;
144 			}
145 			getnumdflt(pp->p_fstype, ptype, 0);
146 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
147 				pp->p_fstype = gettype(cq, fstypenames);
148 			max = p;
149 		}
150 	}
151 	dp->d_npartitions = max + 1 - 'a';
152 	(void)strcpy(psize, "dx");
153 	dx = dp->d_drivedata;
154 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
155 		psize[1] = p;
156 		getnumdflt(*dx, psize, 0);
157 	}
158 	dp->d_magic = DISKMAGIC;
159 	dp->d_magic2 = DISKMAGIC;
160 	free(buf);
161 	return (dp);
162 }
163