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