Blame util.c

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