121417Sdist /* 235104Sbostic * Copyright (c) 1983, 1987 Regents of the University of California. 335104Sbostic * All rights reserved. 435104Sbostic * 5*56134Selan * Redistribution and use in source and binary forms, with or without 6*56134Selan * modification, are permitted provided that the following conditions 7*56134Selan * are met: 8*56134Selan * 1. Redistributions of source code must retain the above copyright 9*56134Selan * notice, this list of conditions and the following disclaimer. 10*56134Selan * 2. Redistributions in binary form must reproduce the above copyright 11*56134Selan * notice, this list of conditions and the following disclaimer in the 12*56134Selan * documentation and/or other materials provided with the distribution. 13*56134Selan * 3. All advertising materials mentioning features or use of this software 14*56134Selan * must display the following acknowledgement: 15*56134Selan * This product includes software developed by the University of 16*56134Selan * California, Berkeley and its contributors. 17*56134Selan * 4. Neither the name of the University nor the names of its contributors 18*56134Selan * may be used to endorse or promote products derived from this software 19*56134Selan * without specific prior written permission. 20*56134Selan * 21*56134Selan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22*56134Selan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23*56134Selan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24*56134Selan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25*56134Selan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26*56134Selan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27*56134Selan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28*56134Selan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29*56134Selan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30*56134Selan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*56134Selan * SUCH DAMAGE. 3221417Sdist */ 3310748Ssam 3426675Sdonn #if defined(LIBC_SCCS) && !defined(lint) 35*56134Selan static char sccsid[] = "@(#)disklabel.c 5.19 (Berkeley) 3/19/92"; 3635104Sbostic #endif /* LIBC_SCCS and not lint */ 3721417Sdist 3830420Skarels #include <sys/param.h> 3945646Sbostic #include <sys/errno.h> 4030420Skarels #include <sys/file.h> 4130420Skarels #define DKTYPENAMES 4230420Skarels #include <sys/disklabel.h> 4351531Sbostic #include <ufs/ffs/fs.h> 4410748Ssam #include <stdio.h> 4542022Sbostic #include <string.h> 4646597Sdonn #include <stdlib.h> 4746597Sdonn #include <unistd.h> 4810748Ssam 49*56134Selan static int error __P((int)); 50*56134Selan static int gettype __P((char *, char **)); 5110748Ssam 5230420Skarels struct disklabel * 5310748Ssam getdiskbyname(name) 5446597Sdonn const char *name; 5510748Ssam { 5633399Smarc static struct disklabel disk; 5730420Skarels register struct disklabel *dp = &disk; 5810760Ssam register struct partition *pp; 59*56134Selan char *buf; 60*56134Selan char *db_array[2] = { _PATH_DISKTAB, 0 }; 61*56134Selan char *cp, *cq; /* can't be register */ 6233399Smarc char p, max, psize[3], pbsize[3], 6333399Smarc pfsize[3], poffset[3], ptype[3]; 6433399Smarc u_long *dx; 6510748Ssam 66*56134Selan if (cgetent(&buf, db_array, (char *) name) < 0) 67*56134Selan return NULL; 68*56134Selan 6930420Skarels bzero((char *)&disk, sizeof(disk)); 7033399Smarc /* 7133399Smarc * typename 7233399Smarc */ 7330420Skarels cq = dp->d_typename; 7430420Skarels cp = buf; 7530420Skarels while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 7630420Skarels (*cq = *cp) && *cq != '|' && *cq != ':') 7730420Skarels cq++, cp++; 7830420Skarels *cq = '\0'; 7933399Smarc /* 8033399Smarc * boot name (optional) xxboot, bootxx 8133399Smarc */ 82*56134Selan cgetstr(buf, "b0", &dp->d_boot0); 83*56134Selan cgetstr(buf, "b1", &dp->d_boot1); 84*56134Selan 85*56134Selan if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) 8630420Skarels dp->d_flags |= D_REMOVABLE; 8730420Skarels else if (cq && strcmp(cq, "simulated") == 0) 8830420Skarels dp->d_flags |= D_RAMDISK; 89*56134Selan if (cgetcap(buf, "sf", ':') != NULL) 9030420Skarels dp->d_flags |= D_BADSECT; 9133399Smarc 9230420Skarels #define getnumdflt(field, dname, dflt) \ 93*56134Selan { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 9430420Skarels 9530420Skarels getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 96*56134Selan cgetnum(buf, "nt",(long *) &dp->d_ntracks); 97*56134Selan cgetnum(buf, "ns",(long *) &dp->d_nsectors); 98*56134Selan cgetnum(buf, "nc",(long *) &dp->d_ncylinders); 99*56134Selan 100*56134Selan if (cgetstr(buf, "dt", &cq) > 0) 10130420Skarels dp->d_type = gettype(cq, dktypenames); 10230420Skarels else 10330420Skarels getnumdflt(dp->d_type, "dt", 0); 10430420Skarels getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 10530420Skarels getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 10630420Skarels getnumdflt(dp->d_rpm, "rm", 3600); 10730420Skarels getnumdflt(dp->d_interleave, "il", 1); 10830420Skarels getnumdflt(dp->d_trackskew, "sk", 0); 10930420Skarels getnumdflt(dp->d_cylskew, "cs", 0); 11030420Skarels getnumdflt(dp->d_headswitch, "hs", 0); 11130420Skarels getnumdflt(dp->d_trkseek, "ts", 0); 11230420Skarels getnumdflt(dp->d_bbsize, "bs", BBSIZE); 11330420Skarels getnumdflt(dp->d_sbsize, "sb", SBSIZE); 11410760Ssam strcpy(psize, "px"); 11510760Ssam strcpy(pbsize, "bx"); 11610760Ssam strcpy(pfsize, "fx"); 11730420Skarels strcpy(poffset, "ox"); 11830420Skarels strcpy(ptype, "tx"); 11930420Skarels max = 'a' - 1; 12030420Skarels pp = &dp->d_partitions[0]; 12130420Skarels for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 12230420Skarels psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 123*56134Selan if (cgetnum(buf, psize,(long *) &pp->p_size) == -1) 12430420Skarels pp->p_size = 0; 12530420Skarels else { 126*56134Selan cgetnum(buf, poffset, (long *) &pp->p_offset); 12730420Skarels getnumdflt(pp->p_fsize, pfsize, 0); 128*56134Selan if (pp->p_fsize) { 129*56134Selan cgetnum(buf, pbsize, (long *) &pp->p_frag); 130*56134Selan pp->p_frag /= pp->p_fsize; 131*56134Selan } 13230420Skarels getnumdflt(pp->p_fstype, ptype, 0); 133*56134Selan if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 13430420Skarels pp->p_fstype = gettype(cq, fstypenames); 13530420Skarels max = p; 13630420Skarels } 13710748Ssam } 13830420Skarels dp->d_npartitions = max + 1 - 'a'; 13930681Sbostic (void)strcpy(psize, "dx"); 14030420Skarels dx = dp->d_drivedata; 14130420Skarels for (p = '0'; p < '0' + NDDATA; p++, dx++) { 14230420Skarels psize[1] = p; 14330420Skarels getnumdflt(*dx, psize, 0); 14430420Skarels } 14530420Skarels dp->d_magic = DISKMAGIC; 14630420Skarels dp->d_magic2 = DISKMAGIC; 147*56134Selan free(buf); 14810748Ssam return (dp); 14910748Ssam } 15010748Ssam 151*56134Selan static int 15230420Skarels gettype(t, names) 15330420Skarels char *t; 15430420Skarels char **names; 15530420Skarels { 15630420Skarels register char **nm; 15730420Skarels 15830420Skarels for (nm = names; *nm; nm++) 15933261Smckusick if (strcasecmp(t, *nm) == 0) 16030420Skarels return (nm - names); 16130420Skarels if (isdigit(*t)) 16230420Skarels return (atoi(t)); 16330420Skarels return (0); 16430420Skarels } 16530420Skarels 166*56134Selan static int 16745646Sbostic error(err) 16845646Sbostic int err; 16945646Sbostic { 17045646Sbostic char *p; 17145646Sbostic 17245646Sbostic (void)write(STDERR_FILENO, "disktab: ", 9); 17345646Sbostic (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1); 17453016Sbostic (void)write(STDERR_FILENO, ": ", 2); 17545646Sbostic p = strerror(err); 17645646Sbostic (void)write(STDERR_FILENO, p, strlen(p)); 17745646Sbostic (void)write(STDERR_FILENO, "\n", 1); 17845646Sbostic } 179