xref: /netbsd-src/sys/arch/atari/stand/tostools/libtos/bsdlabel.c (revision e90ec2adccb33e58ef3669058f4215039a20b881)
1*e90ec2adStsutsui /*	$NetBSD: bsdlabel.c,v 1.4 2009/03/31 11:48:15 tsutsui Exp $	*/
27b23fbe2Sleo 
37b23fbe2Sleo /*
47b23fbe2Sleo  * Copyright (c) 1995 Waldi Ravens.
57b23fbe2Sleo  * All rights reserved.
67b23fbe2Sleo  *
77b23fbe2Sleo  * Redistribution and use in source and binary forms, with or without
87b23fbe2Sleo  * modification, are permitted provided that the following conditions
97b23fbe2Sleo  * are met:
107b23fbe2Sleo  * 1. Redistributions of source code must retain the above copyright
117b23fbe2Sleo  *    notice, this list of conditions and the following disclaimer.
127b23fbe2Sleo  * 2. Redistributions in binary form must reproduce the above copyright
137b23fbe2Sleo  *    notice, this list of conditions and the following disclaimer in the
147b23fbe2Sleo  *    documentation and/or other materials provided with the distribution.
157b23fbe2Sleo  * 3. All advertising materials mentioning features or use of this software
167b23fbe2Sleo  *    must display the following acknowledgement:
177b23fbe2Sleo  *        This product includes software developed by Waldi Ravens.
187b23fbe2Sleo  * 4. The name of the author may not be used to endorse or promote products
197b23fbe2Sleo  *    derived from this software without specific prior written permission.
207b23fbe2Sleo  *
217b23fbe2Sleo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227b23fbe2Sleo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237b23fbe2Sleo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247b23fbe2Sleo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257b23fbe2Sleo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267b23fbe2Sleo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277b23fbe2Sleo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287b23fbe2Sleo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297b23fbe2Sleo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307b23fbe2Sleo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
317b23fbe2Sleo  */
327b23fbe2Sleo 
337b23fbe2Sleo #include <sys/types.h>
347b23fbe2Sleo #include <stdlib.h>
357b23fbe2Sleo #include <stdio.h>
367b23fbe2Sleo #include <string.h>
377b23fbe2Sleo #include "libtos.h"
387b23fbe2Sleo #include "diskio.h"
397b23fbe2Sleo #include "ahdilbl.h"
407b23fbe2Sleo #include "disklbl.h"
417b23fbe2Sleo 
427b23fbe2Sleo static int	dkcksum    PROTO((struct disklabel *));
437b23fbe2Sleo 
447b23fbe2Sleo int
bsd_getlabel(disk_t * dd,struct disklabel * dlp,u_int offset)45454af1c0Sdsl bsd_getlabel(disk_t *dd, struct disklabel *dlp, u_int offset)
467b23fbe2Sleo {
477b23fbe2Sleo 	u_char		*bblk;
487b23fbe2Sleo 	u_int		nsec;
497b23fbe2Sleo 	int		rv;
507b23fbe2Sleo 
517b23fbe2Sleo 	nsec = (BBMINSIZE + (dd->bsize - 1)) / dd->bsize;
527b23fbe2Sleo 	bblk = disk_read(dd, offset, nsec);
537b23fbe2Sleo 	if (bblk) {
547b23fbe2Sleo 		u_int	*end, *p;
557b23fbe2Sleo 
567b23fbe2Sleo 		end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
577b23fbe2Sleo 		rv  = 1;
587b23fbe2Sleo 		for (p = (u_int *)bblk; p < end; ++p) {
597b23fbe2Sleo 			struct disklabel *dl = (struct disklabel *)&p[1];
607b23fbe2Sleo 			if (  (  (p[0] == NBDAMAGIC && offset == 0)
617b23fbe2Sleo 			      || (p[0] == AHDIMAGIC && offset != 0)
627b23fbe2Sleo 			      || (u_char *)dl - bblk == 7168
637b23fbe2Sleo 			      )
647b23fbe2Sleo 			   && dl->d_npartitions <= MAXPARTITIONS
657b23fbe2Sleo 			   && dl->d_magic2 == DISKMAGIC
667b23fbe2Sleo 			   && dl->d_magic  == DISKMAGIC
677b23fbe2Sleo 			   && dkcksum(dl)  == 0
687b23fbe2Sleo 			   )	{
69e2cb8590Scegger 				memcpy(dlp, dl, sizeof(*dlp));
707b23fbe2Sleo 				rv = 0;
717b23fbe2Sleo 				break;
727b23fbe2Sleo 			}
737b23fbe2Sleo 		}
747b23fbe2Sleo 		free(bblk);
757b23fbe2Sleo 	}
767b23fbe2Sleo 	else rv = -1;
777b23fbe2Sleo 
787b23fbe2Sleo 	return(rv);
797b23fbe2Sleo }
807b23fbe2Sleo 
817b23fbe2Sleo static int
dkcksum(struct disklabel * dl)82454af1c0Sdsl dkcksum(struct disklabel *dl)
837b23fbe2Sleo {
847b23fbe2Sleo 	u_short	*start, *end, sum = 0;
857b23fbe2Sleo 
867b23fbe2Sleo 	start = (u_short *)dl;
877b23fbe2Sleo 	end   = (u_short *)&dl->d_partitions[dl->d_npartitions];
887b23fbe2Sleo 	while (start < end)
897b23fbe2Sleo 		sum ^= *start++;
907b23fbe2Sleo 	return(sum);
917b23fbe2Sleo }
92