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