Home | History | Annotate | Line # | Download | only in installboot
disklabel.c revision 1.2.150.1
      1  1.2.150.1  mjf /*	$NetBSD: disklabel.c,v 1.2.150.1 2009/01/17 13:27:55 mjf Exp $	*/
      2        1.1  leo 
      3        1.1  leo /*
      4        1.1  leo  * Copyright (c) 1995 Waldi Ravens
      5        1.1  leo  * All rights reserved.
      6        1.1  leo  *
      7        1.1  leo  * Redistribution and use in source and binary forms, with or without
      8        1.1  leo  * modification, are permitted provided that the following conditions
      9        1.1  leo  * are met:
     10        1.1  leo  * 1. Redistributions of source code must retain the above copyright
     11        1.1  leo  *    notice, this list of conditions and the following disclaimer.
     12        1.1  leo  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  leo  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  leo  *    documentation and/or other materials provided with the distribution.
     15        1.1  leo  * 3. All advertising materials mentioning features or use of this software
     16        1.1  leo  *    must display the following acknowledgement:
     17        1.1  leo  *        This product includes software developed by Waldi Ravens.
     18        1.1  leo  * 4. The name of the author may not be used to endorse or promote products
     19        1.1  leo  *    derived from this software without specific prior written permission
     20        1.1  leo  *
     21        1.1  leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22        1.1  leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23        1.1  leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24        1.1  leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25        1.1  leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26        1.1  leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27        1.1  leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28        1.1  leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29        1.1  leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30        1.1  leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31        1.1  leo  */
     32        1.1  leo 
     33        1.1  leo #include <sys/types.h>
     34        1.1  leo #include <sys/param.h>
     35        1.2  leo #include <ufs/ufs/dinode.h>
     36        1.1  leo #include <ufs/ffs/fs.h>
     37        1.1  leo #include <sys/disklabel.h>
     38        1.1  leo #include <machine/ahdilabel.h>
     39        1.1  leo #include <unistd.h>
     40        1.1  leo #include <string.h>
     41        1.1  leo #include <stdlib.h>
     42        1.1  leo #include <fcntl.h>
     43        1.1  leo #include <err.h>
     44        1.1  leo 
     45        1.1  leo #if (BBSIZE < MINBBSIZE)
     46        1.1  leo #error BBSIZE is smaller than MINBBSIZE
     47        1.1  leo #endif
     48        1.1  leo 
     49        1.1  leo struct ahdilabel {
     50        1.1  leo 	u_int		 nsecs;
     51        1.1  leo 	daddr_t		 bslst;
     52        1.1  leo 	daddr_t		 bslend;
     53        1.1  leo 	u_int		 nroots;
     54        1.1  leo 	daddr_t		 *roots;
     55        1.1  leo 	u_int		 nparts;
     56        1.1  leo 	struct ahdi_part *parts;
     57        1.1  leo };
     58        1.1  leo 
     59        1.1  leo u_int	dkcksum __P((struct disklabel *));
     60  1.2.150.1  mjf u_int32_t readdisklabel __P((char *, struct disklabel *));
     61        1.1  leo 
     62        1.1  leo static int  bsd_label __P((int, off_t, struct disklabel *));
     63  1.2.150.1  mjf static int  ahdi_label __P((int, u_int32_t *, struct disklabel *));
     64        1.1  leo static int  ahdi_getparts __P((int, daddr_t, daddr_t, struct ahdilabel *));
     65        1.1  leo 
     66        1.1  leo u_int
     67        1.1  leo dkcksum (dl)
     68        1.1  leo 	struct disklabel *dl;
     69        1.1  leo {
     70        1.1  leo 	u_int16_t sum  = 0,
     71        1.1  leo 		  *st  = (u_int16_t *)dl,
     72        1.1  leo 		  *end = (u_int16_t *)&dl->d_partitions[dl->d_npartitions];
     73        1.1  leo 
     74        1.1  leo 	while (st < end)
     75        1.1  leo 		sum ^= *st++;
     76        1.1  leo 	return(sum);
     77        1.1  leo }
     78        1.1  leo 
     79  1.2.150.1  mjf u_int32_t
     80        1.1  leo readdisklabel (fn, dl)
     81        1.1  leo 	char		 *fn;
     82        1.1  leo 	struct disklabel *dl;
     83        1.1  leo {
     84        1.1  leo 	int		 fd, e;
     85  1.2.150.1  mjf 	u_int32_t	 bbsec;
     86        1.1  leo 
     87        1.1  leo 	memset(dl, 0, sizeof *dl);
     88        1.1  leo 
     89        1.1  leo 	if ((fd = open(fn, O_RDONLY)) < 0)
     90        1.1  leo 		err(EXIT_FAILURE, "%s", fn);
     91        1.1  leo 
     92        1.1  leo 	/* Try NetBSD/Atari format first */
     93        1.1  leo 	if ((e = bsd_label(fd, (off_t)0, dl)) < 0)
     94        1.1  leo 		err(EXIT_FAILURE, "%s", fn);
     95        1.1  leo 	if (!e)
     96        1.1  leo 		return(0);
     97        1.1  leo 
     98        1.1  leo 	/* Try unprotected AHDI format last */
     99        1.1  leo 	if ((e = ahdi_label(fd, &bbsec, dl)) < 0)
    100        1.1  leo 		err(EXIT_FAILURE, "%s", fn);
    101        1.1  leo 	if (!e)
    102        1.1  leo 		return(bbsec);
    103        1.1  leo 
    104        1.1  leo 	warnx("%s: Unknown disk label format.", fn);
    105        1.1  leo 	return(NO_BOOT_BLOCK);
    106        1.1  leo }
    107        1.1  leo 
    108        1.1  leo static int
    109        1.1  leo bsd_label (fd, offs, label)
    110        1.1  leo 	int		 fd;
    111        1.1  leo 	off_t		 offs;
    112        1.1  leo 	struct disklabel *label;
    113        1.1  leo {
    114        1.1  leo 	struct bootblock bb;
    115        1.1  leo 	struct disklabel *p;
    116        1.1  leo 
    117        1.1  leo 	if (lseek(fd, offs, SEEK_SET) != offs)
    118        1.1  leo 		return(-1);
    119        1.1  leo 	if (read(fd, &bb, sizeof(bb)) != sizeof(bb))
    120        1.1  leo 		return(-1);
    121        1.1  leo 
    122        1.1  leo 	p = (struct disklabel *)bb.bb_label;
    123        1.1  leo 	if (  (offs == 0 && bb.bb_magic != NBDAMAGIC)
    124        1.1  leo 	   || (offs != 0 && bb.bb_magic != AHDIMAGIC)
    125        1.1  leo 	   || p->d_npartitions > MAXPARTITIONS
    126        1.1  leo 	   || p->d_magic2 != DISKMAGIC
    127        1.1  leo 	   || p->d_magic  != DISKMAGIC
    128        1.1  leo 	   || dkcksum(p)  != 0
    129        1.1  leo 	   )	{
    130        1.1  leo 		return(1);
    131        1.1  leo 	}
    132        1.1  leo 
    133        1.1  leo 	*label = *p;
    134        1.1  leo 	return(0);
    135        1.1  leo }
    136        1.1  leo 
    137        1.1  leo static int
    138        1.1  leo ahdi_label (fd, bbsec, label)
    139        1.1  leo 	int		 fd;
    140  1.2.150.1  mjf 	u_int32_t	 *bbsec;
    141        1.1  leo 	struct disklabel *label;
    142        1.1  leo {
    143        1.1  leo 	struct ahdilabel al;
    144        1.1  leo 	u_int		 i, j;
    145        1.1  leo 	int		 e;
    146        1.1  leo 
    147        1.1  leo 	memset(&al, 0, sizeof(al));
    148        1.1  leo 	if ((e = ahdi_getparts(fd, AHDI_BBLOCK, AHDI_BBLOCK, &al)))
    149        1.1  leo 		return(e);
    150        1.1  leo 
    151        1.1  leo 	/*
    152        1.1  leo 	 * Perform sanity checks.
    153        1.1  leo 	 */
    154        1.1  leo 	if (al.bslst == 0 || al.bslend == 0)
    155        1.1  leo 		return(1);
    156        1.1  leo 	if (al.nsecs == 0 || al.nparts == 0)
    157        1.1  leo 		return(1);
    158        1.1  leo 	if (al.nparts > AHDI_MAXPARTS)
    159        1.1  leo 		warnx("Too many AHDI partitions (%u).", al.nparts);
    160        1.1  leo 	for (i = 0; i < al.nparts; ++i) {
    161        1.1  leo 		struct ahdi_part *p1 = &al.parts[i];
    162        1.1  leo 		for (j = 0; j < al.nroots; ++j) {
    163        1.1  leo 			daddr_t	aux = al.roots[j];
    164        1.1  leo 			if (aux >= p1->ap_st && aux <= p1->ap_end)
    165        1.1  leo 				return(1);
    166        1.1  leo 		}
    167        1.1  leo 		for (j = i + 1; j < al.nparts; ++j) {
    168        1.1  leo 			struct ahdi_part *p2 = &al.parts[j];
    169        1.1  leo 			if (p1->ap_st >= p2->ap_st && p1->ap_st <= p2->ap_end)
    170        1.1  leo 				return(1);
    171        1.1  leo 			if (p2->ap_st >= p1->ap_st && p2->ap_st <= p1->ap_end)
    172        1.1  leo 				return(1);
    173        1.1  leo 		}
    174        1.1  leo 		if (p1->ap_st >= al.bslst && p1->ap_st <= al.bslend)
    175        1.1  leo 			return(1);
    176        1.1  leo 		if (al.bslst >= p1->ap_st && al.bslst <= p1->ap_end)
    177        1.1  leo 			return(1);
    178        1.1  leo 	}
    179        1.1  leo 
    180        1.1  leo 	/*
    181        1.1  leo 	 * Search for a NetBSD boot block
    182        1.1  leo 	 */
    183        1.1  leo 	for (i = 0; i < al.nparts; ++i) {
    184        1.1  leo 		struct ahdi_part *pd = &al.parts[i];
    185        1.1  leo 		u_int id = *((u_int32_t *)&pd->ap_flg);
    186        1.1  leo 
    187        1.1  leo 		if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
    188        1.1  leo 			off_t	offs = pd->ap_st * AHDI_BSIZE;
    189        1.1  leo 			if ((e = bsd_label(fd, offs, label)) < 0)
    190        1.1  leo 				return(e);
    191        1.1  leo 			if (!e) {
    192        1.1  leo 				*bbsec = pd->ap_st;	/* got it */
    193        1.1  leo 				return(0);
    194        1.1  leo 			}
    195        1.1  leo 		}
    196        1.1  leo 	}
    197        1.1  leo 	*bbsec = NO_BOOT_BLOCK;	/* AHDI label, no NetBSD boot block */
    198        1.1  leo 	return(0);
    199        1.1  leo }
    200        1.1  leo 
    201        1.1  leo static int
    202        1.1  leo ahdi_getparts(fd, rsec, esec, alab)
    203        1.1  leo 	int		 fd;
    204        1.1  leo 	daddr_t		 rsec,
    205        1.1  leo 			 esec;
    206        1.1  leo 	struct ahdilabel *alab;
    207        1.1  leo {
    208        1.1  leo 	struct ahdi_part *part, *end;
    209        1.1  leo 	struct ahdi_root root;
    210        1.1  leo 	off_t		 ro;
    211        1.1  leo 
    212        1.1  leo 	ro = rsec * AHDI_BSIZE;
    213        1.1  leo 	if (lseek(fd, ro, SEEK_SET) != ro) {
    214        1.1  leo 		off_t	mend = lseek(fd, 0, SEEK_END);
    215        1.1  leo 		if (mend == -1 || mend > ro)
    216        1.1  leo 			return(-1);
    217        1.1  leo 		return(1);
    218        1.1  leo 	}
    219        1.1  leo 	if (read(fd, &root, sizeof(root)) != sizeof(root))
    220        1.1  leo 		return(-1);
    221        1.1  leo 
    222        1.1  leo 	if (rsec == AHDI_BBLOCK)
    223        1.1  leo 		end = &root.ar_parts[AHDI_MAXRPD];
    224        1.1  leo 	else end = &root.ar_parts[AHDI_MAXARPD];
    225        1.1  leo 	for (part = root.ar_parts; part < end; ++part) {
    226        1.1  leo 		u_int	id = *((u_int32_t *)&part->ap_flg);
    227        1.1  leo 		if (!(id & 0x01000000))
    228        1.1  leo 			continue;
    229        1.1  leo 		if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
    230        1.1  leo 			int	e;
    231        1.1  leo 			daddr_t	aux = part->ap_st + esec;
    232        1.1  leo 			alab->roots = realloc(alab->roots,
    233        1.1  leo 					(alab->nroots + 1) * sizeof(*alab->roots));
    234        1.1  leo 			alab->roots[alab->nroots++] = aux;
    235        1.1  leo 			e = ahdi_getparts(fd, aux,
    236        1.1  leo 					   esec == AHDI_BBLOCK ? aux : esec, alab);
    237        1.1  leo 			if (e)
    238        1.1  leo 				return(e);
    239        1.1  leo 		} else {
    240        1.1  leo 			struct ahdi_part *p;
    241        1.1  leo 			alab->parts = realloc(alab->parts,
    242        1.1  leo 					(alab->nparts + 1) * sizeof(*alab->parts));
    243        1.1  leo 			p = &alab->parts[alab->nparts++];
    244        1.1  leo 			*((u_int32_t *)&p->ap_flg) = id;
    245        1.1  leo 			p->ap_st = part->ap_st + rsec;
    246        1.1  leo 			p->ap_end  = p->ap_st + part->ap_size - 1;
    247        1.1  leo 		}
    248        1.1  leo 	}
    249        1.1  leo 	alab->nsecs  = root.ar_hdsize;
    250        1.1  leo 	alab->bslst  = root.ar_bslst;
    251        1.1  leo 	alab->bslend = root.ar_bslst + root.ar_bslsize - 1;
    252        1.1  leo 	return(0);
    253        1.1  leo }
    254