subr_disk_mbr.c revision 1.55 1 1.55 kamil /* $NetBSD: subr_disk_mbr.c,v 1.55 2019/11/07 20:30:49 kamil Exp $ */
2 1.1 dsl
3 1.1 dsl /*
4 1.1 dsl * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5 1.1 dsl * All rights reserved.
6 1.1 dsl *
7 1.1 dsl * Redistribution and use in source and binary forms, with or without
8 1.1 dsl * modification, are permitted provided that the following conditions
9 1.1 dsl * are met:
10 1.1 dsl * 1. Redistributions of source code must retain the above copyright
11 1.1 dsl * notice, this list of conditions and the following disclaimer.
12 1.1 dsl * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 dsl * notice, this list of conditions and the following disclaimer in the
14 1.1 dsl * documentation and/or other materials provided with the distribution.
15 1.2 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 dsl * may be used to endorse or promote products derived from this software
17 1.1 dsl * without specific prior written permission.
18 1.1 dsl *
19 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 dsl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 dsl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 dsl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 dsl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 dsl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 dsl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 dsl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 dsl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 dsl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 dsl * SUCH DAMAGE.
30 1.1 dsl *
31 1.1 dsl * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
32 1.1 dsl */
33 1.1 dsl
34 1.1 dsl /*
35 1.1 dsl * Code to find a NetBSD label on a disk that contains an i386 style MBR.
36 1.1 dsl * The first NetBSD label found in the 2nd sector of a NetBSD partition
37 1.3 dsl * is used.
38 1.3 dsl * If we don't find a label searching the MBR, we look at the start of the
39 1.3 dsl * disk, if that fails then a label is faked up from the MBR.
40 1.1 dsl *
41 1.32 reinoud * If there isn't a disklabel or anything in the MBR then the disc is searched
42 1.32 reinoud * for ecma-167/iso9660/udf style partition indicators.
43 1.32 reinoud * Useful for media or files that contain single filesystems (etc).
44 1.1 dsl *
45 1.1 dsl * This code will read host endian netbsd labels from little endian MBR.
46 1.1 dsl *
47 1.1 dsl * Based on the i386 disksubr.c
48 1.1 dsl *
49 1.3 dsl * Since the mbr only has 32bit fields for sector addresses, we do the same.
50 1.3 dsl *
51 1.1 dsl * XXX There are potential problems writing labels to disks where there
52 1.1 dsl * is only space for 8 netbsd partitions but this code has been compiled
53 1.1 dsl * with MAXPARTITIONS=16.
54 1.1 dsl */
55 1.1 dsl
56 1.1 dsl #include <sys/cdefs.h>
57 1.55 kamil __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.55 2019/11/07 20:30:49 kamil Exp $");
58 1.1 dsl
59 1.1 dsl #include <sys/param.h>
60 1.1 dsl #include <sys/systm.h>
61 1.1 dsl #include <sys/buf.h>
62 1.27 dyoung #include <sys/bootblock.h>
63 1.1 dsl #include <sys/disklabel.h>
64 1.1 dsl #include <sys/disk.h>
65 1.1 dsl #include <sys/syslog.h>
66 1.32 reinoud #include <sys/vnode.h>
67 1.32 reinoud #include <sys/fcntl.h>
68 1.32 reinoud #include <sys/conf.h>
69 1.32 reinoud #include <sys/cdio.h>
70 1.37 pooka #include <sys/dkbad.h>
71 1.32 reinoud #include <fs/udf/ecma167-udf.h>
72 1.32 reinoud
73 1.32 reinoud #include <sys/kauth.h>
74 1.1 dsl
75 1.36 pooka #ifdef _KERNEL_OPT
76 1.1 dsl #include "opt_mbr.h"
77 1.36 pooka #endif /* _KERNEL_OPT */
78 1.1 dsl
79 1.13 dsl typedef struct mbr_partition mbr_partition_t;
80 1.13 dsl
81 1.17 dsl /*
82 1.38 pooka * We allocate a buffer 3 sectors large, and look in all....
83 1.17 dsl * That means we find labels written by other ports with different offsets.
84 1.17 dsl * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
85 1.17 dsl */
86 1.38 pooka #define SCANBLOCKS 3
87 1.38 pooka #define DISKLABEL_SIZE 404
88 1.38 pooka #if LABELSECTOR*DEV_BSIZE + LABELOFFSET > SCANBLOCKS*DEV_BSIZE - DISKLABEL_SIZE
89 1.41 pooka #if _MACHINE != ews4800mips /* XXX: fail silently, ews4800mips LABELSECTOR */
90 1.17 dsl #error Invalid LABELSECTOR or LABELOFFSET
91 1.17 dsl #endif
92 1.40 tsutsui #endif
93 1.17 dsl
94 1.1 dsl #define MBR_LABELSECTOR 1
95 1.1 dsl
96 1.1 dsl #define SCAN_CONTINUE 0
97 1.1 dsl #define SCAN_FOUND 1
98 1.1 dsl #define SCAN_ERROR 2
99 1.1 dsl
100 1.1 dsl typedef struct mbr_args {
101 1.1 dsl struct disklabel *lp;
102 1.1 dsl void (*strat)(struct buf *);
103 1.1 dsl struct buf *bp;
104 1.1 dsl const char *msg;
105 1.1 dsl int error;
106 1.3 dsl int written; /* number of times we wrote label */
107 1.17 dsl int found_mbr; /* set if disk has a valid mbr */
108 1.3 dsl uint label_sector; /* where we found the label */
109 1.17 dsl int action;
110 1.28 dyoung uint32_t secperunit;
111 1.1 dsl #define READ_LABEL 1
112 1.3 dsl #define UPDATE_LABEL 2
113 1.3 dsl #define WRITE_LABEL 3
114 1.17 dsl } mbr_args_t;
115 1.17 dsl
116 1.17 dsl static int validate_label(mbr_args_t *, uint);
117 1.13 dsl static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
118 1.13 dsl static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
119 1.1 dsl
120 1.47 rin #ifdef DISKLABEL_EI
121 1.47 rin static void swap_disklabel(struct disklabel *, struct disklabel *);
122 1.47 rin #endif
123 1.47 rin
124 1.1 dsl static int
125 1.17 dsl read_sector(mbr_args_t *a, uint sector, int count)
126 1.1 dsl {
127 1.1 dsl int error;
128 1.1 dsl
129 1.28 dyoung error = disk_read_sectors(a->strat, a->lp, a->bp, sector, count);
130 1.1 dsl if (error != 0)
131 1.1 dsl a->error = error;
132 1.1 dsl return error;
133 1.1 dsl }
134 1.1 dsl
135 1.6 perry /*
136 1.1 dsl * Scan MBR for partitions, call 'action' routine for each.
137 1.1 dsl */
138 1.1 dsl
139 1.1 dsl static int
140 1.13 dsl scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
141 1.1 dsl {
142 1.13 dsl mbr_partition_t ptns[MBR_PART_COUNT];
143 1.13 dsl mbr_partition_t *dp;
144 1.4 lukem struct mbr_sector *mbr;
145 1.1 dsl uint ext_base, this_ext, next_ext;
146 1.1 dsl int rval;
147 1.1 dsl int i;
148 1.18 christos int j;
149 1.1 dsl #ifdef COMPAT_386BSD_MBRPART
150 1.1 dsl int dp_386bsd = -1;
151 1.18 christos int ap_386bsd = -1;
152 1.1 dsl #endif
153 1.1 dsl
154 1.1 dsl ext_base = 0;
155 1.1 dsl this_ext = 0;
156 1.1 dsl for (;;) {
157 1.17 dsl if (read_sector(a, this_ext, 1)) {
158 1.1 dsl a->msg = "dos partition I/O error";
159 1.1 dsl return SCAN_ERROR;
160 1.1 dsl }
161 1.1 dsl
162 1.1 dsl /* Note: Magic number is little-endian. */
163 1.1 dsl mbr = (void *)a->bp->b_data;
164 1.4 lukem if (mbr->mbr_magic != htole16(MBR_MAGIC))
165 1.3 dsl return SCAN_CONTINUE;
166 1.1 dsl
167 1.46 matt /*
168 1.46 matt * If this is a protective MBR, bail now.
169 1.46 matt */
170 1.46 matt if (mbr->mbr_parts[0].mbrp_type == MBR_PTYPE_PMBR
171 1.46 matt && mbr->mbr_parts[1].mbrp_type == MBR_PTYPE_UNUSED
172 1.46 matt && mbr->mbr_parts[2].mbrp_type == MBR_PTYPE_UNUSED
173 1.46 matt && mbr->mbr_parts[3].mbrp_type == MBR_PTYPE_UNUSED)
174 1.46 matt return SCAN_CONTINUE;
175 1.46 matt
176 1.1 dsl /* Copy data out of buffer so action can use bp */
177 1.1 dsl memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
178 1.1 dsl
179 1.14 christos /* Look for drivers and skip them */
180 1.17 dsl if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
181 1.16 jmmv /* We've found a DM6 DDO partition type (used by
182 1.16 jmmv * the Ontrack Disk Manager drivers).
183 1.16 jmmv *
184 1.16 jmmv * Ensure that there are no other partitions in the
185 1.16 jmmv * MBR and jump to the real partition table (stored
186 1.16 jmmv * in the first sector of the second track). */
187 1.25 thorpej bool ok = true;
188 1.14 christos
189 1.14 christos for (i = 1; i < MBR_PART_COUNT; i++)
190 1.14 christos if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
191 1.25 thorpej ok = false;
192 1.14 christos
193 1.14 christos if (ok) {
194 1.15 christos this_ext = le32toh(a->lp->d_secpercyl /
195 1.15 christos a->lp->d_ntracks);
196 1.14 christos continue;
197 1.14 christos }
198 1.14 christos }
199 1.14 christos
200 1.1 dsl /* look for NetBSD partition */
201 1.1 dsl next_ext = 0;
202 1.1 dsl dp = ptns;
203 1.18 christos j = 0;
204 1.4 lukem for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
205 1.18 christos if (dp->mbrp_type == MBR_PTYPE_UNUSED)
206 1.1 dsl continue;
207 1.17 dsl /* Check end of partition is inside disk limits */
208 1.17 dsl if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
209 1.17 dsl le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
210 1.17 dsl /* This mbr doesn't look good.... */
211 1.17 dsl a->msg = "mbr partition exceeds disk size";
212 1.17 dsl /* ...but don't report this as an error (yet) */
213 1.17 dsl return SCAN_CONTINUE;
214 1.17 dsl }
215 1.17 dsl a->found_mbr = 1;
216 1.4 lukem if (MBR_IS_EXTENDED(dp->mbrp_type)) {
217 1.1 dsl next_ext = le32toh(dp->mbrp_start);
218 1.1 dsl continue;
219 1.1 dsl }
220 1.1 dsl #ifdef COMPAT_386BSD_MBRPART
221 1.4 lukem if (dp->mbrp_type == MBR_PTYPE_386BSD) {
222 1.1 dsl /*
223 1.1 dsl * If more than one matches, take last,
224 1.1 dsl * as NetBSD install tool does.
225 1.1 dsl */
226 1.18 christos if (this_ext == 0) {
227 1.1 dsl dp_386bsd = i;
228 1.18 christos ap_386bsd = j;
229 1.18 christos }
230 1.1 dsl continue;
231 1.1 dsl }
232 1.1 dsl #endif
233 1.18 christos rval = (*actn)(a, dp, j, this_ext);
234 1.1 dsl if (rval != SCAN_CONTINUE)
235 1.1 dsl return rval;
236 1.18 christos j++;
237 1.1 dsl }
238 1.1 dsl if (next_ext == 0)
239 1.1 dsl break;
240 1.1 dsl if (ext_base == 0) {
241 1.1 dsl ext_base = next_ext;
242 1.1 dsl next_ext = 0;
243 1.1 dsl }
244 1.1 dsl next_ext += ext_base;
245 1.1 dsl if (next_ext <= this_ext)
246 1.1 dsl break;
247 1.1 dsl this_ext = next_ext;
248 1.1 dsl }
249 1.1 dsl #ifdef COMPAT_386BSD_MBRPART
250 1.1 dsl if (this_ext == 0 && dp_386bsd != -1)
251 1.18 christos return (*actn)(a, &ptns[dp_386bsd], ap_386bsd, 0);
252 1.1 dsl #endif
253 1.1 dsl return SCAN_CONTINUE;
254 1.1 dsl }
255 1.1 dsl
256 1.32 reinoud
257 1.32 reinoud static void
258 1.32 reinoud scan_iso_vrs_session(mbr_args_t *a, uint32_t first_sector,
259 1.32 reinoud int *is_iso9660, int *is_udf)
260 1.32 reinoud {
261 1.32 reinoud struct vrs_desc *vrsd;
262 1.32 reinoud uint64_t vrs;
263 1.32 reinoud int sector_size;
264 1.33 reinoud int blks, inc;
265 1.32 reinoud
266 1.32 reinoud sector_size = a->lp->d_secsize;
267 1.32 reinoud blks = sector_size / DEV_BSIZE;
268 1.33 reinoud inc = MAX(1, 2048 / sector_size);
269 1.32 reinoud
270 1.32 reinoud /* by definition */
271 1.32 reinoud vrs = ((32*1024 + sector_size - 1) / sector_size)
272 1.32 reinoud + first_sector;
273 1.32 reinoud
274 1.32 reinoud /* read first vrs sector */
275 1.34 reinoud if (read_sector(a, vrs * blks, 1))
276 1.32 reinoud return;
277 1.32 reinoud
278 1.32 reinoud /* skip all CD001 records */
279 1.32 reinoud vrsd = a->bp->b_data;
280 1.33 reinoud /* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
281 1.32 reinoud while (memcmp(vrsd->identifier, "CD001", 5) == 0) {
282 1.32 reinoud /* for sure */
283 1.32 reinoud *is_iso9660 = first_sector;
284 1.32 reinoud
285 1.33 reinoud vrs += inc;
286 1.34 reinoud if (read_sector(a, vrs * blks, 1))
287 1.32 reinoud return;
288 1.32 reinoud }
289 1.32 reinoud
290 1.32 reinoud /* search for BEA01 */
291 1.32 reinoud vrsd = a->bp->b_data;
292 1.32 reinoud /* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
293 1.32 reinoud if (memcmp(vrsd->identifier, "BEA01", 5))
294 1.32 reinoud return;
295 1.32 reinoud
296 1.32 reinoud /* read successor */
297 1.33 reinoud vrs += inc;
298 1.34 reinoud if (read_sector(a, vrs * blks, 1))
299 1.32 reinoud return;
300 1.32 reinoud
301 1.32 reinoud /* check for NSR[23] */
302 1.32 reinoud vrsd = a->bp->b_data;
303 1.33 reinoud /* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
304 1.32 reinoud if (memcmp(vrsd->identifier, "NSR0", 4))
305 1.32 reinoud return;
306 1.32 reinoud
307 1.32 reinoud *is_udf = first_sector;
308 1.32 reinoud }
309 1.32 reinoud
310 1.32 reinoud
311 1.32 reinoud /*
312 1.32 reinoud * Scan for ISO Volume Recognition Sequences
313 1.32 reinoud */
314 1.32 reinoud
315 1.32 reinoud static int
316 1.32 reinoud scan_iso_vrs(mbr_args_t *a)
317 1.32 reinoud {
318 1.32 reinoud struct mmc_discinfo di;
319 1.32 reinoud struct mmc_trackinfo ti;
320 1.32 reinoud dev_t dev;
321 1.32 reinoud uint64_t sector;
322 1.32 reinoud int is_iso9660, is_udf;
323 1.32 reinoud int tracknr, sessionnr;
324 1.32 reinoud int new_session, error;
325 1.32 reinoud
326 1.32 reinoud is_iso9660 = is_udf = -1;
327 1.32 reinoud
328 1.32 reinoud /* parse all sessions of disc if we're on a SCSI MMC device */
329 1.32 reinoud if (a->lp->d_flags & D_SCSI_MMC) {
330 1.32 reinoud /* get disc info */
331 1.32 reinoud dev = a->bp->b_dev;
332 1.32 reinoud error = bdev_ioctl(dev, MMCGETDISCINFO, &di, FKIOCTL, curlwp);
333 1.32 reinoud if (error)
334 1.32 reinoud return SCAN_CONTINUE;
335 1.32 reinoud
336 1.32 reinoud /* go trough all (data) tracks */
337 1.32 reinoud sessionnr = -1;
338 1.32 reinoud for (tracknr = di.first_track;
339 1.32 reinoud tracknr <= di.first_track_last_session; tracknr++)
340 1.32 reinoud {
341 1.32 reinoud ti.tracknr = tracknr;
342 1.32 reinoud error = bdev_ioctl(dev, MMCGETTRACKINFO, &ti,
343 1.32 reinoud FKIOCTL, curlwp);
344 1.32 reinoud if (error)
345 1.32 reinoud return SCAN_CONTINUE;
346 1.32 reinoud new_session = (ti.sessionnr != sessionnr);
347 1.32 reinoud sessionnr = ti.sessionnr;
348 1.32 reinoud if (new_session) {
349 1.32 reinoud if (ti.flags & MMC_TRACKINFO_BLANK)
350 1.32 reinoud continue;
351 1.32 reinoud if (!(ti.flags & MMC_TRACKINFO_DATA))
352 1.32 reinoud continue;
353 1.32 reinoud sector = ti.track_start;
354 1.32 reinoud scan_iso_vrs_session(a, sector,
355 1.32 reinoud &is_iso9660, &is_udf);
356 1.32 reinoud }
357 1.32 reinoud }
358 1.32 reinoud } else {
359 1.32 reinoud /* try start of disc */
360 1.32 reinoud sector = 0;
361 1.32 reinoud scan_iso_vrs_session(a, sector, &is_iso9660, &is_udf);
362 1.32 reinoud }
363 1.32 reinoud
364 1.32 reinoud if ((is_iso9660 < 0) && (is_udf < 0))
365 1.32 reinoud return SCAN_CONTINUE;
366 1.32 reinoud
367 1.35 reinoud strncpy(a->lp->d_typename, "iso partition", 16);
368 1.35 reinoud
369 1.45 mlelstv /* adjust session information for iso9660 partition */
370 1.32 reinoud if (is_iso9660 >= 0) {
371 1.32 reinoud /* set 'a' partition to iso9660 */
372 1.32 reinoud a->lp->d_partitions[0].p_offset = 0;
373 1.32 reinoud a->lp->d_partitions[0].p_size = a->lp->d_secperunit;
374 1.32 reinoud a->lp->d_partitions[0].p_cdsession = is_iso9660;
375 1.32 reinoud a->lp->d_partitions[0].p_fstype = FS_ISO9660;
376 1.32 reinoud }
377 1.32 reinoud
378 1.45 mlelstv /* UDF doesn't care about the cd session specified here */
379 1.32 reinoud
380 1.32 reinoud return SCAN_FOUND;
381 1.32 reinoud }
382 1.32 reinoud
383 1.32 reinoud
384 1.1 dsl /*
385 1.1 dsl * Attempt to read a disk label from a device
386 1.1 dsl * using the indicated strategy routine.
387 1.1 dsl * The label must be partly set up before this:
388 1.1 dsl * secpercyl, secsize and anything required for a block i/o read
389 1.1 dsl * operation in the driver's strategy/start routines
390 1.1 dsl * must be filled in before calling us.
391 1.1 dsl *
392 1.1 dsl * If dos partition table requested, attempt to load it and
393 1.1 dsl * find disklabel inside a DOS partition. Also, if bad block
394 1.1 dsl * table needed, attempt to extract it as well. Return buffer
395 1.1 dsl * for use in signalling errors if requested.
396 1.1 dsl *
397 1.1 dsl * Returns null on success and an error string on failure.
398 1.1 dsl */
399 1.1 dsl const char *
400 1.13 dsl readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
401 1.13 dsl struct cpu_disklabel *osdep)
402 1.1 dsl {
403 1.1 dsl int rval;
404 1.1 dsl int i;
405 1.1 dsl mbr_args_t a;
406 1.1 dsl
407 1.3 dsl memset(&a, 0, sizeof a);
408 1.1 dsl a.lp = lp;
409 1.1 dsl a.strat = strat;
410 1.17 dsl a.action = READ_LABEL;
411 1.1 dsl
412 1.1 dsl /* minimal requirements for architypal disk label */
413 1.1 dsl if (lp->d_secsize == 0)
414 1.1 dsl lp->d_secsize = DEV_BSIZE;
415 1.1 dsl if (lp->d_secperunit == 0)
416 1.1 dsl lp->d_secperunit = 0x1fffffff;
417 1.28 dyoung a.secperunit = lp->d_secperunit;
418 1.1 dsl lp->d_npartitions = RAW_PART + 1;
419 1.1 dsl for (i = 0; i < RAW_PART; i++) {
420 1.1 dsl lp->d_partitions[i].p_size = 0;
421 1.1 dsl lp->d_partitions[i].p_offset = 0;
422 1.1 dsl }
423 1.1 dsl if (lp->d_partitions[RAW_PART].p_size == 0)
424 1.17 dsl lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
425 1.1 dsl lp->d_partitions[RAW_PART].p_offset = 0;
426 1.1 dsl
427 1.1 dsl /*
428 1.1 dsl * Set partition 'a' to be the whole disk.
429 1.1 dsl * Cleared if we find an mbr or a netbsd label.
430 1.1 dsl */
431 1.1 dsl lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
432 1.1 dsl lp->d_partitions[0].p_fstype = FS_BSDFFS;
433 1.1 dsl
434 1.32 reinoud /*
435 1.34 reinoud * Get a buffer big enough to read a disklabel in and initialize it
436 1.38 pooka * make it three sectors long for the validate_label(); see comment at
437 1.34 reinoud * start of file.
438 1.32 reinoud */
439 1.38 pooka a.bp = geteblk(SCANBLOCKS * (int)lp->d_secsize);
440 1.1 dsl a.bp->b_dev = dev;
441 1.1 dsl
442 1.1 dsl if (osdep)
443 1.1 dsl /*
444 1.1 dsl * Scan mbr searching for netbsd partition and saving
445 1.1 dsl * bios partition information to use if the netbsd one
446 1.1 dsl * is absent.
447 1.1 dsl */
448 1.1 dsl rval = scan_mbr(&a, look_netbsd_part);
449 1.1 dsl else
450 1.1 dsl rval = SCAN_CONTINUE;
451 1.1 dsl
452 1.3 dsl if (rval == SCAN_CONTINUE) {
453 1.1 dsl /* Look at start of disk */
454 1.17 dsl rval = validate_label(&a, 0);
455 1.1 dsl }
456 1.1 dsl
457 1.32 reinoud if (rval == SCAN_CONTINUE) {
458 1.32 reinoud rval = scan_iso_vrs(&a);
459 1.32 reinoud }
460 1.3 dsl #if 0
461 1.3 dsl /*
462 1.3 dsl * Save sector where we found the label for the 'don't overwrite
463 1.3 dsl * the label' check in bounds_check_with_label.
464 1.3 dsl */
465 1.3 dsl if (rval == SCAN_FOUND)
466 1.3 dsl xxx->label_sector = a.label_sector;
467 1.3 dsl #endif
468 1.3 dsl
469 1.1 dsl /* Obtain bad sector table if requested and present */
470 1.37 pooka #ifdef __HAVE_DISKLABEL_DKBAD
471 1.1 dsl if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
472 1.37 pooka struct dkbad *bdp, *db;
473 1.1 dsl int blkno;
474 1.1 dsl
475 1.1 dsl bdp = &osdep->bad;
476 1.1 dsl i = 0;
477 1.1 dsl rval = SCAN_ERROR;
478 1.1 dsl do {
479 1.1 dsl /* read a bad sector table */
480 1.1 dsl blkno = lp->d_secperunit - lp->d_nsectors + i;
481 1.1 dsl if (lp->d_secsize > DEV_BSIZE)
482 1.1 dsl blkno *= lp->d_secsize / DEV_BSIZE;
483 1.1 dsl else
484 1.1 dsl blkno /= DEV_BSIZE / lp->d_secsize;
485 1.1 dsl /* if successful, validate, otherwise try another */
486 1.17 dsl if (read_sector(&a, blkno, 1)) {
487 1.1 dsl a.msg = "bad sector table I/O error";
488 1.1 dsl continue;
489 1.1 dsl }
490 1.1 dsl db = (struct dkbad *)(a.bp->b_data);
491 1.1 dsl #define DKBAD_MAGIC 0x4321
492 1.1 dsl if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
493 1.1 dsl a.msg = "bad sector table corrupted";
494 1.1 dsl continue;
495 1.1 dsl }
496 1.1 dsl rval = SCAN_FOUND;
497 1.1 dsl *bdp = *db;
498 1.1 dsl break;
499 1.29 ad } while (a.bp->b_error && (i += 2) < 10 &&
500 1.1 dsl i < lp->d_nsectors);
501 1.1 dsl }
502 1.37 pooka #endif /* __HAVE_DISKLABEL_DKBAD */
503 1.1 dsl
504 1.30 ad brelse(a.bp, 0);
505 1.17 dsl if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
506 1.10 cube return a.msg;
507 1.8 reinoud return NULL;
508 1.1 dsl }
509 1.1 dsl
510 1.1 dsl static int
511 1.13 dsl look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
512 1.1 dsl {
513 1.1 dsl struct partition *pp;
514 1.1 dsl int ptn_base = ext_base + le32toh(dp->mbrp_start);
515 1.1 dsl int rval;
516 1.1 dsl
517 1.1 dsl if (
518 1.1 dsl #ifdef COMPAT_386BSD_MBRPART
519 1.4 lukem dp->mbrp_type == MBR_PTYPE_386BSD ||
520 1.1 dsl #endif
521 1.4 lukem dp->mbrp_type == MBR_PTYPE_NETBSD) {
522 1.17 dsl rval = validate_label(a, ptn_base);
523 1.1 dsl
524 1.12 dsl #if RAW_PART == 3
525 1.1 dsl /* Put actual location where we found the label into ptn 2 */
526 1.12 dsl if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
527 1.1 dsl a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
528 1.1 dsl a->lp->d_partitions[2].p_offset = ptn_base;
529 1.1 dsl }
530 1.12 dsl #endif
531 1.1 dsl
532 1.1 dsl /* If we got a netbsd label look no further */
533 1.1 dsl if (rval == SCAN_FOUND)
534 1.1 dsl return rval;
535 1.1 dsl }
536 1.1 dsl
537 1.1 dsl /* Install main partitions into e..h and extended into i+ */
538 1.1 dsl if (ext_base == 0)
539 1.1 dsl slot += 4;
540 1.1 dsl else {
541 1.4 lukem slot = 4 + MBR_PART_COUNT;
542 1.1 dsl pp = &a->lp->d_partitions[slot];
543 1.1 dsl for (; slot < MAXPARTITIONS; pp++, slot++) {
544 1.1 dsl /* This gets called twice - avoid duplicates */
545 1.1 dsl if (pp->p_offset == ptn_base &&
546 1.1 dsl pp->p_size == le32toh(dp->mbrp_size))
547 1.1 dsl break;
548 1.1 dsl if (pp->p_size == 0)
549 1.1 dsl break;
550 1.1 dsl }
551 1.1 dsl }
552 1.1 dsl
553 1.1 dsl if (slot < MAXPARTITIONS) {
554 1.1 dsl /* Stop 'a' being the entire disk */
555 1.1 dsl a->lp->d_partitions[0].p_size = 0;
556 1.1 dsl a->lp->d_partitions[0].p_fstype = 0;
557 1.1 dsl
558 1.1 dsl /* save partition info */
559 1.1 dsl pp = &a->lp->d_partitions[slot];
560 1.1 dsl pp->p_offset = ptn_base;
561 1.1 dsl pp->p_size = le32toh(dp->mbrp_size);
562 1.4 lukem pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
563 1.1 dsl
564 1.1 dsl if (slot >= a->lp->d_npartitions)
565 1.1 dsl a->lp->d_npartitions = slot + 1;
566 1.1 dsl }
567 1.1 dsl
568 1.1 dsl return SCAN_CONTINUE;
569 1.1 dsl }
570 1.1 dsl
571 1.55 kamil __noubsan
572 1.51 christos static bool
573 1.51 christos check_label_magic(const struct disklabel *dlp, uint32_t diskmagic)
574 1.51 christos {
575 1.54 kamil return memcmp(&dlp->d_magic, &diskmagic, sizeof(diskmagic)) == 0 &&
576 1.54 kamil memcmp(&dlp->d_magic2, &diskmagic, sizeof(diskmagic)) == 0;
577 1.51 christos }
578 1.1 dsl
579 1.47 rin #ifdef DISKLABEL_EI
580 1.47 rin /*
581 1.47 rin * - For read, convert a label to the native byte order.
582 1.47 rin * - For update or write, if a label already exists, keep its byte order.
583 1.47 rin * Otherwise, write a new label in the native byte order.
584 1.47 rin */
585 1.47 rin #endif
586 1.1 dsl static int
587 1.17 dsl validate_label(mbr_args_t *a, uint label_sector)
588 1.1 dsl {
589 1.53 kamil struct disklabel *dlp, tlp;
590 1.17 dsl char *dlp_lim, *dlp_byte;
591 1.1 dsl int error;
592 1.47 rin #ifdef DISKLABEL_EI
593 1.47 rin int swapped = 0;
594 1.47 rin uint16_t npartitions;
595 1.47 rin #endif
596 1.1 dsl
597 1.1 dsl /* Next, dig out disk label */
598 1.38 pooka if (read_sector(a, label_sector, SCANBLOCKS)) {
599 1.3 dsl a->msg = "disk label read failed";
600 1.1 dsl return SCAN_ERROR;
601 1.1 dsl }
602 1.1 dsl
603 1.1 dsl /* Locate disk label within block and validate */
604 1.1 dsl /*
605 1.1 dsl * XXX (dsl) This search may be a waste of time, a lot of other i386
606 1.1 dsl * code assumes the label is at offset LABELOFFSET (=0) in the sector.
607 1.1 dsl *
608 1.3 dsl * If we want to support disks from other netbsd ports, then the
609 1.1 dsl * code should also allow for a shorter label nearer the end of
610 1.1 dsl * the disk sector, and (IIRC) labels within 8k of the disk start.
611 1.1 dsl */
612 1.3 dsl dlp = (void *)a->bp->b_data;
613 1.26 christos dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
614 1.48 christos for (;; dlp = (void *)((char *)dlp + sizeof(uint32_t))) {
615 1.17 dsl if ((char *)dlp > dlp_lim) {
616 1.17 dsl if (a->action != WRITE_LABEL)
617 1.3 dsl return SCAN_CONTINUE;
618 1.42 wiz /* Write at arch. dependent default location */
619 1.26 christos dlp_byte = (char *)a->bp->b_data + LABELOFFSET;
620 1.17 dsl if (label_sector)
621 1.17 dsl dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
622 1.17 dsl else
623 1.17 dsl dlp_byte += LABELSECTOR * a->lp->d_secsize;
624 1.17 dsl dlp = (void *)dlp_byte;
625 1.53 kamil memcpy(&tlp, dlp, sizeof(tlp));
626 1.1 dsl break;
627 1.1 dsl }
628 1.53 kamil memcpy(&tlp, dlp, sizeof(tlp));
629 1.53 kamil if (!check_label_magic(&tlp, DISKMAGIC))
630 1.47 rin #ifdef DISKLABEL_EI
631 1.47 rin {
632 1.53 kamil if (!check_label_magic(&tlp, bswap32(DISKMAGIC)))
633 1.47 rin continue;
634 1.47 rin
635 1.47 rin /*
636 1.47 rin * The label is in the other byte order. We need to
637 1.47 rin * checksum before swapping the byte order.
638 1.47 rin */
639 1.53 kamil npartitions = bswap16(tlp.d_npartitions);
640 1.47 rin if (npartitions > MAXPARTITIONS ||
641 1.53 kamil dkcksum_sized(&tlp, npartitions) != 0)
642 1.47 rin goto corrupted;
643 1.47 rin
644 1.47 rin swapped = 1;
645 1.47 rin }
646 1.47 rin #else
647 1.17 dsl continue;
648 1.47 rin #endif
649 1.53 kamil else if (tlp.d_npartitions > MAXPARTITIONS ||
650 1.53 kamil dkcksum(&tlp) != 0) {
651 1.47 rin #ifdef DISKLABEL_EI
652 1.47 rin corrupted:
653 1.47 rin #endif
654 1.17 dsl a->msg = "disk label corrupted";
655 1.17 dsl continue;
656 1.17 dsl }
657 1.17 dsl break;
658 1.3 dsl }
659 1.3 dsl
660 1.17 dsl switch (a->action) {
661 1.3 dsl case READ_LABEL:
662 1.47 rin #ifdef DISKLABEL_EI
663 1.47 rin if (swapped)
664 1.53 kamil swap_disklabel(a->lp, &tlp);
665 1.47 rin else
666 1.53 kamil *a->lp = tlp;
667 1.47 rin #else
668 1.53 kamil *a->lp = tlp;
669 1.47 rin #endif
670 1.28 dyoung if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp,
671 1.28 dyoung a->secperunit)) != NULL)
672 1.28 dyoung return SCAN_ERROR;
673 1.3 dsl a->label_sector = label_sector;
674 1.1 dsl return SCAN_FOUND;
675 1.3 dsl case UPDATE_LABEL:
676 1.3 dsl case WRITE_LABEL:
677 1.47 rin #ifdef DISKLABEL_EI
678 1.47 rin /* DO NOT swap a->lp itself for later references. */
679 1.47 rin if (swapped)
680 1.53 kamil swap_disklabel(&tlp, a->lp);
681 1.47 rin else
682 1.53 kamil tlp = *a->lp;
683 1.47 rin #else
684 1.53 kamil tlp = *a->lp;
685 1.47 rin #endif
686 1.53 kamil memcpy(dlp, &tlp, sizeof(tlp));
687 1.31 ad a->bp->b_oflags &= ~BO_DONE;
688 1.31 ad a->bp->b_flags &= ~B_READ;
689 1.3 dsl a->bp->b_flags |= B_WRITE;
690 1.3 dsl (*a->strat)(a->bp);
691 1.3 dsl error = biowait(a->bp);
692 1.3 dsl if (error != 0) {
693 1.3 dsl a->error = error;
694 1.3 dsl a->msg = "disk label write failed";
695 1.3 dsl return SCAN_ERROR;
696 1.3 dsl }
697 1.3 dsl a->written++;
698 1.3 dsl /* Write label to all mbr partitions */
699 1.3 dsl return SCAN_CONTINUE;
700 1.3 dsl default:
701 1.3 dsl return SCAN_ERROR;
702 1.1 dsl }
703 1.1 dsl }
704 1.1 dsl
705 1.1 dsl /*
706 1.1 dsl * Write disk label back to device after modification.
707 1.1 dsl */
708 1.1 dsl int
709 1.13 dsl writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
710 1.13 dsl struct cpu_disklabel *osdep)
711 1.1 dsl {
712 1.1 dsl mbr_args_t a;
713 1.1 dsl
714 1.3 dsl memset(&a, 0, sizeof a);
715 1.1 dsl a.lp = lp;
716 1.1 dsl a.strat = strat;
717 1.1 dsl
718 1.1 dsl /* get a buffer and initialize it */
719 1.38 pooka a.bp = geteblk(SCANBLOCKS * (int)lp->d_secsize);
720 1.1 dsl a.bp->b_dev = dev;
721 1.1 dsl
722 1.17 dsl /* osdep => we expect an mbr with label in netbsd ptn */
723 1.17 dsl a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
724 1.17 dsl
725 1.17 dsl /* Write/update the label to every netbsd mbr partition */
726 1.17 dsl scan_mbr(&a, write_netbsd_label);
727 1.3 dsl
728 1.17 dsl /* Old write the label at the start of the volume on disks that
729 1.17 dsl * don't have a valid mbr (always update an existing one) */
730 1.17 dsl a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
731 1.17 dsl validate_label(&a, 0);
732 1.1 dsl
733 1.3 dsl if (a.written == 0 && a.error == 0)
734 1.1 dsl a.error = ESRCH;
735 1.1 dsl
736 1.30 ad brelse(a.bp, 0);
737 1.1 dsl return a.error;
738 1.1 dsl }
739 1.1 dsl
740 1.1 dsl static int
741 1.21 yamt write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
742 1.1 dsl {
743 1.1 dsl int ptn_base = ext_base + le32toh(dp->mbrp_start);
744 1.1 dsl
745 1.4 lukem if (dp->mbrp_type != MBR_PTYPE_NETBSD)
746 1.1 dsl return SCAN_CONTINUE;
747 1.1 dsl
748 1.17 dsl return validate_label(a, ptn_base);
749 1.1 dsl }
750 1.47 rin
751 1.47 rin #ifdef DISKLABEL_EI
752 1.47 rin /*
753 1.47 rin * from sh3/disksubr.c with modifications:
754 1.47 rin * - update d_checksum properly
755 1.47 rin * - replace memcpy(9) by memmove(9) as a precaution
756 1.47 rin */
757 1.47 rin static void
758 1.47 rin swap_disklabel(struct disklabel *nlp, struct disklabel *olp)
759 1.47 rin {
760 1.47 rin int i;
761 1.47 rin uint16_t npartitions;
762 1.47 rin
763 1.47 rin #define SWAP16(x) nlp->x = bswap16(olp->x)
764 1.47 rin #define SWAP32(x) nlp->x = bswap32(olp->x)
765 1.47 rin
766 1.47 rin SWAP32(d_magic);
767 1.47 rin SWAP16(d_type);
768 1.47 rin SWAP16(d_subtype);
769 1.47 rin /* Do not need to swap char strings. */
770 1.47 rin memmove(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
771 1.47 rin
772 1.47 rin /* XXX What should we do for d_un (an union of char and pointers) ? */
773 1.47 rin memmove(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
774 1.47 rin
775 1.47 rin SWAP32(d_secsize);
776 1.47 rin SWAP32(d_nsectors);
777 1.47 rin SWAP32(d_ntracks);
778 1.47 rin SWAP32(d_ncylinders);
779 1.47 rin SWAP32(d_secpercyl);
780 1.47 rin SWAP32(d_secperunit);
781 1.47 rin
782 1.47 rin SWAP16(d_sparespertrack);
783 1.47 rin SWAP16(d_sparespercyl);
784 1.47 rin
785 1.47 rin SWAP32(d_acylinders);
786 1.47 rin
787 1.47 rin SWAP16(d_rpm);
788 1.47 rin SWAP16(d_interleave);
789 1.47 rin SWAP16(d_trackskew);
790 1.47 rin SWAP16(d_cylskew);
791 1.47 rin SWAP32(d_headswitch);
792 1.47 rin SWAP32(d_trkseek);
793 1.47 rin SWAP32(d_flags);
794 1.47 rin for (i = 0; i < NDDATA; i++)
795 1.47 rin SWAP32(d_drivedata[i]);
796 1.47 rin for (i = 0; i < NSPARE; i++)
797 1.47 rin SWAP32(d_spare[i]);
798 1.47 rin SWAP32(d_magic2);
799 1.47 rin /* d_checksum is updated later. */
800 1.47 rin
801 1.47 rin SWAP16(d_npartitions);
802 1.47 rin SWAP32(d_bbsize);
803 1.47 rin SWAP32(d_sbsize);
804 1.47 rin for (i = 0; i < MAXPARTITIONS; i++) {
805 1.47 rin SWAP32(d_partitions[i].p_size);
806 1.47 rin SWAP32(d_partitions[i].p_offset);
807 1.47 rin SWAP32(d_partitions[i].p_fsize);
808 1.47 rin /* p_fstype and p_frag is uint8_t, so no need to swap. */
809 1.47 rin nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
810 1.47 rin nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
811 1.47 rin SWAP16(d_partitions[i].p_cpg);
812 1.47 rin }
813 1.47 rin
814 1.47 rin #undef SWAP16
815 1.47 rin #undef SWAP32
816 1.47 rin
817 1.47 rin /* Update checksum in the target endian. */
818 1.47 rin nlp->d_checksum = 0;
819 1.47 rin npartitions = nlp->d_magic == DISKMAGIC ?
820 1.47 rin nlp->d_npartitions : olp->d_npartitions;
821 1.47 rin /*
822 1.47 rin * npartitions can be larger than MAXPARTITIONS when the label was not
823 1.47 rin * validated by setdisklabel. If so, the label is intentionally(?)
824 1.47 rin * corrupted and checksum should be meaningless.
825 1.47 rin */
826 1.47 rin if (npartitions <= MAXPARTITIONS)
827 1.47 rin nlp->d_checksum = dkcksum_sized(nlp, npartitions);
828 1.47 rin }
829 1.47 rin #endif /* DISKLABEL_EI */
830