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