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