1 /* $OpenBSD: disksubr.c,v 1.94 2007/06/20 18:15:45 deraadt 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 #include <sys/reboot.h>
40 #include <sys/conf.h>
41
42 #include <machine/biosvar.h>
43
44 /*
45 * Attempt to read a disk label from a device
46 * using the indicated strategy routine.
47 * The label must be partly set up before this:
48 * secpercyl, secsize and anything required for a block i/o read
49 * operation in the driver's strategy/start routines
50 * must be filled in before calling us.
51 *
52 * If dos partition table requested, attempt to load it and
53 * find disklabel inside a DOS partition. Return buffer
54 * for use in signalling errors if requested.
55 *
56 * We would like to check if each MBR has a valid DOSMBR_SIGNATURE, but
57 * we cannot because it doesn't always exist. So.. we assume the
58 * MBR is valid.
59 *
60 * Returns null on success and an error string on failure.
61 */
62 bios_diskinfo_t *bios_getdiskinfo(dev_t dev);
63
64 char *
65 readdisklabel(dev_t dev, void (*strat)(struct buf *),
66 struct disklabel *lp, int spoofonly)
67 {
68 bios_diskinfo_t *pdi;
69 struct buf *bp = NULL;
70 dev_t devno;
71 char *msg;
72
73 if ((msg = initdisklabel(lp)))
74 goto done;
75
76 /* Look for any BIOS geometry information we should honour. */
77 devno = chrtoblk(dev);
78 if (devno == NODEV)
79 devno = dev;
80 pdi = bios_getdiskinfo(MAKEBOOTDEV(major(devno), 0, 0, DISKUNIT(devno),
81 RAW_PART));
82 if (pdi != NULL && pdi->bios_heads > 0 && pdi->bios_sectors > 0) {
83 #ifdef DEBUG
84 printf("Disk GEOM %u/%u/%u -> BIOS GEOM %u/%u/%u\n",
85 lp->d_ntracks, lp->d_nsectors, lp->d_ncylinders,
86 pdi->bios_heads, pdi->bios_sectors,
87 DL_GETDSIZE(lp) / (pdi->bios_heads * pdi->bios_sectors));
88 #endif
89 lp->d_ntracks = pdi->bios_heads;
90 lp->d_nsectors = pdi->bios_sectors;
91 lp->d_secpercyl = pdi->bios_sectors * pdi->bios_heads;
92 lp->d_ncylinders = DL_GETDSIZE(lp) / lp->d_secpercyl;
93 }
94
95 /* get a buffer and initialize it */
96 bp = geteblk((int)lp->d_secsize);
97 bp->b_dev = dev;
98
99 msg = readdoslabel(bp, strat, lp, NULL, spoofonly);
100 if (msg == NULL)
101 goto done;
102
103 #if defined(CD9660)
104 if (iso_disklabelspoof(dev, strat, lp) == 0) {
105 msg = NULL;
106 goto done;
107 }
108 #endif
109 #if defined(UDF)
110 if (udf_disklabelspoof(dev, strat, lp) == 0) {
111 msg = NULL;
112 goto done;
113 }
114 #endif
115
116 done:
117 if (bp) {
118 bp->b_flags |= B_INVAL;
119 brelse(bp);
120 }
121 return (msg);
122 }
123
124 /*
125 * Write disk label back to device after modification.
126 */
127 int
128 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
129 {
130 int error = EIO, partoff = -1;
131 struct disklabel *dlp;
132 struct buf *bp = NULL;
133
134 /* get a buffer and initialize it */
135 bp = geteblk((int)lp->d_secsize);
136 bp->b_dev = dev;
137
138 if (readdoslabel(bp, strat, lp, &partoff, 1) != NULL)
139 goto done;
140
141 /* Read it in, slap the new label in, and write it back out */
142 bp->b_blkno = partoff + LABELSECTOR;
143 bp->b_bcount = lp->d_secsize;
144 bp->b_flags = B_BUSY | B_READ;
145 (*strat)(bp);
146 if ((error = biowait(bp)) != 0)
147 goto done;
148
149 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
150 *dlp = *lp;
151 bp->b_flags = B_BUSY | B_WRITE;
152 (*strat)(bp);
153 error = biowait(bp);
154
155 done:
156 if (bp) {
157 bp->b_flags |= B_INVAL;
158 brelse(bp);
159 }
160 return (error);
161 }