blob: d15082f02b80adbf570a836b90f67d397fe8b821 [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 Heijligen5194d582022-02-19 22:31:32 +010025#include <stddef.h>
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010026#include <stdint.h>
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000027
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010028/* swap bytes */
29/* OpenBSD has conflicting definitions for swapX and __swapX */
30static inline uint8_t ___swap8(const uint8_t value)
31{
32 return (value & (uint8_t)0xffU);
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000033}
34
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010035static inline uint16_t ___swap16(const uint16_t value)
36{
37 return ((value & (uint16_t)0x00ffU) << 8) |
38 ((value & (uint16_t)0xff00U) >> 8);
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000039}
40
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010041static inline uint32_t ___swap32(const uint32_t value)
42{
43 return ((value & (uint32_t)0x000000ffUL) << 24) |
44 ((value & (uint32_t)0x0000ff00UL) << 8) |
45 ((value & (uint32_t)0x00ff0000UL) >> 8) |
46 ((value & (uint32_t)0xff000000UL) >> 24);
47}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000048
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010049static inline uint64_t ___swap64(const uint64_t value)
50{
51 return ((value & (uint64_t)0x00000000000000ffULL) << 56) |
52 ((value & (uint64_t)0x000000000000ff00ULL) << 40) |
53 ((value & (uint64_t)0x0000000000ff0000ULL) << 24) |
54 ((value & (uint64_t)0x00000000ff000000ULL) << 8) |
55 ((value & (uint64_t)0x000000ff00000000ULL) >> 8) |
56 ((value & (uint64_t)0x0000ff0000000000ULL) >> 24) |
57 ((value & (uint64_t)0x00ff000000000000ULL) >> 40) |
58 ((value & (uint64_t)0xff00000000000000ULL) >> 56);
59}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000060
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010061/*
62 * macro to return the same value as passed.
63 *
64 * `___return_same(cpu_to_le, 8)`
65 * expands to
66 * `uint8_t cpu_to_le8 (const uint8_t value) { return value; }`
67 */
68#define ___return_same(name, bits) \
69 uint##bits##_t name##bits (const uint##bits##_t value) { return value; }
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000070
Thomas Heijligen5618d5b2022-02-19 21:17:44 +010071/*
72 * macro to return the swapped value as passed.
73 *
74 * `___return_swapped(cpu_to_be, 8)`
75 * expands to
76 * `uint8_t cpu_to_be8 (const uint8_t value) { return ___swap8 (value); }`
77 */
78#define ___return_swapped(name, bits) \
79 uint##bits##_t name##bits (const uint##bits##_t value) { return ___swap##bits (value); }
80
81/* convert cpu native endian to little endian */
82uint8_t cpu_to_le8 (uint8_t value);
83uint16_t cpu_to_le16(uint16_t value);
84uint32_t cpu_to_le32(uint32_t value);
85uint64_t cpu_to_le64(uint64_t value);
86
87/* convert cpu native endian to big endian */
88uint8_t cpu_to_be8 (uint8_t value);
89uint16_t cpu_to_be16(uint16_t value);
90uint32_t cpu_to_be32(uint32_t value);
91uint64_t cpu_to_be64(uint64_t value);
92
93/* convert little endian to cpu native endian */
94uint8_t le_to_cpu8 (uint8_t value);
95uint16_t le_to_cpu16(uint16_t value);
96uint32_t le_to_cpu32(uint32_t value);
97uint64_t le_to_cpu64(uint64_t value);
98
99/* convert big endian to cpu native endian */
100uint8_t be_to_cpu8 (uint8_t value);
101uint16_t be_to_cpu16(uint16_t value);
102uint32_t be_to_cpu32(uint32_t value);
103uint64_t be_to_cpu64(uint64_t value);
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000104
Thomas Heijligen5194d582022-02-19 22:31:32 +0100105/* read value from base at offset in little endian */
106uint8_t read_le8 (const void *base, size_t offset);
107uint16_t read_le16(const void *base, size_t offset);
108uint32_t read_le32(const void *base, size_t offset);
109uint64_t read_le64(const void *base, size_t offset);
110
111/* read value from base at offset in big endian */
112uint8_t read_be8 (const void *base, size_t offset);
113uint16_t read_be16(const void *base, size_t offset);
114uint32_t read_be32(const void *base, size_t offset);
115uint64_t read_be64(const void *base, size_t offset);
116
Thomas Heijligenc92f94b2022-03-17 13:41:17 +0100117#endif /* !__PLATFORM_H__ */