1 /* 2 * Copyright (c) 1983, 1987, 1993 3 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #define DKTYPENAMES 32 #include <sys/disklabel.h> 33 #include <ufs/ffs/fs.h> 34 35 #include <ctype.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <limits.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 static u_int gettype(char *, char **); 45 46 struct disklabel * 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 bzero((char *)&disk, 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 (cgetcap(buf, "sf", ':') != NULL) 79 dp->d_flags |= D_BADSECT; 80 81 #define getnumdflt(field, dname, dflt) \ 82 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 83 #define getnum(field, dname) \ 84 { long f; cgetnum(buf, dname, &f); field = f; } 85 86 getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 87 getnum(dp->d_ntracks, "nt"); 88 getnum(dp->d_nsectors, "ns"); 89 getnum(dp->d_ncylinders, "nc"); 90 91 if (cgetstr(buf, "dt", &cq) > 0) 92 dp->d_type = (u_short)gettype(cq, dktypenames); 93 else 94 getnumdflt(dp->d_type, "dt", 0); 95 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 96 /* XXX */ 97 dp->d_secperunith = 0; 98 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 99 getnumdflt(dp->d_bbsize, "bs", BBSIZE); 100 getnumdflt(dp->d_sbsize, "sb", SBSIZE); 101 strlcpy(psize, "px", sizeof psize); 102 strlcpy(pbsize, "bx", sizeof pbsize); 103 strlcpy(pfsize, "fx", sizeof pfsize); 104 strlcpy(poffset, "ox", sizeof poffset); 105 strlcpy(ptype, "tx", sizeof ptype); 106 max = 'a' - 1; 107 pp = &dp->d_partitions[0]; 108 dp->d_version = 1; 109 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 110 long f; 111 112 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 113 /* XXX */ 114 if (cgetnum(buf, psize, &f) == -1) 115 DL_SETPSIZE(pp, 0); 116 else { 117 u_int32_t fsize, frag = 8; 118 119 DL_SETPSIZE(pp, f); 120 /* XXX */ 121 pp->p_offseth = 0; 122 getnum(pp->p_offset, poffset); 123 getnumdflt(fsize, pfsize, 0); 124 if (fsize) { 125 long bsize; 126 127 if (cgetnum(buf, pbsize, &bsize) == 0) 128 frag = bsize / fsize; 129 pp->p_fragblock = 130 DISKLABELV1_FFS_FRAGBLOCK(fsize, frag); 131 } 132 getnumdflt(pp->p_fstype, ptype, 0); 133 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 134 pp->p_fstype = (u_char)gettype(cq, fstypenames); 135 max = p; 136 } 137 } 138 dp->d_npartitions = max + 1 - 'a'; 139 (void)strlcpy(psize, "dx", sizeof psize); 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 u_int 152 gettype(char *t, char **names) 153 { 154 char **nm; 155 156 for (nm = names; *nm; nm++) 157 if (strcasecmp(t, *nm) == 0) 158 return (nm - names); 159 if (isdigit((u_char)*t)) 160 return ((u_int)strtonum(t, 0, USHRT_MAX, NULL)); 161 return (0); 162 } 163