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