Home | History | Annotate | Line # | Download | only in libtos
      1 /*	$NetBSD: bsdlabel.c,v 1.5 2025/11/19 09:43:27 nia Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Waldi Ravens.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/types.h>
     31 #include <stdlib.h>
     32 #include <stdio.h>
     33 #include <string.h>
     34 #include "libtos.h"
     35 #include "diskio.h"
     36 #include "ahdilbl.h"
     37 #include "disklbl.h"
     38 
     39 static int	dkcksum    PROTO((struct disklabel *));
     40 
     41 int
     42 bsd_getlabel(disk_t *dd, struct disklabel *dlp, u_int offset)
     43 {
     44 	u_char		*bblk;
     45 	u_int		nsec;
     46 	int		rv;
     47 
     48 	nsec = (BBMINSIZE + (dd->bsize - 1)) / dd->bsize;
     49 	bblk = disk_read(dd, offset, nsec);
     50 	if (bblk) {
     51 		u_int	*end, *p;
     52 
     53 		end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
     54 		rv  = 1;
     55 		for (p = (u_int *)bblk; p < end; ++p) {
     56 			struct disklabel *dl = (struct disklabel *)&p[1];
     57 			if (  (  (p[0] == NBDAMAGIC && offset == 0)
     58 			      || (p[0] == AHDIMAGIC && offset != 0)
     59 			      || (u_char *)dl - bblk == 7168
     60 			      )
     61 			   && dl->d_npartitions <= MAXPARTITIONS
     62 			   && dl->d_magic2 == DISKMAGIC
     63 			   && dl->d_magic  == DISKMAGIC
     64 			   && dkcksum(dl)  == 0
     65 			   )	{
     66 				memcpy(dlp, dl, sizeof(*dlp));
     67 				rv = 0;
     68 				break;
     69 			}
     70 		}
     71 		free(bblk);
     72 	}
     73 	else rv = -1;
     74 
     75 	return(rv);
     76 }
     77 
     78 static int
     79 dkcksum(struct disklabel *dl)
     80 {
     81 	u_short	*start, *end, sum = 0;
     82 
     83 	start = (u_short *)dl;
     84 	end   = (u_short *)&dl->d_partitions[dl->d_npartitions];
     85 	while (start < end)
     86 		sum ^= *start++;
     87 	return(sum);
     88 }
     89