forked from USTC-Hackergame/hackergame2022-writeups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware.c
266 lines (217 loc) · 5.6 KB
/
firmware.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdint.h>
#include <stdbool.h>
#ifndef NULL
#define NULL ((void*)0)
#endif
__sfr __at(0xf2) SERIAL_OUT_DATA;
__sfr __at(0xf3) SERIAL_OUT_READY;
__sfr __at(0xfa) SERIAL_IN_DATA;
__sfr __at(0xfb) SERIAL_IN_READY;
__sfr __at(0xe1) I2C_STATUS;
__sfr __at(0xe2) I2C_BUFFER_XRAM_LOW;
__sfr __at(0xe3) I2C_BUFFER_XRAM_HIGH;
__sfr __at(0xe4) I2C_BUFFER_SIZE;
__sfr __at(0xe6) I2C_ADDRESS; // 7-bit address
__sfr __at(0xe7) I2C_READ_WRITE;
__sfr __at(0xff) POWEROFF;
__sfr __at(0xfe) POWERSAVE;
int8_t init_rand=7;
int8_t speedtb[8]={0x11,0x45,0x14,0x19,0x81,0x24,0x00,0x19};
int8_t rand(){
init_rand+=1;
init_rand&=7;
return speedtb[init_rand];
}
void serial_print(const char *s) {
while (*s) {
while (!SERIAL_OUT_READY);
SERIAL_OUT_DATA = *s++;
}
}
int8_t i2c_write(int8_t port, uint8_t req_len, __xdata uint8_t *buf) {
while (I2C_STATUS == 1) {
POWERSAVE = 1; // Enter power save mode for a few milliseconds.
}
I2C_BUFFER_XRAM_LOW = (uint8_t)(uint16_t)buf;
I2C_BUFFER_XRAM_HIGH = (uint8_t)((uint16_t)buf >> 8);
I2C_BUFFER_SIZE = req_len;
I2C_ADDRESS = port;
I2C_READ_WRITE = 0; // Start write.
int8_t status;
while ((status = I2C_STATUS) == 1) {
POWERSAVE = 1; // Enter power save mode for a few milliseconds.
}
return status;
}
int8_t i2c_read(int8_t port, uint8_t req_len, __xdata uint8_t *buf) {
while (I2C_STATUS == 1) {
POWERSAVE = 1; // Enter power save mode for a few milliseconds.
}
I2C_BUFFER_XRAM_LOW = (uint8_t)(uint16_t)buf;
I2C_BUFFER_XRAM_HIGH = (uint8_t)((uint16_t)buf >> 8);
I2C_BUFFER_SIZE = req_len;
I2C_ADDRESS = port;
I2C_READ_WRITE = 1; // Start read.
int8_t status;
while ((status = I2C_STATUS) == 1) {
POWERSAVE = 1; // Enter power save mode for a few milliseconds.
}
return status;
}
const char *i2c_status_to_error(int8_t err) {
switch (err) {
case 0: return "i2c status: transaction completed / ready\n";
case 1: return "i2c status: busy\n";
case 2: return "i2c status: error - device not found\n";
case 3: return "i2c status: error - device misbehaved\n";
}
return "i2c status: unknown error\n";
}
char serial_read_char(void) {
while (1) {
if (SERIAL_IN_READY) {
return (char)SERIAL_IN_DATA;
}
POWERSAVE = 1;
}
}
struct tokenizer_st {
char *ptr;
int replaced;
};
void tokenizer_init(struct tokenizer_st *t, char *str) {
t->ptr = str;
t->replaced = 0x7fff;
}
char *tokenizer_next(struct tokenizer_st *t) {
if (t->replaced != 0x7fff) {
*t->ptr = (char)t->replaced;
}
while (*t->ptr == ' ') {
t->ptr++;
}
if (*t->ptr == '\0') {
return NULL;
}
char *token_start = t->ptr;
for (;;) {
char ch = *t->ptr;
if (ch != ' ' && ch != '\0') {
t->ptr++;
continue;
}
t->replaced = *t->ptr;
*t->ptr = '\0';
return token_start;
}
}
uint8_t table[20]="0123456789ABCDEF";
void uint8_to_str(char *buf, uint8_t v) {
*buf++=table[(v&0xf0)>>4];
*buf++=table[v&0x0f];
*buf=0;
}
uint8_t str_to_uint8(const char *s) {
uint8_t v = 0;
while (*s) {
uint8_t digit = *s++ - '0';
v = v * 10 + digit;
}
return v;
}
int8_t port_to_int8(char *port) {
if (port[1]!=0&&port[1]!=' ') {
//no more than 1 digit.
return -1;
}
return (int8_t)str_to_uint8(port);
}
#define CMD_BUF_SZ 384
#define I2C_BUF_SZ 128
int main(void) {
serial_print("INIT OK\n");
static __xdata char cmd[CMD_BUF_SZ];
static __xdata uint8_t i2c_buf[I2C_BUF_SZ],i2c_buf2[I2C_BUF_SZ];
while (true) {
serial_print("> ");
int i;
for (i = 0; i < CMD_BUF_SZ; i++) {
char ch = serial_read_char();
if (ch == '\n') {
cmd[i] = '\0';
break;
}
cmd[i] = ch;
}
if (i == CMD_BUF_SZ) {
serial_print("-err: command too long, rejected\n");
continue;
}
struct tokenizer_st t;
tokenizer_init(&t, cmd);
char *p = tokenizer_next(&t);
if (p == NULL) {
serial_print("-err: command format incorrect\n");
continue;
}
bool write;
if (*p == 'r') {
write = false;
} else if (*p == 'w') {
write = true;
} else {
serial_print("-err: unknown command\n");
continue;
}
p = tokenizer_next(&t);
if (p == NULL) {
serial_print("-err: command format incorrect\n");
continue;
}
int8_t port = port_to_int8(p);
if (port == -1) {
serial_print("-err: port invalid or not allowed\n");
continue;
}
p = tokenizer_next(&t);
if (p == NULL) {
serial_print("-err: command format incorrect\n");
continue;
}
uint8_t req_len = str_to_uint8(p);
if (req_len == 0 || req_len > I2C_BUF_SZ) {
serial_print("-err: I2C request length incorrect\n");
continue;
}
if (write) {
for (uint8_t i = 0; i < req_len; i++) {
p = tokenizer_next(&t);
if (p == NULL) {
break;
}
i2c_buf[i] = str_to_uint8(p);
i2c_buf2[i] = rand();
}
int8_t ret = i2c_write(port, req_len, i2c_buf);
i2c_write((port+1)%10,req_len,i2c_buf2);
i2c_write((port+9)%10,req_len,i2c_buf2);
serial_print(i2c_status_to_error(ret));
} else {
int8_t ret = i2c_read(port, req_len, i2c_buf);
serial_print(i2c_status_to_error(ret));
if(ret!=2)
for (uint8_t i = 0; i < req_len; i++) {
char num[4];
uint8_to_str(num, i2c_buf[i]);
serial_print(num);
if ((i + 1) % 16 == 0 && i +1 != req_len) {
serial_print("\n");
} else {
serial_print(" ");
}
}
serial_print("\n-end\n");
}
}
// Should never reach this place.
}