blkdev.c revision 1.3
11.3Sandvar/* $NetBSD: blkdev.c,v 1.3 2021/07/24 21:31:33 andvar Exp $ */
21.1Smrg
31.1Smrg/*
41.1Smrg * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
51.1Smrg *
61.1Smrg * Redistribution and use in source and binary forms, with or without
71.1Smrg * modification, are permitted provided that the following conditions
81.1Smrg * are met:
91.1Smrg * 1. Redistributions of source code must retain the above copyright
101.1Smrg *    notice, this list of conditions and the following disclaimer.
111.1Smrg * 2. Redistributions in binary form must reproduce the above copyright
121.1Smrg *    notice, this list of conditions and the following disclaimer in the
131.1Smrg *    documentation and/or other materials provided with the distribution.
141.1Smrg * 3. All advertising materials mentioning features or use of this software
151.1Smrg *    must display the following acknowledgement:
161.1Smrg *      This product includes software developed by Christopher G. Demetriou
171.1Smrg *	for the NetBSD Project.
181.1Smrg * 4. The name of the author may not be used to endorse or promote products
191.1Smrg *    derived from this software without specific prior written permission
201.1Smrg *
211.1Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
221.1Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
231.1Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
241.1Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
251.1Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
261.1Smrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271.1Smrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281.1Smrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291.1Smrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
301.1Smrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311.1Smrg */
321.1Smrg
331.1Smrg/*
341.1Smrg * Copyright (c) 1992, 1993
351.1Smrg *	The Regents of the University of California.  All rights reserved.
361.1Smrg *
371.1Smrg * This code is derived from software contributed to Berkeley by
381.1Smrg * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
391.1Smrg *
401.1Smrg * Redistribution and use in source and binary forms, with or without
411.1Smrg * modification, are permitted provided that the following conditions
421.1Smrg * are met:
431.1Smrg * 1. Redistributions of source code must retain the above copyright
441.1Smrg *    notice, this list of conditions and the following disclaimer.
451.1Smrg * 2. Redistributions in binary form must reproduce the above copyright
461.1Smrg *    notice, this list of conditions and the following disclaimer in the
471.1Smrg *    documentation and/or other materials provided with the distribution.
481.1Smrg * 3. Neither the name of the University nor the names of its contributors
491.1Smrg *    may be used to endorse or promote products derived from this software
501.1Smrg *    without specific prior written permission.
511.1Smrg *
521.1Smrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
531.1Smrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
541.1Smrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
551.1Smrg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
561.1Smrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
571.1Smrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
581.1Smrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
591.1Smrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
601.1Smrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
611.1Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
621.1Smrg * SUCH DAMAGE.
631.1Smrg *
641.1Smrg *	@(#)rz.c	8.1 (Berkeley) 6/10/93
651.1Smrg */
661.1Smrg
671.1Smrg#include <lib/libsa/stand.h>
681.1Smrg#include <lib/libkern/libkern.h>
691.1Smrg
701.1Smrg#include <sys/param.h>
711.1Smrg#include <sys/disklabel.h>
721.1Smrg
731.2Smrg#include "stand/sbmips/common/cfe_api.h"
741.1Smrg
751.2Smrg#include "stand/sbmips/common/common.h"
761.1Smrg#include "blkdev.h"
771.1Smrg
781.1Smrg/*
791.1Smrg * If BOOTXX_FS_TYPE is defined, we'll try to find and use the first
801.1Smrg * partition with that type mentioned in the disklabel, else default to
811.1Smrg * using block 0.
821.1Smrg *
831.1Smrg * The old boot blocks used to look for a file system starting at block
841.1Smrg * 0.  It's not immediately obvious that change here is necessary or good,
851.1Smrg * so for now we don't bother looking for the specific type.
861.1Smrg */
871.1Smrg#undef BOOTXX_FS_TYPE
881.1Smrg
891.1Smrgint		blkdev_is_open;
901.1Smrgu_int32_t	blkdev_part_offset;
911.1Smrg
921.1Smrg/*
931.1Smrg * Since we have only one device, and want to squeeze space, we just
941.1Smrg * short-circuit devopen() to do the disk open as well.
951.1Smrg *
961.1Smrg * Devopen is supposed to decode the string 'fname', open the device,
971.1Smrg * and make 'file' point to the remaining file name.  Since we don't
981.1Smrg * do any device munging, we can just set *file to fname.
991.1Smrg */
1001.1Smrgint
1011.1Smrgdevopen(struct open_file *f, const char *fname, char **file)
1021.1Smrg	/* file:	 out */
1031.1Smrg{
1041.1Smrg#if defined(BOOTXX_FS_TYPE)
1051.1Smrg	int i;
1061.1Smrg	size_t cnt;
1071.1Smrg	char *msg, buf[DEV_BSIZE];
1081.1Smrg	struct disklabel l;
1091.1Smrg#endif /* defined(BOOTXX_FS_TYPE) */
1101.1Smrg
1111.1Smrg	if (blkdev_is_open) {
1121.1Smrg		return (EBUSY);
1131.1Smrg	    }
1141.1Smrg
1151.1Smrg	*file = (char *)fname;
1161.1Smrg
1171.1Smrg#if 0
1181.1Smrg	f->f_devdata = NULL;			/* no point */
1191.1Smrg#endif
1201.1Smrg
1211.1Smrg	/* Try to read disk label and partition table information. */
1221.1Smrg	blkdev_part_offset = 0;
1231.1Smrg#if defined(BOOTXX_FS_TYPE)
1241.1Smrg
1251.1Smrg	i = diskstrategy(NULL, F_READ,
1261.1Smrg	    (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
1271.1Smrg	if (i || cnt != DEV_BSIZE) {
1281.1Smrg		return (ENXIO);
1291.1Smrg	}
1301.1Smrg	msg = getdisklabel(buf, &l);
1311.1Smrg	if (msg == NULL) {
1321.1Smrg		/*
1331.1Smrg		 * there's a label.  find the first partition of the
1341.1Smrg		 * type we want and use its offset.  if none are
1351.1Smrg		 * found, we just use offset 0.
1361.1Smrg		 */
1371.1Smrg		for (i = 0; i < l.d_npartitions; i++) {
1381.1Smrg			if (l.d_partitions[i].p_fstype == BOOTXX_FS_TYPE) {
1391.1Smrg				blkdev_part_offset = l.d_partitions[i].p_offset;
1401.1Smrg				break;
1411.1Smrg			}
1421.1Smrg		}
1431.1Smrg	} else {
1441.1Smrg		/* just use offset 0; it's already set that way */
1451.1Smrg	}
1461.1Smrg#endif /* defined(BOOTXX_FS_TYPE) */
1471.1Smrg
1481.1Smrg	blkdev_is_open = 1;
1491.1Smrg	return (0);
1501.1Smrg}
1511.1Smrg
1521.1Smrgint
1531.1Smrgblkdevstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addrvoid, size_t *cnt)
1541.3Sandvar	/* cnt:	 out: number of bytes transferred */
1551.1Smrg{
1561.1Smrg	unsigned char *addr = addrvoid;
1571.1Smrg	int res;
1581.1Smrg
1591.1Smrg#if !defined(LIBSA_NO_TWIDDLE)
1601.1Smrg	twiddle();
1611.1Smrg#endif
1621.1Smrg
1631.1Smrg	/* Partial-block transfers not handled. */
1641.1Smrg	if (reqcnt & (DEV_BSIZE - 1)) {
1651.1Smrg		*cnt = 0;
1661.1Smrg		return (EINVAL);
1671.1Smrg	}
1681.1Smrg	res = cfe_readblk(booted_dev_fd,(bn+blkdev_part_offset)*DEV_BSIZE,addr,reqcnt);
1691.1Smrg	if (res < 0) return EIO;
1701.1Smrg
1711.1Smrg	*cnt = res;
1721.1Smrg	return (0);
1731.1Smrg}
1741.1Smrg
1751.1Smrg#if !defined(LIBSA_NO_FS_CLOSE)
1761.1Smrgint
1771.1Smrgblkdevclose(struct open_file *f)
1781.1Smrg{
1791.1Smrg
1801.1Smrg	blkdev_is_open = 0;
1811.1Smrg	return (0);
1821.1Smrg}
1831.1Smrg#endif /* !defined(LIBSA_NO_FS_CLOSE) */
184