1 /* $OpenBSD: disksubr.c,v 1.7 2011/07/08 00:08:00 krw Exp $ */ 2 /* $NetBSD: disksubr.c,v 1.21 1996/05/03 19:42:03 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1996 Theo de Raadt 6 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/buf.h> 37 #include <sys/disklabel.h> 38 #include <sys/disk.h> 39 40 /* 41 * Attempt to read a disk label from a device 42 * using the indicated strategy routine. 43 * The label must be partly set up before this: 44 * secpercyl, secsize and anything required for a block i/o read 45 * operation in the driver's strategy/start routines 46 * must be filled in before calling us. 47 * 48 * If dos partition table requested, attempt to load it and 49 * find disklabel inside a DOS partition. 50 * 51 * We would like to check if each MBR has a valid DOSMBR_SIGNATURE, but 52 * we cannot because it doesn't always exist. So.. we assume the 53 * MBR is valid. 54 */ 55 int 56 readdisklabel(dev_t dev, void (*strat)(struct buf *), 57 struct disklabel *lp, int spoofonly) 58 { 59 struct buf *bp = NULL; 60 int error; 61 62 if ((error = initdisklabel(lp))) 63 goto done; 64 65 /* get a buffer and initialize it */ 66 bp = geteblk((int)lp->d_secsize); 67 bp->b_dev = dev; 68 69 error = readdoslabel(bp, strat, lp, NULL, spoofonly); 70 if (error == 0) 71 goto done; 72 73 #if defined(CD9660) 74 error = iso_disklabelspoof(dev, strat, lp); 75 if (error == 0) 76 goto done; 77 #endif 78 #if defined(UDF) 79 error = udf_disklabelspoof(dev, strat, lp); 80 if (error == 0) 81 goto done; 82 #endif 83 84 done: 85 if (bp) { 86 bp->b_flags |= B_INVAL; 87 brelse(bp); 88 } 89 disk_change = 1; 90 return (error); 91 } 92 93 /* 94 * Write disk label back to device after modification. 95 */ 96 int 97 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp) 98 { 99 int error = EIO, partoff = -1; 100 int offset; 101 struct disklabel *dlp; 102 struct buf *bp = NULL; 103 104 /* get a buffer and initialize it */ 105 bp = geteblk((int)lp->d_secsize); 106 bp->b_dev = dev; 107 108 if (readdoslabel(bp, strat, lp, &partoff, 1) != 0) 109 goto done; 110 111 /* Read it in, slap the new label in, and write it back out */ 112 bp->b_blkno = DL_BLKTOSEC(lp, partoff + DOS_LABELSECTOR) * 113 DL_BLKSPERSEC(lp); 114 offset = DL_BLKOFFSET(lp, partoff + DOS_LABELSECTOR); 115 bp->b_bcount = lp->d_secsize; 116 CLR(bp->b_flags, B_READ | B_WRITE | B_DONE); 117 SET(bp->b_flags, B_BUSY | B_READ | B_RAW); 118 (*strat)(bp); 119 if ((error = biowait(bp)) != 0) 120 goto done; 121 122 dlp = (struct disklabel *)(bp->b_data + offset); 123 *dlp = *lp; 124 CLR(bp->b_flags, B_READ | B_WRITE | B_DONE); 125 SET(bp->b_flags, B_BUSY | B_WRITE | B_RAW); 126 (*strat)(bp); 127 error = biowait(bp); 128 129 done: 130 if (bp) { 131 bp->b_flags |= B_INVAL; 132 brelse(bp); 133 } 134 disk_change = 1; 135 return (error); 136 } 137