Home | History | Annotate | Line # | Download | only in mbrlabel
mbrlabel.c revision 1.28
      1  1.28       wiz /*	$NetBSD: mbrlabel.c,v 1.28 2012/07/14 20:14:17 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.28       wiz __RCSID("$NetBSD: mbrlabel.c,v 1.28 2012/07/14 20:14:17 wiz Exp $");
     37   1.1        ws #endif /* not lint */
     38   1.1        ws 
     39   1.1        ws #include <stdio.h>
     40  1.25      jmmv #include <err.h>
     41  1.25      jmmv #include <errno.h>
     42   1.1        ws #include <fcntl.h>
     43  1.25      jmmv #include <limits.h>
     44   1.8      matt #include <stdlib.h>
     45   1.1        ws #include <string.h>
     46   1.1        ws #include <unistd.h>
     47   1.1        ws #include <util.h>
     48   1.1        ws 
     49   1.1        ws #include <sys/param.h>
     50  1.11     lukem #define FSTYPENAMES
     51   1.1        ws #include <sys/disklabel.h>
     52  1.23     lukem #include <sys/bootblock.h>
     53   1.1        ws #include <sys/ioctl.h>
     54   1.1        ws 
     55   1.1        ws #include "dkcksum.h"
     56  1.11     lukem #include "extern.h"
     57   1.1        ws 
     58  1.27     joerg __dead static void	usage(void);
     59  1.27     joerg static void	getlabel(int);
     60  1.27     joerg static void	setlabel(int, int);
     61  1.27     joerg static int	getparts(int, u_int32_t, u_int32_t, int);
     62  1.27     joerg static u_int16_t	getshort(void *);
     63  1.27     joerg static u_int32_t	getlong(void *);
     64   1.1        ws 
     65   1.1        ws struct disklabel label;
     66   1.1        ws 
     67  1.27     joerg static void
     68   1.9       wiz getlabel(int sd)
     69   1.1        ws {
     70   1.1        ws 
     71   1.1        ws 	if (ioctl(sd, DIOCGDINFO, &label) < 0) {
     72   1.1        ws 		perror("get label");
     73   1.1        ws 		exit(1);
     74   1.1        ws 	}
     75   1.1        ws 	/*
     76   1.1        ws 	 * Some ports seem to not set the number of partitions
     77  1.17       wiz 	 * correctly, albeit they seem to set the raw partition ok!
     78   1.1        ws 	 */
     79  1.18        ad 	if (label.d_npartitions <= getrawpartition())
     80  1.18        ad 		label.d_npartitions = getrawpartition() + 1;
     81   1.1        ws }
     82   1.1        ws 
     83  1.27     joerg static void
     84  1.12     lukem setlabel(int sd, int doraw)
     85   1.1        ws {
     86  1.22       dsl 	int one = 1;
     87  1.11     lukem 
     88   1.1        ws 	label.d_checksum = 0;
     89   1.1        ws 	label.d_checksum = dkcksum(&label);
     90  1.12     lukem 	if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
     91   1.1        ws 		perror("set label");
     92   1.1        ws 		exit(1);
     93   1.1        ws 	}
     94  1.22       dsl 	if (!doraw)
     95  1.22       dsl 		/* If we haven't written to the disk, don't discard on close */
     96  1.22       dsl 		ioctl(sd, DIOCKLABEL, &one);
     97  1.22       dsl 
     98   1.1        ws }
     99   1.1        ws 
    100  1.27     joerg static u_int16_t
    101  1.15     lukem getshort(void *p)
    102  1.15     lukem {
    103  1.15     lukem 	unsigned char *cp = p;
    104  1.15     lukem 
    105  1.15     lukem 	return (cp[0] | (cp[1] << 8));
    106  1.15     lukem }
    107  1.15     lukem 
    108  1.27     joerg static u_int32_t
    109   1.9       wiz getlong(void *p)
    110   1.1        ws {
    111   1.1        ws 	unsigned char *cp = p;
    112   1.1        ws 
    113  1.11     lukem 	return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
    114   1.1        ws }
    115   1.1        ws 
    116  1.27     joerg static int
    117  1.15     lukem getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
    118   1.1        ws {
    119  1.11     lukem 	unsigned char		buf[DEV_BSIZE];
    120  1.23     lukem 	struct mbr_partition	parts[MBR_PART_COUNT];
    121  1.11     lukem 	struct partition	npe;
    122  1.11     lukem 	off_t			loff;
    123  1.11     lukem 	int			i, j, unused, changed;
    124   1.2      fair 
    125  1.11     lukem 	changed = 0;
    126   1.2      fair 	loff = (off_t)off * DEV_BSIZE;
    127   1.1        ws 
    128   1.1        ws 	if (lseek(sd, loff, SEEK_SET) != loff) {
    129   1.1        ws 		perror("seek label");
    130   1.1        ws 		exit(1);
    131   1.1        ws 	}
    132  1.19      ross 	if (read(sd, buf, sizeof buf) != DEV_BSIZE) {
    133  1.25      jmmv 		if (off != MBR_BBSECTOR)
    134  1.25      jmmv 			perror("read label (sector is possibly out of "
    135  1.25      jmmv 			    "range)");
    136  1.25      jmmv 		else
    137  1.25      jmmv 			perror("read label");
    138   1.1        ws 		exit(1);
    139   1.1        ws 	}
    140  1.23     lukem 	if (getshort(buf + MBR_MAGIC_OFFSET) != MBR_MAGIC)
    141  1.11     lukem 		return (changed);
    142  1.23     lukem 	memcpy(parts, buf + MBR_PART_OFFSET, sizeof parts);
    143  1.11     lukem 
    144  1.11     lukem 				/* scan partition table */
    145  1.23     lukem 	for (i = 0; i < MBR_PART_COUNT; i++) {
    146  1.23     lukem 		if (parts[i].mbrp_type == 0 ||
    147  1.11     lukem 				/* extended partitions are handled below */
    148  1.23     lukem 		    MBR_IS_EXTENDED(parts[i].mbrp_type))
    149  1.11     lukem 			continue;
    150  1.11     lukem 
    151  1.11     lukem 		memset((void *)&npe, 0, sizeof(npe));
    152  1.11     lukem 		npe.p_size = getlong(&parts[i].mbrp_size);
    153  1.11     lukem 		npe.p_offset = getlong(&parts[i].mbrp_start) + off;
    154  1.23     lukem 		npe.p_fstype = xlat_mbr_fstype(parts[i].mbrp_type);
    155  1.11     lukem 
    156  1.11     lukem 				/* find existing entry, or first free slot */
    157  1.11     lukem 		unused = -1;	/* flag as no free slot */
    158  1.11     lukem 		if (verbose)
    159  1.13     lukem 			printf(
    160  1.13     lukem 			    "Found %s partition; size %u (%u MB), offset %u\n",
    161  1.13     lukem 			    fstypenames[npe.p_fstype],
    162  1.13     lukem 			    npe.p_size, npe.p_size / 2048, npe.p_offset);
    163  1.11     lukem 		for (j = 0; j < label.d_npartitions; j++) {
    164  1.11     lukem 			struct partition *lpe;
    165  1.11     lukem 
    166  1.11     lukem 			if (j == RAW_PART)
    167  1.11     lukem 				continue;
    168  1.11     lukem 			lpe = &label.d_partitions[j];
    169  1.11     lukem 			if (lpe->p_size == npe.p_size &&
    170  1.11     lukem 			    lpe->p_offset == npe.p_offset
    171  1.11     lukem #ifdef notyet
    172  1.11     lukem 			    && (lpe->p_fstype == npe.p_fstype ||
    173  1.11     lukem 			     lpe->p_fstype == FS_UNUSED) */
    174  1.11     lukem #endif
    175  1.11     lukem 			     ) {
    176  1.11     lukem 				if (verbose)
    177  1.11     lukem 					printf(
    178  1.11     lukem 			    "  skipping existing %s partition at slot %c.\n",
    179  1.11     lukem 					    fstypenames[lpe->p_fstype],
    180  1.11     lukem 					    j + 'a');
    181  1.11     lukem 				unused = -2;	/* flag as existing */
    182  1.11     lukem 				break;
    183  1.11     lukem 			}
    184  1.11     lukem 			if (unused == -1 && lpe->p_size == 0 &&
    185  1.11     lukem 			    lpe->p_fstype == FS_UNUSED)
    186  1.11     lukem 				unused = j;
    187  1.11     lukem 		}
    188  1.11     lukem 		if (unused == -2)
    189  1.11     lukem 			continue;	/* entry exists, skip... */
    190  1.11     lukem 		if (unused == -1) {
    191  1.11     lukem 			if (label.d_npartitions < MAXPARTITIONS) {
    192  1.11     lukem 				unused = label.d_npartitions;
    193  1.11     lukem 				label.d_npartitions++;
    194  1.11     lukem 			} else {
    195  1.11     lukem 				printf(
    196  1.11     lukem 				"  WARNING: no slots free for %s partition.\n",
    197  1.11     lukem 				    fstypenames[npe.p_fstype]);
    198  1.11     lukem 				continue;
    199  1.11     lukem 			}
    200  1.11     lukem 		}
    201  1.11     lukem 
    202  1.11     lukem 		if (verbose)
    203  1.11     lukem 			printf("  adding %s partition to slot %c.\n",
    204  1.11     lukem 			    fstypenames[npe.p_fstype], unused + 'a');
    205  1.11     lukem 		switch (npe.p_fstype) {
    206  1.11     lukem 		case FS_BSDFFS:
    207  1.21       dbj 		case FS_APPLEUFS:
    208  1.11     lukem 			npe.p_size = 16384;	/* XXX */
    209  1.11     lukem 			npe.p_fsize = 1024;
    210  1.11     lukem 			npe.p_frag = 8;
    211  1.11     lukem 			npe.p_cpg = 16;
    212   1.1        ws 			break;
    213  1.11     lukem #ifdef	__does_not_happen__
    214  1.11     lukem 		case FS_BSDLFS:
    215  1.11     lukem 			npe.p_size = 16384;	/* XXX */
    216  1.11     lukem 			npe.p_fsize = 1024;
    217  1.11     lukem 			npe.p_frag = 8;
    218  1.11     lukem 			npe.p_sgs = XXX;
    219   1.1        ws 			break;
    220   1.1        ws #endif
    221   1.1        ws 		}
    222  1.11     lukem 		changed++;
    223  1.11     lukem 		label.d_partitions[unused] = npe;
    224   1.1        ws 	}
    225  1.11     lukem 
    226  1.11     lukem 				/* recursively scan extended partitions */
    227  1.23     lukem 	for (i = 0; i < MBR_PART_COUNT; i++) {
    228   1.6        ws 		u_int32_t poff;
    229   1.6        ws 
    230  1.23     lukem 		if (MBR_IS_EXTENDED(parts[i].mbrp_type)) {
    231  1.11     lukem 			poff = getlong(&parts[i].mbrp_start) + extoff;
    232  1.15     lukem 			changed += getparts(sd, poff,
    233  1.11     lukem 			    extoff ? extoff : poff, verbose);
    234   1.1        ws 		}
    235   1.1        ws 	}
    236  1.11     lukem 	return (changed);
    237   1.1        ws }
    238   1.1        ws 
    239  1.27     joerg static void
    240   1.9       wiz usage(void)
    241   1.1        ws {
    242  1.28       wiz 	fprintf(stderr, "usage: %s [-fqrw] [-s sector] device\n",
    243  1.25      jmmv 	    getprogname());
    244   1.1        ws 	exit(1);
    245   1.1        ws }
    246   1.1        ws 
    247  1.11     lukem 
    248   1.1        ws int
    249   1.9       wiz main(int argc, char **argv)
    250   1.1        ws {
    251  1.11     lukem 	int	sd, ch, changed;
    252  1.25      jmmv 	char	*ep, name[MAXPATHLEN];
    253  1.11     lukem 	int	force;			/* force label update */
    254  1.12     lukem 	int	raw;			/* update on-disk label as well */
    255  1.11     lukem 	int	verbose;		/* verbose output */
    256  1.15     lukem 	int	write_it;		/* update in-core label if changed */
    257  1.25      jmmv 	uint32_t sector;		/* sector that contains the MBR */
    258  1.26  christos 	ulong	ulsector;
    259  1.11     lukem 
    260  1.11     lukem 	force = 0;
    261  1.12     lukem 	raw = 0;
    262  1.11     lukem 	verbose = 1;
    263  1.15     lukem 	write_it = 0;
    264  1.25      jmmv 	sector = MBR_BBSECTOR;
    265  1.25      jmmv 	while ((ch = getopt(argc, argv, "fqrs:w")) != -1) {
    266  1.11     lukem 		switch (ch) {
    267  1.11     lukem 		case 'f':
    268  1.11     lukem 			force = 1;
    269  1.11     lukem 			break;
    270  1.11     lukem 		case 'q':
    271  1.11     lukem 			verbose = 0;
    272  1.11     lukem 			break;
    273  1.12     lukem 		case 'r':
    274  1.12     lukem 			raw = 1;
    275  1.12     lukem 			break;
    276  1.25      jmmv 		case 's':
    277  1.25      jmmv 			errno = 0;
    278  1.26  christos 			ulsector = strtoul(optarg, &ep, 10);
    279  1.25      jmmv 			if (optarg[0] == '\0' || *ep != '\0')
    280  1.25      jmmv 				errx(EXIT_FAILURE,
    281  1.25      jmmv 				    "sector number (%s) incorrectly specified",
    282  1.25      jmmv 				    optarg);
    283  1.26  christos 			if ((errno == ERANGE && ulsector == ULONG_MAX) ||
    284  1.26  christos 			    ulsector > UINT32_MAX)
    285  1.25      jmmv 			    	errx(EXIT_FAILURE,
    286  1.25      jmmv 				    "sector number (%s) out of range",
    287  1.25      jmmv 				    optarg);
    288  1.26  christos 			sector = (uint32_t)ulsector;
    289  1.25      jmmv 			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.20      yamt 	if ((sd = opendisk(argv[0], write_it ? O_RDWR : O_RDONLY, name,
    303  1.20      yamt 	    (size_t)MAXPATHLEN, 0)) < 0) {
    304  1.11     lukem 		perror(argv[0]);
    305   1.1        ws 		exit(1);
    306   1.1        ws 	}
    307   1.1        ws 	getlabel(sd);
    308  1.25      jmmv 	changed = getparts(sd, sector, 0, verbose);
    309  1.11     lukem 
    310  1.11     lukem 	if (verbose) {
    311  1.11     lukem 		putchar('\n');
    312  1.11     lukem 		showpartitions(stdout, &label, 0);
    313  1.11     lukem 		putchar('\n');
    314  1.11     lukem 	}
    315  1.15     lukem 	if (write_it) {
    316  1.12     lukem 		if (! changed && ! force)
    317  1.11     lukem 			printf("No change; not updating disk label.\n");
    318  1.11     lukem 		else {
    319  1.11     lukem 			if (verbose)
    320  1.12     lukem 				printf("Updating in-core %sdisk label.\n",
    321  1.12     lukem 				    raw ? "and on-disk " : "");
    322  1.12     lukem 			setlabel(sd, raw);
    323  1.11     lukem 		}
    324  1.11     lukem 	} else {
    325  1.11     lukem 		printf("Not updating disk label.\n");
    326  1.11     lukem 	}
    327   1.1        ws 	close(sd);
    328  1.11     lukem 	return (0);
    329   1.1        ws }
    330