-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddrIntercept.py
More file actions
123 lines (96 loc) · 3.14 KB
/
addrIntercept.py
File metadata and controls
123 lines (96 loc) · 3.14 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3.5m
# -*- coding: utf-8 -*-
"""
Covered by Apache License 2.0
Copyright (C) 2018 Evgeny Chormonov (en.chormonov@gmail.com)
For more information https://github.com/ser-mk/AddressIntercept
"""
from scanf import scanf
from ocd_rpc import OpenOcd
from ocd_wrapp import OCDWrapp
import logging
PATTERN_SCANF = "id:%d | %s | addr: %x | size: %d | value: %x | st: "
PATTERN_SPRINTF = "id:%d | %s | addr: 0x%x | size: %d | value: 0x%x | st: %s\n"
COMMAND_LOAD = "LOAD"
COMMAND_STORE = "STORE"
COMMAND_NOP = "NOP!"
NO_ERROR_STATUS = "NO_ERROR"
OCD_ERROR_STATUS = "OCD_ERROR"
_ocdw = None
def initOCD(ocd: OpenOcd) -> None:
global _ocdw
_ocdw = OCDWrapp(ocd)
ocd.send("reset halt")
logging.info("connect OCD server")
class AddrCommand:
_id = -1
command = None
addr = -1
size = -1
value = -1
status = NO_ERROR_STATUS
def _checkValueSuccess(value):
if type(value) is int:
if value >= 0:
return True
return False
def _load(struct_command: AddrCommand) -> int:
global _ocdw
value = -1
try:
value = _ocdw.load(struct_command.addr, struct_command.size)
except Exception as e:
# raise
# struct_command.status = OCD_ERROR_STATUS
logging.critical(e)
else:
if not _checkValueSuccess(value):
# struct_command.status = OCD_ERROR_STATUS
value = -1
return value
def _store(struct_command: AddrCommand) -> bool:
global _ocdw
success = False
try:
success = _ocdw.store(struct_command.addr,
struct_command.size, struct_command.value)
except Exception as e:
# raise
# struct_command.status = OCD_ERROR_STATUS
logging.critical(e)
return success
def _createLine(struct_command: AddrCommand) -> str:
tmp = PATTERN_SPRINTF % (struct_command._id, struct_command.command, struct_command.addr,
struct_command.size, struct_command.value, struct_command.status)
return tmp
def _parse(line: str) -> AddrCommand:
struct = AddrCommand()
try:
_id, command, addr, size, value = scanf(PATTERN_SCANF, line)
except Exception as e:
logging.error("parse - %s - line : %s", e, line)
raise e
else:
struct._id = _id
struct.command = command
struct.addr = addr
struct.size = size
struct.value = value
finally:
return struct
def proccess(line: str) -> str:
# todo: check size of line
struct_command = _parse(line)
logging.debug("struct_command: %d %s %x %d %x %s", struct_command._id, struct_command.command,
struct_command.addr, struct_command.size, struct_command.value, struct_command.status)
# todo: check id of message
if struct_command.command == COMMAND_LOAD:
value = _load(struct_command)
if value < 0:
struct_command.status = OCD_ERROR_STATUS
else:
struct_command.value = value
elif struct_command.command == COMMAND_STORE:
if not _store(struct_command):
struct_command.status = OCD_ERROR_STATUS
return _createLine(struct_command)