OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [util/] [memtool.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*
2
 * Copyright (c) 2011, Mentor Graphics Corporation
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
/* XXX would be better to stay closer to dd(1) parameters and functionality */
31
 
32
#include <stdio.h>
33
#include <sys/mman.h>
34
#include <errno.h>
35
#include <sys/types.h>
36
#include <sys/stat.h>
37
#include <fcntl.h>
38
#include <unistd.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <assert.h>
42
#include <sys/ioctl.h>
43
#include <getopt.h>
44
#include <limits.h>
45
 
46
#define DEVMCOMM "/dev/mcomm"
47
 
48
typedef int (*command_t)(void *mem, size_t bytes, unsigned long base);
49
 
50
static ssize_t read_mcomm_size(void)
51
{
52
        ssize_t size;
53
        unsigned long val;
54
        FILE *f;
55
        int rc;
56
 
57
        f = fopen("/sys/devices/mcomm.0/size", "r");
58
        if (!f)
59
                return -ENOENT;
60
 
61
        rc = fscanf(f, "%lx", &val);
62
        size = val;
63
        if (rc < 0) {
64
                perror("fscanf");
65
                size = -EINVAL;
66
        }
67
 
68
        fclose(f);
69
 
70
        return size;
71
}
72
 
73
static void *map(int fd, ssize_t bytes, unsigned long off)
74
{
75
        void *mem = NULL;
76
 
77
        mem = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, fd, off);
78
        if (mem == MAP_FAILED) {
79
                perror("mmap");
80
                return NULL;
81
        }
82
 
83
        return mem;
84
}
85
 
86
static void unmap(int fd, void *mem, int bytes)
87
{
88
        close(fd);
89
        munmap(mem, bytes);
90
}
91
 
92
void usage(char *name)
93
{
94
        printf("Usage: %s [options] <command>\n"
95
               "Options:\n"
96
                   "  -d, --device=<path>\n"
97
                   "          Device to open.\n"
98
                   "\n"
99
                   "  -o, --offset=<offset>\n"
100
                   "          Offset (in bytes) at which to start. Must be page-aligned.\n"
101
                   "\n"
102
                   "  -l, --length=<length>\n"
103
                   "          Number of bytes on which to operate.\n"
104
                   "          Defaults to the entire mcomm region, or 4K for other devices.\n"
105
                   "\n"
106
               "Commands:\n"
107
                   "  clear: zeroes memory\n"
108
                   "  dump:  dump memory contents as hex\n",
109
                   name);
110
        exit(1);
111
}
112
 
113
static int clear(void *mem, size_t bytes, unsigned long base)
114
{
115
        memset(mem, 0, bytes);
116
        return 0;
117
}
118
 
119
static int dump(void *addr, size_t bytes, unsigned long base)
120
{
121
        const int COLS = 4;
122
        unsigned int *data = addr;
123
        int pos, i;
124
 
125
    for (pos = 0; sizeof(int) * pos < bytes; pos += COLS) {
126
        printf("%08lx:", base + pos * sizeof(int));
127
        for (i = 0; i < COLS; i++)
128
            printf(" %08x", data[pos + i]);
129
        printf("\n");
130
    }
131
 
132
        return 0;
133
}
134
 
135
int main(int argc, char *argv[])
136
{
137
        unsigned long offset = 0;
138
        unsigned long length = ULONG_MAX;
139
        char *dev = "/dev/mem";
140
        unsigned int *mem;
141
        command_t command = NULL;
142
        ssize_t bytes;
143
        int dev_fd;
144
        int c;
145
        int rc = 0;
146
        unsigned int pagesize;
147
 
148
        while (1) {
149
                static struct option long_options[] = {
150
                        {"device", 1, 0, 'd'},
151
                        {"offset", 1, 0, 'o'},
152
                        {"length", 1, 0, 'l'},
153
                        {NULL, 0, 0, 0},
154
                };
155
                int option_index = 0;
156
 
157
                c = getopt_long(argc, argv, "d:o:l:", long_options, &option_index);
158
                if (c == -1)
159
                        break;
160
 
161
                switch (c) {
162
                case 'd':
163
                        dev = optarg;
164
                        break;
165
 
166
                case 'o':
167
                        offset = strtoul(optarg, NULL, 0);
168
                        if (offset == ULONG_MAX) {
169
                                printf("couldn't use offset\n");
170
                                usage(argv[0]);
171
                        }
172
                        pagesize = getpagesize();
173
                        if (offset & (pagesize-1)) {
174
                                printf("offset must be a multiple of 0x%x\n", pagesize);
175
                                usage(argv[0]);
176
                        }
177
                        break;
178
 
179
                case 'l':
180
                        length = strtoul(optarg, NULL, 0);
181
                        if (length == ULONG_MAX) {
182
                                printf("couldn't use length\n");
183
                                usage(argv[0]);
184
                        }
185
                        break;
186
 
187
                default:
188
                        printf("%d\n", c);
189
                        usage(argv[0]);
190
                }
191
        }
192
 
193
        if (optind < argc) {
194
                char *cmdstr = argv[optind];
195
 
196
                if (strcmp(cmdstr, "dump") == 0)
197
                        command = dump;
198
                else if (strcmp(cmdstr, "clear") == 0)
199
                        command = clear;
200
 
201
                optind++;
202
        }
203
 
204
        if ((optind < argc) || (command == NULL)) {
205
                usage(argv[0]);
206
        }
207
 
208
        dev_fd = open(dev, O_RDWR);
209
        if (dev_fd < 0) {
210
                rc = errno;
211
                perror("open");
212
                return rc;
213
        }
214
 
215
        if (strncmp(dev, DEVMCOMM, strlen(DEVMCOMM)) == 0) {
216
                bytes = read_mcomm_size();
217
                if (bytes <= 0) {
218
                        perror("read mcomm size");
219
                        goto out;
220
                }
221
        } else {
222
                bytes = 1<<12;
223
        }
224
 
225
        if (length < bytes)
226
                bytes = length;
227
 
228
        mem = map(dev_fd, bytes, offset);
229
        if (mem == NULL) {
230
                rc = -1;
231
                goto out;
232
        }
233
 
234
        rc = command(mem, bytes, offset);
235
 
236
        unmap(dev_fd, mem, bytes);
237
 
238
out:
239
        return rc;
240
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.