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