00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifdef SAVE_RCSID
00011 static char *rcsid =
00012 "@(#) $Id: cbmformat.c,v 1.18 2006/02/24 12:01:02 trikalio Exp $";
00013 #endif
00014
00015 #include "opencbm.h"
00016
00017 #include <ctype.h>
00018 #include <getopt.h>
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022
00023 #include "arch.h"
00024
00025 static unsigned char dskfrmt[] = {
00026 #include "cbmformat.inc"
00027 };
00028
00029 static void help()
00030 {
00031 printf(
00032 "Usage: cbmformat [OPTION]... DRIVE NAME,ID\n"
00033 "Fast CBM-1541 disk formatter\n"
00034 "\n"
00035 " -h, --help display this help and exit\n"
00036 " -V, --version display version information and exit\n"
00037 "\n"
00038 " -n, --no-bump do not bump drive head\n"
00039 " -x, --extended format 40 track disk\n"
00040 " -c, --clear clear (demagnetize) this disk.\n"
00041 " this is highly recommended if this disk\n"
00042 " is used for the first time.\n"
00043 " -v, --verify verify each track after it is written\n"
00044 " -o, --original fill sectors with the original pattern (0x4b, 0x01...)\n"
00045 " instead of zeroes\n"
00046 " -s, --status display drive status after formatting\n"
00047 " -p, --progress display progress indicator\n"
00048 "\n"
00049 );
00050 }
00051
00052 static void hint(char *s)
00053 {
00054 fprintf(stderr, "Try `%s' -h for more information.\n", s);
00055 }
00056
00057 int ARCH_MAINDECL main(int argc, char *argv[])
00058 {
00059 int status = 0, id_ofs = 0, name_len, i;
00060 CBM_FILE fd;
00061 unsigned char drive, tracks = 35, bump = 1, orig = 0, show_progress = 0;
00062 unsigned char verify = 0;
00063 unsigned char demagnetize = 0;
00064 char cmd[40], c, name[20], *arg;
00065 int err = 0;
00066
00067 struct option longopts[] =
00068 {
00069 { "help" , no_argument , NULL, 'h' },
00070 { "version" , no_argument , NULL, 'V' },
00071 { "no-bump" , no_argument , NULL, 'n' },
00072 { "extended" , no_argument , NULL, 'x' },
00073 { "original" , no_argument , NULL, 'o' },
00074 { "status" , no_argument , NULL, 's' },
00075 { "progress" , no_argument , NULL, 'p' },
00076 { "verify" , no_argument , NULL, 'v' },
00077 { "clear" , no_argument , NULL, 'c' },
00078
00079
00080 { "end-track" , required_argument, NULL, 't' },
00081 { NULL , 0 , NULL, 0 }
00082 };
00083
00084 const char shortopts[] ="hVnxospvct:";
00085
00086 while((c=(unsigned char)getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
00087 {
00088 switch(c)
00089 {
00090 case 'n': bump = 0;
00091 break;
00092 case 'o': orig = 1;
00093 break;
00094 case 's': status = 1;
00095 break;
00096 case 'x': tracks = 40;
00097 break;
00098 case 'h': help();
00099 return 0;
00100 case 'V': printf("cbmformat %s\n", OPENCBM_VERSION);
00101 return 0;
00102 case 'p': show_progress = 1;
00103 break;
00104 case 'v': verify = 1;
00105 break;
00106 case 'c': demagnetize = 1;
00107 break;
00108 case 't': tracks = arch_atoc(optarg);
00109 break;
00110 default : hint(argv[0]);
00111 return 1;
00112 }
00113 }
00114
00115 if(optind + 2 != argc)
00116 {
00117 fprintf(stderr, "Usage: %s [OPTION]... DRIVE NAME,ID\n", argv[0]);
00118 hint(argv[0]);
00119 return 1;
00120 }
00121
00122 arg = argv[optind++];
00123 drive = arch_atoc(arg);
00124 if(drive < 8 || drive > 11)
00125 {
00126 fprintf(stderr, "Invalid drive number (%s)\n", arg);
00127 return 1;
00128 }
00129
00130 arg = argv[optind++];
00131 name_len = 0;
00132 while(*arg)
00133 {
00134 c = (unsigned char) toupper(*arg);
00135 if(c == ',')
00136 {
00137 if(id_ofs)
00138 {
00139 fprintf(stderr, "More than one `,' in disk name\n");
00140 return 1;
00141 }
00142 id_ofs = name_len;
00143 }
00144 name[name_len++] = c;
00145 if(name_len > 19)
00146 {
00147 fprintf(stderr, "Disk name too long\n");
00148 return 1;
00149 }
00150 arg++;
00151 }
00152 name[name_len] = 0;
00153
00154 if(cbm_driver_open(&fd, 0) == 0)
00155 {
00156 cbm_upload(fd, drive, 0x0500, dskfrmt, sizeof(dskfrmt));
00157 sprintf(cmd, "M-E%c%c%c%c%c%c%c%c0:%s", 3, 5, tracks + 1,
00158 orig, bump, show_progress, demagnetize, verify, name);
00159 cbm_exec_command(fd, drive, cmd, 13+strlen(name));
00160
00161 if(show_progress)
00162 {
00163
00164 cbm_iec_release(fd, IEC_CLOCK);
00165 for(i = 1; i <= tracks; i++)
00166 {
00167 cbm_iec_wait(fd, IEC_DATA, 1);
00168 cbm_iec_set(fd, IEC_CLOCK);
00169 cbm_iec_wait(fd, IEC_DATA, 0);
00170 cbm_iec_release(fd, IEC_CLOCK);
00171
00172 printf("#");
00173 fflush(stdout);
00174 }
00175 printf("\n");
00176 }
00177
00178 err = cbm_device_status(fd, drive, cmd, sizeof(cmd));
00179
00180 if(err && status)
00181 {
00182 printf("%s\n", cmd);
00183 }
00184
00185 if(!err && (tracks > 35))
00186 {
00187 cbm_open(fd, drive, 2, "#", 1);
00188 cbm_exec_command(fd, drive, "U1:2 0 18 0", 11);
00189 cbm_exec_command(fd, drive, "B-P2 192", 8);
00190 cbm_listen(fd, drive, 2);
00191 while(tracks > 35)
00192 {
00193 cbm_raw_write(fd, "\021\377\377\001", 4);
00194 tracks--;
00195 }
00196 cbm_unlisten(fd);
00197 cbm_exec_command(fd, drive, "U2:2 0 18 0", 11);
00198 cbm_close(fd, drive, 2);
00199 }
00200
00201 if(!err && status)
00202 {
00203 cbm_device_status(fd, drive, cmd, sizeof(cmd));
00204 printf("%s\n", cmd);
00205 }
00206 cbm_driver_close(fd);
00207 return 0;
00208 }
00209 else
00210 {
00211 arch_error(0, arch_get_errno(), "%s", cbm_get_driver_name(0));
00212 return 1;
00213 }
00214 }