This repository was archived by the owner on Jan 1, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemScan.java
More file actions
Latest commit
204 lines (194 loc) · 7.84 KB
/
Copy pathMemScan.java
File metadata and controls
204 lines (194 loc) · 7.84 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) Steven P. Goldsmith. All rights reserved.
*/
packagecom.codeferm.periphery.mmio;
importstaticcom.codeferm.periphery.Common.cString;
importcom.codeferm.periphery.Gpio;
importstaticcom.codeferm.periphery.Gpio.GPIO_BIAS_DEFAULT;
importstaticcom.codeferm.periphery.Gpio.GPIO_BIAS_DISABLE;
importstaticcom.codeferm.periphery.Gpio.GPIO_BIAS_PULL_DOWN;
importstaticcom.codeferm.periphery.Gpio.GPIO_BIAS_PULL_UP;
importstaticcom.codeferm.periphery.Gpio.GPIO_DIR_IN;
importstaticcom.codeferm.periphery.Gpio.GPIO_DIR_OUT;
importstaticcom.codeferm.periphery.Gpio.GPIO_DRIVE_DEFAULT;
importstaticcom.codeferm.periphery.Gpio.GPIO_EDGE_NONE;
importcom.codeferm.periphery.Mmio;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.concurrent.Callable;
importorg.apache.logging.log4j.LogManager;
importpicocli.CommandLine;
/**
* Scan memory for changes based on start address, range and GPIO chip and line.
*
* Make sure you disable all hardware in armbian-config System, Hardware and remove console=serial from /boot/armbianEnv.txt. You
* want multi-function pins to act as GPIO pins.
*
* @author Steven P. Goldsmith
* @version 1.0.0
* @since 1.0.0
*/
@CommandLine.Command(name = "memscan", mixinStandardHelpOptions = true, version = "memscan 1.0.0",
description = "Use GPIO device to detect memory changes")
publicclassMemScanimplementsCallable<Integer> {
/**
* Logger.
*/
privatefinalorg.apache.logging.log4j.Loggerlogger = LogManager.getLogger(MemScan.class);
/**
* MMIO path.
*/
@CommandLine.Option(names = {"-p", "--path"}, description = "Path defaults to /dev/mem")
privateStringpath = "/dev/mem";
/**
* Memory address.
*/
@CommandLine.Option(names = {"-a", "--address"}, description = "Memorry address defaults to 0x00")
privatelongaddress = 0x00;
/**
* Memory size to scan.
*/
@CommandLine.Option(names = {"-w", "--words"}, description = "32 bit words to read defaults to 0x01")
privatelongwords = 0x01;
/**
* Device option.
*/
@CommandLine.Option(names = {"-d", "--device"}, description = "GPIO device defaults to 0")
privateintdevice = 0;
/**
* Line option.
*/
@CommandLine.Option(names = {"-l", "--line"}, description = "GPIO line defaults to 1")
privateintline = 1;
/**
* Return values from all registers.
*
* @param mmioHandle MMIO handle.
* @return List of register values.
*/
publicList<Integer> getRegValues(finalLongmmioHandle) {
finalvarlist = newArrayList<Integer>();
finalvarvalue = newint[1];
for (longi = 0; i < words; i++) {
Mmio.mmioRead32(mmioHandle, i * 4, value);
list.add(value[0]);
}
returnlist;
}
/**
* Compare list values and log difference.
*
* @param list1 First list.
* @param list2 Second list.
* @param text Description text.
*/
publicvoidlistDiff(finalList<Integer> list1, finalList<Integer> list2, finalStringtext) {
for (inti = 0; i < list1.size(); i++) {
if (!list1.get(i).equals(list2.get(i))) {
intdiff;
if (list1.get(i) > list2.get(i)) {
diff = list1.get(i) - list2.get(i);
} else {
diff = list2.get(i) - list1.get(i);
}
logger.info(String.format("%s difference found at offset 0x%08x before 0x%08x after 0x%08x difference 0x%08x", text,
i * 4, list1.get(i), list2.get(i), diff));
}
}
}
/**
* Use GPIO device to detect configuration changes.
*
* @param mmioHandle MMIO handle.
*/
publicvoiddetectMode(finallongmmioHandle) {
finalvardev = String.format("/dev/gpiochip%d", device);
// Set pin for input, output and look for delta
try (finalvargpio = newGpio(dev, line, newGpio.GpioConfig().setBias(GPIO_BIAS_DEFAULT).setDirection(GPIO_DIR_IN).setDrive(
GPIO_DRIVE_DEFAULT).setEdge(GPIO_EDGE_NONE).setInverted(false).setLabel(cString(
MemScan.class.getSimpleName())))) {
finalvarlist1 = getRegValues(mmioHandle);
Gpio.gpioSetDirection(gpio.getHandle(), GPIO_DIR_OUT);
finalvarlist2 = getRegValues(mmioHandle);
// Show the register delta
listDiff(list1, list2, "Mode");
} catch (RuntimeExceptione) {
logger.error(String.format("Device %d line %d Error %s", device, line, e.getMessage()));
}
}
/**
* Use GPIO device to detect data changes.
*
* @param mmioHandle MMIO handle.
*/
publicvoiddetectData(finallongmmioHandle) {
finalvardev = String.format("/dev/gpiochip%d", device);
// Set pin for input, output and look for delta
try (finalvargpio = newGpio(dev, line, newGpio.GpioConfig().setBias(GPIO_BIAS_DEFAULT).setDirection(GPIO_DIR_OUT).setDrive(
GPIO_DRIVE_DEFAULT).setEdge(GPIO_EDGE_NONE).setInverted(false).setLabel(cString(
MemScan.class.getSimpleName())))) {
Gpio.gpioWrite(gpio.getHandle(), false);
finalvarlist1 = getRegValues(mmioHandle);
Gpio.gpioWrite(gpio.getHandle(), true);
finalvarlist2 = getRegValues(mmioHandle);
// Show the register delta
listDiff(list1, list2, "Data");
} catch (RuntimeExceptione) {
logger.error(String.format("Device %d line %d Error %s", device, line, e.getMessage()));
}
}
/**
* Use GPIO device to detect pull changes.
*
* @param mmioHandle MMIO handle.
*/
publicvoiddetectPull(finallongmmioHandle) {
finalvardev = String.format("/dev/gpiochip%d", device);
// Set pin for input, output and look for delta
try (finalvargpio = newGpio(dev, line, newGpio.GpioConfig().setBias(GPIO_BIAS_DISABLE).setDirection(GPIO_DIR_IN).setDrive(
GPIO_DRIVE_DEFAULT).setEdge(GPIO_EDGE_NONE).setInverted(false).setLabel(cString(
MemScan.class.getSimpleName())))) {
varlist1 = getRegValues(mmioHandle);
Gpio.gpioSetBias(gpio.getHandle(), GPIO_BIAS_PULL_UP);
varlist2 = getRegValues(mmioHandle);
// Show the register delta
listDiff(list1, list2, "Pull up");
list1 = getRegValues(mmioHandle);
Gpio.gpioSetBias(gpio.getHandle(), GPIO_BIAS_PULL_DOWN);
list2 = getRegValues(mmioHandle);
// Show the register delta
listDiff(list1, list2, "Pull down");
} catch (RuntimeExceptione) {
logger.error(String.format("Device %d line %d Error %s", device, line, e.getMessage()));
}
}
/**
* Detect changes made by GPIO at register level.
*
* @return Exit code.
* @throws InterruptedException Possible exception.
*/
@Override
publicIntegercall() throwsInterruptedException {
varexitCode = 0;
logger.debug(String.format("Memory address 0x%08x words 0x%08x", address, words));
try (finalvarmmio = newMmio(address, words * 4, path)) {
detectMode(mmio.getHandle());
detectData(mmio.getHandle());
detectPull(mmio.getHandle());
} catch (RuntimeExceptione) {
logger.error(e.getMessage());
exitCode = 1;
}
returnexitCode;
}
/**
* Main parsing, error handling and handling user requests for usage help or version help are done with one line of code.
*
* @param args Argument list.
*/
publicstaticvoidmain(String... args) {
System.exit(newCommandLine(newMemScan()).registerConverter(Long.class, Long::decode).registerConverter(Long.TYPE,
Long::decode).execute(args));
}
}