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