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