diff options
author | Stefan Reinauer <stepan@coresystems.de> | 2009-01-26 01:10:48 +0000 |
---|---|---|
committer | Peter Stuge <peter@stuge.se> | 2009-01-26 01:10:48 +0000 |
commit | 53ac3947db69be8c526958b07f59a24dbc3678be (patch) | |
tree | afcde113a21fbc434c6751fcd1b39a9e85137073 /util/flashrom/physmap.c | |
parent | ac29d61a454f0562c5c63ea1f5e14f9068904bf0 (diff) |
flashrom: Abstract mmap() in physmap.c and only open /dev/mem on the first physmap() call.
Signed-off-by: Stefan Reinauer <stepan@coresystems.de>
Signed-off-by: Peter Stuge <peter@stuge.se>
Acked-by: Peter Stuge <peter@stuge.se>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@3903 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
Diffstat (limited to 'util/flashrom/physmap.c')
-rw-r--r-- | util/flashrom/physmap.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/util/flashrom/physmap.c b/util/flashrom/physmap.c new file mode 100644 index 0000000000..65d25698c8 --- /dev/null +++ b/util/flashrom/physmap.c @@ -0,0 +1,58 @@ +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdlib.h> +#include <errno.h> +#include "flash.h" + +#if defined (__sun) && (defined(__i386) || defined(__amd64)) +# define MEM_DEV "/dev/xsvc" +#else +# define MEM_DEV "/dev/mem" +#endif + +static int fd_mem = -1; + +void *sys_physmap(unsigned long phys_addr, size_t len) +{ + void *virt_addr; + + if (-1 == fd_mem) { + /* Open the memory device UNCACHED. Important for MMIO. */ + if (-1 == (fd_mem = open(MEM_DEV, O_RDWR|O_SYNC))) { + perror("Critical error: open(" MEM_DEV ")"); + exit(1); + } + } + + virt_addr = mmap(0, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr); + return MAP_FAILED == virt_addr ? NULL : virt_addr; +} + +void physunmap(void *virt_addr, size_t len) +{ + munmap(virt_addr, len); +} + +void *physmap(const char *descr, unsigned long phys_addr, size_t len) +{ + void *virt_addr = sys_physmap(phys_addr, len); + + if (NULL == virt_addr) { + if (NULL == descr) + descr = "memory"; + fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr); + perror(MEM_DEV " mmap failed"); + if (EINVAL == errno) { + fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); + fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); + fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n"); + fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); + fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); + } + exit(1); + } + + return virt_addr; +} |