1 /* $OpenBSD: disksubr.c,v 1.1 2020/05/16 17:11:14 kettenis 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
39 /*
40 * Attempt to read a disk label from a device
41 * using the indicated strategy routine.
42 * The label must be partly set up before this:
43 * secpercyl, secsize and anything required for a block i/o read
44 * operation in the driver's strategy/start routines
45 * must be filled in before calling us.
46 *
47 * If dos partition table requested, attempt to load it and
48 * find disklabel inside a DOS partition.
49 *
50 * We would like to check if each MBR has a valid DOSMBR_SIGNATURE, but
51 * we cannot because it doesn't always exist. So.. we assume the
52 * MBR is valid.
53 */
54 int
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,int spoofonly)55 readdisklabel(dev_t dev, void (*strat)(struct buf *),
56 struct disklabel *lp, int spoofonly)
57 {
58 struct buf *bp = NULL;
59 int error;
60
61 if ((error = initdisklabel(lp)))
62 goto done;
63
64 /* get a buffer and initialize it */
65 bp = geteblk(lp->d_secsize);
66 bp->b_dev = dev;
67
68 error = readdoslabel(bp, strat, lp, NULL, spoofonly);
69 if (error == 0)
70 goto done;
71
72 #if defined(CD9660)
73 error = iso_disklabelspoof(dev, strat, lp);
74 if (error == 0)
75 goto done;
76 #endif
77 #if defined(UDF)
78 error = udf_disklabelspoof(dev, strat, lp);
79 if (error == 0)
80 goto done;
81 #endif
82
83 done:
84 if (bp) {
85 bp->b_flags |= B_INVAL;
86 brelse(bp);
87 }
88 disk_change = 1;
89 return (error);
90 }
91
92 /*
93 * Write disk label back to device after modification.
94 */
95 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp)96 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
97 {
98 daddr_t partoff = -1;
99 int error = EIO;
100 int offset;
101 struct disklabel *dlp;
102 struct buf *bp = NULL;
103
104 /* get a buffer and initialize it */
105 bp = geteblk(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 error = readdisksector(bp, strat, lp, DL_BLKTOSEC(lp, partoff +
113 DOS_LABELSECTOR));
114 if (error)
115 goto done;
116 offset = DL_BLKOFFSET(lp, partoff + DOS_LABELSECTOR);
117
118 dlp = (struct disklabel *)(bp->b_data + offset);
119 *dlp = *lp;
120 CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
121 SET(bp->b_flags, B_BUSY | B_WRITE | B_RAW);
122 (*strat)(bp);
123 error = biowait(bp);
124
125 done:
126 if (bp) {
127 bp->b_flags |= B_INVAL;
128 brelse(bp);
129 }
130 disk_change = 1;
131 return (error);
132 }
133