Thomas Heijligen has uploaded this change for review.

View Change

hwaccess: replace macros by C code

Split the code for endian convertion into a file for big and one for
little endian. The buildsystem selects the right file for the used
endian. Elaborate the macros into plain C code for better and easier
understanding.

Change-Id: I86d38d816b37c283279c485fac8027f8fb94364a
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com>
---
M Makefile
A hwaccess.c
M hwaccess.h
A hwaccess_endian_big.c
A hwaccess_endian_little.c
M meson.build
6 files changed, 255 insertions(+), 79 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/98/62898/1
diff --git a/Makefile b/Makefile
index f5ed409..08549eb 100644
--- a/Makefile
+++ b/Makefile
@@ -344,13 +344,6 @@
$(call mark_unsupported, $(DEPENDS_ON_SERIAL))
endif

-ifeq ($(ENDIAN), little)
-FEATURE_FLAGS += -D'__FLASHROM_LITTLE_ENDIAN__=1'
-endif
-ifeq ($(ENDIAN), big)
-FEATURE_FLAGS += -D'__FLASHROM_BIG_ENDIAN__=1'
-endif
-
# PCI port I/O support is unimplemented on PPC/MIPS/SPARC and unavailable on ARM.
# Right now this means the drivers below only work on x86.
ifneq ($(ARCH), x86)
@@ -377,7 +370,17 @@
###############################################################################
# Library code.

-LIB_OBJS = libflashrom.o layout.o flashrom.o udelay.o programmer.o programmer_table.o helpers.o ich_descriptors.o fmap.o
+LIB_OBJS = libflashrom.o layout.o flashrom.o udelay.o programmer.o programmer_table.o \
+ helpers.o ich_descriptors.o fmap.o hwaccess.o
+
+ifeq ($(ENDIAN), little)
+LIB_OBJS += hwaccess_endian_little.o
+FEATURE_FLAGS += -D'__FLASHROM_LITTLE_ENDIAN__=1'
+endif
+ifeq ($(ENDIAN), big)
+LIB_OBJS += hwaccess_endian_big.o
+FEATURE_FLAGS += -D'__FLASHROM_BIG_ENDIAN__=1'
+endif

###############################################################################
# Frontend related stuff.
diff --git a/hwaccess.c b/hwaccess.c
new file mode 100644
index 0000000..39f4133
--- /dev/null
+++ b/hwaccess.c
@@ -0,0 +1,36 @@
+#include "hwaccess.h"
+
+uint8_t swap8 (const uint8_t value)
+{
+ return (value & (uint8_t)0xffU);
+}
+
+uint16_t swap16(const uint16_t value)
+{
+ return
+ ((value & (uint16_t)0x00ffU) << 8) |
+ ((value & (uint16_t)0xff00U) >> 8);
+
+}
+
+uint32_t swap32(const uint32_t value)
+{
+ return
+ ((value & (uint32_t)0x000000ffUL) << 24) |
+ ((value & (uint32_t)0x0000ff00UL) << 8) |
+ ((value & (uint32_t)0x00ff0000UL) >> 8) |
+ ((value & (uint32_t)0xff000000UL) >> 24);
+}
+
+uint64_t swap64(const uint64_t value)
+{
+ return
+ ((value & (uint64_t)0x00000000000000ffULL) << 56) |
+ ((value & (uint64_t)0x000000000000ff00ULL) << 40) |
+ ((value & (uint64_t)0x0000000000ff0000ULL) << 24) |
+ ((value & (uint64_t)0x00000000ff000000ULL) << 8) |
+ ((value & (uint64_t)0x000000ff00000000ULL) >> 8) |
+ ((value & (uint64_t)0x0000ff0000000000ULL) >> 24) |
+ ((value & (uint64_t)0x00ff000000000000ULL) >> 40) |
+ ((value & (uint64_t)0xff00000000000000ULL) >> 56);
+}
diff --git a/hwaccess.h b/hwaccess.h
index 98ca08e..4c330c4 100644
--- a/hwaccess.h
+++ b/hwaccess.h
@@ -2,6 +2,8 @@
* This file is part of the flashrom project.
*
* Copyright (C) 2009 Carl-Daniel Hailfinger
+ * Copyright (C) 2022 secunet Secure Networg AG
+ * (written by Thomas Heijligen <thomas.heijligen@secunet.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -20,74 +22,36 @@
#ifndef __HWACCESS_H__
#define __HWACCESS_H__ 1

-#define ___constant_swab8(x) ((uint8_t) ( \
- (((uint8_t)(x) & (uint8_t)0xffU))))
+#include <stdint.h>

