1 /* $NetBSD: v7fs_endian.c,v 1.2 2011/07/18 21:51:49 apb Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: v7fs_endian.c,v 1.2 2011/07/18 21:51:49 apb Exp $");
38 #if defined _KERNEL_OPT
43 #include "v7fs_endian.h"
44 #include "v7fs_impl.h"
51 #define bswap32pdp_le(x) \
53 ((((x) & 0xffff0000) >> 16) | \
54 (((x) & 0x0000ffff) << 16)))
56 #define bswap32pdp_be(x) \
58 ((((x) & 0xff00ff00) >> 8) | \
59 (((x) & 0x00ff00ff) << 8)))
61 static uint32_t val32_normal_order(uint32_t);
62 static uint32_t val32_reverse_order(uint32_t);
63 #if BYTE_ORDER == LITTLE_ENDIAN
64 static uint32_t val32_pdp_to_little(uint32_t);
66 static uint32_t val32_pdp_to_big(uint32_t);
68 static uint16_t val16_normal_order(uint16_t);
69 static uint16_t val16_reverse_order(uint16_t);
70 static v7fs_daddr_t
val24_reverse_order_read(uint8_t *);
71 static void val24_reverse_order_write(v7fs_daddr_t
, uint8_t *);
72 static v7fs_daddr_t
val24_pdp_read(uint8_t *);
73 static void val24_pdp_write(v7fs_daddr_t
, uint8_t *);
76 val32_normal_order(uint32_t v
)
83 val32_reverse_order(uint32_t v
)
88 #if BYTE_ORDER == LITTLE_ENDIAN
90 val32_pdp_to_little(uint32_t v
)
93 return bswap32pdp_le(v
);
97 val32_pdp_to_big(uint32_t v
)
100 return bswap32pdp_be(v
);
104 val16_normal_order(uint16_t v
)
111 val16_reverse_order(uint16_t v
)
118 val24_reverse_order_read(uint8_t *a
)
120 #if BYTE_ORDER == LITTLE_ENDIAN
121 return (a
[0] << 16) | (a
[1] << 8) | a
[2];
123 return (a
[2] << 16) | (a
[1] << 8) | a
[0];
128 val24_reverse_order_write(v7fs_daddr_t addr
, uint8_t *a
)
130 #if BYTE_ORDER == LITTLE_ENDIAN
131 a
[0] = (addr
>> 16) & 0xff;
132 a
[1] = (addr
>> 8) & 0xff;
136 a
[1] = (addr
>> 8) & 0xff;
137 a
[2] = (addr
>> 16) & 0xff;
142 val24_pdp_read(uint8_t *a
)
145 return (a
[0] << 16) | a
[1] | (a
[2] << 8);
149 val24_pdp_write(v7fs_daddr_t addr
, uint8_t *a
)
152 a
[0] = (addr
>> 16) & 0xff;
154 a
[2] = (addr
>> 8) & 0xff;
158 v7fs_endian_init(struct v7fs_self
*fs
)
160 struct endian_conversion_ops
*ops
= &fs
->val
;
164 #if BYTE_ORDER == LITTLE_ENDIAN
166 ops
->conv32
= val32_normal_order
;
167 ops
->conv16
= val16_normal_order
;
168 ops
->conv24read
= val24_normal_order_read
;
169 ops
->conv24write
= val24_normal_order_write
;
172 ops
->conv32
= val32_reverse_order
;
173 ops
->conv16
= val16_reverse_order
;
174 ops
->conv24read
= val24_reverse_order_read
;
175 ops
->conv24write
= val24_reverse_order_write
;
178 ops
->conv32
= val32_pdp_to_little
;
179 ops
->conv16
= val16_normal_order
;
180 ops
->conv24read
= val24_pdp_read
;
181 ops
->conv24write
= val24_pdp_write
;
183 #else /* BIG_ENDIAN */
185 ops
->conv32
= val32_reverse_order
;
186 ops
->conv16
= val16_reverse_order
;
187 ops
->conv24read
= val24_reverse_order_read
;
188 ops
->conv24write
= val24_reverse_order_write
;
191 ops
->conv32
= val32_normal_order
;
192 ops
->conv16
= val16_normal_order
;
193 ops
->conv24read
= val24_normal_order_read
;
194 ops
->conv24write
= val24_normal_order_write
;
197 ops
->conv32
= val32_pdp_to_big
;
198 ops
->conv16
= val16_reverse_order
;
199 ops
->conv24read
= val24_pdp_read
;
200 ops
->conv24write
= val24_pdp_write
;
207 val24_normal_order_read(uint8_t *a
)
209 /*(v7fs_daddr_t)cast is required for int 16bit system. */
210 #if BYTE_ORDER == LITTLE_ENDIAN
211 return ((v7fs_daddr_t
)a
[2] << 16) | ((v7fs_daddr_t
)a
[1] << 8) |
214 return ((v7fs_daddr_t
)a
[0] << 16) | ((v7fs_daddr_t
)a
[1] << 8) |
220 val24_normal_order_write(v7fs_daddr_t addr
, uint8_t *a
)
222 #if BYTE_ORDER == LITTLE_ENDIAN
224 a
[1] = (addr
>> 8) & 0xff;
225 a
[2] = (addr
>> 16) & 0xff;
227 a
[0] = (addr
>> 16) & 0xff;
228 a
[1] = (addr
>> 8) & 0xff;