xref: /netbsd-src/sys/arch/alpha/alpha/disksubr.c (revision 021d832a1333a7c481b64a5d4c4c4d092bd0bc20)
1 /* $NetBSD: disksubr.c,v 1.42 2019/04/03 22:10:49 christos Exp $ */
2 
3 /*
4  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Authors: Keith Bostic, Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
31 
32 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.42 2019/04/03 22:10:49 christos Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/ioccom.h>
38 #include <sys/device.h>
39 #include <sys/disklabel.h>
40 #include <sys/disk.h>
41 
42 #include <machine/cpu.h>
43 #include <machine/autoconf.h>
44 
45 extern device_t bootdv;
46 
47 /*
48  * Attempt to read a disk label from a device
49  * using the indicated strategy routine.
50  * The label must be partly set up before this:
51  * secpercyl and anything required in the strategy routine
52  * (e.g., sector size) must be filled in before calling us.
53  * Returns null on success and an error string on failure.
54  */
55 const char *
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * clp)56 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp)
57 {
58 	struct buf *bp;
59 	struct disklabel *dlp;
60 	struct dkbad *bdp;
61 	const char *msg = NULL;
62 	int i;
63 
64 	/* minimal requirements for archtypal disk label */
65 	if (lp->d_secsize == 0)
66 		lp->d_secsize = DEV_BSIZE;
67 	if (lp->d_secperunit == 0)
68 		lp->d_secperunit = 0x1fffffff;
69 	lp->d_npartitions = RAW_PART + 1;
70 	if (lp->d_partitions[RAW_PART].p_size == 0)
71 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
72 	lp->d_partitions[RAW_PART].p_offset = 0;
73 
74 	/* obtain buffer to probe drive with */
75 	bp = geteblk((int)lp->d_secsize);
76 
77 	/* next, dig out disk label */
78 	bp->b_dev = dev;
79 	bp->b_blkno = LABELSECTOR;
80 	bp->b_cylinder = 0;
81 	bp->b_bcount = lp->d_secsize;
82 	bp->b_flags |= B_READ;
83 	(*strat)(bp);
84 
85 	/* if successful, locate disk label within block and validate */
86 	if (biowait(bp)) {
87 		msg = "disk label read error";
88 		goto done;
89 	}
90 
91 	dlp = (struct disklabel *)((char *)bp->b_data + LABELOFFSET);
92 	if (dlp->d_magic == DISKMAGIC) {
93 		if (dkcksum(dlp))
94 			msg = "NetBSD disk label corrupted";
95 		else
96 			*lp = *dlp;
97 	} else
98 		msg = "no disk label";
99 	if (msg)
100 		goto done;
101 
102 	/* obtain bad sector table if requested and present */
103 	if (clp && (bdp = &clp->bad) != NULL && (lp->d_flags & D_BADSECT)) {
104 		struct dkbad *db;
105 
106 		i = 0;
107 		do {
108 			/* read a bad sector table */
109 			bp->b_oflags &= ~BO_DONE;
110 			bp->b_flags |= B_READ;
111 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
112 			if (lp->d_secsize > DEV_BSIZE)
113 				bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
114 			else
115 				bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
116 			bp->b_bcount = lp->d_secsize;
117 			bp->b_cylinder = lp->d_ncylinders - 1;
118 			(*strat)(bp);
119 
120 			/* if successful, validate, otherwise try another */
121 			if (biowait(bp)) {
122 				msg = "bad sector table I/O error";
123 			} else {
124 				db = (struct dkbad *)(bp->b_data);
125 #define DKBAD_MAGIC 0x4321
126 				if (db->bt_mbz == 0
127 					&& db->bt_flag == DKBAD_MAGIC) {
128 					msg = NULL;
129 					*bdp = *db;
130 					break;
131 				} else
132 					msg = "bad sector table corrupted";
133 			}
134 		} while (bp->b_error != 0 && (i += 2) < 10 &&
135 			i < lp->d_nsectors);
136 	}
137 
138 done:
139 	brelse(bp, 0);
140 	return (msg);
141 }
142 
143 /*
144  * Write disk label back to device after modification.
145  * This means write out the rigid disk blocks to represent the
146  * label.  Hope the user was careful.
147  */
148 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * clp)149 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp)
150 {
151 	struct buf *bp;
152 	struct disklabel *dlp;
153 	int error = 0;
154 
155 	bp = geteblk((int)lp->d_secsize);
156 	bp->b_dev = dev;
157 	bp->b_blkno = LABELSECTOR;
158 	bp->b_cylinder = 0;
159 	bp->b_bcount = lp->d_secsize;
160 	bp->b_flags |= B_READ;           /* get current label */
161 	(*strat)(bp);
162 	if ((error = biowait(bp)) != 0)
163 		goto done;
164 
165 	dlp = (struct disklabel *)((char *)bp->b_data + LABELOFFSET);
166 	*dlp = *lp;     /* struct assignment */
167 
168 	/*
169 	 * The Alpha requires that the boot block be checksummed.
170 	 * The first 63 8-bit quantites are summed into the 64th.
171 	 */
172 	{
173 		int i;
174 		u_long *dp, sum;
175 
176 		dp = (u_long *)bp->b_data;
177 		sum = 0;
178 		for (i = 0; i < 63; i++)
179 			sum += dp[i];
180 		dp[63] = sum;
181 	}
182 
183 	bp->b_flags &= ~B_READ;
184 	bp->b_flags |= B_WRITE;
185 	bp->b_oflags &= ~BO_DONE;
186 	(*strat)(bp);
187 	error = biowait(bp);
188 
189 done:
190 	brelse(bp, 0);
191 	return (error);
192 }
193