xref: /freebsd/lib/libc/gen/disklabel.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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/param.h>
33 #define DKTYPENAMES
34 #define FSTYPENAMES
35 #include <sys/disklabel.h>
36 
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <ctype.h>
43 
44 static int
45 gettype(char *t, const char **names)
46 {
47 	const char **nm;
48 
49 	for (nm = names; *nm; nm++)
50 		if (strcasecmp(t, *nm) == 0)
51 			return (nm - names);
52 	if (isdigit((unsigned char)*t))
53 		return (atoi(t));
54 	return (0);
55 }
56 
57 struct disklabel *
58 getdiskbyname(const char *name)
59 {
60 	static struct	disklabel disk;
61 	struct	disklabel *dp = &disk;
62 	struct partition *pp;
63 	char	*buf;
64 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
65 	char	*cp, *cq;	/* can't be register */
66 	char	p, max, psize[3], pbsize[3],
67 		pfsize[3], poffset[3], ptype[3];
68 	u_int32_t *dx;
69 
70 	if (cgetent(&buf, db_array, (char *) name) < 0)
71 		return NULL;
72 
73 	bzero((char *)&disk, sizeof(disk));
74 	/*
75 	 * typename
76 	 */
77 	cq = dp->d_typename;
78 	cp = buf;
79 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
80 	    (*cq = *cp) && *cq != '|' && *cq != ':')
81 		cq++, cp++;
82 	*cq = '\0';
83 
84 	if (cgetstr(buf, "ty", &cq) > 0) {
85 		if (strcmp(cq, "removable") == 0)
86 			dp->d_flags |= D_REMOVABLE;
87 		else  if (cq && strcmp(cq, "simulated") == 0)
88 			dp->d_flags |= D_RAMDISK;
89 		free(cq);
90 	}
91 	if (cgetcap(buf, "sf", ':') != NULL)
92 		dp->d_flags |= D_BADSECT;
93 
94 #define getnumdflt(field, dname, dflt) \
95         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
96 
97 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
98 	getnumdflt(dp->d_ntracks, "nt", 0);
99 	getnumdflt(dp->d_nsectors, "ns", 0);
100 	getnumdflt(dp->d_ncylinders, "nc", 0);
101 
102 	if (cgetstr(buf, "dt", &cq) > 0) {
103 		dp->d_type = gettype(cq, dktypenames);
104 		free(cq);
105 	} else
106 		getnumdflt(dp->d_type, "dt", 0);
107 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
108 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
109 	getnumdflt(dp->d_rpm, "rm", 3600);
110 	getnumdflt(dp->d_interleave, "il", 1);
111 	getnumdflt(dp->d_trackskew, "sk", 0);
112 	getnumdflt(dp->d_cylskew, "cs", 0);
113 	getnumdflt(dp->d_headswitch, "hs", 0);
114 	getnumdflt(dp->d_trkseek, "ts", 0);
115 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
116 	getnumdflt(dp->d_sbsize, "sb", 0);
117 	strcpy(psize, "px");
118 	strcpy(pbsize, "bx");
119 	strcpy(pfsize, "fx");
120 	strcpy(poffset, "ox");
121 	strcpy(ptype, "tx");
122 	max = 'a' - 1;
123 	pp = &dp->d_partitions[0];
124 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
125 		long l;
126 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
127 		if (cgetnum(buf, psize, &l) == -1)
128 			pp->p_size = 0;
129 		else {
130 			pp->p_size = l;
131 			cgetnum(buf, poffset, &l);
132 			pp->p_offset = l;
133 			getnumdflt(pp->p_fsize, pfsize, 0);
134 			if (pp->p_fsize) {
135 				long bsize;
136 
137 				if (cgetnum(buf, pbsize, &bsize) == 0)
138 					pp->p_frag = bsize / pp->p_fsize;
139 				else
140 					pp->p_frag = 8;
141 			}
142 			getnumdflt(pp->p_fstype, ptype, 0);
143 			if (pp->p_fstype == 0)
144 				if (cgetstr(buf, ptype, &cq) >= 0) {
145 					pp->p_fstype = gettype(cq, fstypenames);
146 					free(cq);
147 				}
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