|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* Common "util" functions
|
|
Packit Service |
e18529 |
* This file is part of the dmidecode project.
|
|
Packit Service |
e18529 |
*
|
|
Packit Service |
e18529 |
* Copyright (C) 2002-2018 Jean Delvare <jdelvare@suse.de>
|
|
Packit Service |
e18529 |
*
|
|
Packit Service |
e18529 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
e18529 |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
e18529 |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit Service |
e18529 |
* (at your option) any later version.
|
|
Packit Service |
e18529 |
*
|
|
Packit Service |
e18529 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
e18529 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
e18529 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
e18529 |
* GNU General Public License for more details.
|
|
Packit Service |
e18529 |
*
|
|
Packit Service |
e18529 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
e18529 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
e18529 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit Service |
e18529 |
*
|
|
Packit Service |
e18529 |
* For the avoidance of doubt the "preferred form" of this code is one which
|
|
Packit Service |
e18529 |
* is in an open unpatent encumbered format. Where cryptographic key signing
|
|
Packit Service |
e18529 |
* forms part of the process of creating an executable the information
|
|
Packit Service |
e18529 |
* including keys needed to generate an equivalently functional executable
|
|
Packit Service |
e18529 |
* are deemed to be part of the source code.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#include <sys/types.h>
|
|
Packit Service |
e18529 |
#include <sys/stat.h>
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#include "config.h"
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#ifdef USE_MMAP
|
|
Packit Service |
e18529 |
#include <sys/mman.h>
|
|
Packit Service |
e18529 |
#ifndef MAP_FAILED
|
|
Packit Service |
e18529 |
#define MAP_FAILED ((void *) -1)
|
|
Packit Service |
e18529 |
#endif /* !MAP_FAILED */
|
|
Packit Service |
e18529 |
#endif /* USE MMAP */
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#include <stdio.h>
|
|
Packit Service |
e18529 |
#include <stdlib.h>
|
|
Packit Service |
e18529 |
#include <unistd.h>
|
|
Packit Service |
e18529 |
#include <string.h>
|
|
Packit Service |
e18529 |
#include <fcntl.h>
|
|
Packit Service |
e18529 |
#include <errno.h>
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#include "types.h"
|
|
Packit Service |
e18529 |
#include "util.h"
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
static int myread(int fd, u8 *buf, size_t count, const char *prefix)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
ssize_t r = 1;
|
|
Packit Service |
e18529 |
size_t r2 = 0;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
while (r2 != count && r != 0)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
r = read(fd, buf + r2, count - r2);
|
|
Packit Service |
e18529 |
if (r == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
if (errno != EINTR)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
perror(prefix);
|
|
Packit Service |
e18529 |
return -1;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
else
|
|
Packit Service |
e18529 |
r2 += r;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (r2 != count)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: Unexpected end of file\n", prefix);
|
|
Packit Service |
e18529 |
return -1;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
return 0;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
int checksum(const u8 *buf, size_t len)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
u8 sum = 0;
|
|
Packit Service |
e18529 |
size_t a;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
for (a = 0; a < len; a++)
|
|
Packit Service |
e18529 |
sum += buf[a];
|
|
Packit Service |
e18529 |
return (sum == 0);
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* Reads all of file from given offset, up to max_len bytes.
|
|
Packit Service |
e18529 |
* A buffer of at most max_len bytes is allocated by this function, and
|
|
Packit Service |
e18529 |
* needs to be freed by the caller.
|
|
Packit Service |
e18529 |
* This provides a similar usage model to mem_chunk()
|
|
Packit Service |
e18529 |
*
|
|
Packit Service |
e18529 |
* Returns a pointer to the allocated buffer, or NULL on error, and
|
|
Packit Service |
e18529 |
* sets max_len to the length actually read.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
void *read_file(off_t base, size_t *max_len, const char *filename)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
struct stat statbuf;
|
|
Packit Service |
e18529 |
int fd;
|
|
Packit Service |
e18529 |
u8 *p;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* Don't print error message on missing file, as we will try to read
|
|
Packit Service |
e18529 |
* files that may or may not be present.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
if ((fd = open(filename, O_RDONLY)) == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
if (errno != ENOENT)
|
|
Packit Service |
e18529 |
perror(filename);
|
|
Packit Service |
e18529 |
return NULL;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* Check file size, don't allocate more than can be read.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
if (fstat(fd, &statbuf) == 0)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
if (base >= statbuf.st_size)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: Can't read data beyond EOF\n",
|
|
Packit Service |
e18529 |
filename);
|
|
Packit Service |
e18529 |
p = NULL;
|
|
Packit Service |
e18529 |
goto out;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
if (*max_len > (size_t)statbuf.st_size - base)
|
|
Packit Service |
e18529 |
*max_len = statbuf.st_size - base;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if ((p = malloc(*max_len)) == NULL)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
perror("malloc");
|
|
Packit Service |
e18529 |
goto out;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (lseek(fd, base, SEEK_SET) == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", filename);
|
|
Packit Service |
e18529 |
perror("lseek");
|
|
Packit Service |
e18529 |
goto err_free;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (myread(fd, p, *max_len, filename) == 0)
|
|
Packit Service |
e18529 |
goto out;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
err_free:
|
|
Packit Service |
e18529 |
free(p);
|
|
Packit Service |
e18529 |
p = NULL;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
out:
|
|
Packit Service |
e18529 |
if (close(fd) == -1)
|
|
Packit Service |
e18529 |
perror(filename);
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
return p;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
ebf1ba |
static void safe_memcpy(void *dest, const void *src, size_t n)
|
|
Packit Service |
ebf1ba |
{
|
|
Packit Service |
ebf1ba |
#ifdef USE_SLOW_MEMCPY
|
|
Packit Service |
ebf1ba |
size_t i;
|
|
Packit Service |
ebf1ba |
|
|
Packit Service |
ebf1ba |
for (i = 0; i < n; i++)
|
|
Packit Service |
ebf1ba |
*((u8 *)dest + i) = *((const u8 *)src + i);
|
|
Packit Service |
ebf1ba |
#else
|
|
Packit Service |
ebf1ba |
memcpy(dest, src, n);
|
|
Packit Service |
ebf1ba |
#endif
|
|
Packit Service |
ebf1ba |
}
|
|
Packit Service |
ebf1ba |
|
|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* Copy a physical memory chunk into a memory buffer.
|
|
Packit Service |
e18529 |
* This function allocates memory.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
void *mem_chunk(off_t base, size_t len, const char *devmem)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
void *p;
|
|
Packit Service |
e18529 |
int fd;
|
|
Packit Service |
e18529 |
#ifdef USE_MMAP
|
|
Packit Service |
e18529 |
struct stat statbuf;
|
|
Packit Service |
e18529 |
off_t mmoffset;
|
|
Packit Service |
e18529 |
void *mmp;
|
|
Packit Service |
e18529 |
#endif
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if ((fd = open(devmem, O_RDONLY)) == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
perror(devmem);
|
|
Packit Service |
e18529 |
return NULL;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if ((p = malloc(len)) == NULL)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
perror("malloc");
|
|
Packit Service |
e18529 |
goto out;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#ifdef USE_MMAP
|
|
Packit Service |
e18529 |
if (fstat(fd, &statbuf) == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", devmem);
|
|
Packit Service |
e18529 |
perror("stat");
|
|
Packit Service |
e18529 |
goto err_free;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* mmap() will fail with SIGBUS if trying to map beyond the end of
|
|
Packit Service |
e18529 |
* the file.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
if (S_ISREG(statbuf.st_mode) && base + (off_t)len > statbuf.st_size)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "mmap: Can't map beyond end of file %s\n",
|
|
Packit Service |
e18529 |
devmem);
|
|
Packit Service |
e18529 |
goto err_free;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
#ifdef _SC_PAGESIZE
|
|
Packit Service |
e18529 |
mmoffset = base % sysconf(_SC_PAGESIZE);
|
|
Packit Service |
e18529 |
#else
|
|
Packit Service |
e18529 |
mmoffset = base % getpagesize();
|
|
Packit Service |
e18529 |
#endif /* _SC_PAGESIZE */
|
|
Packit Service |
e18529 |
/*
|
|
Packit Service |
e18529 |
* Please note that we don't use mmap() for performance reasons here,
|
|
Packit Service |
e18529 |
* but to workaround problems many people encountered when trying
|
|
Packit Service |
e18529 |
* to read from /dev/mem using regular read() calls.
|
|
Packit Service |
e18529 |
*/
|
|
Packit Service |
e18529 |
mmp = mmap(NULL, mmoffset + len, PROT_READ, MAP_SHARED, fd, base - mmoffset);
|
|
Packit Service |
e18529 |
if (mmp == MAP_FAILED)
|
|
Packit Service |
e18529 |
goto try_read;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
ebf1ba |
safe_memcpy(p, (u8 *)mmp + mmoffset, len);
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (munmap(mmp, mmoffset + len) == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", devmem);
|
|
Packit Service |
e18529 |
perror("munmap");
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
goto out;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
try_read:
|
|
Packit Service |
e18529 |
#endif /* USE_MMAP */
|
|
Packit Service |
e18529 |
if (lseek(fd, base, SEEK_SET) == -1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", devmem);
|
|
Packit Service |
e18529 |
perror("lseek");
|
|
Packit Service |
e18529 |
goto err_free;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (myread(fd, p, len, devmem) == 0)
|
|
Packit Service |
e18529 |
goto out;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
err_free:
|
|
Packit Service |
e18529 |
free(p);
|
|
Packit Service |
e18529 |
p = NULL;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
out:
|
|
Packit Service |
e18529 |
if (close(fd) == -1)
|
|
Packit Service |
e18529 |
perror(devmem);
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
return p;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
int write_dump(size_t base, size_t len, const void *data, const char *dumpfile, int add)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
FILE *f;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
f = fopen(dumpfile, add ? "r+b" : "wb");
|
|
Packit Service |
e18529 |
if (!f)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", dumpfile);
|
|
Packit Service |
e18529 |
perror("fopen");
|
|
Packit Service |
e18529 |
return -1;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (fseek(f, base, SEEK_SET) != 0)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", dumpfile);
|
|
Packit Service |
e18529 |
perror("fseek");
|
|
Packit Service |
e18529 |
goto err_close;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (fwrite(data, len, 1, f) != 1)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", dumpfile);
|
|
Packit Service |
e18529 |
perror("fwrite");
|
|
Packit Service |
e18529 |
goto err_close;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (fclose(f))
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
fprintf(stderr, "%s: ", dumpfile);
|
|
Packit Service |
e18529 |
perror("fclose");
|
|
Packit Service |
e18529 |
return -1;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
return 0;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
err_close:
|
|
Packit Service |
e18529 |
fclose(f);
|
|
Packit Service |
e18529 |
return -1;
|
|
Packit Service |
e18529 |
}
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
/* Returns end - start + 1, assuming start < end */
|
|
Packit Service |
e18529 |
u64 u64_range(u64 start, u64 end)
|
|
Packit Service |
e18529 |
{
|
|
Packit Service |
e18529 |
u64 res;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
res.h = end.h - start.h;
|
|
Packit Service |
e18529 |
res.l = end.l - start.l;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
if (end.l < start.l)
|
|
Packit Service |
e18529 |
res.h--;
|
|
Packit Service |
e18529 |
if (++res.l == 0)
|
|
Packit Service |
e18529 |
res.h++;
|
|
Packit Service |
e18529 |
|
|
Packit Service |
e18529 |
return res;
|
|
Packit Service |
e18529 |
}
|