1 /* $NetBSD: devopen.c,v 1.9 2007/12/23 03:04:57 tsutsui Exp $ */
2
3 /*-
4 * Copyright (C) 1999 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <lib/libkern/libkern.h>
30 #include <lib/libsa/stand.h>
31 #include <lib/libsa/ufs.h>
32 #include <lib/libsa/ustarfs.h>
33
34 #include <machine/romcall.h>
35
36 #ifdef BOOT_DEBUG
37 # define DPRINTF printf
38 #else
39 # define DPRINTF while (0) printf
40 #endif
41
42 int dkopen(struct open_file *, ...);
43 int dkclose(struct open_file *);
44 int dkstrategy(void *, int, daddr_t, size_t, void *, size_t *);
45
46 struct devsw devsw[] = {
47 { "dk", dkstrategy, dkopen, dkclose, noioctl }
48 };
49 int ndevs = __arraycount(devsw);
50
51 struct fs_ops file_system[] = {
52 FS_OPS(ufs),
53 FS_OPS(ustarfs),
54 };
55 int nfsys = __arraycount(file_system);
56
57 struct romdev {
58 int fd;
59 } romdev;
60
61 int
devopen(struct open_file * f,const char * fname,char ** file)62 devopen(struct open_file *f, const char *fname, char **file)
63 {
64 int fd;
65 char devname[32];
66 char *cp;
67
68 DPRINTF("devopen: %s\n", fname);
69
70 strcpy(devname, fname);
71 cp = strchr(devname, ')') + 1;
72 *cp = 0;
73 fd = rom_open(devname, 0);
74
75 DPRINTF("devname = %s, fd = %d\n", devname, fd);
76 if (fd == -1)
77 return -1;
78
79 romdev.fd = fd;
80
81 f->f_dev = devsw;
82 f->f_devdata = &romdev;
83 *file = strchr(fname, ')') + 1;
84
85 return 0;
86 }
87
88 int
dkopen(struct open_file * f,...)89 dkopen(struct open_file *f, ...)
90 {
91
92 DPRINTF("dkopen\n");
93 return 0;
94 }
95
96 int
dkclose(struct open_file * f)97 dkclose(struct open_file *f)
98 {
99 struct romdev *dev = f->f_devdata;
100
101 DPRINTF("dkclose\n");
102 rom_close(dev->fd);
103 return 0;
104 }
105
106 int
dkstrategy(void * devdata,int rw,daddr_t blk,size_t size,void * buf,size_t * rsize)107 dkstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
108 size_t *rsize)
109 {
110 struct romdev *dev = devdata;
111
112 /* XXX should use partition offset */
113
114 rom_lseek(dev->fd, blk * 512, 0);
115 rom_read(dev->fd, buf, size);
116 *rsize = size;
117 return 0;
118 }
119