11.56Smartin/* $NetBSD: disksubr.c,v 1.56 2019/12/15 12:50:39 martin Exp $ */ 21.3Scgd 31.1Sderaadt/* 41.1Sderaadt * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 51.1Sderaadt * All rights reserved. 61.1Sderaadt * 71.1Sderaadt * Redistribution and use in source and binary forms, with or without 81.1Sderaadt * modification, are permitted provided that the following conditions 91.1Sderaadt * are met: 101.1Sderaadt * 1. Redistributions of source code must retain the above copyright 111.1Sderaadt * notice, this list of conditions and the following disclaimer. 121.1Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 131.1Sderaadt * notice, this list of conditions and the following disclaimer in the 141.1Sderaadt * documentation and/or other materials provided with the distribution. 151.41Sagc * 3. Neither the name of the University nor the names of its contributors 161.1Sderaadt * may be used to endorse or promote products derived from this software 171.1Sderaadt * without specific prior written permission. 181.1Sderaadt * 191.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221.1Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291.1Sderaadt * SUCH DAMAGE. 301.1Sderaadt * 311.3Scgd * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 321.1Sderaadt */ 331.40Slukem 341.40Slukem#include <sys/cdefs.h> 351.56Smartin__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.56 2019/12/15 12:50:39 martin Exp $"); 361.54Smatt 371.54Smatt#include "opt_compat_ultrix.h" 381.1Sderaadt 391.8Smycroft#include <sys/param.h> 401.8Smycroft#include <sys/systm.h> 411.8Smycroft#include <sys/buf.h> 421.9Sjonathan#include <sys/disk.h> 431.8Smycroft#include <sys/disklabel.h> 441.1Sderaadt 451.33Smatt#include <dev/dec/dec_boot.h> 461.28Smhitch#include <ufs/ufs/dinode.h> /* XXX for fs.h */ 471.28Smhitch#include <ufs/ffs/fs.h> /* XXX for BBSIZE & SBSIZE */ 481.5Sjonathan 491.50Sdslconst char *compat_label(dev_t dev, void (*strat)(struct buf *bp), 501.50Sdsl struct disklabel *lp, struct cpu_disklabel *osdep); /* XXX */ 511.5Sjonathan 521.1Sderaadt/* 531.1Sderaadt * Attempt to read a disk label from a device 541.35Swiz * using the indicated strategy routine. 551.1Sderaadt * The label must be partly set up before this: 561.1Sderaadt * secpercyl and anything required in the strategy routine 571.1Sderaadt * (e.g., sector size) must be filled in before calling us. 581.1Sderaadt * Returns null on success and an error string on failure. 591.1Sderaadt */ 601.38Sdslconst char * 611.53Stsutsuireaddisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 621.53Stsutsui struct cpu_disklabel *osdep) 631.1Sderaadt{ 641.20Ssimonb struct buf *bp; 651.1Sderaadt struct disklabel *dlp; 661.42Sdrochner const char *msg = NULL; 671.1Sderaadt 681.1Sderaadt if (lp->d_secperunit == 0) 691.1Sderaadt lp->d_secperunit = 0x1fffffff; 701.1Sderaadt lp->d_npartitions = 1; 711.1Sderaadt if (lp->d_partitions[0].p_size == 0) 721.1Sderaadt lp->d_partitions[0].p_size = 0x1fffffff; 731.1Sderaadt lp->d_partitions[0].p_offset = 0; 741.1Sderaadt 751.1Sderaadt bp = geteblk((int)lp->d_secsize); 761.1Sderaadt bp->b_dev = dev; 771.1Sderaadt bp->b_blkno = LABELSECTOR; 781.1Sderaadt bp->b_bcount = lp->d_secsize; 791.34Schs bp->b_flags |= B_READ; 801.26Sthorpej bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 811.1Sderaadt (*strat)(bp); 821.1Sderaadt if (biowait(bp)) { 831.1Sderaadt msg = "I/O error"; 841.32Sthorpej } else for (dlp = (struct disklabel *)bp->b_data; 851.45Ssimonb dlp <= (struct disklabel *) 861.45Ssimonb ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 871.1Sderaadt dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 881.1Sderaadt if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 891.1Sderaadt if (msg == NULL) 901.1Sderaadt msg = "no disk label"; 911.1Sderaadt } else if (dlp->d_npartitions > MAXPARTITIONS || 921.1Sderaadt dkcksum(dlp) != 0) 931.1Sderaadt msg = "disk label corrupted"; 941.1Sderaadt else { 951.1Sderaadt *lp = *dlp; 961.1Sderaadt msg = NULL; 971.1Sderaadt break; 981.1Sderaadt } 991.1Sderaadt } 1001.46Sad brelse(bp, 0); 1011.28Smhitch /* 1021.28Smhitch * If no NetBSD label was found, check for an Ultrix label and 1031.28Smhitch * construct tne incore label from the Ultrix partition information. 1041.28Smhitch */ 1051.28Smhitch if (msg != NULL) { 1061.28Smhitch msg = compat_label(dev, strat, lp, osdep); 1071.28Smhitch if (msg == NULL) { 1081.28Smhitch printf("WARNING: using Ultrix partition information\n"); 1091.28Smhitch /* set geometry? */ 1101.28Smhitch } 1111.28Smhitch } 1121.28Smhitch/* XXX If no NetBSD label or Ultrix label found, generate default label here */ 1131.53Stsutsui return msg; 1141.5Sjonathan} 1151.5Sjonathan 1161.5Sjonathan/* 1171.5Sjonathan * Given a buffer bp, try and interpret it as an Ultrix disk label, 1181.5Sjonathan * putting the partition info into a native NetBSD label 1191.5Sjonathan */ 1201.42Sdrochnerconst char * 1211.53Stsutsuicompat_label(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 1221.53Stsutsui struct cpu_disklabel *osdep) 1231.5Sjonathan{ 1241.21Ssimonb dec_disklabel *dlp; 1251.5Sjonathan struct buf *bp = NULL; 1261.42Sdrochner const char *msg = NULL; 1271.5Sjonathan 1281.5Sjonathan bp = geteblk((int)lp->d_secsize); 1291.5Sjonathan bp->b_dev = dev; 1301.5Sjonathan bp->b_blkno = DEC_LABEL_SECTOR; 1311.5Sjonathan bp->b_bcount = lp->d_secsize; 1321.34Schs bp->b_flags |= B_READ; 1331.26Sthorpej bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl; 1341.5Sjonathan (*strat)(bp); 1351.5Sjonathan 1361.5Sjonathan if (biowait(bp)) { 1371.4Sdean msg = "I/O error"; 1381.5Sjonathan goto done; 1391.5Sjonathan } 1401.5Sjonathan 1411.32Sthorpej for (dlp = (dec_disklabel *)bp->b_data; 1421.45Ssimonb dlp <= (dec_disklabel *) 1431.45Ssimonb ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp)); 1441.21Ssimonb dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) { 1451.5Sjonathan 1461.5Sjonathan int part; 1471.5Sjonathan 1481.5Sjonathan if (dlp->magic != DEC_LABEL_MAGIC) { 1491.56Smartin#if 0 1501.13Schristos printf("label: %x\n",dlp->magic); 1511.56Smartin#endif 1521.5Sjonathan msg = ((msg != NULL) ? msg: "no disk label"); 1531.5Sjonathan goto done; 1541.5Sjonathan } 1551.5Sjonathan 1561.6Sjonathan#ifdef DIAGNOSTIC 1571.13Schristos/*XXX*/ printf("Interpreting Ultrix label\n"); 1581.6Sjonathan#endif 1591.5Sjonathan 1601.5Sjonathan lp->d_magic = DEC_LABEL_MAGIC; 1611.5Sjonathan lp->d_npartitions = 0; 1621.28Smhitch strncpy(lp->d_packname, "Ultrix label", 16); 1631.28Smhitch lp->d_rpm = 3600; 1641.28Smhitch lp->d_interleave = 1; 1651.28Smhitch lp->d_flags = 0; 1661.28Smhitch lp->d_bbsize = BBSIZE; 1671.37Sdrochner lp->d_sbsize = SBLOCKSIZE; 1681.5Sjonathan for (part = 0; 1691.5Sjonathan part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ? 1701.5Sjonathan MAXPARTITIONS : DEC_NUM_DISK_PARTS); 1711.5Sjonathan part++) { 1721.53Stsutsui lp->d_partitions[part].p_size = 1731.53Stsutsui dlp->map[part].num_blocks; 1741.53Stsutsui lp->d_partitions[part].p_offset = 1751.53Stsutsui dlp->map[part].start_block; 1761.5Sjonathan lp->d_partitions[part].p_fsize = 1024; 1771.5Sjonathan lp->d_partitions[part].p_fstype = 1781.5Sjonathan (part==1) ? FS_SWAP : FS_BSDFFS; 1791.5Sjonathan lp->d_npartitions += 1; 1801.5Sjonathan 1811.6Sjonathan#ifdef DIAGNOSTIC 1821.13Schristos printf(" Ultrix label rz%d%c: start %d len %d\n", 1831.5Sjonathan DISKUNIT(dev), "abcdefgh"[part], 1841.5Sjonathan lp->d_partitions[part].p_offset, 1851.5Sjonathan lp->d_partitions[part].p_size); 1861.19Ssimonb#endif 1871.5Sjonathan } 1881.5Sjonathan break; 1891.4Sdean } 1901.5Sjonathan 1911.5Sjonathandone: 1921.46Sad brelse(bp, 0); 1931.53Stsutsui return msg; 1941.1Sderaadt} 1951.1Sderaadt 1961.1Sderaadt/* 1971.1Sderaadt * Write disk label back to device after modification. 1981.1Sderaadt */ 1991.9Sjonathanint 2001.53Stsutsuiwritedisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp, 2011.53Stsutsui struct cpu_disklabel *osdep) 2021.1Sderaadt{ 2031.1Sderaadt struct buf *bp; 2041.1Sderaadt struct disklabel *dlp; 2051.1Sderaadt int labelpart; 2061.1Sderaadt int error = 0; 2071.1Sderaadt 2081.30Stsutsui labelpart = DISKPART(dev); 2091.1Sderaadt if (lp->d_partitions[labelpart].p_offset != 0) { 2101.1Sderaadt if (lp->d_partitions[0].p_offset != 0) 2111.53Stsutsui return EXDEV; /* not quite right */ 2121.1Sderaadt labelpart = 0; 2131.1Sderaadt } 2141.1Sderaadt bp = geteblk((int)lp->d_secsize); 2151.30Stsutsui bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart)); 2161.1Sderaadt bp->b_blkno = LABELSECTOR; 2171.1Sderaadt bp->b_bcount = lp->d_secsize; 2181.34Schs bp->b_flags |= B_READ; 2191.1Sderaadt (*strat)(bp); 2201.9Sjonathan if ((error = biowait(bp)) != 0) 2211.1Sderaadt goto done; 2221.32Sthorpej for (dlp = (struct disklabel *)bp->b_data; 2231.1Sderaadt dlp <= (struct disklabel *) 2241.45Ssimonb ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); 2251.1Sderaadt dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 2261.1Sderaadt if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 2271.1Sderaadt dkcksum(dlp) == 0) { 2281.1Sderaadt *dlp = *lp; 2291.48Sad bp->b_oflags &= ~(BO_DONE); 2301.48Sad bp->b_flags &= ~(B_READ); 2311.34Schs bp->b_flags |= B_WRITE; 2321.1Sderaadt (*strat)(bp); 2331.1Sderaadt error = biowait(bp); 2341.1Sderaadt goto done; 2351.1Sderaadt } 2361.1Sderaadt } 2371.1Sderaadt error = ESRCH; 2381.1Sderaadtdone: 2391.46Sad brelse(bp, 0); 2401.53Stsutsui return error; 2411.6Sjonathan} 242