1*a0403cdeSmsaitoh /* $NetBSD: blkdev.c,v 1.9 2019/12/27 09:41:48 msaitoh Exp $ */
239ea9122Scgd
339ea9122Scgd /*
439ea9122Scgd * Copyright (c) 1999 Christopher G. Demetriou. All rights reserved.
539ea9122Scgd *
639ea9122Scgd * Redistribution and use in source and binary forms, with or without
739ea9122Scgd * modification, are permitted provided that the following conditions
839ea9122Scgd * are met:
939ea9122Scgd * 1. Redistributions of source code must retain the above copyright
1039ea9122Scgd * notice, this list of conditions and the following disclaimer.
1139ea9122Scgd * 2. Redistributions in binary form must reproduce the above copyright
1239ea9122Scgd * notice, this list of conditions and the following disclaimer in the
1339ea9122Scgd * documentation and/or other materials provided with the distribution.
1439ea9122Scgd * 3. All advertising materials mentioning features or use of this software
1539ea9122Scgd * must display the following acknowledgement:
1639ea9122Scgd * This product includes software developed by Christopher G. Demetriou
1739ea9122Scgd * for the NetBSD Project.
1839ea9122Scgd * 4. The name of the author may not be used to endorse or promote products
1939ea9122Scgd * derived from this software without specific prior written permission
2039ea9122Scgd *
2139ea9122Scgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2239ea9122Scgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2339ea9122Scgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2439ea9122Scgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2539ea9122Scgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2639ea9122Scgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2739ea9122Scgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2839ea9122Scgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2939ea9122Scgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3039ea9122Scgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3139ea9122Scgd */
3239ea9122Scgd
3339ea9122Scgd /*
3439ea9122Scgd * Copyright (c) 1992, 1993
3539ea9122Scgd * The Regents of the University of California. All rights reserved.
3639ea9122Scgd *
3739ea9122Scgd * This code is derived from software contributed to Berkeley by
3839ea9122Scgd * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
3939ea9122Scgd *
4039ea9122Scgd * Redistribution and use in source and binary forms, with or without
4139ea9122Scgd * modification, are permitted provided that the following conditions
4239ea9122Scgd * are met:
4339ea9122Scgd * 1. Redistributions of source code must retain the above copyright
4439ea9122Scgd * notice, this list of conditions and the following disclaimer.
4539ea9122Scgd * 2. Redistributions in binary form must reproduce the above copyright
4639ea9122Scgd * notice, this list of conditions and the following disclaimer in the
4739ea9122Scgd * documentation and/or other materials provided with the distribution.
48aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
4939ea9122Scgd * may be used to endorse or promote products derived from this software
5039ea9122Scgd * without specific prior written permission.
5139ea9122Scgd *
5239ea9122Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5339ea9122Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5439ea9122Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5539ea9122Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5639ea9122Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5739ea9122Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5839ea9122Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5939ea9122Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6039ea9122Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6139ea9122Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6239ea9122Scgd * SUCH DAMAGE.
6339ea9122Scgd *
6439ea9122Scgd * @(#)rz.c 8.1 (Berkeley) 6/10/93
6539ea9122Scgd */
6639ea9122Scgd
6739ea9122Scgd #include <lib/libsa/stand.h>
68728d6723Sthorpej #include <lib/libkern/libkern.h>
6939ea9122Scgd
7039ea9122Scgd #include <sys/param.h>
7139ea9122Scgd #include <sys/disklabel.h>
7239ea9122Scgd
7339ea9122Scgd #include <machine/prom.h>
7439ea9122Scgd
7539ea9122Scgd #include "stand/common/common.h"
7639ea9122Scgd #include "blkdev.h"
7739ea9122Scgd
78215a3a54Ssimonb #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
79215a3a54Ssimonb
80215a3a54Ssimonb
8139ea9122Scgd /*
8239ea9122Scgd * If BOOTXX_FS_TYPE is defined, we'll try to find and use the first
8339ea9122Scgd * partition with that type mentioned in the disklabel, else default to
8439ea9122Scgd * using block 0.
8539ea9122Scgd *
8639ea9122Scgd * The old boot blocks used to look for a file system starting at block
8739ea9122Scgd * 0. It's not immediately obvious that change here is necessary or good,
8839ea9122Scgd * so for now we don't bother looking for the specific type.
89215a3a54Ssimonb *
90215a3a54Ssimonb * However, for filesystem support on RAID1 sets we use a subset of the
91215a3a54Ssimonb * BOOTXX_FS_TYPE support.
9239ea9122Scgd */
93215a3a54Ssimonb #if defined(BOOTXX_RAID1_SUPPORT)
94215a3a54Ssimonb #define BOOTXX_FS_TYPE
95215a3a54Ssimonb #else
9639ea9122Scgd #undef BOOTXX_FS_TYPE
97215a3a54Ssimonb #endif
9839ea9122Scgd
9939ea9122Scgd int blkdev_is_open;
10039ea9122Scgd u_int32_t blkdev_part_offset;
10139ea9122Scgd
10239ea9122Scgd /*
10339ea9122Scgd * Since we have only one device, and want to squeeze space, we just
10439ea9122Scgd * short-circuit devopen() to do the disk open as well.
10539ea9122Scgd *
10639ea9122Scgd * Devopen is supposed to decode the string 'fname', open the device,
10739ea9122Scgd * and make 'file' point to the remaining file name. Since we don't
10839ea9122Scgd * do any device munging, we can just set *file to fname.
10939ea9122Scgd */
11039ea9122Scgd int
devopen(struct open_file * f,const char * fname,char ** file)11182357f6dSdsl devopen(struct open_file *f, const char *fname, char **file)
11282357f6dSdsl /* file: out */
11339ea9122Scgd {
11439ea9122Scgd #if defined(BOOTXX_FS_TYPE)
11539ea9122Scgd int i;
11639ea9122Scgd size_t cnt;
11739ea9122Scgd char *msg, buf[DEV_BSIZE];
11839ea9122Scgd struct disklabel l;
11939ea9122Scgd #endif /* defined(BOOTXX_FS_TYPE) */
12039ea9122Scgd
12139ea9122Scgd if (blkdev_is_open)
12239ea9122Scgd return (EBUSY);
12339ea9122Scgd
12439ea9122Scgd *file = (char *)fname;
12539ea9122Scgd
12639ea9122Scgd #if 0
12739ea9122Scgd f->f_devdata = NULL; /* no point */
12839ea9122Scgd #endif
12939ea9122Scgd
13039ea9122Scgd /* Try to read disk label and partition table information. */
13139ea9122Scgd blkdev_part_offset = 0;
13239ea9122Scgd #if defined(BOOTXX_FS_TYPE)
133215a3a54Ssimonb i = blkdevstrategy(NULL, F_READ,
13439ea9122Scgd (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
13539ea9122Scgd if (i || cnt != DEV_BSIZE) {
13639ea9122Scgd return (ENXIO);
13739ea9122Scgd }
13839ea9122Scgd msg = getdisklabel(buf, &l);
13939ea9122Scgd if (msg == NULL) {
140215a3a54Ssimonb #if defined(BOOTXX_RAID1_SUPPORT)
141215a3a54Ssimonb if (l.d_partitions[0].p_fstype == FS_RAID) {
142215a3a54Ssimonb blkdev_part_offset = RF_PROTECTED_SECTORS;
143215a3a54Ssimonb }
144215a3a54Ssimonb #else
14539ea9122Scgd /*
14639ea9122Scgd * there's a label. find the first partition of the
14739ea9122Scgd * type we want and use its offset. if none are
14839ea9122Scgd * found, we just use offset 0.
14939ea9122Scgd */
15039ea9122Scgd for (i = 0; i < l.d_npartitions; i++) {
15139ea9122Scgd if (l.d_partitions[i].p_fstype == BOOTXX_FS_TYPE) {
15239ea9122Scgd blkdev_part_offset = l.d_partitions[i].p_offset;
15339ea9122Scgd break;
15439ea9122Scgd }
15539ea9122Scgd }
156215a3a54Ssimonb #endif
15739ea9122Scgd } else {
15839ea9122Scgd /* just use offset 0; it's already set that way */
15939ea9122Scgd }
16039ea9122Scgd #endif /* defined(BOOTXX_FS_TYPE) */
16139ea9122Scgd
16239ea9122Scgd blkdev_is_open = 1;
16339ea9122Scgd return (0);
16439ea9122Scgd }
16539ea9122Scgd
16639ea9122Scgd int
blkdevstrategy(void * devdata,int rw,daddr_t bn,size_t reqcnt,void * addrvoid,size_t * cnt)16782357f6dSdsl blkdevstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addrvoid, size_t *cnt)
168*a0403cdeSmsaitoh /* cnt: out: number of bytes transferred */
16939ea9122Scgd {
17039ea9122Scgd char *addr = addrvoid;
17139ea9122Scgd prom_return_t ret;
17239ea9122Scgd
17339ea9122Scgd if ((reqcnt & 0xffffff) != reqcnt ||
17439ea9122Scgd reqcnt == 0)
1752d65de24Sperry __asm("call_pal 0");
17639ea9122Scgd
17739ea9122Scgd twiddle();
17839ea9122Scgd
17939ea9122Scgd /* Partial-block transfers not handled. */
18039ea9122Scgd if (reqcnt & (DEV_BSIZE - 1)) {
18139ea9122Scgd *cnt = 0;
18239ea9122Scgd return (EINVAL);
18339ea9122Scgd }
18439ea9122Scgd
18539ea9122Scgd ret.bits = prom_read(booted_dev_fd, reqcnt, addr,
18639ea9122Scgd bn + blkdev_part_offset);
18739ea9122Scgd if (ret.u.status)
18839ea9122Scgd return (EIO);
18939ea9122Scgd *cnt = ret.u.retval;
19039ea9122Scgd return (0);
19139ea9122Scgd }
19239ea9122Scgd
19339ea9122Scgd #if !defined(LIBSA_NO_FS_CLOSE)
19439ea9122Scgd int
blkdevclose(struct open_file * f)19539ea9122Scgd blkdevclose(struct open_file *f)
19639ea9122Scgd {
19739ea9122Scgd
19839ea9122Scgd blkdev_is_open = 0;
19939ea9122Scgd return (0);
20039ea9122Scgd }
20139ea9122Scgd #endif /* !defined(LIBSA_NO_FS_CLOSE) */
202