1 1.59 tsutsui /* $NetBSD: main.c,v 1.59 2025/01/19 04:37:15 tsutsui Exp $ */ 2 1.13 jmmv 3 1.13 jmmv /* 4 1.13 jmmv * Copyright (c) 2006 The NetBSD Foundation, Inc. 5 1.13 jmmv * All rights reserved. 6 1.13 jmmv * 7 1.13 jmmv * This code is derived from software contributed to The NetBSD Foundation 8 1.13 jmmv * by Julio M. Merino Vidal. 9 1.13 jmmv * 10 1.13 jmmv * Redistribution and use in source and binary forms, with or without 11 1.13 jmmv * modification, are permitted provided that the following conditions 12 1.13 jmmv * are met: 13 1.13 jmmv * 1. Redistributions of source code must retain the above copyright 14 1.13 jmmv * notice, this list of conditions and the following disclaimer. 15 1.13 jmmv * 2. Redistributions in binary form must reproduce the above copyright 16 1.13 jmmv * notice, this list of conditions and the following disclaimer in the 17 1.13 jmmv * documentation and/or other materials provided with the distribution. 18 1.13 jmmv * 19 1.13 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.13 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.13 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.13 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.13 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.13 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.13 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.13 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.13 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.13 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.13 jmmv * POSSIBILITY OF SUCH DAMAGE. 30 1.13 jmmv */ 31 1.1 dyoung 32 1.1 dyoung /* 33 1.1 dyoung * Copyright (c) 1987, 1993 34 1.1 dyoung * The Regents of the University of California. All rights reserved. 35 1.1 dyoung * 36 1.1 dyoung * This code is derived from software contributed to Berkeley by 37 1.1 dyoung * Symmetric Computer Systems. 38 1.1 dyoung * 39 1.1 dyoung * Redistribution and use in source and binary forms, with or without 40 1.1 dyoung * modification, are permitted provided that the following conditions 41 1.1 dyoung * are met: 42 1.1 dyoung * 1. Redistributions of source code must retain the above copyright 43 1.1 dyoung * notice, this list of conditions and the following disclaimer. 44 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright 45 1.1 dyoung * notice, this list of conditions and the following disclaimer in the 46 1.1 dyoung * documentation and/or other materials provided with the distribution. 47 1.1 dyoung * 3. Neither the name of the University nor the names of its contributors 48 1.1 dyoung * may be used to endorse or promote products derived from this software 49 1.1 dyoung * without specific prior written permission. 50 1.1 dyoung * 51 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 1.1 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 1.1 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 1.1 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 1.1 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 1.1 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 1.1 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 1.1 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 1.1 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 1.1 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 1.1 dyoung * SUCH DAMAGE. 62 1.1 dyoung */ 63 1.1 dyoung 64 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H 65 1.1 dyoung #include "nbtool_config.h" 66 1.1 dyoung #endif 67 1.1 dyoung 68 1.1 dyoung #include <sys/cdefs.h> 69 1.1 dyoung #ifndef lint 70 1.19 lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993\ 71 1.19 lukem The Regents of the University of California. All rights reserved."); 72 1.1 dyoung #endif /* not lint */ 73 1.1 dyoung 74 1.1 dyoung #ifndef lint 75 1.1 dyoung #if 0 76 1.1 dyoung static char sccsid[] = "@(#)disklabel.c 8.4 (Berkeley) 5/4/95"; 77 1.1 dyoung /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 78 1.1 dyoung #else 79 1.59 tsutsui __RCSID("$NetBSD: main.c,v 1.59 2025/01/19 04:37:15 tsutsui Exp $"); 80 1.1 dyoung #endif 81 1.1 dyoung #endif /* not lint */ 82 1.1 dyoung 83 1.1 dyoung #include <sys/param.h> 84 1.1 dyoung #include <sys/file.h> 85 1.1 dyoung #include <sys/stat.h> 86 1.1 dyoung #include <sys/wait.h> 87 1.1 dyoung #define DKTYPENAMES 88 1.1 dyoung #define FSTYPENAMES 89 1.1 dyoung 90 1.1 dyoung #include <ctype.h> 91 1.1 dyoung #include <err.h> 92 1.1 dyoung #include <errno.h> 93 1.1 dyoung #include <signal.h> 94 1.1 dyoung #include <string.h> 95 1.1 dyoung #include <stdio.h> 96 1.1 dyoung #include <stdlib.h> 97 1.17 dyoung #include <limits.h> 98 1.1 dyoung #include <unistd.h> 99 1.1 dyoung 100 1.1 dyoung #include <ufs/ufs/dinode.h> 101 1.1 dyoung #include <ufs/ffs/fs.h> 102 1.1 dyoung 103 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H 104 1.1 dyoung #include <nbinclude/sys/disklabel.h> 105 1.6 dsl #include <nbinclude/sys/disklabel_acorn.h> 106 1.1 dyoung #include <nbinclude/sys/bootblock.h> 107 1.1 dyoung #include "../../include/disktab.h" 108 1.1 dyoung #else 109 1.1 dyoung #include <sys/ioctl.h> 110 1.1 dyoung #include <sys/disklabel.h> 111 1.6 dsl #include <sys/disklabel_acorn.h> 112 1.1 dyoung #include <sys/bootblock.h> 113 1.1 dyoung #include <util.h> 114 1.1 dyoung #include <disktab.h> 115 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */ 116 1.1 dyoung 117 1.1 dyoung #include "pathnames.h" 118 1.1 dyoung #include "extern.h" 119 1.1 dyoung #include "dkcksum.h" 120 1.22 tsutsui #include "bswap.h" 121 1.1 dyoung 122 1.1 dyoung /* 123 1.1 dyoung * Disklabel: read and write disklabels. 124 1.1 dyoung * The label is usually placed on one of the first sectors of the disk. 125 1.1 dyoung * Many machines also place a bootstrap in the same area, 126 1.1 dyoung * in which case the label is embedded in the bootstrap. 127 1.1 dyoung * The bootstrap source must leave space at the proper offset 128 1.1 dyoung * for the label on such machines. 129 1.1 dyoung */ 130 1.1 dyoung 131 1.1 dyoung #ifndef BBSIZE 132 1.1 dyoung #define BBSIZE 8192 /* size of boot area, with label */ 133 1.1 dyoung #endif 134 1.1 dyoung 135 1.6 dsl #define DISKMAGIC_REV bswap32(DISKMAGIC) 136 1.6 dsl /* To delete a label, we just invert the magic numbers */ 137 1.6 dsl #define DISKMAGIC_DELETED (~DISKMAGIC) 138 1.6 dsl #define DISKMAGIC_DELETED_REV bswap32(~DISKMAGIC) 139 1.6 dsl 140 1.1 dyoung #define DEFEDITOR _PATH_VI 141 1.1 dyoung 142 1.6 dsl char specname[MAXPATHLEN]; 143 1.1 dyoung 144 1.6 dsl /* Some global data, all too hard to pass about */ 145 1.15 drochner char bootarea[BBSIZE]; /* Buffer matching part of disk */ 146 1.6 dsl int bootarea_len; /* Number of bytes we actually read */ 147 1.6 dsl static struct disklabel lab; /* The label we have updated */ 148 1.6 dsl 149 1.6 dsl static int Aflag; /* Action all labels */ 150 1.6 dsl static int Fflag; /* Read/write from file */ 151 1.6 dsl static int rflag; /* Read/write direct from disk */ 152 1.6 dsl static int tflag; /* Format output as disktab */ 153 1.6 dsl int Cflag; /* CHS format output */ 154 1.6 dsl static int Dflag; /* Delete old labels (use with write) */ 155 1.6 dsl static int Iflag; /* Read/write direct, but default if absent */ 156 1.13 jmmv static int lflag; /* List all known file system types and exit */ 157 1.6 dsl static int verbose; 158 1.6 dsl static int read_all; /* set if op = READ && Aflag */ 159 1.6 dsl 160 1.6 dsl static int write_label(int); 161 1.6 dsl static int readlabel_direct(int); 162 1.6 dsl static void writelabel_direct(int); 163 1.6 dsl static int update_label(int, u_int, u_int); 164 1.6 dsl static struct disklabel *find_label(int, u_int); 165 1.46 christos #if !defined(NATIVELABEL_ONLY) 166 1.29 matt static void getmachineparams(const char *); 167 1.46 christos #endif 168 1.1 dyoung 169 1.1 dyoung static void makedisktab(FILE *, struct disklabel *); 170 1.6 dsl static void makelabel(const char *, const char *); 171 1.1 dyoung static void l_perror(const char *); 172 1.6 dsl static void readlabel(int); 173 1.6 dsl static int edit(int); 174 1.6 dsl static int editit(const char *); 175 1.1 dyoung static char *skip(char *); 176 1.1 dyoung static char *word(char *); 177 1.1 dyoung static int getasciilabel(FILE *, struct disklabel *); 178 1.25 joerg __dead static void usage(void); 179 1.13 jmmv static int qsort_strcmp(const void *, const void *); 180 1.1 dyoung static int getulong(const char *, char, char **, 181 1.1 dyoung unsigned long *, unsigned long); 182 1.1 dyoung #define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX) 183 1.1 dyoung #define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX) 184 1.1 dyoung #define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX) 185 1.1 dyoung 186 1.6 dsl static int set_writable_fd = -1; 187 1.6 dsl 188 1.46 christos #if !defined(NATIVELABEL_ONLY) 189 1.29 matt static u_int labeloffset; 190 1.29 matt static u_int labelsector; 191 1.29 matt static int labelusesmbr; 192 1.29 matt u_int maxpartitions; 193 1.29 matt static int byteorder; 194 1.29 matt 195 1.29 matt static int biendian_p; 196 1.29 matt #ifndef HAVE_NBTOOL_CONFIG_H 197 1.29 matt static int native_p = 1; 198 1.29 matt #endif 199 1.29 matt int bswap_p; 200 1.29 matt 201 1.29 matt static const struct disklabel_params { 202 1.29 matt const char *machine; 203 1.45 christos u_char labelusesmbr : 1; 204 1.45 christos u_char labelsector : 7; 205 1.44 christos u_char maxpartitions; 206 1.44 christos u_char raw_part; 207 1.44 christos u_char oldmaxpartitions; 208 1.44 christos u_short labeloffset; 209 1.44 christos u_short byteorder; 210 1.29 matt } disklabel_params[] = { 211 1.44 christos { "mvme68k", 0, 0, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 212 1.44 christos { "next68k", 0, 0, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 213 1.29 matt 214 1.44 christos { "algor", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* mips */ 215 1.44 christos { "alpha", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* alpha */ 216 1.44 christos { "luna68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 217 1.44 christos { "mac68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 218 1.44 christos { "news68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 219 1.44 christos { "newsmips", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* mips */ 220 1.44 christos { "pmax", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* mips */ 221 1.44 christos { "sun2", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 222 1.44 christos { "sun68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68010 */ 223 1.59 tsutsui { "virt68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 224 1.53 isaki { "x68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 225 1.44 christos 226 1.44 christos { "vax", 0, 0, 12, 2, 8, 64, LITTLE_ENDIAN }, /* vax */ 227 1.44 christos 228 1.44 christos { "amiga", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 229 1.44 christos { "amigappc", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */ 230 1.44 christos { "evbmips", 0, 0, 16, 2, 0, 64, 0 }, /* mips */ 231 1.44 christos { "evbppc", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */ 232 1.44 christos 233 1.44 christos { "sparc", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */ 234 1.44 christos { "sparc64", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */ 235 1.44 christos { "sun3", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */ 236 1.44 christos 237 1.44 christos { "atari", 0, 0, 16, 2, 0, 516, BIG_ENDIAN }, /* m68k */ 238 1.44 christos 239 1.44 christos { "mipsco", 0, 1, 8, 2, 0, 0, BIG_ENDIAN }, /* mips */ 240 1.44 christos { "mvmeppc", 0, 1, 8, 3, 0, 0, BIG_ENDIAN }, /* powerpc */ 241 1.44 christos 242 1.44 christos { "bebox", 0, 1, 8, 3, 0, 0, BIG_ENDIAN }, /* powerpc */ 243 1.44 christos 244 1.44 christos { "emips", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* mips */ 245 1.44 christos { "hppa", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* hppa */ 246 1.44 christos { "ibmnws", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 247 1.44 christos { "ofppc", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 248 1.44 christos { "rs6000", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 249 1.44 christos { "sandpoint", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 250 1.44 christos { "sgimips", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* mips */ 251 1.44 christos 252 1.44 christos { "sbmips", 0, 1, 16, 3, 0, 0, 0 }, /* mips */ 253 1.44 christos 254 1.44 christos { "cesfic", 0, 2, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 255 1.44 christos { "hp300", 0, 2, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 256 1.44 christos 257 1.44 christos { "ews4800mips",0, 9, 16, 15, 0, 0, BIG_ENDIAN }, /* mips */ 258 1.44 christos 259 1.44 christos { "macppc", 1, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */ 260 1.44 christos { "pmon", 1, 0, 16, 2, 0, 64, 0 }, /* evbmips */ 261 1.44 christos 262 1.45 christos { "prep", 1, 1, 8, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 263 1.44 christos 264 1.45 christos { "dreamcast", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* sh3 */ 265 1.45 christos { "evbcf", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* coldfire */ 266 1.45 christos { "evbppc-mbr", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 267 1.45 christos { "evbsh3", 1, 1, 16, 2, 0, 0, 0 }, /* sh3 */ 268 1.45 christos { "hpcsh", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* sh3 */ 269 1.45 christos { "mmeye", 1, 1, 16, 2, 0, 0, 0 }, /* sh3 */ 270 1.45 christos { "or1k", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* or1k */ 271 1.45 christos { "riscv", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* riscv */ 272 1.45 christos 273 1.45 christos { "acorn32", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 274 1.45 christos { "cats", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 275 1.45 christos { "evbarm", 1, 1, 16, 2, 8, 0, 0 }, /* arm */ 276 1.45 christos { "iyonix", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 277 1.45 christos { "netwinder", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 278 1.45 christos { "shark", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 279 1.45 christos 280 1.45 christos { "amd64", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* x86 */ 281 1.45 christos { "arc", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* mips */ 282 1.45 christos { "cobalt", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* mips */ 283 1.45 christos { "landisk", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* sh3 */ 284 1.45 christos 285 1.45 christos { "epoc32", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */ 286 1.45 christos { "hpcarm", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */ 287 1.45 christos { "hpcmips", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* mips */ 288 1.45 christos { "i386", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* x86 */ 289 1.45 christos { "ia64", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* x86 */ 290 1.45 christos { "zaurus", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */ 291 1.29 matt 292 1.44 christos { NULL, 0, 0, 0, 0, 0, 0, 0 }, /* must be last */ 293 1.29 matt }; 294 1.29 matt 295 1.29 matt #ifndef HAVE_NBTOOL_CONFIG_H 296 1.29 matt static struct disklabel_params native_params; 297 1.1 dyoung #endif 298 1.1 dyoung 299 1.29 matt static const struct arch_endian { 300 1.29 matt int byteorder; 301 1.29 matt const char *arch; 302 1.29 matt } arch_endians[] = { 303 1.40 matt { LITTLE_ENDIAN, "aarch64" }, 304 1.29 matt { LITTLE_ENDIAN, "alpha" }, 305 1.29 matt { LITTLE_ENDIAN, "arm" }, 306 1.29 matt { LITTLE_ENDIAN, "earm" }, 307 1.29 matt { LITTLE_ENDIAN, "earmhf" }, 308 1.36 riz { LITTLE_ENDIAN, "earmv4" }, 309 1.36 riz { LITTLE_ENDIAN, "earmv5" }, 310 1.36 riz { LITTLE_ENDIAN, "earmv6" }, 311 1.36 riz { LITTLE_ENDIAN, "earmv6hf" }, 312 1.36 riz { LITTLE_ENDIAN, "earmv7" }, 313 1.36 riz { LITTLE_ENDIAN, "earmv7hf" }, 314 1.29 matt { LITTLE_ENDIAN, "i386" }, 315 1.29 matt { LITTLE_ENDIAN, "ia64" }, 316 1.29 matt { LITTLE_ENDIAN, "mipsel" }, 317 1.29 matt { LITTLE_ENDIAN, "mips64el" }, 318 1.42 matt { LITTLE_ENDIAN, "riscv32" }, 319 1.42 matt { LITTLE_ENDIAN, "riscv64" }, 320 1.29 matt { LITTLE_ENDIAN, "sh3el" }, 321 1.29 matt { LITTLE_ENDIAN, "vax" }, 322 1.32 skrll { LITTLE_ENDIAN, "x86_64" }, 323 1.29 matt 324 1.40 matt { BIG_ENDIAN, "aarch64eb" }, 325 1.29 matt { BIG_ENDIAN, "armeb" }, 326 1.37 matt { BIG_ENDIAN, "coldfire" }, 327 1.29 matt { BIG_ENDIAN, "earmeb" }, 328 1.29 matt { BIG_ENDIAN, "earmhfeb" }, 329 1.36 riz { BIG_ENDIAN, "earmv4eb" }, 330 1.36 riz { BIG_ENDIAN, "earmv5eb" }, 331 1.36 riz { BIG_ENDIAN, "earmv6eb" }, 332 1.36 riz { BIG_ENDIAN, "earmv6hfeb" }, 333 1.36 riz { BIG_ENDIAN, "earmv7eb" }, 334 1.36 riz { BIG_ENDIAN, "earmv7hfeb" }, 335 1.29 matt { BIG_ENDIAN, "hppa" }, 336 1.37 matt { BIG_ENDIAN, "m68000" }, 337 1.29 matt { BIG_ENDIAN, "m68k" }, 338 1.29 matt { BIG_ENDIAN, "mipseb" }, 339 1.29 matt { BIG_ENDIAN, "mips64eb" }, 340 1.42 matt { BIG_ENDIAN, "or1k" }, 341 1.29 matt { BIG_ENDIAN, "powerpc" }, 342 1.29 matt { BIG_ENDIAN, "sh3eb" }, 343 1.29 matt { BIG_ENDIAN, "sparc" }, 344 1.29 matt { BIG_ENDIAN, "sparc64" }, 345 1.29 matt 346 1.29 matt { 0, NULL }, 347 1.29 matt }; 348 1.29 matt 349 1.6 dsl /* Default location for label - only used if we don't find one to update */ 350 1.17 dyoung #define LABEL_OFFSET (dklabel_getlabelsector() * DEV_BSIZE + dklabel_getlabeloffset()) 351 1.46 christos #else 352 1.46 christos #define labeloffset LABELOFFSET 353 1.46 christos #define labelsector LABELSECTOR 354 1.46 christos #define labelusesmbr LABELUSESMBR 355 1.46 christos #define maxpartitions MAXPARTITIONS 356 1.58 tsutsui #define LABEL_OFFSET (LABELSECTOR * DEV_BSIZE + LABELOFFSET) 357 1.46 christos #endif /* !NATIVELABEL_ONLY */ 358 1.6 dsl 359 1.6 dsl /* 360 1.6 dsl * For portability it doesn't make sense to use any other value.... 361 1.6 dsl * Except, maybe, the size of a physical sector. 362 1.6 dsl * This value is used if we have to write a label to the start of an mbr ptn. 363 1.6 dsl */ 364 1.6 dsl #ifndef LABELOFFSET_MBR 365 1.6 dsl #define LABELOFFSET_MBR 512 366 1.6 dsl #endif 367 1.6 dsl 368 1.6 dsl #if HAVE_NBTOOL_CONFIG_H 369 1.6 dsl static int 370 1.6 dsl opendisk(const char *path, int flags, char *buf, int buflen, int cooked) 371 1.6 dsl { 372 1.6 dsl int f; 373 1.6 dsl f = open(path, flags, 0); 374 1.6 dsl strlcpy(buf, path, buflen); 375 1.6 dsl return f; 376 1.6 dsl } 377 1.6 dsl #endif /* HAVE_NBTOOL_CONFIG_H */ 378 1.6 dsl 379 1.46 christos #if !defined(NATIVELABEL_ONLY) 380 1.29 matt static void 381 1.29 matt setbyteorder(int new_byteorder) 382 1.29 matt { 383 1.29 matt static int set_p; 384 1.29 matt 385 1.29 matt if ((!biendian_p || set_p) 386 1.29 matt && byteorder != 0 387 1.29 matt && byteorder != new_byteorder) { 388 1.47 chs warnx("changing %s byteorder to %s", 389 1.29 matt byteorder == LITTLE_ENDIAN ? "le" : "be", 390 1.29 matt new_byteorder == LITTLE_ENDIAN ? "le" : "be"); 391 1.29 matt } 392 1.29 matt byteorder = new_byteorder; 393 1.29 matt biendian_p = 0; 394 1.29 matt set_p = 1; 395 1.29 matt } 396 1.29 matt 397 1.29 matt static void 398 1.29 matt getmachineparams(const char *mach) 399 1.29 matt { 400 1.29 matt const struct disklabel_params *dp = disklabel_params; 401 1.29 matt for (; dp->machine != NULL; dp++) { 402 1.29 matt if (!strcmp(mach, dp->machine)) { 403 1.29 matt labelusesmbr = dp->labelusesmbr; 404 1.29 matt labelsector = dp->labelsector; 405 1.29 matt labeloffset = dp->labeloffset; 406 1.29 matt maxpartitions = dp->maxpartitions; 407 1.29 matt biendian_p = (dp->byteorder == 0); 408 1.29 matt if (!biendian_p) 409 1.29 matt setbyteorder(dp->byteorder); 410 1.29 matt return; 411 1.29 matt } 412 1.29 matt } 413 1.29 matt errx(1, "%s: unknown machine type", mach); 414 1.29 matt } 415 1.29 matt 416 1.29 matt static void 417 1.29 matt getarchbyteorder(const char *arch) 418 1.29 matt { 419 1.29 matt const struct arch_endian *p = arch_endians; 420 1.29 matt for (; p->arch != NULL; p++) { 421 1.29 matt if (!strcmp(arch, p->arch)) { 422 1.29 matt setbyteorder(p->byteorder); 423 1.29 matt return; 424 1.29 matt } 425 1.29 matt } 426 1.29 matt errx(1, "%s: unknown arch", arch); 427 1.29 matt } 428 1.29 matt 429 1.17 dyoung static daddr_t 430 1.17 dyoung dklabel_getlabelsector(void) 431 1.17 dyoung { 432 1.17 dyoung unsigned long int nval; 433 1.17 dyoung char *end; 434 1.17 dyoung const char *val; 435 1.17 dyoung 436 1.17 dyoung if ((val = getenv("DISKLABELSECTOR")) == NULL) 437 1.29 matt return labelsector; 438 1.17 dyoung if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE) 439 1.17 dyoung err(EXIT_FAILURE, "DISKLABELSECTOR in environment"); 440 1.17 dyoung return nval; 441 1.17 dyoung } 442 1.17 dyoung 443 1.17 dyoung static off_t 444 1.17 dyoung dklabel_getlabeloffset(void) 445 1.17 dyoung { 446 1.17 dyoung unsigned long int nval; 447 1.17 dyoung char *end; 448 1.17 dyoung const char *val; 449 1.17 dyoung 450 1.17 dyoung if ((val = getenv("DISKLABELOFFSET")) == NULL) 451 1.29 matt return labeloffset; 452 1.17 dyoung if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE) 453 1.17 dyoung err(EXIT_FAILURE, "DISKLABELOFFSET in environment"); 454 1.17 dyoung return nval; 455 1.17 dyoung } 456 1.46 christos #endif /* !NATIVELABEL_ONLY */ 457 1.17 dyoung 458 1.6 dsl static void 459 1.6 dsl clear_writable(void) 460 1.6 dsl { 461 1.6 dsl static int zero = 0; 462 1.6 dsl dk_ioctl(set_writable_fd, DIOCWLABEL, &zero); 463 1.6 dsl } 464 1.6 dsl 465 1.1 dyoung int 466 1.1 dyoung main(int argc, char *argv[]) 467 1.1 dyoung { 468 1.1 dyoung FILE *t; 469 1.6 dsl int ch, f, error; 470 1.6 dsl char *dkname; 471 1.46 christos #if !defined(NATIVELABEL_ONLY) 472 1.29 matt char *cp; 473 1.46 christos #endif 474 1.6 dsl struct stat sb; 475 1.6 dsl int writable; 476 1.6 dsl enum { 477 1.6 dsl UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, SETREADONLY, 478 1.46 christos WRITE, 479 1.46 christos #if !defined(NO_INTERACT) 480 1.46 christos INTERACT, 481 1.46 christos #endif 482 1.46 christos DELETE 483 1.6 dsl } op = UNSPEC, old_op; 484 1.52 mlelstv #if !defined(NATIVELABEL_ONLY) 485 1.51 mlelstv unsigned long val; 486 1.52 mlelstv #endif 487 1.6 dsl 488 1.29 matt #ifndef HAVE_NBTOOL_CONFIG_H 489 1.46 christos #if !defined(NATIVELABEL_ONLY) 490 1.29 matt labeloffset = native_params.labeloffset = getlabeloffset(); 491 1.29 matt labelsector = native_params.labelsector = getlabelsector(); 492 1.29 matt labelusesmbr = native_params.labelusesmbr = getlabelusesmbr(); 493 1.29 matt maxpartitions = native_params.maxpartitions = getmaxpartitions(); 494 1.31 matt byteorder = native_params.byteorder = BYTE_ORDER; 495 1.29 matt #endif 496 1.46 christos #endif 497 1.29 matt 498 1.46 christos #if !defined(NATIVELABEL_ONLY) 499 1.29 matt if ((cp = getenv("MACHINE")) != NULL) { 500 1.29 matt getmachineparams(cp); 501 1.29 matt } 502 1.29 matt 503 1.29 matt if ((cp = getenv("MACHINE_ARCH")) != NULL) { 504 1.29 matt getarchbyteorder(cp); 505 1.29 matt } 506 1.46 christos #endif 507 1.29 matt 508 1.6 dsl #if HAVE_NBTOOL_CONFIG_H 509 1.6 dsl /* We must avoid doing any ioctl requests */ 510 1.6 dsl Fflag = rflag = 1; 511 1.6 dsl #endif 512 1.1 dyoung 513 1.1 dyoung error = 0; 514 1.52 mlelstv #if !defined(NATIVELABEL_ONLY) 515 1.51 mlelstv while ((ch = getopt(argc, argv, "AB:CDFIL:M:NO:P:RWef:ilmnrtvw")) != -1) { 516 1.52 mlelstv #else 517 1.52 mlelstv while ((ch = getopt(argc, argv, "ACDFINRWef:ilrtvw")) != -1) { 518 1.52 mlelstv #endif 519 1.6 dsl old_op = op; 520 1.1 dyoung switch (ch) { 521 1.6 dsl case 'A': /* Action all labels */ 522 1.6 dsl Aflag = 1; 523 1.6 dsl rflag = 1; 524 1.6 dsl break; 525 1.6 dsl case 'C': /* Display in CHS format */ 526 1.6 dsl Cflag = 1; 527 1.6 dsl break; 528 1.6 dsl case 'D': /* Delete all existing labels */ 529 1.6 dsl Dflag = 1; 530 1.6 dsl rflag = 1; 531 1.6 dsl break; 532 1.6 dsl case 'F': /* Treat 'disk' as a regular file */ 533 1.6 dsl Fflag = 1; 534 1.6 dsl rflag = 1; /* Force direct access */ 535 1.6 dsl break; 536 1.6 dsl case 'I': /* Use default label if none found */ 537 1.6 dsl Iflag = 1; 538 1.6 dsl rflag = 1; /* Implies direct access */ 539 1.1 dyoung break; 540 1.6 dsl case 'R': /* Restore label from text file */ 541 1.1 dyoung op = RESTORE; 542 1.1 dyoung break; 543 1.46 christos #if !defined(NATIVELABEL_ONLY) 544 1.29 matt case 'B': /* byteorder */ 545 1.29 matt if (!strcmp(optarg, "be")) { 546 1.29 matt setbyteorder(BIG_ENDIAN); 547 1.29 matt } else if (!strcmp(optarg, "le")) { 548 1.29 matt setbyteorder(LITTLE_ENDIAN); 549 1.29 matt } else { 550 1.29 matt errx(1, "%s: not be or le", optarg); 551 1.29 matt } 552 1.29 matt break; 553 1.29 matt case 'M': /* machine type */ 554 1.29 matt getmachineparams(optarg); 555 1.29 matt break; 556 1.46 christos #endif 557 1.6 dsl case 'N': /* Disallow writes to label sector */ 558 1.6 dsl op = SETREADONLY; 559 1.1 dyoung break; 560 1.52 mlelstv #if !defined(NATIVELABEL_ONLY) 561 1.51 mlelstv case 'L': /* Label sector */ 562 1.51 mlelstv val = strtoul(optarg, NULL, 10); 563 1.51 mlelstv if ((val == ULONG_MAX && errno == ERANGE) || val > UINT_MAX) 564 1.51 mlelstv err(EXIT_FAILURE, "invalid label sector: %s", optarg); 565 1.51 mlelstv labelsector = val; 566 1.51 mlelstv break; 567 1.51 mlelstv case 'O': /* Label offset */ 568 1.51 mlelstv val = strtoul(optarg, NULL, 10); 569 1.51 mlelstv if ((val == ULONG_MAX && errno == ERANGE) || val > UINT_MAX) 570 1.51 mlelstv err(EXIT_FAILURE, "invalid label offset: %s", optarg); 571 1.51 mlelstv labeloffset = val; 572 1.51 mlelstv break; 573 1.51 mlelstv case 'P': /* Max partitions */ 574 1.51 mlelstv val = strtoul(optarg, NULL, 10); 575 1.51 mlelstv if ((val == ULONG_MAX && errno == ERANGE) || val < 1 || val > UINT_MAX) 576 1.51 mlelstv err(EXIT_FAILURE, "invalid max partitions: %s", optarg); 577 1.51 mlelstv maxpartitions = val; 578 1.51 mlelstv break; 579 1.52 mlelstv #endif 580 1.6 dsl case 'W': /* Allow writes to label sector */ 581 1.1 dyoung op = SETWRITABLE; 582 1.1 dyoung break; 583 1.6 dsl case 'e': /* Edit label with $EDITOR */ 584 1.1 dyoung op = EDIT; 585 1.1 dyoung break; 586 1.6 dsl case 'f': /* Name of disktab file */ 587 1.1 dyoung if (setdisktab(optarg) == -1) 588 1.1 dyoung usage(); 589 1.1 dyoung break; 590 1.46 christos #if !defined(NO_INTERACT) 591 1.6 dsl case 'i': /* Edit using built-in editor */ 592 1.1 dyoung op = INTERACT; 593 1.1 dyoung break; 594 1.46 christos #endif /* !NO_INTERACT */ 595 1.13 jmmv case 'l': /* List all known file system types and exit */ 596 1.13 jmmv lflag = 1; 597 1.13 jmmv break; 598 1.52 mlelstv #if !defined(NATIVELABEL_ONLY) 599 1.6 dsl case 'm': /* Expect disk to have an MBR */ 600 1.51 mlelstv labelusesmbr = 1; 601 1.51 mlelstv break; 602 1.51 mlelstv case 'n': /* Expect disk to not have an MBR */ 603 1.51 mlelstv labelusesmbr = 0; 604 1.6 dsl break; 605 1.52 mlelstv #endif 606 1.6 dsl case 'r': /* Read/write label directly from disk */ 607 1.6 dsl rflag = 1; 608 1.1 dyoung break; 609 1.6 dsl case 't': /* Format output as a disktab entry */ 610 1.6 dsl tflag = 1; 611 1.1 dyoung break; 612 1.6 dsl case 'v': /* verbose/diag output */ 613 1.6 dsl verbose++; 614 1.6 dsl break; 615 1.6 dsl case 'w': /* Write label based on disktab entry */ 616 1.1 dyoung op = WRITE; 617 1.1 dyoung break; 618 1.1 dyoung case '?': 619 1.1 dyoung default: 620 1.1 dyoung usage(); 621 1.6 dsl } 622 1.6 dsl if (old_op != UNSPEC && old_op != op) 623 1.6 dsl usage(); 624 1.1 dyoung } 625 1.29 matt 626 1.51 mlelstv if (maxpartitions > MAXPARTITIONS) { 627 1.51 mlelstv errx(1, "too large maxpartitions > %u\n", MAXPARTITIONS); 628 1.51 mlelstv } 629 1.51 mlelstv 630 1.46 christos #if !defined(NATIVELABEL_ONLY) 631 1.29 matt if (maxpartitions == 0) { 632 1.29 matt errx(1, "unknown label: use -M/-B and $MACHINE/$MACHINE_ARCH"); 633 1.29 matt } 634 1.29 matt if (byteorder != BIG_ENDIAN && byteorder != LITTLE_ENDIAN) { 635 1.29 matt errx(1, "unknown byteorder"); 636 1.29 matt } 637 1.29 matt bswap_p = (byteorder != BYTE_ORDER); 638 1.29 matt #ifdef DEBUG 639 1.30 matt printf("labelusesmbr=%d labelsector=%u labeloffset=%u maxpartitions=%u\n", 640 1.29 matt labelusesmbr, labelsector, labeloffset, maxpartitions); 641 1.29 matt printf("byteorder=%d bswap_p=%d\n", byteorder, bswap_p); 642 1.29 matt #endif 643 1.29 matt #ifndef HAVE_NBTOOL_CONFIG_H 644 1.29 matt /* 645 1.29 matt * If the disklabel has the same location as the native disklabel and 646 1.54 msaitoh * fewer or equal partitions, we can use the native ioctls. Otherwise 647 1.29 matt * force file/raw access. 648 1.29 matt */ 649 1.29 matt native_p = native_params.labelusesmbr == labelusesmbr 650 1.29 matt && native_params.labelsector == labelsector 651 1.29 matt && native_params.labeloffset == labeloffset 652 1.30 matt && maxpartitions <= native_params.maxpartitions 653 1.29 matt && !bswap_p; 654 1.29 matt if (!native_p) 655 1.29 matt Fflag = rflag = 1; 656 1.29 matt #endif 657 1.46 christos #endif /* !NATIVELABEL_ONLY */ 658 1.29 matt 659 1.1 dyoung argc -= optind; 660 1.1 dyoung argv += optind; 661 1.1 dyoung 662 1.13 jmmv if (lflag) 663 1.13 jmmv exit(list_fs_types() ? EXIT_SUCCESS : EXIT_FAILURE); 664 1.13 jmmv 665 1.1 dyoung if (op == UNSPEC) 666 1.6 dsl op = Dflag ? DELETE : READ; 667 1.1 dyoung 668 1.1 dyoung if (argc < 1) 669 1.1 dyoung usage(); 670 1.1 dyoung 671 1.46 christos if (Iflag && op != EDIT 672 1.46 christos #if !defined(NO_INTERACT) 673 1.46 christos && op != INTERACT 674 1.46 christos #endif 675 1.46 christos ) 676 1.1 dyoung usage(); 677 1.1 dyoung 678 1.1 dyoung dkname = argv[0]; 679 1.6 dsl f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, 680 1.6 dsl specname, sizeof specname, 0); 681 1.1 dyoung if (f < 0) 682 1.1 dyoung err(4, "%s", specname); 683 1.1 dyoung 684 1.6 dsl if (!Fflag && fstat(f, &sb) == 0 && S_ISREG(sb.st_mode)) 685 1.6 dsl Fflag = rflag = 1; 686 1.1 dyoung 687 1.1 dyoung switch (op) { 688 1.1 dyoung 689 1.6 dsl case DELETE: /* Remove all existing labels */ 690 1.6 dsl if (argc != 1) 691 1.6 dsl usage(); 692 1.6 dsl Dflag = 2; 693 1.6 dsl writelabel_direct(f); 694 1.6 dsl break; 695 1.6 dsl 696 1.1 dyoung case EDIT: 697 1.1 dyoung if (argc != 1) 698 1.1 dyoung usage(); 699 1.6 dsl readlabel(f); 700 1.6 dsl error = edit(f); 701 1.1 dyoung break; 702 1.1 dyoung 703 1.46 christos #if !defined(NO_INTERACT) 704 1.1 dyoung case INTERACT: 705 1.1 dyoung if (argc != 1) 706 1.1 dyoung usage(); 707 1.6 dsl readlabel(f); 708 1.1 dyoung /* 709 1.1 dyoung * XXX: Fill some default values so checklabel does not fail 710 1.1 dyoung */ 711 1.6 dsl if (lab.d_bbsize == 0) 712 1.6 dsl lab.d_bbsize = BBSIZE; 713 1.6 dsl if (lab.d_sbsize == 0) 714 1.6 dsl lab.d_sbsize = SBLOCKSIZE; 715 1.6 dsl interact(&lab, f); 716 1.1 dyoung break; 717 1.46 christos #endif /* !NO_INTERACT */ 718 1.1 dyoung 719 1.1 dyoung case READ: 720 1.1 dyoung if (argc != 1) 721 1.1 dyoung usage(); 722 1.6 dsl read_all = Aflag; 723 1.6 dsl readlabel(f); 724 1.6 dsl if (read_all) 725 1.6 dsl /* Label got printed in the bowels of readlabel */ 726 1.6 dsl break; 727 1.1 dyoung if (tflag) 728 1.6 dsl makedisktab(stdout, &lab); 729 1.1 dyoung else { 730 1.6 dsl showinfo(stdout, &lab, specname); 731 1.6 dsl showpartitions(stdout, &lab, Cflag); 732 1.1 dyoung } 733 1.6 dsl error = checklabel(&lab); 734 1.1 dyoung if (error) 735 1.1 dyoung error += 100; 736 1.1 dyoung break; 737 1.1 dyoung 738 1.1 dyoung case RESTORE: 739 1.6 dsl if (argc != 2) 740 1.1 dyoung usage(); 741 1.1 dyoung if (!(t = fopen(argv[1], "r"))) 742 1.1 dyoung err(4, "%s", argv[1]); 743 1.6 dsl if (getasciilabel(t, &lab)) 744 1.6 dsl error = write_label(f); 745 1.1 dyoung else 746 1.1 dyoung error = 1; 747 1.1 dyoung break; 748 1.1 dyoung 749 1.6 dsl case SETREADONLY: 750 1.6 dsl writable = 0; 751 1.6 dsl goto do_diocwlabel; 752 1.1 dyoung case SETWRITABLE: 753 1.6 dsl writable = 1; 754 1.6 dsl do_diocwlabel: 755 1.6 dsl if (argc != 1) 756 1.6 dsl usage(); 757 1.6 dsl if (dk_ioctl(f, DIOCWLABEL, &writable) < 0) 758 1.1 dyoung err(4, "ioctl DIOCWLABEL"); 759 1.1 dyoung break; 760 1.1 dyoung 761 1.6 dsl case WRITE: /* Create label from /etc/disktab entry & write */ 762 1.1 dyoung if (argc < 2 || argc > 3) 763 1.1 dyoung usage(); 764 1.6 dsl makelabel(argv[1], argv[2]); 765 1.6 dsl if (checklabel(&lab) == 0) 766 1.6 dsl error = write_label(f); 767 1.1 dyoung else 768 1.1 dyoung error = 1; 769 1.1 dyoung break; 770 1.1 dyoung 771 1.1 dyoung case UNSPEC: 772 1.1 dyoung usage(); 773 1.1 dyoung 774 1.1 dyoung } 775 1.1 dyoung exit(error); 776 1.1 dyoung } 777 1.1 dyoung 778 1.1 dyoung /* 779 1.6 dsl * Construct a prototype disklabel from /etc/disktab. 780 1.1 dyoung */ 781 1.1 dyoung static void 782 1.6 dsl makelabel(const char *type, const char *name) 783 1.1 dyoung { 784 1.1 dyoung struct disklabel *dp; 785 1.1 dyoung 786 1.1 dyoung dp = getdiskbyname(type); 787 1.1 dyoung if (dp == NULL) 788 1.1 dyoung errx(1, "unknown disk type: %s", type); 789 1.6 dsl lab = *dp; 790 1.1 dyoung 791 1.1 dyoung /* d_packname is union d_boot[01], so zero */ 792 1.6 dsl (void)memset(lab.d_packname, 0, sizeof(lab.d_packname)); 793 1.1 dyoung if (name) 794 1.6 dsl (void)strncpy(lab.d_packname, name, sizeof(lab.d_packname)); 795 1.1 dyoung } 796 1.1 dyoung 797 1.6 dsl static int 798 1.6 dsl write_label(int f) 799 1.1 dyoung { 800 1.6 dsl int writable; 801 1.1 dyoung 802 1.6 dsl lab.d_magic = DISKMAGIC; 803 1.6 dsl lab.d_magic2 = DISKMAGIC; 804 1.6 dsl lab.d_checksum = 0; 805 1.6 dsl lab.d_checksum = dkcksum(&lab); 806 1.1 dyoung 807 1.6 dsl if (rflag) { 808 1.6 dsl /* Write the label directly to the disk */ 809 1.1 dyoung 810 1.1 dyoung /* 811 1.1 dyoung * First set the kernel disk label, 812 1.1 dyoung * then write a label to the raw disk. 813 1.1 dyoung * If the SDINFO ioctl fails because it is unimplemented, 814 1.1 dyoung * keep going; otherwise, the kernel consistency checks 815 1.1 dyoung * may prevent us from changing the current (in-core) 816 1.1 dyoung * label. 817 1.1 dyoung */ 818 1.6 dsl if (!Fflag && dk_ioctl(f, DIOCSDINFO, &lab) < 0 && 819 1.1 dyoung errno != ENODEV && errno != ENOTTY) { 820 1.1 dyoung l_perror("ioctl DIOCSDINFO"); 821 1.1 dyoung return (1); 822 1.1 dyoung } 823 1.1 dyoung /* 824 1.1 dyoung * write enable label sector before write (if necessary), 825 1.1 dyoung * disable after writing. 826 1.1 dyoung */ 827 1.1 dyoung writable = 1; 828 1.6 dsl if (!Fflag) { 829 1.6 dsl if (dk_ioctl(f, DIOCWLABEL, &writable) < 0) 830 1.6 dsl perror("ioctl DIOCWLABEL"); 831 1.6 dsl set_writable_fd = f; 832 1.6 dsl atexit(clear_writable); 833 1.1 dyoung } 834 1.1 dyoung 835 1.6 dsl writelabel_direct(f); 836 1.1 dyoung 837 1.1 dyoung /* 838 1.1 dyoung * Now issue a DIOCWDINFO. This will let the kernel convert the 839 1.1 dyoung * disklabel to some machdep format if needed. 840 1.1 dyoung */ 841 1.6 dsl /* XXX: This is stupid! */ 842 1.6 dsl if (!Fflag && dk_ioctl(f, DIOCWDINFO, &lab) < 0) { 843 1.1 dyoung l_perror("ioctl DIOCWDINFO"); 844 1.1 dyoung return (1); 845 1.1 dyoung } 846 1.1 dyoung } else { 847 1.6 dsl /* Get the kernel to write the label */ 848 1.6 dsl if (dk_ioctl(f, DIOCWDINFO, &lab) < 0) { 849 1.1 dyoung l_perror("ioctl DIOCWDINFO"); 850 1.1 dyoung return (1); 851 1.1 dyoung } 852 1.1 dyoung } 853 1.1 dyoung 854 1.21 tsutsui #ifdef VAX_ALTLABELS 855 1.43 christos if (lab.d_type == DKTYPE_SMD && lab.d_flags & D_BADSECT && 856 1.6 dsl lab.d_secsize == 512) { 857 1.6 dsl /* Write the label to the odd sectors of the last track! */ 858 1.1 dyoung daddr_t alt; 859 1.1 dyoung int i; 860 1.6 dsl uint8_t sec0[512]; 861 1.6 dsl 862 1.6 dsl if (pread(f, sec0, 512, 0) < 512) { 863 1.6 dsl warn("read master label to write alternates"); 864 1.6 dsl return 0; 865 1.6 dsl } 866 1.1 dyoung 867 1.6 dsl alt = lab.d_ncylinders * lab.d_secpercyl - lab.d_nsectors; 868 1.20 mhitch for (i = 1; i < 11 && (uint32_t)i < lab.d_nsectors; i += 2) { 869 1.6 dsl if (pwrite(f, sec0, 512, (off_t)(alt + i) * 512) < 512) 870 1.1 dyoung warn("alternate label %d write", i/2); 871 1.1 dyoung } 872 1.1 dyoung } 873 1.21 tsutsui #endif /* VAX_ALTLABELS */ 874 1.1 dyoung 875 1.6 dsl return 0; 876 1.6 dsl } 877 1.6 dsl 878 1.6 dsl int 879 1.6 dsl writelabel(int f, struct disklabel *lp) 880 1.6 dsl { 881 1.6 dsl if (lp != &lab) 882 1.6 dsl lab = *lp; 883 1.6 dsl return write_label(f); 884 1.1 dyoung } 885 1.1 dyoung 886 1.1 dyoung static void 887 1.1 dyoung l_perror(const char *s) 888 1.1 dyoung { 889 1.1 dyoung 890 1.1 dyoung switch (errno) { 891 1.1 dyoung 892 1.1 dyoung case ESRCH: 893 1.1 dyoung warnx("%s: No disk label on disk;\n" 894 1.1 dyoung "use \"disklabel -I\" to install initial label", s); 895 1.1 dyoung break; 896 1.1 dyoung 897 1.1 dyoung case EINVAL: 898 1.1 dyoung warnx("%s: Label magic number or checksum is wrong!\n" 899 1.1 dyoung "(disklabel or kernel is out of date?)", s); 900 1.1 dyoung break; 901 1.1 dyoung 902 1.1 dyoung case EBUSY: 903 1.1 dyoung warnx("%s: Open partition would move or shrink", s); 904 1.1 dyoung break; 905 1.1 dyoung 906 1.1 dyoung case EXDEV: 907 1.1 dyoung warnx("%s: Labeled partition or 'a' partition must start" 908 1.1 dyoung " at beginning of disk", s); 909 1.1 dyoung break; 910 1.1 dyoung 911 1.1 dyoung default: 912 1.1 dyoung warn("%s", s); 913 1.1 dyoung break; 914 1.1 dyoung } 915 1.1 dyoung } 916 1.1 dyoung 917 1.6 dsl #ifdef NO_MBR_SUPPORT 918 1.6 dsl #define process_mbr(f, action) 1 919 1.6 dsl #else 920 1.1 dyoung /* 921 1.6 dsl * Scan DOS/MBR partition table and extended partition list for NetBSD ptns. 922 1.1 dyoung */ 923 1.6 dsl static int 924 1.6 dsl process_mbr(int f, int (*action)(int, u_int)) 925 1.1 dyoung { 926 1.1 dyoung struct mbr_partition *dp; 927 1.1 dyoung struct mbr_sector mbr; 928 1.6 dsl int rval = 1, res; 929 1.1 dyoung int part; 930 1.6 dsl u_int ext_base, next_ext, this_ext, start; 931 1.1 dyoung 932 1.1 dyoung ext_base = 0; 933 1.1 dyoung next_ext = 0; 934 1.1 dyoung for (;;) { 935 1.1 dyoung this_ext = next_ext; 936 1.1 dyoung next_ext = 0; 937 1.6 dsl if (verbose > 1) 938 1.6 dsl warnx("reading mbr sector %u", this_ext); 939 1.1 dyoung if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE) 940 1.1 dyoung != sizeof(mbr)) { 941 1.6 dsl if (verbose) 942 1.24 apb warn("Can't read master boot record %u", 943 1.6 dsl this_ext); 944 1.6 dsl break; 945 1.1 dyoung } 946 1.1 dyoung 947 1.1 dyoung /* Check if table is valid. */ 948 1.1 dyoung if (mbr.mbr_magic != htole16(MBR_MAGIC)) { 949 1.6 dsl if (verbose) 950 1.24 apb warnx("Invalid signature in mbr record %u", 951 1.6 dsl this_ext); 952 1.6 dsl break; 953 1.1 dyoung } 954 1.1 dyoung 955 1.1 dyoung dp = &mbr.mbr_parts[0]; 956 1.1 dyoung 957 1.6 dsl /* Find NetBSD partition(s). */ 958 1.1 dyoung for (part = 0; part < MBR_PART_COUNT; dp++, part++) { 959 1.6 dsl start = le32toh(dp->mbrp_start); 960 1.1 dyoung switch (dp->mbrp_type) { 961 1.6 dsl #ifdef COMPAT_386BSD_MBRPART 962 1.6 dsl case MBR_PTYPE_386BSD: 963 1.6 dsl if (ext_base != 0) 964 1.6 dsl break; 965 1.6 dsl /* FALLTHROUGH */ 966 1.6 dsl #endif 967 1.1 dyoung case MBR_PTYPE_NETBSD: 968 1.6 dsl res = action(f, this_ext + start); 969 1.6 dsl if (res <= 0) 970 1.6 dsl /* Found or failure */ 971 1.6 dsl return res; 972 1.6 dsl if (res > rval) 973 1.6 dsl /* Keep largest value */ 974 1.6 dsl rval = res; 975 1.1 dyoung break; 976 1.1 dyoung case MBR_PTYPE_EXT: 977 1.1 dyoung case MBR_PTYPE_EXT_LBA: 978 1.1 dyoung case MBR_PTYPE_EXT_LNX: 979 1.6 dsl next_ext = start; 980 1.6 dsl break; 981 1.1 dyoung default: 982 1.6 dsl break; 983 1.1 dyoung } 984 1.1 dyoung } 985 1.1 dyoung if (next_ext == 0) 986 1.1 dyoung /* No more extended partitions */ 987 1.1 dyoung break; 988 1.1 dyoung next_ext += ext_base; 989 1.1 dyoung if (ext_base == 0) 990 1.1 dyoung ext_base = next_ext; 991 1.1 dyoung 992 1.1 dyoung if (next_ext <= this_ext) { 993 1.6 dsl if (verbose) 994 1.6 dsl warnx("Invalid extended chain %x <= %x", 995 1.6 dsl next_ext, this_ext); 996 1.1 dyoung break; 997 1.1 dyoung } 998 1.6 dsl /* Maybe we should check against the disk size... */ 999 1.1 dyoung } 1000 1.1 dyoung 1001 1.6 dsl return rval; 1002 1.6 dsl } 1003 1.6 dsl 1004 1.6 dsl static int 1005 1.6 dsl readlabel_mbr(int f, u_int sector) 1006 1.6 dsl { 1007 1.22 tsutsui struct disklabel *disk_lp; 1008 1.6 dsl 1009 1.22 tsutsui disk_lp = find_label(f, sector); 1010 1.22 tsutsui if (disk_lp == NULL) 1011 1.6 dsl return 1; 1012 1.22 tsutsui targettohlabel(&lab, disk_lp); 1013 1.6 dsl return 0; 1014 1.6 dsl } 1015 1.1 dyoung 1016 1.6 dsl static int 1017 1.6 dsl writelabel_mbr(int f, u_int sector) 1018 1.6 dsl { 1019 1.51 mlelstv return update_label(f, sector, labelusesmbr ? LABELOFFSET_MBR : ~0U) ? 2 : 0; 1020 1.1 dyoung } 1021 1.1 dyoung 1022 1.6 dsl #endif /* !NO_MBR_SUPPORT */ 1023 1.6 dsl 1024 1.6 dsl #ifndef USE_ACORN 1025 1.6 dsl #define get_filecore_partition(f) 0 1026 1.6 dsl #else 1027 1.1 dyoung /* 1028 1.1 dyoung * static int filecore_checksum(u_char *bootblock) 1029 1.1 dyoung * 1030 1.1 dyoung * Calculates the filecore boot block checksum. This is used to validate 1031 1.1 dyoung * a filecore boot block on the disk. If a boot block is validated then 1032 1.1 dyoung * it is used to locate the partition table. If the boot block is not 1033 1.1 dyoung * validated, it is assumed that the whole disk is NetBSD. 1034 1.1 dyoung * 1035 1.1 dyoung * The basic algorithm is: 1036 1.1 dyoung * 1037 1.1 dyoung * for (each byte in block, excluding checksum) { 1038 1.1 dyoung * sum += byte; 1039 1.1 dyoung * if (sum > 255) 1040 1.1 dyoung * sum -= 255; 1041 1.1 dyoung * } 1042 1.1 dyoung * 1043 1.1 dyoung * That's equivalent to summing all of the bytes in the block 1044 1.1 dyoung * (excluding the checksum byte, of course), then calculating the 1045 1.1 dyoung * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That 1046 1.1 dyoung * expression may or may not yield a faster checksum function, 1047 1.1 dyoung * but it's easier to reason about. 1048 1.1 dyoung * 1049 1.1 dyoung * Note that if you have a block filled with bytes of a single 1050 1.1 dyoung * value "X" (regardless of that value!) and calculate the cksum 1051 1.1 dyoung * of the block (excluding the checksum byte), you will _always_ 1052 1.1 dyoung * end up with a checksum of X. (Do the math; that can be derived 1053 1.1 dyoung * from the checksum calculation function!) That means that 1054 1.1 dyoung * blocks which contain bytes which all have the same value will 1055 1.55 msaitoh * always checksum properly. That's a _very_ unlikely occurrence 1056 1.1 dyoung * (probably impossible, actually) for a valid filecore boot block, 1057 1.1 dyoung * so we treat such blocks as invalid. 1058 1.1 dyoung */ 1059 1.1 dyoung static int 1060 1.1 dyoung filecore_checksum(u_char *bootblock) 1061 1.1 dyoung { 1062 1.1 dyoung u_char byte0, accum_diff; 1063 1.1 dyoung u_int sum; 1064 1.1 dyoung int i; 1065 1.1 dyoung 1066 1.1 dyoung sum = 0; 1067 1.1 dyoung accum_diff = 0; 1068 1.1 dyoung byte0 = bootblock[0]; 1069 1.1 dyoung 1070 1.1 dyoung /* 1071 1.1 dyoung * Sum the contents of the block, keeping track of whether 1072 1.1 dyoung * or not all bytes are the same. If 'accum_diff' ends up 1073 1.1 dyoung * being zero, all of the bytes are, in fact, the same. 1074 1.1 dyoung */ 1075 1.1 dyoung for (i = 0; i < 511; ++i) { 1076 1.1 dyoung sum += bootblock[i]; 1077 1.1 dyoung accum_diff |= bootblock[i] ^ byte0; 1078 1.1 dyoung } 1079 1.1 dyoung 1080 1.1 dyoung /* 1081 1.1 dyoung * Check to see if the checksum byte is the same as the 1082 1.1 dyoung * rest of the bytes, too. (Note that if all of the bytes 1083 1.1 dyoung * are the same except the checksum, a checksum compare 1084 1.1 dyoung * won't succeed, but that's not our problem.) 1085 1.1 dyoung */ 1086 1.1 dyoung accum_diff |= bootblock[i] ^ byte0; 1087 1.1 dyoung 1088 1.1 dyoung /* All bytes in block are the same; call it invalid. */ 1089 1.1 dyoung if (accum_diff == 0) 1090 1.1 dyoung return (-1); 1091 1.1 dyoung 1092 1.1 dyoung return (sum - ((sum - 1) / 255) * 255); 1093 1.1 dyoung } 1094 1.1 dyoung 1095 1.1 dyoung /* 1096 1.6 dsl * Check for the presence of a RiscOS filecore boot block 1097 1.6 dsl * indicating an ADFS file system on the disc. 1098 1.6 dsl * Return the offset to the NetBSD part of the disc if 1099 1.6 dsl * this can be determined. 1100 1.6 dsl * This routine will terminate disklabel if the disc 1101 1.6 dsl * is found to be ADFS only. 1102 1.1 dyoung */ 1103 1.1 dyoung static u_int 1104 1.1 dyoung get_filecore_partition(int f) 1105 1.1 dyoung { 1106 1.1 dyoung struct filecore_bootblock *fcbb; 1107 1.12 christos static u_char bb[DEV_BSIZE]; 1108 1.1 dyoung u_int offset; 1109 1.6 dsl struct riscix_partition_table *riscix_part; 1110 1.6 dsl int loop; 1111 1.1 dyoung 1112 1.6 dsl if (pread(f, bb, sizeof(bb), (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE) != sizeof(bb)) 1113 1.1 dyoung err(4, "can't read filecore boot block"); 1114 1.1 dyoung fcbb = (struct filecore_bootblock *)bb; 1115 1.1 dyoung 1116 1.1 dyoung /* Check if table is valid. */ 1117 1.1 dyoung if (filecore_checksum(bb) != fcbb->checksum) 1118 1.1 dyoung return (0); 1119 1.1 dyoung 1120 1.1 dyoung /* 1121 1.1 dyoung * Check for NetBSD/arm32 (RiscBSD) partition marker. 1122 1.1 dyoung * If found the NetBSD disklabel location is easy. 1123 1.1 dyoung */ 1124 1.1 dyoung offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8)) 1125 1.1 dyoung * fcbb->heads * fcbb->secspertrack; 1126 1.6 dsl 1127 1.6 dsl switch (fcbb->partition_type) { 1128 1.6 dsl 1129 1.6 dsl case PARTITION_FORMAT_RISCBSD: 1130 1.1 dyoung return (offset); 1131 1.1 dyoung 1132 1.6 dsl case PARTITION_FORMAT_RISCIX: 1133 1.1 dyoung /* 1134 1.1 dyoung * Read the RISCiX partition table and search for the 1135 1.1 dyoung * first partition named "RiscBSD", "NetBSD", or "Empty:" 1136 1.1 dyoung * 1137 1.1 dyoung * XXX is use of 'Empty:' really desirable?! -- cgd 1138 1.1 dyoung */ 1139 1.1 dyoung 1140 1.6 dsl if (pread(f, bb, sizeof(bb), (off_t)offset * DEV_BSIZE) != sizeof(bb)) 1141 1.1 dyoung err(4, "can't read riscix partition table"); 1142 1.1 dyoung riscix_part = (struct riscix_partition_table *)bb; 1143 1.1 dyoung 1144 1.1 dyoung for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) { 1145 1.12 christos if (strcmp((char *)riscix_part->partitions[loop].rp_name, 1146 1.6 dsl "RiscBSD") == 0 || 1147 1.12 christos strcmp((char *)riscix_part->partitions[loop].rp_name, 1148 1.6 dsl "NetBSD") == 0 || 1149 1.12 christos strcmp((char *)riscix_part->partitions[loop].rp_name, 1150 1.6 dsl "Empty:") == 0) { 1151 1.6 dsl return riscix_part->partitions[loop].rp_start; 1152 1.1 dyoung break; 1153 1.1 dyoung } 1154 1.1 dyoung } 1155 1.6 dsl /* 1156 1.6 dsl * Valid filecore boot block, RISCiX partition table 1157 1.6 dsl * but no NetBSD partition. We should leave this 1158 1.6 dsl * disc alone. 1159 1.6 dsl */ 1160 1.6 dsl errx(4, "cannot label: no NetBSD partition found" 1161 1.6 dsl " in RISCiX partition table"); 1162 1.6 dsl 1163 1.6 dsl default: 1164 1.1 dyoung /* 1165 1.1 dyoung * Valid filecore boot block and no non-ADFS partition. 1166 1.1 dyoung * This means that the whole disc is allocated for ADFS 1167 1.1 dyoung * so do not trash ! If the user really wants to put a 1168 1.1 dyoung * NetBSD disklabel on the disc then they should remove 1169 1.1 dyoung * the filecore boot block first with dd. 1170 1.1 dyoung */ 1171 1.1 dyoung errx(4, "cannot label: filecore-only disk" 1172 1.1 dyoung " (no non-ADFS partition)"); 1173 1.1 dyoung } 1174 1.1 dyoung return (0); 1175 1.1 dyoung } 1176 1.1 dyoung #endif /* USE_ACORN */ 1177 1.1 dyoung 1178 1.1 dyoung /* 1179 1.6 dsl * Fetch disklabel for disk to 'lab'. 1180 1.1 dyoung * Use ioctl to get label unless -r flag is given. 1181 1.1 dyoung */ 1182 1.6 dsl static void 1183 1.1 dyoung readlabel(int f) 1184 1.1 dyoung { 1185 1.6 dsl if (rflag) { 1186 1.6 dsl /* Get label directly from disk */ 1187 1.6 dsl if (readlabel_direct(f) == 0) 1188 1.6 dsl return; 1189 1.1 dyoung /* 1190 1.1 dyoung * There was no label on the disk. Get the fictious one 1191 1.1 dyoung * as a basis for initialisation. 1192 1.1 dyoung */ 1193 1.6 dsl if (!Fflag && Iflag && (dk_ioctl(f, DIOCGDINFO, &lab) == 0 || 1194 1.6 dsl dk_ioctl(f, DIOCGDEFLABEL, &lab) == 0)) 1195 1.6 dsl return; 1196 1.1 dyoung } else { 1197 1.6 dsl /* Get label from kernel. */ 1198 1.6 dsl if (dk_ioctl(f, DIOCGDINFO, &lab) < 0) 1199 1.1 dyoung err(4, "ioctl DIOCGDINFO"); 1200 1.6 dsl return; 1201 1.1 dyoung } 1202 1.6 dsl 1203 1.6 dsl if (read_all == 2) 1204 1.6 dsl /* We actually found one, and printed it... */ 1205 1.6 dsl exit(0); 1206 1.6 dsl errx(1, "could not read existing label"); 1207 1.1 dyoung } 1208 1.1 dyoung 1209 1.1 dyoung /* 1210 1.6 dsl * Reading the label from the disk is largely a case of 'hunt the label'. 1211 1.6 dsl * and since different architectures default to different places there 1212 1.6 dsl * could even be more than one label that contradict each other! 1213 1.6 dsl * For now we look in the expected place, then search through likely 1214 1.6 dsl * other locations. 1215 1.1 dyoung */ 1216 1.1 dyoung static struct disklabel * 1217 1.6 dsl find_label(int f, u_int sector) 1218 1.1 dyoung { 1219 1.50 kamil struct disklabel *disk_lp, hlp, tlp; 1220 1.24 apb int i; 1221 1.34 christos off_t offset; 1222 1.6 dsl const char *is_deleted; 1223 1.6 dsl 1224 1.6 dsl bootarea_len = pread(f, bootarea, sizeof bootarea, 1225 1.6 dsl sector * (off_t)DEV_BSIZE); 1226 1.6 dsl if (bootarea_len <= 0) { 1227 1.6 dsl if (verbose) 1228 1.6 dsl warn("failed to read bootarea from sector %u", sector); 1229 1.6 dsl return NULL; 1230 1.6 dsl } 1231 1.6 dsl 1232 1.6 dsl if (verbose > 2) 1233 1.24 apb warnx("read sector %u len %d looking for label", 1234 1.6 dsl sector, bootarea_len); 1235 1.6 dsl 1236 1.6 dsl /* Check expected offset first */ 1237 1.6 dsl for (offset = LABEL_OFFSET, i = -4;; offset = i += 4) { 1238 1.6 dsl is_deleted = ""; 1239 1.6 dsl if (i == LABEL_OFFSET) 1240 1.6 dsl continue; 1241 1.50 kamil disk_lp = (void *)(bootarea + offset); 1242 1.22 tsutsui if ((char *)(disk_lp + 1) > bootarea + bootarea_len) 1243 1.6 dsl break; 1244 1.56 christos memcpy(&tlp, disk_lp, sizeof(tlp)); 1245 1.50 kamil if (tlp.d_magic2 != tlp.d_magic) 1246 1.6 dsl continue; 1247 1.50 kamil if (read_all && (tlp.d_magic == DISKMAGIC_DELETED || 1248 1.50 kamil tlp.d_magic == DISKMAGIC_DELETED_REV)) { 1249 1.50 kamil tlp.d_magic ^= ~0u; 1250 1.50 kamil tlp.d_magic2 ^= ~0u; 1251 1.6 dsl is_deleted = "deleted "; 1252 1.6 dsl } 1253 1.50 kamil if (target32toh(tlp.d_magic) != DISKMAGIC) { 1254 1.6 dsl /* XXX: Do something about byte-swapped labels ? */ 1255 1.50 kamil if (target32toh(tlp.d_magic) == DISKMAGIC_REV && 1256 1.50 kamil target32toh(tlp.d_magic2) == DISKMAGIC_REV) 1257 1.6 dsl warnx("ignoring %sbyteswapped label" 1258 1.35 christos " at offset %jd from sector %u", 1259 1.35 christos is_deleted, (intmax_t)offset, sector); 1260 1.6 dsl continue; 1261 1.6 dsl } 1262 1.50 kamil if (target16toh(tlp.d_npartitions) > maxpartitions || 1263 1.50 kamil dkcksum_target(&tlp) != 0) { 1264 1.6 dsl if (verbose > 0) 1265 1.35 christos warnx("corrupt label found at offset %jd in " 1266 1.35 christos "sector %u", (intmax_t)offset, sector); 1267 1.6 dsl continue; 1268 1.6 dsl } 1269 1.6 dsl if (verbose > 1) 1270 1.35 christos warnx("%slabel found at offset %jd from sector %u", 1271 1.35 christos is_deleted, (intmax_t)offset, sector); 1272 1.6 dsl if (!read_all) 1273 1.22 tsutsui return disk_lp; 1274 1.6 dsl 1275 1.6 dsl /* To print all the labels we have to do it here */ 1276 1.6 dsl /* XXX: maybe we should compare them? */ 1277 1.50 kamil targettohlabel(&hlp, &tlp); 1278 1.35 christos printf("# %ssector %u offset %jd bytes\n", 1279 1.35 christos is_deleted, sector, (intmax_t)offset); 1280 1.6 dsl if (tflag) 1281 1.22 tsutsui makedisktab(stdout, &hlp); 1282 1.6 dsl else { 1283 1.22 tsutsui showinfo(stdout, &hlp, specname); 1284 1.22 tsutsui showpartitions(stdout, &hlp, Cflag); 1285 1.6 dsl } 1286 1.22 tsutsui checklabel(&hlp); 1287 1.50 kamil htotargetlabel(&tlp, &hlp); 1288 1.50 kamil memcpy(disk_lp, &tlp, sizeof(tlp)); 1289 1.6 dsl /* Remember we've found a label */ 1290 1.6 dsl read_all = 2; 1291 1.1 dyoung } 1292 1.6 dsl return NULL; 1293 1.6 dsl } 1294 1.6 dsl 1295 1.6 dsl static void 1296 1.6 dsl write_bootarea(int f, u_int sector) 1297 1.6 dsl { 1298 1.6 dsl int wlen; 1299 1.1 dyoung 1300 1.6 dsl if (bootarea_len <= 0) 1301 1.6 dsl errx(1, "attempting to write after failed read"); 1302 1.6 dsl 1303 1.21 tsutsui #ifdef ALPHA_BOOTBLOCK_CKSUM 1304 1.1 dyoung /* 1305 1.6 dsl * The Alpha requires that the boot block be checksummed. 1306 1.21 tsutsui * <sys/bootblock.h> provides a macro to do it. 1307 1.1 dyoung */ 1308 1.6 dsl if (sector == 0) { 1309 1.6 dsl struct alpha_boot_block *bb; 1310 1.6 dsl 1311 1.10 christos bb = (struct alpha_boot_block *)(void *)bootarea; 1312 1.6 dsl bb->bb_cksum = 0; 1313 1.6 dsl ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum); 1314 1.6 dsl } 1315 1.21 tsutsui #endif /* ALPHA_BOOTBLOCK_CKSUM */ 1316 1.6 dsl 1317 1.6 dsl wlen = pwrite(f, bootarea, bootarea_len, sector * (off_t)DEV_BSIZE); 1318 1.6 dsl if (wlen == bootarea_len) 1319 1.6 dsl return; 1320 1.6 dsl if (wlen == -1) 1321 1.24 apb err(1, "disklabel write (sector %u) size %d failed", 1322 1.6 dsl sector, bootarea_len); 1323 1.24 apb errx(1, "disklabel write (sector %u) size %d truncated to %d", 1324 1.6 dsl sector, bootarea_len, wlen); 1325 1.6 dsl } 1326 1.1 dyoung 1327 1.6 dsl static int 1328 1.6 dsl update_label(int f, u_int label_sector, u_int label_offset) 1329 1.6 dsl { 1330 1.6 dsl struct disklabel *disk_lp; 1331 1.6 dsl 1332 1.6 dsl disk_lp = find_label(f, label_sector); 1333 1.6 dsl 1334 1.6 dsl if (disk_lp && Dflag) { 1335 1.6 dsl /* Invalidate the existing label */ 1336 1.6 dsl disk_lp->d_magic ^= ~0u; 1337 1.6 dsl disk_lp->d_magic2 ^= ~0u; 1338 1.6 dsl if (Dflag == 2) 1339 1.6 dsl write_bootarea(f, label_sector); 1340 1.6 dsl /* Force label to default location */ 1341 1.6 dsl disk_lp = NULL; 1342 1.1 dyoung } 1343 1.1 dyoung 1344 1.6 dsl if (Dflag == 2) 1345 1.6 dsl /* We are just deleting the label */ 1346 1.6 dsl return 0; 1347 1.6 dsl 1348 1.6 dsl if (disk_lp == NULL) { 1349 1.6 dsl if (label_offset == ~0u) 1350 1.6 dsl return 0; 1351 1.6 dsl /* Nothing on the disk - we need to add it */ 1352 1.15 drochner disk_lp = (void *)(bootarea + label_offset); 1353 1.15 drochner if ((char *)(disk_lp + 1) > bootarea + bootarea_len) 1354 1.6 dsl errx(1, "no space in bootarea (sector %u) " 1355 1.6 dsl "to create label", label_sector); 1356 1.6 dsl } 1357 1.6 dsl 1358 1.22 tsutsui htotargetlabel(disk_lp, &lab); 1359 1.6 dsl write_bootarea(f, label_sector); 1360 1.6 dsl return 1; 1361 1.6 dsl } 1362 1.6 dsl 1363 1.6 dsl static void 1364 1.6 dsl writelabel_direct(int f) 1365 1.6 dsl { 1366 1.6 dsl u_int label_sector; 1367 1.6 dsl int written = 0; 1368 1.6 dsl int rval; 1369 1.6 dsl 1370 1.6 dsl label_sector = get_filecore_partition(f); 1371 1.6 dsl if (label_sector != 0) 1372 1.6 dsl /* The offset needs to be that from the acorn ports... */ 1373 1.6 dsl written = update_label(f, label_sector, DEV_BSIZE); 1374 1.6 dsl 1375 1.6 dsl rval = process_mbr(f, writelabel_mbr); 1376 1.6 dsl 1377 1.6 dsl if (rval == 2 || written) 1378 1.6 dsl /* Don't add a label to sector 0, but update one if there */ 1379 1.6 dsl update_label(f, 0, ~0u); 1380 1.6 dsl else 1381 1.6 dsl update_label(f, 0, LABEL_OFFSET); 1382 1.6 dsl } 1383 1.6 dsl 1384 1.6 dsl static int 1385 1.6 dsl readlabel_direct(int f) 1386 1.6 dsl { 1387 1.6 dsl struct disklabel *disk_lp; 1388 1.6 dsl u_int filecore_partition_offset; 1389 1.6 dsl 1390 1.6 dsl filecore_partition_offset = get_filecore_partition(f); 1391 1.6 dsl if (filecore_partition_offset != 0) { 1392 1.6 dsl disk_lp = find_label(f, filecore_partition_offset); 1393 1.6 dsl if (disk_lp != NULL) { 1394 1.22 tsutsui targettohlabel(&lab, disk_lp); 1395 1.6 dsl return 0; 1396 1.6 dsl } 1397 1.6 dsl } 1398 1.6 dsl 1399 1.51 mlelstv if (labelusesmbr && process_mbr(f, readlabel_mbr) == 0) 1400 1.6 dsl return 0; 1401 1.6 dsl 1402 1.6 dsl disk_lp = find_label(f, 0); 1403 1.6 dsl if (disk_lp != NULL) { 1404 1.22 tsutsui targettohlabel(&lab, disk_lp); 1405 1.6 dsl return 0; 1406 1.6 dsl } 1407 1.6 dsl 1408 1.51 mlelstv if (!labelusesmbr && process_mbr(f, readlabel_mbr) == 0) 1409 1.6 dsl return 0; 1410 1.6 dsl 1411 1.6 dsl return 1; 1412 1.1 dyoung } 1413 1.1 dyoung 1414 1.1 dyoung static void 1415 1.1 dyoung makedisktab(FILE *f, struct disklabel *lp) 1416 1.1 dyoung { 1417 1.1 dyoung int i; 1418 1.1 dyoung const char *did; 1419 1.1 dyoung struct partition *pp; 1420 1.1 dyoung 1421 1.1 dyoung did = "\\\n\t:"; 1422 1.1 dyoung (void) fprintf(f, "%.*s|Automatically generated label:\\\n\t:dt=", 1423 1.1 dyoung (int) sizeof(lp->d_typename), lp->d_typename); 1424 1.1 dyoung if ((unsigned) lp->d_type < DKMAXTYPES) 1425 1.1 dyoung (void) fprintf(f, "%s:", dktypenames[lp->d_type]); 1426 1.1 dyoung else 1427 1.23 christos (void) fprintf(f, "unknown%" PRIu16 ":", lp->d_type); 1428 1.1 dyoung 1429 1.23 christos (void) fprintf(f, "se#%" PRIu32 ":", lp->d_secsize); 1430 1.23 christos (void) fprintf(f, "ns#%" PRIu32 ":", lp->d_nsectors); 1431 1.23 christos (void) fprintf(f, "nt#%" PRIu32 ":", lp->d_ntracks); 1432 1.23 christos (void) fprintf(f, "sc#%" PRIu32 ":", lp->d_secpercyl); 1433 1.23 christos (void) fprintf(f, "nc#%" PRIu32 ":", lp->d_ncylinders); 1434 1.1 dyoung 1435 1.1 dyoung if ((lp->d_secpercyl * lp->d_ncylinders) != lp->d_secperunit) { 1436 1.23 christos (void) fprintf(f, "%ssu#%" PRIu32 ":", did, lp->d_secperunit); 1437 1.1 dyoung did = ""; 1438 1.1 dyoung } 1439 1.1 dyoung if (lp->d_rpm != 3600) { 1440 1.23 christos (void) fprintf(f, "%srm#%" PRIu16 ":", did, lp->d_rpm); 1441 1.1 dyoung did = ""; 1442 1.1 dyoung } 1443 1.1 dyoung if (lp->d_interleave != 1) { 1444 1.23 christos (void) fprintf(f, "%sil#%" PRIu16 ":", did, lp->d_interleave); 1445 1.1 dyoung did = ""; 1446 1.1 dyoung } 1447 1.1 dyoung if (lp->d_trackskew != 0) { 1448 1.23 christos (void) fprintf(f, "%ssk#%" PRIu16 ":", did, lp->d_trackskew); 1449 1.1 dyoung did = ""; 1450 1.1 dyoung } 1451 1.1 dyoung if (lp->d_cylskew != 0) { 1452 1.23 christos (void) fprintf(f, "%scs#%" PRIu16 ":", did, lp->d_cylskew); 1453 1.1 dyoung did = ""; 1454 1.1 dyoung } 1455 1.1 dyoung if (lp->d_headswitch != 0) { 1456 1.39 joerg (void) fprintf(f, "%shs#%" PRIu32 ":", did, lp->d_headswitch); 1457 1.1 dyoung did = ""; 1458 1.1 dyoung } 1459 1.1 dyoung if (lp->d_trkseek != 0) { 1460 1.23 christos (void) fprintf(f, "%sts#%" PRIu32 ":", did, lp->d_trkseek); 1461 1.1 dyoung did = ""; 1462 1.1 dyoung } 1463 1.1 dyoung #ifdef notyet 1464 1.1 dyoung (void) fprintf(f, "drivedata: "); 1465 1.1 dyoung for (i = NDDATA - 1; i >= 0; i--) 1466 1.1 dyoung if (lp->d_drivedata[i]) 1467 1.1 dyoung break; 1468 1.1 dyoung if (i < 0) 1469 1.1 dyoung i = 0; 1470 1.1 dyoung for (j = 0; j <= i; j++) 1471 1.23 christos (void) fprintf(f, "%" PRIu32 " ", lp->d_drivedata[j]); 1472 1.1 dyoung #endif /* notyet */ 1473 1.1 dyoung pp = lp->d_partitions; 1474 1.1 dyoung for (i = 0; i < lp->d_npartitions; i++, pp++) { 1475 1.1 dyoung if (pp->p_size) { 1476 1.1 dyoung char c = 'a' + i; 1477 1.1 dyoung (void) fprintf(f, "\\\n\t:"); 1478 1.23 christos (void) fprintf(f, "p%c#%" PRIu32 ":", c, pp->p_size); 1479 1.23 christos (void) fprintf(f, "o%c#%" PRIu32 ":", c, pp->p_offset); 1480 1.1 dyoung if (pp->p_fstype != FS_UNUSED) { 1481 1.1 dyoung if ((unsigned) pp->p_fstype < FSMAXTYPES) 1482 1.1 dyoung (void) fprintf(f, "t%c=%s:", c, 1483 1.1 dyoung fstypenames[pp->p_fstype]); 1484 1.1 dyoung else 1485 1.24 apb (void) fprintf(f, 1486 1.24 apb "t%c=unknown%" PRIu8 ":", 1487 1.24 apb c, pp->p_fstype); 1488 1.1 dyoung } 1489 1.1 dyoung switch (pp->p_fstype) { 1490 1.1 dyoung 1491 1.1 dyoung case FS_UNUSED: 1492 1.1 dyoung break; 1493 1.1 dyoung 1494 1.1 dyoung case FS_BSDFFS: 1495 1.1 dyoung case FS_BSDLFS: 1496 1.1 dyoung case FS_EX2FS: 1497 1.1 dyoung case FS_ADOS: 1498 1.1 dyoung case FS_APPLEUFS: 1499 1.23 christos (void) fprintf(f, "b%c#%" PRIu64 ":", c, 1500 1.23 christos (uint64_t)pp->p_fsize * pp->p_frag); 1501 1.23 christos (void) fprintf(f, "f%c#%" PRIu32 ":", c, 1502 1.23 christos pp->p_fsize); 1503 1.1 dyoung break; 1504 1.1 dyoung default: 1505 1.1 dyoung break; 1506 1.1 dyoung } 1507 1.1 dyoung } 1508 1.1 dyoung } 1509 1.1 dyoung (void) fprintf(f, "\n"); 1510 1.1 dyoung (void) fflush(f); 1511 1.1 dyoung } 1512 1.1 dyoung 1513 1.1 dyoung static int 1514 1.6 dsl edit(int f) 1515 1.1 dyoung { 1516 1.1 dyoung const char *tmpdir; 1517 1.6 dsl char tmpfil[MAXPATHLEN]; 1518 1.1 dyoung int first, ch, fd; 1519 1.9 dsl int get_ok; 1520 1.1 dyoung FILE *fp; 1521 1.1 dyoung 1522 1.1 dyoung if ((tmpdir = getenv("TMPDIR")) == NULL) 1523 1.1 dyoung tmpdir = _PATH_TMP; 1524 1.1 dyoung (void)snprintf(tmpfil, sizeof(tmpfil), "%s/%s", tmpdir, TMPFILE); 1525 1.1 dyoung if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) { 1526 1.1 dyoung warn("%s", tmpfil); 1527 1.1 dyoung return (1); 1528 1.1 dyoung } 1529 1.1 dyoung (void)fchmod(fd, 0600); 1530 1.6 dsl showinfo(fp, &lab, specname); 1531 1.6 dsl showpartitions(fp, &lab, Cflag); 1532 1.1 dyoung (void) fclose(fp); 1533 1.1 dyoung for (;;) { 1534 1.6 dsl if (!editit(tmpfil)) 1535 1.1 dyoung break; 1536 1.1 dyoung fp = fopen(tmpfil, "r"); 1537 1.1 dyoung if (fp == NULL) { 1538 1.1 dyoung warn("%s", tmpfil); 1539 1.1 dyoung break; 1540 1.1 dyoung } 1541 1.6 dsl (void) memset(&lab, 0, sizeof(lab)); 1542 1.9 dsl get_ok = getasciilabel(fp, &lab); 1543 1.9 dsl fclose(fp); 1544 1.9 dsl if (get_ok && write_label(f) == 0) { 1545 1.9 dsl (void) unlink(tmpfil); 1546 1.9 dsl return (0); 1547 1.1 dyoung } 1548 1.1 dyoung (void) printf("re-edit the label? [y]: "); 1549 1.1 dyoung (void) fflush(stdout); 1550 1.1 dyoung first = ch = getchar(); 1551 1.1 dyoung while (ch != '\n' && ch != EOF) 1552 1.1 dyoung ch = getchar(); 1553 1.1 dyoung if (first == 'n' || first == 'N') 1554 1.1 dyoung break; 1555 1.1 dyoung } 1556 1.1 dyoung (void)unlink(tmpfil); 1557 1.1 dyoung return (1); 1558 1.1 dyoung } 1559 1.1 dyoung 1560 1.1 dyoung static int 1561 1.6 dsl editit(const char *tmpfil) 1562 1.1 dyoung { 1563 1.1 dyoung int pid, xpid; 1564 1.1 dyoung int status; 1565 1.1 dyoung sigset_t nsigset, osigset; 1566 1.1 dyoung 1567 1.1 dyoung sigemptyset(&nsigset); 1568 1.1 dyoung sigaddset(&nsigset, SIGINT); 1569 1.1 dyoung sigaddset(&nsigset, SIGQUIT); 1570 1.1 dyoung sigaddset(&nsigset, SIGHUP); 1571 1.1 dyoung sigprocmask(SIG_BLOCK, &nsigset, &osigset); 1572 1.1 dyoung while ((pid = fork()) < 0) { 1573 1.1 dyoung if (errno != EAGAIN) { 1574 1.1 dyoung sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); 1575 1.1 dyoung warn("fork"); 1576 1.1 dyoung return (0); 1577 1.1 dyoung } 1578 1.1 dyoung sleep(1); 1579 1.1 dyoung } 1580 1.1 dyoung if (pid == 0) { 1581 1.1 dyoung const char *ed; 1582 1.1 dyoung char *buf; 1583 1.1 dyoung int retval; 1584 1.1 dyoung 1585 1.1 dyoung sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); 1586 1.1 dyoung setgid(getgid()); 1587 1.1 dyoung setuid(getuid()); 1588 1.1 dyoung if ((ed = getenv("EDITOR")) == (char *)0) 1589 1.1 dyoung ed = DEFEDITOR; 1590 1.1 dyoung /* 1591 1.1 dyoung * Jump through a few extra hoops in case someone's editor 1592 1.1 dyoung * is "editor arg1 arg2". 1593 1.1 dyoung */ 1594 1.1 dyoung asprintf(&buf, "%s %s", ed, tmpfil); 1595 1.1 dyoung if (!buf) 1596 1.1 dyoung err(1, "malloc"); 1597 1.1 dyoung retval = execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", buf, NULL); 1598 1.1 dyoung if (retval == -1) 1599 1.1 dyoung perror(ed); 1600 1.1 dyoung exit(retval); 1601 1.1 dyoung } 1602 1.1 dyoung while ((xpid = wait(&status)) >= 0) 1603 1.1 dyoung if (xpid == pid) 1604 1.1 dyoung break; 1605 1.1 dyoung sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); 1606 1.1 dyoung return (!status); 1607 1.1 dyoung } 1608 1.1 dyoung 1609 1.1 dyoung static char * 1610 1.1 dyoung skip(char *cp) 1611 1.1 dyoung { 1612 1.1 dyoung 1613 1.1 dyoung cp += strspn(cp, " \t"); 1614 1.1 dyoung if (*cp == '\0') 1615 1.1 dyoung return (NULL); 1616 1.1 dyoung return (cp); 1617 1.1 dyoung } 1618 1.1 dyoung 1619 1.1 dyoung static char * 1620 1.1 dyoung word(char *cp) 1621 1.1 dyoung { 1622 1.1 dyoung 1623 1.1 dyoung if (cp == NULL || *cp == '\0') 1624 1.1 dyoung return (NULL); 1625 1.1 dyoung 1626 1.1 dyoung cp += strcspn(cp, " \t"); 1627 1.1 dyoung if (*cp == '\0') 1628 1.1 dyoung return (NULL); 1629 1.1 dyoung *cp++ = '\0'; 1630 1.1 dyoung cp += strspn(cp, " \t"); 1631 1.1 dyoung if (*cp == '\0') 1632 1.1 dyoung return (NULL); 1633 1.1 dyoung return (cp); 1634 1.1 dyoung } 1635 1.1 dyoung 1636 1.1 dyoung #define _CHECKLINE \ 1637 1.1 dyoung if (tp == NULL || *tp == '\0') { \ 1638 1.1 dyoung warnx("line %d: too few fields", lineno); \ 1639 1.1 dyoung errors++; \ 1640 1.1 dyoung break; \ 1641 1.1 dyoung } 1642 1.1 dyoung 1643 1.1 dyoung #define __CHECKLINE \ 1644 1.1 dyoung if (*tp == NULL || **tp == '\0') { \ 1645 1.1 dyoung warnx("line %d: too few fields", lineno); \ 1646 1.1 dyoung *tp = _error_; \ 1647 1.1 dyoung return 0; \ 1648 1.1 dyoung } 1649 1.1 dyoung 1650 1.1 dyoung static char _error_[] = ""; 1651 1.1 dyoung #define NXTNUM(n) if ((n = nxtnum(&tp, lineno),0) + tp != _error_) \ 1652 1.1 dyoung ; else goto error 1653 1.1 dyoung #define NXTXNUM(n) if ((n = nxtxnum(&tp, lp, lineno),0) + tp != _error_) \ 1654 1.1 dyoung ; else goto error 1655 1.1 dyoung 1656 1.1 dyoung static unsigned long 1657 1.1 dyoung nxtnum(char **tp, int lineno) 1658 1.1 dyoung { 1659 1.1 dyoung char *cp; 1660 1.1 dyoung unsigned long v; 1661 1.1 dyoung 1662 1.1 dyoung __CHECKLINE 1663 1.1 dyoung if (getulong(*tp, '\0', &cp, &v, UINT32_MAX) != 0) { 1664 1.1 dyoung warnx("line %d: syntax error", lineno); 1665 1.1 dyoung *tp = _error_; 1666 1.1 dyoung return 0; 1667 1.1 dyoung } 1668 1.1 dyoung *tp = cp; 1669 1.1 dyoung return v; 1670 1.1 dyoung } 1671 1.1 dyoung 1672 1.1 dyoung static unsigned long 1673 1.1 dyoung nxtxnum(char **tp, struct disklabel *lp, int lineno) 1674 1.1 dyoung { 1675 1.1 dyoung char *cp, *ncp; 1676 1.1 dyoung unsigned long n, v; 1677 1.1 dyoung 1678 1.1 dyoung __CHECKLINE 1679 1.1 dyoung cp = *tp; 1680 1.1 dyoung if (getulong(cp, '/', &ncp, &n, UINT32_MAX) != 0) 1681 1.1 dyoung goto bad; 1682 1.1 dyoung 1683 1.1 dyoung if (*ncp == '/') { 1684 1.1 dyoung n *= lp->d_secpercyl; 1685 1.1 dyoung cp = ncp + 1; 1686 1.1 dyoung if (getulong(cp, '/', &ncp, &v, UINT32_MAX) != 0) 1687 1.1 dyoung goto bad; 1688 1.1 dyoung n += v * lp->d_nsectors; 1689 1.1 dyoung cp = ncp + 1; 1690 1.1 dyoung if (getulong(cp, '\0', &ncp, &v, UINT32_MAX) != 0) 1691 1.1 dyoung goto bad; 1692 1.1 dyoung n += v; 1693 1.1 dyoung } 1694 1.1 dyoung *tp = ncp; 1695 1.1 dyoung return n; 1696 1.1 dyoung bad: 1697 1.1 dyoung warnx("line %d: invalid format", lineno); 1698 1.1 dyoung *tp = _error_; 1699 1.1 dyoung return 0; 1700 1.1 dyoung } 1701 1.1 dyoung 1702 1.1 dyoung /* 1703 1.1 dyoung * Read an ascii label in from fd f, 1704 1.1 dyoung * in the same format as that put out by showinfo() and showpartitions(), 1705 1.1 dyoung * and fill in lp. 1706 1.1 dyoung */ 1707 1.1 dyoung static int 1708 1.1 dyoung getasciilabel(FILE *f, struct disklabel *lp) 1709 1.1 dyoung { 1710 1.1 dyoung const char *const *cpp, *s; 1711 1.1 dyoung struct partition *pp; 1712 1.1 dyoung char *cp, *tp, line[BUFSIZ], tbuf[15]; 1713 1.1 dyoung int lineno, errors; 1714 1.1 dyoung unsigned long v; 1715 1.1 dyoung unsigned int part; 1716 1.1 dyoung 1717 1.1 dyoung lineno = 0; 1718 1.1 dyoung errors = 0; 1719 1.1 dyoung lp->d_bbsize = BBSIZE; /* XXX */ 1720 1.1 dyoung lp->d_sbsize = SBLOCKSIZE; /* XXX */ 1721 1.1 dyoung while (fgets(line, sizeof(line) - 1, f)) { 1722 1.1 dyoung lineno++; 1723 1.1 dyoung if ((cp = strpbrk(line, "#\r\n")) != NULL) 1724 1.1 dyoung *cp = '\0'; 1725 1.1 dyoung cp = skip(line); 1726 1.1 dyoung if (cp == NULL) /* blank line or comment line */ 1727 1.1 dyoung continue; 1728 1.1 dyoung tp = strchr(cp, ':'); /* everything has a colon in it */ 1729 1.1 dyoung if (tp == NULL) { 1730 1.1 dyoung warnx("line %d: syntax error", lineno); 1731 1.1 dyoung errors++; 1732 1.1 dyoung continue; 1733 1.1 dyoung } 1734 1.1 dyoung *tp++ = '\0', tp = skip(tp); 1735 1.1 dyoung if (!strcmp(cp, "type")) { 1736 1.1 dyoung if (tp == NULL) { 1737 1.1 dyoung strlcpy(tbuf, "unknown", sizeof(tbuf)); 1738 1.1 dyoung tp = tbuf; 1739 1.1 dyoung } 1740 1.1 dyoung cpp = dktypenames; 1741 1.1 dyoung for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 1742 1.1 dyoung if ((s = *cpp) && !strcasecmp(s, tp)) { 1743 1.1 dyoung lp->d_type = cpp - dktypenames; 1744 1.1 dyoung goto next; 1745 1.1 dyoung } 1746 1.1 dyoung if (GETNUM16(tp, &v) != 0) { 1747 1.1 dyoung warnx("line %d: syntax error", lineno); 1748 1.1 dyoung errors++; 1749 1.1 dyoung continue; 1750 1.1 dyoung } 1751 1.1 dyoung if (v >= DKMAXTYPES) 1752 1.1 dyoung warnx("line %d: warning, unknown disk type: %s", 1753 1.1 dyoung lineno, tp); 1754 1.1 dyoung lp->d_type = v; 1755 1.1 dyoung continue; 1756 1.1 dyoung } 1757 1.1 dyoung if (!strcmp(cp, "flags")) { 1758 1.1 dyoung for (v = 0; (cp = tp) && *cp != '\0';) { 1759 1.1 dyoung tp = word(cp); 1760 1.1 dyoung if (!strcasecmp(cp, "removable")) 1761 1.1 dyoung v |= D_REMOVABLE; 1762 1.1 dyoung else if (!strcasecmp(cp, "ecc")) 1763 1.1 dyoung v |= D_ECC; 1764 1.1 dyoung else if (!strcasecmp(cp, "badsect")) 1765 1.1 dyoung v |= D_BADSECT; 1766 1.1 dyoung else { 1767 1.1 dyoung warnx("line %d: bad flag: %s", 1768 1.1 dyoung lineno, cp); 1769 1.1 dyoung errors++; 1770 1.1 dyoung } 1771 1.1 dyoung } 1772 1.1 dyoung lp->d_flags = v; 1773 1.1 dyoung continue; 1774 1.1 dyoung } 1775 1.1 dyoung if (!strcmp(cp, "drivedata")) { 1776 1.1 dyoung int i; 1777 1.1 dyoung 1778 1.1 dyoung for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 1779 1.1 dyoung if (GETNUM32(cp, &v) != 0) { 1780 1.1 dyoung warnx("line %d: bad drive data", 1781 1.1 dyoung lineno); 1782 1.1 dyoung errors++; 1783 1.1 dyoung } else 1784 1.1 dyoung lp->d_drivedata[i] = v; 1785 1.1 dyoung i++; 1786 1.1 dyoung tp = word(cp); 1787 1.1 dyoung } 1788 1.1 dyoung continue; 1789 1.1 dyoung } 1790 1.1 dyoung if (sscanf(cp, "%lu partitions", &v) == 1) { 1791 1.29 matt if (v == 0 || v > maxpartitions) { 1792 1.1 dyoung warnx("line %d: bad # of partitions", lineno); 1793 1.29 matt lp->d_npartitions = maxpartitions; 1794 1.1 dyoung errors++; 1795 1.1 dyoung } else 1796 1.1 dyoung lp->d_npartitions = v; 1797 1.1 dyoung continue; 1798 1.1 dyoung } 1799 1.1 dyoung if (tp == NULL) { 1800 1.1 dyoung tbuf[0] = '\0'; 1801 1.1 dyoung tp = tbuf; 1802 1.1 dyoung } 1803 1.1 dyoung if (!strcmp(cp, "disk")) { 1804 1.1 dyoung strncpy(lp->d_typename, tp, sizeof(lp->d_typename)); 1805 1.1 dyoung continue; 1806 1.1 dyoung } 1807 1.1 dyoung if (!strcmp(cp, "label")) { 1808 1.1 dyoung strncpy(lp->d_packname, tp, sizeof(lp->d_packname)); 1809 1.1 dyoung continue; 1810 1.1 dyoung } 1811 1.1 dyoung if (!strcmp(cp, "bytes/sector")) { 1812 1.1 dyoung if (GETNUM32(tp, &v) != 0 || v <= 0 || (v % 512) != 0) { 1813 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1814 1.1 dyoung errors++; 1815 1.1 dyoung } else 1816 1.1 dyoung lp->d_secsize = v; 1817 1.1 dyoung continue; 1818 1.1 dyoung } 1819 1.1 dyoung if (!strcmp(cp, "sectors/track")) { 1820 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1821 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1822 1.1 dyoung errors++; 1823 1.1 dyoung } else 1824 1.1 dyoung lp->d_nsectors = v; 1825 1.1 dyoung continue; 1826 1.1 dyoung } 1827 1.1 dyoung if (!strcmp(cp, "sectors/cylinder")) { 1828 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1829 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1830 1.1 dyoung errors++; 1831 1.1 dyoung } else 1832 1.1 dyoung lp->d_secpercyl = v; 1833 1.1 dyoung continue; 1834 1.1 dyoung } 1835 1.1 dyoung if (!strcmp(cp, "tracks/cylinder")) { 1836 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1837 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1838 1.1 dyoung errors++; 1839 1.1 dyoung } else 1840 1.1 dyoung lp->d_ntracks = v; 1841 1.1 dyoung continue; 1842 1.1 dyoung } 1843 1.1 dyoung if (!strcmp(cp, "cylinders")) { 1844 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1845 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1846 1.1 dyoung errors++; 1847 1.1 dyoung } else 1848 1.1 dyoung lp->d_ncylinders = v; 1849 1.1 dyoung continue; 1850 1.1 dyoung } 1851 1.11 christos if (!strcmp(cp, "total sectors") || 1852 1.11 christos !strcmp(cp, "sectors/unit")) { 1853 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1854 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1855 1.1 dyoung errors++; 1856 1.1 dyoung } else 1857 1.1 dyoung lp->d_secperunit = v; 1858 1.1 dyoung continue; 1859 1.1 dyoung } 1860 1.1 dyoung if (!strcmp(cp, "rpm")) { 1861 1.1 dyoung if (GETNUM16(tp, &v) != 0) { 1862 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1863 1.1 dyoung errors++; 1864 1.1 dyoung } else 1865 1.1 dyoung lp->d_rpm = v; 1866 1.1 dyoung continue; 1867 1.1 dyoung } 1868 1.1 dyoung if (!strcmp(cp, "interleave")) { 1869 1.1 dyoung if (GETNUM16(tp, &v) != 0) { 1870 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1871 1.1 dyoung errors++; 1872 1.1 dyoung } else 1873 1.1 dyoung lp->d_interleave = v; 1874 1.1 dyoung continue; 1875 1.1 dyoung } 1876 1.1 dyoung if (!strcmp(cp, "trackskew")) { 1877 1.1 dyoung if (GETNUM16(tp, &v) != 0) { 1878 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1879 1.1 dyoung errors++; 1880 1.1 dyoung } else 1881 1.1 dyoung lp->d_trackskew = v; 1882 1.1 dyoung continue; 1883 1.1 dyoung } 1884 1.1 dyoung if (!strcmp(cp, "cylinderskew")) { 1885 1.1 dyoung if (GETNUM16(tp, &v) != 0) { 1886 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1887 1.1 dyoung errors++; 1888 1.1 dyoung } else 1889 1.1 dyoung lp->d_cylskew = v; 1890 1.1 dyoung continue; 1891 1.1 dyoung } 1892 1.1 dyoung if (!strcmp(cp, "headswitch")) { 1893 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1894 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1895 1.1 dyoung errors++; 1896 1.1 dyoung } else 1897 1.1 dyoung lp->d_headswitch = v; 1898 1.1 dyoung continue; 1899 1.1 dyoung } 1900 1.1 dyoung if (!strcmp(cp, "track-to-track seek")) { 1901 1.1 dyoung if (GETNUM32(tp, &v) != 0) { 1902 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp); 1903 1.1 dyoung errors++; 1904 1.1 dyoung } else 1905 1.1 dyoung lp->d_trkseek = v; 1906 1.1 dyoung continue; 1907 1.1 dyoung } 1908 1.1 dyoung if ('a' > *cp || *cp > 'z' || cp[1] != '\0') { 1909 1.1 dyoung warnx("line %d: unknown field: %s", lineno, cp); 1910 1.1 dyoung errors++; 1911 1.1 dyoung continue; 1912 1.1 dyoung } 1913 1.1 dyoung 1914 1.1 dyoung /* We have a partition entry */ 1915 1.1 dyoung part = *cp - 'a'; 1916 1.1 dyoung 1917 1.29 matt if (part >= maxpartitions) { 1918 1.1 dyoung warnx("line %d: bad partition name: %s", lineno, cp); 1919 1.1 dyoung errors++; 1920 1.1 dyoung continue; 1921 1.1 dyoung } 1922 1.33 christos if (part >= __arraycount(lp->d_partitions)) { 1923 1.33 christos warnx("line %d: partition id %s, >= %zu", lineno, 1924 1.33 christos cp, __arraycount(lp->d_partitions)); 1925 1.33 christos errors++; 1926 1.33 christos continue; 1927 1.33 christos } 1928 1.1 dyoung pp = &lp->d_partitions[part]; 1929 1.1 dyoung 1930 1.1 dyoung NXTXNUM(pp->p_size); 1931 1.1 dyoung NXTXNUM(pp->p_offset); 1932 1.1 dyoung /* can't use word() here because of blanks in fstypenames[] */ 1933 1.1 dyoung tp += strspn(tp, " \t"); 1934 1.1 dyoung _CHECKLINE 1935 1.1 dyoung cp = tp; 1936 1.1 dyoung cpp = fstypenames; 1937 1.1 dyoung for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) { 1938 1.1 dyoung s = *cpp; 1939 1.1 dyoung if (s == NULL || 1940 1.1 dyoung (cp[strlen(s)] != ' ' && 1941 1.1 dyoung cp[strlen(s)] != '\t' && 1942 1.1 dyoung cp[strlen(s)] != '\0')) 1943 1.1 dyoung continue; 1944 1.1 dyoung if (!memcmp(s, cp, strlen(s))) { 1945 1.1 dyoung pp->p_fstype = cpp - fstypenames; 1946 1.1 dyoung tp += strlen(s); 1947 1.1 dyoung if (*tp == '\0') 1948 1.1 dyoung tp = NULL; 1949 1.1 dyoung else { 1950 1.1 dyoung tp += strspn(tp, " \t"); 1951 1.1 dyoung if (*tp == '\0') 1952 1.1 dyoung tp = NULL; 1953 1.1 dyoung } 1954 1.1 dyoung goto gottype; 1955 1.1 dyoung } 1956 1.1 dyoung } 1957 1.1 dyoung tp = word(cp); 1958 1.1 dyoung if (isdigit(*cp & 0xff)) { 1959 1.1 dyoung if (GETNUM8(cp, &v) != 0) { 1960 1.1 dyoung warnx("line %d: syntax error", lineno); 1961 1.1 dyoung errors++; 1962 1.1 dyoung } 1963 1.1 dyoung } else 1964 1.1 dyoung v = FSMAXTYPES; 1965 1.1 dyoung if ((unsigned)v >= FSMAXTYPES) { 1966 1.13 jmmv warnx("line %d: warning, unknown file system type: %s", 1967 1.1 dyoung lineno, cp); 1968 1.13 jmmv warnx("tip: use -l to see all valid file system " 1969 1.13 jmmv "types"); 1970 1.1 dyoung v = FS_UNUSED; 1971 1.1 dyoung } 1972 1.1 dyoung pp->p_fstype = v; 1973 1.1 dyoung gottype: 1974 1.1 dyoung switch (pp->p_fstype) { 1975 1.1 dyoung 1976 1.1 dyoung case FS_UNUSED: /* XXX */ 1977 1.1 dyoung NXTNUM(pp->p_fsize); 1978 1.1 dyoung if (pp->p_fsize == 0) 1979 1.1 dyoung break; 1980 1.1 dyoung NXTNUM(v); 1981 1.1 dyoung pp->p_frag = v / pp->p_fsize; 1982 1.1 dyoung break; 1983 1.1 dyoung 1984 1.1 dyoung case FS_BSDFFS: 1985 1.1 dyoung case FS_ADOS: 1986 1.1 dyoung case FS_APPLEUFS: 1987 1.1 dyoung NXTNUM(pp->p_fsize); 1988 1.1 dyoung if (pp->p_fsize == 0) 1989 1.1 dyoung break; 1990 1.1 dyoung NXTNUM(v); 1991 1.1 dyoung pp->p_frag = v / pp->p_fsize; 1992 1.1 dyoung NXTNUM(pp->p_cpg); 1993 1.1 dyoung break; 1994 1.1 dyoung case FS_BSDLFS: 1995 1.1 dyoung NXTNUM(pp->p_fsize); 1996 1.1 dyoung if (pp->p_fsize == 0) 1997 1.1 dyoung break; 1998 1.1 dyoung NXTNUM(v); 1999 1.1 dyoung pp->p_frag = v / pp->p_fsize; 2000 1.1 dyoung NXTNUM(pp->p_sgs); 2001 1.1 dyoung break; 2002 1.1 dyoung case FS_EX2FS: 2003 1.1 dyoung NXTNUM(pp->p_fsize); 2004 1.1 dyoung if (pp->p_fsize == 0) 2005 1.1 dyoung break; 2006 1.1 dyoung NXTNUM(v); 2007 1.1 dyoung pp->p_frag = v / pp->p_fsize; 2008 1.1 dyoung break; 2009 1.1 dyoung case FS_ISO9660: 2010 1.1 dyoung NXTNUM(pp->p_cdsession); 2011 1.1 dyoung break; 2012 1.1 dyoung default: 2013 1.1 dyoung break; 2014 1.1 dyoung } 2015 1.1 dyoung continue; 2016 1.1 dyoung error: 2017 1.1 dyoung errors++; 2018 1.1 dyoung next: 2019 1.1 dyoung ; 2020 1.1 dyoung } 2021 1.1 dyoung errors += checklabel(lp); 2022 1.1 dyoung return (errors == 0); 2023 1.1 dyoung } 2024 1.1 dyoung 2025 1.1 dyoung /* 2026 1.1 dyoung * Check disklabel for errors and fill in 2027 1.1 dyoung * derived fields according to supplied values. 2028 1.1 dyoung */ 2029 1.1 dyoung int 2030 1.1 dyoung checklabel(struct disklabel *lp) 2031 1.1 dyoung { 2032 1.1 dyoung struct partition *pp, *qp; 2033 1.1 dyoung int i, j, errors; 2034 1.1 dyoung char part; 2035 1.1 dyoung 2036 1.1 dyoung errors = 0; 2037 1.1 dyoung if (lp->d_secsize == 0) { 2038 1.23 christos warnx("sector size %" PRIu32, lp->d_secsize); 2039 1.1 dyoung return (1); 2040 1.1 dyoung } 2041 1.1 dyoung if (lp->d_nsectors == 0) { 2042 1.23 christos warnx("sectors/track %" PRIu32, lp->d_nsectors); 2043 1.1 dyoung return (1); 2044 1.1 dyoung } 2045 1.1 dyoung if (lp->d_ntracks == 0) { 2046 1.23 christos warnx("tracks/cylinder %" PRIu32, lp->d_ntracks); 2047 1.1 dyoung return (1); 2048 1.1 dyoung } 2049 1.1 dyoung if (lp->d_ncylinders == 0) { 2050 1.23 christos warnx("cylinders/unit %" PRIu32, lp->d_ncylinders); 2051 1.1 dyoung errors++; 2052 1.1 dyoung } 2053 1.1 dyoung if (lp->d_rpm == 0) 2054 1.23 christos warnx("warning, revolutions/minute %" PRIu16, lp->d_rpm); 2055 1.1 dyoung if (lp->d_secpercyl == 0) 2056 1.1 dyoung lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 2057 1.1 dyoung if (lp->d_secperunit == 0) 2058 1.1 dyoung lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 2059 1.1 dyoung if (lp->d_bbsize == 0) { 2060 1.23 christos warnx("boot block size %" PRIu32, lp->d_bbsize); 2061 1.1 dyoung errors++; 2062 1.1 dyoung } else if (lp->d_bbsize % lp->d_secsize) 2063 1.1 dyoung warnx("warning, boot block size %% sector-size != 0"); 2064 1.1 dyoung if (lp->d_sbsize == 0) { 2065 1.23 christos warnx("super block size %" PRIu32, lp->d_sbsize); 2066 1.1 dyoung errors++; 2067 1.1 dyoung } else if (lp->d_sbsize % lp->d_secsize) 2068 1.1 dyoung warnx("warning, super block size %% sector-size != 0"); 2069 1.29 matt if (lp->d_npartitions > maxpartitions) 2070 1.24 apb warnx("warning, number of partitions (%" PRIu16 ") > " 2071 1.24 apb "MAXPARTITIONS (%d)", 2072 1.29 matt lp->d_npartitions, maxpartitions); 2073 1.1 dyoung else 2074 1.29 matt for (i = maxpartitions - 1; i >= lp->d_npartitions; i--) { 2075 1.1 dyoung part = 'a' + i; 2076 1.1 dyoung pp = &lp->d_partitions[i]; 2077 1.1 dyoung if (pp->p_size || pp->p_offset) { 2078 1.1 dyoung warnx("warning, partition %c increased " 2079 1.24 apb "number of partitions from %" PRIu16 2080 1.24 apb " to %d", 2081 1.1 dyoung part, lp->d_npartitions, i + 1); 2082 1.1 dyoung lp->d_npartitions = i + 1; 2083 1.1 dyoung break; 2084 1.1 dyoung } 2085 1.1 dyoung } 2086 1.1 dyoung for (i = 0; i < lp->d_npartitions; i++) { 2087 1.1 dyoung part = 'a' + i; 2088 1.1 dyoung pp = &lp->d_partitions[i]; 2089 1.1 dyoung if (pp->p_size == 0 && pp->p_offset != 0) 2090 1.24 apb warnx("warning, partition %c: size 0, but " 2091 1.24 apb "offset %" PRIu32, 2092 1.1 dyoung part, pp->p_offset); 2093 1.1 dyoung #ifdef STRICT_CYLINDER_ALIGNMENT 2094 1.1 dyoung if (pp->p_offset % lp->d_secpercyl) { 2095 1.1 dyoung warnx("warning, partition %c:" 2096 1.1 dyoung " not starting on cylinder boundary", 2097 1.1 dyoung part); 2098 1.1 dyoung errors++; 2099 1.1 dyoung } 2100 1.1 dyoung #endif /* STRICT_CYLINDER_ALIGNMENT */ 2101 1.1 dyoung if (pp->p_offset > lp->d_secperunit) { 2102 1.1 dyoung warnx("partition %c: offset past end of unit", part); 2103 1.1 dyoung errors++; 2104 1.1 dyoung } 2105 1.1 dyoung if (pp->p_offset + pp->p_size > lp->d_secperunit) { 2106 1.1 dyoung warnx("partition %c: partition extends" 2107 1.1 dyoung " past end of unit", 2108 1.1 dyoung part); 2109 1.1 dyoung errors++; 2110 1.1 dyoung } 2111 1.1 dyoung if (pp->p_fstype != FS_UNUSED) 2112 1.1 dyoung for (j = i + 1; j < lp->d_npartitions; j++) { 2113 1.1 dyoung qp = &lp->d_partitions[j]; 2114 1.1 dyoung if (qp->p_fstype == FS_UNUSED) 2115 1.1 dyoung continue; 2116 1.1 dyoung if (pp->p_offset < qp->p_offset + qp->p_size && 2117 1.1 dyoung qp->p_offset < pp->p_offset + pp->p_size) 2118 1.1 dyoung warnx("partitions %c and %c overlap", 2119 1.1 dyoung part, 'a' + j); 2120 1.1 dyoung } 2121 1.1 dyoung } 2122 1.1 dyoung return (errors); 2123 1.1 dyoung } 2124 1.1 dyoung 2125 1.1 dyoung static void 2126 1.1 dyoung usage(void) 2127 1.1 dyoung { 2128 1.1 dyoung static const struct { 2129 1.1 dyoung const char *name; 2130 1.1 dyoung const char *expn; 2131 1.1 dyoung } usages[] = { 2132 1.29 matt { "[-ABCFMrtv] disk", "(to read label)" }, 2133 1.29 matt { "-w [-BDFMrv] [-f disktab] disk disktype [packid]", "(to write label)" }, 2134 1.29 matt { "-e [-BCDFMIrv] disk", "(to edit label)" }, 2135 1.46 christos #if !defined(NO_INTERACT) 2136 1.29 matt { "-i [-BDFMIrv] disk", "(to create a label interactively)" }, 2137 1.46 christos #endif 2138 1.6 dsl { "-D [-v] disk", "(to delete existing label(s))" }, 2139 1.29 matt { "-R [-BDFMrv] disk protofile", "(to restore label)" }, 2140 1.4 dsl { "[-NW] disk", "(to write disable/enable label)" }, 2141 1.13 jmmv { "-l", "(to show all known file system types)" }, 2142 1.4 dsl { NULL, NULL } 2143 1.1 dyoung }; 2144 1.1 dyoung int i; 2145 1.4 dsl const char *pn = getprogname(); 2146 1.4 dsl const char *t = "usage:"; 2147 1.1 dyoung 2148 1.4 dsl for (i = 0; usages[i].name != NULL; i++) { 2149 1.4 dsl (void)fprintf(stderr, "%s %s %s\n\t%s\n", 2150 1.4 dsl t, pn, usages[i].name, usages[i].expn); 2151 1.4 dsl t = "or"; 2152 1.1 dyoung } 2153 1.1 dyoung exit(1); 2154 1.1 dyoung } 2155 1.1 dyoung 2156 1.1 dyoung static int 2157 1.1 dyoung getulong(const char *str, char sep, char **epp, unsigned long *ul, 2158 1.1 dyoung unsigned long max) 2159 1.1 dyoung { 2160 1.1 dyoung char *ep; 2161 1.1 dyoung 2162 1.1 dyoung if (epp == NULL) 2163 1.1 dyoung epp = &ep; 2164 1.1 dyoung 2165 1.1 dyoung *ul = strtoul(str, epp, 10); 2166 1.1 dyoung 2167 1.1 dyoung if ((*ul == ULONG_MAX && errno == ERANGE) || *ul > max) 2168 1.1 dyoung return ERANGE; 2169 1.1 dyoung 2170 1.1 dyoung if (*str == '\0' || (**epp != '\0' && **epp != sep && 2171 1.1 dyoung !isspace((unsigned char)**epp))) 2172 1.1 dyoung return EFTYPE; 2173 1.1 dyoung 2174 1.1 dyoung return 0; 2175 1.1 dyoung } 2176 1.13 jmmv 2177 1.13 jmmv /* 2178 1.13 jmmv * This is a wrapper over the standard strcmp function to be used with 2179 1.13 jmmv * qsort on an array of pointers to strings. 2180 1.13 jmmv */ 2181 1.13 jmmv static int 2182 1.13 jmmv qsort_strcmp(const void *v1, const void *v2) 2183 1.13 jmmv { 2184 1.13 jmmv const char *const *sp1 = (const char *const *)v1; 2185 1.13 jmmv const char *const *sp2 = (const char *const *)v2; 2186 1.13 jmmv 2187 1.13 jmmv return strcmp(*sp1, *sp2); 2188 1.13 jmmv } 2189 1.13 jmmv 2190 1.13 jmmv /* 2191 1.13 jmmv * Prints all know file system types for a partition. 2192 1.13 jmmv * Returns 1 on success, 0 on failure. 2193 1.13 jmmv */ 2194 1.13 jmmv int 2195 1.13 jmmv list_fs_types(void) 2196 1.13 jmmv { 2197 1.13 jmmv int ret; 2198 1.13 jmmv size_t nelems; 2199 1.13 jmmv 2200 1.13 jmmv nelems = 0; 2201 1.13 jmmv { 2202 1.13 jmmv const char *const *namep; 2203 1.13 jmmv 2204 1.13 jmmv namep = fstypenames; 2205 1.13 jmmv while (*namep++ != NULL) 2206 1.13 jmmv nelems++; 2207 1.13 jmmv } 2208 1.13 jmmv 2209 1.13 jmmv ret = 1; 2210 1.13 jmmv if (nelems > 0) { 2211 1.57 nia const char **list = NULL; 2212 1.13 jmmv size_t i; 2213 1.13 jmmv 2214 1.57 nia if (reallocarr(&list, nelems, sizeof(char *)) != 0) { 2215 1.13 jmmv warnx("sorry, could not allocate memory for list"); 2216 1.13 jmmv ret = 0; 2217 1.13 jmmv } else { 2218 1.13 jmmv for (i = 0; i < nelems; i++) 2219 1.13 jmmv list[i] = fstypenames[i]; 2220 1.13 jmmv 2221 1.13 jmmv qsort(list, nelems, sizeof(char *), qsort_strcmp); 2222 1.13 jmmv 2223 1.13 jmmv for (i = 0; i < nelems; i++) 2224 1.13 jmmv (void)printf("%s\n", list[i]); 2225 1.13 jmmv 2226 1.13 jmmv free(list); 2227 1.13 jmmv } 2228 1.13 jmmv } 2229 1.13 jmmv 2230 1.13 jmmv return ret; 2231 1.13 jmmv } 2232 1.29 matt 2233 1.29 matt #ifndef HAVE_NBTOOL_CONFIG_H 2234 1.29 matt int 2235 1.29 matt dk_ioctl(int f, u_long cmd, void *arg) 2236 1.29 matt { 2237 1.46 christos #if !defined(NATIVELABEL_ONLY) 2238 1.29 matt if (!native_p) { 2239 1.29 matt errno = ENOTTY; 2240 1.29 matt return -1; 2241 1.29 matt } 2242 1.46 christos #endif 2243 1.29 matt return ioctl(f, cmd, arg); 2244 1.29 matt } 2245 1.29 matt #endif 2246