xref: /openbsd/sys/arch/loongson/loongson/disksubr.c (revision b986024f)
1 /*	$OpenBSD: disksubr.c,v 1.10 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/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
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,int spoofonly)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(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
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp)97 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
98 {
99 	daddr_t partoff = -1;
100 	int error = EIO;
101 	int offset;
102 	struct disklabel *dlp;
103 	struct buf *bp = NULL;
104 
105 	/* get a buffer and initialize it */
106 	bp = geteblk(lp->d_secsize);
107 	bp->b_dev = dev;
108 
109 	if (readdoslabel(bp, strat, lp, &partoff, 1) != 0)
110 		goto done;
111 
112 	/* Read it in, slap the new label in, and write it back out */
113 	error = readdisksector(bp, strat, lp, DL_BLKTOSEC(lp, partoff +
114 	    DOS_LABELSECTOR));
115 	if (error)
116 		goto done;
117 	offset = DL_BLKOFFSET(lp, partoff + DOS_LABELSECTOR);
118 
119 	dlp = (struct disklabel *)(bp->b_data + offset);
120 	*dlp = *lp;
121 	CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
122 	SET(bp->b_flags, B_BUSY | B_WRITE | B_RAW);
123 	(*strat)(bp);
124 	error = biowait(bp);
125 
126 done:
127 	if (bp) {
128 		bp->b_flags |= B_INVAL;
129 		brelse(bp);
130 	}
131 	disk_change = 1;
132 	return (error);
133 }
134