-
Notifications
You must be signed in to change notification settings - Fork 20
/
sim_disk.c
7140 lines (6530 loc) · 284 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
MARK PIZZOLATO 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 Implementation
Public routines:
sim_disk_attach attach disk unit
sim_disk_attach_ex attach disk unit extended parameters
sim_disk_attach_ex2 attach disk unit additional extended parameters
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
sim_disk_test unit test routine
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>
#if defined SIM_ASYNCH_IO
#include <pthread.h>
#endif
/* Newly created SIMH (and possibly RAW) disk containers */
/* will have this data as the last 512 bytes of the container */
/* It will not be considered part of the data in the container */
/* Previously existing containers will have this appended to */
/* the end of the container if they are opened for write */
struct simh_disk_footer {
uint8 Signature[4]; /* must be 'simh' */
uint8 CreatingSimulator[64]; /* name of simulator */
uint8 DriveType[16];
uint32 SectorSize;
uint32 SectorCount;
uint32 TransferElementSize;
uint8 CreationTime[28]; /* Result of ctime() */
uint8 FooterVersion; /* Initially 0 */
#define FOOTER_VERSION 0
uint8 AccessFormat; /* 1 - SIMH, 2 - RAW */
uint8 Reserved[354]; /* Currently unused */
uint8 DeviceName[16]; /* Name of the Device when created */
uint32 Highwater[2]; /* Size before footer addition or furthest container point written */
uint32 Unused; /* Currently unused */
uint32 Checksum; /* CRC32 of the prior 508 bytes */
};
/* OS Independent Disk Virtual Disk (VHD) I/O support */
#if (defined (VMS) && !(defined (__ALPHA) || defined (__ia64)))
#define DONT_DO_VHD_SUPPORT /* VAX/VMS compilers don't have 64 bit integers */
#endif
#if defined(_WIN32) || defined (__ALPHA) || defined (__ia64) || defined (VMS)
#ifndef __BYTE_ORDER__
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#endif
#endif
#ifndef __BYTE_ORDER__
#define __BYTE_ORDER__ UNKNOWN
#endif
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
static uint32
NtoHl(uint32 value)
{
uint8 *l = (uint8 *)&value;
return (uint32)l[3] | ((uint32)l[2]<<8) | ((uint32)l[1]<<16) | ((uint32)l[0]<<24);
}
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
static uint32
NtoHl(uint32 value)
{
return value;
}
#else
static uint32
NtoHl(uint32 value)
{
uint8 *l = (uint8 *)&value;
if (sim_end)
return l[3] | (l[2]<<8) | (l[1]<<16) | (l[0]<<24);
return value;
}
#endif
struct disk_context {
t_offset container_size; /* Size of the data portion (of the pseudo disk) */
t_offset highwater; /* Furthest written sector in the disk */
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 (8 = quadword, 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 is_cdrom; /* Host system CDROM Device */
uint32 media_removed; /* Media not available flag */
uint32 auto_format; /* Format determined dynamically */
uint32 read_count; /* Number of read operations performed */
uint32 write_count; /* Number of write operations performed */
struct simh_disk_footer
*footer;
#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_unit (ctx->dbit, uptr, \
"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_unit (ctx->dbit, uptr, "_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_unit (ctx->dbit, uptr, "_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 asynchronous 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_unit (ctx->dbit, uptr, "_disk_completion_dispatch(unit=%d, dop=%d, callback=%p)\n", (int)(uptr - ctx->dptr->units), ctx->io_dop, (void *)(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_unit (ctx->dbit, uptr, "_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_unit (ctx->dbit, uptr, "_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, uint32 SectorSize, uint32 xfer_element_size);
static const char *sim_vhd_disk_get_dtype (FILE *f, uint32 *SectorSize, uint32 *xfer_element_size, char sim_name[64], time_t *creation_time);
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_read (UNIT *uptr, t_offset addr, uint8 *buf, uint32 *bytesread, uint32 bytes);
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_write (UNIT *uptr, t_offset addr, uint8 *buf, uint32 *byteswritten, uint32 bytes);
static t_stat sim_os_disk_info_raw (FILE *f, uint32 *sector_size, uint32 *removable, uint32 *is_cdrom);
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, t_bool *readonly);
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[] = {
{ "AUTO detect", 0, DKUF_F_AUTO, NULL},
{ "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, NULL}
};
/* 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) || (*cptr == '\0'))
return SCPE_ARG;
for (f = 0; fmts[f].name; f++) {
if (fmts[f].name && (MATCH_CMD (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 sim_messagef (SCPE_ARG, "Unknown disk format: %s\n", cptr);
}
/* Show disk format */
static const char *sim_disk_fmt (UNIT *uptr)
{
int32 f = DK_GET_FMT (uptr);
size_t i;
for (i = 0; fmts[i].name; i++)
if (fmts[i].fmtval == f) {
return fmts[i].name;
}
return "invalid";
}
t_stat sim_disk_show_fmt (FILE *st, UNIT *uptr, int32 val, CONST void *desc)
{
fprintf (st, "%s format", sim_disk_fmt (uptr));
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)
{
struct disk_context *ctx;
t_bool is_available;
if (!(uptr->flags & UNIT_ATT)) /* attached? */
return FALSE;
ctx = (struct disk_context *)uptr->disk_ctx;
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* SIMH format */
is_available = TRUE;
break;
case DKUF_F_VHD: /* VHD format */
is_available = TRUE;
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
if (sim_os_disk_isavailable_raw (uptr->fileref)) {
if (ctx->media_removed) {
int32 saved_switches = sim_switches;
int32 saved_quiet = sim_quiet;
char *path = (char *)malloc (1 + strlen (uptr->filename));
sim_switches = 0;
sim_quiet = 1;
strcpy (path, uptr->filename);
sim_disk_attach (uptr, path, ctx->sector_size, ctx->xfer_element_size,
FALSE, ctx->dbit, NULL, 0, 0);
sim_quiet = saved_quiet;
sim_switches = saved_switches;
free (path);
ctx->media_removed = 0;
}
}
else
ctx->media_removed = 1;
is_available = !ctx->media_removed;
break;
default:
is_available = FALSE;
break;
}
sim_debug_unit (ctx->dbit, uptr, "sim_disk_isavailable(unit=%d)=%s\n", (int)(uptr - ctx->dptr->units), is_available ? "true" : "false");
return is_available;
}
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;
}
static t_bool sim_disk_no_autosize = FALSE;
t_stat sim_disk_set_noautosize (int32 flag, CONST char *cptr)
{
DEVICE *dptr;
uint32 dev, unit, count = 0;
if (flag == sim_disk_no_autosize)
return sim_messagef (SCPE_ARG, "Autosizing is already %sabled!\n",
sim_disk_no_autosize ? "dis" : "en");
for (dev = 0; (dptr = sim_devices[dev]) != NULL; dev++) {
if ((DEV_TYPE (dptr) != DEV_DISK) ||
(dptr->flags & DEV_DIS))
continue;
++count;
for (unit = 0; unit < dptr->numunits; unit++) {
char cmd[CBUFSIZE];
int32 saved_sim_show_message = sim_show_message;
sim_show_message = FALSE;
sprintf (cmd, "%s %sAUTOSIZE", sim_uname (&dptr->units[unit]), (flag != 0) ? "NO" : "");
set_cmd (0, cmd);
sim_show_message = saved_sim_show_message;
}
}
if (count == 0)
return sim_messagef (SCPE_ARG, "No disk devices support autosizing\n");
sim_disk_no_autosize = flag;
return SCPE_OK;
}
/* 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)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
t_offset physical_size, filesystem_size;
t_bool saved_quiet = sim_quiet;
if ((uptr->flags & UNIT_ATT) == 0)
return (t_offset)-1;
physical_size = ctx->container_size;
sim_quiet = TRUE;
filesystem_size = get_filesystem_size (uptr, NULL);
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: cannot 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_unit (ctx->dbit, uptr, "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_unit (ctx->dbit, uptr, "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_unit (ctx->dbit, uptr, "_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;
while (tbc) {
size_t sectbytes;
clearerr (uptr->fileref);
err = sim_fseeko (uptr->fileref, da, SEEK_SET); /* set pos */
if (err)
return SCPE_IOERR;
i = sim_fread (buf, 1, tbc, uptr->fileref);
if (i < tbc) /* fill */
memset (&buf[i], 0, tbc-i);
if ((i == 0) && /* Reading at or past EOF? */
feof (uptr->fileref))
i = tbc; /* return 0's which have already been filled in buffer */
sectbytes = (i / ctx->sector_size) * ctx->sector_size;
if (i > sectbytes)
sectbytes += ctx->sector_size;
if (sectsread)
*sectsread += sectbytes / ctx->sector_size;
err = ferror (uptr->fileref);
if (err)
return SCPE_IOERR;
tbc -= sectbytes;
if ((tbc == 0) || (i == 0))
return SCPE_OK;
da += sectbytes;
buf += sectbytes;
}
return SCPE_OK;
}
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;
uint32 f = DK_GET_FMT (uptr);
t_seccnt sread = 0;
sim_debug_unit (ctx->dbit, uptr, "sim_disk_rdsect(unit=%d, lba=0x%X, sects=%d)\n", (int)(uptr - ctx->dptr->units), lba, sects);
ctx->read_count++; /* record read operation */
if ((sects == 1) && /* Single sector reads */
(lba >= (uptr->capac*ctx->capac_factor)/(ctx->sector_size/((ctx->dptr->flags & DEV_SECTORS) ? ctx->sector_size : 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)))) ||
(f == DKUF_F_STD) || (f == DKUF_F_VHD)) { /* or SIMH or VHD formats */
switch (f) { /* case on format */
case DKUF_F_STD: /* SIMH format */
r = _sim_disk_rdsect (uptr, lba, buf, &sread, sects);
break;
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;
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 in RAW mode */
size_t tbufsize = sects * ctx->sector_size + 2 * ctx->storage_sector_size;
uint8 *tbuf = (uint8*) malloc (tbufsize);
t_offset ssaddr = (lba * (t_offset)ctx->sector_size) & ~(t_offset)(ctx->storage_sector_size -1);
uint32 soffset = (uint32)((lba * (t_offset)ctx->sector_size) - ssaddr);
uint32 bytesread;
if (sectsread)
*sectsread = 0;
if (tbuf == NULL)
return SCPE_MEM;
r = sim_os_disk_read (uptr, ssaddr, tbuf, &bytesread, tbufsize & ~(ctx->storage_sector_size - 1));
sim_buf_swap_data (tbuf + soffset, ctx->xfer_element_size, (bytesread - soffset) / ctx->xfer_element_size);
memcpy (buf, tbuf + soffset, sects * ctx->sector_size);
if (sectsread) {
*sectsread = (bytesread - soffset) / ctx->sector_size;
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_unit (ctx->dbit, uptr, "_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)
return SCPE_IOERR;
i = sim_fwrite (buf, ctx->xfer_element_size, tbc/ctx->xfer_element_size, uptr->fileref);
if (sectswritten)
*sectswritten += (t_seccnt)((i * ctx->xfer_element_size + ctx->sector_size - 1)/ctx->sector_size);
err = ferror (uptr->fileref);
if (err)
return SCPE_IOERR;
return SCPE_OK;
}
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;
t_seccnt written = 0;
sim_debug_unit (ctx->dbit, uptr, "sim_disk_wrsect(unit=%d, lba=0x%X, sects=%d)\n", (int)(uptr - ctx->dptr->units), lba, sects);
if (sectswritten)
*sectswritten = 0;
ctx->write_count++; /* record write operation */
if (uptr->dynflags & UNIT_DISK_CHK) {
DEVICE *dptr = find_dev_from_unit (uptr);
uint32 capac_factor = ((dptr->dwidth / dptr->aincr) >= 32) ? 8 : ((dptr->dwidth / dptr->aincr) == 16) ? 2 : 1; /* capacity units (quadword: 8, 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: Write Address Verification Error on lbn %d(0x%X) of %d(0x%X).\n", sim_uname (uptr), (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;
}
}
}
switch (f) { /* case on format */
case DKUF_F_STD: /* SIMH format */
r = _sim_disk_wrsect (uptr, lba, buf, &written, sects);
break;
case DKUF_F_VHD: /* VHD format */
if (!sim_end && (ctx->xfer_element_size != sizeof (char))) {
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);
buf = tbuf;
}
r = sim_vhd_disk_wrsect (uptr, lba, buf, &written, sects);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
break; /* handle below */
default:
return SCPE_NOFNC;
}
if (f == DKUF_F_RAW) {
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))) {
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);
buf = tbuf;
}
r = sim_os_disk_wrsect (uptr, lba, buf, &written, sects);
}
else { /* Unaligned and/or partial sector transfers in RAW mode */
size_t tbufsize = sects * ctx->sector_size + 2 * ctx->storage_sector_size;
t_offset ssaddr = (lba * (t_offset)ctx->sector_size) & ~(t_offset)(ctx->storage_sector_size -1);
t_offset sladdr = ((lba + sects) * (t_offset)ctx->sector_size) & ~(t_offset)(ctx->storage_sector_size -1);
uint32 soffset = (uint32)((lba * (t_offset)ctx->sector_size) - ssaddr);
uint32 byteswritten;
tbuf = (uint8*) malloc (tbufsize);
if (tbuf == NULL)
return SCPE_MEM;
/* Partial Sector writes require a read-modify-write sequence for the partial sectors */
if (soffset)
sim_os_disk_read (uptr, ssaddr, tbuf, NULL, ctx->storage_sector_size);
sim_os_disk_read (uptr, sladdr, tbuf + (size_t)(sladdr - ssaddr), NULL, ctx->storage_sector_size);
sim_buf_copy_swapped (tbuf + soffset,
buf, ctx->xfer_element_size, (sects * ctx->sector_size) / ctx->xfer_element_size);
r = sim_os_disk_write (uptr, ssaddr, tbuf, &byteswritten, (soffset + (sects * ctx->sector_size) + ctx->storage_sector_size - 1) & ~(ctx->storage_sector_size - 1));
written = byteswritten / ctx->sector_size;
if (written > sects)
written = sects;
}
}
free (tbuf);
if (sectswritten)
*sectswritten = written;
if (written > 0) {
t_offset da = ((t_offset)lba) * ctx->sector_size;
t_offset end_write = da + (written * ctx->sector_size);
if (ctx->highwater < end_write)
ctx->highwater = end_write;
}
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)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
switch (DK_GET_FMT (uptr)) { /* case on format */
case DKUF_F_STD: /* Simh */
case DKUF_F_VHD: /* VHD format */
ctx->media_removed = 1;
return sim_disk_detach (uptr);
case DKUF_F_RAW: /* Raw Physical Disk Access */
ctx->media_removed = 1;
return sim_os_disk_unload_raw (uptr->fileref); /* remove/eject disk */
break;
default:
return SCPE_NOFNC;
}
}
t_stat sim_disk_erase (UNIT *uptr)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
uint8 *buf;
t_lba lba;
if (uptr->flags & UNIT_ATT)
return SCPE_UNATT;
buf = (uint8 *)calloc (1, ctx->storage_sector_size);
if (buf == NULL)
return SCPE_MEM;
for (lba = 0; lba < ctx->container_size / ctx->sector_size; lba++)
sim_disk_wrsect (uptr, lba, buf, NULL, 1); /* write sector */
free (buf);
return SCPE_OK;
}
/*
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;
}
static t_stat _sim_disk_rdsect_interleave (UNIT *uptr, t_lba lba, uint8 *buf, t_seccnt *sectsread, t_seccnt sects, uint16 sectpertrack, uint16 interleave, uint16 skew, uint16 offset)
{
struct disk_context *ctx = (struct disk_context *)uptr->disk_ctx;
t_lba sectno = lba, psa;
t_stat status;
if (sectsread)
*sectsread = 0;
do {
uint16 i, track, sector;
/*
* Map an LBA address into a physical sector address
*/
track = sectno / sectpertrack;
i = (sectno % sectpertrack) * interleave;
if (i >= sectpertrack)
i++;
sector = (i + (track * skew)) % sectpertrack;