disklabel.c revision 39684
1100384Speter/* 2100384Speter * Copyright (c) 1983, 1987, 1993 3100384Speter * The Regents of the University of California. All rights reserved. 4100384Speter * 5100384Speter * Redistribution and use in source and binary forms, with or without 6100384Speter * modification, are permitted provided that the following conditions 7100384Speter * are met: 8100384Speter * 1. Redistributions of source code must retain the above copyright 9100384Speter * notice, this list of conditions and the following disclaimer. 10100384Speter * 2. Redistributions in binary form must reproduce the above copyright 11100384Speter * notice, this list of conditions and the following disclaimer in the 12100384Speter * documentation and/or other materials provided with the distribution. 13100384Speter * 3. All advertising materials mentioning features or use of this software 14100384Speter * must display the following acknowledgement: 15100384Speter * This product includes software developed by the University of 16100384Speter * California, Berkeley and its contributors. 17100384Speter * 4. Neither the name of the University nor the names of its contributors 18100384Speter * may be used to endorse or promote products derived from this software 19100384Speter * without specific prior written permission. 20100384Speter * 21100384Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22100384Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23100384Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24100384Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25100384Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26100384Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27118031Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28118031Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29118031Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30104738Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31104738Speter * SUCH DAMAGE. 32100384Speter */ 33100384Speter 34100384Speter#ifndef lint 35162954Sphk#if 0 36100384Speterstatic char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; 37100384Speter#endif 38100384Speterstatic const char rcsid[] = 39123746Speter "$Id: disklabel.c,v 1.6 1997/03/11 11:52:25 peter Exp $"; 40100384Speter#endif /* not lint */ 41100384Speter 42161343Sjkim#include <sys/param.h> 43100384Speter#define DKTYPENAMES 44100384Speter#include <sys/disklabel.h> 45100384Speter#include <ufs/ufs/dinode.h> 46151909Sps#include <ufs/ffs/fs.h> 47100384Speter 48100384Speter#include <errno.h> 49100384Speter#include <fcntl.h> 50100384Speter#include <stdio.h> 51100384Speter#include <stdlib.h> 52100384Speter#include <string.h> 53100384Speter#include <unistd.h> 54100384Speter#include <ctype.h> 55100384Speter 56146950Spsstatic int gettype __P((char *, char **)); 57100384Speter 58100384Speterstruct disklabel * 59100384Spetergetdiskbyname(name) 60100384Speter const char *name; 61100384Speter{ 62100384Speter static struct disklabel disk; 63150883Sjhb register struct disklabel *dp = &disk; 64113859Sjhb register struct partition *pp; 65100384Speter char *buf; 66100384Speter char *db_array[2] = { _PATH_DISKTAB, 0 }; 67100384Speter char *cp, *cq; /* can't be register */ 68162551Sdavidxu char p, max, psize[3], pbsize[3], 69100384Speter pfsize[3], poffset[3], ptype[3]; 70162551Sdavidxu u_int32_t *dx; 71100384Speter 72127140Sjhb if (cgetent(&buf, db_array, (char *) name) < 0) 73157285Sps return NULL; 74174381Sjhb 75174381Sjhb bzero((char *)&disk, sizeof(disk)); 76157285Sps /* 77100384Speter * typename 78100384Speter */ 79100384Speter cq = dp->d_typename; 80100384Speter cp = buf; 81100384Speter while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 82100384Speter (*cq = *cp) && *cq != '|' && *cq != ':') 83100384Speter cq++, cp++; 84100384Speter *cq = '\0'; 85100384Speter /* 86151582Sps * boot name (optional) xxboot, bootxx 87151582Sps */ 88119333Speter cgetstr(buf, "b0", &dp->d_boot0); 89119333Speter cgetstr(buf, "b1", &dp->d_boot1); 90174380Sjhb 91163018Sdavidxu if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) 92119333Speter dp->d_flags |= D_REMOVABLE; 93100384Speter else if (cq && strcmp(cq, "simulated") == 0) 94121719Speter dp->d_flags |= D_RAMDISK; 95121719Speter if (cgetcap(buf, "sf", ':') != NULL) 96174377Sjhb dp->d_flags |= D_BADSECT; 97121719Speter 98121719Speter#define getnumdflt(field, dname, dflt) \ 99174377Sjhb { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 100174377Sjhb 101174377Sjhb getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 102174377Sjhb getnumdflt(dp->d_ntracks, "nt", 0); 103174377Sjhb getnumdflt(dp->d_nsectors, "ns", 0); 104174377Sjhb getnumdflt(dp->d_ncylinders, "nc", 0); 105121719Speter 106174377Sjhb if (cgetstr(buf, "dt", &cq) > 0) 107174377Sjhb dp->d_type = gettype(cq, dktypenames); 108174377Sjhb else 109100384Speter getnumdflt(dp->d_type, "dt", 0); 110119333Speter getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 111100384Speter getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 112127140Sjhb getnumdflt(dp->d_rpm, "rm", 3600); 113127140Sjhb getnumdflt(dp->d_interleave, "il", 1); 114136152Sjhb getnumdflt(dp->d_trackskew, "sk", 0); 115100384Speter getnumdflt(dp->d_cylskew, "cs", 0); 116136152Sjhb getnumdflt(dp->d_headswitch, "hs", 0); 117136152Sjhb getnumdflt(dp->d_trkseek, "ts", 0); 118136152Sjhb getnumdflt(dp->d_bbsize, "bs", BBSIZE); 119136152Sjhb getnumdflt(dp->d_sbsize, "sb", SBSIZE); 120136152Sjhb strcpy(psize, "px"); 121100384Speter strcpy(pbsize, "bx"); 122100384Speter strcpy(pfsize, "fx"); 123127140Sjhb strcpy(poffset, "ox"); 124127140Sjhb strcpy(ptype, "tx"); 125127140Sjhb max = 'a' - 1; 126100384Speter pp = &dp->d_partitions[0]; 127100384Speter for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 128100384Speter long l; 129100384Speter psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 130100384Speter if (cgetnum(buf, psize, &l) == -1) 131100384Speter pp->p_size = 0; 132100384Speter else { 133100384Speter pp->p_size = l; 134100384Speter cgetnum(buf, poffset, &l); 135100384Speter pp->p_offset = l; 136100384Speter getnumdflt(pp->p_fsize, pfsize, 0); 137100384Speter if (pp->p_fsize) { 138100384Speter long bsize; 139100384Speter 140100384Speter if (cgetnum(buf, pbsize, &bsize) == 0) 141100384Speter pp->p_frag = bsize / pp->p_fsize; 142127140Sjhb else 143100384Speter pp->p_frag = 8; 144100384Speter } 145100384Speter getnumdflt(pp->p_fstype, ptype, 0); 146100384Speter if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 147128597Smarcel pp->p_fstype = gettype(cq, fstypenames); 148174526Sjhb max = p; 149100384Speter } 150100384Speter } 151172003Sjhb dp->d_npartitions = max + 1 - 'a'; 152174424Sscottl (void)strcpy(psize, "dx"); 153156266Sps dx = dp->d_drivedata; 154100384Speter for (p = '0'; p < '0' + NDDATA; p++, dx++) { 155172003Sjhb psize[1] = p; 156100384Speter getnumdflt(*dx, psize, 0); 157100384Speter } 158100384Speter dp->d_magic = DISKMAGIC; 159172003Sjhb dp->d_magic2 = DISKMAGIC; 160174526Sjhb free(buf); 161100384Speter return (dp); 162100384Speter} 163100384Speter 164100384Speterstatic int 165174526Sjhbgettype(t, names) 166174526Sjhb char *t; 167156266Sps char **names; 168156266Sps{ 169156266Sps register char **nm; 170156266Sps 171174526Sjhb for (nm = names; *nm; nm++) 172174526Sjhb if (strcasecmp(t, *nm) == 0) 173156266Sps return (nm - names); 174156266Sps if (isdigit(*t)) 175100384Speter return (atoi(t)); 176128597Smarcel return (0); 177100384Speter} 178128597Smarcel