xref: /openbsd/sys/arch/loongson/loongson/disksubr.c (revision 17df1aa7)
1 /*	$OpenBSD: disksubr.c,v 1.1.1.1 2009/08/17 06:34:30 miod 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. Return buffer
50  * for use in signalling errors if requested.
51  *
52  * We would like to check if each MBR has a valid DOSMBR_SIGNATURE, but
53  * we cannot because it doesn't always exist. So.. we assume the
54  * MBR is valid.
55  *
56  * Returns null on success and an error string on failure.
57  */
58 int
59 readdisklabel(dev_t dev, void (*strat)(struct buf *),
60     struct disklabel *lp, int spoofonly)
61 {
62 	struct buf *bp = NULL;
63 	int error;
64 
65 	if ((error = initdisklabel(lp)))
66 		goto done;
67 
68 	/* get a buffer and initialize it */
69 	bp = geteblk((int)lp->d_secsize);
70 	bp->b_dev = dev;
71 
72 	error = readdoslabel(bp, strat, lp, NULL, spoofonly);
73 	if (error == 0)
74 		goto done;
75 
76 #if defined(CD9660)
77 	error = iso_disklabelspoof(dev, strat, lp);
78 	if (error == 0)
79 		goto done;
80 #endif
81 #if defined(UDF)
82 	error = udf_disklabelspoof(dev, strat, lp);
83 	if (error == 0)
84 		goto done;
85 #endif
86 
87 done:
88 	if (bp) {
89 		bp->b_flags |= B_INVAL;
90 		brelse(bp);
91 	}
92 	return (error);
93 }
94 
95 /*
96  * Write disk label back to device after modification.
97  */
98 int
99 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
100 {
101 	int error = EIO, partoff = -1;
102 	struct disklabel *dlp;
103 	struct buf *bp = NULL;
104 
105 	/* get a buffer and initialize it */
106 	bp = geteblk((int)lp->d_secsize);
107 	bp->b_dev = dev;
108 
109 	if (readdoslabel(bp, strat, lp, &partoff, 1) != NULL)
110 		goto done;
111 
112 	/* Read it in, slap the new label in, and write it back out */
113 	bp->b_blkno = partoff + LABELSECTOR;
114 	bp->b_bcount = lp->d_secsize;
115 	bp->b_flags = B_BUSY | B_READ | B_RAW;
116 	(*strat)(bp);
117 	if ((error = biowait(bp)) != 0)
118 		goto done;
119 
120 	dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
121 	*dlp = *lp;
122 	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 	return (error);
132 }
133