subr_disk_mbr.c revision 1.29.10.2 1 1.29.10.2 ad /* $NetBSD: subr_disk_mbr.c,v 1.29.10.2 2007/07/29 12:15:46 ad Exp $ */
2 1.29.10.2 ad
3 1.29.10.2 ad /*
4 1.29.10.2 ad * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5 1.29.10.2 ad * All rights reserved.
6 1.29.10.2 ad *
7 1.29.10.2 ad * Redistribution and use in source and binary forms, with or without
8 1.29.10.2 ad * modification, are permitted provided that the following conditions
9 1.29.10.2 ad * are met:
10 1.29.10.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.29.10.2 ad * notice, this list of conditions and the following disclaimer.
12 1.29.10.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.29.10.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.29.10.2 ad * documentation and/or other materials provided with the distribution.
15 1.29.10.2 ad * 3. Neither the name of the University nor the names of its contributors
16 1.29.10.2 ad * may be used to endorse or promote products derived from this software
17 1.29.10.2 ad * without specific prior written permission.
18 1.29.10.2 ad *
19 1.29.10.2 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.29.10.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.29.10.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.29.10.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.29.10.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.29.10.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.29.10.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.29.10.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.29.10.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.29.10.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.29.10.2 ad * SUCH DAMAGE.
30 1.29.10.2 ad *
31 1.29.10.2 ad * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
32 1.29.10.2 ad */
33 1.29.10.2 ad
34 1.29.10.2 ad /*
35 1.29.10.2 ad * Code to find a NetBSD label on a disk that contains an i386 style MBR.
36 1.29.10.2 ad * The first NetBSD label found in the 2nd sector of a NetBSD partition
37 1.29.10.2 ad * is used.
38 1.29.10.2 ad * If we don't find a label searching the MBR, we look at the start of the
39 1.29.10.2 ad * disk, if that fails then a label is faked up from the MBR.
40 1.29.10.2 ad *
41 1.29.10.2 ad * If there isn't a disklabel or anything in the MBR then partition a
42 1.29.10.2 ad * is set to cover the whole disk.
43 1.29.10.2 ad * Useful for files that contain single filesystems (etc).
44 1.29.10.2 ad *
45 1.29.10.2 ad * This code will read host endian netbsd labels from little endian MBR.
46 1.29.10.2 ad *
47 1.29.10.2 ad * Based on the i386 disksubr.c
48 1.29.10.2 ad *
49 1.29.10.2 ad * Since the mbr only has 32bit fields for sector addresses, we do the same.
50 1.29.10.2 ad *
51 1.29.10.2 ad * XXX There are potential problems writing labels to disks where there
52 1.29.10.2 ad * is only space for 8 netbsd partitions but this code has been compiled
53 1.29.10.2 ad * with MAXPARTITIONS=16.
54 1.29.10.2 ad */
55 1.29.10.2 ad
56 1.29.10.2 ad #include <sys/cdefs.h>
57 1.29.10.2 ad __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.29.10.2 2007/07/29 12:15:46 ad Exp $");
58 1.29.10.2 ad
59 1.29.10.2 ad #include <sys/param.h>
60 1.29.10.2 ad #include <sys/systm.h>
61 1.29.10.2 ad #include <sys/buf.h>
62 1.29.10.2 ad #include <sys/bootblock.h>
63 1.29.10.2 ad #include <sys/disklabel.h>
64 1.29.10.2 ad #include <sys/disk.h>
65 1.29.10.2 ad #include <sys/syslog.h>
66 1.29.10.2 ad
67 1.29.10.2 ad #include "opt_mbr.h"
68 1.29.10.2 ad
69 1.29.10.2 ad typedef struct mbr_partition mbr_partition_t;
70 1.29.10.2 ad
71 1.29.10.2 ad /*
72 1.29.10.2 ad * We allocate a buffer 2 sectors large, and look in both....
73 1.29.10.2 ad * That means we find labels written by other ports with different offsets.
74 1.29.10.2 ad * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
75 1.29.10.2 ad */
76 1.29.10.2 ad #if LABELSECTOR > 1 || LABELOFFSET > 512
77 1.29.10.2 ad #error Invalid LABELSECTOR or LABELOFFSET
78 1.29.10.2 ad #endif
79 1.29.10.2 ad
80 1.29.10.2 ad #define MBR_LABELSECTOR 1
81 1.29.10.2 ad
82 1.29.10.2 ad #define SCAN_CONTINUE 0
83 1.29.10.2 ad #define SCAN_FOUND 1
84 1.29.10.2 ad #define SCAN_ERROR 2
85 1.29.10.2 ad
86 1.29.10.2 ad typedef struct mbr_args {
87 1.29.10.2 ad struct disklabel *lp;
88 1.29.10.2 ad void (*strat)(struct buf *);
89 1.29.10.2 ad struct buf *bp;
90 1.29.10.2 ad const char *msg;
91 1.29.10.2 ad int error;
92 1.29.10.2 ad int written; /* number of times we wrote label */
93 1.29.10.2 ad int found_mbr; /* set if disk has a valid mbr */
94 1.29.10.2 ad uint label_sector; /* where we found the label */
95 1.29.10.2 ad int action;
96 1.29.10.2 ad uint32_t secperunit;
97 1.29.10.2 ad #define READ_LABEL 1
98 1.29.10.2 ad #define UPDATE_LABEL 2
99 1.29.10.2 ad #define WRITE_LABEL 3
100 1.29.10.2 ad } mbr_args_t;
101 1.29.10.2 ad
102 1.29.10.2 ad static int validate_label(mbr_args_t *, uint);
103 1.29.10.2 ad static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
104 1.29.10.2 ad static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
105 1.29.10.2 ad
106 1.29.10.2 ad static int
107 1.29.10.2 ad read_sector(mbr_args_t *a, uint sector, int count)
108 1.29.10.2 ad {
109 1.29.10.2 ad int error;
110 1.29.10.2 ad
111 1.29.10.2 ad error = disk_read_sectors(a->strat, a->lp, a->bp, sector, count);
112 1.29.10.2 ad if (error != 0)
113 1.29.10.2 ad a->error = error;
114 1.29.10.2 ad return error;
115 1.29.10.2 ad }
116 1.29.10.2 ad
117 1.29.10.2 ad /*
118 1.29.10.2 ad * Scan MBR for partitions, call 'action' routine for each.
119 1.29.10.2 ad */
120 1.29.10.2 ad
121 1.29.10.2 ad static int
122 1.29.10.2 ad scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
123 1.29.10.2 ad {
124 1.29.10.2 ad mbr_partition_t ptns[MBR_PART_COUNT];
125 1.29.10.2 ad mbr_partition_t *dp;
126 1.29.10.2 ad struct mbr_sector *mbr;
127 1.29.10.2 ad uint ext_base, this_ext, next_ext;
128 1.29.10.2 ad int rval;
129 1.29.10.2 ad int i;
130 1.29.10.2 ad int j;
131 1.29.10.2 ad #ifdef COMPAT_386BSD_MBRPART
132 1.29.10.2 ad int dp_386bsd = -1;
133 1.29.10.2 ad int ap_386bsd = -1;
134 1.29.10.2 ad #endif
135 1.29.10.2 ad
136 1.29.10.2 ad ext_base = 0;
137 1.29.10.2 ad this_ext = 0;
138 1.29.10.2 ad for (;;) {
139 1.29.10.2 ad if (read_sector(a, this_ext, 1)) {
140 1.29.10.2 ad a->msg = "dos partition I/O error";
141 1.29.10.2 ad return SCAN_ERROR;
142 1.29.10.2 ad }
143 1.29.10.2 ad
144 1.29.10.2 ad /* Note: Magic number is little-endian. */
145 1.29.10.2 ad mbr = (void *)a->bp->b_data;
146 1.29.10.2 ad if (mbr->mbr_magic != htole16(MBR_MAGIC))
147 1.29.10.2 ad return SCAN_CONTINUE;
148 1.29.10.2 ad
149 1.29.10.2 ad /* Copy data out of buffer so action can use bp */
150 1.29.10.2 ad memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
151 1.29.10.2 ad
152 1.29.10.2 ad /* Look for drivers and skip them */
153 1.29.10.2 ad if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
154 1.29.10.2 ad /* We've found a DM6 DDO partition type (used by
155 1.29.10.2 ad * the Ontrack Disk Manager drivers).
156 1.29.10.2 ad *
157 1.29.10.2 ad * Ensure that there are no other partitions in the
158 1.29.10.2 ad * MBR and jump to the real partition table (stored
159 1.29.10.2 ad * in the first sector of the second track). */
160 1.29.10.2 ad bool ok = true;
161 1.29.10.2 ad
162 1.29.10.2 ad for (i = 1; i < MBR_PART_COUNT; i++)
163 1.29.10.2 ad if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
164 1.29.10.2 ad ok = false;
165 1.29.10.2 ad
166 1.29.10.2 ad if (ok) {
167 1.29.10.2 ad this_ext = le32toh(a->lp->d_secpercyl /
168 1.29.10.2 ad a->lp->d_ntracks);
169 1.29.10.2 ad continue;
170 1.29.10.2 ad }
171 1.29.10.2 ad }
172 1.29.10.2 ad
173 1.29.10.2 ad /* look for NetBSD partition */
174 1.29.10.2 ad next_ext = 0;
175 1.29.10.2 ad dp = ptns;
176 1.29.10.2 ad j = 0;
177 1.29.10.2 ad for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
178 1.29.10.2 ad if (dp->mbrp_type == MBR_PTYPE_UNUSED)
179 1.29.10.2 ad continue;
180 1.29.10.2 ad /* Check end of partition is inside disk limits */
181 1.29.10.2 ad if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
182 1.29.10.2 ad le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
183 1.29.10.2 ad /* This mbr doesn't look good.... */
184 1.29.10.2 ad a->msg = "mbr partition exceeds disk size";
185 1.29.10.2 ad /* ...but don't report this as an error (yet) */
186 1.29.10.2 ad return SCAN_CONTINUE;
187 1.29.10.2 ad }
188 1.29.10.2 ad a->found_mbr = 1;
189 1.29.10.2 ad if (MBR_IS_EXTENDED(dp->mbrp_type)) {
190 1.29.10.2 ad next_ext = le32toh(dp->mbrp_start);
191 1.29.10.2 ad continue;
192 1.29.10.2 ad }
193 1.29.10.2 ad #ifdef COMPAT_386BSD_MBRPART
194 1.29.10.2 ad if (dp->mbrp_type == MBR_PTYPE_386BSD) {
195 1.29.10.2 ad /*
196 1.29.10.2 ad * If more than one matches, take last,
197 1.29.10.2 ad * as NetBSD install tool does.
198 1.29.10.2 ad */
199 1.29.10.2 ad if (this_ext == 0) {
200 1.29.10.2 ad dp_386bsd = i;
201 1.29.10.2 ad ap_386bsd = j;
202 1.29.10.2 ad }
203 1.29.10.2 ad continue;
204 1.29.10.2 ad }
205 1.29.10.2 ad #endif
206 1.29.10.2 ad rval = (*actn)(a, dp, j, this_ext);
207 1.29.10.2 ad if (rval != SCAN_CONTINUE)
208 1.29.10.2 ad return rval;
209 1.29.10.2 ad j++;
210 1.29.10.2 ad }
211 1.29.10.2 ad if (next_ext == 0)
212 1.29.10.2 ad break;
213 1.29.10.2 ad if (ext_base == 0) {
214 1.29.10.2 ad ext_base = next_ext;
215 1.29.10.2 ad next_ext = 0;
216 1.29.10.2 ad }
217 1.29.10.2 ad next_ext += ext_base;
218 1.29.10.2 ad if (next_ext <= this_ext)
219 1.29.10.2 ad break;
220 1.29.10.2 ad this_ext = next_ext;
221 1.29.10.2 ad }
222 1.29.10.2 ad #ifdef COMPAT_386BSD_MBRPART
223 1.29.10.2 ad if (this_ext == 0 && dp_386bsd != -1)
224 1.29.10.2 ad return (*actn)(a, &ptns[dp_386bsd], ap_386bsd, 0);
225 1.29.10.2 ad #endif
226 1.29.10.2 ad return SCAN_CONTINUE;
227 1.29.10.2 ad }
228 1.29.10.2 ad
229 1.29.10.2 ad /*
230 1.29.10.2 ad * Attempt to read a disk label from a device
231 1.29.10.2 ad * using the indicated strategy routine.
232 1.29.10.2 ad * The label must be partly set up before this:
233 1.29.10.2 ad * secpercyl, secsize and anything required for a block i/o read
234 1.29.10.2 ad * operation in the driver's strategy/start routines
235 1.29.10.2 ad * must be filled in before calling us.
236 1.29.10.2 ad *
237 1.29.10.2 ad * If dos partition table requested, attempt to load it and
238 1.29.10.2 ad * find disklabel inside a DOS partition. Also, if bad block
239 1.29.10.2 ad * table needed, attempt to extract it as well. Return buffer
240 1.29.10.2 ad * for use in signalling errors if requested.
241 1.29.10.2 ad *
242 1.29.10.2 ad * Returns null on success and an error string on failure.
243 1.29.10.2 ad */
244 1.29.10.2 ad const char *
245 1.29.10.2 ad readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
246 1.29.10.2 ad struct cpu_disklabel *osdep)
247 1.29.10.2 ad {
248 1.29.10.2 ad struct dkbad *bdp;
249 1.29.10.2 ad int rval;
250 1.29.10.2 ad int i;
251 1.29.10.2 ad mbr_args_t a;
252 1.29.10.2 ad
253 1.29.10.2 ad memset(&a, 0, sizeof a);
254 1.29.10.2 ad a.lp = lp;
255 1.29.10.2 ad a.strat = strat;
256 1.29.10.2 ad a.action = READ_LABEL;
257 1.29.10.2 ad
258 1.29.10.2 ad /* minimal requirements for architypal disk label */
259 1.29.10.2 ad if (lp->d_secsize == 0)
260 1.29.10.2 ad lp->d_secsize = DEV_BSIZE;
261 1.29.10.2 ad if (lp->d_secperunit == 0)
262 1.29.10.2 ad lp->d_secperunit = 0x1fffffff;
263 1.29.10.2 ad a.secperunit = lp->d_secperunit;
264 1.29.10.2 ad lp->d_npartitions = RAW_PART + 1;
265 1.29.10.2 ad for (i = 0; i < RAW_PART; i++) {
266 1.29.10.2 ad lp->d_partitions[i].p_size = 0;
267 1.29.10.2 ad lp->d_partitions[i].p_offset = 0;
268 1.29.10.2 ad }
269 1.29.10.2 ad if (lp->d_partitions[RAW_PART].p_size == 0)
270 1.29.10.2 ad lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
271 1.29.10.2 ad lp->d_partitions[RAW_PART].p_offset = 0;
272 1.29.10.2 ad
273 1.29.10.2 ad /*
274 1.29.10.2 ad * Set partition 'a' to be the whole disk.
275 1.29.10.2 ad * Cleared if we find an mbr or a netbsd label.
276 1.29.10.2 ad */
277 1.29.10.2 ad lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
278 1.29.10.2 ad lp->d_partitions[0].p_fstype = FS_BSDFFS;
279 1.29.10.2 ad
280 1.29.10.2 ad /* get a buffer and initialize it */
281 1.29.10.2 ad a.bp = geteblk(2 * (int)lp->d_secsize);
282 1.29.10.2 ad a.bp->b_dev = dev;
283 1.29.10.2 ad
284 1.29.10.2 ad if (osdep)
285 1.29.10.2 ad /*
286 1.29.10.2 ad * Scan mbr searching for netbsd partition and saving
287 1.29.10.2 ad * bios partition information to use if the netbsd one
288 1.29.10.2 ad * is absent.
289 1.29.10.2 ad */
290 1.29.10.2 ad rval = scan_mbr(&a, look_netbsd_part);
291 1.29.10.2 ad else
292 1.29.10.2 ad rval = SCAN_CONTINUE;
293 1.29.10.2 ad
294 1.29.10.2 ad if (rval == SCAN_CONTINUE) {
295 1.29.10.2 ad /* Look at start of disk */
296 1.29.10.2 ad rval = validate_label(&a, 0);
297 1.29.10.2 ad }
298 1.29.10.2 ad
299 1.29.10.2 ad #if 0
300 1.29.10.2 ad /*
301 1.29.10.2 ad * Save sector where we found the label for the 'don't overwrite
302 1.29.10.2 ad * the label' check in bounds_check_with_label.
303 1.29.10.2 ad */
304 1.29.10.2 ad if (rval == SCAN_FOUND)
305 1.29.10.2 ad xxx->label_sector = a.label_sector;
306 1.29.10.2 ad #endif
307 1.29.10.2 ad
308 1.29.10.2 ad /* Obtain bad sector table if requested and present */
309 1.29.10.2 ad if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
310 1.29.10.2 ad struct dkbad *db;
311 1.29.10.2 ad int blkno;
312 1.29.10.2 ad
313 1.29.10.2 ad bdp = &osdep->bad;
314 1.29.10.2 ad i = 0;
315 1.29.10.2 ad rval = SCAN_ERROR;
316 1.29.10.2 ad do {
317 1.29.10.2 ad /* read a bad sector table */
318 1.29.10.2 ad blkno = lp->d_secperunit - lp->d_nsectors + i;
319 1.29.10.2 ad if (lp->d_secsize > DEV_BSIZE)
320 1.29.10.2 ad blkno *= lp->d_secsize / DEV_BSIZE;
321 1.29.10.2 ad else
322 1.29.10.2 ad blkno /= DEV_BSIZE / lp->d_secsize;
323 1.29.10.2 ad /* if successful, validate, otherwise try another */
324 1.29.10.2 ad if (read_sector(&a, blkno, 1)) {
325 1.29.10.2 ad a.msg = "bad sector table I/O error";
326 1.29.10.2 ad continue;
327 1.29.10.2 ad }
328 1.29.10.2 ad db = (struct dkbad *)(a.bp->b_data);
329 1.29.10.2 ad #define DKBAD_MAGIC 0x4321
330 1.29.10.2 ad if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
331 1.29.10.2 ad a.msg = "bad sector table corrupted";
332 1.29.10.2 ad continue;
333 1.29.10.2 ad }
334 1.29.10.2 ad rval = SCAN_FOUND;
335 1.29.10.2 ad *bdp = *db;
336 1.29.10.2 ad break;
337 1.29.10.2 ad } while (a.bp->b_error && (i += 2) < 10 &&
338 1.29.10.2 ad i < lp->d_nsectors);
339 1.29.10.2 ad }
340 1.29.10.2 ad
341 1.29.10.2 ad brelse(a.bp);
342 1.29.10.2 ad if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
343 1.29.10.2 ad return a.msg;
344 1.29.10.2 ad return NULL;
345 1.29.10.2 ad }
346 1.29.10.2 ad
347 1.29.10.2 ad static int
348 1.29.10.2 ad look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
349 1.29.10.2 ad {
350 1.29.10.2 ad struct partition *pp;
351 1.29.10.2 ad int ptn_base = ext_base + le32toh(dp->mbrp_start);
352 1.29.10.2 ad int rval;
353 1.29.10.2 ad
354 1.29.10.2 ad if (
355 1.29.10.2 ad #ifdef COMPAT_386BSD_MBRPART
356 1.29.10.2 ad dp->mbrp_type == MBR_PTYPE_386BSD ||
357 1.29.10.2 ad #endif
358 1.29.10.2 ad dp->mbrp_type == MBR_PTYPE_NETBSD) {
359 1.29.10.2 ad rval = validate_label(a, ptn_base);
360 1.29.10.2 ad
361 1.29.10.2 ad #if RAW_PART == 3
362 1.29.10.2 ad /* Put actual location where we found the label into ptn 2 */
363 1.29.10.2 ad if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
364 1.29.10.2 ad a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
365 1.29.10.2 ad a->lp->d_partitions[2].p_offset = ptn_base;
366 1.29.10.2 ad }
367 1.29.10.2 ad #endif
368 1.29.10.2 ad
369 1.29.10.2 ad /* If we got a netbsd label look no further */
370 1.29.10.2 ad if (rval == SCAN_FOUND)
371 1.29.10.2 ad return rval;
372 1.29.10.2 ad }
373 1.29.10.2 ad
374 1.29.10.2 ad /* Install main partitions into e..h and extended into i+ */
375 1.29.10.2 ad if (ext_base == 0)
376 1.29.10.2 ad slot += 4;
377 1.29.10.2 ad else {
378 1.29.10.2 ad slot = 4 + MBR_PART_COUNT;
379 1.29.10.2 ad pp = &a->lp->d_partitions[slot];
380 1.29.10.2 ad for (; slot < MAXPARTITIONS; pp++, slot++) {
381 1.29.10.2 ad /* This gets called twice - avoid duplicates */
382 1.29.10.2 ad if (pp->p_offset == ptn_base &&
383 1.29.10.2 ad pp->p_size == le32toh(dp->mbrp_size))
384 1.29.10.2 ad break;
385 1.29.10.2 ad if (pp->p_size == 0)
386 1.29.10.2 ad break;
387 1.29.10.2 ad }
388 1.29.10.2 ad }
389 1.29.10.2 ad
390 1.29.10.2 ad if (slot < MAXPARTITIONS) {
391 1.29.10.2 ad /* Stop 'a' being the entire disk */
392 1.29.10.2 ad a->lp->d_partitions[0].p_size = 0;
393 1.29.10.2 ad a->lp->d_partitions[0].p_fstype = 0;
394 1.29.10.2 ad
395 1.29.10.2 ad /* save partition info */
396 1.29.10.2 ad pp = &a->lp->d_partitions[slot];
397 1.29.10.2 ad pp->p_offset = ptn_base;
398 1.29.10.2 ad pp->p_size = le32toh(dp->mbrp_size);
399 1.29.10.2 ad pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
400 1.29.10.2 ad
401 1.29.10.2 ad if (slot >= a->lp->d_npartitions)
402 1.29.10.2 ad a->lp->d_npartitions = slot + 1;
403 1.29.10.2 ad }
404 1.29.10.2 ad
405 1.29.10.2 ad return SCAN_CONTINUE;
406 1.29.10.2 ad }
407 1.29.10.2 ad
408 1.29.10.2 ad
409 1.29.10.2 ad static int
410 1.29.10.2 ad validate_label(mbr_args_t *a, uint label_sector)
411 1.29.10.2 ad {
412 1.29.10.2 ad struct disklabel *dlp;
413 1.29.10.2 ad char *dlp_lim, *dlp_byte;
414 1.29.10.2 ad int error;
415 1.29.10.2 ad
416 1.29.10.2 ad /* Next, dig out disk label */
417 1.29.10.2 ad if (read_sector(a, label_sector, 2)) {
418 1.29.10.2 ad a->msg = "disk label read failed";
419 1.29.10.2 ad return SCAN_ERROR;
420 1.29.10.2 ad }
421 1.29.10.2 ad
422 1.29.10.2 ad /* Locate disk label within block and validate */
423 1.29.10.2 ad /*
424 1.29.10.2 ad * XXX (dsl) This search may be a waste of time, a lot of other i386
425 1.29.10.2 ad * code assumes the label is at offset LABELOFFSET (=0) in the sector.
426 1.29.10.2 ad *
427 1.29.10.2 ad * If we want to support disks from other netbsd ports, then the
428 1.29.10.2 ad * code should also allow for a shorter label nearer the end of
429 1.29.10.2 ad * the disk sector, and (IIRC) labels within 8k of the disk start.
430 1.29.10.2 ad */
431 1.29.10.2 ad dlp = (void *)a->bp->b_data;
432 1.29.10.2 ad dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
433 1.29.10.2 ad for (;; dlp = (void *)((char *)dlp + sizeof(long))) {
434 1.29.10.2 ad if ((char *)dlp > dlp_lim) {
435 1.29.10.2 ad if (a->action != WRITE_LABEL)
436 1.29.10.2 ad return SCAN_CONTINUE;
437 1.29.10.2 ad /* Write at arch. dependant default location */
438 1.29.10.2 ad dlp_byte = (char *)a->bp->b_data + LABELOFFSET;
439 1.29.10.2 ad if (label_sector)
440 1.29.10.2 ad dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
441 1.29.10.2 ad else
442 1.29.10.2 ad dlp_byte += LABELSECTOR * a->lp->d_secsize;
443 1.29.10.2 ad dlp = (void *)dlp_byte;
444 1.29.10.2 ad break;
445 1.29.10.2 ad }
446 1.29.10.2 ad if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
447 1.29.10.2 ad continue;
448 1.29.10.2 ad if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
449 1.29.10.2 ad a->msg = "disk label corrupted";
450 1.29.10.2 ad continue;
451 1.29.10.2 ad }
452 1.29.10.2 ad break;
453 1.29.10.2 ad }
454 1.29.10.2 ad
455 1.29.10.2 ad switch (a->action) {
456 1.29.10.2 ad case READ_LABEL:
457 1.29.10.2 ad *a->lp = *dlp;
458 1.29.10.2 ad if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp,
459 1.29.10.2 ad a->secperunit)) != NULL)
460 1.29.10.2 ad return SCAN_ERROR;
461 1.29.10.2 ad a->label_sector = label_sector;
462 1.29.10.2 ad return SCAN_FOUND;
463 1.29.10.2 ad case UPDATE_LABEL:
464 1.29.10.2 ad case WRITE_LABEL:
465 1.29.10.2 ad *dlp = *a->lp;
466 1.29.10.2 ad a->bp->b_flags &= ~(B_READ|B_DONE);
467 1.29.10.2 ad a->bp->b_flags |= B_WRITE;
468 1.29.10.2 ad (*a->strat)(a->bp);
469 1.29.10.2 ad error = biowait(a->bp);
470 1.29.10.2 ad if (error != 0) {
471 1.29.10.2 ad a->error = error;
472 1.29.10.2 ad a->msg = "disk label write failed";
473 1.29.10.2 ad return SCAN_ERROR;
474 1.29.10.2 ad }
475 1.29.10.2 ad a->written++;
476 1.29.10.2 ad /* Write label to all mbr partitions */
477 1.29.10.2 ad return SCAN_CONTINUE;
478 1.29.10.2 ad default:
479 1.29.10.2 ad return SCAN_ERROR;
480 1.29.10.2 ad }
481 1.29.10.2 ad }
482 1.29.10.2 ad
483 1.29.10.2 ad /*
484 1.29.10.2 ad * Check new disk label for sensibility
485 1.29.10.2 ad * before setting it.
486 1.29.10.2 ad */
487 1.29.10.2 ad int
488 1.29.10.2 ad setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
489 1.29.10.2 ad struct cpu_disklabel *osdep)
490 1.29.10.2 ad {
491 1.29.10.2 ad int i;
492 1.29.10.2 ad struct partition *opp, *npp;
493 1.29.10.2 ad
494 1.29.10.2 ad /* sanity clause */
495 1.29.10.2 ad if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
496 1.29.10.2 ad || (nlp->d_secsize % DEV_BSIZE) != 0)
497 1.29.10.2 ad return (EINVAL);
498 1.29.10.2 ad
499 1.29.10.2 ad /* special case to allow disklabel to be invalidated */
500 1.29.10.2 ad if (nlp->d_magic == 0xffffffff) {
501 1.29.10.2 ad *olp = *nlp;
502 1.29.10.2 ad return (0);
503 1.29.10.2 ad }
504 1.29.10.2 ad
505 1.29.10.2 ad if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
506 1.29.10.2 ad dkcksum(nlp) != 0)
507 1.29.10.2 ad return (EINVAL);
508 1.29.10.2 ad
509 1.29.10.2 ad /* XXX missing check if other dos partitions will be overwritten */
510 1.29.10.2 ad
511 1.29.10.2 ad while (openmask != 0) {
512 1.29.10.2 ad i = ffs(openmask) - 1;
513 1.29.10.2 ad openmask &= ~(1 << i);
514 1.29.10.2 ad if (i > nlp->d_npartitions)
515 1.29.10.2 ad return (EBUSY);
516 1.29.10.2 ad opp = &olp->d_partitions[i];
517 1.29.10.2 ad npp = &nlp->d_partitions[i];
518 1.29.10.2 ad /*
519 1.29.10.2 ad * Copy internally-set partition information
520 1.29.10.2 ad * if new label doesn't include it. XXX
521 1.29.10.2 ad */
522 1.29.10.2 ad if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
523 1.29.10.2 ad *npp = *opp;
524 1.29.10.2 ad continue;
525 1.29.10.2 ad }
526 1.29.10.2 ad if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
527 1.29.10.2 ad return (EBUSY);
528 1.29.10.2 ad }
529 1.29.10.2 ad nlp->d_checksum = 0;
530 1.29.10.2 ad nlp->d_checksum = dkcksum(nlp);
531 1.29.10.2 ad *olp = *nlp;
532 1.29.10.2 ad return (0);
533 1.29.10.2 ad }
534 1.29.10.2 ad
535 1.29.10.2 ad
536 1.29.10.2 ad /*
537 1.29.10.2 ad * Write disk label back to device after modification.
538 1.29.10.2 ad */
539 1.29.10.2 ad int
540 1.29.10.2 ad writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
541 1.29.10.2 ad struct cpu_disklabel *osdep)
542 1.29.10.2 ad {
543 1.29.10.2 ad mbr_args_t a;
544 1.29.10.2 ad
545 1.29.10.2 ad memset(&a, 0, sizeof a);
546 1.29.10.2 ad a.lp = lp;
547 1.29.10.2 ad a.strat = strat;
548 1.29.10.2 ad
549 1.29.10.2 ad /* get a buffer and initialize it */
550 1.29.10.2 ad a.bp = geteblk(2 * (int)lp->d_secsize);
551 1.29.10.2 ad a.bp->b_dev = dev;
552 1.29.10.2 ad
553 1.29.10.2 ad /* osdep => we expect an mbr with label in netbsd ptn */
554 1.29.10.2 ad a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
555 1.29.10.2 ad
556 1.29.10.2 ad /* Write/update the label to every netbsd mbr partition */
557 1.29.10.2 ad scan_mbr(&a, write_netbsd_label);
558 1.29.10.2 ad
559 1.29.10.2 ad /* Old write the label at the start of the volume on disks that
560 1.29.10.2 ad * don't have a valid mbr (always update an existing one) */
561 1.29.10.2 ad a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
562 1.29.10.2 ad validate_label(&a, 0);
563 1.29.10.2 ad
564 1.29.10.2 ad if (a.written == 0 && a.error == 0)
565 1.29.10.2 ad a.error = ESRCH;
566 1.29.10.2 ad
567 1.29.10.2 ad brelse(a.bp);
568 1.29.10.2 ad return a.error;
569 1.29.10.2 ad }
570 1.29.10.2 ad
571 1.29.10.2 ad static int
572 1.29.10.2 ad write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
573 1.29.10.2 ad {
574 1.29.10.2 ad int ptn_base = ext_base + le32toh(dp->mbrp_start);
575 1.29.10.2 ad
576 1.29.10.2 ad if (dp->mbrp_type != MBR_PTYPE_NETBSD)
577 1.29.10.2 ad return SCAN_CONTINUE;
578 1.29.10.2 ad
579 1.29.10.2 ad return validate_label(a, ptn_base);
580 1.29.10.2 ad }
581