1 /* $NetBSD: disklabel.c,v 1.23 1999/01/19 06:24:08 abs 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; 40 #else 41 __RCSID("$NetBSD: disklabel.c,v 1.23 1999/01/19 06:24:08 abs Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 #include <sys/param.h> 47 #define DKTYPENAMES 48 #define FSTYPENAMES 49 #include <sys/disklabel.h> 50 #include <ufs/ufs/dinode.h> 51 #include <ufs/ffs/fs.h> 52 53 #include <ctype.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include <disktab.h> 62 63 #ifdef __weak_alias 64 __weak_alias(getdiskbyname,_getdiskbyname); 65 #endif 66 67 #if 0 68 static void error __P((int)); 69 #endif 70 static int gettype __P((char *, const char *const *)); 71 72 static char *db_array[2] = { _PATH_DISKTAB, 0 }; 73 74 int 75 setdisktab(name) 76 char *name; 77 { 78 if (!name || !*name) 79 return -1; 80 81 db_array[0] = name; 82 return 0; 83 } 84 85 86 struct disklabel * 87 getdiskbyname(name) 88 const char *name; 89 { 90 static struct disklabel disk; 91 struct disklabel *dp = &disk; 92 struct partition *pp; 93 char *buf; 94 char *cp, *cq; /* can't be */ 95 char p, max, psize[3], pbsize[3], 96 pfsize[3], poffset[3], ptype[3]; 97 u_int32_t *dx; 98 long f; 99 100 if (cgetent(&buf, db_array, name) < 0) 101 return NULL; 102 103 memset(&disk, 0, sizeof(disk)); 104 /* 105 * typename 106 */ 107 cq = dp->d_typename; 108 cp = buf; 109 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 110 (*cq = *cp) && *cq != '|' && *cq != ':') 111 cq++, cp++; 112 *cq = '\0'; 113 /* 114 * boot name (optional) xxboot, bootxx 115 */ 116 cgetstr(buf, "b0", &dp->d_boot0); 117 cgetstr(buf, "b1", &dp->d_boot1); 118 119 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) 120 dp->d_flags |= D_REMOVABLE; 121 else if (cq && strcmp(cq, "simulated") == 0) 122 dp->d_flags |= D_RAMDISK; 123 if (cgetcap(buf, "sf", ':') != NULL) 124 dp->d_flags |= D_BADSECT; 125 126 #define getnumdflt(field, dname, dflt) \ 127 (field) = (u_int32_t) ((cgetnum(buf, dname, &f) == -1) ? (dflt) : f) 128 #define getnum(field, dname) \ 129 if (cgetnum(buf, dname, &f) != -1) field = (u_int32_t)f 130 131 getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 132 getnum(dp->d_ntracks, "nt"); 133 getnum(dp->d_nsectors, "ns"); 134 getnum(dp->d_ncylinders, "nc"); 135 136 if (cgetstr(buf, "dt", &cq) > 0) 137 dp->d_type = gettype(cq, dktypenames); 138 else 139 getnumdflt(dp->d_type, "dt", 0); 140 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 141 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 142 getnumdflt(dp->d_rpm, "rm", 3600); 143 getnumdflt(dp->d_interleave, "il", 1); 144 getnumdflt(dp->d_trackskew, "sk", 0); 145 getnumdflt(dp->d_cylskew, "cs", 0); 146 getnumdflt(dp->d_headswitch, "hs", 0); 147 getnumdflt(dp->d_trkseek, "ts", 0); 148 getnumdflt(dp->d_bbsize, "bs", BBSIZE); 149 getnumdflt(dp->d_sbsize, "sb", SBSIZE); 150 strcpy(psize, "px"); /* XXX: strcpy is safe */ 151 strcpy(pbsize, "bx"); /* XXX: strcpy is safe */ 152 strcpy(pfsize, "fx"); /* XXX: strcpy is safe */ 153 strcpy(poffset, "ox"); /* XXX: strcpy is safe */ 154 strcpy(ptype, "tx"); /* XXX: strcpy is safe */ 155 max = 'a' - 1; 156 pp = &dp->d_partitions[0]; 157 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 158 long ff; 159 160 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 161 if (cgetnum(buf, psize, &ff) == -1) 162 pp->p_size = 0; 163 else { 164 pp->p_size = (u_int32_t)ff; 165 getnum(pp->p_offset, poffset); 166 getnumdflt(pp->p_fsize, pfsize, 0); 167 if (pp->p_fsize) { 168 long bsize; 169 170 if (cgetnum(buf, pbsize, &bsize) == -1) 171 pp->p_frag = 8; 172 else 173 pp->p_frag = 174 (u_int8_t)(bsize / pp->p_fsize); 175 } 176 getnumdflt(pp->p_fstype, ptype, 0); 177 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 178 pp->p_fstype = gettype(cq, fstypenames); 179 max = p; 180 } 181 } 182 dp->d_npartitions = max + 1 - 'a'; 183 strcpy(psize, "dx"); /* XXX: strcpy is safe */ 184 dx = dp->d_drivedata; 185 for (p = '0'; p < '0' + NDDATA; p++, dx++) { 186 psize[1] = p; 187 getnumdflt(*dx, psize, 0); 188 } 189 dp->d_magic = DISKMAGIC; 190 dp->d_magic2 = DISKMAGIC; 191 free(buf); 192 return (dp); 193 } 194 195 static int 196 gettype(t, names) 197 char *t; 198 const char *const *names; 199 { 200 const char *const *nm; 201 202 for (nm = names; *nm; nm++) 203 if (strcasecmp(t, *nm) == 0) 204 return (nm - names); 205 if (isdigit(*t)) 206 return (atoi(t)); 207 return (0); 208 } 209 210 #if 0 211 static void 212 error(err) 213 int err; 214 { 215 char *p; 216 217 (void)write(STDERR_FILENO, "disktab: ", 9); 218 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1); 219 (void)write(STDERR_FILENO, ": ", 2); 220 p = strerror(err); 221 (void)write(STDERR_FILENO, p, strlen(p)); 222 (void)write(STDERR_FILENO, "\n", 1); 223 } 224 #endif 225