Thomas Heijligen | 5194d58 | 2022-02-19 22:31:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2016 secunet Security Networks AG |
| 5 | * (written by Thomas Heijligen <thomas.heijligen@secunet.com>) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #include "../platform.h" |
| 19 | |
| 20 | /* |
| 21 | * macro to return endian aware read function |
| 22 | * |
| 23 | * `___read(le, 8)` |
| 24 | * expands to |
| 25 | * `uint8_t read_le8 (const void *const base, const size_t offset) |
| 26 | * { return le_to_cpu8 (*(uint8_t *)((uintptr_t)base + offset)); }` |
| 27 | */ |
| 28 | #define ___read(endian, bits) \ |
| 29 | uint##bits##_t read_##endian##bits (const void *const base, const size_t offset) \ |
| 30 | { return endian##_to_cpu##bits (*(uint##bits##_t *)((uintptr_t)base + offset)); } |
| 31 | |
| 32 | /* read value from base at offset in little endian */ |
| 33 | ___read(le, 8) |
| 34 | ___read(le, 16) |
| 35 | ___read(le, 32) |
| 36 | ___read(le, 64) |
| 37 | |
| 38 | /* read value from base at offset in big endian */ |
| 39 | ___read(be, 8) |
| 40 | ___read(be, 16) |
| 41 | ___read(be, 32) |
| 42 | ___read(be, 64) |