Hi,

attached patches implement libpayload support and improve life for DOS
based flashrom, too:

1.physmap-allow-NULL changes the physmap* behaviour to use -1 as error
code instead of 0. That way, 1:1 mapped memory can be supported properly
because 0 is not a magic number anymore.
-1 on the other hand is a rather unlikely memory offset, so that should
be safe.

2.support-libpayload adds libpayload implementations of various
functions and data structures and improves compatibility to libpayload
by #ifdefing out various include statements, as well as file access
related code.
Some parts look weird, but I had to work around name clashes (eg. msr_t)

flashrom.c is mostly unchanged, but my local changes beyond those in the
patch aren't suitable for upstream. Any flashrom-as-payload project
requires some entirely new frontend, but I hope that some
libflashrom-type effort benefits from these patches and simplifies any
frontend work later on.


Both patches are
Signed-off-by: Patrick Georgi <[email protected]>


Regards,
Patrick
Change physmap behaviour to use -1 instead of 0 as error code, to allow
flashrom to work with 1:1 mappings

Signed-off-by: Patrick Georgi <[email protected]>
Index: flashrom-patches/cbtable.c
===================================================================
--- flashrom-patches.orig/cbtable.c     2010-08-16 20:28:49.000000000 +0200
+++ flashrom-patches/cbtable.c  2010-08-16 20:32:32.048812200 +0200
@@ -212,7 +212,7 @@
        start = 0x0;
 #endif
        table_area = physmap_try_ro("low megabyte", start, BYTES_TO_MAP - 
start);
-       if (!table_area) {
+       if (ERROR_PTR == table_area) {
                msg_perr("Failed getting access to coreboot low tables.\n");
                return -1;
        }
@@ -228,7 +228,7 @@
                        start &= ~(getpagesize() - 1);
                        physunmap(table_area, BYTES_TO_MAP);
                        table_area = physmap_try_ro("high tables", start, 
BYTES_TO_MAP);
-                       if (!table_area) {
+                       if (ERROR_PTR == table_area) {
                                msg_perr("Failed getting access to coreboot "
                                         "high tables.\n");
                                return -1;
Index: flashrom-patches/flash.h
===================================================================
--- flashrom-patches.orig/flash.h       2010-08-16 20:28:49.000000000 +0200
+++ flashrom-patches/flash.h    2010-08-16 20:44:59.316553500 +0200
@@ -33,6 +33,8 @@
 #undef max
 #endif
 
+#define ERROR_PTR ((void*)-1)
+
 typedef unsigned long chipaddr;
 
 int register_shutdown(void (*function) (void *data), void *data);
Index: flashrom-patches/physmap.c
===================================================================
--- flashrom-patches.orig/physmap.c     2010-08-16 20:28:49.000000000 +0200
+++ flashrom-patches/physmap.c  2010-08-16 20:44:49.284978600 +0200
@@ -52,11 +52,11 @@
        realmem_map = valloc(1024 * 1024);
 
        if (!realmem_map) {
-               return NULL;
+               return ERROR_PTR;
        }
 
        if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
-               return NULL;
+               return ERROR_PTR;
        }
 
        return realmem_map + phys_addr;
@@ -69,7 +69,7 @@
 
        /* enable 4GB limit on DS descriptor */
        if (!__djgpp_nearptr_enable()) {
-               return NULL;
+               return ERROR_PTR;
        }
 
        if ((phys_addr + len - 1) < (1024 * 1024)) {
@@ -82,7 +82,7 @@
        ret =  __dpmi_physical_address_mapping (&mi);
 
        if (ret != 0) {
-               return NULL;
+               return ERROR_PTR;
        }
 
        return (void *) mi.address + __djgpp_conventional_base;
@@ -151,7 +151,7 @@
 
        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;
+       return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
 }
 
 /* For reading DMI/coreboot/whatever tables. We should never write, and we
@@ -171,7 +171,7 @@
 
        virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
                         fd_mem_cached, (off_t)phys_addr);
-       return MAP_FAILED == virt_addr ? NULL : virt_addr;
+       return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
 }
 
 void physunmap(void *virt_addr, size_t len)
@@ -197,7 +197,7 @@
        if (len == 0) {
                msg_pspew("Not mapping %s, zero size at 0x%08lx.\n",
                             descr, phys_addr);
-               return NULL;
+               return ERROR_PTR;
        }
                
        if ((getpagesize() - 1) & len) {
@@ -216,7 +216,7 @@
                virt_addr = sys_physmap_rw_uncached(phys_addr, len);
        }
 
-       if (NULL == virt_addr) {
+       if (ERROR_PTR == virt_addr) {
                if (NULL == descr)
                        descr = "memory";
                msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, 
(unsigned long)len, phys_addr);
Add libpayload support where applicable
Avoid system headers where not necessary with libpayload
Comment out code for libpayload that accesses files

flashrom.c is mostly unchanged as my other local changes are
unsuitable for public consumption (and that won't change in future)

Signed-off-by: Patrick Georgi <[email protected]>
Index: flashrom-patches/physmap.c
===================================================================
--- flashrom-patches.orig/physmap.c     2010-08-16 20:31:55.000000000 +0200
+++ flashrom-patches/physmap.c  2010-08-16 20:33:28.225025300 +0200
@@ -28,7 +28,7 @@
 #include "flash.h"
 
 /* Do we need any file access or ioctl for physmap or MSR? */
-#if !defined(__DJGPP__)
+#if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__)
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -104,6 +104,31 @@
        __dpmi_free_physical_address_mapping(&mi);
 }
 
+#elif defined(__LIBPAYLOAD__)
+#include <arch/virtual.h>
+
+#define MEM_DEV ""
+
+void *sys_physmap(unsigned long phys_addr, size_t len)
+{
+       return (void*)phys_to_virt(phys_addr);
+}
+
+#define sys_physmap_rw_uncached        sys_physmap
+#define sys_physmap_ro_cached  sys_physmap
+
+void physunmap(void *virt_addr, size_t len)
+{
+}
+
+int setup_cpu_msr(int cpu)
+{
+       return 0;
+}
+
+void cleanup_cpu_msr(void)
+{
+}
 #elif defined(__DARWIN__)
 
 #include <DirectIO/darwinio.h>
@@ -448,6 +473,20 @@
 {
        // Nothing, yet.
 }
+#elif defined(__LIBPAYLOAD__)
+msr_t libpayload_rdmsr(int addr)
+{
+       msr_t msr;
+       unsigned long long val = _rdmsr(addr);
+       msr.lo = val & 0xffffffff;
+       msr.hi = val >> 32;
+       return msr;
+}
+
+int libpayload_wrmsr(int addr, msr_t msr)
+{
+       _wrmsr(addr, (unsigned long long)(msr.lo | ((unsigned long long)msr.hi 
<< 32)));
+}
 #else
 msr_t rdmsr(int addr)
 {
Index: flashrom-patches/hwaccess.c
===================================================================
--- flashrom-patches.orig/hwaccess.c    2010-08-16 20:28:49.000000000 +0200
+++ flashrom-patches/hwaccess.c 2010-08-16 20:34:41.625223500 +0200
@@ -22,9 +22,11 @@
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
-#if !defined (__DJGPP__)
+#if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__)
 #include <unistd.h>
 #include <fcntl.h>
+#endif
+#if !defined (__DJGPP__)
 #include <errno.h>
 #endif
 #include "flash.h"
@@ -44,7 +46,7 @@
 
 void get_io_perms(void)
 {
-#if defined(__DJGPP__)
+#if defined(__DJGPP__) || defined(__LIBPAYLOAD__)
        /* We have full permissions by default. */
        return;
 #else
Index: flashrom-patches/hwaccess.h
===================================================================
--- flashrom-patches.orig/hwaccess.h    2010-08-16 20:28:49.000000000 +0200
+++ flashrom-patches/hwaccess.h 2010-08-16 20:34:41.630223800 +0200
@@ -292,7 +292,7 @@
   #endif
 #endif
 
-#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
+#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__DragonFly__) 
&& !defined(__LIBPAYLOAD__)
 typedef struct { uint32_t hi, lo; } msr_t;
 msr_t rdmsr(int addr);
 int wrmsr(int addr, msr_t msr);
@@ -307,6 +307,16 @@
 msr_t freebsd_rdmsr(int addr);
 int freebsd_wrmsr(int addr, msr_t msr);
 #endif
+#if defined(__LIBPAYLOAD__)
+#include <arch/io.h>
+#include <arch/msr.h>
+typedef struct { uint32_t hi, lo; } msr_t;
+msr_t libpayload_rdmsr(int addr);
+int libpayload_wrmsr(int addr, msr_t msr);
+#undef rdmsr
+#define rdmsr libpayload_rdmsr
+#define wrmsr libpayload_wrmsr
+#endif
 
 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || 
defined(__ppc64__)
 
Index: flashrom-patches/layout.c
===================================================================
--- flashrom-patches.orig/layout.c      2010-08-16 20:28:49.000000000 +0200
+++ flashrom-patches/layout.c   2010-08-16 20:34:41.640224400 +0200
@@ -134,6 +134,7 @@
 }
 #endif
 
+#ifndef __LIBPAYLOAD__
 int read_romlayout(char *name)
 {
        FILE *romlayout;
@@ -181,6 +182,7 @@
 
        return 0;
 }
+#endif
 
 int find_romentry(char *name)
 {
Index: flashrom-patches/udelay.c
===================================================================
--- flashrom-patches.orig/udelay.c      2010-08-16 20:28:50.000000000 +0200
+++ flashrom-patches/udelay.c   2010-08-16 20:35:45.419872400 +0200
@@ -19,6 +19,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#ifndef __LIBPAYLOAD__
+
 #include <unistd.h>
 #include <sys/time.h>
 #include <stdlib.h>
@@ -179,3 +181,15 @@
        }
 }
 
+#else 
+
+void myusec_calibrate_delay(void)
+{
+       get_cpu_speed();
+}
+
+void internal_delay(int usecs)
+{
+       udelay(usecs);
+}
+#endif
Index: flashrom-patches/flashrom.c
===================================================================
--- flashrom-patches.orig/flashrom.c    2010-08-16 20:28:50.000000000 +0200
+++ flashrom-patches/flashrom.c 2010-08-16 20:38:27.395136800 +0200
@@ -22,9 +22,11 @@
  */
 
 #include <stdio.h>
-#include <fcntl.h>
 #include <sys/types.h>
+#ifndef __LIBPAYLOAD__
+#include <fcntl.h>
 #include <sys/stat.h>
+#endif
 #include <string.h>
 #include <stdlib.h>
 #include <getopt.h>
_______________________________________________
flashrom mailing list
[email protected]
http://www.flashrom.org/mailman/listinfo/flashrom

Reply via email to