forked from M4Duke/cpcxfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
351 lines (282 loc) · 7.17 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
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
340
341
342
343
344
345
346
347
348
349
350
351
/*
CPC xfer.... transfer files to/from M4 board sd card, via command line, useful for ie. crossdev
Duke 2016/2017
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "http.h"
#include "parse.h"
#include "cpc.h"
// mres = m4 reboot
// cres = cpc reset
// chlt = cpc pause/unpause (BUSRQ)
// run = run file on sd at startup
void dispInfo(char *exename)
{
printf("CPC M4 xfer tool v2.0.0 - Duke 2016/2017\r\n");
printf("%s -u ipaddr file path opt\t\t- Upload file, opt 0: no header add, 1: add ascii header\r\n", exename);
printf("%s -d ipaddr file path opt\t\t- Download file, opt 0: leave header, 1: remove header\r\n", exename);
printf("%s -f ipaddr file slot name\t\t- Upload rom\r\n", exename);
printf("%s -x ipaddr path+file\t\t- Execute file on CPC\r\n", exename);
printf("%s -s ipaddr\t\t\t\t- Reset CPC\r\n", exename);
printf("%s -r ipaddr\t\t\t\t- Reboot M4\r\n", exename);
}
void reset(char *ip)
{ char httpReq[128];
int sd;
volatile int n;
sd = httpConnect(ip);
if ( sd > 0 )
{
n = sprintf(httpReq, "GET /config.cgi?mres HTTP/1.0\r\nHost: %s\r\nUser-Agent: cpcxfer\r\n\r\n", ip);
send(sd, httpReq, n, 0);
while (n > 0) // ignore response... a 200 OK check would be a good idea....
n = recv(sd, httpReq, sizeof(httpReq),0 );
#ifdef __WIN32__
closesocket(sd);
#else
close(sd);
#endif
printf("M4 Reset request sent.\r\n");
}
}
void resetCPC(char *ip)
{ char httpReq[128];
int sd;
volatile int n;
sd = httpConnect(ip);
if ( sd > 0 )
{
n = sprintf(httpReq, "GET /config.cgi?cres HTTP/1.0\r\nHost: %s\r\nUser-Agent: cpcxfer\r\n\r\n", ip);
send(sd, httpReq, n, 0);
while (n > 0) // ignore response... a 200 OK check would be a good idea....
n = recv(sd, httpReq, sizeof(httpReq),0 );
#ifdef __WIN32__
closesocket(sd);
#else
close(sd);
#endif
printf("M4 Reset request sent.\r\n");
}
}
void run(char *ip, char *path)
{ char httpReq[128];
int sd;
volatile int n;
sd = httpConnect(ip);
if ( sd > 0 )
{
n = sprintf(httpReq, "GET /config.cgi?run=%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: cpcxfer\r\n\r\n", path, ip);
send(sd, httpReq, n, 0);
while (n > 0) // ignore response... a 200 OK check would be a good idea....
n = recv(sd, httpReq, sizeof(httpReq),0 );
#ifdef __WIN32__
closesocket(sd);
#else
close(sd);
#endif
printf("Running %s\r\n", path);
}
}
void upload(char *filename, char *path, char *ip, int opt)
{
int ret, size, p, n, k, i, j;
char fullpath[256]; // don't exceed this, the cpc wouldn't be able |cd it anyway
FILE *fd;
SOCKET sd;
unsigned char *buf;
fd = fopen(filename, "rb");
if ( fd == NULL )
{ printf("file %s not found\n", filename);
return;
}
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(size+0x80);
if ( buf == NULL )
{
printf("Not enough memory!\n");
fclose(fd);
return;
}
if ( opt != 0 )
{ _cpchead *cpcheader = (_cpchead *)buf;
memset(cpcheader, 0, 0x80);
// set up cleaned filename and extionsion
formatfn(cpcheader->filename, filename);
getExtension(cpcheader->extension, filename);
// add more file types here... and more paramters....
switch (opt)
{
default: // just ascii for now
cpcheader->addr = 0x172; // protext uses this
cpcheader->type = 10;
cpcheader->size = size;
cpcheader->size2 = size;
cpcheader->checksum = checksum16(buf, 66);
break;
}
fread(&buf[0x80], size, 1, fd);
size+=0x80;
}
else
fread(buf, size, 1, fd);
fclose(fd);
ret = httpConnect(ip);
if ( ret >= 0 )
{ sd = ret; // connect socket
p = pathPos(filename, strlen(filename)); // strip any PC path from filename
sprintf(fullpath, "%s/%s", path, &filename[p]);
k = strlen(fullpath);
n = 0;
while ( n < k ) // remove leading / from path
{
if ( fullpath[n] != '/' )
break;
n++;
}
// remove duplicate /'s
k = strlen(fullpath);
j = 0;
for (i=n; i < (k+1); i++)
{
if ( (fullpath[i] == '/') && (fullpath[i+1] == '/') )
i++;
fullpath[j++] = fullpath[i];
}
if ( httpSend(sd, fullpath, buf, size, "upfile", "/upload.html", ip) >= 0 )
ret = httpResponse(sd);
httpClose(sd);
if ( ret == 200 )
printf("Upload OK!\r\n");
else
printf("Upload error code. %i\r\n", ret);
}
else
printf("Connect to %s failed\n", ip);
free(buf);
}
void uploadRom(char *filename, char *ip, int slot, char *slotname)
{
int ret, size, p, n, k, i, j;
char fullpath[256]; // don't exceed this, the cpc wouldn't be able |cd it anyway
FILE *fd;
SOCKET sd;
unsigned char *buf;
fd = fopen(filename, "rb");
if ( fd == NULL )
{ printf("file %s not found\n", filename);
return;
}
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(size);
if ( buf == NULL )
{
printf("Not enough memory!\n");
fclose(fd);
return;
}
fread(buf, size, 1, fd);
fclose(fd);
ret = httpConnect(ip);
if ( ret >= 0 )
{ sd = ret; // connect socket
if ( httpSendRom(sd, "rom.bin", buf, size, slot, "/roms.shtml", ip, slotname) >= 0 )
ret = httpResponse(sd);
httpClose(sd);
if ( ret == 200 )
printf("Upload OK!\r\n");
else
printf("Upload error code. %i\r\n", ret);
}
else
printf("Connect to %s failed\n", ip);
free(buf);
}
void download(char *filename, char *path, char *ip, int opt)
{
SOCKET sd;
int ret, k, i, j;
char fullpath[256];
sprintf(fullpath, "sd/%s/%s", path, filename);
// remove duplicate /'s
k = strlen(fullpath);
j = 0;
for (i=0; i < (k+1); i++)
{
if ( (fullpath[i] == '/') && (fullpath[i+1] == '/') )
i++;
fullpath[j++] = fullpath[i];
}
ret = httpConnect(ip);
sd = ret;
if ( ret >= 0 )
{
if ( httpGet(sd, ip, fullpath, opt) == 0)
printf("Downloaded succesfully.\r\n");
else
printf("Download failed.\r\n");
}
else
printf("Connect failed, wrong ip?\r\n");
}
int main(int argc, char *argv[])
{ int opt = 0;
#ifdef __WIN32__
WSADATA WsaDat;
#endif
if ( argc < 2 )
{
dispInfo(argv[0]);
exit(0);
}
#ifdef __WIN32__
WSAStartup(MAKEWORD(2,2),&WsaDat);
#endif
if ( !strnicmp(argv[1], "-r", 2) ) // reboot M4
{
reset(argv[2]);
}
else if ( !strnicmp(argv[1], "-s", 2) ) // reset cpc
{
resetCPC(argv[2]);
}
else if ( !strnicmp(argv[1], "-x", 2) && (argc>=3) ) // execute
{
run(argv[2], argv[3]);
}
else if ( !strnicmp(argv[1], "-f", 2) && (argc>=4) ) // upload (flash) rom
{
uploadRom(argv[3], argv[2], atoi(argv[4]), argv[5] );
}
else if ( !strnicmp(argv[1], "-u", 2) && (argc>=4) ) // upload
{
if ( argc < 5 )
{
dispInfo(argv[0]);
exit(0);
}
if ( argc>5 )
opt = atoi(argv[5]);
upload(argv[3], argv[4], argv[2], opt);
}
else if ( !strnicmp(argv[1], "-d", 2) && (argc>=4) ) // download
{
if ( argc>5 )
opt = atoi(argv[5]);
download(argv[3], argv[4], argv[2], opt);
}
else
{
dispInfo(argv[0]);
exit(0);
}
#ifdef __WIN32__
WSACleanup();
#endif
return 0;
}