xref: /netbsd-src/sys/fs/v7fs/v7fs_endian.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: v7fs_endian.c,v 1.1 2011/06/27 11:52:24 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: v7fs_endian.c,v 1.1 2011/06/27 11:52:24 uch Exp $");
34 #if defined _KERNEL_OPT
35 #include "opt_v7fs.h"
36 #endif
37 
38 #include "v7fs.h"
39 #include "v7fs_endian.h"
40 #include "v7fs_impl.h"
41 
42 #ifndef BYTE_ORDER
43 #error
44 #endif
45 
46 /* PDP to Little */
47 #define	bswap32pdp_le(x)			\
48 	((uint32_t)				\
49 	    ((((x) & 0xffff0000) >> 16) |	\
50 		(((x) & 0x0000ffff) << 16)))
51 /* PDP to Big */
52 #define	bswap32pdp_be(x)			\
53 	((uint32_t)				\
54 	    ((((x) & 0xff00ff00) >> 8) |	\
55 		(((x) & 0x00ff00ff) <<  8)))
56 #ifdef V7FS_EI
57 static uint32_t val32_normal_order(uint32_t);
58 static uint32_t val32_reverse_order(uint32_t);
59 #if BYTE_ORDER == LITTLE_ENDIAN
60 static uint32_t val32_pdp_to_little(uint32_t);
61 #else
62 static uint32_t val32_pdp_to_big(uint32_t);
63 #endif
64 static uint16_t val16_normal_order(uint16_t);
65 static uint16_t val16_reverse_order(uint16_t);
66 static v7fs_daddr_t val24_reverse_order_read(uint8_t *);
67 static void val24_reverse_order_write(v7fs_daddr_t, uint8_t *);
68 static v7fs_daddr_t val24_pdp_read(uint8_t *);
69 static void val24_pdp_write(v7fs_daddr_t, uint8_t *);
70 
71 static uint32_t
72 val32_normal_order(uint32_t v)
73 {
74 
75 	return v;
76 }
77 
78 static uint32_t
79 val32_reverse_order(uint32_t v)
80 {
81 
82 	return bswap32(v);
83 }
84 #if BYTE_ORDER == LITTLE_ENDIAN
85 static uint32_t
86 val32_pdp_to_little(uint32_t v)
87 {
88 
89 	return bswap32pdp_le(v);
90 }
91 #else
92 static uint32_t
93 val32_pdp_to_big(uint32_t v)
94 {
95 
96 	return bswap32pdp_be(v);
97 }
98 #endif
99 static uint16_t
100 val16_normal_order(uint16_t v)
101 {
102 
103 	return v;
104 }
105 
106 static uint16_t
107 val16_reverse_order(uint16_t v)
108 {
109 
110 	return bswap16(v);
111 }
112 
113 static v7fs_daddr_t
114 val24_reverse_order_read(uint8_t *a)
115 {
116 #if BYTE_ORDER == LITTLE_ENDIAN
117 	return (a[0] << 16) | (a[1] << 8) | a[2];
118 #else
119 	return (a[2] << 16) | (a[1] << 8) | a[0];
120 #endif
121 }
122 
123 static void
124 val24_reverse_order_write(v7fs_daddr_t addr, uint8_t *a)
125 {
126 #if BYTE_ORDER == LITTLE_ENDIAN
127 	a[0] = (addr >> 16) & 0xff;
128 	a[1] = (addr >> 8) & 0xff;
129 	a[2] = addr & 0xff;
130 #else
131 	a[0] = addr & 0xff;
132 	a[1] = (addr >> 8) & 0xff;
133 	a[2] = (addr >> 16) & 0xff;
134 #endif
135 }
136 
137 static v7fs_daddr_t
138 val24_pdp_read(uint8_t *a)
139 {
140 
141 	return (a[0] << 16) | a[1] | (a[2] << 8);
142 }
143 
144 static void
145 val24_pdp_write(v7fs_daddr_t addr, uint8_t *a)
146 {
147 
148 	a[0] = (addr >> 16) & 0xff;
149 	a[1] = addr & 0xff;
150 	a[2] = (addr >> 8) & 0xff;
151 }
152 
153 void
154 v7fs_endian_init(struct v7fs_self *fs)
155 {
156 	struct endian_conversion_ops *ops = &fs->val;
157 
158 	switch (fs->endian)
159 	{
160 #if BYTE_ORDER == LITTLE_ENDIAN
161 	case LITTLE_ENDIAN:
162 		ops->conv32 = val32_normal_order;
163 		ops->conv16 = val16_normal_order;
164 		ops->conv24read = val24_normal_order_read;
165 		ops->conv24write = val24_normal_order_write;
166 		break;
167 	case BIG_ENDIAN:
168 		ops->conv32 = val32_reverse_order;
169 		ops->conv16 = val16_reverse_order;
170 		ops->conv24read = val24_reverse_order_read;
171 		ops->conv24write = val24_reverse_order_write;
172 		break;
173 	case PDP_ENDIAN:
174 		ops->conv32 = val32_pdp_to_little;
175 		ops->conv16 = val16_normal_order;
176 		ops->conv24read = val24_pdp_read;
177 		ops->conv24write = val24_pdp_write;
178 		break;
179 #else /* BIG_ENDIAN */
180 	case LITTLE_ENDIAN:
181 		ops->conv32 = val32_reverse_order;
182 		ops->conv16 = val16_reverse_order;
183 		ops->conv24read = val24_reverse_order_read;
184 		ops->conv24write = val24_reverse_order_write;
185 		break;
186 	case BIG_ENDIAN:
187 		ops->conv32 = val32_normal_order;
188 		ops->conv16 = val16_normal_order;
189 		ops->conv24read = val24_normal_order_read;
190 		ops->conv24write = val24_normal_order_write;
191 		break;
192 	case PDP_ENDIAN:
193 		ops->conv32 = val32_pdp_to_big;
194 		ops->conv16 = val16_reverse_order;
195 		ops->conv24read = val24_pdp_read;
196 		ops->conv24write = val24_pdp_write;
197 		break;
198 #endif
199 	}
200 }
201 #endif	/* V7FS_EI */
202 v7fs_daddr_t
203 val24_normal_order_read(uint8_t *a)
204 {
205 	/*(v7fs_daddr_t)cast is required for int 16bit system. */
206 #if BYTE_ORDER == LITTLE_ENDIAN
207 	return ((v7fs_daddr_t)a[2] << 16) | ((v7fs_daddr_t)a[1] << 8) |
208 	    (v7fs_daddr_t)a[0];
209 #else
210 	return ((v7fs_daddr_t)a[0] << 16) | ((v7fs_daddr_t)a[1] << 8) |
211 	    (v7fs_daddr_t)a[2];
212 #endif
213 }
214 
215 void
216 val24_normal_order_write(v7fs_daddr_t addr, uint8_t *a)
217 {
218 #if BYTE_ORDER == LITTLE_ENDIAN
219 	a[0] = addr & 0xff;
220 	a[1] = (addr >> 8) & 0xff;
221 	a[2] = (addr >> 16) & 0xff;
222 #else
223 	a[0] = (addr >> 16) & 0xff;
224 	a[1] = (addr >> 8) & 0xff;
225 	a[2] = addr & 0xff;
226 #endif
227 }
228