1 /* $NetBSD: ukfs_disklabel.c,v 1.3 2011/02/22 15:42:15 pooka Exp $ */
2
3 /*
4 * Local copies of libutil disklabel routines. This uncouples libukfs
5 * from the NetBSD-only libutil. All identifiers are prefixed with
6 * ukfs or UKFS, otherwise the routines are the same.
7 */
8
9 /*
10 * From:
11 * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp
12 */
13
14 /*-
15 * Copyright (c) 2002 The NetBSD Foundation, Inc.
16 * All rights reserved.
17 *
18 * This code is derived from software contributed to The NetBSD Foundation
19 * by Roland C. Dowdeswell.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 #include <sys/types.h>
44
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "ukfs_int_disklabel.h"
49
50 #define SCAN_INCR 4
51
52 int
ukfs__disklabel_scan(struct ukfs__disklabel * lp,int * isswapped,char * buf,size_t buflen)53 ukfs__disklabel_scan(struct ukfs__disklabel *lp, int *isswapped,
54 char *buf, size_t buflen)
55 {
56 size_t i;
57 int imswapped;
58 uint16_t npart;
59
60 /* scan for the correct magic numbers. */
61
62 for (i=0; i <= buflen - sizeof(*lp); i += SCAN_INCR) {
63 memcpy(lp, buf + i, sizeof(*lp));
64 if (lp->d_magic == UKFS_DISKMAGIC &&
65 lp->d_magic2 == UKFS_DISKMAGIC) {
66 imswapped = 0;
67 goto sanity;
68 }
69 if (lp->d_magic == bswap32(UKFS_DISKMAGIC) &&
70 lp->d_magic2 == bswap32(UKFS_DISKMAGIC)) {
71 imswapped = 1;
72 goto sanity;
73 }
74 }
75
76 return 1;
77
78 sanity:
79 if (imswapped)
80 npart = bswap16(lp->d_npartitions);
81 else
82 npart = lp->d_npartitions;
83 /* we've found something, let's sanity check it */
84 if (npart > UKFS_MAXPARTITIONS
85 || ukfs__disklabel_dkcksum(lp, imswapped))
86 return 1;
87
88 *isswapped = imswapped;
89 return 0;
90 }
91
92
93 /*
94 * From:
95 * $NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
96 */
97
98 /*-
99 * Copyright (c) 1991, 1993
100 * The Regents of the University of California. All rights reserved.
101 *
102 * Redistribution and use in source and binary forms, with or without
103 * modification, are permitted provided that the following conditions
104 * are met:
105 * 1. Redistributions of source code must retain the above copyright
106 * notice, this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright
108 * notice, this list of conditions and the following disclaimer in the
109 * documentation and/or other materials provided with the distribution.
110 * 3. Neither the name of the University nor the names of its contributors
111 * may be used to endorse or promote products derived from this software
112 * without specific prior written permission.
113 *
114 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
115 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
117 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
118 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
119 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
120 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
121 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
122 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
123 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124 * SUCH DAMAGE.
125 */
126
127 uint16_t
ukfs__disklabel_dkcksum(struct ukfs__disklabel * lp,int imswapped)128 ukfs__disklabel_dkcksum(struct ukfs__disklabel *lp, int imswapped)
129 {
130 uint16_t *start, *end;
131 uint16_t sum;
132 uint16_t npart;
133
134 if (imswapped)
135 npart = bswap16(lp->d_npartitions);
136 else
137 npart = lp->d_npartitions;
138
139 sum = 0;
140 start = (uint16_t *)(void *)lp;
141 end = (uint16_t *)(void *)&lp->d_partitions[npart];
142 while (start < end) {
143 if (imswapped)
144 sum ^= bswap16(*start);
145 else
146 sum ^= *start;
147 start++;
148 }
149 return (sum);
150 }
151