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 uint16_t le16dec(const void *pp
)
101 uint8_t const *p
= (uint8_t const *)pp
;
103 return (uint16_t)((p
[1] << 8) | p
[0]);
106 static inline uint32_t le32dec(const void *pp
)
108 uint8_t const *p
= (uint8_t const *)pp
;
110 return ((uint32_t)(p
[3] << 24) | (uint32_t)(p
[2] << 16) |
111 (uint32_t)(p
[1] << 8) | p
[0]);
114 static inline void bebitenc(void *pp
, uint32_t u
, uint8_t b
)
116 uint8_t *p
= (uint8_t *)pp
;
119 for (i
= 0; i
< b
; i
++)
120 p
[(b
- 1) - i
] = (u
>> i
*8) & 0xFF;
123 static inline void be16enc(void *pp
, uint16_t u
)
128 static inline void be32enc(void *pp
, uint32_t u
)
133 static inline void lebitenc(void *pp
, uint32_t u
, uint8_t b
)
135 uint8_t *p
= (uint8_t *)pp
;
138 for (i
= 0; i
< b
; i
++)
139 p
[i
] = (u
>> i
*8) & 0xFF;
142 static inline void le16enc(void *pp
, uint16_t u
)
147 static inline void le32enc(void *pp
, uint32_t u
)
152 /* Deprecated names (not in glibc / BSD) */
153 #define htobew(in) htobe16(in)
154 #define htobel(in) htobe32(in)
155 #define htobell(in) htobe64(in)
156 #define htolew(in) htole16(in)
157 #define htolel(in) htole32(in)
158 #define htolell(in) htole64(in)
159 #define betohw(in) be16toh(in)
160 #define betohl(in) be32toh(in)
161 #define betohll(in) be64toh(in)
162 #define letohw(in) le16toh(in)
163 #define letohl(in) le32toh(in)
164 #define letohll(in) le64toh(in)
166 /* Handy bit manipulation macros */
168 #define __clrsetbits(endian, bits, addr, clear, set) \
169 write##bits(addr, hto##endian##bits((endian##bits##toh( \
170 read##bits(addr)) & ~((uint##bits##_t)(clear))) | (set)))
172 #define clrbits_le64(addr, clear) __clrsetbits(le, 64, addr, clear, 0)
173 #define clrbits_be64(addr, clear) __clrsetbits(be, 64, addr, clear, 0)
174 #define clrbits_le32(addr, clear) __clrsetbits(le, 32, addr, clear, 0)
175 #define clrbits_be32(addr, clear) __clrsetbits(be, 32, addr, clear, 0)
176 #define clrbits_le16(addr, clear) __clrsetbits(le, 16, addr, clear, 0)
177 #define clrbits_be16(addr, clear) __clrsetbits(be, 16, addr, clear, 0)
179 #define setbits_le64(addr, set) __clrsetbits(le, 64, addr, 0, set)
180 #define setbits_be64(addr, set) __clrsetbits(be, 64, addr, 0, set)
181 #define setbits_le32(addr, set) __clrsetbits(le, 32, addr, 0, set)
182 #define setbits_be32(addr, set) __clrsetbits(be, 32, addr, 0, set)
183 #define setbits_le16(addr, set) __clrsetbits(le, 16, addr, 0, set)
184 #define setbits_be16(addr, set) __clrsetbits(be, 16, addr, 0, set)
186 #define clrsetbits_le64(addr, clear, set) __clrsetbits(le, 64, addr, clear, set)
187 #define clrsetbits_be64(addr, clear, set) __clrsetbits(be, 64, addr, clear, set)
188 #define clrsetbits_le32(addr, clear, set) __clrsetbits(le, 32, addr, clear, set)
189 #define clrsetbits_be32(addr, clear, set) __clrsetbits(be, 32, addr, clear, set)
190 #define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
191 #define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
193 #define __clrsetbits_impl(bits, addr, clear, set) write##bits(addr, \
194 (read##bits(addr) & ~((uint##bits##_t)(clear))) | (set))
196 #define clrsetbits8(addr, clear, set) __clrsetbits_impl(8, addr, clear, set)
197 #define clrsetbits16(addr, clear, set) __clrsetbits_impl(16, addr, clear, set)
198 #define clrsetbits32(addr, clear, set) __clrsetbits_impl(32, addr, clear, set)
199 #define clrsetbits64(addr, clear, set) __clrsetbits_impl(64, addr, clear, set)
201 #define setbits8(addr, set) clrsetbits8(addr, 0, set)
202 #define setbits16(addr, set) clrsetbits16(addr, 0, set)
203 #define setbits32(addr, set) clrsetbits32(addr, 0, set)
204 #define setbits64(addr, set) clrsetbits64(addr, 0, set)
206 #define clrbits8(addr, clear) clrsetbits8(addr, clear, 0)
207 #define clrbits16(addr, clear) clrsetbits16(addr, clear, 0)
208 #define clrbits32(addr, clear) clrsetbits32(addr, clear, 0)
209 #define clrbits64(addr, clear) clrsetbits64(addr, clear, 0)
211 #endif /* _ENDIAN_H_ */