/
loadp2.c
1572 lines (1466 loc) · 44.1 KB
/
loadp2.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 2017-2019 by Dave Hein
* Copyright (c) 2019-2023 Total Spectrum Software Inc.
* Based on p2load written by David Betz
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include "osint.h"
#include "loadelf.h"
#define ARGV_ADDR 0xFC000
#define ARGV_MAGIC ('A' | ('R' << 8) | ('G'<<16) | ('v'<<24))
#define ARGV_MAX_BYTES 1020
#define ARGC_MAX_ITEMS 32
/* default FIFO size of FT231X in P2-EVAL board and PropPlugs */
//#define DEFAULT_FIFO_SIZE 512
#define DEFAULT_FIFO_SIZE 1024 /* seems to work better */
#define NO_ENTER 0
#define ENTER_TAQOZ 1
#define ENTER_DEBUG 2
#define LOAD_CHIP 0
#define LOAD_FPGA 1
#define LOAD_SINGLE 2
#define LOAD_SPI 3
#define ROUND_UP(x) (((x)+3) & ~3)
static int loader_baud = 2000000;
static int clock_mode = -1;
static int user_baud = 115200;
static int clock_freq = 80000000;
static int extra_cycles = 7;
static int load_mode = -1;
static int patch_mode = 0;
static int use_checksum = 1;
static int quiet_mode = 0;
static int enter_rom = NO_ENTER;
static char *send_script = NULL;
static int mem_argv_bytes = 0;
static char *mem_argv_data = NULL;
int get_loader_baud(int ubaud, int lbaud);
static void RunScript(char *script);
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
#define PORT_PREFIX "com"
#define INTEGER_PREFIXES
#include <windows.h>
#elif defined(MACOSX)
#define PORT_PREFIX "cu.usbserial"
#include <dirent.h>
#else
#define PORT_PREFIX "ttyUSB"
#include <dirent.h>
#endif
#include "MainLoader_fpga.h"
#include "MainLoader_chip.h"
#include "flash_loader.h"
static int32_t ibuf[256];
static int32_t ibin[32];
static char *buffer = (char *)ibuf;
static char *binbuffer = (char *)ibin;
static int verbose = 0;
int waitAtExit = 0;
static int force_zero = 0; /* default to zeroing memory */
static int do_hwreset = 1;
static int fifo_size = DEFAULT_FIFO_SIZE;
int ignoreEof = 0;
/* duplicate a string, useful if our original string might
* not be modifiable
*/
static char *duplicate_string(const char *str)
{
size_t len = strlen(str)+1;
char *r = malloc(len);
if (r) {
strcpy(r, str);
}
return r;
}
/* promptexit: print a prompt if waitAtExit is set, then exit */
void
promptexit(int r)
{
int c;
if (waitAtExit) {
fflush(stderr);
printf("Press enter to continue...\n");
fflush(stdout);
do {
c = getchar();
} while (c > 0 && c != '\n' && c != '\r');
}
exit(r);
}
/* Usage - display a usage message and exit */
static void Usage(const char *msg)
{
if (msg) {
printf("%s\n", msg);
}
printf("\
loadp2 - a loader for the propeller 2 - version 0.072 " __DATE__ "\n\
usage: loadp2\n\
[ -p port ] serial port\n\
[ -b baud ] user baud rate (default is %d)\n\
[ -l baud ] loader baud rate (default is %d)\n\
[ -f clkfreq ] clock frequency (default is %d)\n\
[ -m clkmode ] clock mode in hex (default is %02x)\n\
[ -s address ] starting address in hex (default is 0)\n\
[ -t ] enter terminal mode after running the program\n\
[ -T ] enter PST-compatible terminal mode\n\
[ -v ] enable verbose mode\n\
[ -k ] wait for user input before exit\n\
[ -q ] quiet mode: also checks for exit sequence\n\
[ -n ] no reset; skip any hardware reset\n\
[ -9 dir ] serve 9p remote filesystem from dir\n\
[ -FIFO bytes] modify serial FIFO size (default is %d bytes)\n\
[ -? ] display a usage message and exit\n\
[ -DTR ] use DTR for reset (default)\n\
[ -RTS ] use RTS for reset\n\
[ -xDEBUG ] enter ROM debug monitor\n\
[ -xTAQOZ ] enter ROM version of TAQOZ\n\
[ -xTERM ] enter terminal, avoid reset\n\
[ -CHIP ] set load mode for CHIP\n\
[ -FPGA ] set load mode for FPGA\n\
[ -NOZERO ] do not clear memory before download (default)\n\
[ -ZERO ] clear memory before download\n\
[ -PATCH ] patch in clock frequency and serial parms\n\
[ -SINGLE ] set load mode for single stage\n\
[ -FLASH ] like -SINGLE, but copies application to SPI flash\n\
[ -SPI ] alias for -FLASH\n\
[ -NOEOF ] ignore EOF on input\n\
filespec file to load\n\
[ -e script ] send a sequence of characters after starting P2\n\
[ -a arg1 [arg2 ...] ] put arguments for program into memory\n\
", user_baud, loader_baud, clock_freq, clock_mode, DEFAULT_FIFO_SIZE);
printf("\n\
In -CHIP mode, filespec may optionally be multiple files with address\n\
specifiers, such as:\n\
@ADDR=file1,@ADDR=file2,@ADDR+file3\n\
Here ADDR is a hex address at which to load the next file, followed by = or +\n\
If it is followed by + then the size of the file is put in memory followed by\n\
the file data. This feature is useful for loading data that a program wishes\n\
to act on. For example, a VGA program which displays data from $1000 may be\n\
loaded with:\n\
@0=vgacode.bin,@1000=picture.bmp\n\
The main executable code must always be specified first\n\
");
promptexit(1);
}
void tx_raw_byte(unsigned int c)
{
buffer[0] = c & 0xff;
tx((uint8_t *)buffer, 1);
}
void tx_raw_long(unsigned int size)
{
buffer[0] = (size >> 0) & 0xff;
buffer[1] = (size >> 8) & 0xff;
buffer[2] = (size >> 16) & 0xff;
buffer[3] = (size >> 24) & 0xff;
tx((uint8_t *)buffer, 4);
}
void txbyte(int val)
{
sprintf(buffer, " %2.2x", val&255);
tx((uint8_t *)buffer, strlen(buffer));
}
void txval(int val)
{
sprintf(buffer, " %2.2x %2.2x %2.2x %2.2x",
val&255, (val >> 8) & 255, (val >> 16) & 255, (val >> 24) & 255);
tx((uint8_t *)buffer, strlen(buffer));
}
void txstring(unsigned char *bytes, int len)
{
while (len-- > 0) {
txbyte(*bytes++);
}
}
int compute_checksum(int *ptr, int num)
{
int checksum = 0;
while (num-- > 0)
checksum += *ptr++;
return checksum;
}
/*
* ultimately the final image to be loaded ends up in a binary blob pointed to
* by g_filedata, of size g_filesize. g_fileptr is used to stream the data
*/
uint8_t *g_filedata;
int g_filesize;
int g_fileptr;
/*
* read an ELF file into memory
* optionally prepends a block of data
* to the file (if prepend_data is non-NULL)
*/
static int
readElfFile(FILE *infile, ElfHdr *hdr, uint8_t *prepend_data, int prepend_size)
{
ElfContext *c;
ElfProgramHdr program;
int size = 0;
unsigned int base = -1;
unsigned int top = 0;
int i, r;
uint8_t *program_mem;
c = OpenElfFile(infile, hdr);
if (!c) {
printf("error: opening elf file\n");
return -1;
}
/* walk through the program table */
for (i = 0; i < c->hdr.phnum; i++) {
if (!LoadProgramTableEntry(c, i, &program)) {
printf("Error reading ELF program header %d\n", i);
return -1;
}
if (program.type != PT_LOAD) {
continue;
}
//printf("load %d bytes at %x\n", program.filesz, program.paddr);
if (program.paddr < base) {
base = program.paddr;
}
if (program.memsz < program.filesz) {
printf("bad ELF file: program size in file too big\n");
return -1;
}
if (program.paddr + program.memsz > top) {
top = program.paddr + program.memsz;
}
}
size = top - size;
if (size > 0xffffff) {
printf("image size %d bytes is too large to handle\n", size);
return -1;
}
size += prepend_size;
g_filedata = (uint8_t *)calloc(1, size);
if (!g_filedata) {
printf("Could not allocate %d bytes\n", size);
return -1;
}
g_filesize = size;
if (prepend_data && prepend_size > 0) {
program_mem = g_filedata + prepend_size;
memcpy(g_filedata, prepend_data, prepend_size);
} else {
program_mem = g_filedata;
}
for (i = 0; i < c->hdr.phnum; i++) {
if (!LoadProgramTableEntry(c, i, &program)) {
printf("Error reading ELF program header %d\n", i);
return -1;
}
if (program.type != PT_LOAD) {
continue;
}
fseek(infile, program.offset, SEEK_SET);
r = fread(program_mem + program.paddr - base, 1, program.filesz, infile);
if (r != program.filesz) {
printf("read error in ELF file\n");
return -1;
}
}
//printf("ELF: total size = %d\n", size);
return size;
}
/*
* read a simple binary file into memory
* sets g_filedata to point to the data,
* and g_filesize to the length
* returns g_filesize, or -1 on error
* if prepend_data is non-NULL and prepend_size > 0,
* then that data is prepended to the total to be downloaded
*/
static int
readBinaryFile(char *fname, uint8_t *prepend_data, int prepend_size)
{
int size;
int fsize;
FILE *infile;
ElfHdr hdr;
uint8_t *progbase;
g_fileptr = 0;
infile = fopen(fname, "rb");
if (!infile)
{
perror(fname);
return -1;
}
if (ReadAndCheckElfHdr(infile, &hdr)) {
/* this is an ELF file, load using ReadElf instead */
return readElfFile(infile, &hdr, prepend_data, prepend_size);
} else {
//printf("not an ELF file\n");
}
fseek(infile, 0, SEEK_END);
fsize = ftell(infile);
fseek(infile, 0, SEEK_SET);
size = ROUND_UP(fsize + prepend_size);
g_filedata = (uint8_t *)malloc(size);
if (!g_filedata) {
printf("Could not allocate %d bytes\n", size);
return -1;
}
if (prepend_data && prepend_size) {
memcpy(g_filedata, prepend_data, prepend_size);
}
progbase = g_filedata + prepend_size;
int origFsize = fsize;
fsize = fread(progbase, 1, fsize, infile);
fclose(infile);
if (fsize <= 0) {
size = g_filesize = fsize;
} else {
if (fsize != origFsize) {
printf("WARNING: short read of file\n");
}
size = g_filesize = ROUND_UP(fsize + prepend_size);
}
return size;
}
int
loadBytes(char *buffer, int size)
{
int r = 0;
if (!g_filedata) {
printf("No file data present\n");
return 0;
}
while (size > 0 && g_fileptr < g_filesize) {
*buffer++ = g_filedata[g_fileptr++];
--size;
++r;
}
return r;
}
static void
patchBinaryFileForFlash(int total_size, int header_len) {
int size = total_size;
uint32_t *lptr;
uint32_t chksum = 0;
if (size & 3) {
printf("Error: flashing a file that is not a multiple of 4 bytes long\n");
promptexit(1);
}
lptr = (uint32_t *)g_filedata;
lptr[2] = 0; // NOP out the DEBUG flag *before* calculating checksum
size /= 4;
for (int i = 0; i < size; i++) {
chksum += lptr[i];
}
lptr = (uint32_t *)g_filedata; // go to the header
lptr[1] = -chksum; // patch in the - chksum
}
int loadfilesingle(char *fname)
{
int num, size, i;
int patch = patch_mode;
int checksum = 0;
if (load_mode == LOAD_SPI) {
size = readBinaryFile(fname, (uint8_t *)flash_loader_bin, flash_loader_bin_len);
// need to patch up the binary
if (size > 0) {
patchBinaryFileForFlash(size, flash_loader_bin_len);
}
} else {
size = readBinaryFile(fname, NULL, 0);
}
if (size < 0) {
return 1;
}
if (verbose) printf("Loading %s - %d bytes\n", fname, size);
tx((uint8_t *)"> Prop_Hex 0 0 0 0", 18);
while ((num=loadBytes(binbuffer, 128)))
{
if (patch)
{
patch = 0;
memcpy(&binbuffer[0x14], &clock_freq, 4);
memcpy(&binbuffer[0x18], &clock_mode, 4);
memcpy(&binbuffer[0x1c], &user_baud, 4);
}
if (use_checksum)
{
num = (num + 3) & ~3;
checksum += compute_checksum(ibin, num/4);
}
for( i = 0; i < num; i++ )
sprintf( &buffer[i*3], " %2.2x", binbuffer[i] & 255 );
strcat(buffer, " > ");
tx( (uint8_t *)buffer, strlen(buffer) );
}
if (use_checksum)
{
char *ptr = (char *)&checksum;
checksum = 0x706f7250 - checksum;
for( i = 0; i < 4; i++ )
sprintf( &buffer[i*3], " %2.2x", ptr[i] & 255 );
tx( (uint8_t *)buffer, strlen(buffer) );
tx((uint8_t *)"?", 1);
wait_drain();
//msleep(100+fifo_size*10*1000/loader_baud);
//num = rx_timeout((uint8_t *)buffer, 1, 100);
num = rx_timeout((uint8_t *)buffer, 1, 100 + fifo_size*10000/loader_baud);
if (num >= 0) buffer[num] = 0;
else buffer[0] = 0;
if (strcmp(buffer, "."))
{
printf("%s failed to load\n", fname);
printf("Error response was \"%s\"\n", buffer);
promptexit(1);
}
if (verbose)
printf("Checksum (0x%08x) validated\n", checksum);
}
else
{
tx((uint8_t *)"~", 1); // Added for Prop2-v28
wait_drain();
msleep(fifo_size*10*1000/loader_baud);
}
// msleep(100);
if (verbose) printf("%s loaded\n", fname);
return 0;
}
static unsigned flag_bits()
{
// bit 0: zero out HUB memory
// bit 1: binary has been patched with correct frequency
return force_zero | (patch_mode << 1);
}
int loadfileFPGA(char *fname, int address)
{
int num, size;
int patch = patch_mode;
size = readBinaryFile(fname, NULL, 0);
if (size < 0)
{
printf("Could not open %s\n", fname);
return 1;
}
if (verbose) {
printf("Loading fast loader for %s...\n", (load_mode == LOAD_FPGA) ? "fpga" : "chip");
}
tx((uint8_t *)"> Prop_Hex 0 0 0 0", 18);
txstring((uint8_t *)MainLoader_fpga_bin, MainLoader_fpga_bin_len);
// OLD FPGA loader
txval(clock_mode);
txval((3*clock_freq+loader_baud)/(loader_baud*2)-extra_cycles);
txval((clock_freq+loader_baud/2)/loader_baud-extra_cycles);
txval(size);
txval(address);
txval(flag_bits());
tx((uint8_t *)"~", 1);
msleep(200);
if (verbose) printf("Loading %s - %d bytes\n", fname, size);
while ((num=loadBytes(buffer, 1024)))
{
if (patch)
{
patch = 0;
memcpy(&buffer[0x14], &clock_freq, 4);
memcpy(&buffer[0x18], &clock_mode, 4);
memcpy(&buffer[0x1c], &user_baud, 4);
}
tx((uint8_t *)buffer, num);
}
wait_drain();
msleep(100);
if (verbose) printf("%s loaded\n", fname);
return 0;
}
static char *getNextFile(char *fname, char **next_p, int *address_p)
{
int address = *address_p;
char *next = fname;
if (!*fname) {
*next_p = NULL;
return NULL;
}
if (*fname == '@') {
address = strtoul(fname+1, &next, 16);
if (*next == '=')
next++;
}
// scan ahead to start of next fname
while (*fname && *fname != ',') {
fname++;
}
if (*fname) {
*fname++ = 0;
}
*address_p = address;
*next_p = next;
return fname;
}
static int verify_chksum(unsigned chksum)
{
unsigned recv_chksum = 0;
int num;
wait_drain();
//msleep(1+fifo_size*10*1000/loader_baud);
//num = rx_timeout((uint8_t *)buffer, 3, 400);
num = rx_timeout((uint8_t *)buffer, 3, 400 + fifo_size*10000/loader_baud);
if (num != 3) {
printf("ERROR: timeout waiting for checksum at end: got %d\n", num);
printf("Try increasing the FIFO setting if not large enough for your setup\n");
promptexit(1);
}
recv_chksum = (buffer[0] - '@') << 4;
recv_chksum += (buffer[1] - '@');
chksum &= 0xff;
if (recv_chksum != (chksum & 0xff)) {
printf("ERROR: bad checksum, expected %02x got %02x (chksum characters %c%c%c)\n", chksum, recv_chksum, buffer[0], buffer[1], buffer[2]);
promptexit(1);
}
if (verbose) printf("chksum: %x OK\n", recv_chksum);
return 0;
}
int loadfile(char *fname, int address)
{
int num, size;
int patch = patch_mode;
unsigned chksum;
char *next_fname = NULL;
int send_size;
if (load_mode == LOAD_SINGLE || load_mode == LOAD_SPI) {
if (address != 0) {
printf("ERROR: -SINGLE and -FLASH can only load at address 0\n");
promptexit(1);
}
if (mem_argv_bytes != 0) {
printf("ERROR: ARGv is not compatible with -SINGLE and -FLASH\n");
promptexit(1);
}
return loadfilesingle(fname);
}
if (load_mode == LOAD_FPGA) {
if (mem_argv_bytes != 0) {
printf("ERROR: ARGv is not compatible with LOAD_FPGAn");
promptexit(1);
}
return loadfileFPGA(fname, address);
}
if (verbose) {
printf("Loading fast loader for %s...\n", (load_mode == LOAD_FPGA) ? "fpga" : "chip");
}
tx((uint8_t *)"> Prop_Hex 0 0 0 0", 18);
txstring((uint8_t *)MainLoader_chip_bin, MainLoader_chip_bin_len);
txval(clock_mode);
txval(flag_bits());
tx((uint8_t *)"~", 1);
{
int retry;
// receive checksum, verify it's "@@ "
wait_drain();
msleep(1+fifo_size*10*1000/loader_baud); // wait for external USB fifo to drain
flush_input();
msleep(50); // wait for code to start up
tx_raw_byte(0x80);
wait_drain();
msleep(2);
for (retry = 0; retry < 5; retry++) {
// send autobaud character
tx_raw_byte(0x80);
wait_drain();
msleep(10);
num = rx_timeout((uint8_t *)buffer, 3, 200);
if (num == 3) break;
}
if (num != 3) {
printf("ERROR: timeout waiting for initial checksum: got %d\n", num);
printf("Try increasing the FIFO setting if not large enough for your setup\n");
promptexit(1);
}
// every so often we get a 0 byte first before the checksum; if
// we do, throw it away
if (buffer[0] == 0 && buffer[2] == '@') {
buffer[0] = buffer[2];
rx_timeout((uint8_t *)&buffer[2], 1, 100);
}
if (buffer[0] != '@' || buffer[1] != '@') {
printf("ERROR: got incorrect initial chksum: %c%c%c (%02x %02x %02x)\n", buffer[0], buffer[1], buffer[2], buffer[0], buffer[1], buffer[2]);
promptexit(1);
}
}
// we want to be able to insert 0 characters in fname
// in order to break up multiple file names into different strings
// so we have to copy it to a duplicate buffer
fname = duplicate_string(fname);
do {
fname = getNextFile(fname, &next_fname, &address);
if (!next_fname) {
break; /* no more files */
}
if (*next_fname == '+') {
next_fname++;
send_size = 1;
} else {
send_size = 0;
}
size = readBinaryFile(next_fname, NULL, 0);
if (size < 0)
{
printf("Could not open %s\n", next_fname);
return 1;
}
/* OK, now send the address and file size */
if (verbose) printf("Sending header\n");
tx_raw_long(address);
if (send_size) {
int effective_size = size + 4;
// prefix the file data with 4 bytes of size
tx_raw_long(effective_size);
address += effective_size;
tx_raw_long(size);
// initialize the checksum to the data we just sent
chksum = size & 0xff;
chksum += (size>>8) & 0xff;
chksum += (size>>16) & 0xff;
chksum += (size>>24) & 0xff;
} else {
tx_raw_long(size);
address += size;
chksum = 0;
}
if (verbose) printf("Loading %s - %d bytes\n", next_fname, size);
while ((num=loadBytes(buffer, 1024)))
{
int i;
if (patch)
{
patch = 0;
memcpy(&buffer[0x14], &clock_freq, 4);
memcpy(&buffer[0x18], &clock_mode, 4);
memcpy(&buffer[0x1c], &user_baud, 4);
}
tx((uint8_t *)buffer, num);
for (i = 0; i < num; i++) {
chksum += buffer[i];
}
}
// receive checksum, verify it
verify_chksum(chksum);
if (mem_argv_bytes || *fname) {
// more files to send
tx_raw_byte('+');
} else {
tx_raw_byte('-');
}
wait_drain();
} while (*fname);
if (mem_argv_bytes) {
/* send ARGv info to $FC000 */
if (verbose) printf("sending %d arg bytes\n", mem_argv_bytes);
tx_raw_long(ARGV_ADDR);
tx_raw_long(mem_argv_bytes);
chksum = 0;
for (int i = 0; i < mem_argv_bytes; i++) {
chksum += mem_argv_data[i];
}
tx((uint8_t *)mem_argv_data, mem_argv_bytes);
verify_chksum(chksum);
mem_argv_bytes = 0;
tx_raw_byte('-'); // all done
wait_drain();
}
msleep(100);
return 0;
}
// check for a p2 on a specific port
static int
checkp2_and_init(char *Port, int baudrate, int retries)
{
char buffer[101];
int num;
int i;
if (!serial_init(Port, baudrate)) {
return 0;
}
if (!do_hwreset) {
return 1;
}
// reset and look for a P2
hwreset();
msleep(20); // wait for P2 to become active
if (verbose) printf("trying %s...\n", Port);
for (i = 0; i < retries; i++) {
flush_input();
tx((uint8_t *)"> Prop_Chk 0 0 0 0 ", 20);
wait_drain();
msleep(50+20*10*1000/loader_baud); // wait at least for 20 chars to empty through any fifo at the loader baud rate
num = rx_timeout((uint8_t *)buffer, 20, 10+20*10*1000/loader_baud); // read 20 characters
if (num >= 0) buffer[num] = 0;
else {
buffer[0] = 0;
if (0 && verbose) printf(" timeout\n");
}
if (!strncmp(buffer, "\r\nProp_Ver ", 11))
{
if (verbose) printf("P2 version %c found on serial port %s\n", buffer[11], Port);
if (load_mode == -1)
{
if (buffer[11] == 'B')
{
load_mode = LOAD_FPGA;
if (verbose) printf("Setting load mode to FPGA\n");
}
else if (buffer[11] == 'A' || buffer[11] == 'G')
{
load_mode = LOAD_CHIP;
if (verbose) printf("Setting load mode to CHIP\n");
}
else
{
printf("Warning: Unknown version %c, assuming CHIP\n", buffer[11]);
load_mode = LOAD_CHIP;
}
}
return 1;
}
}
// if we get here we failed to find a chip
serial_done();
return 0;
}
// look for a p2
int findp2(char *portprefix, int baudrate)
{
char Port[1024];
#ifdef INTEGER_PREFIXES
int i;
char targetPath[1024];
if (verbose) printf("Searching serial ports for a P2\n");
for (i = 1; i < 255; i++)
{
sprintf(Port, "%s%d", portprefix, i);
if (0==QueryDosDevice(Port, targetPath, sizeof(targetPath)))
continue;
if (checkp2_and_init(Port, baudrate, 50))
{
return 1;
}
}
#else
DIR *dir;
struct dirent *entry;
size_t prefixlen = strlen(portprefix);
dir = opendir("/dev");
if (!dir) {
printf("Unable to access /dev\n");
promptexit(1);
}
for(;;) {
entry = readdir(dir);
if (!entry) break;
if (0 != strncmp(entry->d_name, portprefix, prefixlen)) {
continue;
}
strcpy(Port, "/dev/");
strcat(Port, entry->d_name);
if (checkp2_and_init(Port, baudrate, 60)) {
closedir(dir);
return 1;
}
}
closedir(dir);
#endif
return 0;
}
int atox(char *ptr)
{
int value;
sscanf(ptr, "%x", &value);
return value;
}
int get_clock_mode(int sysfreq)
{
int xtalfreq = 20000000;
int xdiv = 4;
int xdivp = 2;
int xosc = 2;
int xmul, xpppp, setfreq;
//int xsel = 3;
//int enafreq;
if (sysfreq > 180)
{
xdiv = 10;
xdivp = 1;
}
xmul = sysfreq/100*xdiv*xdivp/(xtalfreq/100);
xpppp = ((xdivp >> 1) + 15) & 0xf;
setfreq = (1 << 24) + ((xdiv-1) << 18) + ((xmul - 1) << 8) + (xpppp << 4) + (xosc << 2);
//enafreq = setfreq + xsel;
//printf("SYSFREQ = %d, XMUL = %d, SETFREQ = %8.8x, ENAFREQ = %8.8x\n", sysfreq, xmul, setfreq, enafreq);
//printf("VCOFREQ = %d\n", xtalfreq/xdiv*xmul);
return setfreq;
}
int main(int argc, char **argv)
{
int i;
int runterm = 0;
int pstmode = 0;
char *fname = 0;
char *port = 0;
int address = 0;
char *u9root = 0;
// Parse the command-line parameters
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (argv[i][1] == 'p')
{
if(argv[i][2])
port = &argv[i][2];
else if (++i < argc)
port = argv[i];
else {
Usage("Missing parameter for -p");
}
}
else if (argv[i][1] == 'a' || !strcmp(argv[i], "--args"))
{
i++;
if (i >= argc) {
// allow for 0 arguments to be explicitly passed
argc++;
}
break;
}
else if (argv[i][1] == 'b')
{
if(argv[i][2])
user_baud = atoi(&argv[i][2]);
else if (++i < argc)
user_baud = atoi(argv[i]);
else {
Usage("Missing parameter for -b");
}
}
else if (argv[i][1] == 'l')
{
if(argv[i][2])
loader_baud = atoi(&argv[i][2]);
else if (++i < argc)
loader_baud = atoi(argv[i]);
else
Usage("Missing parameter for -l");
}
else if (argv[i][1] == 'X')
{
if(argv[i][2])
extra_cycles = atoi(&argv[i][2]);
else if (++i < argc)
extra_cycles = atoi(argv[i]);
else
Usage("Missing parameter for -X");
}
else if (argv[i][1] == 'e')
{
if(argv[i][2])
send_script = &argv[i][2];
else if (++i < argc)
send_script = argv[i];
else
Usage("Missing script for -e");
}
else if (argv[i][1] == 'f')
{
if(argv[i][2])
clock_freq = atoi(&argv[i][2]);
else if (++i < argc)
clock_freq = atoi(argv[i]);
else
Usage("Missing frequency for -f");
}
else if (!strcmp(argv[i], "-FIFO"))
{
if (++i < argc)
fifo_size = atoi(argv[i]);
else
Usage("Missing byte count for -FIFO");
}
else if (argv[i][1] == 'k')
{
waitAtExit = 1;
}
else if (argv[i][1] == 'q')
{
quiet_mode = 1;
}
else if (argv[i][1] == 'n')
{
do_hwreset = 0;
}
else if (argv[i][1] == 'm')
{
if(argv[i][2])
clock_mode = atox(&argv[i][2]);
else if (++i < argc)