v7fs_endian.c revision 1.1 1 1.1 uch /* $NetBSD: v7fs_endian.c,v 1.1 2011/06/27 11:52:24 uch Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch *
19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uch */
31 1.1 uch
32 1.1 uch #include <sys/cdefs.h>
33 1.1 uch __KERNEL_RCSID(0, "$NetBSD: v7fs_endian.c,v 1.1 2011/06/27 11:52:24 uch Exp $");
34 1.1 uch #if defined _KERNEL_OPT
35 1.1 uch #include "opt_v7fs.h"
36 1.1 uch #endif
37 1.1 uch
38 1.1 uch #include "v7fs.h"
39 1.1 uch #include "v7fs_endian.h"
40 1.1 uch #include "v7fs_impl.h"
41 1.1 uch
42 1.1 uch #ifndef BYTE_ORDER
43 1.1 uch #error
44 1.1 uch #endif
45 1.1 uch
46 1.1 uch /* PDP to Little */
47 1.1 uch #define bswap32pdp_le(x) \
48 1.1 uch ((uint32_t) \
49 1.1 uch ((((x) & 0xffff0000) >> 16) | \
50 1.1 uch (((x) & 0x0000ffff) << 16)))
51 1.1 uch /* PDP to Big */
52 1.1 uch #define bswap32pdp_be(x) \
53 1.1 uch ((uint32_t) \
54 1.1 uch ((((x) & 0xff00ff00) >> 8) | \
55 1.1 uch (((x) & 0x00ff00ff) << 8)))
56 1.1 uch #ifdef V7FS_EI
57 1.1 uch static uint32_t val32_normal_order(uint32_t);
58 1.1 uch static uint32_t val32_reverse_order(uint32_t);
59 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
60 1.1 uch static uint32_t val32_pdp_to_little(uint32_t);
61 1.1 uch #else
62 1.1 uch static uint32_t val32_pdp_to_big(uint32_t);
63 1.1 uch #endif
64 1.1 uch static uint16_t val16_normal_order(uint16_t);
65 1.1 uch static uint16_t val16_reverse_order(uint16_t);
66 1.1 uch static v7fs_daddr_t val24_reverse_order_read(uint8_t *);
67 1.1 uch static void val24_reverse_order_write(v7fs_daddr_t, uint8_t *);
68 1.1 uch static v7fs_daddr_t val24_pdp_read(uint8_t *);
69 1.1 uch static void val24_pdp_write(v7fs_daddr_t, uint8_t *);
70 1.1 uch
71 1.1 uch static uint32_t
72 1.1 uch val32_normal_order(uint32_t v)
73 1.1 uch {
74 1.1 uch
75 1.1 uch return v;
76 1.1 uch }
77 1.1 uch
78 1.1 uch static uint32_t
79 1.1 uch val32_reverse_order(uint32_t v)
80 1.1 uch {
81 1.1 uch
82 1.1 uch return bswap32(v);
83 1.1 uch }
84 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
85 1.1 uch static uint32_t
86 1.1 uch val32_pdp_to_little(uint32_t v)
87 1.1 uch {
88 1.1 uch
89 1.1 uch return bswap32pdp_le(v);
90 1.1 uch }
91 1.1 uch #else
92 1.1 uch static uint32_t
93 1.1 uch val32_pdp_to_big(uint32_t v)
94 1.1 uch {
95 1.1 uch
96 1.1 uch return bswap32pdp_be(v);
97 1.1 uch }
98 1.1 uch #endif
99 1.1 uch static uint16_t
100 1.1 uch val16_normal_order(uint16_t v)
101 1.1 uch {
102 1.1 uch
103 1.1 uch return v;
104 1.1 uch }
105 1.1 uch
106 1.1 uch static uint16_t
107 1.1 uch val16_reverse_order(uint16_t v)
108 1.1 uch {
109 1.1 uch
110 1.1 uch return bswap16(v);
111 1.1 uch }
112 1.1 uch
113 1.1 uch static v7fs_daddr_t
114 1.1 uch val24_reverse_order_read(uint8_t *a)
115 1.1 uch {
116 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
117 1.1 uch return (a[0] << 16) | (a[1] << 8) | a[2];
118 1.1 uch #else
119 1.1 uch return (a[2] << 16) | (a[1] << 8) | a[0];
120 1.1 uch #endif
121 1.1 uch }
122 1.1 uch
123 1.1 uch static void
124 1.1 uch val24_reverse_order_write(v7fs_daddr_t addr, uint8_t *a)
125 1.1 uch {
126 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
127 1.1 uch a[0] = (addr >> 16) & 0xff;
128 1.1 uch a[1] = (addr >> 8) & 0xff;
129 1.1 uch a[2] = addr & 0xff;
130 1.1 uch #else
131 1.1 uch a[0] = addr & 0xff;
132 1.1 uch a[1] = (addr >> 8) & 0xff;
133 1.1 uch a[2] = (addr >> 16) & 0xff;
134 1.1 uch #endif
135 1.1 uch }
136 1.1 uch
137 1.1 uch static v7fs_daddr_t
138 1.1 uch val24_pdp_read(uint8_t *a)
139 1.1 uch {
140 1.1 uch
141 1.1 uch return (a[0] << 16) | a[1] | (a[2] << 8);
142 1.1 uch }
143 1.1 uch
144 1.1 uch static void
145 1.1 uch val24_pdp_write(v7fs_daddr_t addr, uint8_t *a)
146 1.1 uch {
147 1.1 uch
148 1.1 uch a[0] = (addr >> 16) & 0xff;
149 1.1 uch a[1] = addr & 0xff;
150 1.1 uch a[2] = (addr >> 8) & 0xff;
151 1.1 uch }
152 1.1 uch
153 1.1 uch void
154 1.1 uch v7fs_endian_init(struct v7fs_self *fs)
155 1.1 uch {
156 1.1 uch struct endian_conversion_ops *ops = &fs->val;
157 1.1 uch
158 1.1 uch switch (fs->endian)
159 1.1 uch {
160 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
161 1.1 uch case LITTLE_ENDIAN:
162 1.1 uch ops->conv32 = val32_normal_order;
163 1.1 uch ops->conv16 = val16_normal_order;
164 1.1 uch ops->conv24read = val24_normal_order_read;
165 1.1 uch ops->conv24write = val24_normal_order_write;
166 1.1 uch break;
167 1.1 uch case BIG_ENDIAN:
168 1.1 uch ops->conv32 = val32_reverse_order;
169 1.1 uch ops->conv16 = val16_reverse_order;
170 1.1 uch ops->conv24read = val24_reverse_order_read;
171 1.1 uch ops->conv24write = val24_reverse_order_write;
172 1.1 uch break;
173 1.1 uch case PDP_ENDIAN:
174 1.1 uch ops->conv32 = val32_pdp_to_little;
175 1.1 uch ops->conv16 = val16_normal_order;
176 1.1 uch ops->conv24read = val24_pdp_read;
177 1.1 uch ops->conv24write = val24_pdp_write;
178 1.1 uch break;
179 1.1 uch #else /* BIG_ENDIAN */
180 1.1 uch case LITTLE_ENDIAN:
181 1.1 uch ops->conv32 = val32_reverse_order;
182 1.1 uch ops->conv16 = val16_reverse_order;
183 1.1 uch ops->conv24read = val24_reverse_order_read;
184 1.1 uch ops->conv24write = val24_reverse_order_write;
185 1.1 uch break;
186 1.1 uch case BIG_ENDIAN:
187 1.1 uch ops->conv32 = val32_normal_order;
188 1.1 uch ops->conv16 = val16_normal_order;
189 1.1 uch ops->conv24read = val24_normal_order_read;
190 1.1 uch ops->conv24write = val24_normal_order_write;
191 1.1 uch break;
192 1.1 uch case PDP_ENDIAN:
193 1.1 uch ops->conv32 = val32_pdp_to_big;
194 1.1 uch ops->conv16 = val16_reverse_order;
195 1.1 uch ops->conv24read = val24_pdp_read;
196 1.1 uch ops->conv24write = val24_pdp_write;
197 1.1 uch break;
198 1.1 uch #endif
199 1.1 uch }
200 1.1 uch }
201 1.1 uch #endif /* V7FS_EI */
202 1.1 uch v7fs_daddr_t
203 1.1 uch val24_normal_order_read(uint8_t *a)
204 1.1 uch {
205 1.1 uch /*(v7fs_daddr_t)cast is required for int 16bit system. */
206 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
207 1.1 uch return ((v7fs_daddr_t)a[2] << 16) | ((v7fs_daddr_t)a[1] << 8) |
208 1.1 uch (v7fs_daddr_t)a[0];
209 1.1 uch #else
210 1.1 uch return ((v7fs_daddr_t)a[0] << 16) | ((v7fs_daddr_t)a[1] << 8) |
211 1.1 uch (v7fs_daddr_t)a[2];
212 1.1 uch #endif
213 1.1 uch }
214 1.1 uch
215 1.1 uch void
216 1.1 uch val24_normal_order_write(v7fs_daddr_t addr, uint8_t *a)
217 1.1 uch {
218 1.1 uch #if BYTE_ORDER == LITTLE_ENDIAN
219 1.1 uch a[0] = addr & 0xff;
220 1.1 uch a[1] = (addr >> 8) & 0xff;
221 1.1 uch a[2] = (addr >> 16) & 0xff;
222 1.1 uch #else
223 1.1 uch a[0] = (addr >> 16) & 0xff;
224 1.1 uch a[1] = (addr >> 8) & 0xff;
225 1.1 uch a[2] = addr & 0xff;
226 1.1 uch #endif
227 1.1 uch }
228