summaryrefslogtreecommitdiff
path: root/other/Kermit/lib/rwKernel.cpp
blob: 4077ff58dcf76cf6f625e2b286335e9ebf1c733c (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * rwKernel.cpp:
 * written by palmers / teso
 */
#include <rwKernel.hpp>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <iostream>


  bool rwKernel::openFile (int w)
  {
    int			a = 0;
    void		*tmp = NULL;
    char		*file = NULL;

    if (w == DEVMEM)
      file = "/dev/mem";
    else if (w == PROCKCORE)
      file = "/proc/kcore";

    if ((a = open (file, O_RDWR)) <= 0)
      {
        cerr << "open error" << endl;
        abort ();
      }

    if ((tmp = mmap (NULL, 0x40000000, PROT_READ | \
	PROT_WRITE, MAP_SHARED, a, 0xc0000000 - mem_conf)) == (void *) -1)
      {
        cerr << "mmap failed" << endl;
        abort ();
      }
    fd = (char *) tmp;
    return true;
  }


  void rwKernel::setOffset (int x)
  {
    switch (x)
      {
        case CONF_1GB:
	  mem_conf = 0xC0000000;
	  break;
        case CONF_2GB:
	  mem_conf = 0x80000000;
	  break;
        case CONF_3GB:
	  mem_conf = 0x40000000;
	  break;
        case IGNORE:
	  mem_conf = 0xC0000000;
	  break;
	default:
	  mem_conf = 0xC0000000;
	  break;
      }
  }


  void rwKernel::closeFile ()
  {
    munmap (fd, 0x40000000);
  }


  rwKernel::rwKernel ()
  {
    setOffset (CONF_1GB);
    openFile (DEVMEM);
  }


  rwKernel::rwKernel (int file, int off)
  {
    if (file == PROCKCORE)
      off = IGNORE;
    setOffset (off);
    openFile (file);
  }


  rwKernel::~rwKernel ()
  {
    closeFile ();
  }


  void rwKernel::read (unsigned char *dest, unsigned int len, \
		unsigned int offset)
  {
    offset -= mem_conf;
    copy (fd + offset, fd + offset + len, dest);
  }


  void rwKernel::write (unsigned char *src, unsigned int len, \
		unsigned int offset)
  {
    offset -= mem_conf;
    copy (src, src + len, fd + offset); 
  }