2
* Common "util" functions
3
* This file is part of the dmidecode project.
5
* (C) 2002-2005 Jean Delvare <khali@linux-fr>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* For the avoidance of doubt the "preferred form" of this code is one which
22
* is in an open unpatent encumbered format. Where cryptographic key signing
23
* forms part of the process of creating an executable the information
24
* including keys needed to generate an equivalently functional executable
25
* are deemed to be part of the source code.
28
#include <sys/types.h>
36
#define MAP_FAILED ((void *) -1)
37
#endif /* !MAP_FAILED */
51
static int myread(int fd, u8 *buf, size_t count, const char *prefix)
56
while(r2!=count && r!=0)
58
r=read(fd, buf+r2, count-r2);
75
fprintf(stderr, "%s: Unexpected end of file\n", prefix);
83
int checksum(const u8 *buf, size_t len)
94
* Copy a physical memory chunk into a memory buffer.
95
* This function allocates memory.
97
void *mem_chunk(size_t base, size_t len, const char *devmem)
106
if((fd=open(devmem, O_RDONLY))==-1)
111
if((p=malloc(len))==NULL)
119
mmoffset=base%sysconf(_SC_PAGESIZE);
121
mmoffset=base%getpagesize();
122
#endif /* _SC_PAGESIZE */
124
* Please note that we don't use mmap() for performance reasons here,
125
* but to workaround problems many people encountered when trying
126
* to read from /dev/mem using regular read() calls.
128
mmp=mmap(0, mmoffset+len, PROT_READ, MAP_SHARED, fd, base-mmoffset);
135
memcpy(p, (u8 *)mmp+mmoffset, len);
137
if(munmap(mmp, mmoffset+len)==-1)
139
fprintf(stderr, "%s: ", devmem);
143
if(lseek(fd, base, SEEK_SET)==-1)
145
fprintf(stderr, "%s: ", devmem);
151
if(myread(fd, p, len, devmem)==-1)
156
#endif /* USE_MMAP */