blob: 934bb6abba7eeabdf9deed142f7a5556e9bf660f [file] [log] [blame]
Gabe Black0af03d22012-03-19 03:06:46 -07001/*
Gabe Black7c7b5ff2013-11-23 00:38:49 -08002 *
Jon Murphyc4e90452022-06-28 10:36:23 -06003 * Copyright (c) 2012 The ChromiumOS Authors.
Gabe Black0af03d22012-03-19 03:06:46 -07004 *
Gabe Black7c7b5ff2013-11-23 00:38:49 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
Gabe Black0af03d22012-03-19 03:06:46 -070015 *
Gabe Black7c7b5ff2013-11-23 00:38:49 -080016 * 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
26 * SUCH DAMAGE.
Gabe Black0af03d22012-03-19 03:06:46 -070027 */
28
29#ifndef _ENDIAN_H_
30#define _ENDIAN_H_
31
Julius Werner8a1d11f2014-07-17 10:43:15 -070032#include <arch/io.h>
Gabe Black0af03d22012-03-19 03:06:46 -070033#include <arch/types.h>
34#include <libpayload-config.h>
35
Hung-Te Line112b742013-03-13 18:08:29 +080036/* Endian functions from glibc 2.9 / BSD "endian.h" */
Gabe Black0af03d22012-03-19 03:06:46 -070037
Julius Wernereab2a292019-03-05 16:55:15 -080038#if CONFIG(LP_BIG_ENDIAN)
Gabe Black0af03d22012-03-19 03:06:46 -070039
Hung-Te Line112b742013-03-13 18:08:29 +080040#define htobe16(in) (in)
41#define htobe32(in) (in)
42#define htobe64(in) (in)
Gabe Black0af03d22012-03-19 03:06:46 -070043
Julius Werner879ea7f2019-11-28 12:53:43 -080044#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))
Gabe Black0af03d22012-03-19 03:06:46 -070047
Julius Wernereab2a292019-03-05 16:55:15 -080048#elif CONFIG(LP_LITTLE_ENDIAN)
Gabe Black0af03d22012-03-19 03:06:46 -070049
Julius Werner879ea7f2019-11-28 12:53:43 -080050#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))
Gabe Black0af03d22012-03-19 03:06:46 -070053
Hung-Te Line112b742013-03-13 18:08:29 +080054#define htole16(in) (in)
55#define htole32(in) (in)
56#define htole64(in) (in)
Gabe Black0af03d22012-03-19 03:06:46 -070057
58#else
59
60#error Cant tell if the CPU is little or big endian.
61
Edward O'Callaghan6bedc272014-02-12 21:53:44 +110062#endif /* CONFIG_*_ENDIAN */
Gabe Black0af03d22012-03-19 03:06:46 -070063
Hung-Te Line112b742013-03-13 18:08:29 +080064#define be16toh(in) htobe16(in)
65#define be32toh(in) htobe32(in)
66#define be64toh(in) htobe64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070067
Hung-Te Line112b742013-03-13 18:08:29 +080068#define le16toh(in) htole16(in)
69#define le32toh(in) htole32(in)
70#define le64toh(in) htole64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070071
Hung-Te Line112b742013-03-13 18:08:29 +080072#define htonw(in) htobe16(in)
73#define htonl(in) htobe32(in)
74#define htonll(in) htobe64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070075
Hung-Te Line112b742013-03-13 18:08:29 +080076#define ntohw(in) be16toh(in)
77#define ntohl(in) be32toh(in)
78#define ntohll(in) be64toh(in)
79
Edward O'Callaghan6bedc272014-02-12 21:53:44 +110080/*
81 * Alignment-agnostic encode/decode bytestream to/from little/big endian.
82 */
83
84static inline uint16_t be16dec(const void *pp)
85{
86 uint8_t const *p = (uint8_t const *)pp;
87
Patrick Georgic34ebab2019-11-25 16:42:56 +010088 return (uint16_t)((p[0] << 8) | p[1]);
Edward O'Callaghan6bedc272014-02-12 21:53:44 +110089}
90
91static inline uint32_t be32dec(const void *pp)
92{
93 uint8_t const *p = (uint8_t const *)pp;
94
Patrick Georgic34ebab2019-11-25 16:42:56 +010095 return (((uint32_t)p[0] << 24) | (uint32_t)(p[1] << 16) |
96 (uint32_t)(p[2] << 8) | p[3]);
Edward O'Callaghan6bedc272014-02-12 21:53:44 +110097}
98
99static inline uint16_t le16dec(const void *pp)
100{
101 uint8_t const *p = (uint8_t const *)pp;
102
Patrick Georgic34ebab2019-11-25 16:42:56 +0100103 return (uint16_t)((p[1] << 8) | p[0]);
Edward O'Callaghan6bedc272014-02-12 21:53:44 +1100104}
105
106static inline uint32_t le32dec(const void *pp)
107{
108 uint8_t const *p = (uint8_t const *)pp;
109
Patrick Georgic34ebab2019-11-25 16:42:56 +0100110 return ((uint32_t)(p[3] << 24) | (uint32_t)(p[2] << 16) |
111 (uint32_t)(p[1] << 8) | p[0]);
Edward O'Callaghan6bedc272014-02-12 21:53:44 +1100112}
113
114static inline void bebitenc(void *pp, uint32_t u, uint8_t b)
115{
116 uint8_t *p = (uint8_t *)pp;
117 int i;
118
Julius Werner59e90802017-11-03 13:54:16 -0700119 for (i = 0; i < b; i++)
120 p[(b - 1) - i] = (u >> i*8) & 0xFF;
Edward O'Callaghan6bedc272014-02-12 21:53:44 +1100121}
122
123static inline void be16enc(void *pp, uint16_t u)
124{
125 bebitenc(pp, u, 2);
126}
127
128static inline void be32enc(void *pp, uint32_t u)
129{
130 bebitenc(pp, u, 4);
131}
132
133static inline void lebitenc(void *pp, uint32_t u, uint8_t b)
134{
135 uint8_t *p = (uint8_t *)pp;
136 int i;
137
138 for (i = 0; i < b; i++)
139 p[i] = (u >> i*8) & 0xFF;
140}
141
142static inline void le16enc(void *pp, uint16_t u)
143{
144 lebitenc(pp, u, 2);
145}
146
147static inline void le32enc(void *pp, uint32_t u)
148{
149 lebitenc(pp, u, 4);
150}
151
Hung-Te Line112b742013-03-13 18:08:29 +0800152/* 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)
Gabe Black0af03d22012-03-19 03:06:46 -0700165
Julius Werner8a1d11f2014-07-17 10:43:15 -0700166/* Handy bit manipulation macros */
167
Julius Werner1c371572019-12-02 18:02:51 -0800168#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)))
Julius Werner8a1d11f2014-07-17 10:43:15 -0700171
Julius Werner1c371572019-12-02 18:02:51 -0800172#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)
178
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)
185
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)
192
193#define __clrsetbits_impl(bits, addr, clear, set) write##bits(addr, \
194 (read##bits(addr) & ~((uint##bits##_t)(clear))) | (set))
195
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)
200
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)
205
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)
Julius Werner8a1d11f2014-07-17 10:43:15 -0700210
Edward O'Callaghan6bedc272014-02-12 21:53:44 +1100211#endif /* _ENDIAN_H_ */