Home | History | Annotate | Line # | Download | only in gen
disklabel.c revision 1.31
      1 /*	$NetBSD: disklabel.c,v 1.31 2004/04/23 14:47:23 christos Exp $	*/
      2 
      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 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
     36 #else
     37 __RCSID("$NetBSD: disklabel.c,v 1.31 2004/04/23 14:47:23 christos Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include <sys/param.h>
     43 #define DKTYPENAMES
     44 #define FSTYPENAMES
     45 #include <sys/disklabel.h>
     46 #include <ufs/ufs/dinode.h>
     47 #include <ufs/ffs/fs.h>
     48 
     49 #include <assert.h>
     50 #include <ctype.h>
     51 #include <errno.h>
     52 #include <fcntl.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 #include <disktab.h>
     59 
     60 #ifdef __weak_alias
     61 __weak_alias(getdiskbyname,_getdiskbyname)
     62 #endif
     63 
     64 #if 0
     65 static void	error __P((int));
     66 #endif
     67 static int	gettype __P((char *, const char *const *));
     68 
     69 static const char *db_array[2] = { _PATH_DISKTAB, 0 };
     70 
     71 int
     72 setdisktab(name)
     73 	const char *name;
     74 {
     75 	if (!name || !*name)
     76 		return -1;
     77 
     78 	db_array[0] = name;
     79 	return 0;
     80 }
     81 
     82 
     83 struct disklabel *
     84 getdiskbyname(name)
     85 	const char *name;
     86 {
     87 	static struct	disklabel disk;
     88 	struct	disklabel *dp = &disk;
     89 	struct partition *pp;
     90 	char	*buf;
     91 	char	*cp, *cq;	/* can't be */
     92 	char	p, max, psize[3], pbsize[3],
     93 		pfsize[3], poffset[3], ptype[3];
     94 	u_int32_t *dx;
     95 	long f;
     96 
     97 	_DIAGASSERT(name != NULL);
     98 
     99 	if (cgetent(&buf, db_array, name) < 0)
    100 		return NULL;
    101 
    102 	memset(&disk, 0, sizeof(disk));
    103 	/*
    104 	 * typename
    105 	 */
    106 	cq = dp->d_typename;
    107 	cp = buf;
    108 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
    109 	    (*cq = *cp) && *cq != '|' && *cq != ':')
    110 		cq++, cp++;
    111 	*cq = '\0';
    112 	/*
    113 	 * boot name (optional)  xxboot, bootxx
    114 	 */
    115 	cgetstr(buf, "b0", &dp->d_boot0);
    116 	cgetstr(buf, "b1", &dp->d_boot1);
    117 
    118 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
    119 		dp->d_flags |= D_REMOVABLE;
    120 	else  if (cq && strcmp(cq, "simulated") == 0)
    121 		dp->d_flags |= D_RAMDISK;
    122 	if (cgetcap(buf, "sf", ':') != NULL)
    123 		dp->d_flags |= D_BADSECT;
    124 
    125 #define getnumdflt(field, dname, dflt) \
    126     (field) = ((cgetnum(buf, dname, &f) == -1) ? (dflt) : (u_int32_t) f)
    127 #define	getnum(field, dname) \
    128 	if (cgetnum(buf, dname, &f) != -1) field = (u_int32_t)f
    129 
    130 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
    131 	getnum(dp->d_ntracks, "nt");
    132 	getnum(dp->d_nsectors, "ns");
    133 	getnum(dp->d_ncylinders, "nc");
    134 
    135 	if (cgetstr(buf, "dt", &cq) > 0)
    136 		dp->d_type = gettype(cq, dktypenames);
    137 	else
    138 		getnumdflt(dp->d_type, "dt", 0);
    139 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
    140 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
    141 	getnumdflt(dp->d_rpm, "rm", 3600);
    142 	getnumdflt(dp->d_interleave, "il", 1);
    143 	getnumdflt(dp->d_trackskew, "sk", 0);
    144 	getnumdflt(dp->d_cylskew, "cs", 0);
    145 	getnumdflt(dp->d_headswitch, "hs", 0);
    146 	getnumdflt(dp->d_trkseek, "ts", 0);
    147 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
    148 	getnumdflt(dp->d_sbsize, "sb", SBLOCKSIZE);
    149 	strcpy(psize, "px");	/* XXX: strcpy is safe */
    150 	strcpy(pbsize, "bx");	/* XXX: strcpy is safe */
    151 	strcpy(pfsize, "fx");	/* XXX: strcpy is safe */
    152 	strcpy(poffset, "ox");	/* XXX: strcpy is safe */
    153 	strcpy(ptype, "tx");	/* XXX: strcpy is safe */
    154 	max = 'a' - 1;
    155 	pp = &dp->d_partitions[0];
    156 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
    157 		long ff;
    158 
    159 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
    160 		if (cgetnum(buf, psize, &ff) == -1)
    161 			pp->p_size = 0;
    162 		else {
    163 			pp->p_size = (u_int32_t)ff;
    164 			getnum(pp->p_offset, poffset);
    165 			getnumdflt(pp->p_fsize, pfsize, 0);
    166 			if (pp->p_fsize) {
    167 				long bsize;
    168 
    169 				if (cgetnum(buf, pbsize, &bsize) == -1)
    170 					pp->p_frag = 8;
    171 				else
    172 					pp->p_frag =
    173 					    (u_int8_t)(bsize / pp->p_fsize);
    174 			}
    175 			getnumdflt(pp->p_fstype, ptype, 0);
    176 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
    177 				pp->p_fstype = gettype(cq, fstypenames);
    178 			max = p;
    179 		}
    180 	}
    181 	dp->d_npartitions = max + 1 - 'a';
    182 	strcpy(psize, "dx");	/* XXX: strcpy is safe */
    183 	dx = dp->d_drivedata;
    184 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
    185 		psize[1] = p;
    186 		getnumdflt(*dx, psize, 0);
    187 	}
    188 	dp->d_magic = DISKMAGIC;
    189 	dp->d_magic2 = DISKMAGIC;
    190 	free(buf);
    191 	return (dp);
    192 }
    193 
    194 static int
    195 gettype(t, names)
    196 	char *t;
    197 	const char *const *names;
    198 {
    199 	const char *const *nm;
    200 
    201 	_DIAGASSERT(t != NULL);
    202 	_DIAGASSERT(names != NULL);
    203 
    204 	for (nm = names; *nm; nm++)
    205 		if (strcasecmp(t, *nm) == 0)
    206 			return (nm - names);
    207 	if (isdigit((unsigned char) *t))
    208 		return (atoi(t));
    209 	return (0);
    210 }
    211 
    212 #if 0
    213 static void
    214 error(err)
    215 	int err;
    216 {
    217 	char *p;
    218 
    219 	(void)write(STDERR_FILENO, "disktab: ", 9);
    220 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
    221 	(void)write(STDERR_FILENO, ": ", 2);
    222 	p = strerror(err);
    223 	(void)write(STDERR_FILENO, p, strlen(p));
    224 	(void)write(STDERR_FILENO, "\n", 1);
    225 }
    226 #endif
    227