file2swp.c revision 1.1 1 1.1 leo /* $NetBSD: file2swp.c,v 1.1 2002/02/27 20:21:16 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.1 leo #include "cread.h"
45 1.1 leo
46 1.1 leo char *Infile = "minifs.gz";
47 1.1 leo const char version[] = "$Revision: 1.1 $";
48 1.1 leo
49 1.1 leo extern const char *program_name;
50 1.1 leo
51 1.1 leo int main PROTO((int, char **));
52 1.1 leo static void usage PROTO((void)) NORETURN;
53 1.1 leo
54 1.1 leo static void
55 1.1 leo usage()
56 1.1 leo {
57 1.1 leo eprintf("Usage: %s [OPTIONS] DISK\n"
58 1.1 leo "where OPTIONS are:\n"
59 1.1 leo "\t-V display version information\n"
60 1.1 leo "\t-f FILE File to copy. The FILE may be a gzipped file.\n"
61 1.1 leo "\t If not specified, it defaults to minifs.gz.\n"
62 1.1 leo "\t-h display this help and exit\n"
63 1.1 leo "\t-o FILE send output to FILE instead of stdout\n"
64 1.1 leo "\t-w wait for key press before exiting\n\n"
65 1.1 leo "DISK is the concatenation of BUS, TARGET and LUN.\n"
66 1.1 leo "BUS is one of `i' (IDE), `a' (ACSI) or `s' (SCSI).\n"
67 1.1 leo "TARGET and LUN are one decimal digit each. LUN must\n"
68 1.1 leo "not be specified for IDE devices and is optional for\n"
69 1.1 leo "ACSI/SCSI devices (if omitted, LUN defaults to 0).\n\n"
70 1.1 leo "Examples: a0 refers to ACSI target 0 lun 0\n"
71 1.1 leo " s21 refers to SCSI target 2 lun 1\n"
72 1.1 leo , program_name);
73 1.1 leo xexit(EXIT_SUCCESS);
74 1.1 leo }
75 1.1 leo
76 1.1 leo int
77 1.1 leo main(argc, argv)
78 1.1 leo int argc;
79 1.1 leo char **argv;
80 1.1 leo {
81 1.1 leo extern int optind;
82 1.1 leo extern char *optarg;
83 1.1 leo
84 1.1 leo disk_t *dd;
85 1.1 leo ptable_t pt;
86 1.1 leo int rv, c, i, fd;
87 1.1 leo u_int32_t currblk;
88 1.1 leo char buf[AHDI_BSIZE];
89 1.1 leo
90 1.1 leo i = rv = 0;
91 1.1 leo init_toslib(*argv);
92 1.1 leo
93 1.1 leo while ((c = getopt(argc, argv, "Vf:ho:w")) != -1) {
94 1.1 leo switch (c) {
95 1.1 leo case 'f':
96 1.1 leo Infile = optarg;
97 1.1 leo break;
98 1.1 leo case 'o':
99 1.1 leo redirect_output(optarg);
100 1.1 leo break;
101 1.1 leo case 'w':
102 1.1 leo set_wait_for_key();
103 1.1 leo break;
104 1.1 leo case 'V':
105 1.1 leo error(-1, "%s", version);
106 1.1 leo break;
107 1.1 leo /* NOT REACHED */
108 1.1 leo case 'h':
109 1.1 leo default:
110 1.1 leo usage();
111 1.1 leo /* NOT REACHED */
112 1.1 leo }
113 1.1 leo }
114 1.1 leo argv += optind;
115 1.1 leo
116 1.1 leo if (!*argv) {
117 1.1 leo error(-1, "missing DISK argument");
118 1.1 leo usage();
119 1.1 leo /* NOT REACHED */
120 1.1 leo }
121 1.1 leo dd = disk_open(*argv);
122 1.1 leo pt.nparts = 0;
123 1.1 leo pt.parts = NULL;
124 1.1 leo
125 1.1 leo if (!ahdi_getparts(dd, &pt, AHDI_BBLOCK, AHDI_BBLOCK)) {
126 1.1 leo for (i = 0; i < pt.nparts; i++) {
127 1.1 leo if (!strncmp(pt.parts[i].id, "SWP", 3))
128 1.1 leo break;
129 1.1 leo }
130 1.1 leo if (i == pt.nparts) {
131 1.1 leo eprintf("No swap ('SWP') partition found!\n");
132 1.1 leo xexit(1);
133 1.1 leo }
134 1.1 leo }
135 1.1 leo else xexit(1);
136 1.1 leo if ((fd = open(Infile, O_RDONLY)) < 0) {
137 1.1 leo eprintf("Unable to open <%s>\n", Infile);
138 1.1 leo xexit(1);
139 1.1 leo }
140 1.1 leo
141 1.1 leo eprintf("Found Swap ('SWP') partition start: %d, end: %d.\n",
142 1.1 leo pt.parts[i].start, pt.parts[i].end);
143 1.1 leo switch(key_wait("Are you sure (y/n)? ")) {
144 1.1 leo case 'y':
145 1.1 leo case 'Y':
146 1.1 leo currblk = pt.parts[i].start;
147 1.1 leo while(c = read(fd, buf, sizeof(buf)) > 0) {
148 1.1 leo if (disk_write(dd, currblk, 1, buf) < 0) {
149 1.1 leo eprintf("Error writing to swap partition\n");
150 1.1 leo xexit(1);
151 1.1 leo }
152 1.1 leo if (++currblk >= pt.parts[i].end) {
153 1.1 leo eprintf("Error: filesize exceeds swap "
154 1.1 leo "partition size\n");
155 1.1 leo xexit(1);
156 1.1 leo }
157 1.1 leo }
158 1.1 leo close(fd);
159 1.1 leo eprintf("Ready\n");
160 1.1 leo xexit(0);
161 1.1 leo break;
162 1.1 leo default :
163 1.1 leo eprintf("Aborted\n");
164 1.1 leo break;
165 1.1 leo }
166 1.1 leo rv = EXIT_FAILURE;
167 1.1 leo return(rv);
168 1.1 leo }
169