1*a0403cdeSmsaitoh /* $NetBSD: blkdev.c,v 1.7 2019/12/27 09:41:49 msaitoh Exp $ */
2c188bc0cScgd
3c188bc0cScgd /*
4c188bc0cScgd * Copyright (c) 1999 Christopher G. Demetriou. All rights reserved.
5c188bc0cScgd *
6c188bc0cScgd * Redistribution and use in source and binary forms, with or without
7c188bc0cScgd * modification, are permitted provided that the following conditions
8c188bc0cScgd * are met:
9c188bc0cScgd * 1. Redistributions of source code must retain the above copyright
10c188bc0cScgd * notice, this list of conditions and the following disclaimer.
11c188bc0cScgd * 2. Redistributions in binary form must reproduce the above copyright
12c188bc0cScgd * notice, this list of conditions and the following disclaimer in the
13c188bc0cScgd * documentation and/or other materials provided with the distribution.
14c188bc0cScgd * 3. All advertising materials mentioning features or use of this software
15c188bc0cScgd * must display the following acknowledgement:
16c188bc0cScgd * This product includes software developed by Christopher G. Demetriou
17c188bc0cScgd * for the NetBSD Project.
18c188bc0cScgd * 4. The name of the author may not be used to endorse or promote products
19c188bc0cScgd * derived from this software without specific prior written permission
20c188bc0cScgd *
21c188bc0cScgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22c188bc0cScgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23c188bc0cScgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24c188bc0cScgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25c188bc0cScgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26c188bc0cScgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27c188bc0cScgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28c188bc0cScgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29c188bc0cScgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30c188bc0cScgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31c188bc0cScgd */
32c188bc0cScgd
33c188bc0cScgd /*
34c188bc0cScgd * Copyright (c) 1992, 1993
35c188bc0cScgd * The Regents of the University of California. All rights reserved.
36c188bc0cScgd *
37c188bc0cScgd * This code is derived from software contributed to Berkeley by
38c188bc0cScgd * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
39c188bc0cScgd *
40c188bc0cScgd * Redistribution and use in source and binary forms, with or without
41c188bc0cScgd * modification, are permitted provided that the following conditions
42c188bc0cScgd * are met:
43c188bc0cScgd * 1. Redistributions of source code must retain the above copyright
44c188bc0cScgd * notice, this list of conditions and the following disclaimer.
45c188bc0cScgd * 2. Redistributions in binary form must reproduce the above copyright
46c188bc0cScgd * notice, this list of conditions and the following disclaimer in the
47c188bc0cScgd * documentation and/or other materials provided with the distribution.
48aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
49c188bc0cScgd * may be used to endorse or promote products derived from this software
50c188bc0cScgd * without specific prior written permission.
51c188bc0cScgd *
52c188bc0cScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53c188bc0cScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54c188bc0cScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55c188bc0cScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56c188bc0cScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57c188bc0cScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58c188bc0cScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59c188bc0cScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60c188bc0cScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61c188bc0cScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62c188bc0cScgd * SUCH DAMAGE.
63c188bc0cScgd *
64c188bc0cScgd * @(#)rz.c 8.1 (Berkeley) 6/10/93
65c188bc0cScgd */
66c188bc0cScgd
67c188bc0cScgd #include <lib/libsa/stand.h>
68c188bc0cScgd #include <lib/libkern/libkern.h>
69c188bc0cScgd
70c188bc0cScgd #include <sys/param.h>
71c188bc0cScgd #include <sys/disklabel.h>
72c188bc0cScgd
73c188bc0cScgd #include "stand/common/cfe_api.h"
74c188bc0cScgd
75c188bc0cScgd #include "stand/common/common.h"
76c188bc0cScgd #include "blkdev.h"
77c188bc0cScgd
78c188bc0cScgd /*
79c188bc0cScgd * If BOOTXX_FS_TYPE is defined, we'll try to find and use the first
80c188bc0cScgd * partition with that type mentioned in the disklabel, else default to
81c188bc0cScgd * using block 0.
82c188bc0cScgd *
83c188bc0cScgd * The old boot blocks used to look for a file system starting at block
84c188bc0cScgd * 0. It's not immediately obvious that change here is necessary or good,
85c188bc0cScgd * so for now we don't bother looking for the specific type.
86c188bc0cScgd */
87c188bc0cScgd #undef BOOTXX_FS_TYPE
88c188bc0cScgd
89c188bc0cScgd int blkdev_is_open;
90c188bc0cScgd u_int32_t blkdev_part_offset;
91c188bc0cScgd
92c188bc0cScgd /*
93c188bc0cScgd * Since we have only one device, and want to squeeze space, we just
94c188bc0cScgd * short-circuit devopen() to do the disk open as well.
95c188bc0cScgd *
96c188bc0cScgd * Devopen is supposed to decode the string 'fname', open the device,
97c188bc0cScgd * and make 'file' point to the remaining file name. Since we don't
98c188bc0cScgd * do any device munging, we can just set *file to fname.
99c188bc0cScgd */
100c188bc0cScgd int
devopen(struct open_file * f,const char * fname,char ** file)10182357f6dSdsl devopen(struct open_file *f, const char *fname, char **file)
10282357f6dSdsl /* file: out */
103c188bc0cScgd {
104c188bc0cScgd #if defined(BOOTXX_FS_TYPE)
105c188bc0cScgd int i;
106c188bc0cScgd size_t cnt;
107c188bc0cScgd char *msg, buf[DEV_BSIZE];
108c188bc0cScgd struct disklabel l;
109c188bc0cScgd #endif /* defined(BOOTXX_FS_TYPE) */
110c188bc0cScgd
111c188bc0cScgd if (blkdev_is_open) {
112c188bc0cScgd return (EBUSY);
113c188bc0cScgd }
114c188bc0cScgd
115c188bc0cScgd *file = (char *)fname;
116c188bc0cScgd
117c188bc0cScgd #if 0
118c188bc0cScgd f->f_devdata = NULL; /* no point */
119c188bc0cScgd #endif
120c188bc0cScgd
121c188bc0cScgd /* Try to read disk label and partition table information. */
122c188bc0cScgd blkdev_part_offset = 0;
123c188bc0cScgd #if defined(BOOTXX_FS_TYPE)
124c188bc0cScgd
125c188bc0cScgd i = diskstrategy(NULL, F_READ,
126c188bc0cScgd (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
127c188bc0cScgd if (i || cnt != DEV_BSIZE) {
128c188bc0cScgd return (ENXIO);
129c188bc0cScgd }
130c188bc0cScgd msg = getdisklabel(buf, &l);
131c188bc0cScgd if (msg == NULL) {
132c188bc0cScgd /*
133c188bc0cScgd * there's a label. find the first partition of the
134c188bc0cScgd * type we want and use its offset. if none are
135c188bc0cScgd * found, we just use offset 0.
136c188bc0cScgd */
137c188bc0cScgd for (i = 0; i < l.d_npartitions; i++) {
138c188bc0cScgd if (l.d_partitions[i].p_fstype == BOOTXX_FS_TYPE) {
139c188bc0cScgd blkdev_part_offset = l.d_partitions[i].p_offset;
140c188bc0cScgd break;
141c188bc0cScgd }
142c188bc0cScgd }
143c188bc0cScgd } else {
144c188bc0cScgd /* just use offset 0; it's already set that way */
145c188bc0cScgd }
146c188bc0cScgd #endif /* defined(BOOTXX_FS_TYPE) */
147c188bc0cScgd
148c188bc0cScgd blkdev_is_open = 1;
149c188bc0cScgd return (0);
150c188bc0cScgd }
151c188bc0cScgd
152c188bc0cScgd int
blkdevstrategy(void * devdata,int rw,daddr_t bn,size_t reqcnt,void * addrvoid,size_t * cnt)15382357f6dSdsl blkdevstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addrvoid, size_t *cnt)
154*a0403cdeSmsaitoh /* cnt: out: number of bytes transferred */
155c188bc0cScgd {
156edb2ee0cStsutsui unsigned char *addr = addrvoid;
157c188bc0cScgd int res;
158c188bc0cScgd
15923113d90She #if !defined(LIBSA_NO_TWIDDLE)
160c188bc0cScgd twiddle();
16123113d90She #endif
162c188bc0cScgd
163c188bc0cScgd /* Partial-block transfers not handled. */
164c188bc0cScgd if (reqcnt & (DEV_BSIZE - 1)) {
165c188bc0cScgd *cnt = 0;
166c188bc0cScgd return (EINVAL);
167c188bc0cScgd }
168c188bc0cScgd res = cfe_readblk(booted_dev_fd,(bn+blkdev_part_offset)*DEV_BSIZE,addr,reqcnt);
169c188bc0cScgd if (res < 0) return EIO;
170c188bc0cScgd
171c188bc0cScgd *cnt = res;
172c188bc0cScgd return (0);
173c188bc0cScgd }
174c188bc0cScgd
175c188bc0cScgd #if !defined(LIBSA_NO_FS_CLOSE)
176c188bc0cScgd int
blkdevclose(struct open_file * f)177c188bc0cScgd blkdevclose(struct open_file *f)
178c188bc0cScgd {
179c188bc0cScgd
180c188bc0cScgd blkdev_is_open = 0;
181c188bc0cScgd return (0);
182c188bc0cScgd }
183c188bc0cScgd #endif /* !defined(LIBSA_NO_FS_CLOSE) */
184