xref: /netbsd-src/sys/arch/atari/stand/tostools/libtos/bsdlabel.c (revision e90ec2adccb33e58ef3669058f4215039a20b881)
1 /*	$NetBSD: bsdlabel.c,v 1.4 2009/03/31 11:48:15 tsutsui 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by Waldi Ravens.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include "libtos.h"
38 #include "diskio.h"
39 #include "ahdilbl.h"
40 #include "disklbl.h"
41 
42 static int	dkcksum    PROTO((struct disklabel *));
43 
44 int
bsd_getlabel(disk_t * dd,struct disklabel * dlp,u_int offset)45 bsd_getlabel(disk_t *dd, struct disklabel *dlp, u_int offset)
46 {
47 	u_char		*bblk;
48 	u_int		nsec;
49 	int		rv;
50 
51 	nsec = (BBMINSIZE + (dd->bsize - 1)) / dd->bsize;
52 	bblk = disk_read(dd, offset, nsec);
53 	if (bblk) {
54 		u_int	*end, *p;
55 
56 		end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
57 		rv  = 1;
58 		for (p = (u_int *)bblk; p < end; ++p) {
59 			struct disklabel *dl = (struct disklabel *)&p[1];
60 			if (  (  (p[0] == NBDAMAGIC && offset == 0)
61 			      || (p[0] == AHDIMAGIC && offset != 0)
62 			      || (u_char *)dl - bblk == 7168
63 			      )
64 			   && dl->d_npartitions <= MAXPARTITIONS
65 			   && dl->d_magic2 == DISKMAGIC
66 			   && dl->d_magic  == DISKMAGIC
67 			   && dkcksum(dl)  == 0
68 			   )	{
69 				memcpy(dlp, dl, sizeof(*dlp));
70 				rv = 0;
71 				break;
72 			}
73 		}
74 		free(bblk);
75 	}
76 	else rv = -1;
77 
78 	return(rv);
79 }
80 
81 static int
dkcksum(struct disklabel * dl)82 dkcksum(struct disklabel *dl)
83 {
84 	u_short	*start, *end, sum = 0;
85 
86 	start = (u_short *)dl;
87 	end   = (u_short *)&dl->d_partitions[dl->d_npartitions];
88 	while (start < end)
89 		sum ^= *start++;
90 	return(sum);
91 }
92