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