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