1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #if defined(LIBC_SCCS) && !defined(lint) 33 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; 34 #endif /* LIBC_SCCS and not lint */ 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #define DKTYPENAMES 40 #define FSTYPENAMES 41 #include <sys/disklabel.h> 42 43 #include <fcntl.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <ctype.h> 49 50 static int 51 gettype(char *t, const char **names) 52 { 53 const char **nm; 54 55 for (nm = names; *nm; nm++) 56 if (strcasecmp(t, *nm) == 0) 57 return (nm - names); 58 if (isdigit((unsigned char)*t)) 59 return (atoi(t)); 60 return (0); 61 } 62 63 struct disklabel * 64 getdiskbyname(const char *name) 65 { 66 static struct disklabel disk; 67 struct disklabel *dp = &disk; 68 struct partition *pp; 69 char *buf; 70 char *db_array[2] = { _PATH_DISKTAB, 0 }; 71 char *cp, *cq; /* can't be register */ 72 char p, max, psize[3], pbsize[3], 73 pfsize[3], poffset[3], ptype[3]; 74 u_int32_t *dx; 75 76 if (cgetent(&buf, db_array, (char *) name) < 0) 77 return NULL; 78 79 bzero((char *)&disk, sizeof(disk)); 80 /* 81 * typename 82 */ 83 cq = dp->d_typename; 84 cp = buf; 85 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 86 (*cq = *cp) && *cq != '|' && *cq != ':') 87 cq++, cp++; 88 *cq = '\0'; 89 90 if (cgetstr(buf, "ty", &cq) > 0) { 91 if (strcmp(cq, "removable") == 0) 92 dp->d_flags |= D_REMOVABLE; 93 else if (cq && strcmp(cq, "simulated") == 0) 94 dp->d_flags |= D_RAMDISK; 95 free(cq); 96 } 97 if (cgetcap(buf, "sf", ':') != NULL) 98 dp->d_flags |= D_BADSECT; 99 100 #define getnumdflt(field, dname, dflt) \ 101 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 102 103 getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 104 getnumdflt(dp->d_ntracks, "nt", 0); 105 getnumdflt(dp->d_nsectors, "ns", 0); 106 getnumdflt(dp->d_ncylinders, "nc", 0); 107 108 if (cgetstr(buf, "dt", &cq) > 0) { 109 dp->d_type = gettype(cq, dktypenames); 110 free(cq); 111 } else 112 getnumdflt(dp->d_type, "dt", 0); 113 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 114 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 115 getnumdflt(dp->d_rpm, "rm", 3600); 116 getnumdflt(dp->d_interleave, "il", 1); 117 getnumdflt(dp->d_trackskew, "sk", 0); 118 getnumdflt(dp->d_cylskew, "cs", 0); 119 getnumdflt(dp->d_headswitch, "hs", 0); 120 getnumdflt(dp->d_trkseek, "ts", 0); 121 getnumdflt(dp->d_bbsize, "bs", BBSIZE); 122 getnumdflt(dp->d_sbsize, "sb", 0); 123 strcpy(psize, "px"); 124 strcpy(pbsize, "bx"); 125 strcpy(pfsize, "fx"); 126 strcpy(poffset, "ox"); 127 strcpy(ptype, "tx"); 128 max = 'a' - 1; 129 pp = &dp->d_partitions[0]; 130 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 131 long l; 132 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 133 if (cgetnum(buf, psize, &l) == -1) 134 pp->p_size = 0; 135 else { 136 pp->p_size = l; 137 cgetnum(buf, poffset, &l); 138 pp->p_offset = l; 139 getnumdflt(pp->p_fsize, pfsize, 0); 140 if (pp->p_fsize) { 141 long bsize; 142 143 if (cgetnum(buf, pbsize, &bsize) == 0) 144 pp->p_frag = bsize / pp->p_fsize; 145 else 146 pp->p_frag = 8; 147 } 148 getnumdflt(pp->p_fstype, ptype, 0); 149 if (pp->p_fstype == 0) 150 if (cgetstr(buf, ptype, &cq) >= 0) { 151 pp->p_fstype = gettype(cq, fstypenames); 152 free(cq); 153 } 154 max = p; 155 } 156 } 157 dp->d_npartitions = max + 1 - 'a'; 158 (void)strcpy(psize, "dx"); 159 dx = dp->d_drivedata; 160 for (p = '0'; p < '0' + NDDATA; p++, dx++) { 161 psize[1] = p; 162 getnumdflt(*dx, psize, 0); 163 } 164 dp->d_magic = DISKMAGIC; 165 dp->d_magic2 = DISKMAGIC; 166 free(buf); 167 return (dp); 168 } 169