[BACK]Return to disksubr.c CVS log [TXT][DIR] Up to [local] / sys / arch / mac68k / mac68k

Annotation of sys/arch/mac68k/mac68k/disksubr.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: disksubr.c,v 1.51 2007/07/11 04:53:42 miod Exp $      */
                      2: /*     $NetBSD: disksubr.c,v 1.22 1997/11/26 04:18:20 briggs Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the University nor the names of its contributors
                     17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*-
                     34:  * Copyright (C) 1993  Allen K. Briggs, Chris P. Caputo,
                     35:  *                     Michael L. Finch, Bradley A. Grantham, and
                     36:  *                     Lawrence A. Kesteloot
                     37:  * All rights reserved.
                     38:  *
                     39:  * Redistribution and use in source and binary forms, with or without
                     40:  * modification, are permitted provided that the following conditions
                     41:  * are met:
                     42:  * 1. Redistributions of source code must retain the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer.
                     44:  * 2. Redistributions in binary form must reproduce the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer in the
                     46:  *    documentation and/or other materials provided with the distribution.
                     47:  * 3. All advertising materials mentioning features or use of this software
                     48:  *    must display the following acknowledgement:
                     49:  *     This product includes software developed by the Alice Group.
                     50:  * 4. The names of the Alice Group or any of its members may not be used
                     51:  *    to endorse or promote products derived from this software without
                     52:  *    specific prior written permission.
                     53:  *
                     54:  * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
                     55:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     56:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     57:  * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
                     58:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     59:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     60:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     61:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     62:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     63:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     64:  *
                     65:  */
                     66:
                     67: /* rewritten, 2-5-93 MLF */
                     68: /* it's a lot cleaner now, and adding support for new partition types
                     69:  * isn't a bitch anymore
                     70:  * known bugs:
                     71:  * 1) when only an HFS_PART part exists on a drive it gets assigned to "B"
                     72:  * this is because of line 623 of sd.c, I think this line should go.
                     73:  * 2) /sbin/disklabel expects the whole disk to be in "D", we put it in
                     74:  * "C" (I think) and we don't set that position in the disklabel structure
                     75:  * as used.  Again, not my fault.
                     76:  */
                     77: #include <sys/param.h>
                     78: #include <sys/systm.h>
                     79: #include <sys/buf.h>
                     80: #include <sys/disk.h>
                     81: #include <sys/disklabel.h>
                     82: #include <sys/malloc.h>
                     83: #include <sys/syslog.h>
                     84:
                     85: #include <mac68k/mac68k/dpme.h>        /* MF the structure of a mac partition entry */
                     86:
                     87: #define NUM_PARTS_PROBED 32
                     88:
                     89: #define ROOT_PART      1
                     90: #define UFS_PART       2
                     91: #define SWAP_PART      3
                     92: #define HFS_PART       4
                     93: #define SCRATCH_PART   5
                     94:
                     95: int getFreeLabelEntry(struct disklabel *);
                     96: int whichType(struct partmapentry *);
                     97: int fixPartTable(struct partmapentry *, long, char *);
                     98: void setPart(struct partmapentry *, struct disklabel *, int, int);
                     99: int getNamedType(struct partmapentry *, int, struct disklabel *, int, int, int *);
                    100: char *read_mac_label(char *, struct disklabel *);
                    101:
                    102: /*
                    103:  * Find an entry in the disk label that is unused and return it
                    104:  * or -1 if no entry
                    105:  */
                    106: int
                    107: getFreeLabelEntry(struct disklabel *lp)
                    108: {
                    109:        int i;
                    110:
                    111:        for (i = 0; i < MAXPARTITIONS; i++) {
                    112:                if (i != RAW_PART &&
                    113:                    lp->d_partitions[i].p_fstype == FS_UNUSED)
                    114:                        return i;
                    115:        }
                    116:        return -1;
                    117: }
                    118:
                    119: /*
                    120:  * figure out what the type of the given part is and return it
                    121:  */
                    122: int
                    123: whichType(struct partmapentry *part)
                    124: {
                    125:        struct blockzeroblock *bzb;
                    126:
                    127:        if (part->pmPartType[0] == '\0')
                    128:                return 0;
                    129:
                    130:        if (strcmp(PART_DRIVER_TYPE, (char *)part->pmPartType) == 0 ||
                    131:            strcmp(PART_DRIVER43_TYPE, (char *)part->pmPartType) == 0 ||
                    132:            strcmp(PART_DRIVERATA_TYPE, (char *)part->pmPartType) == 0 ||
                    133:            strcmp(PART_FWB_COMPONENT_TYPE, (char *)part->pmPartType) == 0 ||
                    134:            strcmp(PART_PARTMAP_TYPE, (char *)part->pmPartType) == 0)
                    135:                return 0;
                    136:        if (strcmp(PART_UNIX_TYPE, (char *)part->pmPartType) == 0) {
                    137:                /* unix part, swap, root, usr */
                    138:                bzb = (struct blockzeroblock *)(&part->pmBootArgs);
                    139:                if (bzb->bzbMagic != BZB_MAGIC)
                    140:                        return 0;
                    141:
                    142:                if (bzb->bzbFlags & BZB_ROOTFS)
                    143:                        return ROOT_PART;
                    144:
                    145:                if ((bzb->bzbFlags & BZB_USRFS) ||
                    146:                    (bzb->bzbFlags & BZB_EXFS4) ||
                    147:                    (bzb->bzbFlags & BZB_EXFS5) ||
                    148:                    (bzb->bzbFlags & BZB_EXFS6))
                    149:                        return UFS_PART;
                    150:
                    151:                if (bzb->bzbType == BZB_TYPESWAP)
                    152:                        return SWAP_PART;
                    153:
                    154:                return SCRATCH_PART;
                    155:        }
                    156:        if (strcmp(PART_MAC_TYPE, (char *)part->pmPartType) == 0)
                    157:                return HFS_PART;
                    158: /*
                    159:        if (strcmp(PART_SCRATCH, (char *)part->pmPartType) == 0)
                    160:                return SCRATCH_PART;
                    161: */
                    162:        return SCRATCH_PART;    /* no known type, but label it, anyway */
                    163: }
                    164:
                    165: /*
                    166:  * Take part table in crappy form, place it in a structure we can depend
                    167:  * upon.  Make sure names are NUL terminated.  Capitalize the names
                    168:  * of part types.
                    169:  */
                    170: int
                    171: fixPartTable(struct partmapentry *partTable, long size, char *base)
                    172: {
                    173:        struct partmapentry *pmap;
                    174:        char *s;
                    175:        int i;
                    176:
                    177:        for (i = 0; i < NUM_PARTS_PROBED; i++) {
                    178:                pmap = (struct partmapentry *)((i * size) + base + DEV_BSIZE);
                    179:                partTable[i] = *pmap;
                    180:                pmap = &partTable[i];
                    181:
                    182:                if (pmap->pmSig != DPME_MAGIC) { /* this is not valid */
                    183:                        pmap->pmPartType[0] = '\0';
                    184:                        return i;
                    185:                }
                    186:
                    187:                pmap->pmPartName[31] = '\0';
                    188:                pmap->pmPartType[31] = '\0';
                    189:
                    190:                for (s = pmap->pmPartType; *s; s++)
                    191:                        if ((*s >= 'a') && (*s <= 'z'))
                    192:                                *s = (*s - 'a' + 'A');
                    193:        }
                    194:        return NUM_PARTS_PROBED;
                    195: }
                    196:
                    197: void
                    198: setPart(struct partmapentry *part, struct disklabel *lp, int fstype, int slot)
                    199: {
                    200:        DL_SETPSIZE(&lp->d_partitions[slot], part->pmPartBlkCnt);
                    201:        DL_SETPOFFSET(&lp->d_partitions[slot], part->pmPyPartStart);
                    202:        lp->d_partitions[slot].p_fstype = fstype;
                    203:        part->pmPartType[0] = '\0';
                    204: }
                    205:
                    206: int
                    207: getNamedType(struct partmapentry *part, int num_parts, struct disklabel *lp,
                    208:     int type, int alt, int *maxslot)
                    209: {
                    210:        struct blockzeroblock *bzb;
                    211:        int i;
                    212:
                    213:        for (i = 0; i < num_parts; i++) {
                    214:                if (whichType(&(part[i])) == type) {
                    215:                        switch (type) {
                    216:                        case ROOT_PART:
                    217:                                bzb = (struct blockzeroblock *)
                    218:                                    (&part[i].pmBootArgs);
                    219:                                if (alt >= 0 && alt != bzb->bzbCluster)
                    220:                                        goto skip;
                    221:                                setPart(&(part[i]), lp, FS_BSDFFS, 0);
                    222:                                break;
                    223:                        case UFS_PART:
                    224:                                bzb = (struct blockzeroblock *)
                    225:                                    (&part[i].pmBootArgs);
                    226:                                if (alt >= 0 && alt != bzb->bzbCluster)
                    227:                                        goto skip;
                    228:                                setPart(&(part[i]), lp, FS_BSDFFS, 6);
                    229:                                if (*maxslot < 6)
                    230:                                        *maxslot = 6;
                    231:                                break;
                    232:                        case SWAP_PART:
                    233:                                setPart(&(part[i]), lp, FS_SWAP, 1);
                    234:                                if (*maxslot < 1)
                    235:                                        *maxslot = 1;
                    236:                                break;
                    237:                        default:
                    238:                                printf("disksubr.c: can't do type %d\n", type);
                    239:                                break;
                    240:                        }
                    241:                        return 0;
                    242:                }
                    243: skip:
                    244:        }
                    245:        return -1;
                    246: }
                    247:
                    248: /*
                    249:  * read in the entire diskpartition table, it may be bigger or smaller
                    250:  * than NUM_PARTS_PROBED but read that many entries.  Each entry has a magic
                    251:  * number so we'll know if an entry is crap.
                    252:  * next fill in the disklabel with info like this
                    253:  * next fill in the root, usr, and swap parts.
                    254:  * then look for anything else and fit it in.
                    255:  *     A: root
                    256:  *     B: Swap
                    257:  *     C: Whole disk
                    258:  *
                    259:  * AKB -- I added to Mike's original algorithm by searching for a bzbCluster
                    260:  *     of zero for root, first.  This allows A/UX to live on cluster 1 and
                    261:  *     NetBSD to live on cluster 0--regardless of the actual order on the
                    262:  *     disk.  This whole algorithm should probably be changed in the future.
                    263:  */
                    264: char *
                    265: read_mac_label(char *dlbuf, struct disklabel *lp)
                    266: {
                    267:        int i, num_parts, maxslot = RAW_PART;
                    268:        struct partmapentry *pmap;
                    269:
                    270:        MALLOC(pmap, struct partmapentry *,
                    271:            NUM_PARTS_PROBED * sizeof(struct partmapentry), M_DEVBUF, M_NOWAIT);
                    272:        if (pmap == NULL)
                    273:                return ("out of memory");
                    274:
                    275:        num_parts = fixPartTable(pmap, lp->d_secsize, dlbuf);
                    276:        if (getNamedType(pmap, num_parts, lp, ROOT_PART, 0, &maxslot))
                    277:                getNamedType(pmap, num_parts, lp, ROOT_PART, -1, &maxslot);
                    278:        getNamedType(pmap, num_parts, lp, SWAP_PART, -1, &maxslot);
                    279:        if (getNamedType(pmap, num_parts, lp, UFS_PART, 0, &maxslot))
                    280:                getNamedType(pmap, num_parts, lp, UFS_PART, -1, &maxslot);
                    281:        for (i = 0; i < num_parts; i++) {
                    282:                int partType, slot;
                    283:
                    284:                slot = getFreeLabelEntry(lp);
                    285:                if (slot < 0)
                    286:                        break;
                    287:
                    288:                partType = whichType(&(pmap[i]));
                    289:
                    290:                switch (partType) {
                    291:                case ROOT_PART:
                    292:                case UFS_PART:
                    293:                        setPart(&(pmap[i]), lp, FS_BSDFFS, slot);
                    294:                        if (slot > maxslot)
                    295:                                maxslot = slot;
                    296:                        break;
                    297:                case SWAP_PART:
                    298:                        setPart(&(pmap[i]), lp, FS_SWAP, slot);
                    299:                        if (slot > maxslot)
                    300:                                maxslot = slot;
                    301:                        break;
                    302:                case HFS_PART:
                    303:                        setPart(&(pmap[i]), lp, FS_HFS, slot);
                    304:                        if (slot > maxslot)
                    305:                                maxslot = slot;
                    306:                        break;
                    307:                case SCRATCH_PART:
                    308:                        setPart(&(pmap[i]), lp, FS_OTHER, slot);
                    309:                        if (slot > maxslot)
                    310:                                maxslot = slot;
                    311:                        break;
                    312:                default:
                    313:                        break;
                    314:                }
                    315:        }
                    316:        lp->d_npartitions = maxslot + 1;
                    317:        lp->d_version = 1;
                    318:        lp->d_checksum = 0;
                    319:        lp->d_checksum = dkcksum(lp);
                    320:        FREE(pmap, M_DEVBUF);
                    321:        return NULL;
                    322: }
                    323:
                    324: /*
                    325:  * Attempt to read a disk label from a device using the indicated strategy
                    326:  * routine.  The label must be partly set up before this: secpercyl and
                    327:  * anything required in the strategy routine (e.g., sector size) must be
                    328:  * filled in before calling us.  Returns null on success and an error
                    329:  * string on failure.
                    330:  */
                    331: char *
                    332: readdisklabel(dev_t dev, void (*strat)(struct buf *),
                    333:     struct disklabel *lp, int spoofonly)
                    334: {
                    335:        struct buf *bp = NULL;
                    336:        u_int16_t *sbSigp;
                    337:        int size;
                    338:        char *msg;
                    339:
                    340:        if ((msg = initdisklabel(lp)))
                    341:                goto done;
                    342:
                    343:        size = roundup((NUM_PARTS_PROBED + 1) << DEV_BSHIFT, lp->d_secsize);
                    344:        bp = geteblk(size);
                    345:        bp->b_dev = dev;
                    346:
                    347:        if (spoofonly)
                    348:                goto doslabel;
                    349:
                    350:        bp->b_blkno = LABELSECTOR;
                    351:        bp->b_bcount = size;
                    352:        bp->b_flags = B_BUSY | B_READ;
                    353:        (*strat)(bp);
                    354:        if (biowait(bp)) {
                    355:                msg = "disk label I/O error";
                    356:                goto done;
                    357:        }
                    358:
                    359:        sbSigp = (u_int16_t *)bp->b_data;
                    360:        if (*sbSigp == 0x4552) {
                    361:                msg = read_mac_label(bp->b_data, lp);
                    362:                if (msg == NULL)
                    363:                        goto done;
                    364:        }
                    365:
                    366:        /* Get a MI label */
                    367:        bp->b_blkno = LABELSECTOR;
                    368:        bp->b_bcount = lp->d_secsize;
                    369:        bp->b_flags = B_BUSY | B_READ;
                    370:        (*strat)(bp);
                    371:        if (biowait(bp)) {
                    372:                msg = "disk label I/O error";
                    373:                goto done;
                    374:        }
                    375:
                    376:        msg = checkdisklabel(bp->b_data + LABELOFFSET, lp);
                    377:        if (msg == NULL)
                    378:                goto done;
                    379:
                    380: doslabel:
                    381:        msg = readdoslabel(bp, strat, lp, NULL, spoofonly);
                    382:        if (msg == NULL)
                    383:                goto done;
                    384:
                    385: #if defined(CD9660)
                    386:        if (iso_disklabelspoof(dev, strat, lp) == 0) {
                    387:                msg = NULL;
                    388:                goto done;
                    389:        }
                    390: #endif
                    391: #if defined(UDF)
                    392:        if (udf_disklabelspoof(dev, strat, lp) == 0) {
                    393:                msg = NULL;
                    394:                goto done;
                    395:        }
                    396: #endif
                    397:
                    398: done:
                    399:        if (bp) {
                    400:                bp->b_flags |= B_INVAL;
                    401:                brelse(bp);
                    402:        }
                    403:        return (msg);
                    404: }
                    405:
                    406: /*
                    407:  * Write disk label back to device after modification.
                    408:  *
                    409:  * To avoid spreading havoc into the MacOS partition structures, we will
                    410:  * refuse to write a disklabel if the media has a MacOS signature.
                    411:  */
                    412: int
                    413: writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
                    414: {
                    415:        struct buf *bp = NULL;
                    416:        struct disklabel *dlp;
                    417:        int error = 0;
                    418:        u_int16_t *sbSigp;
                    419:
                    420:        /* get a buffer and initialize it */
                    421:        bp = geteblk((int)lp->d_secsize);
                    422:        bp->b_dev = dev;
                    423:
                    424:        bp->b_blkno = LABELSECTOR;
                    425:        bp->b_bcount = lp->d_secsize;
                    426:        bp->b_flags = B_BUSY | B_READ;
                    427:        (*strat)(bp);
                    428:        if ((error = biowait(bp)) != 0)
                    429:                goto done;
                    430:
                    431:        /* Check for MacOS fingerprints */
                    432:        sbSigp = (u_int16_t *)bp->b_data;
                    433:        if (*sbSigp == 0x4552) {
                    434:                /* XXX AND THEN DO NOT WRITE?? */
                    435:                goto done;
                    436:        }
                    437:
                    438:        dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
                    439:        *dlp = *lp;
                    440:        bp->b_flags = B_BUSY | B_WRITE;
                    441:        (*strat)(bp);
                    442:        error = biowait(bp);
                    443:
                    444: done:
                    445:        if (bp) {
                    446:                bp->b_flags |= B_INVAL;
                    447:                brelse(bp);
                    448:        }
                    449:        return (error);
                    450: }

CVSweb