3 * Copyright (c) 2012 The ChromiumOS Authors.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <arch/types.h>
34 #include <libpayload-config.h>
36 /* Endian functions from glibc 2.9 / BSD "endian.h" */
38 #if CONFIG(LP_BIG_ENDIAN)
40 #define htobe16(in) (in)
41 #define htobe32(in) (in)
42 #define htobe64(in) (in)
44 #define htole16(in) ((uint16_t)__builtin_bswap16(in))
45 #define htole32(in) ((uint32_t)__builtin_bswap32(in))
46 #define htole64(in) ((uint64_t)__builtin_bswap64(in))
48 #elif CONFIG(LP_LITTLE_ENDIAN)
50 #define htobe16(in) ((uint16_t)__builtin_bswap16(in))
51 #define htobe32(in) ((uint32_t)__builtin_bswap32(in))
52 #define htobe64(in) ((uint64_t)__builtin_bswap64(in))
54 #define htole16(in) (in)
55 #define htole32(in) (in)
56 #define htole64(in) (in)
60 #error Cant tell if the CPU is little or big endian.
62 #endif /* CONFIG_*_ENDIAN */
64 #define be16toh(in) htobe16(in)
65 #define be32toh(in) htobe32(in)
66 #define be64toh(in) htobe64(in)
68 #define le16toh(in) htole16(in)
69 #define le32toh(in) htole32(in)
70 #define le64toh(in) htole64(in)
72 #define htonw(in) htobe16(in)
73 #define htonl(in) htobe32(in)
74 #define htonll(in) htobe64(in)
76 #define ntohw(in) be16toh(in)
77 #define ntohl(in) be32toh(in)
78 #define ntohll(in) be64toh(in)
81 * Alignment-agnostic encode/decode bytestream to/from little/big endian.
84 static inline uint16_t be16dec(const void *pp
)
86 uint8_t const *p
= (uint8_t const *)pp
;
88 return (uint16_t)((p
[0] << 8) | p
[1]);
91 static inline uint32_t be32dec(const void *pp
)
93 uint8_t const *p
= (uint8_t const *)pp
;
95 return (((uint32_t)p
[0] << 24) | (uint32_t)(p
[1] << 16) |
96 (uint32_t)(p
[2] << 8) | p
[3]);
99 static inline uint64_t be64dec(const void *pp
)
101 uint8_t const *p
= (uint8_t const *)pp
;
103 return (((uint64_t)p
[0] << 56) | ((uint64_t)p
[1] << 48) |
104 ((uint64_t)p
[2] << 40) | ((uint64_t)p
[3] << 32) |
105 ((uint64_t)p
[4] << 24) | ((uint64_t)p
[5] << 16) |
106 ((uint64_t)p
[6] << 8) | p
[7]);
109 static inline uint16_t le16dec(const void *pp
)
111 uint8_t const *p
= (uint8_t const *)pp
;
113 return (uint16_t)((p
[1] << 8) | p
[0]);
116 static inline uint32_t le32dec(const void *pp
)
118 uint8_t const *p
= (uint8_t const *)pp
;
120 return ((uint32_t)(p
[3] << 24) | (uint32_t)(p
[2] << 16) |
121 (uint32_t)(p
[1] << 8) | p
[0]);
124 static inline uint64_t le64dec(const void *pp
)
126 uint8_t const *p
= (uint8_t const *)pp
;
128 return (((uint64_t)p
[7] << 56) | ((uint64_t)p
[6] << 48) |
129 ((uint64_t)p
[5] << 40) | ((uint64_t)p
[4] << 32) |
130 ((uint64_t)p
[3] << 24) | ((uint64_t)p
[2] << 16) |
131 ((uint64_t)p
[1] << 8) | p
[0]);
134 static inline void bebitenc(void *pp
, uint32_t u
, uint8_t b
)
136 uint8_t *p
= (uint8_t *)pp
;
139 for (i
= 0; i
< b
; i
++)
140 p
[(b
- 1) - i
] = (u
>> i
*8) & 0xFF;
143 static inline void be16enc(void *pp
, uint16_t u
)
148 static inline void be32enc(void *pp
, uint32_t u
)
153 static inline void be64enc(void *pp
, uint32_t u
)
158 static inline void lebitenc(void *pp
, uint32_t u
, uint8_t b
)
160 uint8_t *p
= (uint8_t *)pp
;
163 for (i
= 0; i
< b
; i
++)
164 p
[i
] = (u
>> i
*8) & 0xFF;
167 static inline void le16enc(void *pp
, uint16_t u
)
172 static inline void le32enc(void *pp
, uint32_t u
)
177 static inline void le64enc(void *pp
, uint32_t u
)
182 /* Deprecated names (not in glibc / BSD) */
183 #define htobew(in) htobe16(in)
184 #define htobel(in) htobe32(in)
185 #define htobell(in) htobe64(in)
186 #define htolew(in) htole16(in)
187 #define htolel(in) htole32(in)
188 #define htolell(in) htole64(in)
189 #define betohw(in) be16toh(in)
190 #define betohl(in) be32toh(in)
191 #define betohll(in) be64toh(in)
192 #define letohw(in) le16toh(in)
193 #define letohl(in) le32toh(in)
194 #define letohll(in) le64toh(in)
196 /* read/write with uintptr_t address */
197 #define read8p(addr) read8((void *)((uintptr_t)(addr)))
198 #define read16p(addr) read16((void *)((uintptr_t)(addr)))
199 #define read32p(addr) read32((void *)((uintptr_t)(addr)))
200 #define read64p(addr) read64((void *)((uintptr_t)(addr)))
201 #define write8p(addr, value) write8((void *)((uintptr_t)(addr)), value)
202 #define write16p(addr, value) write16((void *)((uintptr_t)(addr)), value)
203 #define write32p(addr, value) write32((void *)((uintptr_t)(addr)), value)
204 #define write64p(addr, value) write64((void *)((uintptr_t)(addr)), value)
206 /* Handy bit manipulation macros */
208 #define __clrsetbits(endian, bits, addr, clear, set) \
209 write##bits(addr, hto##endian##bits((endian##bits##toh( \
210 read##bits(addr)) & ~((uint##bits##_t)(clear))) | (set)))
212 #define clrbits_le64(addr, clear) __clrsetbits(le, 64, addr, clear, 0)
213 #define clrbits_be64(addr, clear) __clrsetbits(be, 64, addr, clear, 0)
214 #define clrbits_le32(addr, clear) __clrsetbits(le, 32, addr, clear, 0)
215 #define clrbits_be32(addr, clear) __clrsetbits(be, 32, addr, clear, 0)
216 #define clrbits_le16(addr, clear) __clrsetbits(le, 16, addr, clear, 0)
217 #define clrbits_be16(addr, clear) __clrsetbits(be, 16, addr, clear, 0)
219 #define setbits_le64(addr, set) __clrsetbits(le, 64, addr, 0, set)
220 #define setbits_be64(addr, set) __clrsetbits(be, 64, addr, 0, set)
221 #define setbits_le32(addr, set) __clrsetbits(le, 32, addr, 0, set)
222 #define setbits_be32(addr, set) __clrsetbits(be, 32, addr, 0, set)
223 #define setbits_le16(addr, set) __clrsetbits(le, 16, addr, 0, set)
224 #define setbits_be16(addr, set) __clrsetbits(be, 16, addr, 0, set)
226 #define clrsetbits_le64(addr, clear, set) __clrsetbits(le, 64, addr, clear, set)
227 #define clrsetbits_be64(addr, clear, set) __clrsetbits(be, 64, addr, clear, set)
228 #define clrsetbits_le32(addr, clear, set) __clrsetbits(le, 32, addr, clear, set)
229 #define clrsetbits_be32(addr, clear, set) __clrsetbits(be, 32, addr, clear, set)
230 #define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
231 #define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
233 #define __clrsetbits_impl(bits, addr, clear, set) write##bits(addr, \
234 (read##bits(addr) & ~((uint##bits##_t)(clear))) | (set))
236 #define clrsetbits8(addr, clear, set) __clrsetbits_impl(8, addr, clear, set)
237 #define clrsetbits16(addr, clear, set) __clrsetbits_impl(16, addr, clear, set)
238 #define clrsetbits32(addr, clear, set) __clrsetbits_impl(32, addr, clear, set)
239 #define clrsetbits64(addr, clear, set) __clrsetbits_impl(64, addr, clear, set)
241 #define setbits8(addr, set) clrsetbits8(addr, 0, set)
242 #define setbits16(addr, set) clrsetbits16(addr, 0, set)
243 #define setbits32(addr, set) clrsetbits32(addr, 0, set)
244 #define setbits64(addr, set) clrsetbits64(addr, 0, set)
246 #define clrbits8(addr, clear) clrsetbits8(addr, clear, 0)
247 #define clrbits16(addr, clear) clrsetbits16(addr, clear, 0)
248 #define clrbits32(addr, clear) clrsetbits32(addr, clear, 0)
249 #define clrbits64(addr, clear) clrsetbits64(addr, clear, 0)
251 #define clrsetbits8p(addr, clear, set) clrsetbits8((void *)((uintptr_t)(addr)), clear, set)
252 #define clrsetbits16p(addr, clear, set) clrsetbits16((void *)((uintptr_t)(addr)), clear, set)
253 #define clrsetbits32p(addr, clear, set) clrsetbits32((void *)((uintptr_t)(addr)), clear, set)
254 #define clrsetbits64p(addr, clear, set) clrsetbits64((void *)((uintptr_t)(addr)), clear, set)
256 #define setbits8p(addr, set) clrsetbits8((void *)((uintptr_t)(addr)), 0, set)
257 #define setbits16p(addr, set) clrsetbits16((void *)((uintptr_t)(addr)), 0, set)
258 #define setbits32p(addr, set) clrsetbits32((void *)((uintptr_t)(addr)), 0, set)
259 #define setbits64p(addr, set) clrsetbits64((void *)((uintptr_t)(addr)), 0, set)
261 #define clrbits8p(addr, clear) clrsetbits8((void *)((uintptr_t)(addr)), clear, 0)
262 #define clrbits16p(addr, clear) clrsetbits16((void *)((uintptr_t)(addr)), clear, 0)
263 #define clrbits32p(addr, clear) clrsetbits32((void *)((uintptr_t)(addr)), clear, 0)
264 #define clrbits64p(addr, clear) clrsetbits64((void *)((uintptr_t)(addr)), clear, 0)
266 #endif /* _ENDIAN_H_ */