1 /* $OpenBSD: disksubr.c,v 1.116 2017/02/28 10:49:37 natano 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/buf.h>
36 #include <sys/disklabel.h>
37 #include <sys/disk.h>
38 #include <sys/reboot.h>
39 #include <sys/conf.h>
40
41 #ifdef DEBUG
42 #include <sys/systm.h>
43 #endif
44
45 #include <machine/biosvar.h>
46
47 bios_diskinfo_t *bios_getdiskinfo(dev_t dev);
48
49 /*
50 * Attempt to read a disk label from a device
51 * using the indicated strategy routine.
52 * The label must be partly set up before this:
53 * secpercyl, secsize and anything required for a block i/o read
54 * operation in the driver's strategy/start routines
55 * must be filled in before calling us.
56 *
57 * If dos partition table requested, attempt to load it and
58 * find disklabel inside a DOS partition.
59 *
60 * We would like to check if each MBR has a valid DOSMBR_SIGNATURE, but
61 * we cannot because it doesn't always exist. So.. we assume the
62 * MBR is valid.
63 */
64 int
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,int spoofonly)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 int error;
72
73 if ((error = 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/%llu\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(lp->d_secsize);
97 bp->b_dev = dev;
98
99 error = readdoslabel(bp, strat, lp, NULL, spoofonly);
100 if (error == 0)
101 goto done;
102
103 #if defined(CD9660)
104 error = iso_disklabelspoof(dev, strat, lp);
105 if (error == 0)
106 goto done;
107 #endif
108 #if defined(UDF)
109 error = udf_disklabelspoof(dev, strat, lp);
110 if (error == 0)
111 goto done;
112 #endif
113
114 done:
115 if (bp) {
116 bp->b_flags |= B_INVAL;
117 brelse(bp);
118 }
119 disk_change = 1;
120 return (error);
121 }
122
123 /*
124 * Write disk label back to device after modification.
125 */
126 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp)127 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
128 {
129 daddr_t partoff = -1;
130 int error = EIO;
131 int offset;
132 struct disklabel *dlp;
133 struct buf *bp = NULL;
134
135 /* get a buffer and initialize it */
136 bp = geteblk(lp->d_secsize);
137 bp->b_dev = dev;
138
139 if (readdoslabel(bp, strat, lp, &partoff, 1) != 0)
140 goto done;
141
142 /* Read it in, slap the new label in, and write it back out */
143 error = readdisksector(bp, strat, lp, DL_BLKTOSEC(lp, partoff +
144 DOS_LABELSECTOR));
145 if (error)
146 goto done;
147 offset = DL_BLKOFFSET(lp, partoff + DOS_LABELSECTOR);
148
149 dlp = (struct disklabel *)(bp->b_data + offset);
150 *dlp = *lp;
151 CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
152 SET(bp->b_flags, B_BUSY | B_WRITE | B_RAW);
153 (*strat)(bp);
154 error = biowait(bp);
155
156 done:
157 if (bp) {
158 bp->b_flags |= B_INVAL;
159 brelse(bp);
160 }
161 disk_change = 1;
162 return (error);
163 }
164