~ubuntu-branches/ubuntu/saucy/biosdevname/saucy-proposed

« back to all changes in this revision

Viewing changes to src/dmidecode/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-02-23 17:58:36 UTC
  • Revision ID: james.westby@ubuntu.com-20110223175836-4f0cbcno9zm0lmdu
Tags: upstream-0.3.7
ImportĀ upstreamĀ versionĀ 0.3.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Common "util" functions
 
3
 * This file is part of the dmidecode project.
 
4
 *
 
5
 *   (C) 2002-2005 Jean Delvare <khali@linux-fr>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 *
 
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.
 
26
 */
 
27
 
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
 
 
31
#include "config.h"
 
32
 
 
33
#ifdef USE_MMAP
 
34
#include <sys/mman.h>
 
35
#ifndef MAP_FAILED
 
36
#define MAP_FAILED ((void *) -1)
 
37
#endif /* !MAP_FAILED */
 
38
#endif /* USE MMAP */
 
39
 
 
40
#include <stdio.h>
 
41
#include <stdlib.h>
 
42
#include <unistd.h>
 
43
#include <string.h>
 
44
#include <fcntl.h>
 
45
#include <errno.h>
 
46
 
 
47
#include "types.h"
 
48
#include "util.h"
 
49
 
 
50
#ifndef USE_MMAP
 
51
static int myread(int fd, u8 *buf, size_t count, const char *prefix)
 
52
{
 
53
        ssize_t r=1;
 
54
        size_t r2=0;
 
55
        
 
56
        while(r2!=count && r!=0)
 
57
        {
 
58
                r=read(fd, buf+r2, count-r2);
 
59
                if(r==-1)
 
60
                {
 
61
                        if(errno!=EINTR)
 
62
                        {
 
63
                                close(fd);
 
64
                                perror(prefix);
 
65
                                return -1;
 
66
                        }
 
67
                }
 
68
                else
 
69
                        r2+=r;
 
70
        }
 
71
        
 
72
        if(r2!=count)
 
73
        {
 
74
                close(fd);
 
75
                fprintf(stderr, "%s: Unexpected end of file\n", prefix);
 
76
                return -1;
 
77
        }
 
78
        
 
79
        return 0;
 
80
}
 
81
#endif
 
82
 
 
83
int checksum(const u8 *buf, size_t len)
 
84
{
 
85
        u8 sum=0;
 
86
        size_t a;
 
87
        
 
88
        for(a=0; a<len; a++)
 
89
                sum+=buf[a];
 
90
        return (sum==0);
 
91
}
 
92
 
 
93
/*
 
94
 * Copy a physical memory chunk into a memory buffer.
 
95
 * This function allocates memory.
 
96
 */
 
97
void *mem_chunk(size_t base, size_t len, const char *devmem)
 
98
{
 
99
        void *p;
 
100
        int fd;
 
101
#ifdef USE_MMAP
 
102
        size_t mmoffset;
 
103
        void *mmp;
 
104
#endif
 
105
        
 
106
        if((fd=open(devmem, O_RDONLY))==-1)
 
107
        {
 
108
                return NULL;
 
109
        }
 
110
        
 
111
        if((p=malloc(len))==NULL)
 
112
        {
 
113
                perror("malloc");
 
114
                return NULL;
 
115
        }
 
116
        
 
117
#ifdef USE_MMAP
 
118
#ifdef _SC_PAGESIZE
 
119
        mmoffset=base%sysconf(_SC_PAGESIZE);
 
120
#else
 
121
        mmoffset=base%getpagesize();
 
122
#endif /* _SC_PAGESIZE */
 
123
        /*
 
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.
 
127
         */
 
128
        mmp=mmap(0, mmoffset+len, PROT_READ, MAP_SHARED, fd, base-mmoffset);
 
129
        if(mmp==MAP_FAILED)
 
130
        {
 
131
                free(p);
 
132
                return NULL;
 
133
        }
 
134
        
 
135
        memcpy(p, (u8 *)mmp+mmoffset, len);
 
136
        
 
137
        if(munmap(mmp, mmoffset+len)==-1)
 
138
        {
 
139
                fprintf(stderr, "%s: ", devmem);
 
140
                perror("munmap");
 
141
        }
 
142
#else /* USE_MMAP */
 
143
        if(lseek(fd, base, SEEK_SET)==-1)
 
144
        {
 
145
                fprintf(stderr, "%s: ", devmem);
 
146
                perror("lseek");
 
147
                free(p);
 
148
                return NULL;
 
149
        }
 
150
        
 
151
        if(myread(fd, p, len, devmem)==-1)
 
152
        {
 
153
                free(p);
 
154
                return NULL;
 
155
        }
 
156
#endif /* USE_MMAP */
 
157
        
 
158
        if(close(fd)==-1)
 
159
                perror(devmem);
 
160
        
 
161
        return p;
 
162
}