Home | History | Annotate | Line # | Download | only in mbrlabel
mbrlabel.c revision 1.9
      1  1.9      wiz /*	$NetBSD: mbrlabel.c,v 1.9 2000/12/24 01:50:29 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.9      wiz __RCSID("$NetBSD: mbrlabel.c,v 1.9 2000/12/24 01:50:29 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.1       ws #include <sys/disklabel.h>
     48  1.3  thorpej #include <sys/disklabel_mbr.h>
     49  1.1       ws #include <sys/ioctl.h>
     50  1.1       ws 
     51  1.1       ws #include "dkcksum.h"
     52  1.1       ws 
     53  1.1       ws #define	FIRSTPART	0
     54  1.1       ws 
     55  1.9      wiz int main(int, char **);
     56  1.9      wiz void usage(void);
     57  1.9      wiz void getlabel(int);
     58  1.9      wiz void setlabel(int);
     59  1.9      wiz int getparts(int, int, u_int32_t, u_int32_t);
     60  1.9      wiz int nbsdtype(int);
     61  1.9      wiz u_int32_t getlong(void *p);
     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 	struct partition save;
     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 	save = label.d_partitions[RAW_PART];
     75  1.1       ws 	memset(label.d_partitions, 0, sizeof label.d_partitions);
     76  1.1       ws 	label.d_partitions[RAW_PART] = save;
     77  1.1       ws 	/*
     78  1.1       ws 	 * Some ports seem to not set the number of partitions
     79  1.1       ws 	 * correctly, albeit they seem to set the raw partiton ok!
     80  1.1       ws 	 */
     81  1.1       ws 	if (label.d_npartitions <= RAW_PART)
     82  1.1       ws 		label.d_npartitions = RAW_PART + 1;
     83  1.1       ws }
     84  1.1       ws 
     85  1.1       ws void
     86  1.9      wiz setlabel(int sd)
     87  1.1       ws {
     88  1.1       ws 	label.d_checksum = 0;
     89  1.1       ws 	label.d_checksum = dkcksum(&label);
     90  1.1       ws 	if (ioctl(sd, DIOCSDINFO, &label) < 0) {
     91  1.1       ws 		perror("set label");
     92  1.1       ws 		exit(1);
     93  1.1       ws 	}
     94  1.1       ws }
     95  1.1       ws 
     96  1.1       ws static struct typetab {
     97  1.1       ws 	int mbrtype;
     98  1.1       ws 	int nbsdtype;
     99  1.1       ws } typetable[] = {
    100  1.3  thorpej 	{ MBR_PTYPE_NETBSD, FS_BSDFFS },
    101  1.3  thorpej 	{ MBR_PTYPE_386BSD, FS_BSDFFS },
    102  1.3  thorpej 	{ MBR_PTYPE_FAT12, FS_MSDOS },
    103  1.3  thorpej 	{ MBR_PTYPE_FAT16S, FS_MSDOS },
    104  1.3  thorpej 	{ MBR_PTYPE_FAT16B, FS_MSDOS },
    105  1.3  thorpej 	{ MBR_PTYPE_FAT32, FS_MSDOS },
    106  1.3  thorpej 	{ MBR_PTYPE_FAT32L, FS_MSDOS },
    107  1.3  thorpej 	{ MBR_PTYPE_FAT16L, FS_MSDOS },
    108  1.3  thorpej 	{ MBR_PTYPE_LNXEXT2, FS_EX2FS },
    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.1       ws 			return tt->nbsdtype;
    120  1.1       ws 	return FS_OTHER;
    121  1.1       ws }
    122  1.1       ws 
    123  1.1       ws u_int32_t
    124  1.9      wiz getlong(void *p)
    125  1.1       ws {
    126  1.1       ws 	unsigned char *cp = p;
    127  1.1       ws 
    128  1.1       ws 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    129  1.1       ws }
    130  1.1       ws 
    131  1.1       ws int
    132  1.9      wiz getparts(int sd, int np, u_int32_t off, u_int32_t eoff)
    133  1.1       ws {
    134  1.1       ws 	unsigned char buf[DEV_BSIZE];
    135  1.5      cgd 	struct mbr_partition parts[NMBRPART];
    136  1.2     fair 	off_t loff = 0;	/* XXX this nonsense shuts up GCC 2.7.2.2 */
    137  1.5      cgd 	int i;
    138  1.2     fair 
    139  1.2     fair 	loff = (off_t)off * DEV_BSIZE;
    140  1.1       ws 
    141  1.1       ws 	if (lseek(sd, loff, SEEK_SET) != loff) {
    142  1.1       ws 		perror("seek label");
    143  1.1       ws 		exit(1);
    144  1.1       ws 	}
    145  1.1       ws 	if (read(sd, buf, DEV_BSIZE) != DEV_BSIZE) {
    146  1.1       ws 		perror("read label");
    147  1.1       ws 		exit(1);
    148  1.1       ws 	}
    149  1.1       ws 	if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa)
    150  1.1       ws 		return np;
    151  1.5      cgd 	memcpy(parts, buf + MBR_PARTOFF, sizeof parts);
    152  1.5      cgd 	for (i = 0; i < NMBRPART; i++) {
    153  1.5      cgd 		switch (parts[i].mbrp_typ) {
    154  1.1       ws 		case 0:
    155  1.1       ws 			/* Nothing to do */
    156  1.1       ws 			break;
    157  1.3  thorpej 		case MBR_PTYPE_EXT:
    158  1.3  thorpej 		case MBR_PTYPE_EXT_LBA:
    159  1.6       ws 		case MBR_PTYPE_EXT_LNX:
    160  1.1       ws 			/* Will be handled below */
    161  1.1       ws 			break;
    162  1.1       ws 		default:
    163  1.5      cgd 			label.d_partitions[np].p_size = getlong(&parts[i].mbrp_size);
    164  1.5      cgd 			label.d_partitions[np].p_offset = getlong(&parts[i].mbrp_start) + off;
    165  1.5      cgd 			label.d_partitions[np].p_fstype = nbsdtype(parts[i].mbrp_typ);
    166  1.1       ws 			switch (label.d_partitions[np].p_fstype) {
    167  1.1       ws 			case FS_BSDFFS:
    168  1.1       ws 				label.d_partitions[np].p_size = 16384;
    169  1.1       ws 				label.d_partitions[np].p_fsize = 1024;
    170  1.1       ws 				label.d_partitions[np].p_frag = 8;
    171  1.1       ws 				label.d_partitions[np].p_cpg = 16;
    172  1.1       ws 				break;
    173  1.1       ws #ifdef	__does_not_happen__
    174  1.1       ws 			case FS_BSDLFS:
    175  1.1       ws 				label.d_partitions[np].p_size = 16384;
    176  1.1       ws 				label.d_partitions[np].p_fsize = 1024;
    177  1.1       ws 				label.d_partitions[np].p_frag = 8;
    178  1.1       ws 				label.d_partitions[np].p_sgs = XXX;
    179  1.1       ws 				break;
    180  1.1       ws #endif
    181  1.1       ws 			}
    182  1.1       ws 			np++;
    183  1.1       ws 			break;
    184  1.1       ws 		}
    185  1.1       ws 		if (np >MAXPARTITIONS)
    186  1.1       ws 			return np;
    187  1.1       ws 		if (np == RAW_PART)
    188  1.1       ws 			np++;
    189  1.1       ws 	}
    190  1.5      cgd 	for (i = 0; i < NMBRPART; i++) {
    191  1.6       ws 		u_int32_t poff;
    192  1.6       ws 
    193  1.5      cgd 		switch (parts[i].mbrp_typ) {
    194  1.3  thorpej 		case MBR_PTYPE_EXT:
    195  1.3  thorpej 		case MBR_PTYPE_EXT_LBA:
    196  1.6       ws 		case MBR_PTYPE_EXT_LNX:
    197  1.6       ws 			poff = getlong(&parts[i].mbrp_start) + eoff;
    198  1.6       ws 			np = getparts(sd, np, poff, eoff ? eoff : poff);
    199  1.1       ws 			break;
    200  1.1       ws 		default:
    201  1.1       ws 			break;
    202  1.1       ws 		}
    203  1.1       ws 		if (np >MAXPARTITIONS)
    204  1.1       ws 			return np;
    205  1.1       ws 	}
    206  1.1       ws 	return np;
    207  1.1       ws }
    208  1.1       ws 
    209  1.1       ws void
    210  1.9      wiz usage(void)
    211  1.1       ws {
    212  1.4      cgd 	fprintf(stderr, "usage: mbrlabel rawdisk\n");
    213  1.1       ws 	exit(1);
    214  1.1       ws }
    215  1.1       ws 
    216  1.1       ws int
    217  1.9      wiz main(int argc, char **argv)
    218  1.1       ws {
    219  1.1       ws 	int sd;
    220  1.1       ws 	int np;
    221  1.1       ws 	char name[MAXPATHLEN];
    222  1.1       ws 
    223  1.1       ws 	if (argc != 2)
    224  1.1       ws 		usage();
    225  1.1       ws 
    226  1.1       ws 	if ((sd = opendisk(*++argv, O_RDWR, name, MAXPATHLEN, 0)) < 0) {
    227  1.1       ws 		perror(*argv);
    228  1.1       ws 		exit(1);
    229  1.1       ws 	}
    230  1.1       ws 	getlabel(sd);
    231  1.6       ws 	np = getparts(sd, FIRSTPART, MBR_BBSECTOR, 0);
    232  1.1       ws 	if (np > label.d_npartitions)
    233  1.1       ws 		label.d_npartitions = np;
    234  1.1       ws 	setlabel(sd);
    235  1.1       ws 	close(sd);
    236  1.1       ws 	return 0;
    237  1.1       ws }
    238