blob: f149f6952eb51d9d04ecb8de7720d6b0f4007d69 [file] [log] [blame]
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Carl-Daniel Hailfinger
Thomas Heijligen5618d5b2022-02-19 21:17:44 +01005 * Copyright (C) 2022 secunet Security Networks AG
6 * (written by Thomas Heijligen <thomas.heijligen@secunet.com)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Uwe Hermann43959702010-03-13 17:28:29 +000016 */
17
18/*
Thomas Heijligenc92f94b2022-03-17 13:41:17 +010019 * Header file for platform abstraction.
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000020 */
21
Thomas Heijligenc92f94b2022-03-17 13:41:17 +010022#ifndef __PLATFORM_H__
23#define __PLATFORM_H__ 1
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000024
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010025#include <stdint.h>
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000026
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010027/* swap bytes */
28/* OpenBSD has conflicting definitions for swapX and __swapX */
29static inline uint8_t ___swap8(const uint8_t value)
30{
31 return (value & (uint8_t)0xffU);
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000032}
33
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010034static inline uint16_t ___swap16(const uint16_t value)
35{
36 return ((value & (uint16_t)0x00ffU) << 8) |
37 ((value & (uint16_t)0xff00U) >> 8);
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000038}
39
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010040static inline uint32_t ___swap32(const uint32_t value)
41{
42 return ((value & (uint32_t)0x000000ffUL) << 24) |
43 ((value & (uint32_t)0x0000ff00UL) << 8) |
44 ((value & (uint32_t)0x00ff0000UL) >> 8) |
45 ((value & (uint32_t)0xff000000UL) >> 24);
46}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000047
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010048static inline uint64_t ___swap64(const uint64_t value)
49{
50 return ((value & (uint64_t)0x00000000000000ffULL) << 56) |
51 ((value & (uint64_t)0x000000000000ff00ULL) << 40) |
52 ((value & (uint64_t)0x0000000000ff0000ULL) << 24) |
53 ((value & (uint64_t)0x00000000ff000000ULL) << 8) |
54 ((value & (uint64_t)0x000000ff00000000ULL) >> 8) |
55 ((value & (uint64_t)0x0000ff0000000000ULL) >> 24) |
56 ((value & (uint64_t)0x00ff000000000000ULL) >> 40) |
57 ((value & (uint64_t)0xff00000000000000ULL) >> 56);
58}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000059
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010060/*
61 * macro to return the same value as passed.
62 *
63 * `___return_same(cpu_to_le, 8)`
64 * expands to
65 * `uint8_t cpu_to_le8 (const uint8_t value) { return value; }`
66 */
67#define ___return_same(name, bits) \
68 uint##bits##_t name##bits (const uint##bits##_t value) { return value; }
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000069
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010070/*
71 * macro to return the swapped value as passed.
72 *
73 * `___return_swapped(cpu_to_be, 8)`
74 * expands to
75 * `uint8_t cpu_to_be8 (const uint8_t value) { return ___swap8 (value); }`
76 */
77#define ___return_swapped(name, bits) \
78 uint##bits##_t name##bits (const uint##bits##_t value) { return ___swap##bits (value); }
79
80/* convert cpu native endian to little endian */
81uint8_t cpu_to_le8 (uint8_t value);
82uint16_t cpu_to_le16(uint16_t value);
83uint32_t cpu_to_le32(uint32_t value);
84uint64_t cpu_to_le64(uint64_t value);
85
86/* convert cpu native endian to big endian */
87uint8_t cpu_to_be8 (uint8_t value);
88uint16_t cpu_to_be16(uint16_t value);
89uint32_t cpu_to_be32(uint32_t value);
90uint64_t cpu_to_be64(uint64_t value);
91
92/* convert little endian to cpu native endian */
93uint8_t le_to_cpu8 (uint8_t value);
94uint16_t le_to_cpu16(uint16_t value);
95uint32_t le_to_cpu32(uint32_t value);
96uint64_t le_to_cpu64(uint64_t value);
97
98/* convert big endian to cpu native endian */
99uint8_t be_to_cpu8 (uint8_t value);
100uint16_t be_to_cpu16(uint16_t value);
101uint32_t be_to_cpu32(uint32_t value);
102uint64_t be_to_cpu64(uint64_t value);
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000103
Thomas Heijligenc92f94b2022-03-17 13:41:17 +0100104#endif /* !__PLATFORM_H__ */