forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardfile.cpp
2462 lines (2288 loc) · 62.6 KB
/
hardfile.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
*
* Hardfile emulation
*
* Copyright 1995 Bernd Schmidt
* 2002 Toni Wilen (scsi emulation, 64-bit support)
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "threaddep/thread.h"
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "disk.h"
#include "autoconf.h"
#include "traps.h"
#include "filesys.h"
#include "execlib.h"
#include "native2amiga.h"
#include "gui.h"
#include "uae.h"
#include "scsi.h"
#include "gayle.h"
#include "execio.h"
#include "zfile.h"
#include "ide.h"
#include "debug.h"
#ifdef WITH_CHD
#include "archivers/chd/chdtypes.h"
#include "archivers/chd/chd.h"
#include "archivers/chd/harddisk.h"
#endif
//#undef DEBUGME
#define hf_log(fmt, ...)
#define hf_log2(fmt, ...)
#define scsi_log(fmt, ...)
#define hf_log3(fmt, ...)
//#define DEBUGME
#ifdef DEBUGME
#undef hf_log
#define hf_log write_log
#undef hf_log2
#define hf_log2 write_log
#undef hf_log3
#define hf_log3 write_log
#undef scsi_log
#define scsi_log write_log
#endif
extern int log_scsiemu;
#define MAX_ASYNC_REQUESTS 50
#define ASYNC_REQUEST_NONE 0
#define ASYNC_REQUEST_TEMP 1
#define ASYNC_REQUEST_CHANGEINT 10
struct hardfileprivdata {
volatile uaecptr d_request[MAX_ASYNC_REQUESTS];
volatile int d_request_type[MAX_ASYNC_REQUESTS];
volatile uae_u32 d_request_data[MAX_ASYNC_REQUESTS];
smp_comm_pipe requests;
int thread_running;
uae_sem_t sync_sem;
uaecptr base;
int changenum;
uaecptr changeint;
};
#define HFD_CHD_OTHER 5
#define HFD_CHD_HD 4
#define HFD_VHD_DYNAMIC 3
#define HFD_VHD_FIXED 2
STATIC_INLINE uae_u32 gl (uae_u8 *p)
{
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
}
static uae_sem_t change_sem;
static struct hardfileprivdata hardfpd[MAX_FILESYSTEM_UNITS];
static uae_u32 nscmd_cmd;
static void wl (uae_u8 *p, int v)
{
p[0] = v >> 24;
p[1] = v >> 16;
p[2] = v >> 8;
p[3] = v;
}
static void ww (uae_u8 *p, int v)
{
p[0] = v >> 8;
p[1] = v;
}
static int rl (uae_u8 *p)
{
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]);
}
static void getchs2 (struct hardfiledata *hfd, int *cyl, int *cylsec, int *head, int *tracksec)
{
unsigned int total = (unsigned int)(hfd->virtsize / 1024);
int heads;
int sectors = 63;
/* do we have RDB values? */
if (hfd->rdbcylinders) {
*cyl = hfd->rdbcylinders;
*tracksec = hfd->rdbsectors;
*head = hfd->rdbheads;
*cylsec = hfd->rdbsectors * hfd->rdbheads;
return;
}
/* what about HDF settings? */
if (hfd->ci.surfaces && hfd->ci.sectors) {
*head = hfd->ci.surfaces;
*tracksec = hfd->ci.sectors;
*cylsec = (*head) * (*tracksec);
*cyl = (unsigned int)(hfd->virtsize / hfd->ci.blocksize) / ((*tracksec) * (*head));
return;
}
/* no, lets guess something.. */
if (total <= 504 * 1024)
heads = 16;
else if (total <= 1008 * 1024)
heads = 32;
else if (total <= 2016 * 1024)
heads = 64;
else if (total <= 4032 * 1024)
heads = 128;
else
heads = 255;
*cyl = (unsigned int)(hfd->virtsize / hfd->ci.blocksize) / (sectors * heads);
*cylsec = sectors * heads;
*tracksec = sectors;
*head = heads;
}
static void getchsx (struct hardfiledata *hfd, int *cyl, int *cylsec, int *head, int *tracksec)
{
getchs2 (hfd, cyl, cylsec, head, tracksec);
hf_log (_T("CHS: %08X-%08X %d %d %d %d %d\n"),
(uae_u32)(hfd->virtsize >> 32),(uae_u32)hfd->virtsize,
*cyl, *cylsec, *head, *tracksec);
}
static void getchsgeometry2 (uae_u64 size, int *pcyl, int *phead, int *psectorspertrack, int mode)
{
int sptt[4];
int i, spt, head, cyl;
uae_u64 total = (unsigned int)(size / 512);
if (mode == 1) {
// old-style head=1, spt=32 always mode
head = 1;
spt = 32;
cyl = total / (head * spt);
} else {
sptt[0] = 63;
sptt[1] = 127;
sptt[2] = 255;
sptt[3] = -1;
for (i = 0; sptt[i] >= 0; i++) {
int maxhead = sptt[i] < 255 ? 16 : 255;
spt = sptt[i];
for (head = 4; head <= maxhead; head++) {
cyl = total / (head * spt);
if (size <= 512 * 1024 * 1024) {
if (cyl <= 1023)
break;
} else {
if (cyl < 16383)
break;
if (cyl < 32767 && head >= 5)
break;
if (cyl <= 65535)
break;
}
if (maxhead > 16) {
head *= 2;
head--;
}
}
if (head <= 16)
break;
}
}
if (head > 16)
head--;
*pcyl = cyl;
*phead = head;
*psectorspertrack = spt;
}
void getchsgeometry (uae_u64 size, int *pcyl, int *phead, int *psectorspertrack)
{
getchsgeometry2 (size, pcyl, phead, psectorspertrack, 0);
}
void getchsgeometry_hdf (struct hardfiledata *hfd, uae_u64 size, int *pcyl, int *phead, int *psectorspertrack)
{
uae_u8 block[512];
int i;
uae_u64 minsize = 512 * 1024 * 1024;
if (size <= minsize) {
*phead = 1;
*psectorspertrack = 32;
}
memset (block, 0, sizeof block);
if (hfd) {
hdf_read (hfd, block, 0, 512);
if (block[0] == 'D' && block[1] == 'O' && block[2] == 'S') {
int mode;
for (mode = 0; mode < 2; mode++) {
uae_u32 rootblock;
uae_u32 chk = 0;
getchsgeometry2 (size, pcyl, phead, psectorspertrack, mode);
rootblock = (2 + ((*pcyl) * (*phead) * (*psectorspertrack) - 1)) / 2;
memset (block, 0, sizeof block);
hdf_read (hfd, block, (uae_u64)rootblock * 512, 512);
for (i = 0; i < 512; i += 4)
chk += (block[i] << 24) | (block[i + 1] << 16) | (block[i + 2] << 8) | (block[i + 3] << 0);
if (!chk && block[0] == 0 && block[1] == 0 && block[2] == 0 && block[3] == 2 &&
block[4] == 0 && block[5] == 0 && block[6] == 0 && block[7] == 0 &&
block[8] == 0 && block[9] == 0 && block[10] == 0 && block[11] == 0 &&
block[508] == 0 && block[509] == 0 && block[510] == 0 && block[511] == 1) {
return;
}
}
}
}
getchsgeometry2 (size, pcyl, phead, psectorspertrack, size <= minsize ? 1 : 2);
}
void getchspgeometry (uae_u64 total, int *pcyl, int *phead, int *psectorspertrack, bool idegeometry)
{
uae_u64 blocks = total / 512;
if (blocks > 16515072) {
/* >8G, CHS=16383/16/63 */
*pcyl = 16383;
*phead = 16;
*psectorspertrack = 63;
return;
}
if (idegeometry) {
*phead = 16;
*psectorspertrack = 63;
*pcyl = blocks / ((*psectorspertrack) * (*phead));
return;
}
getchsgeometry (total, pcyl, phead, psectorspertrack);
}
static void getchshd (struct hardfiledata *hfd, int *pcyl, int *phead, int *psectorspertrack)
{
getchspgeometry (hfd->virtsize, pcyl, phead, psectorspertrack, false);
}
static void pl (uae_u8 *p, int off, uae_u32 v)
{
p += off * 4;
p[0] = v >> 24;
p[1] = v >> 16;
p[2] = v >> 8;
p[3] = v >> 0;
}
static void rdb_crc (uae_u8 *p)
{
uae_u32 sum;
int i, blocksize;
sum =0;
blocksize = rl (p + 1 * 4);
for (i = 0; i < blocksize; i++)
sum += rl (p + i * 4);
sum = -sum;
pl (p, 2, sum);
}
static void create_virtual_rdb (struct hardfiledata *hfd)
{
uae_u8 *rdb, *part, *denv;
int cyl = hfd->ci.surfaces * hfd->ci.sectors;
int cyls = 262144 / (cyl * 512);
int size = cyl * cyls * 512;
rdb = xcalloc (uae_u8, size);
hfd->virtual_rdb = rdb;
hfd->virtual_size = size;
part = rdb + 512;
pl(rdb, 0, 0x5244534b);
pl(rdb, 1, 64);
pl(rdb, 2, 0); // chksum
pl(rdb, 3, 7); // hostid
pl(rdb, 4, 512); // blockbytes
pl(rdb, 5, 0); // flags
pl(rdb, 6, -1); // badblock
pl(rdb, 7, 1); // part
pl(rdb, 8, -1); // fs
pl(rdb, 9, -1); // driveinit
pl(rdb, 10, -1); // reserved
pl(rdb, 11, -1); // reserved
pl(rdb, 12, -1); // reserved
pl(rdb, 13, -1); // reserved
pl(rdb, 14, -1); // reserved
pl(rdb, 15, -1); // reserved
pl(rdb, 16, hfd->ci.highcyl);
pl(rdb, 17, hfd->ci.sectors);
pl(rdb, 18, hfd->ci.surfaces);
pl(rdb, 19, hfd->ci.interleave); // interleave
pl(rdb, 20, 0); // park
pl(rdb, 21, -1); // res
pl(rdb, 22, -1); // res
pl(rdb, 23, -1); // res
pl(rdb, 24, 0); // writeprecomp
pl(rdb, 25, 0); // reducedwrite
pl(rdb, 26, 0); // steprate
pl(rdb, 27, -1); // res
pl(rdb, 28, -1); // res
pl(rdb, 29, -1); // res
pl(rdb, 30, -1); // res
pl(rdb, 31, -1); // res
pl(rdb, 32, 0); // rdbblockslo
pl(rdb, 33, cyl * cyls); // rdbblockshi
pl(rdb, 34, cyls); // locyl
pl(rdb, 35, hfd->ci.highcyl + cyls); // hicyl
pl(rdb, 36, cyl); // cylblocks
pl(rdb, 37, 0); // autopark
pl(rdb, 38, 2); // highrdskblock
pl(rdb, 39, -1); // res
ua_copy ((char*)rdb + 40 * 4, 8, hfd->vendor_id);
ua_copy ((char*)rdb + 42 * 4, 16, hfd->product_id);
ua_copy ((char*)rdb + 46 * 4, 4, _T("UAE"));
rdb_crc (rdb);
pl(part, 0, 0x50415254);
pl(part, 1, 64);
pl(part, 2, 0);
pl(part, 3, 0);
pl(part, 4, -1);
pl(part, 5, 1); // bootable
pl(part, 6, -1);
pl(part, 7, -1);
pl(part, 8, 0); // devflags
part[9 * 4] = _tcslen (hfd->ci.devname);
ua_copy ((char*)part + 9 * 4 + 1, 30, hfd->ci.devname);
denv = part + 128;
pl(denv, 0, 80);
pl(denv, 1, 512 / 4);
pl(denv, 2, 0); // secorg
pl(denv, 3, hfd->ci.surfaces);
pl(denv, 4, hfd->ci.blocksize / 512);
pl(denv, 5, hfd->ci.sectors);
pl(denv, 6, hfd->ci.reserved);
pl(denv, 7, 0); // prealloc
pl(denv, 8, hfd->ci.interleave); // interleave
pl(denv, 9, cyls); // lowcyl
pl(denv, 10, hfd->ci.highcyl + cyls - 1);
pl(denv, 11, hfd->ci.buffers);
pl(denv, 12, hfd->ci.bufmemtype);
pl(denv, 13, hfd->ci.maxtransfer);
pl(denv, 14, hfd->ci.mask);
pl(denv, 15, hfd->ci.bootpri);
pl(denv, 16, hfd->ci.dostype);
rdb_crc (part);
hfd->virtsize += size;
}
void hdf_hd_close (struct hd_hardfiledata *hfd)
{
if (!hfd)
return;
hdf_close (&hfd->hfd);
}
int hdf_hd_open (struct hd_hardfiledata *hfd)
{
struct uaedev_config_info *ci = &hfd->hfd.ci;
if (hdf_open (&hfd->hfd) <= 0)
return 0;
if (ci->physical_geometry) {
hfd->cyls = ci->pcyls;
hfd->heads = ci->pheads;
hfd->secspertrack = ci->psecs;
} else if (ci->highcyl && ci->surfaces && ci->sectors) {
hfd->cyls = ci->highcyl;
hfd->heads = ci->surfaces;
hfd->secspertrack = ci->sectors;
} else {
getchshd (&hfd->hfd, &hfd->cyls, &hfd->heads, &hfd->secspertrack);
}
hfd->cyls_def = hfd->cyls;
hfd->secspertrack_def = hfd->secspertrack;
hfd->heads_def = hfd->heads;
if (ci->surfaces && ci->sectors) {
uae_u8 buf[512] = { 0 };
hdf_read (&hfd->hfd, buf, 0, 512);
if (buf[0] != 0 && memcmp (buf, _T("RDSK"), 4)) {
ci->highcyl = (hfd->hfd.virtsize / ci->blocksize) / (ci->sectors * ci->surfaces);
ci->dostype = rl (buf);
create_virtual_rdb (&hfd->hfd);
while (ci->highcyl * ci->surfaces * ci->sectors > hfd->cyls_def * hfd->secspertrack_def * hfd->heads_def) {
hfd->cyls_def++;
}
}
}
hfd->size = hfd->hfd.virtsize;
return 1;
}
static uae_u32 vhd_checksum (uae_u8 *p, int offset)
{
int i;
uae_u32 sum;
sum = 0;
for (i = 0; i < 512; i++) {
if (offset >= 0 && i >= offset && i < offset + 4)
continue;
sum += p[i];
}
return ~sum;
}
static int hdf_write2 (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len);
static int hdf_read2 (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len);
static void hdf_init_cache (struct hardfiledata *hfd)
{
}
static void hdf_flush_cache (struct hardfiledata *hdf)
{
}
static int hdf_cache_read (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
return hdf_read2 (hfd, buffer, offset, len);
}
static int hdf_cache_write (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
return hdf_write2 (hfd, buffer, offset, len);
}
int hdf_open (struct hardfiledata *hfd, const TCHAR *pname)
{
int ret;
uae_u8 tmp[512], tmp2[512];
uae_u32 v;
if ((!pname || pname[0] == 0) && hfd->ci.rootdir[0] == 0)
return 0;
hfd->adide = 0;
hfd->byteswap = 0;
hfd->hfd_type = 0;
if (!pname)
pname = hfd->ci.rootdir;
#ifdef WITH_CHD
TCHAR nametmp[MAX_DPATH];
_tcscpy (nametmp, pname);
TCHAR *ext = _tcsrchr (nametmp, '.');
if (ext && !_tcsicmp (ext, _T(".chd"))) {
bool chd_readonly = false;
struct zfile *zf = NULL;
if (!hfd->ci.readonly)
zf = zfile_fopen (nametmp, _T("rb+"));
if (!zf) {
chd_readonly = true;
zf = zfile_fopen (nametmp, _T("rb"));
}
if (zf) {
int err = CHDERR_FILE_NOT_WRITEABLE;
hard_disk_file *chdf;
chd_file *cf = new chd_file();
if (!chd_readonly)
err = cf->open(*zf, true, NULL);
if (err == CHDERR_FILE_NOT_WRITEABLE) {
chd_readonly = true;
err = cf->open(*zf, false, NULL);
}
if (err != CHDERR_NONE) {
zfile_fclose (zf);
delete cf;
goto end;
}
chdf = hard_disk_open(cf);
if (!chdf) {
hfd->ci.readonly = true;
hfd->hfd_type = HFD_CHD_OTHER;
hfd->chd_handle = cf;
} else {
hfd->hfd_type = HFD_CHD_HD;
hfd->chd_handle = chdf;
}
if (chd_readonly)
hfd->ci.readonly = true;
hfd->virtsize = cf->logical_bytes();
hfd->handle_valid = -1;
write_log(_T("CHD '%s' mounted as %s, %s.\n"), pname, chdf ? _T("HD") : _T("OTHER"), hfd->ci.readonly ? _T("read only") : _T("read/write"));
return 1;
}
}
#endif
ret = hdf_open_target (hfd, pname);
if (ret <= 0)
return ret;
if (hdf_read_target (hfd, tmp, 0, 512) != 512)
goto nonvhd;
v = gl (tmp + 8); // features
if ((v & 3) != 2)
goto nonvhd;
v = gl (tmp + 8 + 4); // version
if ((v >> 16) != 1)
goto nonvhd;
hfd->hfd_type = gl (tmp + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 4 + 8 + 8 + 4);
if (hfd->hfd_type != HFD_VHD_FIXED && hfd->hfd_type != HFD_VHD_DYNAMIC)
goto nonvhd;
v = gl (tmp + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 4);
if (v == 0)
goto nonvhd;
if (vhd_checksum (tmp, 8 + 4 + 4 + 8 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 4) != v)
goto nonvhd;
if (hdf_read_target (hfd, tmp2, hfd->physsize - sizeof tmp2, 512) != 512)
goto end;
if (memcmp (tmp, tmp2, sizeof tmp))
goto nonvhd;
hfd->vhd_footerblock = hfd->physsize - 512;
hfd->virtsize = (uae_u64)(gl (tmp + 8 + 4 + 4 + 8 + 4 + 4 +4 + 4 + 8)) << 32;
hfd->virtsize |= gl (tmp + 8 + 4 + 4 + 8 + 4 + 4 +4 + 4 + 8 + 4);
if (hfd->hfd_type == HFD_VHD_DYNAMIC) {
uae_u32 size;
hfd->vhd_bamoffset = gl (tmp + 8 + 4 + 4 + 4);
if (hfd->vhd_bamoffset == 0 || hfd->vhd_bamoffset >= hfd->physsize)
goto end;
if (hdf_read_target (hfd, tmp, hfd->vhd_bamoffset, 512) != 512)
goto end;
v = gl (tmp + 8 + 8 + 8 + 4 + 4 + 4);
if (vhd_checksum (tmp, 8 + 8 + 8 + 4 + 4 + 4) != v)
goto end;
v = gl (tmp + 8 + 8 + 8);
if ((v >> 16) != 1)
goto end;
hfd->vhd_blocksize = gl (tmp + 8 + 8 + 8 + 4 + 4);
hfd->vhd_bamoffset = gl (tmp + 8 + 8 + 4);
hfd->vhd_bamsize = (((hfd->virtsize + hfd->vhd_blocksize - 1) / hfd->vhd_blocksize) * 4 + 511) & ~511;
size = hfd->vhd_bamoffset + hfd->vhd_bamsize;
hfd->vhd_header = xmalloc (uae_u8, size);
if (hdf_read_target (hfd, hfd->vhd_header, 0, size) != size)
goto end;
hfd->vhd_sectormap = xmalloc (uae_u8, 512);
hfd->vhd_sectormapblock = -1;
hfd->vhd_bitmapsize = ((hfd->vhd_blocksize / (8 * 512)) + 511) & ~511;
}
write_log (_T("HDF is VHD %s image, virtual size=%lldK (%llx %lld)\n"),
hfd->hfd_type == HFD_VHD_FIXED ? _T("fixed") : _T("dynamic"),
hfd->virtsize / 1024, hfd->virtsize, hfd->virtsize);
hdf_init_cache (hfd);
return 1;
nonvhd:
hfd->hfd_type = 0;
return 1;
end:
hdf_close_target (hfd);
return 0;
}
int hdf_open (struct hardfiledata *hfd)
{
return hdf_open (hfd, NULL);
}
void hdf_close (struct hardfiledata *hfd)
{
hdf_flush_cache (hfd);
hdf_close_target (hfd);
#ifdef WITH_CHD
if (hfd->hfd_type == HFD_CHD_OTHER) {
chd_file *cf = (chd_file*)hfd->chd_handle;
cf->close();
delete cf;
} else if (hfd->hfd_type == HFD_CHD_HD) {
hard_disk_file *chdf = (hard_disk_file*)hfd->chd_handle;
chd_file *cf = hard_disk_get_chd(chdf);
hard_disk_close(chdf);
cf->close();
delete cf;
}
hfd->chd_handle = NULL;
#endif
hfd->hfd_type = 0;
xfree (hfd->vhd_header);
hfd->vhd_header = NULL;
xfree (hfd->vhd_sectormap);
hfd->vhd_sectormap = NULL;
}
int hdf_dup (struct hardfiledata *dhfd, const struct hardfiledata *shfd)
{
return hdf_dup_target (dhfd, shfd);
}
static uae_u64 vhd_read (struct hardfiledata *hfd, void *v, uae_u64 offset, uae_u64 len)
{
uae_u64 read;
uae_u8 *dataptr = (uae_u8*)v;
//write_log (_T("%08x %08x\n"), (uae_u32)offset, (uae_u32)len);
read = 0;
if (offset & 511)
return read;
if (len & 511)
return read;
while (len > 0) {
uae_u32 bamoffset = (offset / hfd->vhd_blocksize) * 4 + hfd->vhd_bamoffset;
uae_u32 sectoroffset = gl (hfd->vhd_header + bamoffset);
if (sectoroffset == 0xffffffff) {
memset (dataptr, 0, 512);
read += 512;
} else {
int bitmapoffsetbits;
int bitmapoffsetbytes;
uae_u64 sectormapblock;
bitmapoffsetbits = (offset / 512) % (hfd->vhd_blocksize / 512);
bitmapoffsetbytes = bitmapoffsetbits / 8;
sectormapblock = sectoroffset * (uae_u64)512 + (bitmapoffsetbytes & ~511);
if (hfd->vhd_sectormapblock != sectormapblock) {
// read sector bitmap
//write_log (_T("BM %08x\n"), sectormapblock);
if (hdf_read_target (hfd, hfd->vhd_sectormap, sectormapblock, 512) != 512) {
write_log (_T("vhd_read: bitmap read error\n"));
return read;
}
hfd->vhd_sectormapblock = sectormapblock;
}
// block allocated in bitmap?
if (hfd->vhd_sectormap[bitmapoffsetbytes & 511] & (1 << (7 - (bitmapoffsetbits & 7)))) {
// read data block
uae_u64 block = sectoroffset * (uae_u64)512 + hfd->vhd_bitmapsize + bitmapoffsetbits * 512;
//write_log (_T("DB %08x\n"), block);
if (hdf_read_target (hfd, dataptr, block, 512) != 512) {
write_log (_T("vhd_read: data read error\n"));
return read;
}
} else {
memset (dataptr, 0, 512);
}
read += 512;
}
len -= 512;
dataptr += 512;
offset += 512;
}
return read;
}
static int vhd_write_enlarge (struct hardfiledata *hfd, uae_u32 bamoffset)
{
uae_u8 *buf, *p;
int len;
uae_u32 block;
int v;
len = hfd->vhd_blocksize + hfd->vhd_bitmapsize + 512;
buf = xcalloc (uae_u8, len);
if (!hdf_resize_target (hfd, hfd->physsize + len - 512)) {
write_log (_T("vhd_enlarge: failure\n"));
return 0;
}
// add footer (same as 512 byte header)
memcpy (buf + len - 512, hfd->vhd_header, 512);
v = hdf_write_target (hfd, buf, hfd->vhd_footerblock, len);
xfree (buf);
if (v != len) {
write_log (_T("vhd_enlarge: footer write error\n"));
return 0;
}
// write new offset to BAM
p = hfd->vhd_header + bamoffset;
block = hfd->vhd_footerblock / 512;
p[0] = block >> 24;
p[1] = block >> 16;
p[2] = block >> 8;
p[3] = block >> 0;
// write to disk
if (hdf_write_target (hfd, hfd->vhd_header + hfd->vhd_bamoffset, hfd->vhd_bamoffset, hfd->vhd_bamsize) != hfd->vhd_bamsize) {
write_log (_T("vhd_enlarge: bam write error\n"));
return 0;
}
hfd->vhd_footerblock += len - 512;
return 1;
}
static uae_u64 vhd_write (struct hardfiledata *hfd, void *v, uae_u64 offset, uae_u64 len)
{
uae_u64 written;
uae_u8 *dataptr = (uae_u8*)v;
//write_log (_T("%08x %08x\n"), (uae_u32)offset, (uae_u32)len);
written = 0;
if (offset & 511)
return written;
if (len & 511)
return written;
while (len > 0) {
uae_u32 bamoffset = (offset / hfd->vhd_blocksize) * 4 + hfd->vhd_bamoffset;
uae_u32 sectoroffset = gl (hfd->vhd_header + bamoffset);
if (sectoroffset == 0xffffffff) {
if (!vhd_write_enlarge (hfd, bamoffset))
return written;
continue;
} else {
int bitmapoffsetbits;
int bitmapoffsetbytes;
bitmapoffsetbits = (offset / 512) % (hfd->vhd_blocksize / 512);
bitmapoffsetbytes = bitmapoffsetbits / 8;
uae_u64 sectormapblock = sectoroffset * (uae_u64)512 + (bitmapoffsetbytes & ~511);
if (hfd->vhd_sectormapblock != sectormapblock) {
// read sector bitmap
if (hdf_read_target (hfd, hfd->vhd_sectormap, sectormapblock, 512) != 512) {
write_log (_T("vhd_write: bitmap read error\n"));
return written;
}
hfd->vhd_sectormapblock = sectormapblock;
}
// write data
if (hdf_write_target (hfd, dataptr, sectoroffset * (uae_u64)512 + hfd->vhd_bitmapsize + bitmapoffsetbits * 512, 512) != 512) {
write_log (_T("vhd_write: data write error\n"));
return written;
}
// block already allocated in bitmap?
if (!(hfd->vhd_sectormap[bitmapoffsetbytes & 511] & (1 << (7 - (bitmapoffsetbits & 7))))) {
// no, we need to mark it allocated and write the modified bitmap back to the disk
hfd->vhd_sectormap[bitmapoffsetbytes & 511] |= (1 << (7 - (bitmapoffsetbits & 7)));
if (hdf_write_target (hfd, hfd->vhd_sectormap, sectormapblock, 512) != 512) {
write_log (_T("vhd_write: bam write error\n"));
return written;
}
}
written += 512;
}
len -= 512;
dataptr += 512;
offset += 512;
}
return written;
}
int vhd_create (const TCHAR *name, uae_u64 size, uae_u32 dostype)
{
struct hardfiledata hfd;
struct zfile *zf;
uae_u8 *b;
int cyl, cylsec, head, tracksec;
uae_u32 crc, blocksize, batsize, batentrysize;
int ret, i;
time_t tm;
if (size >= (uae_u64)10 * 1024 * 1024 * 1024)
blocksize = 2 * 1024 * 1024;
else
blocksize = 512 * 1024;
batsize = (size + blocksize - 1) / blocksize;
batentrysize = batsize;
batsize *= 4;
batsize += 511;
batsize &= ~511;
ret = 0;
b = NULL;
zf = zfile_fopen (name, _T("wb"), 0);
if (!zf)
goto end;
b = xcalloc (uae_u8, 512 + 1024 + batsize + 512);
if (zfile_fwrite (b, 512 + 1024 + batsize + 512, 1, zf) != 1)
goto end;
memset (&hfd, 0, sizeof hfd);
hfd.virtsize = hfd.physsize = size;
hfd.ci.blocksize = 512;
strcpy ((char*)b, "conectix"); // cookie
b[0x0b] = 2; // features
b[0x0d] = 1; // version
b[0x10 + 6] = 2; // data offset
// time stamp
tm = time (NULL) - 946684800;
b[0x18] = tm >> 24;
b[0x19] = tm >> 16;
b[0x1a] = tm >> 8;
b[0x1b] = tm >> 0;
strcpy ((char*)b + 0x1c, "vpc "); // creator application
b[0x21] = 5; // creator version
strcpy ((char*)b + 0x24, "Wi2k"); // creator host os
// original and current size
b[0x28] = b[0x30] = size >> 56;
b[0x29] = b[0x31] = size >> 48;
b[0x2a] = b[0x32] = size >> 40;
b[0x2b] = b[0x33] = size >> 32;
b[0x2c] = b[0x34] = size >> 24;
b[0x2d] = b[0x35] = size >> 16;
b[0x2e] = b[0x36] = size >> 8;
b[0x2f] = b[0x37] = size >> 0;
getchs2 (&hfd, &cyl, &cylsec, &head, &tracksec);
// cylinders
b[0x38] = cyl >> 8;
b[0x39] = cyl;
// heads
b[0x3a] = head;
// sectors per track
b[0x3b] = tracksec;
// disk type
b[0x3c + 3] = HFD_VHD_DYNAMIC;
get_guid_target (b + 0x44);
crc = vhd_checksum (b, -1);
b[0x40] = crc >> 24;
b[0x41] = crc >> 16;
b[0x42] = crc >> 8;
b[0x43] = crc >> 0;
// write header
zfile_fseek (zf, 0, SEEK_SET);
zfile_fwrite (b, 512, 1, zf);
// write footer
zfile_fseek (zf, 512 + 1024 + batsize, SEEK_SET);
zfile_fwrite (b, 512, 1, zf);
// dynamic disk header
memset (b, 0, 1024);
// cookie
strcpy ((char*)b, "cxsparse");
// data offset
for (i = 0; i < 8; i++)
b[0x08 + i] = 0xff;
// table offset (bat)
b[0x10 + 6] = 0x06;
// version
b[0x19] = 1;
// max table entries
b[0x1c] = batentrysize >> 24;
b[0x1d] = batentrysize >> 16;
b[0x1e] = batentrysize >> 8;
b[0x1f] = batentrysize >> 0;
b[0x20] = blocksize >> 24;
b[0x21] = blocksize >> 16;
b[0x22] = blocksize >> 8;
b[0x23] = blocksize >> 0;
crc = vhd_checksum (b, -1);
b[0x24] = crc >> 24;
b[0x25] = crc >> 16;
b[0x26] = crc >> 8;
b[0x27] = crc >> 0;
// write dynamic header
zfile_fseek (zf, 512, SEEK_SET);
zfile_fwrite (b, 1024, 1, zf);
// bat
memset (b, 0, batsize);
memset (b, 0xff, batentrysize * 4);
zfile_fwrite (b, batsize, 1, zf);
zfile_fclose (zf);
zf = NULL;
if (dostype) {
uae_u8 bootblock[512] = { 0 };
bootblock[0] = dostype >> 24;
bootblock[1] = dostype >> 16;
bootblock[2] = dostype >> 8;
bootblock[3] = dostype >> 0;
if (hdf_open (&hfd, name) > 0) {
vhd_write (&hfd, bootblock, 0, 512);
hdf_close (&hfd);
}
}
ret = 1;
end:
xfree (b);
zfile_fclose (zf);
return ret;
}
static int hdf_read2 (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
if (hfd->hfd_type == HFD_VHD_DYNAMIC)
return vhd_read (hfd, buffer, offset, len);
else if (hfd->hfd_type == HFD_VHD_FIXED)
return hdf_read_target (hfd, buffer, offset + 512, len);
#ifdef WITH_CHD
else if (hfd->hfd_type == HFD_CHD_OTHER) {
chd_file *cf = (chd_file*)hfd->chd_handle;
if (cf->read_bytes(offset, buffer, len) == CHDERR_NONE)
return len;
return 0;
} else if (hfd->hfd_type == HFD_CHD_HD) {
hard_disk_file *chdf = (hard_disk_file*)hfd->chd_handle;
hard_disk_info *chdi = hard_disk_get_info(chdf);
chd_file *cf = hard_disk_get_chd(chdf);
uae_u8 *buf = (uae_u8*)buffer;
int got = 0;
offset /= chdi->sectorbytes;
while (len > 0) {
if (cf->read_units(offset, buf) != CHDERR_NONE)
return got;
got += chdi->sectorbytes;
buf += chdi->sectorbytes;
len -= chdi->sectorbytes;
offset++;
}
return got;
}
#endif
else
return hdf_read_target (hfd, buffer, offset, len);
}
static int hdf_write2 (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
if (hfd->hfd_type == HFD_VHD_DYNAMIC)
return vhd_write (hfd, buffer, offset, len);
else if (hfd->hfd_type == HFD_VHD_FIXED)
return hdf_write_target (hfd, buffer, offset + 512, len);
#ifdef WITH_CHD
else if (hfd->hfd_type == HFD_CHD_OTHER)
return 0;
else if (hfd->hfd_type == HFD_CHD_HD) {
if (hfd->ci.readonly)
return 0;
hard_disk_file *chdf = (hard_disk_file*)hfd->chd_handle;
hard_disk_info *chdi = hard_disk_get_info(chdf);
chd_file *cf = hard_disk_get_chd(chdf);
uae_u8 *buf = (uae_u8*)buffer;
int got = 0;
offset /= chdi->sectorbytes;
while (len > 0) {
if (cf->write_units(offset, buf) != CHDERR_NONE)
return got;
got += chdi->sectorbytes;
buf += chdi->sectorbytes;
len -= chdi->sectorbytes;
offset++;
}
return got;
}
#endif
else
return hdf_write_target (hfd, buffer, offset, len);
}
static void adide_decode (void *v, int len)
{
int i;
uae_u8 *buffer = (uae_u8*)v;
for (i = 0; i < len; i += 2) {
uae_u8 *b = buffer + i;
uae_u16 w = (b[0] << 8) | (b[1] << 0);
uae_u16 o = adide_decode_word(w);
b[0] = o >> 8;
b[1] = o >> 0;
}
}
static void adide_encode (void *v, int len)
{
int i;
uae_u8 *buffer = (uae_u8*)v;
for (i = 0; i < len; i += 2) {
uae_u8 *b = buffer + i;
uae_u16 w = (b[0] << 8) | (b[1] << 0);
uae_u16 o = adide_encode_word(w);
b[0] = o >> 8;
b[1] = o >> 0;
}
}
static void hdf_byteswap (void *v, int len)
{