Imported Upstream version 1.2.16rel
[debian/mtx] / mam2debug2.c
1 /* Mammoth 2 Debug Buffer Dumper
2    Copyright 2000 Enhanced Software Technologies Inc.
3
4 $Date: 2001/06/05 17:10:21 $
5 $Revision: 1.1.1.1 $
6
7    Written by Eric Lee Green <eric@estinc.com>
8    Released under the terms of the GNU General Public License v2 or
9     above.
10
11    This is an example of how to use the mtx library file 'mtxl.c' to
12    do a special-purpose task -- dump the Mammoth2 debug buffer, in this case. 
13    Note that this debug buffer is 1M-4M in size, thus may overwhelm the
14    SCSI generic subsystem on some supported platforms...
15
16    syntax:
17
18    mam2debug generic-filename output-filename.
19
20
21 */
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include "mtx.h"
29 #include "mtxl.h"
30
31 /* This is a TOTALLY UNDOCUMENTED feature to read the debug data buffer
32  * in an Exabyte Mammoth II and dump it to a file:
33  */
34
35 static RequestSense_T *DumpM2DebugBuff(DEVICE_TYPE MediumChangerFD, int outfile) 
36 {
37   RequestSense_T *RequestSense = xmalloc(sizeof(RequestSense_T));
38   CDB_T CDB;
39
40   unsigned char *databuffer;
41   unsigned char buff_descriptor[4];
42   int numbytes;
43   int testbytes;
44
45   CDB[0]=0x3c; /* command. */ 
46   CDB[1]=0x03;  /* mode - read buff_descriptor! */ 
47   CDB[2]=0x00;    /* page -- data. */ 
48   CDB[3]=0;   /* offset. */
49   CDB[4]=0;
50   CDB[5]=0;  
51   CDB[6]=0;    /* length. */
52   CDB[7]=0;
53   CDB[8]=4;   /* the descriptor is 4 long. */ 
54   CDB[9]=0;
55   
56   if ((testbytes=SCSI_ExecuteCommand(MediumChangerFD, Input, &CDB, 10,
57                           buff_descriptor, 4, RequestSense)) != 0){
58     fprintf(stderr,"mam2debug: could not read buff_descriptor. [%d]\n",testbytes);
59     return RequestSense;  /* couldn't do it. */ 
60   }
61
62   /* okay, read numbytes: */
63   numbytes=(buff_descriptor[1]<<16) + (buff_descriptor[2]<<8) + buff_descriptor[3];
64   databuffer=(unsigned char *) xmalloc(numbytes+1000000); /* see if this helps :-(. */
65   CDB[6]=buff_descriptor[1];
66   CDB[7]=buff_descriptor[2];
67   CDB[8]=buff_descriptor[3];
68
69   CDB[1]=0x02; /* mode -- read buffer! */
70   
71   if (SCSI_ExecuteCommand(MediumChangerFD, Input, &CDB, 10,
72                           databuffer, numbytes, RequestSense) != 0){
73     fprintf(stderr,"mam2debug: could not read buffer.\n");
74     free(databuffer);
75     return RequestSense;  /* couldn't do it. */ 
76   }
77   
78   write(outfile,databuffer,numbytes);
79   close(outfile);
80   free(databuffer);
81   free(RequestSense);
82   return NULL;  /* okay! */
83 }
84
85 static void usage(void) {
86   fprintf(stderr,"Usage: mam2debug scsi-generic-file output-file-name\n");
87   exit(1);
88 }
89
90 /* Now for the actual main() routine: */
91
92 int main(int argc,char** argv) {
93   DEVICE_TYPE changer_fd;
94   static RequestSense_T *result;
95   int outfile;
96
97   if (argc != 3) {
98     usage();
99   }
100   
101   changer_fd=SCSI_OpenDevice(argv[1]);
102   
103   if (changer_fd <= 0) {
104     fprintf(stderr,"Could not open input device\n");
105     usage();
106   }
107
108   outfile=open(argv[2],O_CREAT|O_WRONLY);
109   if (outfile <=0) {
110     fprintf(stderr,"Could not open output file\n");
111     usage();
112   }
113
114   result=DumpM2DebugBuff(changer_fd, outfile);
115
116   if (result) {
117     PrintRequestSense(result);
118     exit(1);
119   }
120
121   exit(0);
122 }
123