summaryrefslogtreecommitdiff
path: root/mclasserase.c
blob: fb610eb6f0dd6155df54ddac4bdc60c660b25a73 (plain)
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
/*  Copyright 2003 Stefan Feuz, Lukas Meyer, Thomas Locher
 *  Copyright 2004,2006,2007,2009 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Filename:
 *    mclasserase.c
 *
 * Original Creation Date:
 *    05.III.2003
 *
 * Copyright:
 *    GPL
 *
 * Programmer:
 *    Stefan Feuz, Lukas Meyer, Thomas Locher
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "vfat.h"
#include "mainloop.h"
#include "fsP.h"

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include "file.h"

#include <unistd.h>
#include <stdio.h>

/**
 * Prints the Usage Message to STDOUT<br>
 *
 * @author  stefan feuz<br>
 *          stefan.feuz@ruag.com
 *
 * @param   n.a.
 *
 * @returns n.a.
 *
 */
static void usage(int ret) NORETURN;
static void usage(int ret)
{
  fprintf(stderr, "Mtools version %s, dated %s\n", mversion, mdate);
  fprintf(stderr, "Usage: %s [-d] drive:\n", progname);
  exit(ret);
}

/**
 * Delete all files on a Drive.<br>
 *
 * @author  Lukas Meyer<br>
 *          lukas.meyer@ruag.com
 * @version 0.4, 11.12.2003
 *
 * @param   drive   the drive to erase
 * @param   debug   1: stop after each erase cycle, 0: normal mode
 * 
 * @returns n.a.
 *
 */
static void do_mclasserase(char drive,int debug)
{
  struct device dev;		/* Device information structure */
  union bootsector boot;

  int media;			/* Just used to enter some in find_device */
  char name[EXPAND_BUF];
  Stream_t *Stream;
  struct label_blk_t *labelBlock;
  
  FILE * fDevice;              /* Stores device's file descriptor */
  
  char cCardType[12];
  
  char drivel[3];		/* Stores the drive letter */


  int i = 0;

  /* FILE *forf; */

  char dummy[2];       /* dummy input for debugging purposes.. */
  int icount=0;
  int iTotalErase = 0;

  const int cycles = 3;		/* How many times we'll overwrite the media */
  char odat[cycles];		/* Data for each overwrite procedure */

  /* Creating values for overwrite  */
  odat[0]=0xff;
  odat[1]=0x00;
  odat[2]=0xff;
  

  if (debug == 1)
     printf("cycles: %i, odats: %i,%i,%i\n",cycles,odat[0],odat[1],odat[2]);
  
  

  /* Reading parameters from card. Exit with -1 if failed. */
  if(! (Stream = find_device(drive, O_RDONLY, &dev, &boot,
					   name, &media, 0, NULL)))
	exit(1);

  FREE(&Stream);

  /* Determine the FAT - type */
#if 0
  if(WORD(fatlen)) {
    labelBlock = &bbelBlock = &boot->ext.old.labelBlock;
  } else {
    labelBlock = &boot->ext.fat32.labelBlock;
  }
#endif

  /* we use only FAT12/16 ...*/
  labelBlock = &boot.boot.ext.old.labelBlock;
   
  /* store card type */
  sprintf(cCardType, "%11.11s", labelBlock->label);

  if (debug == 1)
  {
     printf("Using Device: %s\n",name);
     printf("Card-Type detected: %s\n",cCardType);
  }
      
  /* Forming cat command to overwrite the medias content. */
  sprintf( drivel, "%c:", tolower(drive) );

#if 0
  media_sectors = dev.tracks * dev.sectors;
  sector_size = WORD(secsiz) * dev.heads; 
  

	printf(mcat);
	printf("\n%d\n", media_sectors);
	printf("%d\n", sector_size);
#endif
 
  /*
   * Overwrite device
   */
  for( i=0; i < cycles; i++){

     if (debug==1)
     {
        printf("Erase Cycle %i, writing data: 0x%2.2x...\n",i+1,odat[i]);
     }

     fDevice = fopen(name,"ab+");
          
     if (fDevice == 0)
     {
        perror("Error opening device");
	exit(-1);
     }


     if (debug==1)
     {
        printf("Open successful...\n");
	printf("Flushing device after 32 kBytes of data...\n");
	printf("Erasing:");
	fflush( stdout );
     }

     /* iTotalErase = 0; */
      
     /*
      * overwrite the whole device
      */
     while ((feof(fDevice)==0) && (ferror(fDevice)==0))
     {
          		
	fputc(odat[i],fDevice);

	icount++;
	if (icount > (32 * 1024))
	{
	   /* flush device every 32KB of data...*/
	   fflush( fDevice );
           
	   iTotalErase += icount;
	   if (debug == 1)
	   {
	      printf(".");	   
	      fflush( stdout );
	   }
	   icount=0;
	}
     }
     
     if (debug==1)
     {
        printf("\nPress <ENTER> to continue\n");
        printf("Press <x> and <ENTER> to abort\n");
	
	if(scanf("%c",dummy) < 1)
	  printf("Input error\n");
	fflush( stdin );
	
	if (strcmp(dummy,"x") == 0)
	{
	   printf("exiting.\n");
	   exit(0);
	}
     }
     
     fclose(fDevice);

  }

   
  /*
   * Format device using shell script
   */  
  if (debug == 0)
  {
  	/* redirect STDERR and STDOUT to the black hole... */
	if (dup2(open("/dev/null", O_WRONLY), STDERR_FILENO) != STDERR_FILENO)
		printf("Error with dup2() stdout\n");
	if (dup2(open("/dev/null", O_WRONLY), STDOUT_FILENO) != STDOUT_FILENO)
	        printf("Error with dup2() stdout\n");
  }
	       
  if (debug == 1)
      printf("Calling amuFormat.sh with args: %s,%s\n",cCardType,drivel);
	
  execlp("amuFormat.sh","",cCardType,drivel,NULL);

  /* we never come back...(we shouldn't come back ...) */
  exit(-1);
    
}


