-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdl.c
2786 lines (2455 loc) · 82.2 KB
/
dl.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
/*
* DeskLink for *nix (dl)
* Copyright (C) 2004
* Stephen Hurd
*
* Redistribution of modified and unmodified copies
* is premitted provided the copyright remains intact
*/
/*
DeskLink+
2005 John R. Hogerhuis Extensions and enhancements
2019 Brian K. White - repackaging, reorganizing, bootstrap function
2020 Kurt McCullum - TS-DOS loaders
2022 Gabriele Gorla - TS-DOS subdirectories
DeskLink2
2023 Brian K. White - disk image files, pdd1 FDC mode, pdd2 cache & memory
DeskLink2 is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 or any
later version as published by the Free Software Foundation.
DeskLink2 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 (in the file "COPYING"); if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111, USA.
*/
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#if defined(__linux__)
#include <utmp.h>
#elif defined(__APPLE__) || defined(__NetBSD__) || defined(OpenBSD)
#include <util.h>
#elif defined(__FreeBSD__)
#include <libutil.h>
#endif
#include "constants.h"
#include "dir_list.h"
#include "xattr.h"
/*** config **************************************************/
#ifndef APP_NAME
#define APP_NAME "DeskLink2"
#endif
#ifndef APP_LIB_DIR
#define APP_LIB_DIR "."
#endif
#ifndef TTY_PREFIX
#define TTY_PREFIX "ttyS"
#endif
#ifndef DEFAULT_BAUD
#define DEFAULT_BAUD 19200
#endif
// default model emulation, 1=pdd1 2=pdd2
// TS-DOS sub-directories requires tpdd1
#ifndef DEFAULT_MODEL
#define DEFAULT_MODEL 1
#endif
// if a loader fails in bootstrap(), try increasing this
#ifndef DEFAULT_BASIC_BYTE_MS
#define DEFAULT_BASIC_BYTE_MS 8
#endif
#define DEFAULT_TPDD1_IMG_SUFFIX ".pdd1"
#define DEFAULT_TPDD2_IMG_SUFFIX ".pdd2"
#ifndef DEFAULT_UPCASE
#define DEFAULT_UPCASE false
#endif
#ifndef DEFAULT_RTSCTS
#define DEFAULT_RTSCTS false
#endif
#ifndef DEFAULT_PROFILE
#define DEFAULT_PROFILE "k85"
#endif
#ifndef DEFAULT_OPERATION_MODE
#define DEFAULT_OPERATION_MODE MODE_OPR
#endif
#ifndef DEFAULT_TILDES
#define DEFAULT_TILDES true
#endif
// To mimic the original Desk-Link from Travelling Software:
#ifndef TSDOS_ROOT_LABEL
#define TSDOS_ROOT_LABEL "0: "
#endif
#ifndef TSDOS_PARENT_LABEL
#define TSDOS_PARENT_LABEL "^ "
#endif
// you can't change this unless you also hack ts-dos
#define TSDOS_DIR_LABEL "<>"
/*
* "magic" files - See ref/ur2.txt
*
* Support for Ultimate ROM II, TSLOAD, & any other on-the-fly loaders.
* These filenames will always be loadable "by magic" in any cd path, even
* if no such filename exists anywhere in the share tree.
*
* Whenever a client tries to request any of these filenames,
* after searching cwd-within-share-path as normal, then search share root, finally app_lib_dir.
* They will always be found in app_lib_dir if nowhere else.
* TODO add $XDG_DATA_HOME (~/.local/share/myapp mac: ~/Library/myapp/)
*
* You may add any other files you want here if you find any other software
* that tries to load-use-discard a file from disk like UR2 uses DOS100.CO.
*
* Files must also be added to install target in Makefile.
*
* This list is checked for a match every time a requested filename is not found,
* so keep it short.
*
* TODO add run-time config list of filenames and search paths
*/
const char * magic_files[] = {
"DOS100.CO",
"DOS200.CO",
"DOSNEC.CO",
"SAR100.CO",
"SAR200.CO",
// The rest of these files don't exist, but we are ready to serve them up if they did exist.
// Some are known to have existed, but no known copies available currently.
// Some may not have ever existed. Most filenames are guesses.
"SARNEC.CO", // Sardine for NEC is known to have existed, with this filename.
"DOSM10.CO", // or DOSOLV.CO ? Jeff Birt found TS-DOS for Olivetti M-10 listed in a catalog.
"DOSK85.CO", // or DOSKYO.CO ? may have never existed
"SARM10.CO", // or SAROLV.CO ? Since TS-DOS for M-10 existed, probably Sardine existed too.
"SARK85.CO" // or SRAKYO.CO ? may have never existed
};
// client compatibility profiles
// kc-85 platform can use lowercase filenames just fine, but at least both both
// TS-DOS and TEENY convert to uppercase in places, so upcase to avoid the battle.
// REXCPM native is cpm, but import & export forces 6.2 upcase.
// Cambridge Z88 native is 12.3, not sure what DISCMNGR or DISC_RBL actually does.
// Atari ST native is cpm, but PDDOS limits to 6.2 .
// No xenix client exists probably, but it would be 14.0 .
// { "xenix", 14, 0, false, ATTR_RAW, false, false, false }
// id, base, ext, pad, attr, dme, magic, upcase
#define CLIENT_PROFILES { \
{ "raw", 0, 0, false, ATTR_RAW, false, false, false }, \
{ "k85", 6, 2, true, ATTR_DEF, true, true, true }, \
{ "wp2", 8, 2, true, ATTR_DEF, false, false, false }, \
{ "cpm", 8, 3, false, ATTR_DEF, false, false, false }, \
{ "rexcpm", 6, 2, true, ATTR_DEF, false, false, true }, \
{ "z88", 12, 3, false, ATTR_DEF, false, false, false }, \
{ "st", 6, 2, true, ATTR_DEF, false, false, false } \
}
// terminal emulation
#define SSO "\033[7m" // set standout
#define RSO "\033[m" // reset standout
#define D8C "\033 F" // disable 8-bit vt control bytes (0x80-0x9F)
// The TPDD1 rom is actually the FB-100 rom.
// The roms in Brother FB-100, knitking FDD19, Purple Computing D103, and
// TANDY 26-3808 (TPDD1) have all been dumped and compared, and are all identical.
// That means the rom came from Brother and is the FB-100 rom in all cases.
// We have this file but it's not used currently.
//#ifndef FB100_ROM
//#define FB100_ROM "Brother_FB-100.rom"
//#endif
// The TPDD2 rom is used because the normal TPDD2 memory access functions
// can read the rom contents the same as any other memory address.
#ifndef TPDD2_ROM
#define TPDD2_ROM "TANDY_26-3814.rom"
#endif
// termios VMIN & VTIME
#define C_CC_VMIN 1
#define C_CC_VTIME 5
/*************************************************************/
int debug = 0;
int operation_mode = DEFAULT_OPERATION_MODE;
bool upcase = DEFAULT_UPCASE;
bool rtscts = DEFAULT_RTSCTS;
bool tildes = DEFAULT_TILDES;
uint8_t model = DEFAULT_MODEL;
uint16_t baud = DEFAULT_BAUD;
int BASIC_byte_us = DEFAULT_BASIC_BYTE_MS*1000;
char client_tty_name[PATH_MAX+1] = {0x00};
char disk_img_fname[PATH_MAX+1] = {0x00};
char app_lib_dir[PATH_MAX+1] = APP_LIB_DIR;
char share_path[2][PATH_MAX+1] = {{0},{0}};
char dme_root_label[7] = TSDOS_ROOT_LABEL;
char dme_parent_label[7] = TSDOS_PARENT_LABEL;
char dme_dir_label[3] = TSDOS_DIR_LABEL;
uint8_t cfnl = TPDD_FILENAME_LEN;
#if !defined(_WIN)
bool getty_mode = false;
#endif
char** args;
int f_open_mode = F_OPEN_NONE;
int client_tty_fd = -1;
int disk_img_fd = -1;
struct termios client_termios;
int o_file_h = -1;
uint8_t gb[TPDD_MSG_MAX];
char iwd[PATH_MAX+1] = {0x00};
char cwd[PATH_MAX+1] = {0x00};
char dme_cwd[7] = TSDOS_ROOT_LABEL;
char bootstrap_fname[PATH_MAX+1] = {0x00};
uint8_t in_dme = 0;
uint8_t bank = 0;
uint8_t ch[2] = {0xFF}; // 0x00 is a valid Operation-mode command, so init to 0xFF
uint8_t rb[SECTOR_LEN] = {0x00}; // pdd1 disk image record buffer
FILE_ENTRY* cur_file;
int dir_depth=0;
uint8_t pdd1_condition = PDD1_COND_NONE; // pdd1 condition bit flags
uint8_t pdd2_condition = PDD2_COND_NONE; // pdd2 condition bit flags
// drive cpu memory map
uint8_t ioport[IOPORT_LEN] = {0x00}; // i/o port
uint8_t cpuram[CPURAM_LEN] = {0x00}; // 128 bytes cpu internal ram
uint8_t ga[GA_LEN] = {0x00}; // gate array interface
uint8_t ram[RAM_LEN] = {0x00}; // 2k ram (pdd2 disk image record buffer)
uint8_t rom[ROM_LEN] = {0x00}; // 4k cpu internal mask rom
// client compatibility settings
#define PROFILE_ID_LEN 8
typedef struct {
char id[PROFILE_ID_LEN+1];
uint8_t base;
uint8_t ext;
bool pad;
uint8_t attr;
bool dme;
bool magic;
bool upcase;
} CLIENT_PROFILE;
const CLIENT_PROFILE profiles [] = CLIENT_PROFILES ;
//const char* profile = profiles[0].id;
char profile[PROFILE_ID_LEN+1] = {0};
uint8_t base_len = 0;
uint8_t ext_len = 0;
char default_attr = ATTR_RAW;
bool enable_magic_files = false;
bool pad_fn = false;
bool dme_en = false;
///////////////////////////////////////////////////////////////////////////////
void show_main_help();
/* primitives and utilities */
// dbg(verbosity_threshold, printf_format, args...)
// dbg(3,"err %02X",err); // means only show this message if debug>=3
void dbg( const int v, const char* format, ... ) {
if (debug<v) return;
va_list args;
va_start( args, format );
vfprintf( stderr, format, args );
fflush(stderr);
va_end( args );
}
// dbg_b(verbosity_threshold, buffer, len)
// dbg_b(3, b, 24); // like dbg() except
// print n bytes of b[] as hex pairs and a trailing newline
// if n<0, then use TPDD_MSG_MAX
void dbg_b(const int v, unsigned char* b, int n) {
if (debug<v) return;
unsigned i;
if (n<0) n = TPDD_MSG_MAX;
for (i=0;i<n;i++) fprintf (stderr,"%02X ",b[i]);
fprintf (stderr, "\n");
fflush(stderr);
}
// like dbg_b, except assume b[] is an Operation-mode req or ret block
// and parse it to display the parts: cmd, len, payload, checksum.
void dbg_p(const int v, unsigned char* b) {
dbg(v,"cmd: %1$02X\nlen: %2$02X (%2$u)\nchk: %3$02X\ndat: ",b[0],b[1],b[b[1]+2]);
dbg_b(v,b+2,b[1]);
}
// ascii-to-bool
// true = case-insensitive: 1 y yes t true on enable
bool atobool (const char* s) {
// min 2 chars to tell "on" from "off"
char t[5] = {0};
t[0]=',';
t[1]=s[0]?tolower(s[0]):' '; // replace the nuls to avoid
t[2]=s[1]?tolower(s[1]):' '; // s="o" -> t=",o" -> matches ",on,"
t[3]=',';
return strstr(",on,1 ,t ,y ,tr,ye,en,",t);
}
// int-to-rate - given int 9600 return macro B9600
speed_t itobaud (uint32_t i) {
return
i==0?B0:
i==50?B50:
i==75?B75:
i==110?B110:
i==134?B134:
i==150?B150:
i==200?B200:
i==300?B300:
i==600?B600:
i==1200?B1200:
i==1800?B1800:
i==2400?B2400:
i==4800?B4800:
i==9600?B9600:
i==19200?B19200:
i==38400?B38400:
#ifdef B57600
i==57600?B57600:
#endif
#ifdef B76800
i==76800?B76800:
#endif
#ifdef B115200
i==115200?B115200:
#endif
#ifdef B153600
i==153600?B153600:
#endif
#ifdef B230400
i==230400?B230400:
#endif
#ifdef B307200
i==307200?B307200:
#endif
#ifdef B460800
i==460800?B460800:
#endif
#ifdef B500000
i==500000?B500000:
#endif
#ifdef B576000
i==576000?B576000:
#endif
#ifdef B614400
i==614400?B614400:
#endif
#ifdef B921600
i==921600?B921600:
#endif
#ifdef B1000000
i==1000000?B1000000:
#endif
#ifdef B1152000
i==1152000?B1152000:
#endif
#ifdef B1500000
i==1500000?B1500000:
#endif
#ifdef B2000000
i==2000000?B2000000:
#endif
#ifdef B2500000
i==2500000?B2500000:
#endif
#ifdef B3000000
i==3000000?B3000000:
#endif
#ifdef B3500000
i==3500000?B3500000:
#endif
#ifdef B4000000
i==4000000?B4000000:
#endif
0;
}
// given int 19200 return 9 (the # in "COM:#8N1ENN")
uint8_t baud_to_stat_code (uint16_t r) {
return
r==75?1:
r==110?2:
r==300?3:
r==600?4:
r==1200?5:
r==2400?6:
r==4800?7:
r==9600?8:
r==19200?9:
0;
}
void show_profiles_help (int e) {
const int n = sizeof(profiles)/sizeof(profiles[0]);
dbg(0,
"\n"
"help for Client Compatibility Profiles\n"
"\n"
"usage:\n"
" -c name use profile <name> - (default: \"%s\")\n"
" -c #.# \"raw\" with filenames truncated to #.# & attr='%c'\n"
" -c #.#p \"#.#\" fixed-length space-padded\n"
" -v -c more help\n"
,DEFAULT_PROFILE,ATTR_DEF
);
dbg(1,
"\n"
"Profiles taylor the translation between local filenames and TPDD filenames.\n"
"\n"
"A real TPDD doesn't care what's in the filename, and emulating a TPDD\n"
"doesn't require any translation other than truncating to 24 bytes.\n"
"\n"
"But most TPDD clients write filenames to TPDD drives in specific formats,\n"
"and we need to translate filenames between the local and client formats.\n"
"\n"
"Strictly speaking, \"raw\" always works for any and all clients,\n"
"from the clients point of view. It still emulates a real drive exactly.\n"
"\n"
"The only reason for any compatibility profile is for more convenient\n"
"local filenames. When TS-DOS saves a file like \"A.BA\", it actually\n"
"writes \"A .BA\" to a real drive. In \"raw\" mode this would create a\n"
"local file named verbatim: \"A .BA\", which is legal but inconvenient.\n"
"And TS-DOS does not recognize any disk files that don't conform\n"
"to the \"k85\" profile below. (fixed-length, space-padded, 6.2)\n"
"\n"
"\"raw\" still \"works\" because TS-DOS can both create any files it\n"
"wants, and access any files it created, identical to a real drive.\n"
"\n"
"Profiles just make it so that a local file named \"my_long_file_name.text\"\n"
"appears to TS-DOS as \"my_lo~.t~\", which may be ugly but TS-DOS can use it.\n"
"And when TS-DOS tries to read or write a file named \"FOO .CO\",\n"
"we use \"FOO.CO\" for the local filename.\n"
"\n"
"Most of the parameters in a profile also have individual commandline flags.\n"
"Example: \"-c k85\" is short for \"-c 6.2p -a F -e on\"\n"
"(except k85 is the default so you don't need to use any of those)\n"
"\n"
"The default \"k85\" matches all KC-85-clone platform clients. Examples:\n"
"Floppy, TS-DOS, DSKMGR, TEENY, etc, on TRS-80 Model 100, NEC PC-8201a, etc.\n"
"\n"
"NAME profile name\n"
"BASE basename length\n"
"EXT extension length\n"
"PAD fixed-length space-padded\n"
"ATTR default attribute byte if no xattr\n"
"DME enable TS-DOS directory mode extension\n"
"TSLOAD enable \"magic files\" (ex: DOS100.CO) for TSLOAD / Ultimate ROM II\n"
"UPCASE translate filenames to all uppercase\n"
"\n"
"Available profiles:\n"
);
dbg(0,
"\n"
// "PROFILE\tBASE\tEXT\tPAD\tATTR\tTS-DOS\tMAGIC\tUP\n"
// "NAME\tLEN\tLEN\tFNAMES\tBYTE\tDIRS\tFILES\tCASE\n"
"NAME\tBASE\tEXT\tPAD\tATTR\tDME\tTSLOAD\tUPCASE\n"
"-------------------------------------------------------------\n"
);
for (int i=0; i<n; i++) {
dbg(0,
"%s\t%d\t%d\t%s\t'%c'\t%s\t%s\t%s\n",
profiles[i].id,
profiles[i].base,
profiles[i].ext,
profiles[i].pad?"on":"off",
profiles[i].attr,
profiles[i].dme?"on":"off",
profiles[i].magic?"on":"off",
profiles[i].upcase?"on":"off"
);
}
dbg(0,"\n");
exit(e);
}
bool ckhelp (const char* s) {
return (
!s[0] ||
!strncasecmp(s,"list",PROFILE_ID_LEN) ||
!strncasecmp(s,"help",PROFILE_ID_LEN) ||
!strncasecmp(s,"?",PROFILE_ID_LEN)
);
}
// set base_len, ext_len, pad_fn from ##.##p
void set_fnames (const char* s) {
if (ckhelp(s)) show_profiles_help(0);
int i, p;
char t[4] = {0};
p = strchr(s,'.')-s;
if (p<1 || p>2) show_profiles_help(1);
for (i=sizeof(s);i>p;i--) {
if (s[i]=='p'||s[i]=='P') pad_fn = true;
if (s[i]>='0' && s[i]<='9') break;
}
memcpy(t,s,p);
i = atoi(t);
if (i>0 && i<TPDD_FILENAME_LEN) base_len = i;
memset(t,0,4);
i = sizeof(s)-p-1;
if (i>4) i = 4;
memcpy(t,s+p+1,i);
i = atoi(t);
if (i>-1 && i<TPDD_FILENAME_LEN-base_len) ext_len = i;
snprintf(profile,PROFILE_ID_LEN+1,"%s",s);
pad_fn = false;
default_attr = ATTR_DEF;
dme_en = false;
enable_magic_files = false;
upcase = false;
return;
}
// client compatibility profile
void load_profile (const char* s) {
const int n = sizeof(profiles)/sizeof(profiles[0]);
int i, p;
if (ckhelp(s)) show_profiles_help(0);
// search for matching profile by name
p = false;
for (i=0; i<n; i++) {
if (!strncasecmp(s,profiles[i].id,PROFILE_ID_LEN)) { p = true ;break; }
}
// If no profile by name, try #.#[p]
// do it after searching by name so that a profile name can have "." in it
if (strchr(s,'.')) { set_fnames(s); return; }
if (!p) {
dbg(0,"No profile named \"%s\" found.\n",s);
show_profiles_help(1);
}
strncpy(profile,profiles[i].id,PROFILE_ID_LEN);
base_len = profiles[i].base;
ext_len = profiles[i].ext;
pad_fn = profiles[i].pad;
default_attr = profiles[i].attr;
dme_en = profiles[i].dme;
enable_magic_files = profiles[i].magic;
upcase = profiles[i].upcase;
}
void update_cwd () {
memset(cwd,0x00,PATH_MAX);
(void)!getcwd(cwd,PATH_MAX);
// if the current directory is not writable, set the write-protected disk flag
uint8_t wp = 0;
if (access(cwd,W_OK|X_OK)) wp = 1;
pdd1_condition |= wp << PDD1_COND_BIT_WPROT;
pdd2_condition |= wp << PDD2_COND_BIT_WPROT;
}
void add_share_path (char* s) {
dbg(3,"%s(%s)\n",__func__,s);
if (!share_path[0][0]) { strcpy(share_path[0],s); return; }
if (!share_path[1][0]) { strcpy(share_path[1],s); return; }
dbg(2,"Discarded excess share path \"%s\"\n",s);
}
void cd_share_path () {
if (!share_path[bank][0]) return;
if (!strncmp(cwd,share_path[bank],PATH_MAX)) return;
if (chdir(share_path[bank])) dbg(0,"FAILED CD TO \"%s\"\n",share_path[bank]);
update_cwd();
}
// maybe rewrite f[] with /path/to/f
void find_lib_file (char* f) {
if (f[0]==0x00) return;
char t[PATH_MAX+1]={0x00};
// rewrite ~/foo to $HOME/foo
if (f[0]=='~' && f[1]=='/') {
strcpy(t,f);
memset(f,0x00,PATH_MAX);
strcpy(f,getenv("HOME"));
strcat(f,t+1);
}
if (f[0]=='/') return; // don't rewrite any absolute path
if (f[0]=='.' && f[1]=='/') return; // don't rewrite explicit relative path
if (f[0]=='.' && f[1]=='.' && f[2]=='/') return; // don't rewrite explicit relative path
if (!access(f,F_OK)) return; // if pathless filename exists & accessible, use it as-is
// none of above matched, look in app_lib_dir
memset(t,0x00,PATH_MAX);
strcpy(t,app_lib_dir);
strcat(t,"/");
strcat(t,f);
// if found in app_lib_dir then rewrite with that path
if (!access(t,F_OK)) {
memset(f,0x00,PATH_MAX);
strcpy(f,t);
}
// else leave f[] as it was, some consumers create the file if not exist
}
int check_disk_image () {
// if they didn't ask for any disk image, then bail without error
if (!disk_img_fname[0]) return 0;
dbg(3,"looking for disk image \"%s\"\n",disk_img_fname);
find_lib_file(disk_img_fname);
// disk_img_fname has now been re-written if and as necessary,
// and still may or may not exist
struct stat info;
if (!stat(disk_img_fname, &info) && info.st_size>0) {
// if file exists and >0 bytes
dbg(1,"Loading disk image file \"%s\"\n",disk_img_fname);
// use file size to automatically set model 1 vs 2 or reject file
if (info.st_size==PDD1_IMG_LEN) model = 1;
if (info.st_size==PDD2_IMG_LEN) model = 2;
if (model==1 && info.st_size != PDD1_IMG_LEN) {
dbg(0,"%d bytes, expected %u bytes for TPDD1\n",info.st_size,PDD1_IMG_LEN);
return 1;
}
if (model==2 && info.st_size != PDD2_IMG_LEN) {
dbg(0,"%d bytes, expected %u bytes for TPDD2\n",info.st_size,PDD2_IMG_LEN);
return 1;
}
} else {
// if file doesn't exist or is 0 bytes
dbg(1,"Disk image file \"%s\" is empty or does not exist.\nIt will be created if the client issues a format command.\n",disk_img_fname);
// use file name to automatically set model 1 vs 2
char ext[6] = {0};
strncpy(ext,disk_img_fname+strlen(disk_img_fname)-5,5);
if (!strcasecmp(ext,DEFAULT_TPDD1_IMG_SUFFIX)) model = 1;
else if (!strcasecmp(ext,DEFAULT_TPDD2_IMG_SUFFIX)) model = 2;
}
// rewrite with leading path if not already
// because we we may cd all over the place
if (disk_img_fname[0]=='/') return 0;
char t[PATH_MAX+1] = {0};
strcpy(t,iwd);
strcat(t,"/");
strcat(t,disk_img_fname);
memcpy(disk_img_fname,t,PATH_MAX+1);
return 0;
}
// search for TTY(s) matching TTY_PREFIX
void find_ttys (char* f) {
dbg(3,"%s(%s)\n",__func__,f);
// open /dev
char path[] = "/dev/";
DIR* dir = opendir(path);
if (!dir){dbg(0,"Cannot open \"%s\"\n",path); return;}
// read /dev, look for all files beginning with prefix
// add any matches to ttys[]
char** ttys = malloc(sizeof(char*));
struct dirent *files;
uint16_t nttys = 0, l=strlen(f);
#if defined(__FreeBSD__)
char* p;
#endif
dbg(2,"Searching for \"%s%s*\"\n",path,f);
while ((files = readdir(dir))) {
if (strncmp(files->d_name,f,l)) continue;
#if defined(__FreeBSD__)
p = strrchr(files->d_name,'.');
if (p!=NULL) if (!strcmp(p,".init") || !strcmp(p,".lock")) continue;
#endif
nttys++;
ttys = realloc(ttys, (nttys+1) * sizeof(char(*)));
ttys[nttys] = files->d_name;
}
closedir(dir);
int i=0;
if (nttys==1) i=1; // if there is only one element in ttys[], use it
if (nttys>1) while (!i) { // if more than 1 then menu
dbg(0,"\n");
for (i=1;i<=nttys;i++) dbg(0,"%d) %s\n",i,ttys[i]);
i=0; char a[6]={0};
dbg(0,"Which serial port is the TPDD client on (1-%d or q) ? ",nttys);
if (fgets(a,sizeof(a),stdin)) i=atoi(a);
if (i<1 || i>nttys) i=0;
dbg(0,"\n");
if (a[0]=='q'||a[0]=='Q') break;
}
// set client_tty_name[] with the final result
client_tty_name[0]=0x00;
if (i) {
strcpy(client_tty_name,path);
strcat(client_tty_name,ttys[i]);
}
free(ttys);
}
// take the user-supplied tty arg and figure out the actual /dev/ttyfoo
void resolve_client_tty_name () {
dbg(3,"%s()\n",__func__);
switch (client_tty_name[0]) {
case 0x00:
// nothing supplied, scan for any ttys matching the default prefix
find_ttys(TTY_PREFIX);
break;
case '-':
// stdin/stdout mode, silence all messages - untested
debug = -1;
strcpy (client_tty_name,"/dev/tty");
client_tty_fd=1;
break;
default:
// something given, try with and without prepending /dev/
if (!access(client_tty_name,F_OK)) break;
char t[PATH_MAX+1]={0x00};
int i = 0;
strcpy(t,client_tty_name);
strcpy(client_tty_name,"/dev/");
if (!strncmp(client_tty_name,t,5)) i=5;
strcat(client_tty_name,t+i);
}
}
// set termios VMIN & VTIME
void client_tty_vmt(int m,int t) {
if (m<-1 || t<-1) tcgetattr(client_tty_fd,&client_termios);
if (m<0) m = C_CC_VMIN;
if (t<0) t = C_CC_VTIME;
if (client_termios.c_cc[VMIN] == m && client_termios.c_cc[VTIME] == t) return;
client_termios.c_cc[VMIN] = m;
client_termios.c_cc[VTIME] = t;
tcsetattr(client_tty_fd,TCSANOW,&client_termios);
}
int open_client_tty () {
dbg(3,"%s()\n",__func__);
if (!client_tty_name[0]) {
show_main_help();
dbg(0,"Error: No serial device specified\n(searched: /dev/%s*)\n",TTY_PREFIX);
return 1;
}
dbg(0,"Opening \"%s\" ... ",client_tty_name);
// open with O_NONBLOCK to avoid hang if client not ready, then unset later.
if (client_tty_fd<0) client_tty_fd=open((char *)client_tty_name,O_RDWR|O_NOCTTY|O_NONBLOCK);
if (client_tty_fd<0) { dbg(0,"%s\n",strerror(errno)); return 1; }
dbg(0,"OK\n");
#ifdef TIOCEXCL
ioctl(client_tty_fd,TIOCEXCL);
#endif
#if !defined(_WIN)
if (getty_mode) {
debug = -1;
if (!login_tty(client_tty_fd)) client_tty_fd = STDIN_FILENO;
else (void)!daemon(1,1);
}
#endif
(void)!tcflush(client_tty_fd, TCIOFLUSH);
// unset O_NONBLOCK
fcntl(client_tty_fd, F_SETFL, fcntl(client_tty_fd, F_GETFL, NULL) & ~O_NONBLOCK);
if (tcgetattr(client_tty_fd,&client_termios)==-1) return 21;
cfmakeraw(&client_termios);
client_termios.c_cflag |= CLOCAL|CS8;
if (rtscts) client_termios.c_cflag |= CRTSCTS;
else client_termios.c_cflag &= ~CRTSCTS;
if (cfsetspeed(&client_termios,itobaud(baud))==-1) return 22;
if (tcsetattr(client_tty_fd,TCSANOW,&client_termios)==-1) return 23;
client_tty_vmt(-2,-2);
return 0;
}
int write_client_tty(void* b, int n) {
dbg(4,"%s(%u)\n",__func__,n);
n = write(client_tty_fd,b,n);
dbg(3,"SENT: "); dbg_b(3,b,n);
return n;
}
// It is correct that this blocks and waits forever.
// The one time we don't want to block, we don't use this.
int read_client_tty(void* b, const unsigned int n) {
dbg(4,"%s(%u)\n",__func__,n);
unsigned t = 0;
int i = 0;
while (t<n) if ((i = read(client_tty_fd, b+t, n-t))) t+=i;
if (i<0) {
dbg(0,"error: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
dbg(3,"RCVD: "); dbg_b(3,b,n);
return t;
}
// cat a file to terminal, for custom loader directions in bootstrap()
void dcat(char* f) {
char b[4097]={0x00};
int h=open(f,O_RDONLY);
if (h<0) return;
while (read(h,&b,4096)>0) dbg(0,"%s",b);
close(h);
}
/*
* The manual says:
*
* "The checksum is the one's complement of the least significant byte
* of the number of bytes from the block format through the data block."
*
* But the bytes are summed, not just counted!
* Replace "number of" with "sum of the".
*
* Sum all the bytes in the specified range.
* Take the least significant byte of that sum.
* Invert all the bits in that byte.
*
* b[0] = cmd (block format)
* b[1] = len
* b[2] to b[1+len] = 0 to 128 bytes of payload (data block)
* ignore everything after b[1+len]
*/
uint8_t checksum(unsigned char* b) {
uint16_t s=0; uint8_t i, l=2+b[1];
for (i=0;i<l;i++) s+=b[i];
return ~(s&0xFF);
}
char* collapse_padded_fname(char* fname) {
dbg(3,"%s(\"%s\")\n",__func__,fname);
if (!pad_fn) return fname;
if (!base_len) return fname;
int i;
for (i=base_len;i>1;i--) if (fname[i-1]!=' ') break;
if (fname[base_len+1]==dme_dir_label[0] && fname[base_len+2]==dme_dir_label[1]) {
fname[i]=0x00;
} else {
fname[i]=fname[base_len];
fname[i+1]=fname[base_len+1];
fname[i+2]=fname[base_len+2];
fname[i+3]=0x00;
}
return fname;
}
void lsx (char* path,char* match,char* fmt) {
struct dirent *files;
DIR *dir = opendir(path);
if (!dir){dbg(0,"Cannot open \"%s\"",path); return;}
int i;
while ((files = readdir(dir))) {
for (i=strlen(files->d_name);files->d_name[i]!='.';i--);
if (!strcmp(files->d_name+i+1,match)) dbg(0,fmt,files->d_name);
}
closedir(dir);
}
int check_magic_file(char* b) {
dbg(3,"%s(\"%s\")\n",__func__,b);
if (!enable_magic_files) return 1;
int l = sizeof(magic_files)/sizeof(magic_files[0]);
for (int i=0;i<l;++i) if (!strcmp(magic_files[i],b)) return 0;
return 1;
}
// This is kind of silly but why not? Load a rom image file into rom[],
// then tpdd2 mem_read() in the ROM address range returns data from rom[],
void load_rom(char* f) {
dbg(3,"%s(%s)\n",__func__,f);
char t[PATH_MAX+1] = {0x00};
strncpy(t,f,PATH_MAX);
find_lib_file(t);
int h = open(t,O_RDONLY);
if (h<0) return;
(void)!read(h,rom,ROM_LEN);
close(h);
dbg_b(3,rom,ROM_LEN);
}
////////////////////////////////////////////////////////////////////////
//
// FDC MODE
//
/*
* sectors: 0-79
* sector: 1293 bytes
* | LSC 1 byte | ID 12 bytes | DATA 1280 bytes |
* LSC: logical sector size code
* ID: 12 bytes of arbitrary data, searchable by req_fdc_search_id()
* DATA: 1280 bytes of arbitrary data, read/writable in lsc_to_len(LSC)-sized chunks
*/
// standard fdc-mode 8-byte response
// e = error code ERR_FDC_* -> ascii hex pair
// s = status or data -> ascii hex pair
// l = length or address -> 2 ascii hex pairs
// TODO - don't assume endianness
void ret_fdc_std(uint8_t e, uint8_t s, uint16_t l) {
dbg(2,"%s()\n",__func__);
char b[9] = { 0x00 };
snprintf(b,9,"%02X%02X%04X",e,s,l);
dbg(2,"FDC: response: \"%s\"\n",b);
write_client_tty(b,8);
}
// p : physical sector to seek to
// m : mode read-only / write-only / read-write
int open_disk_image (int p, int m) {
dbg(2,"%s(%d,%d)\n",__func__,p,m);
int of; int e=ERR_FDC_SUCCESS;
if (!*disk_img_fname) e=ERR_FDC_NO_DISK;
if (!e) switch (m) {
case O_RDWR: of=O_RDWR; dbg(2,"edit rw\n");
if (access(disk_img_fname,W_OK)) e=ERR_FDC_WRITE_PROTECT;
break;
case O_WRONLY: of=O_WRONLY;
if (access(disk_img_fname,F_OK)) { of|=O_CREAT; dbg(2,"create\n");} else {
dbg(2,"edit wo\n");
if (access(disk_img_fname,W_OK)) e=ERR_FDC_WRITE_PROTECT;
}
break;
default: of=O_RDONLY; dbg(2,"read\n"); break;
}
if (!e) {
disk_img_fd=open(disk_img_fname,of|O_EXCL,0666);
if (disk_img_fd<0) { dbg(0,"%s\n",strerror(errno)) ;e=ERR_FDC_READ;}