-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstcdude.c
340 lines (301 loc) · 8.44 KB
/
stcdude.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright 2014 Andrew 'Necromant' Andrianov <andrew@ncrmnt.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "stcdude.h"
#include "mcudb.h"
#include "payload.h"
#include "uart.h"
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/stat.h>
#include <lualib.h>
#include <lauxlib.h>
static struct uart_settings_t* us;
static int delay_hack = 0;
int g_no_crc = 0;
void usage(char* nm){
printf("WARNING: This tool is in no way affiliated with STC MCU Limited. \n");
printf("WARNING: Those guys even keep the protocol closed \n");
printf("WARNING: So consider some STM32 for your new project \n");
printf("Usage: %s [options]\n", nm);
printf("Valid options are:\n");
printf("\t -p /dev/ttyUSB0 \tspecify serial port to use\n");
printf("\t -b handshake:upload \tspecify baud rate to use (initial and upload)\n");
printf("\t -h \tprint this help and exit\n");
printf("\t -a action \tspecify an action\n");
printf("\t -f filename.bin \tSend this file to mcu\n");
printf("\t -w \t Workaround broken tcdrain\n");
printf("\t -n \t Disable CRC checking of inbound packets (STC89)\n");
printf("Valid actions (for -a) are:\n");
printf("\t info \t query mcu options\n");
printf("\t wflash \t write file to flash memory\n");
printf("This is free software, feel free to redistribute it under the terms\n");
printf("GPLv3 or above. See COPYING for details\n");
printf("Extra developer options:\n");
printf("\t -l packet monitoring mode\n");
}
#define die(message) { printf(message); exit(EXIT_FAILURE); }
int l_send_packet(lua_State* L) {
int argc = lua_gettop(L);
if (argc!=1)
die("Incorrect number of args to send_packet\n");
const char* payload = lua_tostring(L,1);
char scbuf[3];
scbuf[2]=0x0;
int len = strlen(payload)/2;
unsigned char* tmp = malloc(len);
int i;
for (i=0;i<len; i++)
{
strncpy(scbuf,&payload[i*2],2);
sscanf(scbuf, "%hhx",&tmp[i]);
}
unsigned char* packet = pack_payload(tmp, len, HOST2MCU);
write(us->fd, packet, PACKED_SIZE(len));
tcdrain(us->fd);
if (delay_hack) {
int delay = PACKED_SIZE(len)*1000000/us->speed*12; /* account for 11 bits */
usleep(delay);
}
free(tmp);
free(packet);
return 0;
}
int l_get_packet(lua_State* L) {
struct packet* packet;
packet = fetch_packet(us->fd);
//printf("Got %hd bytes\n ", packet->size);
int i;
lua_newtable(L);
for (i=0; i<packet->size; i++) {
lua_pushnumber(L, i);
lua_pushnumber(L, (int) packet->payload[i]);
lua_settable(L, -3);
}
return 1; /* return the table with bytes */
}
int l_set_baud(lua_State *L) {
int argc = lua_gettop(L);
if (argc!=1)
die("Incorrect number of args to set_baud\n");
int newbaud = lua_tonumber(L,1);
if (us->speed == newbaud)
return 0;
stc_uart_reconf(us, newbaud);
uart_init(us);
return 0;
}
static void display_progressbar(int max, int value){
int percent = 100 - value * 100 / max;
int bars = 60 - value * 60 / max;
int i;
printf("\r %d %% done | ", percent);
for (i=0; i<bars; i++)
printf("#");
for (i=bars; i<60; i++)
printf(" ");
printf("| %d K", (max-value) / 1024);
fflush(stdout);
}
int l_send_file(lua_State *L) {
int argc = lua_gettop(L);
if (argc!=2)
die("Incorrect number of args to set_baud\n");
const char* filename = lua_tostring(L,1);
int chunksize = lua_tonumber(L,2);
FILE* fd = fopen(filename, "r");
struct stat inf;
if (0 != stat(filename,&inf)) {
perror("stat failed: ");
die("");
}
if (fd<0){
perror("couldn't open file:");
die("");
}
unsigned int sz = (unsigned int)inf.st_size;
if (minf->iromsz < sz) {
printf("WARNING: File too big to fit in flash, truncating!\n");
sz = minf->iromsz+1;
}
printf("Downloading %s (%d bytes)\n", filename, sz);
unsigned int maxsz = sz;
//unsigned short sz = PACKED_SIZE(chunksize+7);
/* We add a few bytes.
type of packet? 0x00
0x0 0x0
2 bytes, offset to write at
2 bytes, size to write
*/
unsigned char* tmp = calloc(1, chunksize+7);
int len;
unsigned short offset=0;
/* Since chunksize is fixed */
tmp[5]=HIGH_BYTE((unsigned short) chunksize);
tmp[6]=LOW_BYTE((unsigned short) chunksize);
struct packet* response;
struct write_response *rsp;
unsigned short crc;
while (sz) {
len = fread(&tmp[7], 1, chunksize, fd);
crc = byte_sum(&tmp[7], chunksize);
crc = crc & 0x00ff; /* We get a shorted, one byte crc */
tmp[3]=HIGH_BYTE(offset);
tmp[4]=LOW_BYTE(offset);
unsigned char* packet = pack_payload(tmp,chunksize+7, HOST2MCU);
write(us->fd, packet, PACKED_SIZE(chunksize+7));
do_dump_packet(packet,PACKED_SIZE(chunksize+7));
free(packet);
response = fetch_packet(us->fd);
rsp = (struct write_response *) response->payload;
if (rsp->errcode !=0 )
{
fprintf(stderr, "Warning, mcu reports error @0x%hx: %hhx\n", offset, rsp->errcode);
//printf();
}else
{
//tmp[0]
}
if (rsp->crc != (unsigned char) crc )
fprintf(stderr, "Warning, crc error @0x%hx: %hhx vs %hhx\n",
offset,
rsp->crc,
(unsigned char) crc);
do_dump_packet(response->payload, response->size);
sz-= len;
offset+=len;
display_progressbar(maxsz,sz);
}
printf("\n");
return 0;
}
static char pulsechar[] = { 0x7f, 0x7f };
int l_mcu_connect(lua_State* L) {
struct packet *packet;
start_pulsing(us->fd, 50000, pulsechar, 2);
do {
packet = fetch_packet(us->fd);
} while (!packet);
stop_pulsing();
int hspeed = lua_tonumber(L,1);
parse_info_packet(L, packet, hspeed);
return 0;
}
void register_luastuff(lua_State* L) {
lua_register(L, "send_packet", l_send_packet);
lua_register(L, "get_packet", l_get_packet);
lua_register(L, "set_baud", l_set_baud);
lua_register(L, "send_file", l_send_file);
lua_register(L, "mcu_connect", l_mcu_connect);
}
int main(int argc, char* argv[]) {
printf("STC ISP Tool. (c) Necromant 2012\n");
int opt;
int action = ACTION_SEQ;
char* port = "/dev/ttyUSB0";
char* scenario = "default.lua";
char* filename = "null";
int uspeed = 57600;
int hspeed = 1200;
char* seq = "info";
lua_State* L = luaL_newstate();
luaL_openlibs(L);
register_luastuff(L);
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
while ((opt = getopt(argc, argv, "nb:p:lb:a:f:w")) != -1) {
switch (opt) {
case 'b':
sscanf(optarg, "%d:%d",&hspeed, &uspeed);
break;
case 'p':
port = optarg;
break;
case 'l':
printf("Starting to monitor all incoming packets\n");
action = ACTION_MON;
break;
case 'a':
action = ACTION_SEQ;
seq = optarg;
break;
case 'f':
filename = optarg;
break;
case 'w':
delay_hack = 1;
break;
case 'n':
g_no_crc=1;
break;
default: /* '?' */
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
/* Push a few options to lua */
lua_pushstring( L, filename );
lua_setglobal( L, "filename" );
lua_pushnumber( L, hspeed );
lua_setglobal( L, "handshake_speed" );
lua_pushnumber( L, uspeed );
lua_setglobal( L, "upload_speed" );
lua_pushstring( L, SEQDIR);
lua_setglobal( L, "SEQDIR" );
lua_pushstring( L, MCUDB_DIR);
lua_setglobal( L, "MCUDBDIR" );
/* All checks passsed, let's rick'n'roll */
mcudb_open(L, SCRIPTS_PATH "/init.lua" );
if (action == ACTION_NONE)
{
printf("No valid action set, have a quick look at the help\n");
usage(argv[0]);
exit(EXIT_FAILURE);
}
us = stc_uart_settings(port, hspeed);
if (uart_init(us)<0) {
exit(EXIT_FAILURE);
}
struct packet* packet;
switch (action)
{
case ACTION_MON:
while (1)
{
packet = fetch_packet(us->fd);
free(packet->data);
free(packet);
}
break;
case ACTION_SEQ: /* Scripted action */
lua_getglobal(L, "run_sequence"); /* function to be called */
lua_pushstring(L, seq); /* prepare parameter */
if (lua_pcall(L, 1, 0, 0) != 0)
{
fprintf(stderr, "error running sequence '%s': %s\n",
seq, lua_tostring(L, -1));
return 1;
}
break;
}
}