-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim_disk.c
4548 lines (4175 loc) · 169 KB
/
sim_disk.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
/* sim_disk.c: simulator disk support library
Copyright (c) 2011, Mark Pizzolato
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
ROBERT M SUPNIK 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.
Except as contained in this notice, the names of Mark Pizzolato shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Mark Pizzolato.
This is the place which hides processing of various disk formats,
as well as OS-specific direct hardware access.
25-Jan-11 MP Initial Implemementation
Public routines:
sim_disk_attach attach disk unit
sim_disk_detach detach disk unit
sim_disk_attach_help help routine for attaching disks
sim_disk_rdsect read disk sectors
sim_disk_rdsect_a read disk sectors asynchronously
sim_disk_wrsect write disk sectors
sim_disk_wrsect_a write disk sectors asynchronously
sim_disk_unload unload or detach a disk as needed
sim_disk_reset reset unit
sim_disk_wrp TRUE if write protected
sim_disk_isavailable TRUE if available for I/O
sim_disk_size get disk size
sim_disk_set_fmt set disk format
sim_disk_show_fmt show disk format
sim_disk_set_capac set disk capacity
sim_disk_show_capac show disk capacity
sim_disk_set_async enable asynchronous operation
sim_disk_clr_async disable asynchronous operation
sim_disk_data_trace debug support
Internal routines:
sim_os_disk_open_raw platform specific open raw device
sim_os_disk_close_raw platform specific close raw device
sim_os_disk_size_raw platform specific raw device size
sim_os_disk_unload_raw platform specific disk unload/eject
sim_os_disk_rdsect platform specific read sectors
sim_os_disk_wrsect platform specific write sectors
sim_vhd_disk_open platform independent open virtual disk file
sim_vhd_disk_create platform independent create virtual disk file
sim_vhd_disk_create_diff platform independent create differencing virtual disk file
sim_vhd_disk_close platform independent close virtual disk file
sim_vhd_disk_size platform independent virtual disk size
sim_vhd_disk_rdsect platform independent read virtual disk sectors
sim_vhd_disk_wrsect platform independent write virtual disk sectors
*/
#define _FILE_OFFSET_BITS 64 /* 64 bit file offset for raw I/O operations */
#include "sim_defs.h"
#include "sim_disk.h"
#include "sim_ether.h"
#include <ctype.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#endif
#if defined SIM_ASYNCH_IO
#include <pthread.h>
#endif
struct disk_context {
DEVICE *dptr; /* Device for unit (access to debug flags) */
uint32 dbit; /* debugging bit */
uint32 sector_size; /* Disk Sector Size (of the pseudo disk) */
uint32 capac_factor; /* Units of Capacity (2 = word, 1 = byte) */
uint32 xfer_element_size; /* Disk Bus Transfer size (1 - byte, 2 - word, 4 - longword) */
uint32 storage_sector_size;/* Sector size of the containing storage */
uint32 removable; /* Removable device flag */
uint32 auto_format; /* Format determined dynamically */
#if defined _WIN32
HANDLE disk_handle; /* OS specific Raw device handle */
#endif
#if defined SIM_ASYNCH_IO
int asynch_io; /* Asynchronous Interrupt scheduling enabled */
int asynch_io_latency; /* instructions to delay pending interrupt */
pthread_mutex_t lock;
pthread_t io_thread; /* I/O Thread Id */
pthread_mutex_t io_lock;
pthread_cond_t io_cond;
pthread_cond_t io_done;
pthread_cond_t startup_cond;
int io_dop;
uint8 *buf;
t_seccnt *rsects;
t_seccnt sects;
t_lba lba;
DISK_PCALLBACK callback;
t_stat io_status;
#endif
};
#define disk_ctx up8 /* Field in Unit structure which points to the disk_context */
#if defined SIM_ASYNCH_IO
#define AIO_CALLSETUP \
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx; \
\
if ((!callback) || !ctx->asynch_io)
#define AIO_CALL(op, _lba, _buf, _rsects, _sects, _callback) \
if (ctx->asynch_io) { \
struct disk_context *ctx = \
(struct disk_context *)uptr->disk_ctx; \
\
pthread_mutex_lock (&ctx->io_lock); \
\
sim_debug (ctx->dbit, ctx->dptr, \
"sim_disk AIO_CALL(op=%d, unit=%d, lba=0x%X, sects=%d)\n",\
op, (int)(uptr-ctx->dptr->units), _lba, _sects);\
\
if (ctx->callback) \
abort(); /* horrible mistake, stop */ \
ctx->io_dop = op; \
ctx->lba = _lba; \
ctx->buf = _buf; \
ctx->sects = _sects; \
ctx->rsects = _rsects; \
ctx->callback = _callback; \
pthread_cond_signal (&ctx->io_cond); \
pthread_mutex_unlock (&ctx->io_lock); \
} \
else \
if (_callback) \
(_callback) (uptr, r);
#define DOP_DONE 0 /* close */
#define DOP_RSEC 1 /* sim_disk_rdsect_a */
#define DOP_WSEC 2 /* sim_disk_wrsect_a */
#define DOP_IAVL 3 /* sim_disk_isavailable_a */
static void *
_disk_io(void *arg)
{
UNIT* volatile uptr = (UNIT*)arg;
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
/* Boost Priority for this I/O thread vs the CPU instruction execution
thread which in general won't be readily yielding the processor when
this thread needs to run */
sim_os_set_thread_priority (PRIORITY_ABOVE_NORMAL);
sim_debug (ctx->dbit, ctx->dptr, "_disk_io(unit=%d) starting\n", (int)(uptr-ctx->dptr->units));
pthread_mutex_lock (&ctx->io_lock);
pthread_cond_signal (&ctx->startup_cond); /* Signal we're ready to go */
while (ctx->asynch_io) {
pthread_cond_wait (&ctx->io_cond, &ctx->io_lock);
if (ctx->io_dop == DOP_DONE)
break;
pthread_mutex_unlock (&ctx->io_lock);
switch (ctx->io_dop) {
case DOP_RSEC:
ctx->io_status = sim_disk_rdsect (uptr, ctx->lba, ctx->buf, ctx->rsects, ctx->sects);
break;
case DOP_WSEC:
ctx->io_status = sim_disk_wrsect (uptr, ctx->lba, ctx->buf, ctx->rsects, ctx->sects);
break;
case DOP_IAVL:
ctx->io_status = sim_disk_isavailable (uptr);
break;
}
pthread_mutex_lock (&ctx->io_lock);
ctx->io_dop = DOP_DONE;
pthread_cond_signal (&ctx->io_done);
sim_activate (uptr, ctx->asynch_io_latency);
}
pthread_mutex_unlock (&ctx->io_lock);
sim_debug (ctx->dbit, ctx->dptr, "_disk_io(unit=%d) exiting\n", (int)(uptr-ctx->dptr->units));
return NULL;
}
/* This routine is called in the context of the main simulator thread before
processing events for any unit. It is only called when an asynchronous
thread has called sim_activate() to activate a unit. The job of this
routine is to put the unit in proper condition to digest what may have
occurred in the asynchrconous thread.
Since disk processing only handles a single I/O at a time to a
particular disk device (due to using stdio for the SimH Disk format
and stdio doesn't have an atomic seek+(read|write) operation),
we have the opportunity to possibly detect improper attempts to
issue multiple concurrent I/O requests. */
static void _disk_completion_dispatch (UNIT *uptr)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
DISK_PCALLBACK callback = ctx->callback;
sim_debug (ctx->dbit, ctx->dptr, "_disk_completion_dispatch(unit=%d, dop=%d, callback=%p)\n", (int)(uptr-ctx->dptr->units), ctx->io_dop, ctx->callback);
if (ctx->io_dop != DOP_DONE)
abort(); /* horribly wrong, stop */
if (ctx->callback && ctx->io_dop == DOP_DONE) {
ctx->callback = NULL;
callback (uptr, ctx->io_status);
}
}
static t_bool _disk_is_active (UNIT *uptr)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
if (ctx) {
sim_debug (ctx->dbit, ctx->dptr, "_disk_is_active(unit=%d, dop=%d)\n", (int)(uptr-ctx->dptr->units), ctx->io_dop);
return (ctx->io_dop != DOP_DONE);
}
return FALSE;
}
static t_bool _disk_cancel (UNIT *uptr)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
if (ctx) {
sim_debug (ctx->dbit, ctx->dptr, "_disk_cancel(unit=%d, dop=%d)\n", (int)(uptr-ctx->dptr->units), ctx->io_dop);
if (ctx->asynch_io) {
pthread_mutex_lock (&ctx->io_lock);
while (ctx->io_dop != DOP_DONE)
pthread_cond_wait (&ctx->io_done, &ctx->io_lock);
pthread_mutex_unlock (&ctx->io_lock);
}
}
return FALSE;
}
#else
#define AIO_CALLSETUP
#define AIO_CALL(op, _lba, _buf, _rsects, _sects, _callback) \
if (_callback) \
(_callback) (uptr, r);
#endif
/* Forward declarations */
static t_stat sim_vhd_disk_implemented (void);
static FILE *sim_vhd_disk_open (const char *rawdevicename, const char *openmode);
static FILE *sim_vhd_disk_create (const char *szVHDPath, t_offset desiredsize);
static FILE *sim_vhd_disk_create_diff (const char *szVHDPath, const char *szParentVHDPath);
static FILE *sim_vhd_disk_merge (const char *szVHDPath, char **ParentVHD);
static int sim_vhd_disk_close (FILE *f);
static void sim_vhd_disk_flush (FILE *f);
static t_offset sim_vhd_disk_size (FILE *f);
static t_stat sim_vhd_disk_rdsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectsread, t_seccnt sects);
static t_stat sim_vhd_disk_wrsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectswritten, t_seccnt sects);
static t_stat sim_vhd_disk_clearerr (UNIT *uptr);
static t_stat sim_vhd_disk_set_dtype (FILE *f, const char *dtype);
static const char *sim_vhd_disk_get_dtype (FILE *f);
static t_stat sim_os_disk_implemented_raw (void);
static FILE *sim_os_disk_open_raw (const char *rawdevicename, const char *openmode);
static int sim_os_disk_close_raw (FILE *f);
static void sim_os_disk_flush_raw (FILE *f);
static t_offset sim_os_disk_size_raw (FILE *f);
static t_stat sim_os_disk_unload_raw (FILE *f);
static t_bool sim_os_disk_isavailable_raw (FILE *f);
static t_stat sim_os_disk_rdsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectsread, t_seccnt sects);
static t_stat sim_os_disk_wrsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectswritten, t_seccnt sects);
static t_stat sim_os_disk_info_raw (FILE *f, uint32 *sector_size, uint32 *removable);
static char *HostPathToVhdPath (const char *szHostPath, char *szVhdPath, size_t VhdPathSize);
static char *VhdPathToHostPath (const char *szVhdPath, char *szHostPath, size_t HostPathSize);
static t_offset get_filesystem_size (UNIT *uptr);
struct sim_disk_fmt {
const char *name; /* name */
int32 uflags; /* unit flags */
int32 fmtval; /* Format type value */
t_stat (*impl_fnc)(void); /* Implemented Test Function */
};
static struct sim_disk_fmt fmts[DKUF_N_FMT] = {
{ "SIMH", 0, DKUF_F_STD, NULL},
{ "RAW", 0, DKUF_F_RAW, sim_os_disk_implemented_raw},
{ "VHD", 0, DKUF_F_VHD, sim_vhd_disk_implemented},
{ NULL, 0, 0}
};
/* Set disk format */
t_stat sim_disk_set_fmt (UNIT *uptr, int32 val, CONST char *cptr, void *desc)
{
uint32 f;
if (uptr == NULL)
return SCPE_IERR;
if (cptr == NULL)
return SCPE_ARG;
for (f = 0; f < DKUF_N_FMT && fmts[f].name; f++) {
if (fmts[f].name && (strcmp (cptr, fmts[f].name) == 0)) {
if ((fmts[f].impl_fnc) && (fmts[f].impl_fnc() != SCPE_OK))
return SCPE_NOFNC;
uptr->flags = (uptr->flags & ~DKUF_FMT) |
(fmts[f].fmtval << DKUF_V_FMT) | fmts[f].uflags;
return SCPE_OK;
}
}
return SCPE_ARG;
}
/* Show disk format */
t_stat sim_disk_show_fmt (FILE *st, UNIT *uptr, int32 val, CONST void *desc)
{
int32 f = DK_GET_FMT (uptr);
size_t i;
for (i = 0; i < DKUF_N_FMT; i++)
if (fmts[i].fmtval == f) {
fprintf (st, "%s format", fmts[i].name);
return SCPE_OK;
}
fprintf (st, "invalid format");
return SCPE_OK;
}
/* Set disk capacity */
t_stat sim_disk_set_capac (UNIT *uptr, int32 val, CONST char *cptr, void *desc)
{
t_offset cap;
t_stat r;
DEVICE *dptr = find_dev_from_unit (uptr);
if ((cptr == NULL) || (*cptr == 0))
return SCPE_ARG;
if (uptr->flags & UNIT_ATT)
return SCPE_ALATT;
cap = (t_offset) get_uint (cptr, 10, sim_taddr_64? 2000000: 2000, &r);
if (r != SCPE_OK)
return SCPE_ARG;
uptr->capac = (t_addr)((cap * ((t_offset) 1000000))/((dptr->flags & DEV_SECTORS) ? 512 : 1));
return SCPE_OK;
}
/* Show disk capacity */
t_stat sim_disk_show_capac (FILE *st, UNIT *uptr, int32 val, CONST void *desc)
{
const char *cap_units = "B";
DEVICE *dptr = find_dev_from_unit (uptr);
t_offset capac = ((t_offset)uptr->capac)*((dptr->flags & DEV_SECTORS) ? 512 : 1);
if ((dptr->dwidth / dptr->aincr) == 16)
cap_units = "W";
if (capac) {
if (capac >= (t_offset) 1000000)
fprintf (st, "capacity=%dM%s", (uint32) (capac / ((t_offset) 1000000)), cap_units);
else if (uptr->capac >= (t_addr) 1000)
fprintf (st, "capacity=%dK%s", (uint32) (capac / ((t_offset) 1000)), cap_units);
else fprintf (st, "capacity=%d%s", (uint32) capac, cap_units);
}
else fprintf (st, "undefined capacity");
return SCPE_OK;
}
/* Test for available */
t_bool sim_disk_isavailable (UNIT *uptr)
{
if (!(uptr->flags & UNIT_ATT)) /* attached? */
return FALSE;
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* SIMH format */
return TRUE;
case DKUF_F_VHD: /* VHD format */
return TRUE;
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
return sim_os_disk_isavailable_raw (uptr->fileref);
break;
default:
return FALSE;
}
}
t_bool sim_disk_isavailable_a (UNIT *uptr, DISK_PCALLBACK callback)
{
t_bool r = FALSE;
AIO_CALLSETUP
r = sim_disk_isavailable (uptr);
AIO_CALL(DOP_IAVL, 0, NULL, NULL, 0, callback);
return r;
}
/* Test for write protect */
t_bool sim_disk_wrp (UNIT *uptr)
{
return (uptr->flags & DKUF_WRP)? TRUE: FALSE;
}
/* Get Disk size */
t_offset sim_disk_size (UNIT *uptr)
{
t_offset physical_size, filesystem_size;
t_bool saved_quiet = sim_quiet;
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* SIMH format */
physical_size = sim_fsize_ex (uptr->fileref);
break;
case DKUF_F_VHD: /* VHD format */
physical_size = sim_vhd_disk_size (uptr->fileref);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
physical_size = sim_os_disk_size_raw (uptr->fileref);
break;
default:
return (t_offset)-1;
}
sim_quiet = TRUE;
filesystem_size = get_filesystem_size (uptr);
sim_quiet = saved_quiet;
if ((filesystem_size == (t_offset)-1) ||
(filesystem_size < physical_size))
return physical_size;
return filesystem_size;
}
/* Enable asynchronous operation */
t_stat sim_disk_set_async (UNIT *uptr, int latency)
{
#if !defined(SIM_ASYNCH_IO)
char *msg = "Disk: can't operate asynchronously\r\n";
sim_printf ("%s", msg);
return SCPE_NOFNC;
#else
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
pthread_attr_t attr;
sim_debug (ctx->dbit, ctx->dptr, "sim_disk_set_async(unit=%d)\n", (int)(uptr-ctx->dptr->units));
ctx->asynch_io = sim_asynch_enabled;
ctx->asynch_io_latency = latency;
if (ctx->asynch_io) {
pthread_mutex_init (&ctx->io_lock, NULL);
pthread_cond_init (&ctx->io_cond, NULL);
pthread_cond_init (&ctx->io_done, NULL);
pthread_cond_init (&ctx->startup_cond, NULL);
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_mutex_lock (&ctx->io_lock);
pthread_create (&ctx->io_thread, &attr, _disk_io, (void *)uptr);
pthread_attr_destroy(&attr);
pthread_cond_wait (&ctx->startup_cond, &ctx->io_lock); /* Wait for thread to stabilize */
pthread_mutex_unlock (&ctx->io_lock);
pthread_cond_destroy (&ctx->startup_cond);
}
uptr->a_check_completion = _disk_completion_dispatch;
uptr->a_is_active = _disk_is_active;
uptr->cancel = _disk_cancel;
return SCPE_OK;
#endif
}
/* Disable asynchronous operation */
t_stat sim_disk_clr_async (UNIT *uptr)
{
#if !defined(SIM_ASYNCH_IO)
return SCPE_NOFNC;
#else
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
/* make sure device exists */
if (!ctx) return SCPE_UNATT;
sim_debug (ctx->dbit, ctx->dptr, "sim_disk_clr_async(unit=%d)\n", (int)(uptr-ctx->dptr->units));
if (ctx->asynch_io) {
pthread_mutex_lock (&ctx->io_lock);
ctx->asynch_io = 0;
pthread_cond_signal (&ctx->io_cond);
pthread_mutex_unlock (&ctx->io_lock);
pthread_join (ctx->io_thread, NULL);
pthread_mutex_destroy (&ctx->io_lock);
pthread_cond_destroy (&ctx->io_cond);
pthread_cond_destroy (&ctx->io_done);
}
return SCPE_OK;
#endif
}
/* Read Sectors */
static t_stat _sim_disk_rdsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectsread, t_seccnt sects)
{
t_offset da;
uint32 err, tbc;
size_t i;
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
sim_debug (ctx->dbit, ctx->dptr, "_sim_disk_rdsect(unit=%d, lba=0x%X, sects=%d)\n", (int)(uptr-ctx->dptr->units), lba, sects);
da = ((t_offset)lba) * ctx->sector_size;
tbc = sects * ctx->sector_size;
if (sectsread)
*sectsread = 0;
err = sim_fseeko (uptr->fileref, da, SEEK_SET); /* set pos */
if (!err) {
i = sim_fread (buf, ctx->xfer_element_size, tbc/ctx->xfer_element_size, uptr->fileref);
if (i < tbc/ctx->xfer_element_size) /* fill */
memset (&buf[i*ctx->xfer_element_size], 0, tbc-(i*ctx->xfer_element_size));
err = ferror (uptr->fileref);
if ((!err) && (sectsread))
*sectsread = (t_seccnt)((i*ctx->xfer_element_size+ctx->sector_size-1)/ctx->sector_size);
}
return err;
}
t_stat sim_disk_rdsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectsread, t_seccnt sects)
{
t_stat r;
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
t_seccnt sread = 0;
sim_debug (ctx->dbit, ctx->dptr, "sim_disk_rdsect(unit=%d, lba=0x%X, sects=%d)\n", (int)(uptr-ctx->dptr->units), lba, sects);
if ((sects == 1) && /* Single sector reads */
(lba >= (uptr->capac*ctx->capac_factor)/(ctx->sector_size/((ctx->dptr->flags & DEV_SECTORS) ? 512 : 1)))) {/* beyond the end of the disk */
memset (buf, '\0', ctx->sector_size); /* are bad block management efforts - zero buffer */
if (sectsread)
*sectsread = 1;
return SCPE_OK; /* return success */
}
if ((0 == (ctx->sector_size & (ctx->storage_sector_size - 1))) || /* Sector Aligned & whole sector transfers */
((0 == ((lba*ctx->sector_size) & (ctx->storage_sector_size - 1))) &&
(0 == ((sects*ctx->sector_size) & (ctx->storage_sector_size - 1))))) {
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* SIMH format */
return _sim_disk_rdsect (uptr, lba, buf, sectsread, sects);
case DKUF_F_VHD: /* VHD format */
r = sim_vhd_disk_rdsect (uptr, lba, buf, &sread, sects);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
r = sim_os_disk_rdsect (uptr, lba, buf, &sread, sects);
break;
default:
return SCPE_NOFNC;
}
if (sectsread)
*sectsread = sread;
if (r != SCPE_OK)
return r;
sim_buf_swap_data (buf, ctx->xfer_element_size, (sread * ctx->sector_size) / ctx->xfer_element_size);
return r;
}
else { /* Unaligned and/or partial sector transfers */
uint8 *tbuf = (uint8*) malloc (sects*ctx->sector_size + 2*ctx->storage_sector_size);
t_lba sspsts = ctx->storage_sector_size/ctx->sector_size; /* sim sectors in a storage sector */
t_lba tlba = lba & ~(sspsts - 1);
t_seccnt tsects = sects + (lba - tlba);
tsects = (tsects + (sspsts - 1)) & ~(sspsts - 1);
if (sectsread)
*sectsread = 0;
if (tbuf == NULL)
return SCPE_MEM;
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* SIMH format */
r = _sim_disk_rdsect (uptr, tlba, tbuf, &sread, tsects);
break;
case DKUF_F_VHD: /* VHD format */
r = sim_vhd_disk_rdsect (uptr, tlba, tbuf, &sread, tsects);
if (r == SCPE_OK)
sim_buf_swap_data (tbuf, ctx->xfer_element_size, (sread * ctx->sector_size) / ctx->xfer_element_size);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
r = sim_os_disk_rdsect (uptr, tlba, tbuf, &sread, tsects);
if (r == SCPE_OK)
sim_buf_swap_data (tbuf, ctx->xfer_element_size, (sread * ctx->sector_size) / ctx->xfer_element_size);
break;
default:
free (tbuf);
return SCPE_NOFNC;
}
if (r == SCPE_OK) {
memcpy (buf, tbuf + ((lba - tlba) * ctx->sector_size), sects * ctx->sector_size);
if (sectsread) {
*sectsread = sread - (lba - tlba);
if (*sectsread > sects)
*sectsread = sects;
}
}
free (tbuf);
return r;
}
}
t_stat sim_disk_rdsect_a (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectsread, t_seccnt sects, DISK_PCALLBACK callback)
{
t_stat r = SCPE_OK;
AIO_CALLSETUP
r = sim_disk_rdsect (uptr, lba, buf, sectsread, sects);
AIO_CALL(DOP_RSEC, lba, buf, sectsread, sects, callback);
return r;
}
/* Write Sectors */
static t_stat _sim_disk_wrsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectswritten, t_seccnt sects)
{
t_offset da;
uint32 err, tbc;
size_t i;
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
sim_debug (ctx->dbit, ctx->dptr, "_sim_disk_wrsect(unit=%d, lba=0x%X, sects=%d)\n", (int)(uptr-ctx->dptr->units), lba, sects);
da = ((t_offset)lba) * ctx->sector_size;
tbc = sects * ctx->sector_size;
if (sectswritten)
*sectswritten = 0;
err = sim_fseeko (uptr->fileref, da, SEEK_SET); /* set pos */
if (!err) {
i = sim_fwrite (buf, ctx->xfer_element_size, tbc/ctx->xfer_element_size, uptr->fileref);
err = ferror (uptr->fileref);
if ((!err) && (sectswritten))
*sectswritten = (t_seccnt)((i*ctx->xfer_element_size+ctx->sector_size-1)/ctx->sector_size);
}
return err;
}
t_stat sim_disk_wrsect (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectswritten, t_seccnt sects)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
uint32 f = DK_GET_FMT (uptr);
t_stat r;
uint8 *tbuf = NULL;
sim_debug (ctx->dbit, ctx->dptr, "sim_disk_wrsect(unit=%d, lba=0x%X, sects=%d)\n", (int)(uptr-ctx->dptr->units), lba, sects);
if (uptr->dynflags & UNIT_DISK_CHK) {
DEVICE *dptr = find_dev_from_unit (uptr);
uint32 capac_factor = ((dptr->dwidth / dptr->aincr) == 16) ? 2 : 1; /* capacity units (word: 2, byte: 1) */
t_lba total_sectors = (t_lba)((uptr->capac*capac_factor)/(ctx->sector_size/((dptr->flags & DEV_SECTORS) ? 512 : 1)));
t_lba sect;
for (sect = 0; sect < sects; sect++) {
t_lba offset;
t_bool sect_error = FALSE;
for (offset = 0; offset < ctx->sector_size; offset += sizeof(uint32)) {
if (*((uint32 *)&buf[sect*ctx->sector_size + offset]) != (uint32)(lba + sect)) {
sect_error = TRUE;
break;
}
}
if (sect_error) {
uint32 save_dctrl = dptr->dctrl;
FILE *save_sim_deb = sim_deb;
sim_printf ("\n%s%d: Write Address Verification Error on lbn %d(0x%X) of %d(0x%X).\n", sim_dname (dptr), (int)(uptr-dptr->units), (int)(lba+sect), (int)(lba+sect), (int)total_sectors, (int)total_sectors);
dptr->dctrl = 0xFFFFFFFF;
sim_deb = save_sim_deb ? save_sim_deb : stdout;
sim_disk_data_trace (uptr, buf+sect*ctx->sector_size, lba+sect, ctx->sector_size, "Found", TRUE, 1);
dptr->dctrl = save_dctrl;
sim_deb = save_sim_deb;
}
}
}
if (f == DKUF_F_STD)
return _sim_disk_wrsect (uptr, lba, buf, sectswritten, sects);
if ((0 == (ctx->sector_size & (ctx->storage_sector_size - 1))) || /* Sector Aligned & whole sector transfers */
((0 == ((lba*ctx->sector_size) & (ctx->storage_sector_size - 1))) &&
(0 == ((sects*ctx->sector_size) & (ctx->storage_sector_size - 1))))) {
if (sim_end || (ctx->xfer_element_size == sizeof (char)))
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_VHD: /* VHD format */
return sim_vhd_disk_wrsect (uptr, lba, buf, sectswritten, sects);
case DKUF_F_RAW: /* Raw Physical Disk Access */
return sim_os_disk_wrsect (uptr, lba, buf, sectswritten, sects);
default:
return SCPE_NOFNC;
}
tbuf = (uint8*) malloc (sects * ctx->sector_size);
if (NULL == tbuf)
return SCPE_MEM;
sim_buf_copy_swapped (tbuf, buf, ctx->xfer_element_size, (sects * ctx->sector_size) / ctx->xfer_element_size);
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_VHD: /* VHD format */
r = sim_vhd_disk_wrsect (uptr, lba, tbuf, sectswritten, sects);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
r = sim_os_disk_wrsect (uptr, lba, tbuf, sectswritten, sects);
break;
default:
r = SCPE_NOFNC;
break;
}
}
else { /* Unaligned and/or partial sector transfers */
t_lba sspsts = ctx->storage_sector_size/ctx->sector_size; /* sim sectors in a storage sector */
t_lba tlba = lba & ~(sspsts - 1);
t_seccnt tsects = sects + (lba - tlba);
tbuf = (uint8*) malloc (sects*ctx->sector_size + 2*ctx->storage_sector_size);
tsects = (tsects + (sspsts - 1)) & ~(sspsts - 1);
if (sectswritten)
*sectswritten = 0;
if (tbuf == NULL)
return SCPE_MEM;
/* Partial Sector writes require a read-modify-write sequence for the partial sectors */
if ((lba & (sspsts - 1)) ||
(sects < sspsts))
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_VHD: /* VHD format */
sim_vhd_disk_rdsect (uptr, tlba, tbuf, NULL, sspsts);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
sim_os_disk_rdsect (uptr, tlba, tbuf, NULL, sspsts);
break;
default:
r = SCPE_NOFNC;
break;
}
if ((tsects > sspsts) &&
((sects + lba - tlba) & (sspsts - 1)))
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_VHD: /* VHD format */
sim_vhd_disk_rdsect (uptr, tlba + tsects - sspsts,
tbuf + (tsects - sspsts) * ctx->sector_size,
NULL, sspsts);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
sim_os_disk_rdsect (uptr, tlba + tsects - sspsts,
tbuf + (tsects - sspsts) * ctx->sector_size,
NULL, sspsts);
break;
default:
r = SCPE_NOFNC;
break;
}
sim_buf_copy_swapped (tbuf + (lba & (sspsts - 1)) * ctx->sector_size,
buf, ctx->xfer_element_size, (sects * ctx->sector_size) / ctx->xfer_element_size);
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_VHD: /* VHD format */
r = sim_vhd_disk_wrsect (uptr, tlba, tbuf, sectswritten, tsects);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
r = sim_os_disk_wrsect (uptr, tlba, tbuf, sectswritten, tsects);
break;
default:
r = SCPE_NOFNC;
break;
}
if ((r == SCPE_OK) && sectswritten) {
*sectswritten -= (lba - tlba);
if (*sectswritten > sects)
*sectswritten = sects;
}
}
free (tbuf);
return r;
}
t_stat sim_disk_wrsect_a (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectswritten, t_seccnt sects, DISK_PCALLBACK callback)
{
t_stat r = SCPE_OK;
AIO_CALLSETUP
r = sim_disk_wrsect (uptr, lba, buf, sectswritten, sects);
AIO_CALL(DOP_WSEC, lba, buf, sectswritten, sects, callback);
return r;
}
t_stat sim_disk_unload (UNIT *uptr)
{
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* Simh */
case DKUF_F_VHD: /* VHD format */
return sim_disk_detach (uptr);
case DKUF_F_RAW: /* Raw Physical Disk Access */
return sim_os_disk_unload_raw (uptr->fileref); /* remove/eject disk */
break;
default:
return SCPE_NOFNC;
}
}
/*
This routine is called when the simulator stops and any time
the asynch mode is changed (enabled or disabled)
*/
static void _sim_disk_io_flush (UNIT *uptr)
{
uint32 f = DK_GET_FMT (uptr);
#if defined (SIM_ASYNCH_IO)
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
sim_disk_clr_async (uptr);
if (sim_asynch_enabled)
sim_disk_set_async (uptr, ctx->asynch_io_latency);
#endif
switch (f) { /* case on format */
case DKUF_F_STD: /* Simh */
fflush (uptr->fileref);
break;
case DKUF_F_VHD: /* Virtual Disk */
sim_vhd_disk_flush (uptr->fileref);
break;
case DKUF_F_RAW: /* Physical */
sim_os_disk_flush_raw (uptr->fileref);
break;
}
}
static t_stat _err_return (UNIT *uptr, t_stat stat)
{
free (uptr->filename);
uptr->filename = NULL;
free (uptr->disk_ctx);
uptr->disk_ctx = NULL;
return stat;
}
#pragma pack(push,1)
typedef struct _ODS1_HomeBlock
{
uint16 hm1_w_ibmapsize;
uint32 hm1_l_ibmaplbn;
uint16 hm1_w_maxfiles;
uint16 hm1_w_cluster;
uint16 hm1_w_devtype;
uint16 hm1_w_structlev;
#define HM1_C_LEVEL1 0401
#define HM1_C_LEVEL2 0402
uint8 hm1_t_volname[12];
uint8 hm1_b_fill_1[4];
uint16 hm1_w_volowner;
uint16 hm1_w_protect;
uint16 hm1_w_volchar;
uint16 hm1_w_fileprot;
uint8 hm1_b_fill_2[6];
uint8 hm1_b_window;
uint8 hm1_b_extend;
uint8 hm1_b_lru_lim;
uint8 hm1_b_fill_3[11];
uint16 hm1_w_checksum1;
uint8 hm1_t_credate[14];
uint8 hm1_b_fill_4[382];
uint32 hm1_l_serialnum;
uint8 hm1_b_fill_5[12];
uint8 hm1_t_volname2[12];
uint8 hm1_t_ownername[12];
uint8 hm1_t_format[12];
uint8 hm1_t_fill_6[2];
uint16 hm1_w_checksum2;
} ODS1_HomeBlock;
typedef struct _ODS2_HomeBlock
{
uint32 hm2_l_homelbn;
uint32 hm2_l_alhomelbn;
uint32 hm2_l_altidxlbn;
uint8 hm2_b_strucver;
uint8 hm2_b_struclev;
uint16 hm2_w_cluster;
uint16 hm2_w_homevbn;
uint16 hm2_w_alhomevbn;
uint16 hm2_w_altidxvbn;
uint16 hm2_w_ibmapvbn;
uint32 hm2_l_ibmaplbn;
uint32 hm2_l_maxfiles;
uint16 hm2_w_ibmapsize;
uint16 hm2_w_resfiles;
uint16 hm2_w_devtype;
uint16 hm2_w_rvn;
uint16 hm2_w_setcount;
uint16 hm2_w_volchar;
uint32 hm2_l_volowner;
uint32 hm2_l_reserved;
uint16 hm2_w_protect;
uint16 hm2_w_fileprot;
uint16 hm2_w_reserved;
uint16 hm2_w_checksum1;
uint32 hm2_q_credate[2];
uint8 hm2_b_window;
uint8 hm2_b_lru_lim;
uint16 hm2_w_extend;
uint32 hm2_q_retainmin[2];
uint32 hm2_q_retainmax[2];
uint32 hm2_q_revdate[2];
uint8 hm2_r_min_class[20];
uint8 hm2_r_max_class[20];
uint8 hm2_r_reserved[320];
uint32 hm2_l_serialnum;
uint8 hm2_t_strucname[12];
uint8 hm2_t_volname[12];
uint8 hm2_t_ownername[12];
uint8 hm2_t_format[12];
uint16 hm2_w_reserved2;
uint16 hm2_w_checksum2;
} ODS2_HomeBlock;
typedef struct _ODS1_FileHeader
{
uint8 fh1_b_idoffset;
uint8 fh1_b_mpoffset;
uint16 fh1_w_fid_num;
uint16 fh1_w_fid_seq;
uint16 fh1_w_struclev;
uint16 fh1_w_fileowner;
uint16 fh1_w_fileprot;
uint16 fh1_w_filechar;
uint16 fh1_w_recattr;
uint8 fh1_b_fill_1[494];
uint16 fh1_w_checksum;
} ODS1_FileHeader;
typedef struct _ODS2_FileHeader
{
uint8 fh2_b_idoffset;
uint8 fh2_b_mpoffset;
uint8 fh2_b_acoffset;
uint8 fh2_b_rsoffset;
uint16 fh2_w_seg_num;
uint16 fh2_w_structlev;
uint16 fh2_w_fid[3];
uint16 fh2_w_ext_fid[3];
uint16 fh2_w_recattr[16];
uint32 fh2_l_filechar;
uint16 fh2_w_remaining[228];
} ODS2_FileHeader;
typedef union _ODS2_Retreval
{
struct
{
unsigned fm2___fill : 14; /* type specific data */
unsigned fm2_v_format : 2; /* format type code */
} fm2_r_word0_bits;
struct
{
unsigned fm2_v_exact : 1; /* exact placement specified */
unsigned fm2_v_oncyl : 1; /* on cylinder allocation desired */
unsigned fm2___fill : 10;
unsigned fm2_v_lbn : 1; /* use LBN of next map pointer */
unsigned fm2_v_rvn : 1; /* place on specified RVN */
unsigned fm2_v_format0 : 2;
} fm2_r_map_bits0;
struct
{
unsigned fm2_b_count1 : 8; /* low byte described below */
unsigned fm2_v_highlbn1 : 6; /* high order LBN */
unsigned fm2_v_format1 : 2;
unsigned fm2_w_lowlbn1 : 16; /* low order LBN */
} fm2_r_map_bits1;
struct
{
struct
{
unsigned fm2_v_count2 : 14; /* count field */
unsigned fm2_v_format2 : 2;
unsigned fm2_l_lowlbn2 : 16; /* low order LBN */
} fm2_r_map2_long0;
uint16 fm2_l_highlbn2; /* high order LBN */
} fm2_r_map_bits2;
struct
{
struct
{
unsigned fm2_v_highcount3 : 14; /* low order count field */
unsigned fm2_v_format3 : 2;
unsigned fm2_w_lowcount3 : 16; /* high order count field */