Home | History | Annotate | Line # | Download | only in file2swp
file2swp.c revision 1.1
      1 /*	$NetBSD: file2swp.c,v 1.1 2002/02/27 20:21:16 leo 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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by the NetBSD
     18  *        Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <fcntl.h>
     40 #include <unistd.h>
     41 #include "libtos.h"
     42 #include "diskio.h"
     43 #include "ahdilbl.h"
     44 #include "cread.h"
     45 
     46 char		*Infile = "minifs.gz";
     47 const char	version[] = "$Revision: 1.1 $";
     48 
     49 extern const char	*program_name;
     50 
     51 int		main    PROTO((int, char **));
     52 static void	usage   PROTO((void)) NORETURN;
     53 
     54 static void
     55 usage()
     56 {
     57 	eprintf("Usage: %s [OPTIONS] DISK\n"
     58 		"where OPTIONS are:\n"
     59 		"\t-V         display version information\n"
     60 		"\t-f FILE    File to copy. The FILE may be a gzipped file.\n"
     61 		"\t           If not specified, it defaults to minifs.gz.\n"
     62 		"\t-h         display this help and exit\n"
     63 		"\t-o FILE    send output to FILE instead of stdout\n"
     64 		"\t-w         wait for key press before exiting\n\n"
     65 		"DISK is the concatenation of BUS, TARGET and LUN.\n"
     66 		"BUS is one of `i' (IDE), `a' (ACSI) or `s' (SCSI).\n"
     67 		"TARGET and LUN are one decimal digit each. LUN must\n"
     68 		"not be specified for IDE devices and is optional for\n"
     69 		"ACSI/SCSI devices (if omitted, LUN defaults to 0).\n\n"
     70 		"Examples:  a0  refers to ACSI target 0 lun 0\n"
     71 		"           s21 refers to SCSI target 2 lun 1\n"
     72 		, program_name);
     73 	xexit(EXIT_SUCCESS);
     74 }
     75 
     76 int
     77 main(argc, argv)
     78 	int		argc;
     79 	char		**argv;
     80 {
     81 	extern int	optind;
     82 	extern char	*optarg;
     83 
     84 	disk_t		*dd;
     85 	ptable_t	pt;
     86 	int		rv, c, i, fd;
     87 	u_int32_t	currblk;
     88 	char		buf[AHDI_BSIZE];
     89 
     90 	i = rv = 0;
     91 	init_toslib(*argv);
     92 
     93 	while ((c = getopt(argc, argv, "Vf:ho:w")) != -1) {
     94 		switch (c) {
     95 		  case 'f':
     96 			Infile = optarg;
     97 			break;
     98 		  case 'o':
     99 			redirect_output(optarg);
    100 			break;
    101 		  case 'w':
    102 		  	set_wait_for_key();
    103 			break;
    104 		  case 'V':
    105 			error(-1, "%s", version);
    106 			break;
    107 			/* NOT REACHED */
    108 		  case 'h':
    109 		  default:
    110 			usage();
    111 			/* NOT REACHED */
    112 		}
    113 	}
    114 	argv += optind;
    115 
    116 	if (!*argv) {
    117 		error(-1, "missing DISK argument");
    118 		usage();
    119 		/* NOT REACHED */
    120 	}
    121 	dd = disk_open(*argv);
    122 	pt.nparts = 0;
    123 	pt.parts  = NULL;
    124 
    125 	if (!ahdi_getparts(dd, &pt, AHDI_BBLOCK, AHDI_BBLOCK)) {
    126 		for (i = 0; i < pt.nparts; i++) {
    127 			if (!strncmp(pt.parts[i].id, "SWP", 3))
    128 				break;
    129 		}
    130 		if (i == pt.nparts) {
    131 			eprintf("No swap ('SWP') partition found!\n");
    132 			xexit(1);
    133 		}
    134 	}
    135 	else xexit(1);
    136 	if ((fd = open(Infile, O_RDONLY)) < 0) {
    137 		eprintf("Unable to open <%s>\n", Infile);
    138 		xexit(1);
    139 	}
    140 
    141 	eprintf("Found Swap ('SWP') partition start: %d, end: %d.\n",
    142 				pt.parts[i].start, pt.parts[i].end);
    143 	switch(key_wait("Are you sure (y/n)? ")) {
    144 	  case 'y':
    145 	  case 'Y':
    146 		currblk = pt.parts[i].start;
    147 		while(c = read(fd, buf, sizeof(buf)) > 0) {
    148 		    if (disk_write(dd, currblk, 1, buf) < 0) {
    149 			eprintf("Error writing to swap partition\n");
    150 			xexit(1);
    151 		    }
    152 		    if (++currblk >= pt.parts[i].end) {
    153 			eprintf("Error: filesize exceeds swap "
    154 							"partition size\n");
    155 			xexit(1);
    156 		    }
    157 		}
    158 		close(fd);
    159 		eprintf("Ready\n");
    160 		xexit(0);
    161 		break;
    162 	  default :
    163 		eprintf("Aborted\n");
    164 		break;
    165 	}
    166 	rv = EXIT_FAILURE;
    167 	return(rv);
    168 }
    169