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.21 (Berkeley) 09/14/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 cgetnum(buf, pbsize, (long *) &pp->p_frag); 104 pp->p_frag /= pp->p_fsize; 105 } 106 getnumdflt(pp->p_fstype, ptype, 0); 107 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 108 pp->p_fstype = gettype(cq, fstypenames); 109 max = p; 110 } 111 } 112 dp->d_npartitions = max + 1 - 'a'; 113 (void)strcpy(psize, "dx"); 114 dx = dp->d_drivedata; 115 for (p = '0'; p < '0' + NDDATA; p++, dx++) { 116 psize[1] = p; 117 getnumdflt(*dx, psize, 0); 118 } 119 dp->d_magic = DISKMAGIC; 120 dp->d_magic2 = DISKMAGIC; 121 free(buf); 122 return (dp); 123 } 124 125 static int 126 gettype(t, names) 127 char *t; 128 char **names; 129 { 130 register char **nm; 131 132 for (nm = names; *nm; nm++) 133 if (strcasecmp(t, *nm) == 0) 134 return (nm - names); 135 if (isdigit(*t)) 136 return (atoi(t)); 137 return (0); 138 } 139 140 static int 141 error(err) 142 int err; 143 { 144 char *p; 145 146 (void)write(STDERR_FILENO, "disktab: ", 9); 147 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1); 148 (void)write(STDERR_FILENO, ": ", 2); 149 p = strerror(err); 150 (void)write(STDERR_FILENO, p, strlen(p)); 151 (void)write(STDERR_FILENO, "\n", 1); 152 } 153