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