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