/**
 * Total Erase of Data on a Disk. After using mclasserase there wont
 * be ANY bits of old files on the disk.<br>
 * </b>
 * @author  stefan feuz<br>
 *          thomas locher<br>
 *          stefan.feuz@ruag.com
 *          thomas.locher@ruag.com
 * @version 0.3, 02.12.2003
 *
 * @param   argc    generated automatically by operating systems
 * @param   **argv1  generated automatically by operating systems
 * @param   type    generated automatically by operating systems
 *
 * @param   -d      stop after each erase cycle, for testing purposes
 * 
 * @returns int     0 if all is well done<br>
 *          int     -1 if there is something wrong
 *
 * @info    mclasserase [-p tempFilePath] [-d] drive:
 *
 *
 */
void mclasserase(int argc, char **argv, int type UNUSEDP)
{
  /* declaration of all variables */
  int c;
  int debug=0;
  /* char* tempFilePath=NULL; */
  char drive='a';

  int extern optind;

  destroy_privs();

  /* check and read command line arguments */
#ifdef DEBUG
  printf("mclasserase: argc = %i\n",argc);
#endif
  /* check num of arguments */
  if(helpFlag(argc, argv))
    usage(0);
  if ( (argc != 2) & (argc != 3) & (argc != 4))
    { /* wrong num of arguments */
    printf ("mclasserase: wrong num of args\n");
    usage(1);
  }
  else
  { /* correct num of arguments */
    while ((c = getopt(argc, argv, "+p:dh")) != EOF)
    {
      switch (c)
      {
	
	case 'd':
           
	   printf("=============\n");
	   printf("Debug Mode...\n");
	   printf("=============\n");
           debug = 1;
	   break;
	case 'p':
           printf("option -p not implemented yet\n"); 
           break;
	case 'h':
	  usage(0);
        case '?':
           usage(1);
        default:
           break;
       }
     }
#ifdef DEBUG
     printf("mclasserase: optind = %i\n",optind);
   /* look for the drive to erase */
   printf("mclasserase: searching drive\n");
#endif
   for(; optind < argc; optind++)
   {
     if(!argv[optind][0] || argv[optind][1] != ':')
     {
       usage(1);
     }
     drive = toupper(argv[optind][0]);
   }
  }
#ifdef DEBUG
  printf("mclasserase: found drive %c\n", drive);
#endif 
  /* remove all data on drive, you never come back if drive does
   * not exist */
  
  do_mclasserase(drive,debug);
  
  exit (0);
}