Abstract mmap() in physmap.c and only open /dev/mem on the first physmap() call

Corresponding to flashrom svn r397 and coreboot v2 svn r3903.

Signed-off-by: Stefan Reinauer <stepan@coresystems.de>
Signed-off-by: Peter Stuge <peter@stuge.se>
Acked-by: Peter Stuge <peter@stuge.se>
diff --git a/flashrom.c b/flashrom.c
index eb0d8e0..5d0f1ed 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -22,7 +22,6 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -44,7 +43,6 @@
 struct pci_access *pacc;	/* For board and chipset_enable */
 int exclude_start_page, exclude_end_page;
 int verbose = 0;
-int fd_mem;
 
 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
 {
@@ -84,31 +82,10 @@
 	return NULL;
 }
 
-void mmap_errmsg()
-{
-	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");
-	}
-}
-
 void map_flash_registers(struct flashchip *flash)
 {
-	volatile uint8_t *registers;
 	size_t size = flash->total_size * 1024;
-
-	registers = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-			 fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
-
-	if (registers == MAP_FAILED) {
-		perror("Can't mmap registers using " MEM_DEV);
-		mmap_errmsg();
-		exit(1);
-	}
-	flash->virtual_registers = registers;
+	flash->virtual_registers = physmap("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
 }
 
 struct flashchip *probe_flash(struct flashchip *first_flash, int force)
@@ -145,14 +122,7 @@
 		}
 
 		base = flashbase ? flashbase : (0xffffffff - size + 1);
-		bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-			    fd_mem, (off_t) base);
-		if (bios == MAP_FAILED) {
-			perror("Can't mmap memory using " MEM_DEV);
-			mmap_errmsg();
-			exit(1);
-		}
-		flash->virtual_memory = bios;
+		flash->virtual_memory = bios = physmap("flash chip", base, size);
 
 		if (force)
 			break;
@@ -165,7 +135,7 @@
 			break;
 
 notfound:
-		munmap((void *)bios, size);
+		physunmap((void *)bios, size);
 	}
 
 	if (!flash || !flash->name)
@@ -451,13 +421,6 @@
 	pci_init(pacc);		/* Initialize the PCI library */
 	pci_scan_bus(pacc);	/* We want to get the list of devices */
 
-	/* Open the memory device UNCACHED. That's important for MMIO. */
-	if ((fd_mem = open(MEM_DEV, O_RDWR | O_SYNC)) < 0) {
-		perror("Error: Can not access memory using " MEM_DEV
-		       ". You need to be root.");
-		exit(1);
-	}
-
 	myusec_calibrate_delay();
 
 	/* We look at the lbtable first to see if we need a