From 1c52faa693c512a01a9f9fb601940f6bf70ae20e Mon Sep 17 00:00:00 2001 From: "Michael S. Tsirkin" Date: Thu, 6 Jan 2005 10:15:49 +0000 Subject: [PATCH] r1483: 1. fix endian-ness for configuration cycles data 2. more sanity checks for mmap result --- mtcr.h | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/mtcr.h b/mtcr.h index 2e1e887..77ac39c 100644 --- a/mtcr.h +++ b/mtcr.h @@ -52,6 +52,7 @@ #include #include +#include #include #include #include @@ -69,6 +70,30 @@ #define __cpu_to_be32(x) htonl(x) #endif +#if __BYTE_ORDER == __LITTLE_ENDIAN +#ifndef __cpu_to_le32 +#define __cpu_to_le32(x) (x) +#endif +#ifndef __le32_to_cpu +#define __le32_to_cpu(x) (x) +#endif +#elif __BYTE_ORDER == __BIG_ENDIAN +#ifndef __cpu_to_le32 +#define __cpu_to_le32(x) bswap_32(x) +#endif +#ifndef __le32_to_cpu +#define __le32_to_cpu(x) bswap_32(x) +#endif +#else +#ifndef __cpu_to_le32 +#define __cpu_to_le32(x) bswap_32(__cpu_to_be32(x)) +#endif +#ifndef __le32_to_cpu +#define __le32_to_cpu(x) __be32_to_cpu(bswap_32(x)) +#endif +#endif + + #ifdef __cplusplus extern "C" { #endif @@ -170,7 +195,7 @@ mfind(const char* name, off_t* offset_p ) { fprintf(stderr,"proc: parse error (read only %d items)\n", cnt); fprintf(stderr,"the offending line in " "/proc/bus/pci/devices" " is " - "\"%.*s\"\n", sizeof(buf), buf); + "\"%.*s\"\n", (int)sizeof(buf), buf); goto error; } bus = dfn >> 8U; @@ -193,7 +218,7 @@ mfind(const char* name, off_t* offset_p ) "cnt=%d, size[0]=%#x, size[1]=%#x\n", cnt,size[0],size[1]); fprintf(stderr,"the offending line in " "/proc/bus/pci/devices" " is " - "\"%.*s\"\n", sizeof(buf), buf); + "\"%.*s\"\n", (int)sizeof(buf), buf); goto error; } @@ -250,7 +275,7 @@ mfile *mopen(const char *name) mf->ptr = mmap(NULL, 0x100000, PROT_READ | PROT_WRITE, MAP_SHARED, mf->fd, offset); - if (mf->ptr == MAP_FAILED) goto map_failed; + if ( (! mf->ptr) || (mf->ptr == MAP_FAILED) ) goto map_failed; #else goto open_failed; #endif @@ -300,6 +325,7 @@ int mread4(mfile *mf, unsigned int offset, u_int32_t *value) #if CONFIG_ENABLE_PCICONF { int rc; + offset=__cpu_to_le32(offset); rc=pwrite(mf->fd, &offset, 4, 22*4); if (rc < 0) { @@ -313,6 +339,7 @@ int mread4(mfile *mf, unsigned int offset, u_int32_t *value) perror("read value"); return rc; } + *value=__le32_to_cpu(*value); return rc; } #else @@ -335,6 +362,7 @@ int mwrite4(mfile *mf, unsigned int offset, u_int32_t value) #if CONFIG_ENABLE_PCICONF { int rc; + offset=__cpu_to_le32(offset); rc=pwrite(mf->fd, &offset, 4, 22*4); if (rc < 0) { @@ -342,6 +370,7 @@ int mwrite4(mfile *mf, unsigned int offset, u_int32_t value) return rc; } if (rc!=4) return 0; + value=__cpu_to_le32(value); rc=pwrite(mf->fd, &value, 4, 23*4); if (rc < 0) { -- 2.41.0