Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Carl-Daniel Hailfinger |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 5 | * Copyright (C) 2022 secunet Security Networks AG |
| 6 | * (written by Thomas Heijligen <thomas.heijligen@secunet.com) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 7 | * |
| 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 Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
Thomas Heijligen | c92f94b | 2022-03-17 13:41:17 +0100 | [diff] [blame] | 19 | * Header file for platform abstraction. |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Thomas Heijligen | c92f94b | 2022-03-17 13:41:17 +0100 | [diff] [blame] | 22 | #ifndef __PLATFORM_H__ |
| 23 | #define __PLATFORM_H__ 1 |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 24 | |
Thomas Heijligen | 5194d58 | 2022-02-19 22:31:32 +0100 | [diff] [blame] | 25 | #include <stddef.h> |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 26 | #include <stdint.h> |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 27 | |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 28 | /* swap bytes */ |
| 29 | /* OpenBSD has conflicting definitions for swapX and __swapX */ |
| 30 | static inline uint8_t ___swap8(const uint8_t value) |
| 31 | { |
| 32 | return (value & (uint8_t)0xffU); |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 35 | static inline uint16_t ___swap16(const uint16_t value) |
| 36 | { |
| 37 | return ((value & (uint16_t)0x00ffU) << 8) | |
| 38 | ((value & (uint16_t)0xff00U) >> 8); |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 41 | static 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 Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 48 | |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 49 | static 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 Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 60 | |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 61 | /* |
| 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 Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 70 | |
Thomas Heijligen | 5618d5b | 2022-02-19 21:17:44 +0100 | [diff] [blame] | 71 | /* |
| 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 */ |
| 82 | uint8_t cpu_to_le8 (uint8_t value); |
| 83 | uint16_t cpu_to_le16(uint16_t value); |
| 84 | uint32_t cpu_to_le32(uint32_t value); |
| 85 | uint64_t cpu_to_le64(uint64_t value); |
| 86 | |
| 87 | /* convert cpu native endian to big endian */ |
| 88 | uint8_t cpu_to_be8 (uint8_t value); |
| 89 | uint16_t cpu_to_be16(uint16_t value); |
| 90 | uint32_t cpu_to_be32(uint32_t value); |
| 91 | uint64_t cpu_to_be64(uint64_t value); |
| 92 | |
| 93 | /* convert little endian to cpu native endian */ |
| 94 | uint8_t le_to_cpu8 (uint8_t value); |
| 95 | uint16_t le_to_cpu16(uint16_t value); |
| 96 | uint32_t le_to_cpu32(uint32_t value); |
| 97 | uint64_t le_to_cpu64(uint64_t value); |
| 98 | |
| 99 | /* convert big endian to cpu native endian */ |
| 100 | uint8_t be_to_cpu8 (uint8_t value); |
| 101 | uint16_t be_to_cpu16(uint16_t value); |
| 102 | uint32_t be_to_cpu32(uint32_t value); |
| 103 | uint64_t be_to_cpu64(uint64_t value); |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 104 | |
Thomas Heijligen | 5194d58 | 2022-02-19 22:31:32 +0100 | [diff] [blame] | 105 | /* read value from base at offset in little endian */ |
| 106 | uint8_t read_le8 (const void *base, size_t offset); |
| 107 | uint16_t read_le16(const void *base, size_t offset); |
| 108 | uint32_t read_le32(const void *base, size_t offset); |
| 109 | uint64_t read_le64(const void *base, size_t offset); |
| 110 | |
| 111 | /* read value from base at offset in big endian */ |
| 112 | uint8_t read_be8 (const void *base, size_t offset); |
| 113 | uint16_t read_be16(const void *base, size_t offset); |
| 114 | uint32_t read_be32(const void *base, size_t offset); |
| 115 | uint64_t read_be64(const void *base, size_t offset); |
| 116 | |
Thomas Heijligen | c92f94b | 2022-03-17 13:41:17 +0100 | [diff] [blame] | 117 | #endif /* !__PLATFORM_H__ */ |