Home | History | Annotate | Line # | Download | only in bootst
dev_tape.c revision 1.5.8.2
      1 /*	$NetBSD: dev_tape.c,v 1.5.8.2 2001/07/07 09:06:44 scw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * This module implements a "raw device" interface suitable for
     41  * use by the stand-alone I/O library UFS file-system code, and
     42  * possibly for direct access (i.e. boot from tape).
     43  */
     44 
     45 #include <sys/types.h>
     46 #include <machine/prom.h>
     47 #include <machine/stdarg.h>
     48 
     49 #include <lib/libkern/libkern.h>
     50 
     51 #include "stand.h"
     52 #include "libsa.h"
     53 #include "dev_tape.h"
     54 
     55 extern int debug;
     56 
     57 struct mvmeprom_dskio tape_ioreq;
     58 
     59 static int hackprom_diskrd(struct mvmeprom_dskio *);
     60 
     61 /*
     62  * This is a special version of devopen() for tape boot.
     63  * In this version, the file name is a numeric string of
     64  * one digit, which is passed to the device open so it
     65  * can open the appropriate tape segment.
     66  */
     67 int
     68 devopen(f, fname, file)
     69 	struct open_file *f;
     70 	const char *fname;		/* normally "1" */
     71 	char **file;
     72 {
     73 	struct devsw *dp;
     74 
     75 	*file = (char*)fname;
     76 	dp = &devsw[0];
     77 	f->f_dev = dp;
     78 
     79 	/* The following will call tape_open() */
     80 	return (dp->dv_open(f, fname));
     81 }
     82 
     83 int
     84 tape_open(struct open_file *f, ...)
     85 {
     86 	char *fname;		/* partition number, i.e. "1" */
     87 	int	part;
     88 	struct mvmeprom_dskio *ti;
     89 	va_list ap;
     90 
     91 	va_start(ap, f);
     92 	fname = va_arg(ap, char *);
     93 	va_end(ap);
     94 
     95 	/*
     96 	 * Set the tape segment number to the one indicated
     97 	 * by the single digit fname passed in above.
     98 	 */
     99 	if ((fname[0] < '0') && (fname[0] > '9')) {
    100 		return ENOENT;
    101 	}
    102 	part = fname[0] - '0';
    103 
    104 	/*
    105 	 * Setup our part of the saioreq.
    106 	 * (determines what gets opened)
    107 	 */
    108 	ti = &tape_ioreq;
    109 	memset((caddr_t)ti, 0, sizeof(*ti));
    110 
    111 	ti->ctrl_lun = bugargs.ctrl_lun;
    112 	ti->dev_lun = bugargs.dev_lun;
    113 	ti->status = 0;
    114 	ti->pbuffer = NULL;
    115 	ti->blk_num = part;
    116 	ti->blk_cnt = 0;
    117 	ti->flag = 0;
    118 	ti->addr_mod = 0;
    119 
    120 	f->f_devdata = ti;
    121 
    122 	return (0);
    123 }
    124 
    125 int
    126 tape_close(f)
    127 	struct open_file *f;
    128 {
    129 	struct mvmeprom_dskio *ti;
    130 
    131 
    132 	ti = f->f_devdata;
    133 	f->f_devdata = NULL;
    134 	return 0;
    135 }
    136 
    137 #define MVMEPROM_SCALE (512/MVMEPROM_BLOCK_SIZE)
    138 
    139 int
    140 tape_strategy(devdata, flag, dblk, size, buf, rsize)
    141 	void	*devdata;
    142 	int	flag;
    143 	daddr_t	dblk;
    144 	u_int	size;
    145 	void	*buf;
    146 	u_int	*rsize;
    147 {
    148 	struct mvmeprom_dskio *ti;
    149 	int ret;
    150 
    151 	ti = devdata;
    152 
    153 	if (flag != F_READ)
    154 		return(EROFS);
    155 
    156 	ti->status = 0;
    157 	ti->pbuffer = buf;
    158 	/* don't change block #, set in open */
    159 	ti->blk_cnt = size / (512 / MVMEPROM_SCALE);
    160 
    161 	/* work around for stupid '147 prom bug */
    162 	if ( bugargs.cputyp == 0x147 )
    163 		ret = hackprom_diskrd(ti);
    164 	else
    165 		ret = mvmeprom_diskrd(ti);
    166 
    167 	if (ret != 0)
    168 		return (EIO);
    169 
    170 	*rsize = (ti->blk_cnt / MVMEPROM_SCALE) * 512;
    171 	ti->flag |= IGNORE_FILENUM; /* ignore next time */
    172 
    173 	return (0);
    174 }
    175 
    176 int
    177 tape_ioctl(struct open_file *f, u_long cmd, void * data)
    178 {
    179 	return EIO;
    180 }
    181 
    182 static int
    183 hackprom_diskrd(struct mvmeprom_dskio *ti)
    184 {
    185 	static int blkoffset = 0;
    186 
    187 #define	hackload_addr	((char *) 0x080000)	/* Load tape segment here */
    188 #define hackload_blocks 0x2000			/* 2Mb worth */
    189 
    190 	if ( (ti->flag & IGNORE_FILENUM) == 0 ) {
    191 		/*
    192 		 * First time through. Load the whole tape segment...
    193 		 */
    194 		struct mvmeprom_dskio nti;
    195 		int ret;
    196 
    197 		nti = *ti;
    198 
    199 		nti.pbuffer = hackload_addr;
    200 		nti.blk_cnt = hackload_blocks;
    201 		nti.flag |= END_OF_FILE;
    202 
    203 		ret = mvmeprom_diskrd(&nti);
    204 
    205 		/*
    206 		 * PROM returns 1 on end-of-file. This isn't an
    207 		 * error in this instance, just in case you're wondering! ;-)
    208 		 */
    209 		if ( ret < 0 || ret > 1 )
    210 			return ret;
    211 
    212 		blkoffset = 0;
    213 	}
    214 
    215 	/*
    216 	 * Grab the required number of block(s)
    217 	 */
    218 	memcpy(ti->pbuffer, &(hackload_addr[blkoffset]),
    219 	      ti->blk_cnt * MVMEPROM_BLOCK_SIZE);
    220 
    221 	blkoffset += (ti->blk_cnt * MVMEPROM_BLOCK_SIZE);
    222 
    223 	return 0;
    224 }
    225