-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_disk.c
8572 lines (7859 loc) · 351 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 Implemementation
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_autosize MTAB set autosize
sim_disk_show_autosize MTAB display autosize
sim_disk_set_autozap MTAB set autozap
sim_disk_show_autozap MTAB display autozap
sim_disk_set_async enable asynchronous operation
sim_disk_clr_async disable asynchronous operation
sim_disk_data_trace debug support
sim_disk_set_drive_type MTAB validator routine
sim_disk_set_drive_type_by_name device reset initialization
sim_disk_show_drive_type MTAB display routine
sim_disk_get_drive_type_set_string set command arguments for the specified unit
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_CHS platform independent virtual disk size CHS value
sim_vhd_disk_parent_path platform independent differencing virtual disk parent path
sim_vhd_disk_rdsect platform independent read virtual disk sectors
sim_vhd_disk_wrsect platform independent write virtual disk sectors
sim_disk_find_type locate DRVTYP of named disk type
*/
#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 "sim_scsi.h"
#include "sim_scp_private.h"
#define DKUF_F_AUTO 0 /* Auto detect format format */
#define DKUF_F_STD 1 /* SIMH format */
#define DKUF_F_RAW 2 /* Raw Physical Disk Access */
#define DKUF_F_VHD 3 /* VHD format */
#define DKUF_E_AUTO 0 /* Auto detect encoding */
#define DKUF_E_DLD9 1 /* KLH10 packed 36bit little endian word */
#define DKUF_E_DBD9 2 /* KLH10 packed 36bit big endian word */
#define DK_GET_FMT(u) (((u)->flags >> DKUF_V_FMT) & DKUF_M_FMT)
#define DK_GET_ENC(u) (((u)->flags >> DKUF_V_ENC) & DKUF_M_ENC)
#if defined SIM_ASYNCH_IO
#include <pthread.h>
#endif
static t_bool sim_disk_check_attached_container (const char *filename, UNIT **auptr);
/* 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 ElementEncodingSize;
uint8 CreationTime[28]; /* Result of ctime() */
uint8 FooterVersion; /* Initially 0 */
#define FOOTER_VERSION 1
uint8 AccessFormat; /* 1 - SIMH, 2 - RAW */
uint8 Reserved[342]; /* Currently unused */
uint32 Geometry; /* CHS (Cylinders, Heads and Sectors) */
uint32 DataWidth; /* Data Width in the Transfer Size */
uint32 MediaID; /* Media ID */
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_encode_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 media_id; /* MediaID of the container */
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 */
uint32 data_ileave; /* Data sectors interleaved in container */
uint32 data_ileave_skew; /* Data sectors track skew in container */
DRVTYP *initial_drvtyp; /* Unit Drive Type before any autosize */
t_addr initial_capac; /* Unit Capacity before any autosize */
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) /* horrible mistake, stop */ \
SIM_SCP_ABORT ("AIO_CALL error"); \
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 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_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)
SIM_SCP_ABORT ("_disk_completion_dispatch()"); /* 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, DRVTYP *drvtyp);
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 uint32 sim_vhd_CHS (FILE *f);
static const char *sim_vhd_disk_parent_path (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_encode_size, uint32 media_id, const char *device_name, uint32 data_width, DRVTYP *drvtyp);
static const char *sim_vhd_disk_get_dtype (FILE *f, uint32 *SectorSize, uint32 *xfer_encode_size, char sim_name[64], time_t *creation_time, uint32 *media_id, char device_name[16], uint32 *data_width);
static DRVTYP *sim_disk_find_type (UNIT *uptr, const char *dtype);
uint32 sim_disk_drvtype_geometry (DRVTYP *drvtyp, uint32 totalSectors);
static uint32 sim_SectorsToCHS (uint32 totalSectors);
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 */
uint32 encode; /* Data Encode Default - 0 means take from attach parameter */
t_stat (*impl_fnc)(void); /* Implemented Test Function */
};
static struct sim_disk_fmt fmts[] = {
{ "AUTO detect", 0, DKUF_F_AUTO, 0, NULL},
{ "SIMH", 0, DKUF_F_STD, 0, NULL},
{ "RAW", 0, DKUF_F_RAW, 0, sim_os_disk_implemented_raw},
{ "VHD", 0, DKUF_F_VHD, 0, sim_vhd_disk_implemented},
{ NULL, 0, 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;
if (fmts[f].fmtval == DKUF_F_AUTO)
uptr->flags = (uptr->flags & ~DKUF_ENC) | (DKUF_E_AUTO << DKUF_V_ENC);
return SCPE_OK;
}
}
if (MATCH_CMD (cptr, "DLD9") == 0) {
if (DK_GET_FMT (uptr) == DKUF_F_AUTO)
uptr->flags = (uptr->flags & ~DKUF_FMT) |
(DKUF_F_STD << DKUF_V_FMT) | fmts[f].uflags;
uptr->flags = (uptr->flags & ~DKUF_ENC) | (DKUF_E_DLD9 << DKUF_V_ENC);
return SCPE_OK;
}
if (MATCH_CMD (cptr, "DBD9") == 0) {
if (DK_GET_FMT (uptr) == DKUF_F_AUTO)
uptr->flags = (uptr->flags & ~DKUF_FMT) |
(DKUF_F_STD << DKUF_V_FMT) | fmts[f].uflags;
uptr->flags = (uptr->flags & ~DKUF_ENC) | (DKUF_E_DBD9 << DKUF_V_ENC);
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);
static char fmt_buf[32];
static const char *encodings[] = {"", "DLD9", "DBD9", ""};
size_t i;
for (i = 0; fmts[i].name; i++)
if (fmts[i].fmtval == f) {
snprintf (fmt_buf, sizeof (fmt_buf), "%s%s%s", fmts[i].name, (DK_GET_ENC (uptr) > DKUF_E_AUTO) ? "-" : "", encodings[DK_GET_ENC (uptr)]);
return fmt_buf;
}
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;
}
const char *_disk_tranfer_encoding (uint32 val)
{
static char encoding[128];
switch (val) {
case 0:
snprintf (encoding, sizeof (encoding), "Unexpected packing/encoding missing (i.e. 0)");
break;
case 1:
case 2:
case 4:
case 8:
snprintf (encoding, sizeof (encoding), "%u bytes in and out", val);
break;
case DK_ENC_LL_DLD9:
snprintf (encoding, sizeof (encoding), "DLD9: 36bits on disk (little endian order) to 64bits in memory");
break;
case DK_ENC_LL_DBD9:
snprintf (encoding, sizeof (encoding), "DBD9: 36bits on disk (big endian order) to 64bits in memory");
break;
default:
snprintf (encoding, sizeof (encoding), "Unexpected encoding: %u bits on disk packed %s endian order to %u bits in memory %s endian order",
(val >> DK_ENC_XFR_IN) & 0x7F, ((val >> DK_ENC_XFR_IN) & DK_ENC_X_LSB) ? "little" : "big",
(val >> DK_ENC_XFR_OUT) & 0x7F, ((val >> DK_ENC_XFR_OUT) & DK_ENC_X_LSB) ? "little" : "big");
break;
}
return encoding;
}
/* 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_encode_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;
}
t_stat sim_disk_set_all_noautosize (int32 flag, CONST char *cptr)
{
DEVICE *dptr;
uint32 dev, unit, count = 0;
int32 saved_sim_show_message = sim_show_message;
sim_show_message = FALSE;
for (dev = 0; (dptr = sim_devices[dev]) != NULL; dev++) {
t_bool device_disabled = ((dptr->flags & DEV_DIS) != 0);
if ((DEV_TYPE (dptr) != DEV_DISK) &&
(DEV_TYPE (dptr) != DEV_SCSI)) /* If not a sim_disk device? */
continue; /* skip this device */
if (device_disabled)
dptr->flags &= ~DEV_DIS; /* Temporarily enable device */
++count;
for (unit = 0; unit < dptr->numunits; unit++) {
char cmd[CBUFSIZE];
t_bool unit_disabled = ((dptr->units[unit].flags & UNIT_DIS) != 0);
if (unit_disabled && /* disabled and */
((dptr->units[unit].flags & UNIT_DISABLE) == 0)) /* can't be enabled? */
continue; /* Not a drive unit, so skip. */
if (unit_disabled)
dptr->units[unit].flags &= ~UNIT_DIS; /* Temporarily enable unit */
sprintf (cmd, "%s %sAUTOSIZE", sim_uname (&dptr->units[unit]), (flag != 0) ? "NO" : "");
set_cmd (0, cmd);
if (unit_disabled)
dptr->units[unit].flags |= ~UNIT_DIS; /* leave unit disabled again */
}
if (device_disabled)
dptr->flags |= DEV_DIS; /* leave device the way we found it */
}
sim_show_message = saved_sim_show_message;
if (count == 0)
return sim_messagef (SCPE_ARG, "No disk devices support autosizing\n");
return SCPE_OK;
}
/* Set disk autosize */
t_stat sim_disk_set_autosize (UNIT *uptr, int32 val, CONST char *cptr, void *desc)
{
if (uptr == NULL)
return SCPE_IERR;
if ((uptr->drvtyp != NULL) &&
(DRVFL_GET_IFTYPE(uptr->drvtyp) == DRVFL_TYPE_SCSI) &&
(uptr->drvtyp->devtype == SCSI_TAPE))
return sim_messagef (SCPE_NOFNC, "%s: Autosizing Tapes is not supported\n", sim_uname (uptr));
if (cptr != NULL)
return sim_messagef (SCPE_ARG, "%s: Unexpected autosize argument: %s\n", sim_uname (uptr), cptr);
if (((uptr->flags & UNIT_ATT) != 0) && ((uptr->drvtyp == NULL) || ((uptr->drvtyp->flags & DRVFL_DETAUTO) == 0)))
return sim_messagef (SCPE_ALATT, "%s: Disk already attached, autosizing not changed\n", sim_uname (uptr));
if (val ^ ((uptr->flags & DKUF_NOAUTOSIZE) != 0))
return SCPE_OK;
if (val)
uptr->flags &= ~DKUF_NOAUTOSIZE;
else
uptr->flags |= DKUF_NOAUTOSIZE;
return SCPE_OK;
}
/* Show disk autosize */
t_stat sim_disk_show_autosize (FILE *st, UNIT *uptr, int32 val, CONST void *desc)
{
if ((uptr->drvtyp != NULL) &&
(DRVFL_GET_IFTYPE(uptr->drvtyp) == DRVFL_TYPE_SCSI) &&
(uptr->drvtyp->devtype == SCSI_TAPE))
return SCPE_NOFNC;
fprintf (st, "%sautosize", ((uptr->flags & DKUF_NOAUTOSIZE) != 0) ? "no" : "");
return SCPE_OK;
}
t_stat sim_disk_set_all_autozap (int32 flag, CONST char *cptr)
{
DEVICE *dptr;
uint32 dev, unit, count = 0;
int32 saved_sim_show_message = sim_show_message;
sim_show_message = FALSE;
for (dev = 0; (dptr = sim_devices[dev]) != NULL; dev++) {
t_bool device_disabled = ((dptr->flags & DEV_DIS) != 0);
if ((DEV_TYPE (dptr) != DEV_DISK) &&
(DEV_TYPE (dptr) != DEV_SCSI)) /* If not a sim_disk device? */
continue; /* skip this device */
if (device_disabled)
dptr->flags &= ~DEV_DIS; /* Temporarily enable device */
++count;
for (unit = 0; unit < dptr->numunits; unit++) {
char cmd[CBUFSIZE];
t_bool unit_disabled = ((dptr->units[unit].flags & UNIT_DIS) != 0);
if (unit_disabled && /* disabled and */
((dptr->units[unit].flags & UNIT_DISABLE) == 0)) /* can't be enabled? */
continue; /* Not a drive unit, so skip. */
if (unit_disabled)
dptr->units[unit].flags &= ~UNIT_DIS; /* Temporarily enable unit */
sprintf (cmd, "%s %sAUTOZAP", sim_uname (&dptr->units[unit]), (flag != 0) ? "" : "NO");
set_cmd (0, cmd);
if (unit_disabled)
dptr->units[unit].flags |= ~UNIT_DIS; /* leave unit disabled again */
}
if (device_disabled)
dptr->flags |= DEV_DIS; /* leave device the way we found it */
}
sim_show_message = saved_sim_show_message;
if (count == 0)
return sim_messagef (SCPE_ARG, "No disk devices in the %s simulator support autozap\n", sim_name);
return SCPE_OK;
}
/* Set disk autozap */
t_stat sim_disk_set_autozap (UNIT *uptr, int32 val, CONST char *cptr, void *desc)
{
if (uptr == NULL)
return SCPE_IERR;
if ((uptr->drvtyp != NULL) &&
(DRVFL_GET_IFTYPE(uptr->drvtyp) == DRVFL_TYPE_SCSI) &&
(uptr->drvtyp->devtype == SCSI_TAPE))
return sim_messagef (SCPE_NOFNC, "%s: Autozapping Tapes is not supported\n", sim_uname (uptr));
if (cptr != NULL)
return sim_messagef (SCPE_ARG, "%s: Unexpected autozap argument: %s\n", sim_uname (uptr), cptr);
if (val ^ ((uptr->flags & DKUF_AUTOZAP) == 0))
return SCPE_OK;
if (val)
uptr->flags |= DKUF_AUTOZAP;
else
uptr->flags &= ~DKUF_AUTOZAP;
return SCPE_OK;
}
/* Show disk autozap */
t_stat sim_disk_show_autozap (FILE *st, UNIT *uptr, int32 val, CONST void *desc)
{
if ((uptr->drvtyp != NULL) &&
(DRVFL_GET_IFTYPE(uptr->drvtyp) == DRVFL_TYPE_SCSI) &&
(uptr->drvtyp->devtype == SCSI_TAPE))
return SCPE_NOFNC;
fprintf (st, "%sautozap", ((uptr->flags & DKUF_AUTOZAP) != 0) ? "" : "no" );
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;
uint8 *tbuf = NULL;
uint8 *rbuf;
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 */
if (ctx->xfer_encode_size > DK_ENC_LONGLONG) {
tbuf = (uint8*) malloc (ctx->sector_size * sects);
if (tbuf == NULL)
return SCPE_MEM;
rbuf = tbuf;
}
else
rbuf = buf;
switch (f) { /* case on format */
case DKUF_F_STD: /* SIMH format */
r = _sim_disk_rdsect (uptr, lba, rbuf, &sread, sects);
break;
case DKUF_F_VHD: /* VHD format */
r = sim_vhd_disk_rdsect (uptr, lba, rbuf, &sread, sects);
break;
case DKUF_F_RAW: /* Raw Physical Disk Access */
r = sim_os_disk_rdsect (uptr, lba, rbuf, &sread, sects);
break;
default:
free (tbuf);
return SCPE_NOFNC;
}
if (sectsread)
*sectsread = sread;
if (ctx->xfer_encode_size > DK_ENC_LONGLONG) {
uint32 sbits = (ctx->xfer_encode_size >> DK_ENC_XFR_IN) & 0x7F;
t_bool sLSB = (((ctx->xfer_encode_size >> DK_ENC_XFR_IN) & DK_ENC_X_LSB) != 0);
uint32 dbits = (ctx->xfer_encode_size >> DK_ENC_XFR_OUT) & 0x7F;
t_bool dLSB = (((ctx->xfer_encode_size >> DK_ENC_XFR_OUT) & DK_ENC_X_LSB) != 0);
uint32 scount = ((sread * ctx->sector_size) * 8) / sbits;
sim_buf_pack_unpack (rbuf, /* source buffer pointer */
buf, /* destination buffer pointer */
sbits, /* source buffer element size in bits */
sLSB, /* source numbered using LSB ordering */
scount, /* count of source elements */
dbits, /* interesting bits of each destination element */
dLSB); /* destination numbered using LSB ordering */
}
else
sim_buf_swap_data (buf, ctx->xfer_encode_size, (sread * ctx->sector_size) / ctx->xfer_encode_size);
free (tbuf);
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));
sread = (bytesread - soffset) / ctx->sector_size;
if (sread > sects)
sread = sects;
if (sectsread)
*sectsread = sread;
if (ctx->xfer_encode_size > DK_ENC_LONGLONG) {
uint32 sbits = (ctx->xfer_encode_size >> DK_ENC_XFR_IN) & 0x7F;
t_bool sLSB = (((ctx->xfer_encode_size >> DK_ENC_XFR_IN) & DK_ENC_X_LSB) != 0);
uint32 dbits = (ctx->xfer_encode_size >> DK_ENC_XFR_OUT) & 0x7F;
t_bool dLSB = (((ctx->xfer_encode_size >> DK_ENC_XFR_OUT) & DK_ENC_X_LSB) != 0);
uint32 scount = ((sread * ctx->sector_size) * 8) / sbits;
sim_buf_pack_unpack (tbuf + soffset, /* source buffer pointer */
buf, /* destination buffer pointer */
sbits, /* source buffer element size in bits */
sLSB, /* source numbered using LSB ordering */
scount, /* count of source elements */
dbits, /* interesting bits of each destination element */
dLSB); /* destination numbered using LSB ordering */
}
else
sim_buf_copy_swapped (buf, tbuf + soffset, ctx->xfer_encode_size, (sread * ctx->sector_size) / ctx->xfer_encode_size);
free (tbuf);
return r;
}