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