pandora-misc: Add op_test_inputs to recipe.
[openpandora.oe.git] / recipes / omap3-deviceid / files / mem.c
1 /*\r
2   Copyright (C) 2009 Mans Rullgard\r
3 \r
4   Permission is hereby granted, free of charge, to any person\r
5   obtaining a copy of this software and associated documentation files\r
6   (the "Software"), to deal in the Software without restriction,\r
7   including without limitation the rights to use, copy, modify, merge,\r
8   publish, distribute, sublicense, and/or sell copies of the Software,\r
9   and to permit persons to whom the Software is furnished to do so,\r
10   subject to the following conditions:\r
11 \r
12   The above copyright notice and this permission notice shall be\r
13   included in all copies or substantial portions of the Software.\r
14 \r
15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
16   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
17   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
18   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
19   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\r
20   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\r
21   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22 */\r
23 \r
24 #include <stdio.h>\r
25 #include <stdlib.h>\r
26 #include <sys/mman.h>\r
27 #include <unistd.h>\r
28 #include <fcntl.h>\r
29 \r
30 static void die(void)\r
31 {\r
32     fprintf(stderr, "usage: memdump {-b|-h|-w} addr count\n");\r
33     exit(1);\r
34 }\r
35 \r
36 static void dump_word(void *p, long count, unsigned long offset)\r
37 {\r
38     unsigned *d = p;\r
39     int i;\r
40 \r
41     while (count > 0) {\r
42         printf("%08lx:", offset);\r
43         for (i = 0; i < 4 && count--; i++)\r
44             printf(" %08x", *d++);\r
45         printf("\n");\r
46         offset += 16;\r
47     }\r
48 }\r
49 \r
50 static void dump_half(void *p, long count, unsigned long offset)\r
51 {\r
52     unsigned short *d = p;\r
53     int i;\r
54 \r
55     while (count > 0) {\r
56         printf("%08lx:", offset);\r
57         for (i = 0; i < 8 && count--; i++)\r
58             printf(" %04x", *d++);\r
59         printf("\n");\r
60         offset += 16;\r
61     }\r
62 }\r
63 \r
64 static void dump_byte(void *p, long count, unsigned long offset)\r
65 {\r
66     unsigned char *d = p;\r
67     int i;\r
68 \r
69     while (count > 0) {\r
70         printf("%08lx:", offset);\r
71         for (i = 0; i < 16 && count--; i++)\r
72             printf(" %02x", *d++);\r
73         printf("\n");\r
74         offset += 16;\r
75     }\r
76 }\r
77 \r
78 int main(int argc, char **argv)\r
79 {\r
80     void (*dump_fun)(void *p, long count, unsigned long offset) = NULL;\r
81     unsigned long offset, map_offset, map_size;\r
82     long count;\r
83     long pagesize;\r
84     void *mem, *data;\r
85     int type = 4;\r
86     int fd;\r
87 \r
88     if (argc < 3) {\r
89         die();\r
90     }\r
91 \r
92     if (*argv[1] == '-') {\r
93         if (argv[1][1] && argv[1][2])\r
94             die();\r
95 \r
96         switch (argv[1][1]) {\r
97         case 'b': type = 1; dump_fun = dump_byte; break;\r
98         case 'h': type = 2; dump_fun = dump_half; break;\r
99         case 'w': type = 4; dump_fun = dump_word; break;\r
100         default:  die();\r
101         }\r
102 \r
103         argc--;\r
104         argv++;\r
105     }\r
106 \r
107     if (argc < 3) {\r
108         die();\r
109     }\r
110 \r
111     offset = strtoul(argv[1], NULL, 0);\r
112     count  = strtoul(argv[2], NULL, 0);\r
113 \r
114     fd = open("/dev/mem", O_RDONLY);\r
115     if (fd == -1) {\r
116         perror("/dev/mem");\r
117         return 1;\r
118     }\r
119 \r
120     pagesize = sysconf(_SC_PAGESIZE);\r
121     map_offset = offset & ~(pagesize - 1);\r
122     map_size = count * type + (offset & (pagesize - 1));\r
123     map_size = (map_size + pagesize - 1) & ~(pagesize - 1);\r
124 \r
125     mem = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, map_offset);\r
126     if (mem == MAP_FAILED) {\r
127         perror("mmap");\r
128         exit(1);\r
129     }\r
130 \r
131     data = (char*)mem + (offset - map_offset);\r
132 \r
133     dump_fun(data, count, offset);\r
134 \r
135     munmap(mem, map_size);\r
136     close(fd);\r
137 \r
138     return 0;\r
139 }\r
140 \r