Create a physical memory mapping function which requests cached readonly memory
This should take care of picky Linux kernels which do not allow uncached
mappings to cached areas. Handle mapping failure gracefully (no forced
exit()) if the caller specifies it.
Such cached areas which can handle mapping failure are DMI tables and
coreboot tables. On failure we just ignore those tables. That is not
perfect, but a lot better than aborting flashrom due to an error in
nonessential functionality.
This should fix flashrom on a sizable number of machines where it
currently aborts early.
Yes, I could have exploited a Linux kernel bug to "solve" this, but
relying on such bugs is not exactly the best idea.
Corresponding to flashrom svn r889.
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Vincent Pelletier <plr.vincent@gmail.com>
diff --git a/cbtable.c b/cbtable.c
index 2611a62..c12354c 100644
--- a/cbtable.c
+++ b/cbtable.c
@@ -6,6 +6,7 @@
* (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
* Copyright (C) 2006-2009 coresystems GmbH
* (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
+ * Copyright (C) 2010 Carl-Daniel Hailfinger
*
* 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
@@ -207,11 +208,15 @@
#else
start = 0x0;
#endif
- table_area = physmap("low megabyte", start, BYTES_TO_MAP);
+ table_area = physmap_try_ro("low megabyte", start, BYTES_TO_MAP - start);
+ if (!table_area) {
+ msg_perr("Failed getting access to coreboot low tables.\n");
+ return -1;
+ }
lb_table = find_lb_table(table_area, 0x00000, 0x1000);
if (!lb_table)
- lb_table = find_lb_table(table_area, 0xf0000, BYTES_TO_MAP);
+ lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
if (lb_table) {
struct lb_forward *forward = (struct lb_forward *)
(((char *)lb_table) + lb_table->header_bytes);
@@ -219,7 +224,12 @@
start = forward->forward;
start &= ~(getpagesize() - 1);
physunmap(table_area, BYTES_TO_MAP);
- table_area = physmap("high tables", start, BYTES_TO_MAP);
+ table_area = physmap_try_ro("high tables", start, BYTES_TO_MAP);
+ if (!table_area) {
+ msg_perr("Failed getting access to coreboot "
+ "high tables.\n");
+ return -1;
+ }
lb_table = find_lb_table(table_area, 0x00000, 0x1000);
}
}
diff --git a/flash.h b/flash.h
index 3d31d44..d8f02bb 100644
--- a/flash.h
+++ b/flash.h
@@ -343,6 +343,7 @@
/* physmap.c */
void *physmap(const char *descr, unsigned long phys_addr, size_t len);
+void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len);
void physunmap(void *virt_addr, size_t len);
int setup_cpu_msr(int cpu);
void cleanup_cpu_msr(void);
diff --git a/physmap.c b/physmap.c
index 3a003fe..fba7a53 100644
--- a/physmap.c
+++ b/physmap.c
@@ -3,6 +3,7 @@
*
* Copyright (C) 2009 Peter Stuge <peter@stuge.se>
* Copyright (C) 2009 coresystems GmbH
+ * Copyright (C) 2010 Carl-Daniel Hailfinger
*
* 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
@@ -36,6 +37,10 @@
return map_physical(phys_addr, len);
}
+/* The OS X driver does not differentiate between mapping types. */
+#define sys_physmap_rw_uncached sys_physmap
+#define sys_physmap_ro_cached sys_physmap
+
void physunmap(void *virt_addr, size_t len)
{
unmap_physical(virt_addr, len);
@@ -51,8 +56,10 @@
#endif
static int fd_mem = -1;
+static int fd_mem_cached = -1;
-void *sys_physmap(unsigned long phys_addr, size_t len)
+/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
+void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
{
void *virt_addr;
@@ -69,6 +76,26 @@
return MAP_FAILED == virt_addr ? NULL : virt_addr;
}
+/* For reading DMI/coreboot/whatever tables. We should never write, and we
+ * do not care about caching.
+ */
+void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
+{
+ void *virt_addr;
+
+ if (-1 == fd_mem_cached) {
+ /* Open the memory device CACHED. */
+ if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
+ perror("Critical error: open(" MEM_DEV ")");
+ exit(2);
+ }
+ }
+
+ virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
+ fd_mem_cached, (off_t)phys_addr);
+ return MAP_FAILED == virt_addr ? NULL : virt_addr;
+}
+
void physunmap(void *virt_addr, size_t len)
{
if (len == 0) {
@@ -80,7 +107,12 @@
}
#endif
-void *physmap(const char *descr, unsigned long phys_addr, size_t len)
+#define PHYSMAP_NOFAIL 0
+#define PHYSMAP_MAYFAIL 1
+#define PHYSMAP_RW 0
+#define PHYSMAP_RO 1
+
+void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly)
{
void *virt_addr;
@@ -100,7 +132,11 @@
descr, (unsigned long)len, phys_addr);
}
- virt_addr = sys_physmap(phys_addr, len);
+ if (readonly) {
+ virt_addr = sys_physmap_ro_cached(phys_addr, len);
+ } else {
+ virt_addr = sys_physmap_rw_uncached(phys_addr, len);
+ }
if (NULL == virt_addr) {
if (NULL == descr)
@@ -114,12 +150,23 @@
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(3);
+ if (!mayfail)
+ exit(3);
}
return virt_addr;
}
+void *physmap(const char *descr, unsigned long phys_addr, size_t len)
+{
+ return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW);
+}
+
+void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
+{
+ return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO);
+}
+
#ifdef __linux__
/*
* Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,