Nico Huber has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/31016
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
hwaccess: Add endianness converting deserialization functions
Add functions like
uint32_t read_le32(const void *);
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Nico Huber nico.h@gmx.de --- M hwaccess.h 1 file changed, 14 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/1
diff --git a/hwaccess.h b/hwaccess.h index af7054b..5df4fc3 100644 --- a/hwaccess.h +++ b/hwaccess.h @@ -20,6 +20,7 @@ #ifndef __HWACCESS_H__ #define __HWACCESS_H__ 1
+#include <stdint.h> #include "platform.h"
#if NEED_PCI == 1 @@ -109,6 +110,19 @@ #define le_to_cpu32 cpu_to_le32 #define le_to_cpu64 cpu_to_le64
+#define read_from(endianness, bits) \ +static inline uint##bits##_t read_##endianness##bits(const void *from) \ +{ \ + return le_to_cpu##bits(*(uint##bits##_t *)from); \ +} + +read_from(be, 8) +read_from(be, 16) +read_from(be, 32) +read_from(le, 8) +read_from(le, 16) +read_from(le, 32) + #if NEED_RAW_ACCESS == 1 #if IS_X86
Paul Menzel has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 1: Code-Review+1
David Hendricks has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 3: Code-Review-1
(1 comment)
https://review.coreboot.org/c/flashrom/+/31016/3/hwaccess.h File hwaccess.h:
https://review.coreboot.org/c/flashrom/+/31016/3/hwaccess.h@116 PS3, Line 116: le_to_cpu##bits Shouldn't this be 'endianness##_to_cpu##bits'?
Attention is currently required from: David Hendricks, Paul Menzel, Thomas Heijligen. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 7: Code-Review+1
Attention is currently required from: David Hendricks, Paul Menzel, Thomas Heijligen. Thomas Heijligen has uploaded a new patch set (#8) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
hwaccess: Add endianness converting deserialization functions
Add functions like
uint32_t read_le32(const void *);
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Nico Huber nico.h@gmx.de --- M hwaccess.h 1 file changed, 16 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/8
Attention is currently required from: David Hendricks, Paul Menzel, Thomas Heijligen, Edward O'Callaghan, Angel Pons, Anastasia Klimchuk. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 8:
(1 comment)
Patchset:
PS8: What I mentioned to Thomas offline once: We could also add an offset parameter. e.g.
uint32_t read_le32(const void *src, size_t offset);
This would have the benefit that the reader would always see that the offset is in bytes. For instance, consider these two calls:
uint32_t *buf, val; ... val = read_le32(buf + 3); val = read_le32(buf, 12);
They are supposed to do the same. But in the first case we need to have pointer-arithmetic in mind to see it. The second case would always be the same, no matter the type of `buf`.
Attention is currently required from: David Hendricks, Paul Menzel, Thomas Heijligen, Edward O'Callaghan, Angel Pons, Anastasia Klimchuk. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 8:
(1 comment)
File hwaccess.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/38e43741_e82f6392 PS8, Line 116: return endianness##_to_cpu##bits(*(uint##bits##_t *)from); \ I've just noticed that we have unaligned reads in FMAP. So technically, we should read the value byte-by-byte and not with a blunt pointer dereference. Most platforms will handle this gracefully for us. So maybe we can just wait if anybody complains ;)
Attention is currently required from: Nico Huber, David Hendricks, Paul Menzel, Thomas Heijligen, Edward O'Callaghan, Angel Pons. Anastasia Klimchuk has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 8:
(1 comment)
File hwaccess.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/855b36c9_09b06510 PS8, Line 104: #define be_to_cpu8 cpu_to_be8 Why do we need two macros to be the same? Why this can't be just one macro (any of these). read_from(be, 8) expands into the call to be_to_cpu8, but why it can't call cpu_to_be8, they are the same anyway? I am sure there is an answer, it is just me unable to understand from looking at the code.
Attention is currently required from: Nico Huber, David Hendricks, Paul Menzel, Thomas Heijligen, Edward O'Callaghan, Anastasia Klimchuk. Angel Pons has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 8: Code-Review+1
(1 comment)
File hwaccess.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/fdd57133_3283594e PS8, Line 104: #define be_to_cpu8 cpu_to_be8
Why do we need two macros to be the same? Why this can't be just one macro (any of these). […]
If you call the same function twice on any value, the result is the input value. Both functions do the same thing, but having different names allows writing self-documenting code.
Attention is currently required from: David Hendricks, Paul Menzel, Thomas Heijligen, Edward O'Callaghan, Angel Pons, Anastasia Klimchuk. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 8:
(1 comment)
File hwaccess.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/2ef87cfb_79a8e1cf PS8, Line 104: #define be_to_cpu8 cpu_to_be8
If you call the same function twice on any value, the result is the input value. […]
Yep, it's just about readable code.
Attention is currently required from: Nico Huber, David Hendricks, Thomas Heijligen, Edward O'Callaghan, Angel Pons. Anastasia Klimchuk has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: Add endianness converting deserialization functions ......................................................................
Patch Set 8: Code-Review+1
(1 comment)
File hwaccess.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/3014e453_90a5cf1d PS8, Line 104: #define be_to_cpu8 cpu_to_be8
Yep, it's just about readable code.
Thank you! It makes sense, I understand now.
Attention is currently required from: Nico Huber, David Hendricks, Thomas Heijligen, Edward O'Callaghan, Angel Pons. Thomas Heijligen has uploaded a new patch set (#9) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like
uint32_t read_le32(const void *);
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M platform.h M platform/endian.c 2 files changed, 54 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/9
Attention is currently required from: Nico Huber, David Hendricks, Thomas Heijligen, Edward O'Callaghan, Angel Pons. Thomas Heijligen has uploaded a new patch set (#10) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like
uint32_t read_le32(const void *);
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M platform.h 1 file changed, 12 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/10
Attention is currently required from: David Hendricks, Thomas Heijligen, Edward O'Callaghan, Angel Pons. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 14: Code-Review+1
(1 comment)
Patchset:
PS8:
What I mentioned to Thomas offline once: We could also add […]
Any opinions on this?
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 14:
(1 comment)
Patchset:
PS8:
Any opinions on this?
Could we use variadic functions for it? uint32_t read_le32(const void *address, ...); read_le32(buffer); read_le32(buffer, 12);
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan, Angel Pons. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 14:
(1 comment)
Patchset:
PS8:
Could we use variadic functions for it?
Technically yes, but it seems like a lot of overhead for such simple functions.
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 14:
(1 comment)
Patchset:
PS8:
Could we use variadic functions for it? […]
We should also align this with the mmio version of this functions.
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has uploaded a new patch set (#15) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *const buffer, const size_t offset);` Read a X bit unsigned from a buffer at an offset.
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M platform.h 1 file changed, 27 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/15
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 15:
(1 comment)
Patchset:
PS8:
We should also align this with the mmio version of this functions.
Is this more like what you think?
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has uploaded a new patch set (#16) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *const base, const size_t offset);` Read a 32 bit unsigned from a base with an offset.
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M platform.h 1 file changed, 27 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/16
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has uploaded a new patch set (#17) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *const base, const size_t offset);` Read a 32 bit unsigned from a base with an offset. The macro generated implementation matches the prototypes in platform.h
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M Makefile M meson.build M platform.h A platform/memaccess.c 4 files changed, 72 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/17
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan, Angel Pons. Thomas Heijligen has uploaded a new patch set (#18) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *const base, const size_t offset);` Read a 32 bit unsigned from a base with an offset. The macro generated implementation matches the prototypes in platform.h
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M Makefile M meson.build M platform.h A platform/memaccess.c 4 files changed, 56 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/18
Attention is currently required from: Nico Huber, Thomas Heijligen, David Hendricks, Edward O'Callaghan. Angel Pons has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 18: Code-Review+1
Attention is currently required from: Nico Huber, Thomas Heijligen, David Hendricks, Edward O'Callaghan. Thomas Heijligen has uploaded a new patch set (#19) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *const base, const size_t offset);` Read a 32 bit unsigned from a base with an offset. Having prototypes and a macro generated implementation makes it easier to read, understand and spot errors in one of them.
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M Makefile M meson.build M platform.h A platform/memaccess.c 4 files changed, 58 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/19
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 19: Code-Review+1
(3 comments)
Patchset:
PS8:
We should also align this with the mmio version of this functions.
MMIO access is usually done with some offset too, but when it's with specific endianness than we usually use pci_mmio_* functions. Maybe those are worth to adapt? If we start to make changes that need an update for a lot of calls, we probably should discuss that a little more. One thing that comes to mind is the backwards parameters of *mmio_write[bwl].
Is this more like what you think?
Yes that is what I had in mind :)
File platform.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/c03b7e96_6c1a059f PS19, Line 111: const base, const size_t offset The latter 2 `const` are superfluous in a forward declaration. It's not meaningful to the callers, they shouldn't care if the callee can change its variables. Please drop them here (same applies to the commit message, I guess).
File platform/memaccess.c:
https://review.coreboot.org/c/flashrom/+/31016/comment/c8295ab7_203e2503 PS19, Line 26: base + offset Technically this is wrong because arithmetic is undefined for `void *`. So we should add another cast (to a pointer of some- thing with size 1), e.g.
return le_to_cpu8 (*(uint8_t *)((char *)base + offset));
Casting to `uintptr_t` should also work.
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan. Thomas Heijligen has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 19:
(1 comment)
File platform/memaccess.c:
https://review.coreboot.org/c/flashrom/+/31016/comment/8ac621a9_7352ec83 PS19, Line 26: base + offset
Technically this is wrong because arithmetic is undefined for […]
can we just use uintptr_t as type instead of void*? read_le8(uintptr_t base, size_t offset)?
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 19:
(1 comment)
File platform/memaccess.c:
https://review.coreboot.org/c/flashrom/+/31016/comment/2f5d48d1_4daee6cb PS19, Line 26: base + offset
can we just use uintptr_t as type instead of void*? […]
That would mean every caller who has a pointer (they usually have) would have to do the cast. It's most likely less hassle to hide the casting here. `void *` is particularly nice in the function signature as you can call it with any pointer without a cast.
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan. Thomas Heijligen has uploaded a new patch set (#20) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *base, size_t offset);` Read a 32 bit unsigned from a base with an offset. Having prototypes and a macro generated implementation makes it easier to read, understand and spot errors in one of them.
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M Makefile M meson.build M platform.h A platform/memaccess.c 4 files changed, 58 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/20
Attention is currently required from: Nico Huber, David Hendricks, Edward O'Callaghan. Thomas Heijligen has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 20:
(2 comments)
File platform.h:
https://review.coreboot.org/c/flashrom/+/31016/comment/22bac632_9b5101f7 PS19, Line 111: const base, const size_t offset
The latter 2 `const` are superfluous in a forward declaration. […]
Done
File platform/memaccess.c:
https://review.coreboot.org/c/flashrom/+/31016/comment/1017909a_8c5c1c58 PS19, Line 26: base + offset
That would mean every caller who has a pointer (they usually have) […]
Done
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 20: Code-Review+2
Attention is currently required from: Thomas Heijligen, David Hendricks, Edward O'Callaghan. Thomas Heijligen has uploaded a new patch set (#21) to the change originally created by Nico Huber. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *base, size_t offset);` Read a 32 bit unsigned from a base with an offset. Having prototypes and a macro generated implementation makes it easier to read, understand and spot errors in one of them.
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com --- M Makefile M meson.build M platform.h A platform/memaccess.c 4 files changed, 58 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/16/31016/21
Attention is currently required from: Nico Huber, Thomas Heijligen, David Hendricks, Edward O'Callaghan. Angel Pons has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
Patch Set 21: Code-Review+2
Nico Huber has submitted this change. ( https://review.coreboot.org/c/flashrom/+/31016 )
Change subject: hwaccess: add endianness converting deserialization functions ......................................................................
hwaccess: add endianness converting deserialization functions
Add functions like `uint32_t read_le32(const void *base, size_t offset);` Read a 32 bit unsigned from a base with an offset. Having prototypes and a macro generated implementation makes it easier to read, understand and spot errors in one of them.
Change-Id: Idde177acf8bc5f94cd046b6539dc31532c98e452 Signed-off-by: Thomas Heijligen thomas.heijligen@secunet.com Reviewed-on: https://review.coreboot.org/c/flashrom/+/31016 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Angel Pons th3fanbus@gmail.com Reviewed-by: Nico Huber nico.h@gmx.de --- M Makefile M meson.build M platform.h A platform/memaccess.c 4 files changed, 58 insertions(+), 1 deletion(-)
Approvals: build bot (Jenkins): Verified Nico Huber: Looks good to me, approved Angel Pons: Looks good to me, approved
diff --git a/Makefile b/Makefile index f94f046..bfa9c67 100644 --- a/Makefile +++ b/Makefile @@ -378,7 +378,7 @@ # Library code.
LIB_OBJS = libflashrom.o layout.o flashrom.o udelay.o programmer.o programmer_table.o \ - helpers.o ich_descriptors.o fmap.o platform/endian_$(ENDIAN).o + helpers.o ich_descriptors.o fmap.o platform/endian_$(ENDIAN).o platform/memaccess.o
############################################################################### diff --git a/meson.build b/meson.build index c84c5de..5a6bc2a 100644 --- a/meson.build +++ b/meson.build @@ -135,6 +135,8 @@ add_project_arguments('-DHAVE_UTSNAME=1', language : 'c') endif
+srcs += 'platform/memaccess.c' + if host_machine.endian() == 'little' srcs += 'platform/endian_little.c' add_project_arguments('-D__FLASHROM_LITTLE_ENDIAN__=1', language : 'c') diff --git a/platform.h b/platform.h index 4f2a4c1..cf73c29 100644 --- a/platform.h +++ b/platform.h @@ -22,6 +22,7 @@ #ifndef __PLATFORM_H__ #define __PLATFORM_H__ 1
+#include <stddef.h> #include <stdint.h>
/* swap bytes */ @@ -100,4 +101,16 @@ uint32_t be_to_cpu32(uint32_t value); uint64_t be_to_cpu64(uint64_t value);
+/* read value from base at offset in little endian */ +uint8_t read_le8 (const void *base, size_t offset); +uint16_t read_le16(const void *base, size_t offset); +uint32_t read_le32(const void *base, size_t offset); +uint64_t read_le64(const void *base, size_t offset); + +/* read value from base at offset in big endian */ +uint8_t read_be8 (const void *base, size_t offset); +uint16_t read_be16(const void *base, size_t offset); +uint32_t read_be32(const void *base, size_t offset); +uint64_t read_be64(const void *base, size_t offset); + #endif /* !__PLATFORM_H__ */ diff --git a/platform/memaccess.c b/platform/memaccess.c new file mode 100644 index 0000000..44ef410 --- /dev/null +++ b/platform/memaccess.c @@ -0,0 +1,42 @@ +/* + * This file is part of the flashrom project. + * + * Copyright (C) 2016 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 "../platform.h" + +/* + * macro to return endian aware read function + * + * `___read(le, 8)` + * expands to + * `uint8_t read_le8 (const void *const base, const size_t offset) + * { return le_to_cpu8 (*(uint8_t *)((uintptr_t)base + offset)); }` + */ +#define ___read(endian, bits) \ + uint##bits##_t read_##endian##bits (const void *const base, const size_t offset) \ + { return endian##_to_cpu##bits (*(uint##bits##_t *)((uintptr_t)base + offset)); } + +/* read value from base at offset in little endian */ +___read(le, 8) +___read(le, 16) +___read(le, 32) +___read(le, 64) + +/* read value from base at offset in big endian */ +___read(be, 8) +___read(be, 16) +___read(be, 32) +___read(be, 64)