1 /* $NetBSD: file2swp.c,v 1.6 2009/03/18 10:22:26 cegger 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.6 $"; 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(void) 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(disk_t *dd, u_int32_t offset, u_int32_t *start, u_int32_t *end) 154 { 155 struct disklabel dl; 156 int err; 157 158 err = bsd_getlabel(dd, &dl, offset); 159 if (err < 0) { 160 eprintf("Device I/O error (hardware problem?)\n\n"); 161 return (-1); 162 } 163 if (!err) { 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); 169 return (0); 170 } 171 eprintf("NetBSD/Atari format: no swap defined\n"); 172 } 173 return 1; 174 } 175 176 static int 177 readdisklabel(disk_t *dd, u_int32_t *start, u_int32_t *end) 178 { 179 ptable_t pt; 180 int err, i; 181 182 183 err = check_bsdlabel(dd, LABELSECTOR, start, end); 184 if (err != 1) 185 return (err); 186 memset(&pt, 0, sizeof(pt)); 187 err = ahdi_getparts(dd, &pt, AHDI_BBLOCK, AHDI_BBLOCK); 188 if (err < 0) { 189 eprintf("Device I/O error (hardware problem?)\n\n"); 190 return (-1); 191 } 192 if (!err) { 193 /* 194 * Check for hidden BSD labels 195 */ 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); 199 if (err != 1) 200 return (err); 201 } 202 } 203 for (i = 0; i < pt.nparts; i++) { 204 if (!strncmp(pt.parts[i].id, "SWP", 3)) 205 break; 206 } 207 if (i < pt.nparts) { 208 *start = pt.parts[i].start; 209 *end = pt.parts[i].end; 210 eprintf("AHDI format, SWP partition: start:%d,end: %d\n", 211 *start, *end); 212 return (0); 213 } 214 eprintf("AHDI format, no swap ('SWP') partition found!\n"); 215 } 216 eprintf("Unknown label format.\n\n"); 217 return(-1); 218 } 219