xref: /original-bsd/sys/stand/disklabel.c (revision 0999a820)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)disklabel.c	8.1 (Berkeley) 06/11/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/disklabel.h>
12 
13 char *
14 getdisklabel(buf, lp)
15 	const char *buf;
16 	struct disklabel *lp;
17 {
18 	register struct buf *bp;
19 	struct disklabel *dlp, *elp;
20 	char *msg = (char *)0;
21 
22 	elp = (struct disklabel *)(buf + DEV_BSIZE - sizeof(*dlp));
23 	for (dlp = (struct disklabel *)buf; dlp <= elp;
24 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
25 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
26 			if (msg == (char *)0)
27 				msg = "no disk label";
28 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
29 			   dkcksum(dlp) != 0)
30 			msg = "disk label corrupted";
31 		else {
32 			*lp = *dlp;
33 			msg = (char *)0;
34 			break;
35 		}
36 	}
37 	return (msg);
38 }
39 
40 /*
41  * Compute checksum for disk label.
42  */
43 dkcksum(lp)
44 	register struct disklabel *lp;
45 {
46 	register u_short *start, *end;
47 	register u_short sum = 0;
48 
49 	start = (u_short *)lp;
50 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
51 	while (start < end)
52 		sum ^= *start++;
53 	return (sum);
54 }
55