Home | History | Annotate | Line # | Download | only in mbrlabel
mbrlabel.c revision 1.17
      1  1.17      wiz /*	$NetBSD: mbrlabel.c,v 1.17 2001/07/26 22:47:35 wiz Exp $	*/
      2   1.1       ws 
      3   1.1       ws /*
      4   1.1       ws  * Copyright (C) 1998 Wolfgang Solfrank.
      5   1.1       ws  * Copyright (C) 1998 TooLs GmbH.
      6   1.1       ws  * All rights reserved.
      7   1.1       ws  *
      8   1.1       ws  * Redistribution and use in source and binary forms, with or without
      9   1.1       ws  * modification, are permitted provided that the following conditions
     10   1.1       ws  * are met:
     11   1.1       ws  * 1. Redistributions of source code must retain the above copyright
     12   1.1       ws  *    notice, this list of conditions and the following disclaimer.
     13   1.1       ws  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       ws  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       ws  *    documentation and/or other materials provided with the distribution.
     16   1.1       ws  * 3. All advertising materials mentioning features or use of this software
     17   1.1       ws  *    must display the following acknowledgement:
     18   1.1       ws  *	This product includes software developed by TooLs GmbH.
     19   1.1       ws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20   1.1       ws  *    derived from this software without specific prior written permission.
     21   1.1       ws  *
     22   1.1       ws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23   1.1       ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       ws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26   1.1       ws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27   1.1       ws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28   1.1       ws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29   1.1       ws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30   1.1       ws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31   1.1       ws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       ws  */
     33   1.1       ws 
     34   1.1       ws #include <sys/cdefs.h>
     35   1.1       ws #ifndef lint
     36  1.17      wiz __RCSID("$NetBSD: mbrlabel.c,v 1.17 2001/07/26 22:47:35 wiz Exp $");
     37   1.1       ws #endif /* not lint */
     38   1.1       ws 
     39   1.1       ws #include <stdio.h>
     40   1.1       ws #include <fcntl.h>
     41   1.8     matt #include <stdlib.h>
     42   1.1       ws #include <string.h>
     43   1.1       ws #include <unistd.h>
     44   1.1       ws #include <util.h>
     45   1.1       ws 
     46   1.1       ws #include <sys/param.h>
     47  1.11    lukem #define FSTYPENAMES
     48   1.1       ws #include <sys/disklabel.h>
     49   1.3  thorpej #include <sys/disklabel_mbr.h>
     50   1.1       ws #include <sys/ioctl.h>
     51   1.1       ws 
     52   1.1       ws #include "dkcksum.h"
     53  1.11    lukem #include "extern.h"
     54   1.1       ws 
     55  1.11    lukem int	main(int, char **);
     56  1.11    lukem void	usage(void);
     57  1.11    lukem void	getlabel(int);
     58  1.12    lukem void	setlabel(int, int);
     59  1.15    lukem int	getparts(int, u_int32_t, u_int32_t, int);
     60  1.11    lukem int	nbsdtype(int);
     61  1.15    lukem u_int16_t	getshort(void *);
     62  1.15    lukem u_int32_t	getlong(void *);
     63   1.1       ws 
     64   1.1       ws struct disklabel label;
     65   1.1       ws 
     66   1.1       ws void
     67   1.9      wiz getlabel(int sd)
     68   1.1       ws {
     69   1.1       ws 
     70   1.1       ws 	if (ioctl(sd, DIOCGDINFO, &label) < 0) {
     71   1.1       ws 		perror("get label");
     72   1.1       ws 		exit(1);
     73   1.1       ws 	}
     74   1.1       ws 	/*
     75   1.1       ws 	 * Some ports seem to not set the number of partitions
     76  1.17      wiz 	 * correctly, albeit they seem to set the raw partition ok!
     77   1.1       ws 	 */
     78   1.1       ws 	if (label.d_npartitions <= RAW_PART)
     79   1.1       ws 		label.d_npartitions = RAW_PART + 1;
     80   1.1       ws }
     81   1.1       ws 
     82   1.1       ws void
     83  1.12    lukem setlabel(int sd, int doraw)
     84   1.1       ws {
     85  1.11    lukem 
     86   1.1       ws 	label.d_checksum = 0;
     87   1.1       ws 	label.d_checksum = dkcksum(&label);
     88  1.12    lukem 	if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
     89   1.1       ws 		perror("set label");
     90   1.1       ws 		exit(1);
     91   1.1       ws 	}
     92   1.1       ws }
     93   1.1       ws 
     94   1.1       ws static struct typetab {
     95   1.1       ws 	int mbrtype;
     96   1.1       ws 	int nbsdtype;
     97   1.1       ws } typetable[] = {
     98  1.11    lukem 	{ MBR_PTYPE_386BSD,	FS_BSDFFS },
     99  1.11    lukem 	{ MBR_PTYPE_FAT12,	FS_MSDOS },
    100  1.11    lukem 	{ MBR_PTYPE_FAT16B,	FS_MSDOS },
    101  1.11    lukem 	{ MBR_PTYPE_FAT16L,	FS_MSDOS },
    102  1.11    lukem 	{ MBR_PTYPE_FAT16S,	FS_MSDOS },
    103  1.11    lukem 	{ MBR_PTYPE_FAT32,	FS_MSDOS },
    104  1.11    lukem 	{ MBR_PTYPE_FAT32L,	FS_MSDOS },
    105  1.11    lukem 	{ MBR_PTYPE_LNXEXT2,	FS_EX2FS },
    106  1.11    lukem 	{ MBR_PTYPE_LNXSWAP,	FS_SWAP },
    107  1.11    lukem 	{ MBR_PTYPE_NETBSD,	FS_BSDFFS },
    108  1.11    lukem 	{ MBR_PTYPE_NTFS,	FS_NTFS },
    109   1.1       ws 	{ 0, 0 }
    110   1.1       ws };
    111   1.1       ws 
    112   1.1       ws int
    113   1.9      wiz nbsdtype(int type)
    114   1.1       ws {
    115   1.1       ws 	struct typetab *tt;
    116   1.1       ws 
    117   1.1       ws 	for (tt = typetable; tt->mbrtype; tt++)
    118   1.1       ws 		if (tt->mbrtype == type)
    119  1.11    lukem 			return (tt->nbsdtype);
    120  1.11    lukem 	return (FS_OTHER);
    121   1.1       ws }
    122   1.1       ws 
    123  1.15    lukem u_int16_t
    124  1.15    lukem getshort(void *p)
    125  1.15    lukem {
    126  1.15    lukem 	unsigned char *cp = p;
    127  1.15    lukem 
    128  1.15    lukem 	return (cp[0] | (cp[1] << 8));
    129  1.15    lukem }
    130  1.15    lukem 
    131   1.1       ws u_int32_t
    132   1.9      wiz getlong(void *p)
    133   1.1       ws {
    134   1.1       ws 	unsigned char *cp = p;
    135   1.1       ws 
    136  1.11    lukem 	return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
    137   1.1       ws }
    138   1.1       ws 
    139   1.1       ws int
    140  1.15    lukem getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
    141   1.1       ws {
    142  1.11    lukem 	unsigned char		buf[DEV_BSIZE];
    143  1.11    lukem 	struct mbr_partition	parts[NMBRPART];
    144  1.11    lukem 	struct partition	npe;
    145  1.11    lukem 	off_t			loff;
    146  1.11    lukem 	int			i, j, unused, changed;
    147   1.2     fair 
    148  1.11    lukem 	changed = 0;
    149   1.2     fair 	loff = (off_t)off * DEV_BSIZE;
    150   1.1       ws 
    151   1.1       ws 	if (lseek(sd, loff, SEEK_SET) != loff) {
    152   1.1       ws 		perror("seek label");
    153   1.1       ws 		exit(1);
    154   1.1       ws 	}
    155   1.1       ws 	if (read(sd, buf, DEV_BSIZE) != DEV_BSIZE) {
    156   1.1       ws 		perror("read label");
    157   1.1       ws 		exit(1);
    158   1.1       ws 	}
    159  1.15    lukem 	if (getshort(buf + MBR_MAGICOFF) != MBR_MAGIC)
    160  1.11    lukem 		return (changed);
    161   1.5      cgd 	memcpy(parts, buf + MBR_PARTOFF, sizeof parts);
    162  1.11    lukem 
    163  1.11    lukem 				/* scan partition table */
    164   1.5      cgd 	for (i = 0; i < NMBRPART; i++) {
    165  1.11    lukem 		if (parts[i].mbrp_typ == 0 ||
    166  1.11    lukem 				/* extended partitions are handled below */
    167  1.11    lukem 		    MBR_IS_EXTENDED(parts[i].mbrp_typ))
    168  1.11    lukem 			continue;
    169  1.11    lukem 
    170  1.11    lukem 		memset((void *)&npe, 0, sizeof(npe));
    171  1.11    lukem 		npe.p_size = getlong(&parts[i].mbrp_size);
    172  1.11    lukem 		npe.p_offset = getlong(&parts[i].mbrp_start) + off;
    173  1.11    lukem 		npe.p_fstype = nbsdtype(parts[i].mbrp_typ);
    174  1.11    lukem 
    175  1.11    lukem 				/* find existing entry, or first free slot */
    176  1.11    lukem 		unused = -1;	/* flag as no free slot */
    177  1.11    lukem 		if (verbose)
    178  1.13    lukem 			printf(
    179  1.13    lukem 			    "Found %s partition; size %u (%u MB), offset %u\n",
    180  1.13    lukem 			    fstypenames[npe.p_fstype],
    181  1.13    lukem 			    npe.p_size, npe.p_size / 2048, npe.p_offset);
    182  1.11    lukem 		for (j = 0; j < label.d_npartitions; j++) {
    183  1.11    lukem 			struct partition *lpe;
    184  1.11    lukem 
    185  1.11    lukem 			if (j == RAW_PART)
    186  1.11    lukem 				continue;
    187  1.11    lukem 			lpe = &label.d_partitions[j];
    188  1.11    lukem 			if (lpe->p_size == npe.p_size &&
    189  1.11    lukem 			    lpe->p_offset == npe.p_offset
    190  1.11    lukem #ifdef notyet
    191  1.11    lukem 			    && (lpe->p_fstype == npe.p_fstype ||
    192  1.11    lukem 			     lpe->p_fstype == FS_UNUSED) */
    193  1.11    lukem #endif
    194  1.11    lukem 			     ) {
    195  1.11    lukem 				if (verbose)
    196  1.11    lukem 					printf(
    197  1.11    lukem 			    "  skipping existing %s partition at slot %c.\n",
    198  1.11    lukem 					    fstypenames[lpe->p_fstype],
    199  1.11    lukem 					    j + 'a');
    200  1.11    lukem 				unused = -2;	/* flag as existing */
    201  1.11    lukem 				break;
    202  1.11    lukem 			}
    203  1.11    lukem 			if (unused == -1 && lpe->p_size == 0 &&
    204  1.11    lukem 			    lpe->p_fstype == FS_UNUSED)
    205  1.11    lukem 				unused = j;
    206  1.11    lukem 		}
    207  1.11    lukem 		if (unused == -2)
    208  1.11    lukem 			continue;	/* entry exists, skip... */
    209  1.11    lukem 		if (unused == -1) {
    210  1.11    lukem 			if (label.d_npartitions < MAXPARTITIONS) {
    211  1.11    lukem 				unused = label.d_npartitions;
    212  1.11    lukem 				label.d_npartitions++;
    213  1.11    lukem 			} else {
    214  1.11    lukem 				printf(
    215  1.11    lukem 				"  WARNING: no slots free for %s partition.\n",
    216  1.11    lukem 				    fstypenames[npe.p_fstype]);
    217  1.11    lukem 				continue;
    218  1.11    lukem 			}
    219  1.11    lukem 		}
    220  1.11    lukem 
    221  1.11    lukem 		if (verbose)
    222  1.11    lukem 			printf("  adding %s partition to slot %c.\n",
    223  1.11    lukem 			    fstypenames[npe.p_fstype], unused + 'a');
    224  1.11    lukem 		switch (npe.p_fstype) {
    225  1.11    lukem 		case FS_BSDFFS:
    226  1.11    lukem 			npe.p_size = 16384;	/* XXX */
    227  1.11    lukem 			npe.p_fsize = 1024;
    228  1.11    lukem 			npe.p_frag = 8;
    229  1.11    lukem 			npe.p_cpg = 16;
    230   1.1       ws 			break;
    231  1.11    lukem #ifdef	__does_not_happen__
    232  1.11    lukem 		case FS_BSDLFS:
    233  1.11    lukem 			npe.p_size = 16384;	/* XXX */
    234  1.11    lukem 			npe.p_fsize = 1024;
    235  1.11    lukem 			npe.p_frag = 8;
    236  1.11    lukem 			npe.p_sgs = XXX;
    237   1.1       ws 			break;
    238   1.1       ws #endif
    239   1.1       ws 		}
    240  1.11    lukem 		changed++;
    241  1.11    lukem 		label.d_partitions[unused] = npe;
    242   1.1       ws 	}
    243  1.11    lukem 
    244  1.11    lukem 				/* recursively scan extended partitions */
    245   1.5      cgd 	for (i = 0; i < NMBRPART; i++) {
    246   1.6       ws 		u_int32_t poff;
    247   1.6       ws 
    248  1.11    lukem 		if (MBR_IS_EXTENDED(parts[i].mbrp_typ)) {
    249  1.11    lukem 			poff = getlong(&parts[i].mbrp_start) + extoff;
    250  1.15    lukem 			changed += getparts(sd, poff,
    251  1.11    lukem 			    extoff ? extoff : poff, verbose);
    252   1.1       ws 		}
    253   1.1       ws 	}
    254  1.11    lukem 	return (changed);
    255   1.1       ws }
    256   1.1       ws 
    257   1.1       ws void
    258   1.9      wiz usage(void)
    259   1.1       ws {
    260  1.16      cgd 	fprintf(stderr, "Usage: %s [-fqrw] rawdisk\n", getprogname());
    261   1.1       ws 	exit(1);
    262   1.1       ws }
    263   1.1       ws 
    264  1.11    lukem 
    265   1.1       ws int
    266   1.9      wiz main(int argc, char **argv)
    267   1.1       ws {
    268  1.11    lukem 	int	sd, ch, changed;
    269  1.11    lukem 	char	name[MAXPATHLEN];
    270  1.11    lukem 	int	force;			/* force label update */
    271  1.12    lukem 	int	raw;			/* update on-disk label as well */
    272  1.11    lukem 	int	verbose;		/* verbose output */
    273  1.15    lukem 	int	write_it;		/* update in-core label if changed */
    274  1.11    lukem 
    275  1.11    lukem 	force = 0;
    276  1.12    lukem 	raw = 0;
    277  1.11    lukem 	verbose = 1;
    278  1.15    lukem 	write_it = 0;
    279  1.12    lukem 	while ((ch = getopt(argc, argv, "fqrw")) != -1) {
    280  1.11    lukem 		switch (ch) {
    281  1.11    lukem 		case 'f':
    282  1.11    lukem 			force = 1;
    283  1.11    lukem 			break;
    284  1.11    lukem 		case 'q':
    285  1.11    lukem 			verbose = 0;
    286  1.11    lukem 			break;
    287  1.12    lukem 		case 'r':
    288  1.12    lukem 			raw = 1;
    289  1.12    lukem 			break;
    290  1.12    lukem 		case 'w':
    291  1.15    lukem 			write_it = 1;
    292  1.12    lukem 			break;
    293  1.11    lukem 		default:
    294  1.11    lukem 			usage();
    295  1.11    lukem 		}
    296  1.11    lukem 	}
    297  1.11    lukem 	argc -= optind;
    298  1.11    lukem 	argv += optind;
    299  1.11    lukem 	if (argc != 1)
    300   1.1       ws 		usage();
    301   1.1       ws 
    302  1.11    lukem 	if ((sd = opendisk(argv[0], O_RDWR, name, MAXPATHLEN, 0)) < 0) {
    303  1.11    lukem 		perror(argv[0]);
    304   1.1       ws 		exit(1);
    305   1.1       ws 	}
    306   1.1       ws 	getlabel(sd);
    307  1.15    lukem 	changed = getparts(sd, MBR_BBSECTOR, 0, verbose);
    308  1.11    lukem 
    309  1.11    lukem 	if (verbose) {
    310  1.11    lukem 		putchar('\n');
    311  1.11    lukem 		showpartitions(stdout, &label, 0);
    312  1.11    lukem 		putchar('\n');
    313  1.11    lukem 	}
    314  1.15    lukem 	if (write_it) {
    315  1.12    lukem 		if (! changed && ! force)
    316  1.11    lukem 			printf("No change; not updating disk label.\n");
    317  1.11    lukem 		else {
    318  1.11    lukem 			if (verbose)
    319  1.12    lukem 				printf("Updating in-core %sdisk label.\n",
    320  1.12    lukem 				    raw ? "and on-disk " : "");
    321  1.12    lukem 			setlabel(sd, raw);
    322  1.11    lukem 		}
    323  1.11    lukem 	} else {
    324  1.11    lukem 		printf("Not updating disk label.\n");
    325  1.11    lukem 	}
    326   1.1       ws 	close(sd);
    327  1.11    lukem 	return (0);
    328   1.1       ws }
    329