Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 1 | /* |
Gabe Black | 7c7b5ff | 2013-11-23 00:38:49 -0800 | [diff] [blame] | 2 | * |
Jon Murphy | c4e9045 | 2022-06-28 10:36:23 -0600 | [diff] [blame] | 3 | * Copyright (c) 2012 The ChromiumOS Authors. |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 4 | * |
Gabe Black | 7c7b5ff | 2013-11-23 00:38:49 -0800 | [diff] [blame] | 5 | * 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 Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 15 | * |
Gabe Black | 7c7b5ff | 2013-11-23 00:38:49 -0800 | [diff] [blame] | 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 |
| 26 | * SUCH DAMAGE. |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #ifndef _ENDIAN_H_ |
| 30 | #define _ENDIAN_H_ |
| 31 | |
Julius Werner | 8a1d11f | 2014-07-17 10:43:15 -0700 | [diff] [blame] | 32 | #include <arch/io.h> |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 33 | #include <arch/types.h> |
| 34 | #include <libpayload-config.h> |
| 35 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 36 | /* Endian functions from glibc 2.9 / BSD "endian.h" */ |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 37 | |
Julius Werner | eab2a29 | 2019-03-05 16:55:15 -0800 | [diff] [blame] | 38 | #if CONFIG(LP_BIG_ENDIAN) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 39 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 40 | #define htobe16(in) (in) |
| 41 | #define htobe32(in) (in) |
| 42 | #define htobe64(in) (in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 43 | |
Julius Werner | 879ea7f | 2019-11-28 12:53:43 -0800 | [diff] [blame] | 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)) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 47 | |
Julius Werner | eab2a29 | 2019-03-05 16:55:15 -0800 | [diff] [blame] | 48 | #elif CONFIG(LP_LITTLE_ENDIAN) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 49 | |
Julius Werner | 879ea7f | 2019-11-28 12:53:43 -0800 | [diff] [blame] | 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)) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 53 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 54 | #define htole16(in) (in) |
| 55 | #define htole32(in) (in) |
| 56 | #define htole64(in) (in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 57 | |
| 58 | #else |
| 59 | |
| 60 | #error Cant tell if the CPU is little or big endian. |
| 61 | |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 62 | #endif /* CONFIG_*_ENDIAN */ |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 63 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 64 | #define be16toh(in) htobe16(in) |
| 65 | #define be32toh(in) htobe32(in) |
| 66 | #define be64toh(in) htobe64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 67 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 68 | #define le16toh(in) htole16(in) |
| 69 | #define le32toh(in) htole32(in) |
| 70 | #define le64toh(in) htole64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 71 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 72 | #define htonw(in) htobe16(in) |
| 73 | #define htonl(in) htobe32(in) |
| 74 | #define htonll(in) htobe64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 75 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 76 | #define ntohw(in) be16toh(in) |
| 77 | #define ntohl(in) be32toh(in) |
| 78 | #define ntohll(in) be64toh(in) |
| 79 | |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 80 | /* |
| 81 | * Alignment-agnostic encode/decode bytestream to/from little/big endian. |
| 82 | */ |
| 83 | |
| 84 | static inline uint16_t be16dec(const void *pp) |
| 85 | { |
| 86 | uint8_t const *p = (uint8_t const *)pp; |
| 87 | |
Patrick Georgi | c34ebab | 2019-11-25 16:42:56 +0100 | [diff] [blame] | 88 | return (uint16_t)((p[0] << 8) | p[1]); |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static inline uint32_t be32dec(const void *pp) |
| 92 | { |
| 93 | uint8_t const *p = (uint8_t const *)pp; |
| 94 | |
Patrick Georgi | c34ebab | 2019-11-25 16:42:56 +0100 | [diff] [blame] | 95 | return (((uint32_t)p[0] << 24) | (uint32_t)(p[1] << 16) | |
| 96 | (uint32_t)(p[2] << 8) | p[3]); |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static inline uint16_t le16dec(const void *pp) |
| 100 | { |
| 101 | uint8_t const *p = (uint8_t const *)pp; |
| 102 | |
Patrick Georgi | c34ebab | 2019-11-25 16:42:56 +0100 | [diff] [blame] | 103 | return (uint16_t)((p[1] << 8) | p[0]); |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline uint32_t le32dec(const void *pp) |
| 107 | { |
| 108 | uint8_t const *p = (uint8_t const *)pp; |
| 109 | |
Patrick Georgi | c34ebab | 2019-11-25 16:42:56 +0100 | [diff] [blame] | 110 | return ((uint32_t)(p[3] << 24) | (uint32_t)(p[2] << 16) | |
| 111 | (uint32_t)(p[1] << 8) | p[0]); |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static inline void bebitenc(void *pp, uint32_t u, uint8_t b) |
| 115 | { |
| 116 | uint8_t *p = (uint8_t *)pp; |
| 117 | int i; |
| 118 | |
Julius Werner | 59e9080 | 2017-11-03 13:54:16 -0700 | [diff] [blame] | 119 | for (i = 0; i < b; i++) |
| 120 | p[(b - 1) - i] = (u >> i*8) & 0xFF; |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static inline void be16enc(void *pp, uint16_t u) |
| 124 | { |
| 125 | bebitenc(pp, u, 2); |
| 126 | } |
| 127 | |
| 128 | static inline void be32enc(void *pp, uint32_t u) |
| 129 | { |
| 130 | bebitenc(pp, u, 4); |
| 131 | } |
| 132 | |
| 133 | static 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 | |
| 142 | static inline void le16enc(void *pp, uint16_t u) |
| 143 | { |
| 144 | lebitenc(pp, u, 2); |
| 145 | } |
| 146 | |
| 147 | static inline void le32enc(void *pp, uint32_t u) |
| 148 | { |
| 149 | lebitenc(pp, u, 4); |
| 150 | } |
| 151 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 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) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 165 | |
Julius Werner | 8a1d11f | 2014-07-17 10:43:15 -0700 | [diff] [blame] | 166 | /* Handy bit manipulation macros */ |
| 167 | |
Julius Werner | 1c37157 | 2019-12-02 18:02:51 -0800 | [diff] [blame] | 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))) |
Julius Werner | 8a1d11f | 2014-07-17 10:43:15 -0700 | [diff] [blame] | 171 | |
Julius Werner | 1c37157 | 2019-12-02 18:02:51 -0800 | [diff] [blame] | 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) |
| 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 Werner | 8a1d11f | 2014-07-17 10:43:15 -0700 | [diff] [blame] | 210 | |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 211 | #endif /* _ENDIAN_H_ */ |