1 /* $NetBSD: file2swp.c,v 1.5 2009/03/14 21:04:07 dsl Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/types.h>
40 char *Infile
= "minifs.gz";
41 const char version
[] = "$Revision: 1.6 $";
43 extern const char *program_name
;
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
;
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"
73 main(int argc
, char **argv
)
87 while ((c
= getopt(argc
, argv
, "Vf:ho:w")) != -1) {
93 redirect_output(optarg
);
99 error(-1, "%s", version
);
111 error(-1, "missing DISK argument");
115 dd
= disk_open(*argv
);
117 if (readdisklabel(dd
, &start
, &end
) != 0)
120 if ((fd
= open(Infile
, O_RDONLY
)) < 0) {
121 eprintf("Unable to open <%s>\n", Infile
);
125 switch(key_wait("Are you sure (y/n)? ")) {
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");
134 if (++currblk
>= end
) {
135 eprintf("Error: filesize exceeds swap "
145 eprintf("Aborted\n");
153 check_bsdlabel(disk_t
*dd
, u_int32_t offset
, u_int32_t
*start
, u_int32_t
*end
)
158 err
= bsd_getlabel(dd
, &dl
, offset
);
160 eprintf("Device I/O error (hardware problem?)\n\n");
164 if (dl
.d_partitions
[1].p_size
> 0) {
165 *start
= dl
.d_partitions
[1].p_offset
;
166 *end
= *start
+ dl
.d_partitions
[1].p_size
-1;
167 eprintf("NetBSD/Atari format%s, Swap partition start:%d, end:%d\n",
168 offset
!= 0 ? " (embedded)" : "", *start
, *end
);
171 eprintf("NetBSD/Atari format: no swap defined\n");
177 readdisklabel(disk_t
*dd
, u_int32_t
*start
, u_int32_t
*end
)
183 err
= check_bsdlabel(dd
, LABELSECTOR
, start
, end
);
186 memset(&pt
, 0, sizeof(pt
));
187 err
= ahdi_getparts(dd
, &pt
, AHDI_BBLOCK
, AHDI_BBLOCK
);
189 eprintf("Device I/O error (hardware problem?)\n\n");
194 * Check for hidden BSD labels
196 for (i
= 0; i
< pt
.nparts
; i
++) {
197 if (!strncmp(pt
.parts
[i
].id
, "NBD", 3)) {
198 err
= check_bsdlabel(dd
, pt
.parts
[i
].start
, start
, end
);
203 for (i
= 0; i
< pt
.nparts
; i
++) {
204 if (!strncmp(pt
.parts
[i
].id
, "SWP", 3))
208 *start
= pt
.parts
[i
].start
;
209 *end
= pt
.parts
[i
].end
;
210 eprintf("AHDI format, SWP partition: start:%d,end: %d\n",
214 eprintf("AHDI format, no swap ('SWP') partition found!\n");
216 eprintf("Unknown label format.\n\n");