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