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