file2swp.c revision 1.2 1 1.2 leo /* $NetBSD: file2swp.c,v 1.2 2002/03/22 21:29:23 leo Exp $ */
2 1.1 leo
3 1.1 leo /*-
4 1.1 leo * Copyright (c) 2002 The NetBSD Foundation, Inc.
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 the NetBSD
18 1.1 leo * Foundation, Inc. and its contributors.
19 1.1 leo * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1 leo * contributors may be used to endorse or promote products derived
21 1.1 leo * from this software without specific prior written permission.
22 1.1 leo *
23 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 leo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 leo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 leo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 leo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 leo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 leo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 leo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 leo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 leo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 leo * POSSIBILITY OF SUCH DAMAGE.
34 1.1 leo */
35 1.1 leo
36 1.1 leo #include <sys/types.h>
37 1.1 leo #include <stdlib.h>
38 1.1 leo #include <string.h>
39 1.1 leo #include <fcntl.h>
40 1.1 leo #include <unistd.h>
41 1.1 leo #include "libtos.h"
42 1.1 leo #include "diskio.h"
43 1.1 leo #include "ahdilbl.h"
44 1.2 leo #include "disklbl.h"
45 1.1 leo #include "cread.h"
46 1.1 leo
47 1.1 leo char *Infile = "minifs.gz";
48 1.2 leo const char version[] = "$Revision: 1.2 $";
49 1.1 leo
50 1.1 leo extern const char *program_name;
51 1.1 leo
52 1.1 leo int main PROTO((int, char **));
53 1.2 leo static int check_bsdlabel PROTO((disk_t *,u_int32_t,u_int32_t *,u_int32_t *));
54 1.2 leo static int readdisklabel PROTO((disk_t *, u_int32_t *, u_int32_t *));
55 1.2 leo static void usage PROTO((void)) NORETURN;
56 1.1 leo
57 1.1 leo static void
58 1.1 leo usage()
59 1.1 leo {
60 1.1 leo eprintf("Usage: %s [OPTIONS] DISK\n"
61 1.1 leo "where OPTIONS are:\n"
62 1.1 leo "\t-V display version information\n"
63 1.1 leo "\t-f FILE File to copy. The FILE may be a gzipped file.\n"
64 1.1 leo "\t If not specified, it defaults to minifs.gz.\n"
65 1.1 leo "\t-h display this help and exit\n"
66 1.1 leo "\t-o FILE send output to FILE instead of stdout\n"
67 1.1 leo "\t-w wait for key press before exiting\n\n"
68 1.1 leo "DISK is the concatenation of BUS, TARGET and LUN.\n"
69 1.1 leo "BUS is one of `i' (IDE), `a' (ACSI) or `s' (SCSI).\n"
70 1.1 leo "TARGET and LUN are one decimal digit each. LUN must\n"
71 1.1 leo "not be specified for IDE devices and is optional for\n"
72 1.1 leo "ACSI/SCSI devices (if omitted, LUN defaults to 0).\n\n"
73 1.1 leo "Examples: a0 refers to ACSI target 0 lun 0\n"
74 1.1 leo " s21 refers to SCSI target 2 lun 1\n"
75 1.1 leo , program_name);
76 1.1 leo xexit(EXIT_SUCCESS);
77 1.1 leo }
78 1.1 leo
79 1.1 leo int
80 1.1 leo main(argc, argv)
81 1.1 leo int argc;
82 1.1 leo char **argv;
83 1.1 leo {
84 1.1 leo extern int optind;
85 1.1 leo extern char *optarg;
86 1.1 leo
87 1.1 leo disk_t *dd;
88 1.1 leo int rv, c, i, fd;
89 1.1 leo u_int32_t currblk;
90 1.2 leo u_int32_t start, end;
91 1.1 leo char buf[AHDI_BSIZE];
92 1.1 leo
93 1.1 leo i = rv = 0;
94 1.1 leo init_toslib(*argv);
95 1.1 leo
96 1.1 leo while ((c = getopt(argc, argv, "Vf:ho:w")) != -1) {
97 1.1 leo switch (c) {
98 1.1 leo case 'f':
99 1.1 leo Infile = optarg;
100 1.1 leo break;
101 1.1 leo case 'o':
102 1.1 leo redirect_output(optarg);
103 1.1 leo break;
104 1.1 leo case 'w':
105 1.1 leo set_wait_for_key();
106 1.1 leo break;
107 1.1 leo case 'V':
108 1.1 leo error(-1, "%s", version);
109 1.1 leo break;
110 1.1 leo /* NOT REACHED */
111 1.1 leo case 'h':
112 1.1 leo default:
113 1.1 leo usage();
114 1.1 leo /* NOT REACHED */
115 1.1 leo }
116 1.1 leo }
117 1.1 leo argv += optind;
118 1.1 leo
119 1.1 leo if (!*argv) {
120 1.1 leo error(-1, "missing DISK argument");
121 1.1 leo usage();
122 1.1 leo /* NOT REACHED */
123 1.1 leo }
124 1.1 leo dd = disk_open(*argv);
125 1.1 leo
126 1.2 leo if (readdisklabel(dd, &start, &end) != 0)
127 1.2 leo xexit(1);
128 1.2 leo
129 1.1 leo if ((fd = open(Infile, O_RDONLY)) < 0) {
130 1.1 leo eprintf("Unable to open <%s>\n", Infile);
131 1.1 leo xexit(1);
132 1.1 leo }
133 1.1 leo
134 1.1 leo switch(key_wait("Are you sure (y/n)? ")) {
135 1.1 leo case 'y':
136 1.1 leo case 'Y':
137 1.2 leo currblk = start;
138 1.1 leo while(c = read(fd, buf, sizeof(buf)) > 0) {
139 1.1 leo if (disk_write(dd, currblk, 1, buf) < 0) {
140 1.1 leo eprintf("Error writing to swap partition\n");
141 1.1 leo xexit(1);
142 1.1 leo }
143 1.2 leo if (++currblk >= end) {
144 1.1 leo eprintf("Error: filesize exceeds swap "
145 1.1 leo "partition size\n");
146 1.1 leo xexit(1);
147 1.1 leo }
148 1.1 leo }
149 1.1 leo close(fd);
150 1.1 leo eprintf("Ready\n");
151 1.1 leo xexit(0);
152 1.1 leo break;
153 1.1 leo default :
154 1.1 leo eprintf("Aborted\n");
155 1.1 leo break;
156 1.1 leo }
157 1.1 leo rv = EXIT_FAILURE;
158 1.1 leo return(rv);
159 1.2 leo }
160 1.2 leo
161 1.2 leo static int
162 1.2 leo check_bsdlabel(dd, offset, start, end)
163 1.2 leo disk_t *dd;
164 1.2 leo u_int32_t offset, *start, *end;
165 1.2 leo {
166 1.2 leo struct disklabel dl;
167 1.2 leo int err;
168 1.2 leo
169 1.2 leo err = bsd_getlabel(dd, &dl, offset);
170 1.2 leo if (err < 0) {
171 1.2 leo eprintf("Device I/O error (hardware problem?)\n\n");
172 1.2 leo return (-1);
173 1.2 leo }
174 1.2 leo if (!err) {
175 1.2 leo if (dl.d_partitions[1].p_size > 0) {
176 1.2 leo *start = dl.d_partitions[1].p_offset;
177 1.2 leo *end = *start + dl.d_partitions[1].p_size-1;
178 1.2 leo eprintf("NetBSD/Atari format%s, Swap partition start:%d, end:%d\n",
179 1.2 leo offset != 0 ? " (embedded)" : "", *start, *end);
180 1.2 leo return (0);
181 1.2 leo }
182 1.2 leo eprintf("NetBSD/Atari format: no swap defined\n");
183 1.2 leo }
184 1.2 leo return 1;
185 1.2 leo }
186 1.2 leo
187 1.2 leo static int
188 1.2 leo readdisklabel(dd, start, end)
189 1.2 leo disk_t *dd;
190 1.2 leo u_int32_t *start, *end;
191 1.2 leo {
192 1.2 leo ptable_t pt;
193 1.2 leo int err, i;
194 1.2 leo
195 1.2 leo
196 1.2 leo err = check_bsdlabel(dd, LABELSECTOR, start, end);
197 1.2 leo if (err != 1)
198 1.2 leo return (err);
199 1.2 leo memset(&pt, 0, sizeof(pt));
200 1.2 leo err = ahdi_getparts(dd, &pt, AHDI_BBLOCK, AHDI_BBLOCK);
201 1.2 leo if (err < 0) {
202 1.2 leo eprintf("Device I/O error (hardware problem?)\n\n");
203 1.2 leo return (-1);
204 1.2 leo }
205 1.2 leo if (!err) {
206 1.2 leo /*
207 1.2 leo * Check for hidden BSD labels
208 1.2 leo */
209 1.2 leo for (i = 0; i < pt.nparts; i++) {
210 1.2 leo if (!strncmp(pt.parts[i].id, "NBD", 3)) {
211 1.2 leo err = check_bsdlabel(dd, pt.parts[i].start, start, end);
212 1.2 leo if (err != 1)
213 1.2 leo return (err);
214 1.2 leo }
215 1.2 leo }
216 1.2 leo for (i = 0; i < pt.nparts; i++) {
217 1.2 leo if (!strncmp(pt.parts[i].id, "SWP", 3))
218 1.2 leo break;
219 1.2 leo }
220 1.2 leo if (i < pt.nparts) {
221 1.2 leo *start = pt.parts[i].start;
222 1.2 leo *end = pt.parts[i].end;
223 1.2 leo eprintf("AHDI format, SWP partition: start:%d,end: %d\n",
224 1.2 leo *start, *end);
225 1.2 leo return (0);
226 1.2 leo }
227 1.2 leo eprintf("AHDI format, no swap ('SWP') partition found!\n");
228 1.2 leo }
229 1.2 leo eprintf("Unknown label format.\n\n");
230 1.2 leo return(-1);
231 1.1 leo }
232