-#define ___constant_swab16(x) ((uint16_t) ( \
- (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
- (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
+/* swap bytes */
+uint8_t swap8 (const uint8_t value);
+uint16_t swap16(const uint16_t value);
+uint32_t swap32(const uint32_t value);
+uint64_t swap64(const uint64_t value);

-#define ___constant_swab32(x) ((uint32_t) ( \
- (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
- (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
- (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
- (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
+/* convert cpu native endian to little endian */
+uint8_t cpu_to_le8 (const uint8_t value);
+uint16_t cpu_to_le16(const uint16_t value);
+uint32_t cpu_to_le32(const uint32_t value);
+uint64_t cpu_to_le64(const uint64_t value);

-#define ___constant_swab64(x) ((uint64_t) ( \
- (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
- (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
- (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
- (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
- (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
- (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
- (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
- (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
+/* convert cpu native endian to big endian */
+uint8_t cpu_to_be8 (const uint8_t value);
+uint16_t cpu_to_be16(const uint16_t value);
+uint32_t cpu_to_be32(const uint32_t value);
+uint64_t cpu_to_be64(const uint64_t value);

-#if defined (__FLASHROM_BIG_ENDIAN__)
+/* convert little endian to cpu native endian */
+uint8_t le_to_cpu8 (const uint8_t value);
+uint16_t le_to_cpu16(const uint16_t value);
+uint32_t le_to_cpu32(const uint32_t value);
+uint64_t le_to_cpu64(const uint64_t value);

-#define cpu_to_le(bits) \
-static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \
-{ \
- return ___constant_swab##bits(val); \
-}
-
-cpu_to_le(8)
-cpu_to_le(16)
-cpu_to_le(32)
-cpu_to_le(64)
-
-#define cpu_to_be8
-#define cpu_to_be16
-#define cpu_to_be32
-#define cpu_to_be64
-
-#elif defined (__FLASHROM_LITTLE_ENDIAN__)
-
-#define cpu_to_be(bits) \
-static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \
-{ \
- return ___constant_swab##bits(val); \
-}
-
-cpu_to_be(8)
-cpu_to_be(16)
-cpu_to_be(32)
-cpu_to_be(64)
-
-#define cpu_to_le8
-#define cpu_to_le16
-#define cpu_to_le32
-#define cpu_to_le64
-
-#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
-
-#define be_to_cpu8 cpu_to_be8
-#define be_to_cpu16 cpu_to_be16
-#define be_to_cpu32 cpu_to_be32
-#define be_to_cpu64 cpu_to_be64
-#define le_to_cpu8 cpu_to_le8
-#define le_to_cpu16 cpu_to_le16
-#define le_to_cpu32 cpu_to_le32
-#define le_to_cpu64 cpu_to_le64
+/* convert big endian to cpu native endian */
+uint8_t be_to_cpu8 (const uint8_t value);
+uint16_t be_to_cpu16(const uint16_t value);
+uint32_t be_to_cpu32(const uint32_t value);
+uint64_t be_to_cpu64(const uint64_t value);

#endif /* !__HWACCESS_H__ */
diff --git a/hwaccess_endian_big.c b/hwaccess_endian_big.c
new file mode 100644
index 0000000..fd122dc
--- /dev/null
+++ b/hwaccess_endian_big.c
@@ -0,0 +1,85 @@
+#include "hwaccess.h"
+
+/* convert cpu native endian to little endian */
+uint8_t cpu_to_le8 (const uint8_t value)
+{
+ return swap8(value);
+}
+
+uint16_t cpu_to_le16(const uint16_t value)
+{
+ return swap16(value);
+}
+
+uint32_t cpu_to_le32(const uint32_t value)
+{
+ return swap32(value);
+}
+
+uint64_t cpu_to_le64(const uint64_t value)
+{
+ return swap64(value);
+}
+
+/* convert cpu native endian to big endian */
+uint8_t cpu_to_be8 (const uint8_t value)
+{
+ return value;
+}
+
+uint16_t cpu_to_be16(const uint16_t value)
+{
+ return value;
+}
+
+uint32_t cpu_to_be32(const uint32_t value)
+{
+ return value;
+}
+
+uint64_t cpu_to_be64(const uint64_t value)
+{
+ return value;
+}
+
+/* convert little endian to cpu native endian */
+uint8_t le_to_cpu8 (const uint8_t value)
+{
+ return swap8(value);
+}
+
+uint16_t le_to_cpu16(const uint16_t value)
+{
+ return swap16(value);
+}
+
+uint32_t le_to_cpu32(const uint32_t value)
+{
+ return swap32(value);
+}
+
+uint64_t le_to_cpu64(const uint64_t value)
+{
+ return swap64(value);
+}
+
+/* convert big endian to cpu native endian */
+uint8_t be_to_cpu8 (const uint8_t value)
+{
+ return value;
+}
+
+uint16_t be_to_cpu16(const uint16_t value)
+{
+ return value;
+}
+
+uint32_t be_to_cpu32(const uint32_t value)
+{
+ return value;
+}
+
+uint64_t be_to_cpu64(const uint64_t value)
+{
+ return value;
+}
diff --git a/hwaccess_endian_little.c b/hwaccess_endian_little.c
new file mode 100644
index 0000000..4446ce2
--- /dev/null
+++ b/hwaccess_endian_little.c
@@ -0,0 +1,85 @@
+#include "hwaccess.h"
+
+/* convert cpu native endian to little endian */
+uint8_t cpu_to_le8 (const uint8_t value)
+{
+ return value;
+}
+
+uint16_t cpu_to_le16(const uint16_t value)
+{
+ return value;
+}
+
+uint32_t cpu_to_le32(const uint32_t value)
+{
+ return value;
+}
+
+uint64_t cpu_to_le64(const uint64_t value)
+{
+ return value;
+}
+
+/* convert cpu native endian to big endian */
+uint8_t cpu_to_be8 (const uint8_t value)
+{
+ return swap8(value);
+}
+
+uint16_t cpu_to_be16(const uint16_t value)
+{
+ return swap16(value);
+}
+
+uint32_t cpu_to_be32(const uint32_t value)
+{
+ return swap32(value);
+}
+
+uint64_t cpu_to_be64(const uint64_t value)
+{
+ return swap64(value);
+}
+
+/* convert little endian to cpu native endian */
+uint8_t le_to_cpu8 (const uint8_t value)
+{
+ return value;
+}
+
+uint16_t le_to_cpu16(const uint16_t value)
+{
+ return value;
+}
+
+uint32_t le_to_cpu32(const uint32_t value)
+{
+ return value;
+}
+
+uint64_t le_to_cpu64(const uint64_t value)
+{
+ return value;
+}
+
+/* convert big endian to cpu native endian */
+uint8_t be_to_cpu8 (const uint8_t value)
+{
+ return swap8(value);
+}
+
+uint16_t be_to_cpu16(const uint16_t value)
+{
+ return swap16(value);
+}
+
+uint32_t be_to_cpu32(const uint32_t value)
+{
+ return swap32(value);
+}
+
+uint64_t be_to_cpu64(const uint64_t value)
+{
+ return swap64(value);
+}
diff --git a/meson.build b/meson.build
index b7fc25f..5275bd2 100644
--- a/meson.build
+++ b/meson.build
@@ -29,13 +29,6 @@
add_project_arguments('-D_BSD_SOURCE', language : 'c') # required for glibc < v2.19
add_project_arguments('-DFLASHROM_VERSION="' + meson.project_version() + '"', language : 'c')

-if host_machine.endian() == 'little'
- add_project_arguments('-D__FLASHROM_LITTLE_ENDIAN__=1', language : 'c')
-endif
-if host_machine.endian() == 'big'
- add_project_arguments('-D__FLASHROM_BIG_ENDIAN__=1', language : 'c')
-endif
-
if host_machine.system() in ['cygwin', 'windows']
add_project_arguments('-DIS_WINDOWS=1', language : 'c')
else
@@ -142,6 +135,16 @@
add_project_arguments('-DHAVE_UTSNAME=1', language : 'c')
endif

+srcs += 'hwaccess.c'
+if host_machine.endian() == 'little'
+ srcs += 'hwaccess_endian_little.c'
+ add_project_arguments('-D__FLASHROM_LITTLE_ENDIAN__=1', language : 'c')
+endif
+if host_machine.endian() == 'big'
+ srcs += 'hwaccess_endian_big.c'
+ add_project_arguments('-D__FLASHROM_BIG_ENDIAN__=1', language : 'c')
+endif
+
# some programmers require libusb
if get_option('usb')
srcs += 'usbdev.c'

To view, visit change 62898. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I86d38d816b37c283279c485fac8027f8fb94364a
Gerrit-Change-Number: 62898
Gerrit-PatchSet: 1
Gerrit-Owner: Thomas Heijligen <src@posteo.de>
Gerrit-MessageType: newchange