Home | History | Annotate | Line # | Download | only in mbrlabel
mbrlabel.c revision 1.23
      1  1.23  lukem /*	$NetBSD: mbrlabel.c,v 1.23 2003/10/08 04:25:44 lukem 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.23  lukem __RCSID("$NetBSD: mbrlabel.c,v 1.23 2003/10/08 04:25:44 lukem 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.23  lukem #include <sys/bootblock.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.15  lukem u_int16_t	getshort(void *);
     61  1.15  lukem u_int32_t	getlong(void *);
     62   1.1     ws 
     63   1.1     ws struct disklabel label;
     64   1.1     ws 
     65   1.1     ws void
     66   1.9    wiz getlabel(int sd)
     67   1.1     ws {
     68   1.1     ws 
     69   1.1     ws 	if (ioctl(sd, DIOCGDINFO, &label) < 0) {
     70   1.1     ws 		perror("get label");
     71   1.1     ws 		exit(1);
     72   1.1     ws 	}
     73   1.1     ws 	/*
     74   1.1     ws 	 * Some ports seem to not set the number of partitions
     75  1.17    wiz 	 * correctly, albeit they seem to set the raw partition ok!
     76   1.1     ws 	 */
     77  1.18     ad 	if (label.d_npartitions <= getrawpartition())
     78  1.18     ad 		label.d_npartitions = getrawpartition() + 1;
     79   1.1     ws }
     80   1.1     ws 
     81   1.1     ws void
     82  1.12  lukem setlabel(int sd, int doraw)
     83   1.1     ws {
     84  1.22    dsl 	int one = 1;
     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.22    dsl 	if (!doraw)
     93  1.22    dsl 		/* If we haven't written to the disk, don't discard on close */
     94  1.22    dsl 		ioctl(sd, DIOCKLABEL, &one);
     95  1.22    dsl 
     96   1.1     ws }
     97   1.1     ws 
     98  1.15  lukem u_int16_t
     99  1.15  lukem getshort(void *p)
    100  1.15  lukem {
    101  1.15  lukem 	unsigned char *cp = p;
    102  1.15  lukem 
    103  1.15  lukem 	return (cp[0] | (cp[1] << 8));
    104  1.15  lukem }
    105  1.15  lukem 
    106   1.1     ws u_int32_t
    107   1.9    wiz getlong(void *p)
    108   1.1     ws {
    109   1.1     ws 	unsigned char *cp = p;
    110   1.1     ws 
    111  1.11  lukem 	return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
    112   1.1     ws }
    113   1.1     ws 
    114   1.1     ws int
    115  1.15  lukem getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
    116   1.1     ws {
    117  1.11  lukem 	unsigned char		buf[DEV_BSIZE];
    118  1.23  lukem 	struct mbr_partition	parts[MBR_PART_COUNT];
    119  1.11  lukem 	struct partition	npe;
    120  1.11  lukem 	off_t			loff;
    121  1.11  lukem 	int			i, j, unused, changed;
    122   1.2   fair 
    123  1.11  lukem 	changed = 0;
    124   1.2   fair 	loff = (off_t)off * DEV_BSIZE;
    125   1.1     ws 
    126   1.1     ws 	if (lseek(sd, loff, SEEK_SET) != loff) {
    127   1.1     ws 		perror("seek label");
    128   1.1     ws 		exit(1);
    129   1.1     ws 	}
    130  1.19   ross 	if (read(sd, buf, sizeof buf) != DEV_BSIZE) {
    131   1.1     ws 		perror("read label");
    132   1.1     ws 		exit(1);
    133   1.1     ws 	}
    134  1.23  lukem 	if (getshort(buf + MBR_MAGIC_OFFSET) != MBR_MAGIC)
    135  1.11  lukem 		return (changed);
    136  1.23  lukem 	memcpy(parts, buf + MBR_PART_OFFSET, sizeof parts);
    137  1.11  lukem 
    138  1.11  lukem 				/* scan partition table */
    139  1.23  lukem 	for (i = 0; i < MBR_PART_COUNT; i++) {
    140  1.23  lukem 		if (parts[i].mbrp_type == 0 ||
    141  1.11  lukem 				/* extended partitions are handled below */
    142  1.23  lukem 		    MBR_IS_EXTENDED(parts[i].mbrp_type))
    143  1.11  lukem 			continue;
    144  1.11  lukem 
    145  1.11  lukem 		memset((void *)&npe, 0, sizeof(npe));
    146  1.11  lukem 		npe.p_size = getlong(&parts[i].mbrp_size);
    147  1.11  lukem 		npe.p_offset = getlong(&parts[i].mbrp_start) + off;
    148  1.23  lukem 		npe.p_fstype = xlat_mbr_fstype(parts[i].mbrp_type);
    149  1.11  lukem 
    150  1.11  lukem 				/* find existing entry, or first free slot */
    151  1.11  lukem 		unused = -1;	/* flag as no free slot */
    152  1.11  lukem 		if (verbose)
    153  1.13  lukem 			printf(
    154  1.13  lukem 			    "Found %s partition; size %u (%u MB), offset %u\n",
    155  1.13  lukem 			    fstypenames[npe.p_fstype],
    156  1.13  lukem 			    npe.p_size, npe.p_size / 2048, npe.p_offset);
    157  1.11  lukem 		for (j = 0; j < label.d_npartitions; j++) {
    158  1.11  lukem 			struct partition *lpe;
    159  1.11  lukem 
    160  1.11  lukem 			if (j == RAW_PART)
    161  1.11  lukem 				continue;
    162  1.11  lukem 			lpe = &label.d_partitions[j];
    163  1.11  lukem 			if (lpe->p_size == npe.p_size &&
    164  1.11  lukem 			    lpe->p_offset == npe.p_offset
    165  1.11  lukem #ifdef notyet
    166  1.11  lukem 			    && (lpe->p_fstype == npe.p_fstype ||
    167  1.11  lukem 			     lpe->p_fstype == FS_UNUSED) */
    168  1.11  lukem #endif
    169  1.11  lukem 			     ) {
    170  1.11  lukem 				if (verbose)
    171  1.11  lukem 					printf(
    172  1.11  lukem 			    "  skipping existing %s partition at slot %c.\n",
    173  1.11  lukem 					    fstypenames[lpe->p_fstype],
    174  1.11  lukem 					    j + 'a');
    175  1.11  lukem 				unused = -2;	/* flag as existing */
    176  1.11  lukem 				break;
    177  1.11  lukem 			}
    178  1.11  lukem 			if (unused == -1 && lpe->p_size == 0 &&
    179  1.11  lukem 			    lpe->p_fstype == FS_UNUSED)
    180  1.11  lukem 				unused = j;
    181  1.11  lukem 		}
    182  1.11  lukem 		if (unused == -2)
    183  1.11  lukem 			continue;	/* entry exists, skip... */
    184  1.11  lukem 		if (unused == -1) {
    185  1.11  lukem 			if (label.d_npartitions < MAXPARTITIONS) {
    186  1.11  lukem 				unused = label.d_npartitions;
    187  1.11  lukem 				label.d_npartitions++;
    188  1.11  lukem 			} else {
    189  1.11  lukem 				printf(
    190  1.11  lukem 				"  WARNING: no slots free for %s partition.\n",
    191  1.11  lukem 				    fstypenames[npe.p_fstype]);
    192  1.11  lukem 				continue;
    193  1.11  lukem 			}
    194  1.11  lukem 		}
    195  1.11  lukem 
    196  1.11  lukem 		if (verbose)
    197  1.11  lukem 			printf("  adding %s partition to slot %c.\n",
    198  1.11  lukem 			    fstypenames[npe.p_fstype], unused + 'a');
    199  1.11  lukem 		switch (npe.p_fstype) {
    200  1.11  lukem 		case FS_BSDFFS:
    201  1.21    dbj 		case FS_APPLEUFS:
    202  1.11  lukem 			npe.p_size = 16384;	/* XXX */
    203  1.11  lukem 			npe.p_fsize = 1024;
    204  1.11  lukem 			npe.p_frag = 8;
    205  1.11  lukem 			npe.p_cpg = 16;
    206   1.1     ws 			break;
    207  1.11  lukem #ifdef	__does_not_happen__
    208  1.11  lukem 		case FS_BSDLFS:
    209  1.11  lukem 			npe.p_size = 16384;	/* XXX */
    210  1.11  lukem 			npe.p_fsize = 1024;
    211  1.11  lukem 			npe.p_frag = 8;
    212  1.11  lukem 			npe.p_sgs = XXX;
    213   1.1     ws 			break;
    214   1.1     ws #endif
    215   1.1     ws 		}
    216  1.11  lukem 		changed++;
    217  1.11  lukem 		label.d_partitions[unused] = npe;
    218   1.1     ws 	}
    219  1.11  lukem 
    220  1.11  lukem 				/* recursively scan extended partitions */
    221  1.23  lukem 	for (i = 0; i < MBR_PART_COUNT; i++) {
    222   1.6     ws 		u_int32_t poff;
    223   1.6     ws 
    224  1.23  lukem 		if (MBR_IS_EXTENDED(parts[i].mbrp_type)) {
    225  1.11  lukem 			poff = getlong(&parts[i].mbrp_start) + extoff;
    226  1.15  lukem 			changed += getparts(sd, poff,
    227  1.11  lukem 			    extoff ? extoff : poff, verbose);
    228   1.1     ws 		}
    229   1.1     ws 	}
    230  1.11  lukem 	return (changed);
    231   1.1     ws }
    232   1.1     ws 
    233   1.1     ws void
    234   1.9    wiz usage(void)
    235   1.1     ws {
    236  1.16    cgd 	fprintf(stderr, "Usage: %s [-fqrw] rawdisk\n", getprogname());
    237   1.1     ws 	exit(1);
    238   1.1     ws }
    239   1.1     ws 
    240  1.11  lukem 
    241   1.1     ws int
    242   1.9    wiz main(int argc, char **argv)
    243   1.1     ws {
    244  1.11  lukem 	int	sd, ch, changed;
    245  1.11  lukem 	char	name[MAXPATHLEN];
    246  1.11  lukem 	int	force;			/* force label update */
    247  1.12  lukem 	int	raw;			/* update on-disk label as well */
    248  1.11  lukem 	int	verbose;		/* verbose output */
    249  1.15  lukem 	int	write_it;		/* update in-core label if changed */
    250  1.11  lukem 
    251  1.11  lukem 	force = 0;
    252  1.12  lukem 	raw = 0;
    253  1.11  lukem 	verbose = 1;
    254  1.15  lukem 	write_it = 0;
    255  1.12  lukem 	while ((ch = getopt(argc, argv, "fqrw")) != -1) {
    256  1.11  lukem 		switch (ch) {
    257  1.11  lukem 		case 'f':
    258  1.11  lukem 			force = 1;
    259  1.11  lukem 			break;
    260  1.11  lukem 		case 'q':
    261  1.11  lukem 			verbose = 0;
    262  1.11  lukem 			break;
    263  1.12  lukem 		case 'r':
    264  1.12  lukem 			raw = 1;
    265  1.12  lukem 			break;
    266  1.12  lukem 		case 'w':
    267  1.15  lukem 			write_it = 1;
    268  1.12  lukem 			break;
    269  1.11  lukem 		default:
    270  1.11  lukem 			usage();
    271  1.11  lukem 		}
    272  1.11  lukem 	}
    273  1.11  lukem 	argc -= optind;
    274  1.11  lukem 	argv += optind;
    275  1.11  lukem 	if (argc != 1)
    276   1.1     ws 		usage();
    277   1.1     ws 
    278  1.20   yamt 	if ((sd = opendisk(argv[0], write_it ? O_RDWR : O_RDONLY, name,
    279  1.20   yamt 	    (size_t)MAXPATHLEN, 0)) < 0) {
    280  1.11  lukem 		perror(argv[0]);
    281   1.1     ws 		exit(1);
    282   1.1     ws 	}
    283   1.1     ws 	getlabel(sd);
    284  1.15  lukem 	changed = getparts(sd, MBR_BBSECTOR, 0, verbose);
    285  1.11  lukem 
    286  1.11  lukem 	if (verbose) {
    287  1.11  lukem 		putchar('\n');
    288  1.11  lukem 		showpartitions(stdout, &label, 0);
    289  1.11  lukem 		putchar('\n');
    290  1.11  lukem 	}
    291  1.15  lukem 	if (write_it) {
    292  1.12  lukem 		if (! changed && ! force)
    293  1.11  lukem 			printf("No change; not updating disk label.\n");
    294  1.11  lukem 		else {
    295  1.11  lukem 			if (verbose)
    296  1.12  lukem 				printf("Updating in-core %sdisk label.\n",
    297  1.12  lukem 				    raw ? "and on-disk " : "");
    298  1.12  lukem 			setlabel(sd, raw);
    299  1.11  lukem 		}
    300  1.11  lukem 	} else {
    301  1.11  lukem 		printf("Not updating disk label.\n");
    302  1.11  lukem 	}
    303   1.1     ws 	close(sd);
    304  1.11  lukem 	return (0);
    305   1.1     ws }
    306