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