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