forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.cpp
10101 lines (9129 loc) · 273 KB
/
filesys.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* Unix file system handler for AmigaDOS
*
* Copyright 1996 Ed Hanway
* Copyright 1996, 1997 Bernd Schmidt
*
* Version 0.4: 970308
*
* Based on example code (c) 1988 The Software Distillery
* and published in Transactor for the Amiga, Volume 2, Issues 2-5.
* (May - August 1989)
*
* Known limitations:
* Does not support several (useless) 2.0+ packet types.
* May not return the correct error code in some cases.
* Does not check for sane values passed by AmigaDOS. May crash the emulation
* if passed garbage values.
* Could do tighter checks on malloc return values.
* Will probably fail spectacularly in some cases if the filesystem is
* modified at the same time by another process while UAE is running.
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "threaddep/thread.h"
#include "options.h"
#include "traps.h"
#include "uae.h"
#include "memory.h"
#include "custom.h"
#include "events.h"
#include "newcpu.h"
#include "filesys.h"
#include "autoconf.h"
#include "fsusage.h"
#include "native2amiga.h"
#include "scsidev.h"
#include "uaeserial.h"
#include "fsdb.h"
#include "zfile.h"
#include "zarchive.h"
#include "gui.h"
#include "gayle.h"
#include "idecontrollers.h"
#include "savestate.h"
#include "a2091.h"
#include "ncr_scsi.h"
#include "cdtv.h"
#include "sana2.h"
#include "bsdsocket.h"
#include "uaeresource.h"
#include "inputdevice.h"
#include "clipboard.h"
#include "consolehook.h"
#include "blkdev.h"
#include "isofs_api.h"
#include "scsi.h"
#include "uaenative.h"
#include "tabletlibrary.h"
#include "cia.h"
#include "picasso96.h"
#include "cpuboard.h"
#include "rommgr.h"
#include "debug.h"
#include "debugmem.h"
#ifdef RETROPLATFORM
#include "rp.h"
#endif
#define TRACING_ENABLED 1
int log_filesys = 0;
#define TRAPMD 1
#if TRACING_ENABLED
#if 0
#define TRACE(x) if (log_filesys > 0 && (unit->volflags & MYVOLUMEINFO_CDFS)) { write_log x; }
#else
#define TRACE(x) if (log_filesys > 0) { write_log x; }
#endif
#define TRACEI(x) if (log_filesys > 0) { write_log x; }
#define TRACE2(x) if (log_filesys >= 2) { write_log x; }
#define TRACE3(x) if (log_filesys >= 3) { write_log x; }
#define DUMPLOCK(c,u,x) dumplock(c,u,x)
#else
#define TRACE(x)
#define DUMPLOCK(c,u,x)
#define TRACE2(x)
#define TRACE3(x)
#endif
#define KS12_BOOT_HACK 1
#define UNIT_LED(unit) ((unit)->ui.unit_type == UNIT_CDFS ? LED_CD : LED_HD)
static int bootrom_header;
static uae_u32 dlg (uae_u32 a)
{
return (dbg (a + 0) << 24) | (dbg (a + 1) << 16) | (dbg (a + 2) << 8) | (dbg (a + 3) << 0);
}
static void aino_test (a_inode *aino)
{
#ifdef AINO_DEBUG
a_inode *aino2 = aino, *aino3;
for (;;) {
if (!aino || !aino->next)
return;
if ((aino->checksum1 ^ aino->checksum2) != 0xaaaa5555) {
write_log (_T("PANIC: corrupted or freed but used aino detected!"), aino);
}
aino3 = aino;
aino = aino->next;
if (aino->prev != aino3) {
write_log (_T("PANIC: corrupted aino linking!\n"));
break;
}
if (aino == aino2) break;
}
#endif
}
static void aino_test_init (a_inode *aino)
{
#ifdef AINO_DEBUG
aino->checksum1 = (uae_u32)aino;
aino->checksum2 = aino->checksum1 ^ 0xaaaa5555;
#endif
}
#define UAEFS_VERSION "UAE fs 0.6"
uaecptr filesys_initcode, filesys_initcode_ptr, filesys_initcode_real;
static uaecptr bootrom_start;
static uae_u32 fsdevname, fshandlername, filesys_configdev;
static uae_u32 cdfs_devname, cdfs_handlername;
static uaecptr afterdos_name, afterdos_id, afterdos_initcode;
static uaecptr keymaphook_name, keymaphook_id, keymaphook_initcode;
static uaecptr shell_execute_data, shell_execute_process;
static int filesys_in_interrupt;
static uae_u32 mountertask;
static int automountunit = -1;
static int autocreatedunit;
static int cd_unit_offset, cd_unit_number;
static uaecptr ROM_filesys_doio, ROM_filesys_doio_original;
static uaecptr ROM_filesys_putmsg, ROM_filesys_putmsg_original;
static uaecptr ROM_filesys_putmsg_return;
static uaecptr ROM_filesys_hack_remove;
static smp_comm_pipe shellexecute_pipe;
static uae_u32 segtrack_mode = 0;
#define FS_STARTUP 0
#define FS_GO_DOWN 1
#define DEVNAMES_PER_HDF 32
#define UNIT_FILESYSTEM 0
#define UNIT_CDFS 1
typedef struct {
int unit_type;
int open; // >0 start as filesystem, <0 = allocated but do not start
TCHAR *devname; /* device name, e.g. UAE0: */
uaecptr devname_amiga;
uaecptr startup;
uaecptr devicenode;
uaecptr parmpacket;
TCHAR *volname; /* volume name, e.g. CDROM, WORK, etc. */
int volflags; /* volume flags, readonly, stream uaefsdb support */
TCHAR *rootdir; /* root native directory/hdf. empty drive if invalid path */
struct zvolume *zarchive;
TCHAR *rootdirdiff; /* "diff" file/directory */
bool readonly; /* disallow write access? */
bool locked; /* action write protect */
bool unknown_media; /* ID_UNREADABLE_DISK */
int bootpri; /* boot priority. -128 = no autoboot, -129 = no mount */
int devno;
bool wasisempty; /* if true, this unit was created empty */
int canremove; /* if >0, this unit can be safely ejected and remounted */
bool configureddrive; /* if true, this is drive that was manually configured */
bool inject_icons; /* inject icons if directory filesystem */
struct hardfiledata hf;
/* Threading stuff */
smp_comm_pipe *volatile unit_pipe, *volatile back_pipe;
uae_thread_id tid;
struct _unit *self;
/* Reset handling */
uae_sem_t reset_sync_sem;
volatile int reset_state;
/* RDB stuff */
uaecptr rdb_devname_amiga[DEVNAMES_PER_HDF];
int rdb_lowcyl;
int rdb_highcyl;
int rdb_cylblocks;
uae_u8 *rdb_filesysstore;
int rdb_filesyssize;
TCHAR *filesysdir;
/* filesystem seglist */
uaecptr filesysseg;
uae_u32 rdb_dostype;
/* CDFS */
bool cd_open;
int cddevno;
void *cdfs_superblock;
} UnitInfo;
struct uaedev_mount_info {
UnitInfo ui[MAX_FILESYSTEM_UNITS];
};
static struct uaedev_mount_info mountinfo;
int nr_units (void)
{
int cnt = 0;
for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
if (mountinfo.ui[i].open > 0)
cnt++;
}
return cnt;
}
int nr_directory_units (struct uae_prefs *p)
{
int cnt = 0;
if (p) {
for (int i = 0; i < p->mountitems; i++) {
if (p->mountconfig[i].ci.controller_type == HD_CONTROLLER_TYPE_UAE)
cnt++;
}
} else {
for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
if (mountinfo.ui[i].open > 0 && mountinfo.ui[i].hf.ci.controller_type == HD_CONTROLLER_TYPE_UAE)
cnt++;
}
}
return cnt;
}
static int is_virtual (int unit_no)
{
int t = is_hardfile (unit_no);
return t == FILESYS_VIRTUAL || t == FILESYS_CD;
}
int is_hardfile (int unit_no)
{
if (mountinfo.ui[unit_no].volname || mountinfo.ui[unit_no].wasisempty || mountinfo.ui[unit_no].unknown_media) {
if (unit_no >= cd_unit_offset && unit_no < cd_unit_offset + cd_unit_number)
return FILESYS_CD;
return FILESYS_VIRTUAL;
}
if (mountinfo.ui[unit_no].hf.ci.sectors == 0) {
if (mountinfo.ui[unit_no].hf.flags & 1)
return FILESYS_HARDDRIVE;
return FILESYS_HARDFILE_RDB;
}
return FILESYS_HARDFILE;
}
static void close_filesys_unit (UnitInfo *uip)
{
if (!uip->open)
return;
if (uip->hf.handle_valid)
hdf_close (&uip->hf);
if (uip->volname != 0)
xfree (uip->volname);
if (uip->devname != 0)
xfree (uip->devname);
if (uip->rootdir != 0)
xfree (uip->rootdir);
if (uip->unit_pipe)
xfree (uip->unit_pipe);
if (uip->back_pipe)
xfree (uip->back_pipe);
if (uip->cd_open) {
sys_command_close (uip->cddevno);
isofs_unmount (uip->cdfs_superblock);
}
uip->unit_pipe = 0;
uip->back_pipe = 0;
uip->hf.handle_valid = 0;
uip->volname = 0;
uip->devname = 0;
uip->rootdir = 0;
uip->open = 0;
uip->cd_open = 0;
}
static uaedev_config_data *getuci (struct uaedev_config_data *uci, int nr)
{
return &uci[nr];
}
static UnitInfo *getuip (struct uae_prefs *p, int index)
{
if (index < 0)
return NULL;
index = p->mountconfig[index].configoffset;
if (index < 0)
return NULL;
return &mountinfo.ui[index];
}
static int getuindex (struct uae_prefs *p, int index)
{
if (index < 0)
return -1;
return p->mountconfig[index].unitnum;
}
int get_filesys_unitconfig (struct uae_prefs *p, int index, struct mountedinfo *mi)
{
UnitInfo *ui = getuip (p, index);
struct uaedev_config_data *uci = &p->mountconfig[index];
UnitInfo uitmp;
TCHAR filepath[MAX_DPATH];
memset (mi, 0, sizeof (struct mountedinfo));
memset (&uitmp, 0, sizeof uitmp);
_tcscpy(mi->rootdir, uci->ci.rootdir);
if (!ui) {
ui = &uitmp;
if (uci->ci.type == UAEDEV_DIR) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_DIR);
_tcscpy(mi->rootdir, filepath);
mi->ismounted = 1;
if (filepath[0] == 0)
return FILESYS_VIRTUAL;
if (my_existsfile (filepath)) {
mi->ismedia = 1;
return FILESYS_VIRTUAL;
}
if (my_getvolumeinfo (filepath) < 0)
return -1;
mi->ismedia = true;
return FILESYS_VIRTUAL;
} else if (uci->ci.type == UAEDEV_HDF) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_HDF);
_tcscpy(mi->rootdir, filepath);
ui->hf.ci.readonly = true;
ui->hf.ci.blocksize = uci->ci.blocksize;
int err = hdf_open (&ui->hf, filepath);
if (err <= 0) {
mi->ismedia = false;
mi->ismounted = true;
mi->error = err;
if (uci->ci.reserved == 0 && uci->ci.sectors == 0 && uci->ci.surfaces == 0) {
if (ui->hf.flags & 1)
return FILESYS_HARDDRIVE;
return FILESYS_HARDFILE_RDB;
}
return -1;
}
mi->ismedia = true;
if (ui->hf.drive_empty)
mi->ismedia = 0;
hdf_close (&ui->hf);
} else if (uci->ci.type == UAEDEV_CD) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_CD);
_tcscpy(mi->rootdir, filepath);
struct device_info di;
ui->hf.ci.readonly = true;
ui->hf.ci.blocksize = uci->ci.blocksize;
mi->size = -1;
mi->ismounted = true;
if (blkdev_get_info (p, ui->hf.ci.device_emu_unit, &di)) {
mi->ismedia = di.media_inserted != 0;
_tcscpy (mi->rootdir, di.label);
}
#if 0
if (ui->hf.ci.cd_emu_unit == 0)
_tcscpy (mi->rootdir, _T("CD"));
else
_stprintf (mi->rootdir, _T("CD %d"), ui->hf.ci.cd_emu_unit);
#endif
}
} else if (uci->ci.type != UAEDEV_TAPE) {
if (ui->hf.ci.controller_type == HD_CONTROLLER_TYPE_UAE) { // what is this? || (ui->controller && p->cs_ide)) {
mi->ismounted = 1;
if (uci->ci.type == UAEDEV_HDF)
mi->ismedia = ui->hf.drive_empty ? false : true;
else
mi->ismedia = true;
}
}
if (uci->ci.type == UAEDEV_TAPE) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_TAPE);
_tcscpy(mi->rootdir, filepath);
struct device_info di;
int unitnum = getuindex (p, index);
mi->size = -1;
mi->ismounted = false;
if (unitnum >= 0) {
mi->ismounted = true;
if (tape_get_info (unitnum, &di)) {
mi->ismedia = di.media_inserted != 0;
_tcscpy (mi->rootdir, di.label);
}
} else {
struct scsi_data_tape *tape;
unitnum = 0;
tape = tape_alloc (unitnum, filepath, uci->ci.readonly);
if (tape) {
if (tape_get_info (unitnum, &di)) {
mi->ismedia = di.media_inserted != 0;
_tcscpy (mi->rootdir, di.label);
}
tape_free (tape);
}
}
return FILESYS_TAPE;
}
if (mi->size < 0)
return -1;
mi->size = ui->hf.virtsize;
if (uci->ci.highcyl) {
uci->ci.cyls = mi->nrcyls = uci->ci.highcyl;
} else {
uci->ci.cyls = mi->nrcyls = (int)(uci->ci.sectors * uci->ci.surfaces ? (ui->hf.virtsize / uci->ci.blocksize) / (uci->ci.sectors * uci->ci.surfaces) : 0);
}
if (uci->ci.type == UAEDEV_DIR)
return FILESYS_VIRTUAL;
if (uci->ci.reserved == 0 && uci->ci.sectors == 0 && uci->ci.surfaces == 0) {
if (ui->hf.flags & 1)
return FILESYS_HARDDRIVE;
return FILESYS_HARDFILE_RDB;
}
return FILESYS_HARDFILE;
}
static void stripsemicolon (TCHAR *s)
{
if (!s)
return;
while (_tcslen(s) > 0 && s[_tcslen(s) - 1] == ':')
s[_tcslen(s) - 1] = 0;
}
static void stripspace (TCHAR *s)
{
if (!s)
return;
for (int i = 0; i < _tcslen (s); i++) {
if (s[i] == ' ')
s[i] = '_';
}
}
static void striplength (TCHAR *s, int len)
{
if (!s)
return;
if (_tcslen (s) <= len)
return;
s[len] = 0;
}
static void fixcharset (TCHAR *s)
{
char tmp[MAX_DPATH];
if (!s)
return;
ua_fs_copy (tmp, MAX_DPATH, s, '_');
au_fs_copy (s, strlen (tmp) + 1, tmp);
}
TCHAR *validatevolumename (TCHAR *s, const TCHAR *def)
{
stripsemicolon (s);
fixcharset (s);
striplength (s, 30);
if (_tcslen(s) == 0 && def) {
xfree(s);
s = my_strdup(def);
}
return s;
}
TCHAR *validatedevicename (TCHAR *s, const TCHAR *def)
{
stripsemicolon (s);
stripspace (s);
fixcharset (s);
striplength (s, 30);
if (_tcslen(s) == 0 && def) {
xfree(s);
s = my_strdup(def);
}
return s;
}
TCHAR *filesys_createvolname (const TCHAR *volname, const TCHAR *rootdir, struct zvolume *zv, const TCHAR *def)
{
TCHAR *nvol = NULL;
int i, archivehd;
TCHAR *p = NULL;
TCHAR path[MAX_DPATH];
cfgfile_resolve_path_out_load(rootdir, path, MAX_DPATH, PATH_DIR);
archivehd = -1;
if (my_existsfile (path))
archivehd = 1;
else if (my_existsdir (path))
archivehd = 0;
if (zv && zv->volumename && _tcslen(zv->volumename) > 0) {
nvol = my_strdup(zv->volumename);
nvol = validatevolumename (nvol, def);
return nvol;
}
if ((!volname || _tcslen (volname) == 0) && path && archivehd >= 0) {
p = my_strdup (path);
for (i = _tcslen (p) - 1; i >= 0; i--) {
TCHAR c = p[i];
if (c == ':' || c == '/' || c == '\\') {
if (i == _tcslen (p) - 1)
continue;
if (!_tcscmp (p + i, _T(":\\"))) {
xfree (p);
p = xmalloc (TCHAR, 10);
p[0] = path[0];
p[1] = 0;
i = 0;
} else {
i++;
}
break;
}
}
if (i >= 0)
nvol = my_strdup (p + i);
}
if (!nvol && archivehd >= 0) {
if (volname && _tcslen (volname) > 0)
nvol = my_strdup (volname);
else
nvol = my_strdup (def);
}
if (!nvol) {
if (volname && _tcslen (volname))
nvol = my_strdup (volname);
else
nvol = my_strdup (_T(""));
}
nvol = validatevolumename (nvol, def);
xfree (p);
return nvol;
}
static int set_filesys_volume (const TCHAR *rootdir, int *flags, bool *readonly, bool *emptydrive, struct zvolume **zvp)
{
*emptydrive = 0;
if (my_existsfile (rootdir)) {
struct zvolume *zv;
zv = zfile_fopen_archive (rootdir);
if (!zv) {
error_log (_T("'%s' is not a supported archive file."), rootdir);
return -1;
}
*zvp = zv;
*flags = MYVOLUMEINFO_ARCHIVE;
*readonly = 1;
} else {
*flags = my_getvolumeinfo (rootdir);
if (*flags < 0) {
if (rootdir && rootdir[0])
error_log (_T("directory '%s' not found, mounting as empty drive."), rootdir);
*emptydrive = 1;
*flags = 0;
} else if ((*flags) & MYVOLUMEINFO_READONLY) {
error_log (_T("'%s' set to read-only."), rootdir);
*readonly = 1;
}
}
return 1;
}
void uci_set_defaults (struct uaedev_config_info *uci, bool rdb)
{
memset (uci, 0, sizeof (struct uaedev_config_info));
if (!rdb) {
uci->sectors = 32;
uci->reserved = 2;
uci->surfaces = 1;
}
uci->blocksize = 512;
uci->maxtransfer = 0x7fffffff;
uci->mask = 0xffffffff;
uci->bufmemtype = 1;
uci->buffers = 50;
uci->stacksize = 4000;
uci->bootpri = 0;
uci->priority = 10;
uci->sectorsperblock = 1;
uci->device_emu_unit = -1;
}
static void get_usedblocks(struct fs_usage *fsu, bool fs, int *pblocksize, uae_s64 *pnumblocks, uae_s64 *pinuse, bool reduce)
{
uae_s64 numblocks = 0, inuse;
int blocksize = *pblocksize;
if (fs && currprefs.filesys_limit) {
if (fsu->total > (uae_s64)currprefs.filesys_limit * 1024) {
uae_s64 oldtotal = fsu->total;
fsu->total = currprefs.filesys_limit * 1024;
fsu->avail = ((fsu->avail / 1024) * (fsu->total / 1024)) / (oldtotal / 1024);
fsu->avail *= 1024;
}
}
if (reduce) {
while (blocksize < 32768 || numblocks == 0) {
numblocks = fsu->total / blocksize;
if (numblocks <= 10)
numblocks = 10;
// Value that does not overflow when multiplied by 100 (uses 128 to keep it simple)
if (numblocks < 0x02000000)
break;
blocksize *= 2;
}
}
numblocks = fsu->total / blocksize;
inuse = (numblocks * blocksize - fsu->avail) / blocksize;
if (inuse > numblocks)
inuse = numblocks;
if (pnumblocks)
*pnumblocks = numblocks;
if (pinuse)
*pinuse = inuse;
if (pblocksize)
*pblocksize = blocksize;
}
static bool get_blocks(const TCHAR *rootdir, int unit, int flags, int *pblocksize, uae_s64 *pnumblocks, uae_s64 *pinuse, bool reduce)
{
struct fs_usage fsu;
int ret;
bool fs = false;
int blocksize;
blocksize = 512;
if (flags & MYVOLUMEINFO_ARCHIVE) {
ret = zfile_fs_usage_archive(rootdir, 0, &fsu);
fs = true;
} else {
ret = get_fs_usage(rootdir, 0, &fsu);
fs = true;
}
if (ret)
return false;
get_usedblocks(&fsu, fs, &blocksize, pnumblocks, pinuse, reduce);
if (pblocksize)
*pblocksize = blocksize;
return ret == 0;
}
static int set_filesys_unit_1 (int nr, struct uaedev_config_info *ci, bool custom)
{
UnitInfo *ui;
int i;
bool emptydrive = false;
bool iscd;
struct uaedev_config_info c;
memcpy (&c, ci, sizeof (struct uaedev_config_info));
if (nr < 0) {
for (nr = 0; nr < MAX_FILESYSTEM_UNITS; nr++) {
if (!mountinfo.ui[nr].open)
break;
}
if (nr == MAX_FILESYSTEM_UNITS) {
error_log (_T("No slot allocated for this unit"));
return -1;
}
}
if (ci->controller_type != HD_CONTROLLER_TYPE_UAE || ci->type == UAEDEV_TAPE) {
ui = &mountinfo.ui[nr];
memset (ui, 0, sizeof (UnitInfo));
memcpy (&ui->hf.ci, &c, sizeof (struct uaedev_config_info));
ui->readonly = c.readonly;
ui->unit_type = -1;
ui->open = -1;
return nr;
}
iscd = nr >= cd_unit_offset && nr < cd_unit_offset + cd_unit_number;
if (!custom)
cfgfile_resolve_path_load(c.rootdir, MAX_DPATH, iscd ? PATH_CD : (ci->volname[0] ? PATH_DIR : PATH_HDF));
for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
if (nr == i || !mountinfo.ui[i].open || mountinfo.ui[i].rootdir == NULL || is_hardfile (i) == FILESYS_CD)
continue;
if (_tcslen(c.rootdir) > 0 && samepath(mountinfo.ui[i].rootdir, c.rootdir)) {
error_log (_T("directory/hardfile '%s' already added."), c.rootdir);
return -1;
}
}
for (;;) {
bool retry = false;
for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
int hf = is_hardfile(i);
if (nr == i || !mountinfo.ui[i].open || mountinfo.ui[i].rootdir == NULL)
continue;
if (hf != FILESYS_HARDDRIVE && hf != FILESYS_HARDFILE && hf != FILESYS_HARDFILE_RDB)
continue;
if (mountinfo.ui[i].hf.ci.controller_unit == c.controller_unit) {
c.controller_unit++;
if (c.controller_unit >= MAX_FILESYSTEM_UNITS) {
error_log (_T("directory/hardfile '%s' invalid unit number."), c.rootdir);
return -1;
}
retry = true;
}
}
if (!retry)
break;
}
ui = &mountinfo.ui[nr];
memset (ui, 0, sizeof (UnitInfo));
memcpy(&ui->hf.ci, &c, sizeof c);
if (iscd) {
ui->unit_type = UNIT_CDFS;
emptydrive = 1;
ui->volflags = MYVOLUMEINFO_CDFS | MYVOLUMEINFO_READONLY;
c.readonly = true;
} else if (c.volname[0]) {
int flags = 0;
uae_s64 numblocks;
emptydrive = 1;
if (c.rootdir[0]) {
if (set_filesys_volume (c.rootdir, &flags, &c.readonly, &emptydrive, &ui->zarchive) < 0)
return -1;
}
ui->volname = filesys_createvolname (c.volname, c.rootdir, ui->zarchive, _T("harddrive"));
ui->volflags = flags;
TCHAR *vs = au(UAEFS_VERSION);
TCHAR *vsp = vs + _tcslen(vs) - 1;
while (vsp != vs) {
if (*vsp == ' ') {
*vsp++ = 0;
break;
}
vsp--;
}
_tcscpy(ui->hf.vendor_id, _T("UAE"));
_tcscpy(ui->hf.product_id, vs);
_tcscpy(ui->hf.product_rev, vsp);
xfree(vs);
ui->hf.ci.unit_feature_level = HD_LEVEL_SCSI_2;
if (get_blocks(c.rootdir, nr, flags, &ui->hf.ci.blocksize, &numblocks, NULL, false))
ui->hf.ci.max_lba = numblocks > 0xffffffff ? 0xffffffff : numblocks;
else
ui->hf.ci.max_lba = 0x00ffffff;
} else {
ui->unit_type = UNIT_FILESYSTEM;
ui->hf.unitnum = nr;
ui->volname = 0;
if (ui->hf.ci.rootdir[0]) {
if (hdf_open (&ui->hf) <= 0 && !c.readonly) {
write_log (_T("Attempting to open '%s' in read-only mode.\n"), ui->hf.ci.rootdir);
ui->hf.ci.readonly = c.readonly = true;
if (hdf_open (&ui->hf) > 0) {
error_log (_T("'%s' opened in read-only mode.\n"), ui->hf.ci.rootdir);
}
}
} else {
// empty drive?
ui->hf.drive_empty = 1;
}
if (!ui->hf.drive_empty) {
if (ui->hf.handle_valid == 0) {
error_log (_T("Hardfile '%s' not found."), ui->hf.ci.rootdir);
goto err;
}
if (ui->hf.ci.blocksize > ui->hf.virtsize || ui->hf.virtsize == 0) {
error_log (_T("Hardfile '%s' too small."), ui->hf.ci.rootdir);
goto err;
}
}
if ((ui->hf.ci.blocksize & (ui->hf.ci.blocksize - 1)) != 0 || ui->hf.ci.blocksize == 0) {
error_log (_T("Hardfile '%s' bad blocksize %d."), ui->hf.ci.rootdir, ui->hf.ci.blocksize);
goto err;
}
if ((ui->hf.ci.sectors || ui->hf.ci.surfaces || ui->hf.ci.reserved) &&
(ui->hf.ci.sectors < 1 || ui->hf.ci.surfaces < 1 || ui->hf.ci.surfaces > 1023 ||
ui->hf.ci.reserved < 0 || ui->hf.ci.reserved > 1023) != 0) {
error_log (_T("Hardfile '%s' bad hardfile geometry."), ui->hf.ci.rootdir);
goto err;
}
if (!ui->hf.ci.highcyl) {
ui->hf.ci.cyls = (int)(ui->hf.ci.sectors * ui->hf.ci.surfaces ? (ui->hf.virtsize / ui->hf.ci.blocksize) / (ui->hf.ci.sectors * ui->hf.ci.surfaces) : 0);
}
if (!ui->hf.ci.cyls)
ui->hf.ci.cyls = ui->hf.ci.highcyl;
if (!ui->hf.ci.cyls)
ui->hf.ci.cyls = 1;
}
ui->self = 0;
ui->reset_state = FS_STARTUP;
ui->wasisempty = emptydrive;
ui->canremove = emptydrive && (ci->flags & MYVOLUMEINFO_REUSABLE);
ui->rootdir = my_strdup (c.rootdir);
ui->devname = my_strdup (c.devname);
stripsemicolon(ui->devname);
if (c.filesys[0])
ui->filesysdir = my_strdup (c.filesys);
ui->readonly = c.readonly;
if (c.bootpri < -129)
c.bootpri = -129;
if (c.bootpri > 127)
c.bootpri = 127;
ui->bootpri = c.bootpri;
ui->inject_icons = c.inject_icons;
ui->open = 1;
return nr;
err:
if (ui->hf.handle_valid)
hdf_close (&ui->hf);
return -1;
}
static int set_filesys_unit (int nr, struct uaedev_config_info *ci, bool custom)
{
int ret;
ret = set_filesys_unit_1 (nr, ci, custom);
return ret;
}
static int add_filesys_unit (struct uaedev_config_info *ci, bool custom)
{
int nr;
if (nr_units () >= MAX_FILESYSTEM_UNITS)
return -1;
nr = set_filesys_unit_1 (-1, ci, custom);
#ifdef RETROPLATFORM
if (nr >= 0) {
UnitInfo *ui = &mountinfo.ui[nr];
rp_hd_device_enable (nr, true);
if (ui->unit_type == UNIT_CDFS)
rp_cd_image_change(nr, ci->rootdir);
else
rp_harddrive_image_change(nr, ci->readonly, ci->rootdir);
}
#endif
return nr;
}
int kill_filesys_unitconfig (struct uae_prefs *p, int nr)
{
struct uaedev_config_data *uci;
if (nr < 0)
return 0;
uci = getuci (p->mountconfig, nr);
hardfile_do_disk_change (uci, 0);
if (uci->configoffset >= 0 && uci->ci.controller_type == HD_CONTROLLER_TYPE_UAE) {
filesys_media_change(uci->ci.rootdir, 0, uci);
} else {
pcmcia_disk_reinsert(p, &uci->ci, true);
}
while (nr < MOUNT_CONFIG_SIZE) {
memmove (&p->mountconfig[nr], &p->mountconfig[nr + 1], sizeof (struct uaedev_config_data));
nr++;
}
p->mountitems--;
memset (&p->mountconfig[MOUNT_CONFIG_SIZE - 1], 0, sizeof (struct uaedev_config_data));
return 1;
}
int move_filesys_unitconfig (struct uae_prefs *p, int nr, int to)
{
struct uaedev_config_data *uci1, *uci2, tmpuci;
uci1 = getuci (p->mountconfig, nr);
uci2 = getuci (p->mountconfig, to);
if (nr == to)
return 0;
memcpy (&tmpuci, uci1, sizeof (struct uaedev_config_data));
memcpy (uci1, uci2, sizeof (struct uaedev_config_data));
memcpy (uci2, &tmpuci, sizeof (struct uaedev_config_data));
return 1;
}
static void allocuci (struct uae_prefs *p, int nr, int idx, int unitnum)
{
struct uaedev_config_data *uci = &p->mountconfig[nr];
if (idx >= 0) {
UnitInfo *ui;
uci->configoffset = idx;
ui = &mountinfo.ui[idx];
ui->configureddrive = 1;
uci->unitnum = unitnum;
} else {
uci->configoffset = -1;
uci->unitnum = -1;
}
}
static void allocuci (struct uae_prefs *p, int nr, int idx)
{
allocuci (p, nr, idx, -1);
}
static const TCHAR *getunittype(struct uaedev_config_info *uci)
{
return uci->type == UAEDEV_CD ? _T("CD") : (uci->type == UAEDEV_TAPE ? _T("TAPE") : _T("HD"));
}
static int cpuboard_hd;
static romconfig cpuboard_dummy;
void add_cpuboard_unit(int unit, struct uaedev_config_info *uci, struct romconfig *rc)
{
int flags = (uci->controller_type >= HD_CONTROLLER_TYPE_IDE_FIRST && uci->controller_type <= HD_CONTROLLER_TYPE_IDE_LAST) ? EXPANSIONTYPE_IDE : EXPANSIONTYPE_SCSI;
const struct cpuboardtype *cbt = &cpuboards[currprefs.cpuboard_type];
cpuboard_hd = 0;
if (cbt->subtypes) {
if (cbt->subtypes[currprefs.cpuboard_subtype].add && (cbt->subtypes[currprefs.cpuboard_subtype].deviceflags & flags)) {
if (unit >= 0) {
write_log(_T("Adding CPUBoard '%s' %s unit %d ('%s')\n"),
cbt->subtypes[currprefs.cpuboard_subtype].name,
getunittype(uci), unit, uci->rootdir);
}
cbt->subtypes[currprefs.cpuboard_subtype].add(unit, uci, rc);
cpuboard_hd = 1;
}
}
}
static void add_cpuboard_unit_init(void)
{
memset(&cpuboard_dummy, 0, sizeof cpuboard_dummy);
cpuboard_dummy.device_id = 7;
if (currprefs.cpuboard_type) {
struct romconfig *rc = get_device_romconfig(&currprefs, ROMTYPE_CPUBOARD, 0);
if (!rc)
rc = &cpuboard_dummy;
const struct cpuboardtype *cbt = &cpuboards[currprefs.cpuboard_type];
if (cbt->subtypes) {
if (cbt->subtypes[currprefs.cpuboard_subtype].add) {
const struct cpuboardsubtype *cst = &cbt->subtypes[currprefs.cpuboard_subtype];
struct uaedev_config_info ci = { 0 };
write_log(_T("Initializing CPUBoard '%s' %s controller\n"),
cst->name, (cst->deviceflags & EXPANSIONTYPE_SCSI) ? _T("SCSI") : _T("IDE"));
cst->add(-1, &ci, rc);
}
}
}
}
static bool add_ide_unit(int type, int unit, struct uaedev_config_info *uci)
{
bool added = false;
if (type >= HD_CONTROLLER_TYPE_IDE_EXPANSION_FIRST && type <= HD_CONTROLLER_TYPE_IDE_LAST) {
for (int i = 0; expansionroms[i].name; i++) {
if (i == type - HD_CONTROLLER_TYPE_IDE_EXPANSION_FIRST) {
const struct expansionromtype *ert = &expansionroms[i];
if ((ert->deviceflags & 2) && is_board_enabled(&currprefs, ert->romtype, uci->controller_type_unit)) {
cpuboard_hd = 1;
if (ert->add) {
struct romconfig *rc = get_device_romconfig(&currprefs, ert->romtype, uci->controller_type_unit);
write_log(_T("Adding IDE %s '%s' unit %d ('%s')\n"), getunittype(uci),
ert->name, unit, uci->rootdir);
ert->add(unit, uci, rc);
}
if (cpuboard_hd)
added = true;
}
}
}
}
return added;
}
static bool add_scsi_unit(int type, int unit, struct uaedev_config_info *uci)
{
bool added = false;