mirror of
				https://review.coreboot.org/flashrom.git
				synced 2025-10-31 05:10:41 +01:00 
			
		
		
		
	hwaccess: replace macros by C code
Split the code for endian conversion into separate files for big and little endian. The buildsystem selects the correct file for the used endianness. Replace the swab macros with `static inline` c functions. Define macros for returning the same or swapped value. Call those macros in the endian specific files. Change-Id: I86d38d816b37c283279c485fac8027f8fb94364a Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com> Reviewed-on: https://review.coreboot.org/c/flashrom/+/62898 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Nico Huber <nico.h@gmx.de>
This commit is contained in:
		 Thomas Heijligen
					Thomas Heijligen
				
			
				
					committed by
					
						 Nico Huber
						Nico Huber
					
				
			
			
				
	
			
			
			 Nico Huber
						Nico Huber
					
				
			
						parent
						
							b94a5a21c3
						
					
				
				
					commit
					6272c71fbb
				
			
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							| @@ -377,7 +377,9 @@ CHIP_OBJS = jedec.o stm50.o w39.o w29ee011.o \ | |||||||
| ############################################################################### | ############################################################################### | ||||||
| # Library code. | # 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_endian_$(ENDIAN).o | ||||||
|  |  | ||||||
|  |  | ||||||
| ############################################################################### | ############################################################################### | ||||||
| # Frontend related stuff. | # Frontend related stuff. | ||||||
|   | |||||||
							
								
								
									
										132
									
								
								hwaccess.h
									
									
									
									
									
								
							
							
						
						
									
										132
									
								
								hwaccess.h
									
									
									
									
									
								
							| @@ -2,6 +2,8 @@ | |||||||
