telnet.1: Remove unnecessary .Pp
[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.26 2007/06/19 06:07:57 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 #include <sys/diskslice.h>
47 #define DOSPTYP_EXTENDED        5
48 #define DOSPTYP_EXTENDEDX       15
49 #define DOSPTYP_ONTRACK         84
50 #include <sys/diskslice.h>
51 #include <sys/diskmbr.h>
52 #include <sys/disk.h>
53 #include <sys/malloc.h>
54 #include <sys/syslog.h>
55 #include <sys/device.h>
56
57 #define TRACE(str)      do { if (dsi_debug) kprintf str; } while (0)
58
59 static volatile u_char dsi_debug;
60
61 /*
62  * This is what we have embedded in every boot1 for supporting the bogus
63  * "Dangerously Dedicated" mode. However, the old table is broken because
64  * it has an illegal geometry in it - it specifies 256 heads (heads = end
65  * head + 1) which causes nasty stuff when that wraps to zero in bios code.
66  * eg: divide by zero etc. This caused the dead-thinkpad problem, numerous
67  * SCSI bios crashes, EFI to crash, etc.
68  *
69  * We still have to recognize the old table though, even though we stopped
70  * inflicting it upon the world.
71  */
72 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
73         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
74         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
75         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
76         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
77 };
78 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
79         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
80         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
81         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
82         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
83 };
84
85 static int check_part (char *sname, struct dos_partition *dp,
86                            u_int64_t offset, int nsectors, int ntracks,
87                            u_int64_t mbr_offset);
88 static void mbr_extended (cdev_t dev, struct disk_info *info,
89                               struct diskslices *ssp, u_int64_t ext_offset,
90                               u_int64_t ext_size, u_int64_t base_ext_offset,
91                               int nsectors, int ntracks, u_int64_t mbr_offset,
92                               int level);
93 static int mbr_setslice (char *sname, struct disk_info *info,
94                              struct diskslice *sp, struct dos_partition *dp,
95                              u_int64_t br_offset);
96
97
98 int
99 mbrinit(cdev_t dev, struct disk_info *info, struct diskslices **sspp)
100 {
101         struct buf *bp;
102         u_char  *cp;
103         int     dospart;
104         struct dos_partition *dp;
105         struct dos_partition *dp0;
106         struct dos_partition dpcopy[NDOSPART];
107         int     error;
108         int     max_ncyls;
109         int     max_nsectors;
110         int     max_ntracks;
111         u_int64_t mbr_offset;
112         char    partname[2];
113         u_long  secpercyl;
114         char    *sname;
115         struct diskslice *sp;
116         struct diskslices *ssp;
117         cdev_t wdev;
118
119         mbr_offset = DOSBBSECTOR;
120 reread_mbr:
121         if (info->d_media_blksize & DEV_BMASK)
122                 return (EIO);
123         /* Read master boot record. */
124         wdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), WHOLE_SLICE_PART);
125         bp = geteblk((int)info->d_media_blksize);
126         bp->b_bio1.bio_offset = (off_t)mbr_offset * info->d_media_blksize;
127         bp->b_bcount = info->d_media_blksize;
128         bp->b_cmd = BUF_CMD_READ;
129         dev_dstrategy(wdev, &bp->b_bio1);
130         if (biowait(bp) != 0) {
131                 diskerr(&bp->b_bio1, wdev, 
132                         "reading primary partition table: error",
133                         LOG_PRINTF, 0);
134                 kprintf("\n");
135                 error = EIO;
136                 goto done;
137         }
138
139         /* Weakly verify it. */
140         cp = bp->b_data;
141         sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, WHOLE_SLICE_PART, partname);
142         if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
143                 if (bootverbose)
144                         kprintf("%s: invalid primary partition table: no magic\n",
145                                sname);
146                 error = EINVAL;
147                 goto done;
148         }
149
150         /* Make a copy of the partition table to avoid alignment problems. */
151         memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
152
153         dp0 = &dpcopy[0];
154
155         /*
156          * Check for "Ontrack Diskmanager" or GPT.  If a GPT is found in
157          * the first dos partition, ignore the rest of the MBR and go
158          * to GPT processing.
159          */
160         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
161                 if (dospart == 0 &&
162                     (dp->dp_typ == DOSPTYP_PMBR || dp->dp_typ == DOSPTYP_GPT)) {
163                         if (bootverbose)
164                                 kprintf(
165             "%s: Found GPT in slice #%d\n", sname, dospart + 1);
166                         error = gptinit(dev, info, sspp);
167                         goto done;
168                 }
169
170                 if (dp->dp_typ == DOSPTYP_ONTRACK) {
171                         if (bootverbose)
172                                 kprintf(
173             "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
174                         bp->b_flags |= B_INVAL | B_AGE;
175                         brelse(bp);
176                         mbr_offset = 63;
177                         goto reread_mbr;
178                 }
179         }
180
181         if (bcmp(dp0, historical_bogus_partition_table,
182                  sizeof historical_bogus_partition_table) == 0 ||
183             bcmp(dp0, historical_bogus_partition_table_fixed,
184                  sizeof historical_bogus_partition_table_fixed) == 0) {
185 #if 0
186                 TRACE(("%s: invalid primary partition table: historical\n",
187                        sname));
188 #endif /* 0 */
189                 if (bootverbose)
190                         kprintf(
191      "%s: invalid primary partition table: Dangerously Dedicated (ignored)\n",
192                                sname);
193                 error = EINVAL;
194                 goto done;
195         }
196
197         /* Guess the geometry. */
198         /*
199          * TODO:
200          * Perhaps skip entries with 0 size.
201          * Perhaps only look at entries of type DOSPTYP_386BSD.
202          */
203         max_ncyls = 0;
204         max_nsectors = 0;
205         max_ntracks = 0;
206         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
207                 int     ncyls;
208                 int     nsectors;
209                 int     ntracks;
210
211                 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
212                 if (max_ncyls < ncyls)
213                         max_ncyls = ncyls;
214                 nsectors = DPSECT(dp->dp_esect);
215                 if (max_nsectors < nsectors)
216                         max_nsectors = nsectors;
217                 ntracks = dp->dp_ehd + 1;
218                 if (max_ntracks < ntracks)
219                         max_ntracks = ntracks;
220         }
221
222         /*
223          * Check that we have guessed the geometry right by checking the
224          * partition entries.
225          */
226         /*
227          * TODO:
228          * As above.
229          * Check for overlaps.
230          * Check against d_secperunit if the latter is reliable.
231          */
232         error = 0;
233         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
234                 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
235                     && dp->dp_start == 0 && dp->dp_size == 0)
236                         continue;
237                 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
238                                WHOLE_SLICE_PART, partname);
239
240                 /*
241                  * Temporarily ignore errors from this check.  We could
242                  * simplify things by accepting the table eariler if we
243                  * always ignore errors here.  Perhaps we should always
244                  * accept the table if the magic is right but not let
245                  * bad entries affect the geometry.
246                  */
247                 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
248                            mbr_offset);
249         }
250         if (error != 0)
251                 goto done;
252
253         /*
254          * Accept the DOS partition table.
255          *
256          * Adjust the disk information structure with updated CHS
257          * conversion parameters, but only use values extracted from
258          * the primary partition table.
259          *
260          * NOTE!  Regardless of our having to deal with this old cruft,
261          * we do not screw around with the info->d_media* parameters.
262          */
263         secpercyl = (u_long)max_nsectors * max_ntracks;
264         if (secpercyl != 0 && mbr_offset == DOSBBSECTOR) {
265                 info->d_secpertrack = max_nsectors;
266                 info->d_nheads = max_ntracks;
267                 info->d_secpercyl = secpercyl;
268                 info->d_ncylinders = info->d_media_blocks / secpercyl;
269         }
270
271         /*
272          * We are passed a pointer to a suitably initialized minimal
273          * slices "struct" with no dangling pointers in it.  Replace it
274          * by a maximal one.  This usually oversizes the "struct", but
275          * enlarging it while searching for logical drives would be
276          * inconvenient.
277          */
278         kfree(*sspp, M_DEVBUF);
279         ssp = dsmakeslicestruct(MAX_SLICES, info);
280         *sspp = ssp;
281
282         /* Initialize normal slices. */
283         sp = &ssp->dss_slices[BASE_SLICE];
284         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) {
285                 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
286                                WHOLE_SLICE_PART, partname);
287                 (void)mbr_setslice(sname, info, sp, dp, mbr_offset);
288         }
289         ssp->dss_nslices = BASE_SLICE + NDOSPART;
290
291         /* Handle extended partitions. */
292         sp -= NDOSPART;
293         for (dospart = 0; dospart < NDOSPART; dospart++, sp++) {
294                 if (sp->ds_type == DOSPTYP_EXTENDED ||
295                     sp->ds_type == DOSPTYP_EXTENDEDX) {
296                         mbr_extended(wdev, info, ssp,
297                                      sp->ds_offset, sp->ds_size, sp->ds_offset,
298                                      max_nsectors, max_ntracks, mbr_offset, 1);
299                 }
300         }
301
302         /*
303          * mbr_extended() abuses ssp->dss_nslices for the number of slices
304          * that would be found if there were no limit on the number of slices
305          * in *ssp.  Cut it back now.
306          */
307         if (ssp->dss_nslices > MAX_SLICES)
308                 ssp->dss_nslices = MAX_SLICES;
309
310 done:
311         bp->b_flags |= B_INVAL | B_AGE;
312         brelse(bp);
313         if (error == EINVAL)
314                 error = 0;
315         return (error);
316 }
317
318 static int
319 check_part(char *sname, struct dos_partition *dp, u_int64_t offset,
320             int nsectors, int ntracks, u_int64_t mbr_offset)
321 {
322         int     chs_ecyl;
323         int     chs_esect;
324         int     chs_scyl;
325         int     chs_ssect;
326         int     error;
327         u_long  secpercyl;
328         u_int64_t esector;
329         u_int64_t esector1;
330         u_int64_t ssector;
331         u_int64_t ssector1;
332
333         secpercyl = (u_long)nsectors * ntracks;
334         chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
335         chs_ssect = DPSECT(dp->dp_ssect);
336         ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl
337                   + mbr_offset;
338         ssector1 = offset + dp->dp_start;
339
340         /*
341          * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
342          * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
343          * apart from the cylinder being reduced modulo 1024.  Always allow
344          * 1023/255/63, because this is the official way to represent
345          * pure-LBA for the starting position.
346          */
347         if ((ssector < ssector1
348              && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
349                   && chs_scyl == 1023)
350                  || (secpercyl != 0
351                      && (ssector1 - ssector) % (1024 * secpercyl) == 0)))
352             || (dp->dp_scyl == 255 && dp->dp_shd == 255
353                 && dp->dp_ssect == 255)) {
354                 TRACE(("%s: C/H/S start %d/%d/%d, start %llu: allow\n",
355                        sname, chs_scyl, dp->dp_shd, chs_ssect,
356                        (long long)ssector1));
357                 ssector = ssector1;
358         }
359
360         chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect);
361         chs_esect = DPSECT(dp->dp_esect);
362         esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl
363                   + mbr_offset;
364         esector1 = ssector1 + dp->dp_size - 1;
365
366         /*
367          * Allow certain bogus C/H/S values for esector, as above. However,
368          * heads == 255 isn't really legal and causes some BIOS crashes. The
369          * correct value to indicate a pure-LBA end is 1023/heads-1/sectors -
370          * usually 1023/254/63. "heads" is base 0, "sectors" is base 1.
371          */
372         if ((esector < esector1
373              && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
374                   && chs_ecyl == 1023)
375                  || (secpercyl != 0
376                      && (esector1 - esector) % (1024 * secpercyl) == 0)))
377             || (dp->dp_ecyl == 255 && dp->dp_ehd == 255
378                 && dp->dp_esect == 255)) {
379                 TRACE(("%s: C/H/S end %d/%d/%d, end %llu: allow\n",
380                        sname, chs_ecyl, dp->dp_ehd, chs_esect,
381                        (long long)esector1));
382                 esector = esector1;
383         }
384
385         error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
386         if (bootverbose)
387                 kprintf("%s: type 0x%x, start %llu, end = %llu, size %u %s\n",
388                        sname, dp->dp_typ,
389                        (long long)ssector1, (long long)esector1,
390                        dp->dp_size, (error ? "" : ": OK"));
391         if (ssector != ssector1 && bootverbose)
392                 kprintf("%s: C/H/S start %d/%d/%d (%llu) != start %llu: invalid\n",
393                        sname, chs_scyl, dp->dp_shd, chs_ssect,
394                        (long long)ssector, (long long)ssector1);
395         if (esector != esector1 && bootverbose)
396                 kprintf("%s: C/H/S end %d/%d/%d (%llu) != end %llu: invalid\n",
397                        sname, chs_ecyl, dp->dp_ehd, chs_esect,
398                        (long long)esector, (long long)esector1);
399         return (error);
400 }
401
402 static
403 void
404 mbr_extended(cdev_t dev, struct disk_info *info, struct diskslices *ssp,
405             u_int64_t ext_offset, u_int64_t ext_size, u_int64_t base_ext_offset,
406             int nsectors, int ntracks, u_int64_t mbr_offset, int level)
407 {
408         struct buf *bp;
409         u_char  *cp;
410         int     dospart;
411         struct dos_partition *dp;
412         struct dos_partition dpcopy[NDOSPART];
413         u_int64_t ext_offsets[NDOSPART];
414         u_int64_t ext_sizes[NDOSPART];
415         char    partname[2];
416         int     slice;
417         char    *sname;
418         struct diskslice *sp;
419
420         if (level >= 16) {
421                 kprintf(
422         "%s: excessive recursion in search for slices; aborting search\n",
423                        devtoname(dev));
424                 return;
425         }
426
427         /* Read extended boot record. */
428         bp = geteblk((int)info->d_media_blksize);
429         bp->b_bio1.bio_offset = (off_t)ext_offset * info->d_media_blksize;
430         bp->b_bcount = info->d_media_blksize;
431         bp->b_cmd = BUF_CMD_READ;
432         dev_dstrategy(dev, &bp->b_bio1);
433         if (biowait(bp) != 0) {
434                 diskerr(&bp->b_bio1, dev,
435                         "reading extended partition table: error",
436                         LOG_PRINTF, 0);
437                 kprintf("\n");
438                 goto done;
439         }
440
441         /* Weakly verify it. */
442         cp = bp->b_data;
443         if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
444                 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, WHOLE_SLICE_PART,
445                                partname);
446                 if (bootverbose)
447                         kprintf("%s: invalid extended partition table: no magic\n",
448                                sname);
449                 goto done;
450         }
451
452         /* Make a copy of the partition table to avoid alignment problems. */
453         memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
454
455         slice = ssp->dss_nslices;
456         for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
457             dospart++, dp++) {
458                 ext_sizes[dospart] = 0;
459                 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
460                     && dp->dp_start == 0 && dp->dp_size == 0)
461                         continue;
462                 if (dp->dp_typ == DOSPTYP_EXTENDED ||
463                     dp->dp_typ == DOSPTYP_EXTENDEDX) {
464                         static char buf[32];
465
466                         sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
467                                        WHOLE_SLICE_PART, partname);
468                         ksnprintf(buf, sizeof(buf), "%s", sname);
469                         if (strlen(buf) < sizeof buf - 11)
470                                 strcat(buf, "<extended>");
471                         check_part(buf, dp, base_ext_offset, nsectors,
472                                    ntracks, mbr_offset);
473                         ext_offsets[dospart] = base_ext_offset + dp->dp_start;
474                         ext_sizes[dospart] = dp->dp_size;
475                 } else {
476                         sname = dsname(dev, dkunit(dev), slice, WHOLE_SLICE_PART,
477                                        partname);
478                         check_part(sname, dp, ext_offset, nsectors, ntracks,
479                                    mbr_offset);
480                         if (slice >= MAX_SLICES) {
481                                 kprintf("%s: too many slices\n", sname);
482                                 slice++;
483                                 continue;
484                         }
485                         sp = &ssp->dss_slices[slice];
486                         if (mbr_setslice(sname, info, sp, dp, ext_offset) != 0)
487                                 continue;
488                         slice++;
489                 }
490         }
491         ssp->dss_nslices = slice;
492
493         /* If we found any more slices, recursively find all the subslices. */
494         for (dospart = 0; dospart < NDOSPART; dospart++) {
495                 if (ext_sizes[dospart] != 0) {
496                         mbr_extended(dev, info, ssp, ext_offsets[dospart],
497                                      ext_sizes[dospart], base_ext_offset,
498                                      nsectors, ntracks, mbr_offset, ++level);
499                 }
500         }
501
502 done:
503         bp->b_flags |= B_INVAL | B_AGE;
504         brelse(bp);
505 }
506
507 static int
508 mbr_setslice(char *sname, struct disk_info *info, struct diskslice *sp,
509             struct dos_partition *dp, u_int64_t br_offset)
510 {
511         u_int64_t       offset;
512         u_int64_t       size;
513
514         offset = br_offset + dp->dp_start;
515         if (offset > info->d_media_blocks || offset < br_offset) {
516                 kprintf(
517                 "%s: slice starts beyond end of the disk: rejecting it\n",
518                        sname);
519                 return (1);
520         }
521         size = info->d_media_blocks - offset;
522         if (size >= dp->dp_size) {
523                 if (dp->dp_size == 0xFFFFFFFFU) {
524                         kprintf("%s: slice >2TB, using media size instead "
525                                 "of slice table size\n", sname);
526                 } else {
527                         size = dp->dp_size;
528                 }
529         } else {
530                 kprintf("%s: slice extends beyond end of disk: "
531                         "truncating from %u to %llu sectors\n",
532                         sname, dp->dp_size, (unsigned long long)size);
533         }
534         sp->ds_offset = offset;
535         sp->ds_size = size;
536         sp->ds_type = dp->dp_typ;
537         bzero(&sp->ds_type_uuid, sizeof(sp->ds_type_uuid));
538         bzero(&sp->ds_stor_uuid, sizeof(sp->ds_type_uuid));
539
540         /*
541          * Slices do not overlap with the parent (if any).
542          */
543         sp->ds_reserved = 0;
544         return (0);
545 }