-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevtest.c
5078 lines (4590 loc) · 152 KB
/
devtest.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
/*
* devtest
* -------
* Utility to test AmigaOS block devices (trackdisk.device, scsi.device, etc).
*
* Copyright 2024 Chris Hooper. This program and source may be used
* and distributed freely, for any purpose which benefits the Amiga
* community. Commercial use of the binary, source, or algorithms requires
* prior written approval from Chris Hooper <[email protected]>.
* All redistributions must retain this Copyright notice.
*
* DISCLAIMER: THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTY.
* THE AUTHOR ASSUMES NO LIABILITY FOR ANY DAMAGE ARISING OUT OF THE USE
* OR MISUSE OF THIS UTILITY OR INFORMATION REPORTED BY THIS UTILITY.
*/
const char *version = "\0$VER: devtest " VER " ("__DATE__") © Chris Hooper";
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <exec/types.h>
#include <devices/trackdisk.h>
#include <devices/scsidisk.h>
#include <libraries/dos.h>
#include <dos/dosextens.h>
#include <dos/filehandler.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <exec/io.h>
#include <exec/errors.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/memory.h>
#include <libraries/configregs.h>
#ifdef _DCC
#define CDERR_BadDataType 36
#define CDERR_InvalidState 37
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef struct { unsigned long hi; unsigned long lo; } uint64_t;
#define __packed
#else
#include <devices/cd.h>
#include <inline/timer.h>
#include <inline/exec.h>
#include <inline/dos.h>
#include <inttypes.h>
struct ExecBase *SysBase;
struct ExecBase *DOSBase;
struct Device *TimerBase;
#endif
/*
* ULONG has changed from NDK 3.9 to NDK 3.2.
* However, PRI*32 did not. What is the right way to implement this?
*/
#if INCLUDE_VERSION < 47
#undef PRIu32
#define PRIu32 "lu"
#undef PRId32
#define PRId32 "ld"
#undef PRIx32
#define PRIx32 "lx"
#endif
/*
* Trackdisk-64 enhanced commands
*
* Check before defining. AmigaOS 3.2 NDK provides these in trackdisk.h
*/
#ifndef TD_READ64
#define TD_READ64 24 // Read at 64-bit offset
#endif
#ifndef TD_WRITE64
#define TD_WRITE64 25 // Write at 64-bit offset
#endif
#ifndef TD_SEEK64
#define TD_SEEK64 26 // Seek to 64-bit offset
#endif
#ifndef TD_FORMAT64
#define TD_FORMAT64 27 // Format (write) at 64-bit offset
#endif
#define BIT(x) (1U << (x))
/* Internal flag to indicate IOF_QUICK should be set for command */
#define CMD_FLAG_NOT_QUICK BIT(14)
/* NSD commands */
#define NSCMD_DEVICEQUERY 0x4000
#define NSCMD_TD_READ64 0xC000
#define NSCMD_TD_WRITE64 0xC001
#define NSCMD_TD_SEEK64 0xC002
#define NSCMD_TD_FORMAT64 0xC003
#define NSCMD_ETD_READ64 0xE000
#define NSCMD_ETD_WRITE64 0xE001
#define NSCMD_ETD_SEEK64 0xE002
#define NSCMD_ETD_FORMAT64 0xE003
#define NSDEVTYPE_TRACKDISK 5 // Trackdisk-like block storage device
#define ARRAY_SIZE(x) ((sizeof (x) / sizeof ((x)[0])))
#define BUFSIZE 8192
#define RAWBUFSIZE 16384
#define MEMTYPE_ANY 0
#define MEMTYPE_CHIP 1
#define MEMTYPE_FAST 2
#define MEMTYPE_24BIT 3
#define MEMTYPE_ZORRO 4
#define MEMTYPE_ACCEL 5
#define MEMTYPE_COPROC 6
#define MEMTYPE_MB 7
#define MEMTYPE_MAX 7
#define SSD_SENSE_KEY(x) ((x[2]) & 0x0f)
#define SSD_SENSE_ASC(x) (x[12])
#define SSD_SENSE_ASCQ(x) (x[13])
#define SKEY_NOT_READY 0x02
#define SCSI_TEST_UNIT_READY 0x00
struct scsi_test_unit_ready {
uint8_t opcode;
uint8_t byte2;
uint8_t reserved[3];
uint8_t control;
};
#define INQUIRY 0x12
typedef struct scsi_inquiry_data {
uint8_t device;
#define SID_TYPE 0x1f /* device type mask */
#define SID_QUAL 0xe0 /* device qualifier mask */
#define SID_QUAL_LU_NOTPRESENT 0x20 /* logical unit not present */
uint8_t dev_qual2;
uint8_t version;
uint8_t response_format;
uint8_t additional_length;
uint8_t flags1;
uint8_t flags2;
#define SID_REMOVABLE 0x80
uint8_t flags3;
#define SID_SftRe 0x01
#define SID_CmdQue 0x02
#define SID_Linked 0x08
#define SID_Sync 0x10
#define SID_WBus16 0x20
#define SID_WBus32 0x40
#define SID_RelAdr 0x80
#define SID_REMOVABLE 0x80
uint8_t vendor[8];
uint8_t product[16];
uint8_t revision[4];
uint8_t vendor_specific[20];
uint8_t flags4;
uint8_t reserved;
uint8_t version_descriptor[8][2];
} __packed scsi_inquiry_data_t; // 74 bytes
#define READ_CAPACITY_10 0x25
typedef struct scsi_read_capacity_10_data {
uint8_t addr[4];
uint8_t length[4];
} __packed scsi_read_capacity_10_data_t;
#define SERVICE_ACTION_IN 0x9e
typedef struct scsi_read_capacity_16_data {
uint8_t addr[8];
uint8_t length[4];
uint8_t byte13;
#define SRC16D_PROT_EN 0x01
#define SRC16D_RTO_EN 0x02
uint8_t reserved[19];
} __packed scsi_read_capacity_16_data_t;
typedef struct scsi_generic {
uint8_t opcode;
uint8_t bytes[15];
} __packed scsi_generic_t;
#define SCSI_READ_6_COMMAND 0x08
#define SCSI_WRITE_6_COMMAND 0x0a
#define SCSI_WRITE_10_COMMAND 0x2a
#define SCSI_WRITE_12_COMMAND 0xaa
#define SCSI_WRITE_16_COMMAND 0x8a
typedef struct scsi_rw_6 {
uint8_t opcode;
uint8_t addr[3];
uint8_t length;
uint8_t control;
} __packed scsi_rw_6_t;
#define MODE_SENSE_6 0x1a
#define SCSI_MODE_PAGES_BUFSIZE 255
#define DISK_PGCODE 0x3F /* only 6 bits valid */
typedef struct scsi_generic_mode_page {
uint8_t pg_code; /* page code */
uint8_t pg_length; /* page length in bytes */
uint8_t pg_bytes[253]; /* this number of bytes or less */
} scsi_generic_mode_page_t;
struct scsi_mode_sense_6 {
uint8_t opcode;
uint8_t byte2;
#define SMS_DBD 0x08 /* disable block descriptors */
uint8_t page;
#define SMS_PAGE_MASK 0x3f
#define SMS_PCTRL_MASK 0xc0
#define SMS_PCTRL_CURRENT 0x00
#define SMS_PCTRL_CHANGEABLE 0x40
#define SMS_PCTRL_DEFAULT 0x80
#define SMS_PCTRL_SAVED 0xc0
uint8_t reserved;
uint8_t length;
uint8_t control;
};
typedef struct NSDeviceQueryResult
{
/*
* Standard information
*/
ULONG DevQueryFormat; /* this is type 0 */
ULONG SizeAvailable; /* bytes available */
/*
* Common information (READ ONLY!)
*/
UWORD DeviceType; /* what the device does */
UWORD DeviceSubType; /* depends on the main type */
UWORD *SupportedCommands; /* 0 terminated list of cmd's */
/* May be extended in the future! Check SizeAvailable! */
} NSDeviceQueryResult_t;
typedef unsigned int uint;
typedef struct {
char alias[15];
uint8_t flags;
uint64_t mask;
char name[24];
const char * const desc;
const char * const arg_help;
} test_cmds_t;
typedef struct {
uint8_t arg_count;
uint32_t arg[4];
} args_t;
static int do_read_cmd(struct IOExtTD *tio, uint64_t offset, uint len,
void *buf, int nsd);
#define TEST_MAX_CMDS 32
BOOL __check_abort_enabled = 0; // Disable gcc clib2 ^C break handling
static int g_verbose = 0; // Verbose output
static int g_changenum = 0; // Current device media change number
static uint g_sector_size = 512; // Updated when getting drive geometry
static uint64_t g_devsize = 0; // Device total size in bytes
static uint g_lun = 0; // Target LUN to access
static uint g_has_nsd = 0; // Device driver supports NSD
static uint8_t **g_buf; // Saved data buffers for certain tests
static char *g_devname = NULL; // Device name
static uint g_unitno; // Device unit and LUN
static uint g_e_freq; // Frequency of eclock (715909 tps)
static UWORD g_sense_length; // Length of returned sense data (if any)
static UBYTE g_sense_data[255]; // Sense data buffer
static uint8_t *g_ibuf[5]; // Integrity test buffers
static uint8_t *g_align[5]; // Integrity test buffers (aligned)
static UBYTE mem_skip_alloc = 0; // Skip memory allocate
static uint32_t memtype = MEMTYPE_ANY; // Memory type
static uint64_t test_cmd_mask[32];
static args_t test_cmd_args[TEST_MAX_CMDS];
static args_t *cur_test_args = NULL;
static uint flag_destructive = 0;
static BOOL
is_user_abort(void)
{
if (SetSignal(0, 0) & SIGBREAKF_CTRL_C)
return (1);
return (0);
}
static int
open_device(struct IOExtTD *tio)
{
uint flags = 0;
if (strcmp(g_devname, "trackdisk.device") == 0)
flags = TDF_ALLOW_NON_3_5;
return (OpenDevice(g_devname, g_unitno, (struct IORequest *) tio, flags));
}
static void
close_device(struct IOExtTD *tio)
{
CloseDevice((struct IORequest *)tio);
}
static void
usage(void)
{
printf("%s\n\n"
"usage: devtest <options> <x.device> <unit>\n"
" -b benchmark device performance "
"[-bb tests latency]\n"
" -c <cmd> test a specific device driver request\n"
" -d also do destructive operations (write)\n"
// Undocumented: -dd skips save/restore of data with -i integrity test
" -g test drive geometry\n"
" -h display help\n"
" -i <tsize>[,<align>] data integrity test (destructive) "
"[-i=rand -ii=addr -iii=patt]\n"
" -l <loops> run multiple times\n"
" -m <addr> "
"use specific memory (Chip Fast Zorro MB Accel -=list)\n"
" -mm <addr> "
"use specific address without allocation by OS\n"
" -o test open/close\n"
" -p probe SCSI bus for devices\n"
" -t test all packet types (basic, TD64, NSD) "
"[-tt = more]\n",
version + 7);
}
typedef struct {
int errcode;
const char *const errstr;
} err_to_str_t;
static const err_to_str_t err_to_str[] = {
{ IOERR_OPENFAIL, "IOERR_OPENFAIL" }, // -1
{ IOERR_ABORTED, "IOERR_ABORTED" }, // -2
{ IOERR_NOCMD, "IOERR_NOCMD (unsupported)" }, // -3
{ IOERR_BADLENGTH, "IOERR_BADLENGTH" }, // -4
{ IOERR_BADADDRESS, "IOERR_BADADDRESS" }, // -5
{ IOERR_UNITBUSY, "IOERR_UNITBUSY" }, // -6
{ IOERR_SELFTEST, "IOERR_SELFTEST" }, // -7
{ TDERR_NotSpecified, "TDERR_NotSpecified" }, // 20
{ TDERR_NoSecHdr, "TDERR_NoSecHdr" }, // 21
{ TDERR_BadSecPreamble, "TDERR_BadSecPreamble" }, // 22
{ TDERR_BadSecID, "TDERR_BadSecID" }, // 23
{ TDERR_BadHdrSum, "TDERR_BadHdrSum" }, // 24
{ TDERR_BadSecSum, "TDERR_BadSecSum" }, // 25
{ TDERR_TooFewSecs, "TDERR_TooFewSecs" }, // 26
{ TDERR_BadSecHdr, "TDERR_BadSecHdr" }, // 27
{ TDERR_WriteProt, "TDERR_WriteProt" }, // 28
{ TDERR_DiskChanged, "TDERR_DiskChanged" }, // 29
{ TDERR_SeekError, "TDERR_SeekError" }, // 30
{ TDERR_NoMem, "TDERR_NoMem" }, // 31
{ TDERR_BadUnitNum, "TDERR_BadUnitNum" }, // 32
{ TDERR_BadDriveType, "TDERR_BadDriveType" }, // 33
{ TDERR_DriveInUse, "TDERR_DriveInUse" }, // 34
{ TDERR_PostReset, "TDERR_PostReset" }, // 35
{ CDERR_BadDataType, "CDERR_BadDataType" }, // 36
{ CDERR_InvalidState, "CDERR_InvalidState" }, // 37
{ HFERR_SelfUnit, "HFERR_SelfUnit" }, // 40
{ HFERR_DMA, "HFERR_DMA" }, // 41
{ HFERR_Phase, "HFERR_Phase" }, // 42
{ HFERR_Parity, "HFERR_Parity" }, // 42
{ HFERR_SelTimeout, "HFERR_SelTimeout" }, // 44
{ HFERR_BadStatus, "HFERR_BadStatus" }, // 45
{ 46, "ERROR_INQUIRY_FAILED" }, // 46
{ 47, "ERROR_TIMEOUT" }, // 47
{ 48, "ERROR_BUS_RESET" }, // 48
{ 49, "ERROR_TRY_AGAIN" }, // 49
{ HFERR_NoBoard, "HFERR_NoBoard" }, // 50
{ 51, "ERROR_BAD_BOARD" }, // 51
{ 52, "ERROR_SENSE_CODE" }, // 52
/* The following unfortunately overlap several error codes above */
{ EACCES, "EACCES" }, // 2
{ EIO, "EIO" }, // 5
{ ENOMEM, "ENOMEM" }, // 12
{ EBUSY, "EBUSY" }, // 16
{ ENODEV, "ENODEV" }, // 19
{ EINVAL, "EINVAL" }, // 22
{ ENOSPC, "ENOSPC" }, // 28
{ EROFS, "EROFS" }, // 30
{ EAGAIN, "EAGAIN" }, // 35
};
static const char *const floppy_types[] = {
"Unknown",
"3.5\"",
"5.25\"",
"3.5\" 150RPM",
};
static const char *
floppy_type_string(uint dtype)
{
if ((dtype > 0) && (dtype <= DRIVE3_5_150RPM))
return (floppy_types[dtype]);
return ("Unknown");
}
static void
print_test_name(const char *name)
{
printf("%-19s", name);
fflush(stdout);
fflush(NULL); // gcc bug? fflush(stdout) doesn't seem to work
}
static void
print_ltest_name(const char *name)
{
printf("%-28s", name);
fflush(stdout);
fflush(NULL); // gcc bug? fflush(stdout) doesn't seem to work
}
static void
print_fail(int rc)
{
size_t i;
printf("Fail %d", rc);
for (i = 0; i < ARRAY_SIZE(err_to_str); i++) {
if (err_to_str[i].errcode == rc) {
printf(" %s", err_to_str[i].errstr);
break;
}
}
}
static void
print_fail_nl(int rc)
{
if (rc == 0)
printf("Success");
else
print_fail(rc);
printf("\n");
}
static char *
llu_to_str(uint64_t value)
{
unsigned int high = value / 1000000000;
static char buf[32];
if (high > 0) {
sprintf(buf, "%u%09u",
high, (unsigned int) (value - high * 1000000));
} else {
sprintf(buf, "%u", (unsigned int) value);
}
return (buf);
}
APTR
AllocMemType(ULONG byteSize, uint32_t memtype)
{
APTR addr = NULL;
/* Don't bother with memory allocate */
if (mem_skip_alloc)
return ((APTR) memtype);
switch (memtype) {
case 0:
/* Highest priority (usually fast) memory */
addr = AllocMem(byteSize, MEMF_PUBLIC | MEMF_ANY);
break;
case MEMTYPE_CHIP:
/* Chip memory */
addr = AllocMem(byteSize, MEMF_PUBLIC | MEMF_CHIP);
break;
case MEMTYPE_FAST:
/* Fast memory */
addr = AllocMem(byteSize, MEMF_PUBLIC | MEMF_FAST);
break;
case MEMTYPE_24BIT:
/* 24-bit memory */
addr = AllocMem(byteSize, MEMF_PUBLIC | MEMF_24BITDMA);
break;
case MEMTYPE_ZORRO:
case MEMTYPE_COPROC:
case MEMTYPE_MB:
case MEMTYPE_ACCEL: {
/*
* Zorro or accelerator memory -- walk memory list and find
* chunk in that memory space
*/
struct ExecBase *eb = SysBase;
struct MemHeader *mem;
struct MemChunk *chunk;
uint32_t chunksize = 0;
APTR chunkaddr = NULL;
Forbid();
for (mem = (struct MemHeader *)eb->MemList.lh_Head;
mem->mh_Node.ln_Succ != NULL;
mem = (struct MemHeader *)mem->mh_Node.ln_Succ) {
uint32_t size = (uint8_t *) mem->mh_Upper - (uint8_t *) mem;
if ((memtype == MEMTYPE_MB) &&
(((uint32_t) mem < 0x04000000) ||
((uint32_t) mem >= 0x08000000))) {
/* Not in Motherboard address range (A3000/A4000 fastmem) */
continue;
}
if ((memtype == MEMTYPE_COPROC) &&
(((uint32_t) mem < 0x08000000) ||
((uint32_t) mem >= 0x10000000))) {
/* Not in Coprocessor address range */
continue;
}
if ((memtype == MEMTYPE_ZORRO) &&
((((uint32_t) mem < E_MEMORYBASE) ||
((uint32_t) mem > E_MEMORYBASE + E_MEMORYSIZE)) &&
(((uint32_t) mem < 0x10000000) || // EZ3_CONFIGAREA
((uint32_t) mem >= 0x80000000)))) { // EZ3_CONFIGAREAEND
/* Not in Zorro II or Zorro III address range */
continue;
}
if ((memtype == MEMTYPE_ACCEL) &&
(((uint32_t) mem < 0x80000000) ||
((uint32_t) mem >= 0xe0000000))) {
/* Not in Accelerator address range */
continue;
}
/* Find the smallest chunk where this allocation will fit */
for (chunk = mem->mh_First; chunk != NULL;
chunk = chunk->mc_Next) {
uint32_t cursize = chunk->mc_Bytes;
if (((uint32_t) chunk < (uint32_t) mem) ||
((uint32_t) chunk > (uint32_t) mem + size) ||
((uint32_t) chunk + cursize > (uint32_t) mem + size)) {
break; // Memory list corrupt - quit now!
}
if (cursize >= byteSize) {
if ((chunkaddr != NULL) && (chunksize <= cursize))
continue;
chunkaddr = chunk;
chunksize = cursize;
}
}
}
if (chunkaddr != NULL)
addr = AllocAbs(byteSize, chunkaddr);
Permit();
break;
}
default:
/* Allocate user-specified address */
if (memtype > MEMTYPE_MAX)
addr = AllocAbs(byteSize, (APTR) memtype);
else
addr = NULL;
break;
}
if (g_verbose)
printf("Alloc %p\n", addr);
return (addr);
}
void
FreeMemType(APTR addr, ULONG byteSize)
{
if (mem_skip_alloc)
return; /* Memory was not allocated through the OS */
FreeMem(addr, byteSize);
}
static void
setup_scsidirect_cmd(struct SCSICmd *scmd, scsi_generic_t *cmd, uint cmdlen,
void *res, uint reslen)
{
memset(scmd, 0, sizeof (*scmd));
scmd->scsi_Data = (UWORD *) res;
scmd->scsi_Length = reslen;
// scmd.scsi_Actual = 0;
scmd->scsi_Command = (UBYTE *) cmd;
scmd->scsi_CmdLength = cmdlen; // sizeof (cmd);
// scmd.scsi_CmdActual = 0;
if ((cmd->opcode == SCSI_WRITE_6_COMMAND) ||
(cmd->opcode == SCSI_WRITE_10_COMMAND) ||
(cmd->opcode == SCSI_WRITE_12_COMMAND) ||
(cmd->opcode == SCSI_WRITE_16_COMMAND)) {
scmd->scsi_Flags = SCSIF_WRITE;
} else {
scmd->scsi_Flags = SCSIF_READ;
}
scmd->scsi_Flags |= SCSIF_AUTOSENSE;
// scmd.scsi_Status = 0;
scmd->scsi_SenseData = g_sense_data;
scmd->scsi_SenseLength = sizeof (g_sense_data);
// scmd->scsi_SenseActual = 0;
}
static int
do_trackdisk_inquiry(struct IOExtTD *tio, uint *floppytype, uint *numtracks)
{
int rc;
tio->iotd_Req.io_Command = TD_GETDRIVETYPE;
tio->iotd_Req.io_Actual = 0;
tio->iotd_Req.io_Offset = 0;
tio->iotd_Req.io_Length = 0;
tio->iotd_Req.io_Data = NULL;
tio->iotd_Req.io_Flags = 0;
tio->iotd_Req.io_Error = 0xa5;
rc = DoIO((struct IORequest *) tio);
if (rc == 0) {
*floppytype = tio->iotd_Req.io_Actual,
tio->iotd_Req.io_Command = TD_GETNUMTRACKS;
tio->iotd_Req.io_Actual = 0;
tio->iotd_Req.io_Offset = 0;
tio->iotd_Req.io_Length = 0;
tio->iotd_Req.io_Data = NULL;
tio->iotd_Req.io_Flags = 0;
tio->iotd_Req.io_Error = 0xa5;
rc = DoIO((struct IORequest *) tio);
if (rc == 0) {
*numtracks = tio->iotd_Req.io_Actual;
} else {
*numtracks = 0;
}
}
return (rc);
}
static int
do_scsidirect_cmd(struct IOExtTD *tio, scsi_generic_t *cmd, uint cmdlen,
uint reslen, void **resp)
{
int rc;
void *res;
struct SCSICmd scmd;
if (reslen > 0) {
res = AllocMemType(reslen, memtype);
if (res == NULL) {
printf(" AllocMem %x (%x) fail\n", reslen, memtype);
g_sense_length = 0;
return (ENOMEM);
}
memset(res, 0, reslen);
} else {
res = NULL;
}
setup_scsidirect_cmd(&scmd, cmd, cmdlen, res, reslen);
tio->iotd_Req.io_Command = HD_SCSICMD;
tio->iotd_Req.io_Length = sizeof (scmd);
tio->iotd_Req.io_Data = &scmd;
rc = DoIO((struct IORequest *) tio);
if (rc != 0) {
if (reslen != 0) {
FreeMemType(res, reslen);
res = NULL;
}
}
g_sense_length = scmd.scsi_SenseActual;
if (resp != NULL)
*resp = res;
return (rc);
}
static int
do_scsi_inquiry(struct IOExtTD *tio, uint unit, scsi_inquiry_data_t **inq)
{
scsi_generic_t cmd;
uint lun = unit / 10;
memset(&cmd, 0, sizeof (cmd));
cmd.opcode = INQUIRY;
cmd.bytes[0] = lun << 5;
cmd.bytes[1] = 0; // Page code
cmd.bytes[2] = 0;
cmd.bytes[3] = sizeof (scsi_inquiry_data_t);
cmd.bytes[4] = 0; // Control
return (do_scsidirect_cmd(tio, &cmd, 6, sizeof (**inq), (void **) inq));
}
static int
do_scsi_testunitready(struct IOExtTD *tio, uint lun)
{
scsi_generic_t cmd;
memset(&cmd, 0, sizeof (cmd));
cmd.opcode = SCSI_TEST_UNIT_READY;
cmd.bytes[0] = lun << 5;
return (do_scsidirect_cmd(tio, &cmd, 6, 0, NULL));
}
static int
do_scsi_read_capacity_10(struct IOExtTD *tio, uint lun,
scsi_read_capacity_10_data_t **cap)
{
scsi_generic_t cmd;
uint len = sizeof (scsi_read_capacity_10_data_t);
memset(&cmd, 0, sizeof (cmd));
cmd.opcode = READ_CAPACITY_10;
cmd.bytes[0] = lun << 5;
return (do_scsidirect_cmd(tio, &cmd, 10, len, (void **) cap));
}
#define SRC16_SERVICE_ACTION 0x10 // SCSI_READ_CAPACITY_16
static int
do_scsi_read_capacity_16(struct IOExtTD *tio,
scsi_read_capacity_16_data_t **cap)
{
scsi_generic_t cmd;
uint len = sizeof (scsi_read_capacity_16_data_t);
memset(&cmd, 0, sizeof (cmd));
cmd.opcode = SERVICE_ACTION_IN;
cmd.bytes[0] = SRC16_SERVICE_ACTION;
*(uint32_t *)&cmd.bytes[9] = len;
return (do_scsidirect_cmd(tio, &cmd, 16, len, (void **) cap));
}
static int
do_seek_capacity(struct IOExtTD *tio, uint64_t *sectors)
{
uint64_t offset = g_devsize / 2; // Starting point
uint64_t incdec = offset / 2;
uint64_t min_offset = 0;
uint64_t max_offset = 0xffffffffffffffff;
uint8_t *buf;
int rc = 0;
int double_mode = 1;
if (g_sector_size == 0)
g_sector_size = 512;
if (incdec == 0)
incdec = 1;
buf = (uint8_t *) AllocMemType(g_sector_size, memtype);
if (buf == NULL) {
printf(" AllocMem %x (%x) fail\n", g_sector_size, memtype);
return (1);
}
*sectors = 0;
while (incdec >= g_sector_size / 2) {
rc = do_read_cmd(tio, offset, g_sector_size, buf, g_has_nsd);
if (rc == 0) {
min_offset = offset;
if (double_mode) {
offset *= 2;
incdec = offset / 2;
} else {
if (offset + incdec >= max_offset)
incdec /= 2;
offset += incdec;
}
} else {
double_mode = 0;
max_offset = offset;
if (offset - incdec <= min_offset)
incdec /= 2;
offset -= incdec;
}
}
offset &= ~(g_sector_size - 1);
FreeMemType(buf, g_sector_size);
*sectors = min_offset / g_sector_size;
return (0);
}
#if 0
static uint32_t
_2btol(const uint8_t *bytes)
{
return ((bytes[0] << 8) |
bytes[1]);
}
#endif
static uint32_t
_3btol(const uint8_t *bytes)
{
return ((bytes[0] << 16) |
(bytes[1] << 8) |
bytes[2]);
}
#if 0
static uint32_t
_4btol(const uint8_t *bytes)
{
return (((uint32_t)bytes[0] << 24) |
((uint32_t)bytes[1] << 16) |
((uint32_t)bytes[2] << 8) |
(uint32_t)bytes[3]);
}
#endif
static uint64_t
_5btol(const uint8_t *bytes)
{
return (((uint64_t)bytes[0] << 32) |
((uint64_t)bytes[1] << 24) |
((uint64_t)bytes[2] << 16) |
((uint64_t)bytes[3] << 8) |
(uint64_t)bytes[4]);
}
#if 0
static uint64_t
_8btol(const uint8_t *bytes)
{
return (((uint64_t)bytes[0] << 56) |
((uint64_t)bytes[1] << 48) |
((uint64_t)bytes[2] << 40) |
((uint64_t)bytes[3] << 32) |
((uint64_t)bytes[4] << 24) |
((uint64_t)bytes[5] << 16) |
((uint64_t)bytes[6] << 8) |
(uint64_t)bytes[7]);
}
#endif
static int
scsi_read_mode_pages(struct IOExtTD *tio, uint8_t **res)
{
scsi_generic_t cmd;
int len = SCSI_MODE_PAGES_BUFSIZE;
#define SMS_PAGE_ALL_PAGES 0x3f
#define SMS_PAGE_SUBPAGES 0x00
#define SMS_PAGE_NO_SUBPAGES 0xff
#define SMS_DBD 0x08 /* disable block descriptors */
memset(&cmd, 0, sizeof (cmd));
cmd.opcode = MODE_SENSE_6;
cmd.bytes[0] = SMS_DBD;
cmd.bytes[1] = SMS_PAGE_ALL_PAGES;
cmd.bytes[2] = 0; // reserved
cmd.bytes[3] = len; // length
cmd.bytes[4] = 0; // control
return (do_scsidirect_cmd(tio, &cmd, 6, len, (void **) res));
}
static char *
trim_spaces(char *str, size_t len)
{
size_t olen = len;
char *ptr;
for (ptr = str; len > 0; ptr++, len--)
if (*ptr != ' ')
break;
if (len == 0) {
/* Completely empty name */
*str = '\0';
return (str);
} else {
memmove(str, ptr, len);
while ((len > 0) && (str[len - 1] == ' '))
len--;
if (len < olen) /* Is there space for a NIL character? */
str[len] = '\0';
return (str);
}
return (str);
}
static const char *
devtype_str(uint dtype)
{
static const char * const dt_list[] = {
"Disk", "Tape", "Printer", "Proc",
"Worm", "CDROM", "Scanner", "Optical",
"Changer", "Comm", "ASCIT81", "ASCIT82",
};
if (dtype < ARRAY_SIZE(dt_list))
return (dt_list[dtype]);
return ("Unknown");
}
static int
scsi_probe_unit(uint unit, struct IOExtTD *tio)
{
int rc;
int erc;
scsi_inquiry_data_t *inq_res;
rc = OpenDevice(g_devname, unit, (struct IORequest *) tio, 0);
if (rc == 0) {
printf("%3d", unit);
erc = do_scsi_inquiry(tio, unit, &inq_res);
if (erc == ENOMEM)
return (erc);
if (erc == 0) {
printf(" %-*.*s %-*.*s %-*.*s %-7s",
sizeof (inq_res->vendor),
sizeof (inq_res->vendor),
trim_spaces(inq_res->vendor, sizeof (inq_res->vendor)),
sizeof (inq_res->product),
sizeof (inq_res->product),
trim_spaces(inq_res->product, sizeof (inq_res->product)),
sizeof (inq_res->revision),
sizeof (inq_res->revision),
trim_spaces(inq_res->revision, sizeof (inq_res->revision)),
devtype_str((inq_res->device) & SID_TYPE));
FreeMemType(inq_res, sizeof (*inq_res));
} else {
uint floppytype;
uint tracks;
erc = do_trackdisk_inquiry(tio, &floppytype, &tracks);
if (erc == 0) {
printf(" Floppy %s %d tracks",
floppy_type_string(floppytype), tracks);
} else {
printf(" Unknown device type");
}
}
scsi_read_capacity_10_data_t *cap10;
erc = do_scsi_read_capacity_10(tio, unit, &cap10);
if ((erc == 0) && (cap10 != NULL)) {
uint ssize = *(uint32_t *) &cap10->length;
uint cap = (*(uint32_t *) &cap10->addr + 1) / 1000;
uint cap_c = 0; // KMGTPEZY
if (cap > 100000) {
cap /= 1000;
cap_c++;
}
cap *= ssize;
while (cap > 9999) {
cap /= 1000;
cap_c++;
}
printf("%5u %5u %cB", ssize, cap, "KMGTPEZY"[cap_c]);
FreeMemType(cap10, sizeof (*cap10));
}
printf("\n");
close_device(tio);
}
return (rc);
}
static int
scsi_probe(char *unitstr)
{
int rc = 0;
int found = 0;
int justunit = -1;
uint unit;
uint target;
uint lun;
struct IOExtTD *tio;
struct MsgPort *mp;
if ((unitstr != NULL) &&
(sscanf(unitstr, "%i", (int *) &unit) == 1)) {
justunit = unit;
}
mp = CreatePort(0, 0);
if (mp == NULL) {
printf("Failed to create message port\n");
return (1);
}
tio = (struct IOExtTD *) CreateExtIO(mp, sizeof (struct IOExtTD));
if (tio == NULL) {
printf("Failed to create tio struct\n");
rc = 1;
goto extio_fail;
}
for (target = 0; target < 8; target++) {
for (lun = 0; lun < 8; lun++) {
unit = target + lun * 10;
if ((justunit != -1) && (unit != (uint) justunit))