-
Notifications
You must be signed in to change notification settings - Fork 5
/
kmem.c
49 lines (43 loc) · 1.05 KB
/
kmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
void printmem(void *m, int sz);
int rkm(int fd, int offset, void *buf, int size);
int wkm(int fd, int offset, void *buf, int size);
void printmem(void *m, int sz){
unsigned char *p = (unsigned char *)m;
int i;
for (i = 0; i < sz; i++){
printf("%02x ", p[i]);
if((i+1) % 16 == 0){
printf("\n");
}
}
printf("\n");
}
/* read data from kmem */
int rkm(int fd, int offset, void *buf, int size)
{
if (lseek(fd, offset, 0) != offset){
perror("seek error\n");
exit(2);
}
if (read(fd, buf, size) != size){
perror("read error");
exit(2);
}
return size;
}
/* write data to kmem */
int wkm(int fd, int offset, void *buf, int size)
{
if (lseek(fd, offset, 0) != offset){
perror("seek error\n");
exit(2);
}
if (write(fd, buf, size) != size){
perror("write error\n");
exit(2);
}
return size;
}