-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
563 lines (452 loc) · 12.2 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
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"
#ifndef __WIN32__
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define strnicmp strncasecmp
#endif
// 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.3 - 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 2: binary 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 -c [ipaddr] file\t\t\t- Upload cartridge image (.CPR/.BIN)\r\n", exename);
printf("%s -x [ipaddr] path+file\t\t- Execute file on CPC\r\n", exename);
printf("%s -y [ipaddr] local_file\t\t- Upload file on CPC and execute it immediatly (the sd card must contain folder '/tmp')\r\n", exename);
printf("%s -p [ipaddr]\t\t\t\t- Start (plus) cartridge\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 startCard(char *ip)
{ char httpReq[128];
int sd;
volatile int n;
sd = httpConnect(ip);
if ( sd > 0 )
{
n = sprintf(httpReq, "GET /config.cgi?cctr 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);
}
}
#ifndef __WIN32__
int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
#endif
void upload(char *filename, char *path, char *ip, int opt, unsigned short start, unsigned short exec)
{
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;
}
#ifndef __WIN32__
if (!is_regular_file(filename))
{ printf("file %s is not a regular file\n", filename);
fclose(fd);
return;
}
#endif
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)
{
case 1:
cpcheader->addr = 0x172; // protext uses this
cpcheader->type = 10;
cpcheader->size = size;
cpcheader->size2 = size;
cpcheader->checksum = checksum16(buf, 66);
break;
default:
case 2: // binary header
cpcheader->addr = start;
cpcheader->exec = exec;
cpcheader->type = 2;
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 uploadctr(char *filename, char *ip)
{
int ret, size, p, n, k, i, j;
FILE *fd;
SOCKET sd;
unsigned char *buf;
fd = fopen(filename, "rb");
if ( fd == NULL )
{ printf("file %s not found\n", filename);
return;
}
#ifndef __WIN32__
if (!is_regular_file(filename))
{ printf("file %s is not a regular file\n", filename);
fclose(fd);
return;
}
#endif
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); // gah no validation, M4 will verify some...
fclose(fd);
ret = httpConnect(ip);
if ( ret >= 0 )
{ sd = ret; // connect socket
if ( httpSend(sd, "/CARTIMG.BIN", 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");
}
#define GET_CPCIP(argpos, argmax) \
{ \
printf("%d, %d, %d\n", argc, argmax, argpos); \
cpcip = NULL; \
cpcip = getenv("CPCIP"); \
printf("IP1=%s\n", cpcip); \
/*if (cpcip != NULL) \
{ \
while (*cpcip != '=') {++cpcip;}; \
++cpcip; \
} else{printf("not found");}*/\
printf("IP2=%s\n", cpcip); \
if (argc == (argmax)+1 || cpcip==NULL) { \
printf("IP2=%s\n", cpcip); \
cpcip = argv[(argpos)]; \
printf("IP2=%s\n", cpcip); \
} \
else { \
argdelta = -1; \
} \
if (NULL == cpcip) { \
fprintf(stderr, "[ERROR] Unable to retreive the IP of the M4.\n Please provide it as an argument or through CPCIP variable.\n"); \
exit(-1); \
} \
printf("IP3=%s\n", cpcip); \
}
int main(int argc, char *argv[])
{ int opt = 0;
// Retreive the IP of the M4. Can be overriden by the option managment
const char * cpcip=NULL;
// Contains -1 if the ip is not provided in the command line
int argdelta = 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], "-p", 2) ) // reset cpc
{
GET_CPCIP(2, 2);
startCard(cpcip);
}
else
if ( !strnicmp(argv[1], "-r", 2) ) // reboot M4
{
GET_CPCIP(2, 2);
reset(cpcip);
}
else if ( !strnicmp(argv[1], "-s", 2) ) // reset cpc
{
GET_CPCIP(2, 2);
resetCPC(cpcip);
}
else if ( !strnicmp(argv[1], "-x", 2) && (argc>=3) ) // execute
{
GET_CPCIP(2, 2);
run(cpcip, argv[3 + argdelta]);
}
else if ( !strnicmp(argv[1], "-f", 2) && (argc>=4) ) // upload (flash) rom
{
GET_CPCIP(2, 4);
uploadRom(argv[3+argdelta], cpcip, atoi(argv[4+argdelta]), argv[5+argdelta] );
}
else if ( !strnicmp(argv[1], "-u", 2) && (argc>=3) ) // upload
{ unsigned short start = 0;
unsigned short exec = 0;
GET_CPCIP(2, 5);
if ( argc < 5+argdelta && cpcip == NULL)
{
dispInfo(argv[0]);
exit(0);
}
if ( argc>5+argdelta )
{ opt = atoi(argv[5+argdelta]);
if ( opt == 2 ) // add binary header ?
{
// we need more parameters then
if ( argc < 8 )
{
printf("Please add start address and exec. address for binary header\r\n");
exit(0);
}
start = atoh(argv[6+argdelta]);
exec = atoh(argv[7+argdelta]);
}
}
upload(argv[3+argdelta], argv[4+argdelta], cpcip, opt, start, exec);
}
else if ( !strnicmp(argv[1], "-c", 2) && (argc>=4) ) // upload cartridge
{
GET_CPCIP(2, 3);
uploadctr(argv[3+argdelta], cpcip);
}
else if ( !strnicmp(argv[1], "-y", 2) && (argc>=3) )
{
GET_CPCIP(2, 3);
// Prepare the file manipulation variables
char fullpath[256];
char path[] = "/tmp"; // TODO Find a way to use /tmp or something like that
char * filename = argv[3+argdelta];
int p = pathPos(filename, strlen(filename)); // strip any PC path from filename
sprintf(fullpath, "%s/%s", path, &filename[p]);
upload(filename, path, cpcip, 0, 0, 0 ); // Upload the file on CPC
run(cpcip, fullpath); // Execute the file on CPC
}
else if ( !strnicmp(argv[1], "-d", 2) && (argc>=4) ) // download
{
GET_CPCIP(2, 5);
// TODO properlly manage the case when not provided
if ( argc>5 )
opt = atoi(argv[5+argdelta]);
download(argv[3+argdelta], argv[4+argdelta], argv[2], opt);
}
else
{
dispInfo(argv[0]);
exit(0);
}
#ifdef __WIN32__
WSACleanup();
#endif
return 0;
}