-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
189 lines (155 loc) · 3.84 KB
/
main.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
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
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/watchdog.h"
#include "pico/float.h"
// #include "hardware/regs/glitch_detector.h"
#include "hardware/regs/powman.h"
#include "hardware/structs/powman.h"
#include "hardware/structs/otp.h"
#include "rp2350_playground.h"
#include "display/display.h"
#include <stdio.h>
#pragma GCC push_options
#pragma GCC optimize("O0")
#define OUTER_LOOP_CNT 200
#define INNER_LOOP_CNT 200
#define nop() __asm__ volatile ("nop")
void __not_in_flash_func(glitch_loop)(void)
{
// volatile register uint32_t i, j;
// volatile register uint32_t cnt;
uint32_t blink_status = 1;
bool glitch_detector_triggered = (powman_hw->chip_reset & 0x04000000) != 0;
if (glitch_detector_triggered)
{
uart_putc_raw(TARGET_UART_INSTANCE, 'G');
}
else
{
uart_putc_raw(TARGET_UART_INSTANCE, 'R');
}
// if (!glitch_detector_armed())
// {
// uart_putc_raw(TARGET_UART_INSTANCE, 'S');
// }
// else
// {
// uart_putc_raw(TARGET_UART_INSTANCE, 'F');
// }
watchdog_update();
// cnt = 0;
uint32_t cnt = 0, i ,j;
watchdog_update();
gpio_put(TRIGGER_PIN, 1);
sleep_ms(1);
watchdog_update();
gpio_put(TRIGGER_PIN, 0);
gpio_put(TRIGGER2_PIN, 1);
for (i = 0; i < OUTER_LOOP_CNT; i++)
{
for (j = 0; j < INNER_LOOP_CNT; j++)
{
cnt++;
}
}
gpio_put(TRIGGER2_PIN, 0);
// const bool glitch_detected = i != OUTER_LOOP_CNT || j != INNER_LOOP_CNT || cnt != (OUTER_LOOP_CNT * INNER_LOOP_CNT);
// Check for glitch
if (i != OUTER_LOOP_CNT || j != INNER_LOOP_CNT || cnt != (OUTER_LOOP_CNT * INNER_LOOP_CNT))
{
// Q indicates successful glitch
watchdog_update();
uart_putc_raw(uart0, 'Q');
}
else
{
// N indicates regular execution
uart_putc_raw(uart0, 'N');
}
watchdog_update();
// char buffer[20];
// snprintf(buffer, sizeof(buffer), "%d, %d, %d", i, j, cnt);
// uart_puts(uart0, buffer);
}
#pragma GCC pop_options
void __not_in_flash_func(glitch_before_loading)(void)
{
// int label = -1;
volatile register float tmp1 = 0.0;
volatile float features[11] = {4.6f, 0.12f, 0.0f, 0.9f, 0.01f, 1.0f, 6.0f, 0.99f, 2.74f, 0.33f, 8.4f};
volatile register float correct_val = features[6];
uint32_t blink_status = 1;
bool glitch_detector_triggered = (powman_hw->chip_reset & 0x04000000) != 0;
watchdog_update();
if (glitch_detector_triggered)
{
uart_putc_raw(TARGET_UART_INSTANCE, 'G');
}
else
{
uart_putc_raw(TARGET_UART_INSTANCE, 'R');
}
watchdog_update();
gpio_put(TRIGGER_PIN, 1);
sleep_ms(1);
watchdog_update();
gpio_put(TRIGGER_PIN, 0);
gpio_put(TRIGGER2_PIN, 1);
delayNops(50);
nop();
tmp1 = features[6];
delayNops(50);
nop();
gpio_put(TRIGGER2_PIN, 0);
// Check for glitch
if (features[6] != correct_val)
{
// Q indicates successful glitch
watchdog_update();
uart_putc_raw(uart0, 'Q');
}
else
{
// N indicates regular execution
uart_putc_raw(uart0, 'N');
}
watchdog_update();
watchdog_update();
char buffer[12];
snprintf(buffer, sizeof(buffer), "%.1f", tmp1); // Format tmp2 as a string
uart_puts(uart0, buffer);
}
#pragma GCC pop_options
inline void delayNops(long nops)
{
__asm__ volatile(
"L_%=_delayNops:" "\n\t"
"subs %0, #1" "\n\t" // Subtract 1 from nops
"bne L_%=_delayNops" "\n" // Loop if nops is not zero
: "+r" (nops) :
);
}
int main()
{
init_uart();
// while (true) {
// uart_puts(TARGET_UART_INSTANCE, "Hello from RP2350!\r\n");
// sleep_ms(1000);
// }
watchdog_enable(100, 1);
gpio_init(TRIGGER_PIN);
gpio_set_dir(TRIGGER_PIN, GPIO_OUT);
gpio_put(TRIGGER_PIN, 0);
gpio_init(TRIGGER2_PIN);
gpio_set_dir(TRIGGER2_PIN, GPIO_OUT);
gpio_put(TRIGGER2_PIN, 0);
for(int i=0; i < 5; i++) {
gpio_init(joy_io[i]);
gpio_set_dir(joy_io[i], GPIO_IN);
gpio_pull_up(joy_io[i]);
}
glitch_detector_disarm();
glitch_loop();
//glitch_before_loading();
for (;;) { watchdog_update(); }
}