Remove macro definitions for BPF_MTAP
[dragonfly.git] / sys / kern / subr_diskmbr.c
1 /*-
2  * Copyright (c) 1994 Bruce D. Evans.
3  * All rights reserved.
4  *
5  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      from: @(#)ufs_disksubr.c        7.16 (Berkeley) 5/4/91
37  *      from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
38  * $FreeBSD: src/sys/kern/subr_diskmbr.c,v 1.45 2000/01/28 10:22:07 bde Exp $
39  * $DragonFly: src/sys/kern/subr_diskmbr.c,v 1.5 2003/11/10 06:12:13 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/conf.h>
46 #ifdef PC98
47 #define PC98_ATCOMPAT
48 #define dsinit                  atcompat_dsinit
49 #endif
50 #include <sys/disklabel.h>
51 #define DOSPTYP_EXTENDED        5
52 #define DOSPTYP_EXTENDEDX       15
53 #define DOSPTYP_ONTRACK         84
54 #include <sys/diskslice.h>
55 #include <sys/diskmbr.h>
56 #include <sys/malloc.h>
57 #include <sys/syslog.h>
58 #include <sys/device.h>
59
60 #define TRACE(str)      do { if (dsi_debug) printf str; } while (0)
61
62 static volatile u_char dsi_debug;
63
64 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
65         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
66         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
67         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
68         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
69 };
70
71 static int check_part (char *sname, struct dos_partition *dp,
72                            u_long offset, int nsectors, int ntracks,
73                            u_long mbr_offset);
74 static void mbr_extended (dev_t dev, struct disklabel *lp,
75                               struct diskslices *ssp, u_long ext_offset,
76                               u_long ext_size, u_long base_ext_offset,
77                               int nsectors, int ntracks, u_long mbr_offset,
78                               int level);
79 static int mbr_setslice (char *sname, struct disklabel *lp,
80                              struct diskslice *sp, struct dos_partition *dp,
81                              u_long br_offset);
82
83 static int
84 check_part(sname, dp, offset, nsectors, ntracks, mbr_offset )
85         char    *sname;
86         struct dos_partition *dp;
87         u_long  offset;
88         int     nsectors;
89         int     ntracks;
90         u_long  mbr_offset;
91 {
92         int     chs_ecyl;
93         int     chs_esect;
94         int     chs_scyl;
95         int     chs_ssect;
96         int     error;
97         u_long  esector;
98         u_long  esector1;
99         u_long  secpercyl;
100         u_long  ssector;
101         u_long  ssector1;
102
103         secpercyl = (u_long)nsectors * ntracks;
104         chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
105         chs_ssect = DPSECT(dp->dp_ssect);
106         ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl
107                   + mbr_offset;
108         ssector1 = offset + dp->dp_start;
109
110         /*
111          * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
112          * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
113          * apart from the cylinder being reduced modulo 1024.  Always allow
114          * 1023/255/63.
115          */
116         if ((ssector < ssector1
117              && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
118                   && chs_scyl == 1023)
119                  || (secpercyl != 0
120                      && (ssector1 - ssector) % (1024 * secpercyl) == 0)))
121             || (dp->dp_scyl == 255 && dp->dp_shd == 255
122                 && dp->dp_ssect == 255)) {
123                 TRACE(("%s: C/H/S start %d/%d/%d, start %lu: allow\n",
124                        sname, chs_scyl, dp->dp_shd, chs_ssect, ssector1));
125                 ssector = ssector1;
126         }
127
128         chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect);
129         chs_esect = DPSECT(dp->dp_esect);
130         esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl
131                   + mbr_offset;
132         esector1 = ssector1 + dp->dp_size - 1;
133
134         /* Allow certain bogus C/H/S values for esector, as above. */
135         if ((esector < esector1
136              && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
137                   && chs_ecyl == 1023)
138                  || (secpercyl != 0
139                      && (esector1 - esector) % (1024 * secpercyl) == 0)))
140             || (dp->dp_ecyl == 255 && dp->dp_ehd == 255
141                 && dp->dp_esect == 255)) {
142                 TRACE(("%s: C/H/S end %d/%d/%d, end %lu: allow\n",
143                        sname, chs_ecyl, dp->dp_ehd, chs_esect, esector1));
144                 esector = esector1;
145         }
146
147         error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
148         if (bootverbose)
149                 printf("%s: type 0x%x, start %lu, end = %lu, size %lu %s\n",
150                        sname, dp->dp_typ, ssector1, esector1,
151                        (u_long)dp->dp_size, error ? "" : ": OK");
152         if (ssector != ssector1 && bootverbose)
153                 printf("%s: C/H/S start %d/%d/%d (%lu) != start %lu: invalid\n",
154                        sname, chs_scyl, dp->dp_shd, chs_ssect,
155                        ssector, ssector1);
156         if (esector != esector1 && bootverbose)
157                 printf("%s: C/H/S end %d/%d/%d (%lu) != end %lu: invalid\n",
158                        sname, chs_ecyl, dp->dp_ehd, chs_esect,
159                        esector, esector1);
160         return (error);
161 }
162
163 int
164 dsinit(dev, lp, sspp)
165         dev_t   dev;
166         struct disklabel *lp;
167         struct diskslices **sspp;
168 {
169         struct buf *bp;
170         u_char  *cp;
171         int     dospart;
172         struct dos_partition *dp;
173         struct dos_partition *dp0;
174         struct dos_partition dpcopy[NDOSPART];
175         int     error;
176         int     max_ncyls;
177         int     max_nsectors;
178         int     max_ntracks;
179         u_long  mbr_offset;
180         char    partname[2];
181         u_long  secpercyl;
182         char    *sname;
183         struct diskslice *sp;
184         struct diskslices *ssp;
185
186         mbr_offset = DOSBBSECTOR;
187 reread_mbr:
188         /* Read master boot record. */
189         bp = geteblk((int)lp->d_secsize);
190         bp->b_dev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
191         bp->b_blkno = mbr_offset;
192         bp->b_bcount = lp->d_secsize;
193         bp->b_flags |= B_READ;
194         BUF_STRATEGY(bp, 1);
195         if (biowait(bp) != 0) {
196                 diskerr(bp, "reading primary partition table: error",
197                     LOG_PRINTF, 0, (struct disklabel *)NULL);
198                 printf("\n");
199                 error = EIO;
200                 goto done;
201         }
202
203         /* Weakly verify it. */
204         cp = bp->b_data;
205         sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, partname);
206         if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
207                 if (bootverbose)
208                         printf("%s: invalid primary partition table: no magic\n",
209                                sname);
210                 error = EINVAL;
211                 goto done;
212         }
213
214         /* Make a copy of the partition table to avoid alignment problems. */
215         memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
216
217         dp0 = &dpcopy[0];
218
219         /* Check for "Ontrack Diskmanager". */
220         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
221                 if (dp->dp_typ == DOSPTYP_ONTRACK) {
222                         if (bootverbose)
223                                 printf(
224             "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
225                         bp->b_flags |= B_INVAL | B_AGE;
226                         brelse(bp);
227                         mbr_offset = 63;
228                         goto reread_mbr;
229                 }
230         }
231
232         if (bcmp(dp0, historical_bogus_partition_table,
233                  sizeof historical_bogus_partition_table) == 0) {
234                 TRACE(("%s: invalid primary partition table: historical\n",
235                        sname));
236                 error = EINVAL;
237                 goto done;
238         }
239
240         /* Guess the geometry. */
241         /*
242          * TODO:
243          * Perhaps skip entries with 0 size.
244          * Perhaps only look at entries of type DOSPTYP_386BSD.
245          */
246         max_ncyls = 0;
247         max_nsectors = 0;
248         max_ntracks = 0;
249         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
250                 int     ncyls;
251                 int     nsectors;
252                 int     ntracks;
253
254                 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
255                 if (max_ncyls < ncyls)
256                         max_ncyls = ncyls;
257                 nsectors = DPSECT(dp->dp_esect);
258                 if (max_nsectors < nsectors)
259                         max_nsectors = nsectors;
260                 ntracks = dp->dp_ehd + 1;
261                 if (max_ntracks < ntracks)
262                         max_ntracks = ntracks;
263         }
264
265         /*
266          * Check that we have guessed the geometry right by checking the
267          * partition entries.
268          */
269         /*
270          * TODO:
271          * As above.
272          * Check for overlaps.
273          * Check against d_secperunit if the latter is reliable.
274          */
275         error = 0;
276         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
277                 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
278                     && dp->dp_start == 0 && dp->dp_size == 0)
279                         continue;
280                 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
281                                RAW_PART, partname);
282
283                 /*
284                  * Temporarily ignore errors from this check.  We could
285                  * simplify things by accepting the table eariler if we
286                  * always ignore errors here.  Perhaps we should always
287                  * accept the table if the magic is right but not let
288                  * bad entries affect the geometry.
289                  */
290                 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
291                            mbr_offset);
292         }
293         if (error != 0)
294                 goto done;
295
296         /*
297          * Accept the DOS partition table.
298          * First adjust the label (we have been careful not to change it
299          * before we can guarantee success).
300          */
301         secpercyl = (u_long)max_nsectors * max_ntracks;
302         if (secpercyl != 0) {
303 #if 0
304                 u_long  secperunit;
305 #endif
306
307                 lp->d_nsectors = max_nsectors;
308                 lp->d_ntracks = max_ntracks;
309                 lp->d_secpercyl = secpercyl;
310                 /*
311                  * Temporarily, don't even consider adjusting the drive's
312                  * size, since the adjusted size may exceed the hardware's
313                  * addressing capabilities.  The adjustment helped mainly
314                  * for ancient MFM drives with > 1024 cylinders, but now
315                  * breaks at least IDE drives with 63*16*65536 sectors if
316                  * they are controlled by the wd driver in CHS mode.
317                  */
318 #if 0
319                 secperunit = secpercyl * max_ncyls;
320                 if (lp->d_secperunit < secperunit)
321                         lp->d_secperunit = secperunit;
322 #endif
323                 lp->d_ncylinders = lp->d_secperunit / secpercyl;
324         }
325
326         /*
327          * We are passed a pointer to a suitably initialized minimal
328          * slices "struct" with no dangling pointers in it.  Replace it
329          * by a maximal one.  This usually oversizes the "struct", but
330          * enlarging it while searching for logical drives would be
331          * inconvenient.
332          */
333         free(*sspp, M_DEVBUF);
334         ssp = dsmakeslicestruct(MAX_SLICES, lp);
335         *sspp = ssp;
336
337         /* Initialize normal slices. */
338         sp = &ssp->dss_slices[BASE_SLICE];
339         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) {
340                 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
341                                RAW_PART, partname);
342                 (void)mbr_setslice(sname, lp, sp, dp, mbr_offset);
343         }
344         ssp->dss_nslices = BASE_SLICE + NDOSPART;
345
346         /* Handle extended partitions. */
347         sp -= NDOSPART;
348         for (dospart = 0; dospart < NDOSPART; dospart++, sp++)
349                 if (sp->ds_type == DOSPTYP_EXTENDED ||
350                     sp->ds_type == DOSPTYP_EXTENDEDX)
351                         mbr_extended(bp->b_dev, lp, ssp,
352                                      sp->ds_offset, sp->ds_size, sp->ds_offset,
353                                      max_nsectors, max_ntracks, mbr_offset, 1);
354
355         /*
356          * mbr_extended() abuses ssp->dss_nslices for the number of slices
357          * that would be found if there were no limit on the number of slices
358          * in *ssp.  Cut it back now.
359          */
360         if (ssp->dss_nslices > MAX_SLICES)
361                 ssp->dss_nslices = MAX_SLICES;
362
363 done:
364         bp->b_flags |= B_INVAL | B_AGE;
365         brelse(bp);
366         if (error == EINVAL)
367                 error = 0;
368         return (error);
369 }
370
371 void
372 mbr_extended(dev, lp, ssp, ext_offset, ext_size, base_ext_offset, nsectors,
373              ntracks, mbr_offset, level)
374         dev_t   dev;
375         struct disklabel *lp;
376         struct diskslices *ssp;
377         u_long  ext_offset;
378         u_long  ext_size;
379         u_long  base_ext_offset;
380         int     nsectors;
381         int     ntracks;
382         u_long  mbr_offset;
383         int     level;
384 {
385         struct buf *bp;
386         u_char  *cp;
387         int     dospart;
388         struct dos_partition *dp;
389         struct dos_partition dpcopy[NDOSPART];
390         u_long  ext_offsets[NDOSPART];
391         u_long  ext_sizes[NDOSPART];
392         char    partname[2];
393         int     slice;
394         char    *sname;
395         struct diskslice *sp;
396
397         if (level >= 16) {
398                 printf(
399         "%s: excessive recursion in search for slices; aborting search\n",
400                        devtoname(dev));
401                 return;
402         }
403
404         /* Read extended boot record. */
405         bp = geteblk((int)lp->d_secsize);
406         bp->b_dev = dev;
407         bp->b_blkno = ext_offset;
408         bp->b_bcount = lp->d_secsize;
409         bp->b_flags |= B_READ;
410         BUF_STRATEGY(bp, 1);
411         if (biowait(bp) != 0) {
412                 diskerr(bp, "reading extended partition table: error",
413                     LOG_PRINTF, 0, (struct disklabel *)NULL);
414                 printf("\n");
415                 goto done;
416         }
417
418         /* Weakly verify it. */
419         cp = bp->b_data;
420         if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
421                 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART,
422                                partname);
423                 if (bootverbose)
424                         printf("%s: invalid extended partition table: no magic\n",
425                                sname);
426                 goto done;
427         }
428
429         /* Make a copy of the partition table to avoid alignment problems. */
430         memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
431
432         slice = ssp->dss_nslices;
433         for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
434             dospart++, dp++) {
435                 ext_sizes[dospart] = 0;
436                 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
437                     && dp->dp_start == 0 && dp->dp_size == 0)
438                         continue;
439                 if (dp->dp_typ == DOSPTYP_EXTENDED ||
440                     dp->dp_typ == DOSPTYP_EXTENDEDX) {
441                         static char buf[32];
442
443                         sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
444                                        RAW_PART, partname);
445                         snprintf(buf, sizeof(buf), "%s", sname);
446                         if (strlen(buf) < sizeof buf - 11)
447                                 strcat(buf, "<extended>");
448                         check_part(buf, dp, base_ext_offset, nsectors,
449                                    ntracks, mbr_offset);
450                         ext_offsets[dospart] = base_ext_offset + dp->dp_start;
451                         ext_sizes[dospart] = dp->dp_size;
452                 } else {
453                         sname = dsname(dev, dkunit(dev), slice, RAW_PART,
454                                        partname);
455                         check_part(sname, dp, ext_offset, nsectors, ntracks,
456                                    mbr_offset);
457                         if (slice >= MAX_SLICES) {
458                                 printf("%s: too many slices\n", sname);
459                                 slice++;
460                                 continue;
461                         }
462                         sp = &ssp->dss_slices[slice];
463                         if (mbr_setslice(sname, lp, sp, dp, ext_offset) != 0)
464                                 continue;
465                         slice++;
466                 }
467         }
468         ssp->dss_nslices = slice;
469
470         /* If we found any more slices, recursively find all the subslices. */
471         for (dospart = 0; dospart < NDOSPART; dospart++)
472                 if (ext_sizes[dospart] != 0)
473                         mbr_extended(dev, lp, ssp, ext_offsets[dospart],
474                                      ext_sizes[dospart], base_ext_offset,
475                                      nsectors, ntracks, mbr_offset, ++level);
476
477 done:
478         bp->b_flags |= B_INVAL | B_AGE;
479         brelse(bp);
480 }
481
482 static int
483 mbr_setslice(sname, lp, sp, dp, br_offset)
484         char    *sname;
485         struct disklabel *lp;
486         struct diskslice *sp;
487         struct dos_partition *dp;
488         u_long  br_offset;
489 {
490         u_long  offset;
491         u_long  size;
492
493         offset = br_offset + dp->dp_start;
494         if (offset > lp->d_secperunit || offset < br_offset) {
495                 printf(
496                 "%s: slice starts beyond end of the disk: rejecting it\n",
497                        sname);
498                 return (1);
499         }
500         size = lp->d_secperunit - offset;
501         if (size >= dp->dp_size)
502                 size = dp->dp_size;
503         else
504                 printf(
505 "%s: slice extends beyond end of disk: truncating from %lu to %lu sectors\n",
506                        sname, (u_long)dp->dp_size, size);
507         sp->ds_offset = offset;
508         sp->ds_size = size;
509         sp->ds_type = dp->dp_typ;
510 #ifdef PC98_ATCOMPAT
511         /* Fake FreeBSD(98). */
512         if (sp->ds_type == DOSPTYP_386BSD)
513                 sp->ds_type = 0x94;
514 #endif
515 #if 0
516         lp->d_subtype |= (lp->d_subtype & 3) | dospart | DSTYPE_INDOSPART;
517 #endif
518         return (0);
519 }
520
521 #ifdef __alpha__
522 void
523 alpha_fix_srm_checksum(bp)
524         struct buf *bp;
525 {
526         u_int64_t *p;
527         u_int64_t sum;
528         int i;
529
530         p = (u_int64_t *) bp->b_data;
531         sum = 0;
532         for (i = 0; i < 63; i++)
533                 sum += p[i];
534         p[63] = sum;
535 }
536 #endif