|  * This file is part of the flashrom project. |  * This file is part of the flashrom project. | ||||||
|  * |  * | ||||||
|  * Copyright (C) 2009 Carl-Daniel Hailfinger |  * Copyright (C) 2009 Carl-Daniel Hailfinger | ||||||
|  |  * Copyright (C) 2022 secunet Security Networks AG | ||||||
|  |  * (written by Thomas Heijligen <thomas.heijligen@secunet.com) | ||||||
|  * |  * | ||||||
|  * This program is free software; you can redistribute it and/or modify |  * 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 |  * it under the terms of the GNU General Public License as published by | ||||||
| @@ -20,74 +22,82 @@ | |||||||
| #ifndef __HWACCESS_H__ | #ifndef __HWACCESS_H__ | ||||||
| #define __HWACCESS_H__ 1 | #define __HWACCESS_H__ 1 | ||||||
|  |  | ||||||
| #define ___constant_swab8(x) ((uint8_t) (				\ | #include <stdint.h> | ||||||
| 	(((uint8_t)(x) & (uint8_t)0xffU)))) |  | ||||||
|  |  | ||||||
| #define ___constant_swab16(x) ((uint16_t) (				\ | /* swap bytes */ | ||||||
| 	(((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |			\ | static inline uint8_t swap8(const uint8_t value) | ||||||
| 	(((uint16_t)(x) & (uint16_t)0xff00U) >> 8))) | { | ||||||
|  | 	return  (value & (uint8_t)0xffU); | ||||||
| #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))) |  | ||||||
|  |  | ||||||
| #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))) |  | ||||||
|  |  | ||||||
| #if defined (__FLASHROM_BIG_ENDIAN__) |  | ||||||
|  |  | ||||||
| #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) | static inline uint16_t swap16(const uint16_t value) | ||||||
| cpu_to_le(16) | { | ||||||
| cpu_to_le(32) | 	return  ((value & (uint16_t)0x00ffU) << 8) | | ||||||
| cpu_to_le(64) | 		((value & (uint16_t)0xff00U) >> 8); | ||||||
|  |  | ||||||
| #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) | static inline uint32_t swap32(const uint32_t value) | ||||||
| cpu_to_be(16) | { | ||||||
| cpu_to_be(32) | 	return  ((value & (uint32_t)0x000000ffUL) << 24) | | ||||||
| cpu_to_be(64) | 		((value & (uint32_t)0x0000ff00UL) <<  8) | | ||||||
|  | 		((value & (uint32_t)0x00ff0000UL) >>  8) | | ||||||
|  | 		((value & (uint32_t)0xff000000UL) >> 24); | ||||||
|  | } | ||||||
|  |  | ||||||
| #define cpu_to_le8 | static inline uint64_t swap64(const uint64_t value) | ||||||
| #define cpu_to_le16 | { | ||||||
| #define cpu_to_le32 | 	return  ((value & (uint64_t)0x00000000000000ffULL) << 56) | | ||||||
| #define cpu_to_le64 | 		((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); | ||||||
|  | } | ||||||
|  |  | ||||||
| #endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */ | /* | ||||||
|  |  * macro to return the same value as passed. | ||||||
|  |  * | ||||||
|  |  * `___return_same(cpu_to_le, 8)` | ||||||
|  |  *	expands to | ||||||
|  |  * `uint8_t cpu_to_le8 (const uint8_t value) { return value; }` | ||||||
|  |  */ | ||||||
|  | #define ___return_same(name, bits) \ | ||||||
|  | 	uint##bits##_t name##bits (const uint##bits##_t value) { return value; } | ||||||
|  |  | ||||||
| #define be_to_cpu8 cpu_to_be8 | /* | ||||||
| #define be_to_cpu16 cpu_to_be16 |  * macro to return the swapped value as passed. | ||||||
| #define be_to_cpu32 cpu_to_be32 |  * | ||||||
| #define be_to_cpu64 cpu_to_be64 |  * `___return_swapped(cpu_to_be, 8)` | ||||||
| #define le_to_cpu8 cpu_to_le8 |  *	expands to | ||||||
| #define le_to_cpu16 cpu_to_le16 |  * `uint8_t cpu_to_be8 (const uint8_t value) { return swap8 (value); }` | ||||||
| #define le_to_cpu32 cpu_to_le32 |  */ | ||||||
| #define le_to_cpu64 cpu_to_le64 | #define ___return_swapped(name, bits) \ | ||||||
|  | 	uint##bits##_t name##bits (const uint##bits##_t value) { return swap##bits (value); } | ||||||
|  |  | ||||||
|  | /* 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); | ||||||
|  |  | ||||||
|  | /* 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); | ||||||
|  |  | ||||||
|  | /* 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); | ||||||
|  |  | ||||||
|  | /* 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__ */ | #endif /* !__HWACCESS_H__ */ | ||||||
|   | |||||||
							
								
								
									
										42
									
								
								hwaccess_endian_big.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								hwaccess_endian_big.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | /* | ||||||
|  |  * This file is part of the flashrom project. | ||||||
|  |  * | ||||||
|  |  * Copyright (C) 2022 secunet Security Networks 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 | ||||||
|  |  * the Free Software Foundation; either version 2 of the License, or | ||||||
|  |  * (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * This program is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  * GNU General Public License for more details. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "hwaccess.h" | ||||||
|  |  | ||||||
|  | /* convert cpu native endian to little endian */ | ||||||
|  | ___return_swapped(cpu_to_le, 8) | ||||||
|  | ___return_swapped(cpu_to_le, 16) | ||||||
|  | ___return_swapped(cpu_to_le, 32) | ||||||
|  | ___return_swapped(cpu_to_le, 64) | ||||||
|  |  | ||||||
|  | /* convert cpu native endian to big endian */ | ||||||
|  | ___return_same(cpu_to_be, 8) | ||||||
|  | ___return_same(cpu_to_be, 16) | ||||||
|  | ___return_same(cpu_to_be, 32) | ||||||
|  | ___return_same(cpu_to_be, 64) | ||||||
|  |  | ||||||
|  | /* convert little endian to cpu native endian */ | ||||||
|  | ___return_swapped(le_to_cpu, 8) | ||||||
|  | ___return_swapped(le_to_cpu, 16) | ||||||
|  | ___return_swapped(le_to_cpu, 32) | ||||||
|  | ___return_swapped(le_to_cpu, 64) | ||||||
|  |  | ||||||
|  | /* convert big endian to cpu native endian */ | ||||||
|  | ___return_same(be_to_cpu, 8) | ||||||
|  | ___return_same(be_to_cpu, 16) | ||||||
|  | ___return_same(be_to_cpu, 32) | ||||||
|  | ___return_same(be_to_cpu, 64) | ||||||
							
								
								
									
										42
									
								
								hwaccess_endian_little.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								hwaccess_endian_little.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | /* | ||||||
|  |  * This file is part of the flashrom project. | ||||||
|  |  * | ||||||
|  |  * Copyright (C) 2022 secunet Security Networks 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 | ||||||
|  |  * the Free Software Foundation; either version 2 of the License, or | ||||||
|  |  * (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * This program is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  * GNU General Public License for more details. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "hwaccess.h" | ||||||
|  |  | ||||||
|  | /* convert cpu native endian to little endian */ | ||||||
|  | ___return_same(cpu_to_le, 8) | ||||||
|  | ___return_same(cpu_to_le, 16) | ||||||
|  | ___return_same(cpu_to_le, 32) | ||||||
|  | ___return_same(cpu_to_le, 64) | ||||||
|  |  | ||||||
|  | /* convert cpu native endian to big endian */ | ||||||
|  | ___return_swapped(cpu_to_be, 8) | ||||||
|  | ___return_swapped(cpu_to_be, 16) | ||||||
|  | ___return_swapped(cpu_to_be, 32) | ||||||
|  | ___return_swapped(cpu_to_be, 64) | ||||||
|  |  | ||||||
|  | /* convert little endian to cpu native endian */ | ||||||
|  | ___return_same(le_to_cpu, 8) | ||||||
|  | ___return_same(le_to_cpu, 16) | ||||||
|  | ___return_same(le_to_cpu, 32) | ||||||
|  | ___return_same(le_to_cpu, 64) | ||||||
|  |  | ||||||
|  | /* convert big endian to cpu native endian */ | ||||||
|  | ___return_swapped(be_to_cpu, 8) | ||||||
|  | ___return_swapped(be_to_cpu, 16) | ||||||
|  | ___return_swapped(be_to_cpu, 32) | ||||||
|  | ___return_swapped(be_to_cpu, 64) | ||||||
							
								
								
									
										16
									
								
								meson.build
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								meson.build
									
									
									
									
									
								
							| @@ -29,13 +29,6 @@ add_project_arguments('-D_POSIX_C_SOURCE=200809L', language : 'c') # required fo | |||||||
| add_project_arguments('-D_BSD_SOURCE', language : 'c') # required for glibc < v2.19 | add_project_arguments('-D_BSD_SOURCE', language : 'c') # required for glibc < v2.19 | ||||||
| add_project_arguments('-DFLASHROM_VERSION="' + meson.project_version() + '"', language : 'c') | 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'] | if host_machine.system() in ['cygwin', 'windows'] | ||||||
|   add_project_arguments('-DIS_WINDOWS=1', language : 'c') |   add_project_arguments('-DIS_WINDOWS=1', language : 'c') | ||||||
| else | else | ||||||
| @@ -142,6 +135,15 @@ if cc.check_header('sys/utsname.h') | |||||||
|   add_project_arguments('-DHAVE_UTSNAME=1', language : 'c') |   add_project_arguments('-DHAVE_UTSNAME=1', language : 'c') | ||||||
| endif | endif | ||||||
|  |  | ||||||
|  | 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 | # some programmers require libusb | ||||||
| if get_option('usb') | if get_option('usb') | ||||||
|   srcs += 'usbdev.c' |   srcs += 'usbdev.c' | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user