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