1 /* Copyright 2003 Stefan Feuz, Lukas Meyer, Thomas Locher
2 * Copyright 2004,2006,2007,2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
21 * Original Creation Date:
28 * Stefan Feuz, Lukas Meyer, Thomas Locher
31 #include "sysincludes.h"
48 * Prints the Usage Message to STDOUT<br>
50 * @author stefan feuz<br>
51 * stefan.feuz@ruag.com
58 static void usage(int ret) NORETURN;
59 static void usage(int ret)
61 fprintf(stderr, "Mtools version %s, dated %s\n", mversion, mdate);
62 fprintf(stderr, "Usage: %s [-d] drive:\n", progname);
67 * Delete all files on a Drive.<br>
69 * @author Lukas Meyer<br>
70 * lukas.meyer@ruag.com
71 * @version 0.4, 11.12.2003
73 * @param drive the drive to erase
74 * @param debug 1: stop after each erase cycle, 0: normal mode
79 static void do_mclasserase(char drive,int debug)
81 struct device dev; /* Device information structure */
82 union bootsector boot;
84 int media; /* Just used to enter some in find_device */
85 char name[EXPAND_BUF];
87 struct label_blk_t *labelBlock;
89 FILE * fDevice; /* Stores device's file descriptor */
93 char drivel[2]; /* Stores the drive letter */
100 char dummy[2]; /* dummy input for debugging purposes.. */
104 const int cycles = 3; /* How many times we'll overwrite the media */
105 char odat[cycles]; /* Data for each overwrite procedure */
107 /* Creating values for overwrite */
114 printf("cycles: %i, odats: %i,%i,%i\n",cycles,odat[0],odat[1],odat[2]);
118 /* Reading parameters from card. Exit with -1 if failed. */
119 if(! (Stream = find_device(drive, O_RDONLY, &dev, &boot,
120 name, &media, 0, NULL)))
125 /* Determine the FAT - type */
128 labelBlock = &bbelBlock = &boot->ext.old.labelBlock;
130 labelBlock = &boot->ext.fat32.labelBlock;
134 /* we use only FAT12/16 ...*/
135 labelBlock = &boot.boot.ext.old.labelBlock;
137 /* store card type */
138 sprintf(cCardType, "%11.11s", labelBlock->label);
142 printf("Using Device: %s\n",name);
143 printf("Card-Type detected: %s\n",cCardType);
146 /* Forming cat command to overwrite the medias content. */
147 sprintf( drivel, "%c:", tolower(drive) );
150 media_sectors = dev.tracks * dev.sectors;
151 sector_size = WORD(secsiz) * dev.heads;
155 printf("\n%d\n", media_sectors);
156 printf("%d\n", sector_size);
162 for( i=0; i < cycles; i++){
166 printf("Erase Cycle %i, writing data: 0x%2.2x...\n",i+1,odat[i]);
169 fDevice = fopen(name,"ab+");
173 perror("Error opening device");
180 printf("Open successful...\n");
181 printf("Flushing device after 32 kBytes of data...\n");
186 /* iTotalErase = 0; */
189 * overwrite the whole device
191 while ((feof(fDevice)==0) && (ferror(fDevice)==0))
194 fputc(odat[i],fDevice);
197 if (icount > (32 * 1024))
199 /* flush device every 32KB of data...*/
202 iTotalErase += icount;
214 printf("\nPress <ENTER> to continue\n");
215 printf("Press <x> and <ENTER> to abort\n");
217 if(scanf("%c",dummy) < 1)
218 printf("Input error\n");
221 if (strcmp(dummy,"x") == 0)
223 printf("exiting.\n");
234 * Format device using shell script
238 /* redirect STDERR and STDOUT to the black hole... */
239 if (dup2(open("/dev/null", O_WRONLY), STDERR_FILENO) != STDERR_FILENO)
240 printf("Error with dup2() stdout\n");
241 if (dup2(open("/dev/null", O_WRONLY), STDOUT_FILENO) != STDOUT_FILENO)
242 printf("Error with dup2() stdout\n");
246 printf("Calling amuFormat.sh with args: %s,%s\n",cCardType,drivel);
248 execlp("amuFormat.sh","",cCardType,drivel,NULL);
250 /* we never come back...(we shouldn't come back ...) */
257 * Total Erase of Data on a Disk. After using mclasserase there wont
258 * be ANY bits of old files on the disk.<br>
260 * @author stefan feuz<br>
262 * stefan.feuz@ruag.com
263 * thomas.locher@ruag.com
264 * @version 0.3, 02.12.2003
266 * @param argc generated automatically by operating systems
267 * @param **argv1 generated automatically by operating systems
268 * @param type generated automatically by operating systems
270 * @param -d stop after each erase cycle, for testing purposes
272 * @returns int 0 if all is well done<br>
273 * int -1 if there is something wrong
275 * @info mclasserase [-p tempFilePath] [-d] drive:
279 void mclasserase(int argc, char **argv, int type)
281 /* declaration of all variables */
284 /* char* tempFilePath=NULL; */
291 /* check and read command line arguments */
293 printf("mclasserase: argc = %i\n",argc);
295 /* check num of arguments */
296 if(helpFlag(argc, argv))
298 if ( (argc != 2) & (argc != 3) & (argc != 4))
299 { /* wrong num of arguments */
300 printf ("mclasserase: wrong num of args\n");
304 { /* correct num of arguments */
305 while ((c = getopt(argc, argv, "+p:dh")) != EOF)
312 printf("=============\n");
313 printf("Debug Mode...\n");
314 printf("=============\n");
318 printf("option -p not implemented yet\n");
329 printf("mclasserase: optind = %i\n",optind);
330 /* look for the drive to erase */
331 printf("mclasserase: searching drive\n");
333 for(; optind < argc; optind++)
335 if(!argv[optind][0] || argv[optind][1] != ':')
339 drive = toupper(argv[optind][0]);
343 printf("mclasserase: found drive %c\n", drive);
345 /* remove all data on drive, you never come back if drive does
348 do_mclasserase(drive,debug);