121417Sdist /* 235104Sbostic * Copyright (c) 1983, 1987 Regents of the University of California. 335104Sbostic * All rights reserved. 435104Sbostic * 5*42622Sbostic * %sccs.include.redist.c% 621417Sdist */ 710748Ssam 826675Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*42622Sbostic static char sccsid[] = "@(#)disklabel.c 5.13 (Berkeley) 06/01/90"; 1035104Sbostic #endif /* LIBC_SCCS and not lint */ 1121417Sdist 1230420Skarels #include <sys/param.h> 1338498Sbostic #include <ufs/fs.h> 1430420Skarels #include <sys/file.h> 1530420Skarels #define DKTYPENAMES 1630420Skarels #include <sys/disklabel.h> 1710748Ssam #include <stdio.h> 1842022Sbostic #include <string.h> 1910748Ssam 2010748Ssam static char *dgetstr(); 2110748Ssam 2230420Skarels struct disklabel * 2310748Ssam getdiskbyname(name) 2410748Ssam char *name; 2510748Ssam { 2633399Smarc static struct disklabel disk; 2733399Smarc static char boot[BUFSIZ]; 2833399Smarc char localbuf[BUFSIZ]; 2933399Smarc char buf[BUFSIZ]; 3033399Smarc char *cp, *cq; /* can't be register */ 3130420Skarels register struct disklabel *dp = &disk; 3210760Ssam register struct partition *pp; 3333399Smarc char p, max, psize[3], pbsize[3], 3433399Smarc pfsize[3], poffset[3], ptype[3]; 3533399Smarc u_long *dx; 3610748Ssam 3710748Ssam if (dgetent(buf, name) <= 0) 3830420Skarels return ((struct disklabel *)0); 3930420Skarels bzero((char *)&disk, sizeof(disk)); 4033399Smarc /* 4133399Smarc * typename 4233399Smarc */ 4330420Skarels cq = dp->d_typename; 4430420Skarels cp = buf; 4530420Skarels while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 4630420Skarels (*cq = *cp) && *cq != '|' && *cq != ':') 4730420Skarels cq++, cp++; 4830420Skarels *cq = '\0'; 4933399Smarc /* 5033399Smarc * boot name (optional) xxboot, bootxx 5133399Smarc */ 5233399Smarc cp = boot; 5333399Smarc dp->d_boot0 = dgetstr("b0", &cp); 5433399Smarc dp->d_boot1 = dgetstr("b1", &cp); 5530420Skarels cp = localbuf; 5630420Skarels cq = dgetstr("ty", &cp); 5730420Skarels if (cq && strcmp(cq, "removable") == 0) 5830420Skarels dp->d_flags |= D_REMOVABLE; 5930420Skarels else if (cq && strcmp(cq, "simulated") == 0) 6030420Skarels dp->d_flags |= D_RAMDISK; 6130420Skarels if (dgetflag("sf")) 6230420Skarels dp->d_flags |= D_BADSECT; 6333399Smarc 6430420Skarels #define getnumdflt(field, dname, dflt) \ 6530420Skarels { int f = dgetnum(dname); \ 6630420Skarels (field) = f == -1 ? (dflt) : f; } 6730420Skarels 6830420Skarels getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 6910748Ssam dp->d_ntracks = dgetnum("nt"); 7010748Ssam dp->d_nsectors = dgetnum("ns"); 7110748Ssam dp->d_ncylinders = dgetnum("nc"); 7230420Skarels cq = dgetstr("dt", &cp); 7330420Skarels if (cq) 7430420Skarels dp->d_type = gettype(cq, dktypenames); 7530420Skarels else 7630420Skarels getnumdflt(dp->d_type, "dt", 0); 7730420Skarels getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 7830420Skarels getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 7930420Skarels getnumdflt(dp->d_rpm, "rm", 3600); 8030420Skarels getnumdflt(dp->d_interleave, "il", 1); 8130420Skarels getnumdflt(dp->d_trackskew, "sk", 0); 8230420Skarels getnumdflt(dp->d_cylskew, "cs", 0); 8330420Skarels getnumdflt(dp->d_headswitch, "hs", 0); 8430420Skarels getnumdflt(dp->d_trkseek, "ts", 0); 8530420Skarels getnumdflt(dp->d_bbsize, "bs", BBSIZE); 8630420Skarels getnumdflt(dp->d_sbsize, "sb", SBSIZE); 8710760Ssam strcpy(psize, "px"); 8810760Ssam strcpy(pbsize, "bx"); 8910760Ssam strcpy(pfsize, "fx"); 9030420Skarels strcpy(poffset, "ox"); 9130420Skarels strcpy(ptype, "tx"); 9230420Skarels max = 'a' - 1; 9330420Skarels pp = &dp->d_partitions[0]; 9430420Skarels for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 9530420Skarels psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 9610760Ssam pp->p_size = dgetnum(psize); 9730420Skarels if (pp->p_size == -1) 9830420Skarels pp->p_size = 0; 9930420Skarels else { 10030420Skarels pp->p_offset = dgetnum(poffset); 10130420Skarels getnumdflt(pp->p_fsize, pfsize, 0); 10230420Skarels if (pp->p_fsize) 10330420Skarels pp->p_frag = dgetnum(pbsize) / pp->p_fsize; 10430420Skarels getnumdflt(pp->p_fstype, ptype, 0); 10530420Skarels if (pp->p_fstype == 0 && (cq = dgetstr(ptype, &cp))) 10630420Skarels pp->p_fstype = gettype(cq, fstypenames); 10730420Skarels max = p; 10830420Skarels } 10910748Ssam } 11030420Skarels dp->d_npartitions = max + 1 - 'a'; 11130681Sbostic (void)strcpy(psize, "dx"); 11230420Skarels dx = dp->d_drivedata; 11330420Skarels for (p = '0'; p < '0' + NDDATA; p++, dx++) { 11430420Skarels psize[1] = p; 11530420Skarels getnumdflt(*dx, psize, 0); 11630420Skarels } 11730420Skarels dp->d_magic = DISKMAGIC; 11830420Skarels dp->d_magic2 = DISKMAGIC; 11910748Ssam return (dp); 12010748Ssam } 12110748Ssam 12210748Ssam #include <ctype.h> 12310748Ssam 12410748Ssam static char *tbuf; 12510748Ssam static char *dskip(); 12610748Ssam static char *ddecode(); 12710748Ssam 12810748Ssam /* 12910748Ssam * Get an entry for disk name in buffer bp, 13010748Ssam * from the diskcap file. Parse is very rudimentary; 13110748Ssam * we just notice escaped newlines. 13210748Ssam */ 13310748Ssam static 13410748Ssam dgetent(bp, name) 13510748Ssam char *bp, *name; 13610748Ssam { 13710748Ssam register char *cp; 13810748Ssam register int c; 13910748Ssam register int i = 0, cnt = 0; 14010748Ssam char ibuf[BUFSIZ]; 14110748Ssam int tf; 14210748Ssam 14310748Ssam tbuf = bp; 14410748Ssam tf = open(DISKTAB, 0); 14510748Ssam if (tf < 0) 14610748Ssam return (-1); 14710748Ssam for (;;) { 14810748Ssam cp = bp; 14910748Ssam for (;;) { 15010748Ssam if (i == cnt) { 15110748Ssam cnt = read(tf, ibuf, BUFSIZ); 15210748Ssam if (cnt <= 0) { 15310748Ssam close(tf); 15410748Ssam return (0); 15510748Ssam } 15610748Ssam i = 0; 15710748Ssam } 15810748Ssam c = ibuf[i++]; 15910748Ssam if (c == '\n') { 16010748Ssam if (cp > bp && cp[-1] == '\\'){ 16110748Ssam cp--; 16210748Ssam continue; 16310748Ssam } 16410748Ssam break; 16510748Ssam } 16610748Ssam if (cp >= bp+BUFSIZ) { 16710748Ssam write(2,"Disktab entry too long\n", 23); 16810748Ssam break; 16910748Ssam } else 17010748Ssam *cp++ = c; 17110748Ssam } 17210748Ssam *cp = 0; 17310748Ssam 17410748Ssam /* 17510748Ssam * The real work for the match. 17610748Ssam */ 17710748Ssam if (dnamatch(name)) { 17810748Ssam close(tf); 17910748Ssam return (1); 18010748Ssam } 18110748Ssam } 18210748Ssam } 18310748Ssam 18410748Ssam /* 18510748Ssam * Dnamatch deals with name matching. The first field of the disktab 18610748Ssam * entry is a sequence of names separated by |'s, so we compare 18710748Ssam * against each such name. The normal : terminator after the last 18810748Ssam * name (before the first field) stops us. 18910748Ssam */ 19010748Ssam static 19110748Ssam dnamatch(np) 19210748Ssam char *np; 19310748Ssam { 19410748Ssam register char *Np, *Bp; 19510748Ssam 19610748Ssam Bp = tbuf; 19710748Ssam if (*Bp == '#') 19810748Ssam return (0); 19910748Ssam for (;;) { 20010748Ssam for (Np = np; *Np && *Bp == *Np; Bp++, Np++) 20110748Ssam continue; 20210748Ssam if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0)) 20310748Ssam return (1); 20410748Ssam while (*Bp && *Bp != ':' && *Bp != '|') 20510748Ssam Bp++; 20610748Ssam if (*Bp == 0 || *Bp == ':') 20710748Ssam return (0); 20810748Ssam Bp++; 20910748Ssam } 21010748Ssam } 21110748Ssam 21210748Ssam /* 21310748Ssam * Skip to the next field. Notice that this is very dumb, not 21410748Ssam * knowing about \: escapes or any such. If necessary, :'s can be put 21510748Ssam * into the diskcap file in octal. 21610748Ssam */ 21710748Ssam static char * 21810748Ssam dskip(bp) 21910748Ssam register char *bp; 22010748Ssam { 22110748Ssam 22210748Ssam while (*bp && *bp != ':') 22310748Ssam bp++; 22410748Ssam if (*bp == ':') 22510748Ssam bp++; 22610748Ssam return (bp); 22710748Ssam } 22810748Ssam 22910748Ssam /* 23010748Ssam * Return the (numeric) option id. 23110748Ssam * Numeric options look like 23210748Ssam * li#80 23310748Ssam * i.e. the option string is separated from the numeric value by 23410748Ssam * a # character. If the option is not found we return -1. 23510748Ssam * Note that we handle octal numbers beginning with 0. 23610748Ssam */ 23710748Ssam static 23810748Ssam dgetnum(id) 23910748Ssam char *id; 24010748Ssam { 24110748Ssam register int i, base; 24210748Ssam register char *bp = tbuf; 24310748Ssam 24410748Ssam for (;;) { 24510748Ssam bp = dskip(bp); 24610748Ssam if (*bp == 0) 24710748Ssam return (-1); 24810748Ssam if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1]) 24910748Ssam continue; 25010748Ssam if (*bp == '@') 25110748Ssam return (-1); 25210748Ssam if (*bp != '#') 25310748Ssam continue; 25410748Ssam bp++; 25510748Ssam base = 10; 25610748Ssam if (*bp == '0') 25710748Ssam base = 8; 25810748Ssam i = 0; 25910748Ssam while (isdigit(*bp)) 26010748Ssam i *= base, i += *bp++ - '0'; 26110748Ssam return (i); 26210748Ssam } 26310748Ssam } 26410748Ssam 26510748Ssam /* 26610748Ssam * Handle a flag option. 26710748Ssam * Flag options are given "naked", i.e. followed by a : or the end 26810748Ssam * of the buffer. Return 1 if we find the option, or 0 if it is 26910748Ssam * not given. 27010748Ssam */ 27110748Ssam static 27210748Ssam dgetflag(id) 27310748Ssam char *id; 27410748Ssam { 27510748Ssam register char *bp = tbuf; 27610748Ssam 27710748Ssam for (;;) { 27810748Ssam bp = dskip(bp); 27910748Ssam if (!*bp) 28010748Ssam return (0); 28110748Ssam if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) { 28210748Ssam if (!*bp || *bp == ':') 28310748Ssam return (1); 28410748Ssam else if (*bp == '@') 28510748Ssam return (0); 28610748Ssam } 28710748Ssam } 28810748Ssam } 28910748Ssam 29010748Ssam /* 29110748Ssam * Get a string valued option. 29210748Ssam * These are given as 29310748Ssam * cl=^Z 29410748Ssam * Much decoding is done on the strings, and the strings are 29510748Ssam * placed in area, which is a ref parameter which is updated. 29610748Ssam * No checking on area overflow. 29710748Ssam */ 29810748Ssam static char * 29910748Ssam dgetstr(id, area) 30010748Ssam char *id, **area; 30110748Ssam { 30210748Ssam register char *bp = tbuf; 30310748Ssam 30410748Ssam for (;;) { 30510748Ssam bp = dskip(bp); 30610748Ssam if (!*bp) 30710748Ssam return (0); 30810748Ssam if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1]) 30910748Ssam continue; 31010748Ssam if (*bp == '@') 31110748Ssam return (0); 31210748Ssam if (*bp != '=') 31310748Ssam continue; 31410748Ssam bp++; 31510748Ssam return (ddecode(bp, area)); 31610748Ssam } 31710748Ssam } 31810748Ssam 31910748Ssam /* 32010748Ssam * Tdecode does the grung work to decode the 32110748Ssam * string capability escapes. 32210748Ssam */ 32310748Ssam static char * 32410748Ssam ddecode(str, area) 32510748Ssam register char *str; 32610748Ssam char **area; 32710748Ssam { 32810748Ssam register char *cp; 32910748Ssam register int c; 33010748Ssam register char *dp; 33110748Ssam int i; 33210748Ssam 33310748Ssam cp = *area; 33410748Ssam while ((c = *str++) && c != ':') { 33510748Ssam switch (c) { 33610748Ssam 33710748Ssam case '^': 33810748Ssam c = *str++ & 037; 33910748Ssam break; 34010748Ssam 34110748Ssam case '\\': 34210748Ssam dp = "E\033^^\\\\::n\nr\rt\tb\bf\f"; 34310748Ssam c = *str++; 34410748Ssam nextc: 34510748Ssam if (*dp++ == c) { 34610748Ssam c = *dp++; 34710748Ssam break; 34810748Ssam } 34910748Ssam dp++; 35010748Ssam if (*dp) 35110748Ssam goto nextc; 35210748Ssam if (isdigit(c)) { 35310748Ssam c -= '0', i = 2; 35410748Ssam do 35510748Ssam c <<= 3, c |= *str++ - '0'; 35610748Ssam while (--i && isdigit(*str)); 35710748Ssam } 35810748Ssam break; 35910748Ssam } 36010748Ssam *cp++ = c; 36110748Ssam } 36210748Ssam *cp++ = 0; 36310748Ssam str = *area; 36410748Ssam *area = cp; 36510748Ssam return (str); 36610748Ssam } 36730420Skarels 36830420Skarels static 36930420Skarels gettype(t, names) 37030420Skarels char *t; 37130420Skarels char **names; 37230420Skarels { 37330420Skarels register char **nm; 37430420Skarels 37530420Skarels for (nm = names; *nm; nm++) 37633261Smckusick if (strcasecmp(t, *nm) == 0) 37730420Skarels return (nm - names); 37830420Skarels if (isdigit(*t)) 37930420Skarels return (atoi(t)); 38030420Skarels return (0); 38130420Skarels } 38230420Skarels 38330420Skarels dkcksum(lp) 38430420Skarels register struct disklabel *lp; 38530420Skarels { 38630420Skarels register u_short *start, *end; 38730420Skarels register u_short sum = 0; 38830420Skarels 38930420Skarels start = (u_short *)lp; 39030420Skarels end = (u_short *)&lp->d_partitions[lp->d_npartitions]; 39130420Skarels while (start < end) 39230420Skarels sum ^= *start++; 39330420Skarels return (sum); 39430420Skarels } 395