Home | History | Annotate | Line # | Download | only in rawwrite
      1 /*	$NetBSD: rawwrite.c,v 1.11 2009/10/20 19:10:11 snj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <osbind.h>
     29 #include <stdlib.h>
     30 #include <unistd.h>
     31 #include <stdio.h>
     32 #include <fcntl.h>
     33 #include <string.h>
     34 #include "libtos.h"
     35 
     36 #define	SECT_SIZE	512		/* Sector size			*/
     37 #define	NSECT_DD	18		/* Sectors per cylinder 720Kb	*/
     38 #define	NSECT_HD	36		/* Sectors per cylinder 1.44Mb	*/
     39 #define	NTRK		80		/* Number of tracks		*/
     40 
     41 static void help    PROTO((void));
     42 static void usage   PROTO((void));
     43 static void brwrite PROTO((char *, int, int));
     44 
     45 char	buf[NSECT_HD * SECT_SIZE];
     46 int	h_flag = 0;	/* Show help					*/
     47 int	v_flag = 0;	/* Verbose (a dot for each track copied)	*/
     48 int	V_flag = 0;	/* Show version					*/
     49 char	*progname;
     50 
     51 const char version[] = "$Revision: 1.11 $";
     52 
     53 int
     54 main(int argc, char *argv[])
     55 {
     56 	extern	int	optind;
     57 	extern	char	*optarg;
     58 	int		ch;
     59 	char		*infile;
     60 	int		fd;
     61 	int		i, n;
     62 	int		nsect;
     63 
     64 	progname = argv[0];
     65 	init_toslib(argv[0]);
     66 
     67 	nsect  = NSECT_DD;
     68 
     69 	while ((ch = getopt(argc, argv, "hHvVwo:")) != -1) {
     70 		switch (ch) {
     71 			case 'h':
     72 				h_flag = 1;
     73 				break;
     74 			case 'H':
     75 				nsect  = NSECT_HD;
     76 				break;
     77 			case 'o':
     78 				redirect_output(optarg);
     79 				break;
     80 			case 'v':
     81 				v_flag = 1;
     82 				break;
     83 			case 'V':
     84 				V_flag = 1;
     85 				break;
     86 			case 'w':
     87 				set_wait_for_key();
     88 				break;
     89 			default :
     90 				usage();
     91 				break;
     92 		}
     93 	}
     94 	if (h_flag)
     95 		help();
     96 	if (V_flag)
     97 		eprintf("%s\r\n", version);
     98 
     99 	if (optind >= argc)
    100 		usage();
    101 
    102 	infile = argv[optind];
    103 
    104 	if ((fd = open(infile, O_RDONLY)) < 0)
    105 		fatal(-1, "Cannot open '%s'\n", infile);
    106 
    107 	for (i = 0; i < NTRK; i++) {
    108 		n = read(fd, buf, nsect * SECT_SIZE);
    109 		if (n == 0) {
    110 			eprintf("Only %d tracks in input file\r\n", i);
    111 			break;
    112 		}
    113 		if (n < 0)
    114 		    fatal(-1, "\n\rRead error on '%s'\n", infile);
    115 		if (n != (nsect * SECT_SIZE))
    116 		    fatal(-1, "\n\rUnexpected short-read on '%s'\n", infile);
    117 		if (v_flag) {
    118 			if (i && !(i % 40))
    119 				eprintf("\r\n");
    120 			eprintf(".");
    121 		}
    122 		brwrite(buf, i, nsect);
    123 	}
    124 	close(fd);
    125 	if (v_flag)
    126 		eprintf("\r\n");
    127 	xexit(0);
    128 }
    129 
    130 static void
    131 brwrite(char *buf, int trk, int spt)
    132 {
    133 	/*
    134 	 * These need to be static with my version of osbind.h :-(
    135 	 */
    136 	static u_char	trbuf[NSECT_HD * SECT_SIZE * 2];
    137 	static u_int	sideno  = 0;
    138 
    139 	for (sideno = 0; sideno < 2; sideno++) {
    140 		if (Flopfmt(trbuf, 0, 0, spt/2, trk, sideno, 1,
    141 						0x87654321, 0xe5e5))
    142 			fatal(-1, "Format error");
    143 		if (Flopwr(buf, 0, 0, 1, trk, sideno, spt/2))
    144 			fatal(-1, "Write error");
    145 		buf += (spt/2) * SECT_SIZE;
    146 	}
    147 }
    148 static void
    149 usage(void)
    150 {
    151 	eprintf("Usage: %s [-hvVw] [-o <log-file>] <infile>\r\n", progname);
    152 	xexit(1);
    153 }
    154 
    155 static void
    156 help(void)
    157 {
    158 	eprintf("\r
    159 write a raw floppy-image to disk\r
    160 \r
    161 Usage: %s [-hvVw] [-o <log-file>] <infile>\r
    162 \r
    163 Description of options:\r
    164 \r
    165 \t-h  What you're getting right now.\r
    166 \t-H  Write high density floppies.\r
    167 \t-o  Write output to both <output file> and stdout.\r
    168 \t-v  Show a '.' for each track written.\r
    169 \t-V  Print program version.\r
    170 \t-w  Wait for a keypress before exiting.\r
    171 ", progname);
    172 	xexit(0);
    173 }
    174