Home | History | Annotate | Line # | Download | only in aptck
disklbl.c revision 1.2
      1  1.2  leo /*	$NetBSD: disklbl.c,v 1.2 1996/01/20 13:54:46 leo 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 <stdlib.h>
     35  1.1  leo #include <stdio.h>
     36  1.1  leo #include "libtos.h"
     37  1.1  leo #include "aptck.h"
     38  1.1  leo #include "ahdilbl.h"
     39  1.1  leo #include "disklbl.h"
     40  1.1  leo 
     41  1.1  leo static int	dkcksum    PROTO((struct disklabel *));
     42  1.1  leo static int	bsd_label  PROTO((disk_t *, u_int));
     43  1.1  leo static int	ahdi_label PROTO((disk_t *));
     44  1.1  leo static int	ahdi_display PROTO((disk_t *));
     45  1.1  leo static u_int	ahdi_getparts PROTO((disk_t *, u_int, u_int));
     46  1.1  leo 
     47  1.1  leo int
     48  1.1  leo readdisklabel(dd)
     49  1.1  leo 	disk_t	*dd;
     50  1.1  leo {
     51  1.1  leo 	int	e;
     52  1.1  leo 
     53  1.1  leo 	printf("Device     : %s (%s) [%s]\n", dd->sname, dd->fname, dd->product);
     54  1.1  leo 	printf("Medium size: %lu sectors\n", (u_long)dd->msize);
     55  1.1  leo 	printf("Sector size: %lu bytes\n\n", (u_long)dd->bsize);
     56  1.1  leo 
     57  1.1  leo 	e = bsd_label(dd, LABELSECTOR);
     58  1.1  leo 	if (e < 0) {
     59  1.1  leo 		printf("Device I/O error (hardware problem?)\n\n");
     60  1.1  leo 		return(-1);
     61  1.1  leo 	}
     62  1.1  leo 	if (!e) {
     63  1.1  leo 		printf("NetBSD/Atari format, boot block: "
     64  1.1  leo 		       "sector %u labeloffset %u\n\n",
     65  1.1  leo 		       dd->bblock, dd->lblofs);
     66  1.1  leo 		return(0);
     67  1.1  leo 	}
     68  1.1  leo 
     69  1.1  leo 	e = ahdi_label(dd);
     70  1.1  leo 	if (e < 0) {
     71  1.1  leo 		printf("Device I/O error (hardware problem?)\n\n");
     72  1.1  leo 		return(-1);
     73  1.1  leo 	}
     74  1.1  leo 	if (!e) {
     75  1.1  leo 		printf("AHDI format, NetBSD boot block: ");
     76  1.1  leo 		if (dd->bblock != NO_BOOT_BLOCK)
     77  1.1  leo 			printf("sector %u labeloffset %u\n\n",
     78  1.1  leo 			       dd->bblock, dd->lblofs);
     79  1.1  leo 		else printf("none\n\n");
     80  1.1  leo 		return(0);
     81  1.1  leo 	}
     82  1.1  leo 
     83  1.1  leo 	printf("Unknown label format.\n\n");
     84  1.1  leo 	return(-1);
     85  1.1  leo }
     86  1.1  leo 
     87  1.1  leo static int
     88  1.1  leo bsd_label(dd, offset)
     89  1.1  leo 	disk_t		*dd;
     90  1.1  leo 	u_int		offset;
     91  1.1  leo {
     92  1.1  leo 	u_char		*bblk;
     93  1.1  leo 	u_int		nsec;
     94  1.1  leo 	int		rv;
     95  1.1  leo 
     96  1.2  leo 	nsec = (BBMINSIZE + (dd->bsize - 1)) / dd->bsize;
     97  1.1  leo 	bblk = disk_read(dd, offset, nsec);
     98  1.1  leo 	if (bblk) {
     99  1.1  leo 		u_short	*end, *p;
    100  1.1  leo 
    101  1.2  leo 		end = (u_short *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
    102  1.1  leo 		rv = 1;
    103  1.1  leo 		for (p = (u_short *)bblk; p < end; ++p) {
    104  1.1  leo 			struct disklabel *dl = (struct disklabel *)p;
    105  1.1  leo 			if (dl->d_magic == DISKMAGIC && dl->d_magic2 == DISKMAGIC
    106  1.1  leo 		    	    && dl->d_npartitions <= MAXPARTITIONS && !dkcksum(dl)) {
    107  1.1  leo 		    		dd->lblofs = (u_char *)p - bblk;
    108  1.1  leo 		    		dd->bblock = offset;
    109  1.1  leo 				rv = 0;
    110  1.1  leo 				break;
    111  1.1  leo 			}
    112  1.1  leo 		}
    113  1.1  leo 		free(bblk);
    114  1.1  leo 	}
    115  1.1  leo 	else rv = -1;
    116  1.1  leo 
    117  1.1  leo 	return(rv);
    118  1.1  leo }
    119  1.1  leo 
    120  1.1  leo static int
    121  1.1  leo dkcksum(dl)
    122  1.1  leo 	struct disklabel *dl;
    123  1.1  leo {
    124  1.1  leo 	u_short	*start, *end, sum = 0;
    125  1.1  leo 
    126  1.1  leo 	start = (u_short *)dl;
    127  1.1  leo 	end   = (u_short *)&dl->d_partitions[dl->d_npartitions];
    128  1.1  leo 	while (start < end)
    129  1.1  leo 		sum ^= *start++;
    130  1.1  leo 	return(sum);
    131  1.1  leo }
    132  1.1  leo 
    133  1.1  leo int
    134  1.1  leo ahdi_label(dd)
    135  1.1  leo 	disk_t	*dd;
    136  1.1  leo {
    137  1.1  leo 	u_int	i;
    138  1.1  leo 	int	e;
    139  1.1  leo 
    140  1.1  leo 	/*
    141  1.1  leo 	 * The AHDI format requires a specific block size.
    142  1.1  leo 	 */
    143  1.1  leo 	if (dd->bsize != AHDI_BSIZE)
    144  1.1  leo 		return(1);
    145  1.1  leo 
    146  1.1  leo 	/*
    147  1.1  leo 	 * Fetch the AHDI partition descriptors.
    148  1.1  leo 	 */
    149  1.1  leo 	i = ahdi_getparts(dd, AHDI_BBLOCK, AHDI_BBLOCK);
    150  1.1  leo 	if (i) {
    151  1.1  leo 		if (i < dd->msize)
    152  1.1  leo 			return(-1);	/* disk read error		*/
    153  1.1  leo 		else return(1);		/* reading past end of medium	*/
    154  1.1  leo 	}
    155  1.1  leo 
    156  1.1  leo 	/*
    157  1.1  leo 	 * Display and perform sanity checks.
    158  1.1  leo 	 */
    159  1.1  leo 	i = ahdi_display(dd);
    160  1.1  leo 	if (i)
    161  1.1  leo 		return(i);
    162  1.1  leo 
    163  1.1  leo 	/*
    164  1.1  leo 	 * Search for a NetBSD disk label
    165  1.1  leo 	 */
    166  1.1  leo 	dd->bblock = NO_BOOT_BLOCK;
    167  1.1  leo 	for (i = 0; i < dd->nparts; ++i) {
    168  1.1  leo 		part_t	*pd = &dd->parts[i];
    169  1.1  leo 		u_int	id  = *((u_int32_t *)&pd->id) >> 8;
    170  1.1  leo 		if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
    171  1.1  leo 			u_int	offs = pd->start;
    172  1.1  leo 			if ((e = bsd_label(dd, offs)) < 0) {
    173  1.1  leo 				return(e);		/* I/O error */
    174  1.1  leo 			}
    175  1.1  leo 			if (!e) {
    176  1.1  leo 				dd->bblock = offs;	/* got it */
    177  1.1  leo 				return(0);
    178  1.1  leo 			}
    179  1.1  leo 			if (id == AHDI_PID_NBD && dd->bblock == NO_BOOT_BLOCK)
    180  1.1  leo 				dd->bblock = offs;
    181  1.1  leo 		}
    182  1.1  leo 	}
    183  1.1  leo 	return(0);
    184  1.1  leo }
    185  1.1  leo 
    186  1.1  leo static int
    187  1.1  leo root_cmp(x1, x2)
    188  1.1  leo 	const void	*x1, *x2;
    189  1.1  leo {
    190  1.1  leo 	const u_int	*r1 = x1,
    191  1.1  leo 			*r2 = x2;
    192  1.1  leo 
    193  1.1  leo 	if (*r1 < *r2)
    194  1.1  leo 		return(-1);
    195  1.1  leo 	if (*r1 > *r2)
    196  1.1  leo 		return(1);
    197  1.1  leo 	return(0);
    198  1.1  leo }
    199  1.1  leo 
    200  1.1  leo static int
    201  1.1  leo part_cmp(x1, x2)
    202  1.1  leo 	const void	*x1, *x2;
    203  1.1  leo {
    204  1.1  leo 	const part_t	*p1 = x1,
    205  1.1  leo 			*p2 = x2;
    206  1.1  leo 
    207  1.1  leo 	if (p1->start < p2->start)
    208  1.1  leo 		return(-1);
    209  1.1  leo 	if (p1->start > p2->start)
    210  1.1  leo 		return(1);
    211  1.1  leo 	if (p1->end < p2->end)
    212  1.1  leo 		return(-1);
    213  1.1  leo 	if (p1->end > p2->end)
    214  1.1  leo 		return(1);
    215  1.1  leo 	if (p1->rsec < p2->rsec)
    216  1.1  leo 		return(-1);
    217  1.1  leo 	if (p1->rsec > p2->rsec)
    218  1.1  leo 		return(1);
    219  1.1  leo 	if (p1->rent < p2->rent)
    220  1.1  leo 		return(-1);
    221  1.1  leo 	if (p1->rent > p2->rent)
    222  1.1  leo 		return(1);
    223  1.1  leo 	return(0);
    224  1.1  leo }
    225  1.1  leo 
    226  1.1  leo static int
    227  1.1  leo ahdi_display(dd)
    228  1.1  leo 	disk_t	*dd;
    229  1.1  leo {
    230  1.1  leo 	int	i, j, rv = 0;
    231  1.1  leo 
    232  1.1  leo 	printf("Start of bad sector list : %u\n", dd->bslst);
    233  1.1  leo 	if (dd->bslst == 0) {
    234  1.1  leo 		printf("* Illegal value (zero) *\n"); rv = 1;
    235  1.1  leo 	}
    236  1.1  leo 	printf("End of bad sector list   : %u\n", dd->bslend);
    237  1.1  leo 	if (dd->bslend == 0) {
    238  1.1  leo 		printf("* Illegal value (zero) *\n"); rv = 1;
    239  1.1  leo 	}
    240  1.1  leo 	printf("Medium size (in root sec): %u\n", dd->hdsize);
    241  1.1  leo 	if (dd->hdsize == 0) {
    242  1.1  leo 		printf("* Illegal value (zero) *\n"); rv = 1;
    243  1.1  leo 	}
    244  1.1  leo 
    245  1.1  leo 	qsort(dd->roots, dd->nroots, sizeof *dd->roots, root_cmp);
    246  1.1  leo 	qsort(dd->parts, dd->nparts, sizeof *dd->parts, part_cmp);
    247  1.1  leo 	printf("\n    root  desc   id     start       end    MBs\n");
    248  1.1  leo 
    249  1.1  leo 	for (i = 0; i < dd->nparts; ++i) {
    250  1.1  leo 		part_t	*p1 = &dd->parts[i];
    251  1.1  leo 		u_int	megs = p1->end - p1->start + 1,
    252  1.1  leo 			blpm = (1024 * 1024) / dd->bsize;
    253  1.1  leo 		megs = (megs + (blpm >> 1)) / blpm;
    254  1.1  leo 		printf("%8u  %4u  %s  %8u  %8u  (%3u)\n",
    255  1.1  leo 			p1->rsec, p1->rent, p1->id,
    256  1.1  leo 			p1->start, p1->end, megs);
    257  1.1  leo 		for (j = 0; j < dd->nroots; ++j) {
    258  1.1  leo 			u_int	aux = dd->roots[j];
    259  1.1  leo 			if (aux >= p1->start && aux <= p1->end) {
    260  1.1  leo 				printf("FATAL: auxilary root at %u\n", aux); rv = 1;
    261  1.1  leo 			}
    262  1.1  leo 		}
    263  1.1  leo 		for (j = i; j--;) {
    264  1.1  leo 			part_t	*p2 = &dd->parts[j];
    265  1.1  leo 			if (p1->start >= p2->start && p1->start <= p2->end) {
    266  1.1  leo 				printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent); rv = 1;
    267  1.1  leo 			}
    268  1.1  leo 			if (p2->start >= p1->start && p2->start <= p1->end) {
    269  1.1  leo 				printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent); rv = 1;
    270  1.1  leo 			}
    271  1.1  leo 		}
    272  1.1  leo 		if (p1->start >= dd->bslst && p1->start <= dd->bslend) {
    273  1.1  leo 			printf("FATAL: partition overlaps with bad sector list\n"); rv = 1;
    274  1.1  leo 		}
    275  1.1  leo 		if (dd->bslst >= p1->start && dd->bslst <= p1->end) {
    276  1.1  leo 			printf("FATAL: partition overlaps with bad sector list\n"); rv = 1;
    277  1.1  leo 		}
    278  1.1  leo 	}
    279  1.1  leo 
    280  1.1  leo 	printf("\nTotal number of auxilary roots: %u\n", dd->nroots);
    281  1.1  leo 	printf("Total number of partitions    : %u\n", dd->nparts);
    282  1.1  leo 	if (dd->nparts == 0) {
    283  1.1  leo 		printf("* Weird # of partitions (zero) *\n"); rv = 1;
    284  1.1  leo 	}
    285  1.1  leo 	if (dd->nparts > AHDI_MAXPARTS) {
    286  1.1  leo 		printf("* Too many AHDI partitions for the default NetBSD "
    287  1.1  leo 			"kernel *\n  Increase MAXAUXROOTS in src/sys/arch/"
    288  1.1  leo 			"atari/include/disklabel.h\n  to at least %u, and "
    289  1.1  leo 			"recompile the NetBSD kernel.\n", dd->nroots);
    290  1.1  leo 		rv = -1;
    291  1.1  leo 	}
    292  1.1  leo 	return(rv);
    293  1.1  leo }
    294  1.1  leo 
    295  1.1  leo static u_int
    296  1.1  leo ahdi_getparts(dd, rsec, esec)
    297  1.1  leo 	disk_t			*dd;
    298  1.1  leo 	u_int			rsec,
    299  1.1  leo 				esec;
    300  1.1  leo {
    301  1.1  leo 	struct ahdi_part	*part, *end;
    302  1.1  leo 	struct ahdi_root	*root;
    303  1.1  leo 	u_int			rv;
    304  1.1  leo 
    305  1.1  leo 	root = disk_read(dd, rsec, 1);
    306  1.1  leo 	if (!root) {
    307  1.1  leo 		rv = rsec + (rsec == 0);
    308  1.1  leo 		goto done;
    309  1.1  leo 	}
    310  1.1  leo 
    311  1.1  leo 	if (rsec == AHDI_BBLOCK)
    312  1.1  leo 		end = &root->ar_parts[AHDI_MAXRPD];
    313  1.1  leo 	else end = &root->ar_parts[AHDI_MAXARPD];
    314  1.1  leo 	for (part = root->ar_parts; part < end; ++part) {
    315  1.1  leo 		u_int	id = *((u_int32_t *)&part->ap_flg);
    316  1.1  leo 		if (!(id & 0x01000000))
    317  1.1  leo 			continue;
    318  1.1  leo 		if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
    319  1.1  leo 			u_int	offs = part->ap_offs + esec;
    320  1.1  leo 			u_int	i = ++dd->nroots;
    321  1.1  leo 			dd->roots = xrealloc(dd->roots, i * sizeof *dd->roots);
    322  1.1  leo 			dd->roots[--i] = offs;
    323  1.1  leo 			rv = ahdi_getparts(dd, offs, esec == AHDI_BBLOCK ? offs : esec);
    324  1.1  leo 			if (rv)
    325  1.1  leo 				goto done;
    326  1.1  leo 		} else {
    327  1.1  leo 			part_t	*p;
    328  1.1  leo 			u_int	i = ++dd->nparts;
    329  1.1  leo 			dd->parts = xrealloc(dd->parts, i * sizeof *dd->parts);
    330  1.1  leo 			p = &dd->parts[--i];
    331  1.1  leo 			*((u_int32_t *)&p->id) = id << 8;
    332  1.1  leo 			p->start = part->ap_offs + rsec;
    333  1.1  leo 			p->end   = p->start + part->ap_size - 1;
    334  1.1  leo 			p->rsec  = rsec;
    335  1.1  leo 			p->rent  = part - root->ar_parts;
    336  1.1  leo 		}
    337  1.1  leo 	}
    338  1.1  leo 	dd->hdsize = root->ar_hdsize;
    339  1.1  leo 	dd->bslst  = root->ar_bslst;
    340  1.1  leo 	dd->bslend = root->ar_bslst + root->ar_bslsize - 1;
    341  1.1  leo 	rv = 0;
    342  1.1  leo done:
    343  1.1  leo 	free(root);
    344  1.1  leo 	return(rv);
    345  1.1  leo }
    346