Home | History | Annotate | Line # | Download | only in file2swp
file2swp.c revision 1.2.6.2
      1 /*	$NetBSD: file2swp.c,v 1.2.6.2 2002/06/23 17:35:21 jdolecek 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 "disklbl.h"
     45 #include "cread.h"
     46 
     47 char		*Infile = "minifs.gz";
     48 const char	version[] = "$Revision: 1.2.6.2 $";
     49 
     50 extern const char	*program_name;
     51 
     52 int		main    PROTO((int, char **));
     53 static int	check_bsdlabel PROTO((disk_t *,u_int32_t,u_int32_t *,u_int32_t *));
     54 static int	readdisklabel PROTO((disk_t *, u_int32_t *, u_int32_t *));
     55 static void	usage PROTO((void)) NORETURN;
     56 
     57 static void
     58 usage()
     59 {
     60 	eprintf("Usage: %s [OPTIONS] DISK\n"
     61 		"where OPTIONS are:\n"
     62 		"\t-V         display version information\n"
     63 		"\t-f FILE    File to copy. The FILE may be a gzipped file.\n"
     64 		"\t           If not specified, it defaults to minifs.gz.\n"
     65 		"\t-h         display this help and exit\n"
     66 		"\t-o FILE    send output to FILE instead of stdout\n"
     67 		"\t-w         wait for key press before exiting\n\n"
     68 		"DISK is the concatenation of BUS, TARGET and LUN.\n"
     69 		"BUS is one of `i' (IDE), `a' (ACSI) or `s' (SCSI).\n"
     70 		"TARGET and LUN are one decimal digit each. LUN must\n"
     71 		"not be specified for IDE devices and is optional for\n"
     72 		"ACSI/SCSI devices (if omitted, LUN defaults to 0).\n\n"
     73 		"Examples:  a0  refers to ACSI target 0 lun 0\n"
     74 		"           s21 refers to SCSI target 2 lun 1\n"
     75 		, program_name);
     76 	xexit(EXIT_SUCCESS);
     77 }
     78 
     79 int
     80 main(argc, argv)
     81 	int		argc;
     82 	char		**argv;
     83 {
     84 	extern int	optind;
     85 	extern char	*optarg;
     86 
     87 	disk_t		*dd;
     88 	int		rv, c, i, fd;
     89 	u_int32_t	currblk;
     90 	u_int32_t	start, end;
     91 	char		buf[AHDI_BSIZE];
     92 
     93 	i = rv = 0;
     94 	init_toslib(*argv);
     95 
     96 	while ((c = getopt(argc, argv, "Vf:ho:w")) != -1) {
     97 		switch (c) {
     98 		  case 'f':
     99 			Infile = optarg;
    100 			break;
    101 		  case 'o':
    102 			redirect_output(optarg);
    103 			break;
    104 		  case 'w':
    105 		  	set_wait_for_key();
    106 			break;
    107 		  case 'V':
    108 			error(-1, "%s", version);
    109 			break;
    110 			/* NOT REACHED */
    111 		  case 'h':
    112 		  default:
    113 			usage();
    114 			/* NOT REACHED */
    115 		}
    116 	}
    117 	argv += optind;
    118 
    119 	if (!*argv) {
    120 		error(-1, "missing DISK argument");
    121 		usage();
    122 		/* NOT REACHED */
    123 	}
    124 	dd = disk_open(*argv);
    125 
    126 	if (readdisklabel(dd, &start, &end) != 0)
    127 		xexit(1);
    128 
    129 	if ((fd = open(Infile, O_RDONLY)) < 0) {
    130 		eprintf("Unable to open <%s>\n", Infile);
    131 		xexit(1);
    132 	}
    133 
    134 	switch(key_wait("Are you sure (y/n)? ")) {
    135 	  case 'y':
    136 	  case 'Y':
    137 		currblk = start;
    138 		while(c = read(fd, buf, sizeof(buf)) > 0) {
    139 		    if (disk_write(dd, currblk, 1, buf) < 0) {
    140 			eprintf("Error writing to swap partition\n");
    141 			xexit(1);
    142 		    }
    143 		    if (++currblk >= end) {
    144 			eprintf("Error: filesize exceeds swap "
    145 							"partition size\n");
    146 			xexit(1);
    147 		    }
    148 		}
    149 		close(fd);
    150 		eprintf("Ready\n");
    151 		xexit(0);
    152 		break;
    153 	  default :
    154 		eprintf("Aborted\n");
    155 		break;
    156 	}
    157 	rv = EXIT_FAILURE;
    158 	return(rv);
    159 }
    160 
    161 static int
    162 check_bsdlabel(dd, offset, start, end)
    163 	disk_t	*dd;
    164 	u_int32_t	offset, *start, *end;
    165 {
    166 	struct disklabel	dl;
    167 	int					err;
    168 
    169 	err = bsd_getlabel(dd, &dl, offset);
    170 	if (err < 0) {
    171 		eprintf("Device I/O error (hardware problem?)\n\n");
    172 		return (-1);
    173 	}
    174 	if (!err) {
    175 		if (dl.d_partitions[1].p_size > 0) {
    176 			*start = dl.d_partitions[1].p_offset;
    177 			*end   = *start + dl.d_partitions[1].p_size-1;
    178 			eprintf("NetBSD/Atari format%s, Swap partition start:%d, end:%d\n",
    179 				offset != 0 ? " (embedded)" : "", *start, *end);
    180 			return (0);
    181 		}
    182 		eprintf("NetBSD/Atari format: no swap defined\n");
    183 	}
    184 	return 1;
    185 }
    186 
    187 static int
    188 readdisklabel(dd, start, end)
    189 	disk_t		*dd;
    190 	u_int32_t	*start, *end;
    191 {
    192 	ptable_t		pt;
    193 	int				err, i;
    194 
    195 
    196 	err = check_bsdlabel(dd, LABELSECTOR, start, end);
    197 	if (err != 1)
    198 		return (err);
    199 	memset(&pt, 0, sizeof(pt));
    200 	err = ahdi_getparts(dd, &pt, AHDI_BBLOCK, AHDI_BBLOCK);
    201 	if (err < 0) {
    202 		eprintf("Device I/O error (hardware problem?)\n\n");
    203 		return (-1);
    204 	}
    205 	if (!err) {
    206 		/*
    207 		 * Check for hidden BSD labels
    208 		 */
    209 		for (i = 0; i < pt.nparts; i++) {
    210 			if (!strncmp(pt.parts[i].id, "NBD", 3)) {
    211 				err = check_bsdlabel(dd, pt.parts[i].start, start, end);
    212 				if (err != 1)
    213 					return (err);
    214 			}
    215 		}
    216 		for (i = 0; i < pt.nparts; i++) {
    217 			if (!strncmp(pt.parts[i].id, "SWP", 3))
    218 				break;
    219 		}
    220 		if (i < pt.nparts) {
    221 			*start = pt.parts[i].start;
    222 			*end   = pt.parts[i].end;
    223 			eprintf("AHDI format, SWP partition: start:%d,end: %d\n",
    224 				*start, *end);
    225 			return (0);
    226 		}
    227 		eprintf("AHDI format, no swap ('SWP') partition found!\n");
    228 	}
    229 	eprintf("Unknown label format.\n\n");
    230 	return(-1);
    231 }
    232