169 lines
3.6 KiB
Diff
169 lines
3.6 KiB
Diff
From f64c3f549eda36d11b5690117173b0847535ebbe Mon Sep 17 00:00:00 2001
|
|
From: Takashi Iwai <tiwai@suse.de>
|
|
Date: Mon, 24 Aug 2020 14:40:17 +0200
|
|
Subject: [PATCH] Add buffer read helper using read explicitly
|
|
|
|
Since mmap can't work well with a sysfs file, we need to read the
|
|
contents explicitly via read, even if USE_MMAP is enabled.
|
|
Provide a new helper, __mem_cunk(), that does behave like mem_chunk()
|
|
but with the extra use_mmap argument to specify the method to read a
|
|
file.
|
|
|
|
Signed-off-by: Takashi Iwai <tiwai@suse.de>
|
|
CC: Thomas Renninger <trenn@suse.de>
|
|
CC: Michal Suchanek <msuchanek@suse.com>
|
|
---
|
|
src/dmidecode/util.c | 82 ++++++++++++++++++++++++++++----------------
|
|
src/dmidecode/util.h | 1 +
|
|
2 files changed, 53 insertions(+), 30 deletions(-)
|
|
|
|
diff --git a/src/dmidecode/util.c b/src/dmidecode/util.c
|
|
index ea06663..09934ea 100644
|
|
--- a/src/dmidecode/util.c
|
|
+++ b/src/dmidecode/util.c
|
|
@@ -47,7 +47,6 @@
|
|
#include "types.h"
|
|
#include "util.h"
|
|
|
|
-#ifndef USE_MMAP
|
|
static int myread(int fd, u8 *buf, size_t count, const char *prefix)
|
|
{
|
|
ssize_t r=1;
|
|
@@ -78,7 +77,6 @@ static int myread(int fd, u8 *buf, size_t count, const char *prefix)
|
|
|
|
return 0;
|
|
}
|
|
-#endif
|
|
|
|
int checksum(const u8 *buf, size_t len)
|
|
{
|
|
@@ -94,28 +92,13 @@ int checksum(const u8 *buf, size_t len)
|
|
* Copy a physical memory chunk into a memory buffer.
|
|
* This function allocates memory.
|
|
*/
|
|
-void *mem_chunk(size_t base, size_t len, const char *devmem)
|
|
-{
|
|
- void *p;
|
|
- int fd;
|
|
#ifdef USE_MMAP
|
|
+static void *mem_chunk_mmap(size_t base, size_t len, const char *devmem,
|
|
+ int fd, void *p)
|
|
+{
|
|
size_t mmoffset;
|
|
void *mmp;
|
|
-#endif
|
|
-
|
|
- if((fd=open(devmem, O_RDONLY))==-1)
|
|
- {
|
|
- return NULL;
|
|
- }
|
|
-
|
|
- if((p=malloc(len))==NULL)
|
|
- {
|
|
- perror("malloc");
|
|
- close(fd);
|
|
- return NULL;
|
|
- }
|
|
-
|
|
-#ifdef USE_MMAP
|
|
+
|
|
#ifdef _SC_PAGESIZE
|
|
mmoffset=base%sysconf(_SC_PAGESIZE);
|
|
#else
|
|
@@ -129,8 +112,6 @@ void *mem_chunk(size_t base, size_t len, const char *devmem)
|
|
mmp=mmap(0, mmoffset+len, PROT_READ, MAP_SHARED, fd, base-mmoffset);
|
|
if(mmp==MAP_FAILED)
|
|
{
|
|
- free(p);
|
|
- close(fd);
|
|
return NULL;
|
|
}
|
|
|
|
@@ -141,26 +122,67 @@ void *mem_chunk(size_t base, size_t len, const char *devmem)
|
|
fprintf(stderr, "%s: ", devmem);
|
|
perror("munmap");
|
|
}
|
|
-#else /* USE_MMAP */
|
|
+
|
|
+ return p;
|
|
+}
|
|
+#endif /* USE_MMAP */
|
|
+
|
|
+static void *mem_chunk_read(size_t base, size_t len, const char *devmem,
|
|
+ int fd, void *p)
|
|
+{
|
|
if(lseek(fd, base, SEEK_SET)==-1)
|
|
{
|
|
fprintf(stderr, "%s: ", devmem);
|
|
perror("lseek");
|
|
- free(p);
|
|
- close(fd);
|
|
return NULL;
|
|
}
|
|
|
|
if(myread(fd, p, len, devmem)==-1)
|
|
{
|
|
- free(p);
|
|
- close(fd);
|
|
return NULL;
|
|
}
|
|
-#endif /* USE_MMAP */
|
|
+
|
|
+ return p;
|
|
+}
|
|
+
|
|
+void *__mem_chunk(size_t base, size_t len, const char *devmem, int use_mmap)
|
|
+{
|
|
+ void *ret;
|
|
+ void *p;
|
|
+ int fd;
|
|
+
|
|
+#ifndef USE_MMAP
|
|
+ use_mmap = 0;
|
|
+#endif
|
|
+
|
|
+ if((fd=open(devmem, O_RDONLY))==-1)
|
|
+ {
|
|
+ return NULL;
|
|
+ }
|
|
|
|
+ if((p=malloc(len))==NULL)
|
|
+ {
|
|
+ perror("malloc");
|
|
+ close(fd);
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+#ifdef USE_MMAP
|
|
+ if (use_mmap)
|
|
+ ret = mem_chunk_mmap(base, len, devmem, fd, p);
|
|
+ else
|
|
+#endif
|
|
+ ret = mem_chunk_read(base, len, devmem, fd, p);
|
|
+
|
|
if(close(fd)==-1)
|
|
perror(devmem);
|
|
+ if (!ret)
|
|
+ free(p);
|
|
|
|
- return p;
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+void *mem_chunk(size_t base, size_t len, const char *devmem)
|
|
+{
|
|
+ return __mem_chunk(base, len, devmem, 1);
|
|
}
|
|
diff --git a/src/dmidecode/util.h b/src/dmidecode/util.h
|
|
index b546f64..90c411e 100644
|
|
--- a/src/dmidecode/util.h
|
|
+++ b/src/dmidecode/util.h
|
|
@@ -6,3 +6,4 @@
|
|
|
|
int checksum(const u8 *buf, size_t len);
|
|
void *mem_chunk(size_t base, size_t len, const char *devmem);
|
|
+void *__mem_chunk(size_t base, size_t len, const char *devmem, int use_mmap);
|
|
--
|
|
2.44.0
|
|
|