1 /* 2 * Copyright (c) 1983, 1987, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)disklabel.c 8.1 (Berkeley) 06/04/93"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #define DKTYPENAMES 14 #include <sys/disklabel.h> 15 #include <ufs/ffs/fs.h> 16 17 #include <errno.h> 18 #include <fcntl.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include <unistd.h> 23 24 static int error __P((int)); 25 static int gettype __P((char *, char **)); 26 27 struct disklabel * 28 getdiskbyname(name) 29 const char *name; 30 { 31 static struct disklabel disk; 32 register struct disklabel *dp = &disk; 33 register struct partition *pp; 34 char *buf; 35 char *db_array[2] = { _PATH_DISKTAB, 0 }; 36 char *cp, *cq; /* can't be register */ 37 char p, max, psize[3], pbsize[3], 38 pfsize[3], poffset[3], ptype[3]; 39 u_long *dx; 40 41 if (cgetent(&buf, db_array, (char *) name) < 0) 42 return NULL; 43 44 bzero((char *)&disk, sizeof(disk)); 45 /* 46 * typename 47 */ 48 cq = dp->d_typename; 49 cp = buf; 50 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 51 (*cq = *cp) && *cq != '|' && *cq != ':') 52 cq++, cp++; 53 *cq = '\0'; 54 /* 55 * boot name (optional) xxboot, bootxx 56 */ 57 cgetstr(buf, "b0", &dp->d_boot0); 58 cgetstr(buf, "b1", &dp->d_boot1); 59 60 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) 61 dp->d_flags |= D_REMOVABLE; 62 else if (cq && strcmp(cq, "simulated") == 0) 63 dp->d_flags |= D_RAMDISK; 64 if (cgetcap(buf, "sf", ':') != NULL) 65 dp->d_flags |= D_BADSECT; 66 67 #define getnumdflt(field, dname, dflt) \ 68 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 69 70 getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 71 cgetnum(buf, "nt",(long *) &dp->d_ntracks); 72 cgetnum(buf, "ns",(long *) &dp->d_nsectors); 73 cgetnum(buf, "nc",(long *) &dp->d_ncylinders); 74 75 if (cgetstr(buf, "dt", &cq) > 0) 76 dp->d_type = gettype(cq, dktypenames); 77 else 78 getnumdflt(dp->d_type, "dt", 0); 79 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 80 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 81 getnumdflt(dp->d_rpm, "rm", 3600); 82 getnumdflt(dp->d_interleave, "il", 1); 83 getnumdflt(dp->d_trackskew, "sk", 0); 84 getnumdflt(dp->d_cylskew, "cs", 0); 85 getnumdflt(dp->d_headswitch, "hs", 0); 86 getnumdflt(dp->d_trkseek, "ts", 0); 87 getnumdflt(dp->d_bbsize, "bs", BBSIZE); 88 getnumdflt(dp->d_sbsize, "sb", SBSIZE); 89 strcpy(psize, "px"); 90 strcpy(pbsize, "bx"); 91 strcpy(pfsize, "fx"); 92 strcpy(poffset, "ox"); 93 strcpy(ptype, "tx"); 94 max = 'a' - 1; 95 pp = &dp->d_partitions[0]; 96 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 97 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 98 if (cgetnum(buf, psize,(long *) &pp->p_size) == -1) 99 pp->p_size = 0; 100 else { 101 cgetnum(buf, poffset, (long *) &pp->p_offset); 102 getnumdflt(pp->p_fsize, pfsize, 0); 103 if (pp->p_fsize) { 104 long bsize; 105 106 if (cgetnum(buf, pbsize, &bsize) == 0) 107 pp->p_frag = bsize / pp->p_fsize; 108 else 109 pp->p_frag = 8; 110 } 111 getnumdflt(pp->p_fstype, ptype, 0); 112 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 113 pp->p_fstype = gettype(cq, fstypenames); 114 max = p; 115 } 116 } 117 dp->d_npartitions = max + 1 - 'a'; 118 (void)strcpy(psize, "dx"); 119 dx = dp->d_drivedata; 120 for (p = '0'; p < '0' + NDDATA; p++, dx++) { 121 psize[1] = p; 122 getnumdflt(*dx, psize, 0); 123 } 124 dp->d_magic = DISKMAGIC; 125 dp->d_magic2 = DISKMAGIC; 126 free(buf); 127 return (dp); 128 } 129 130 static int 131 gettype(t, names) 132 char *t; 133 char **names; 134 { 135 register char **nm; 136 137 for (nm = names; *nm; nm++) 138 if (strcasecmp(t, *nm) == 0) 139 return (nm - names); 140 if (isdigit(*t)) 141 return (atoi(t)); 142 return (0); 143 } 144 145 static int 146 error(err) 147 int err; 148 { 149 char *p; 150 151 (void)write(STDERR_FILENO, "disktab: ", 9); 152 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1); 153 (void)write(STDERR_FILENO, ": ", 2); 154 p = strerror(err); 155 (void)write(STDERR_FILENO, p, strlen(p)); 156 (void)write(STDERR_FILENO, "\n", 1); 157 } 158