1 /* $NetBSD: disksubr.c,v 1.30 2026/07/15 06:55:57 martin Exp $ */ 2 3 /* 4 * Copyright (c) 1998 Christopher G. Demetriou. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Christopher G. Demetriou 17 * for the NetBSD Project. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 62 */ 63 64 /* 65 * Copyright (c) 1995 Mark Brinicombe 66 * All rights reserved. 67 * 68 * Redistribution and use in source and binary forms, with or without 69 * modification, are permitted provided that the following conditions 70 * are met: 71 * 1. Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 2. Redistributions in binary form must reproduce the above copyright 74 * notice, this list of conditions and the following disclaimer in the 75 * documentation and/or other materials provided with the distribution. 76 * 3. All advertising materials mentioning features or use of this software 77 * must display the following acknowledgement: 78 * This product includes software developed by the University of 79 * California, Berkeley and its contributors. 80 * 4. Neither the name of the University nor the names of its contributors 81 * may be used to endorse or promote products derived from this software 82 * without specific prior written permission. 83 * 84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 94 * SUCH DAMAGE. 95 * 96 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 97 */ 98 99 #include <sys/cdefs.h> 100 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.30 2026/07/15 06:55:57 martin Exp $"); 101 102 #include <sys/param.h> 103 #include <sys/systm.h> 104 #include <sys/buf.h> 105 #include <sys/disklabel.h> 106 #include <sys/syslog.h> 107 #include <sys/device.h> 108 #include <sys/disk.h> 109 110 /* 111 * Attempt to read a disk label from a device 112 * using the indicated strategy routine. 113 * The label must be partly set up before this: 114 * secpercyl, secsize and anything required for a block i/o read 115 * operation in the driver's strategy/start routines 116 * must be filled in before calling us. 117 * 118 * If dos partition table requested, attempt to load it and 119 * find disklabel inside a DOS partition. Also, if bad block 120 * table needed, attempt to extract it as well. Return buffer 121 * for use in signalling errors if requested. 122 * 123 * Returns null on success and an error string on failure. 124 */ 125 126 const char * 127 readdisklabel(dev_t dev, void (*strat)(struct buf *), 128 struct disklabel *lp, struct cpu_disklabel *osdep) 129 { 130 struct buf *bp; 131 struct disklabel *dlp; 132 const char *msg = NULL; 133 int cyl, netbsdpartoff; 134 135 /* minimal requirements for archetypal disk label */ 136 137 if (lp->d_secsize == 0) 138 lp->d_secsize = DEV_BSIZE; 139 140 if (lp->d_secperunit == 0) 141 lp->d_secperunit = 0x1fffffff; 142 143 if (lp->d_npartitions < RAW_PART + 1) 144 lp->d_npartitions = RAW_PART + 1; 145 146 if (lp->d_partitions[RAW_PART].p_size == 0) { 147 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 148 lp->d_partitions[RAW_PART].p_offset = 0; 149 lp->d_partitions[RAW_PART].p_size = 0x1fffffff; 150 } 151 /* 152 * Set partition 'a' to be the whole disk. 153 * Cleared if we find a netbsd label. 154 */ 155 lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size; 156 lp->d_partitions[0].p_fstype = FS_BSDFFS; 157 158 /* obtain buffer to probe drive with */ 159 160 bp = geteblk((int)lp->d_secsize); 161 162 /* request no partition relocation by driver on I/O operations */ 163 164 bp->b_dev = dev; 165 166 /* do netbsd partitions in the process of getting disklabel? */ 167 168 netbsdpartoff = 0; 169 cyl = LABELSECTOR / lp->d_secpercyl; 170 171 if (osdep) { 172 if (filecore_label_read(dev, strat,lp, osdep, &msg, &cyl, 173 &netbsdpartoff) || 174 mbr_label_read(dev, strat, lp, osdep, &msg, &cyl, 175 &netbsdpartoff)) { 176 if (msg != NULL) 177 goto done; 178 } else { 179 /* 180 * We didn't find anything we like; NetBSD native. 181 * netbsdpartoff and cyl should be unchanged. 182 */ 183 KASSERT(netbsdpartoff == 0); 184 KASSERT(cyl == (LABELSECTOR / lp->d_secpercyl)); 185 } 186 } 187 188 /* next, dig out disk label */ 189 190 bp->b_blkno = netbsdpartoff + LABELSECTOR; 191 bp->b_cylinder = bp->b_blkno / lp->d_secpercyl; 192 bp->b_bcount = lp->d_secsize; 193 bp->b_flags |= B_READ; 194 (*strat)(bp); 195 196 /* if successful, locate disk label within block and validate */ 197 198 if (biowait(bp)) { 199 msg = "disk label I/O error"; 200 goto done; 201 } 202 for (dlp = (struct disklabel *)bp->b_data; 203 dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize 204 - sizeof(*dlp)); 205 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 206 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 207 continue; 208 } else if (dlp->d_npartitions > MAXPARTITIONS || 209 dkcksum(dlp) != 0) 210 msg = "disk label corrupted"; 211 else { 212 *lp = *dlp; 213 msg = NULL; 214 break; 215 } 216 } 217 218 done: 219 brelse(bp, 0); 220 return (msg); 221 } 222 223 224 /* 225 * Write disk label back to device after modification. 226 */ 227 228 int 229 writedisklabel(dev_t dev, void (*strat)(struct buf *), 230 struct disklabel *lp, struct cpu_disklabel *osdep) 231 { 232 struct buf *bp; 233 struct disklabel *dlp; 234 int cyl, netbsdpartoff; 235 int error = 0, rv; 236 237 /* get a buffer and initialize it */ 238 239 bp = geteblk((int)lp->d_secsize); 240 bp->b_dev = dev; 241 242 /* do netbsd partitions in the process of getting disklabel? */ 243 244 netbsdpartoff = 0; 245 cyl = LABELSECTOR / lp->d_secpercyl; 246 247 if (osdep) { 248 if ((rv = filecore_label_locate(dev, strat,lp, osdep, &cyl, 249 &netbsdpartoff)) != 0 || 250 (rv = mbr_label_locate(dev, strat, lp, osdep, &cyl, 251 &netbsdpartoff)) != 0) { 252 if (rv > 0) 253 goto done; 254 } else { 255 /* 256 * We didn't find anything we like; NetBSD native. 257 * netbsdpartoff and cyl should be unchanged. 258 */ 259 KASSERT(netbsdpartoff == 0); 260 KASSERT(cyl == (LABELSECTOR / lp->d_secpercyl)); 261 } 262 } 263 264 /* writelabel: */ 265 266 #ifdef DEBUG_LABEL 267 printf("%s: Reading disklabel addr=%08x\n", __func__, 268 netbsdpartoff * DEV_BSIZE); 269 #endif 270 271 /* next, dig out disk label */ 272 273 bp->b_blkno = netbsdpartoff + LABELSECTOR; 274 bp->b_cylinder = cyl; 275 bp->b_bcount = lp->d_secsize; 276 bp->b_oflags &= ~(BO_DONE); 277 bp->b_flags |= B_READ; 278 (*strat)(bp); 279 280 /* if successful, locate disk label within block and validate */ 281 282 if ((error = biowait(bp))) 283 goto done; 284 for (dlp = (struct disklabel *)bp->b_data; 285 dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize 286 - sizeof(*dlp)); 287 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 288 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 289 dkcksum(dlp) == 0) { 290 *dlp = *lp; 291 bp->b_flags &= ~(B_READ); 292 bp->b_oflags &= ~(BO_DONE); 293 bp->b_flags |= B_WRITE; 294 (*strat)(bp); 295 error = biowait(bp); 296 goto done; 297 } 298 } 299 300 error = ESRCH; 301 302 done: 303 brelse(bp, 0); 304 return (error); 305 } 306 307 /* End of disksubr.c */ 308