1 /* $NetBSD: bootfs.c,v 1.3 2007/12/15 00:39:16 perry Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <lib/libsa/stand.h>
33 #include <lib/libkern/libkern.h>
35 #include <machine/param.h>
36 #include <machine/bfs.h>
37 #include <machine/sector.h>
45 struct fs_ops bfs_ops
= {
46 bfs_open
, bfs_close
, bfs_read
, bfs_write
, bfs_seek
, bfs_stat
54 uint8_t buf
[DEV_BSIZE
];
58 bfs_open(const char *name
, struct open_file
*f
)
60 struct bfs_file
*file
;
62 if ((file
= alloc(sizeof *file
)) == 0)
64 memset(file
, 0, sizeof *file
);
66 if (bfs_init(&file
->bfs
) != 0) {
67 dealloc(file
, sizeof *file
);
71 if (!bfs_file_lookup(file
->bfs
, name
, &file
->start
, &file
->end
,
74 dealloc(file
, sizeof *file
);
78 printf("%s: %s %d %d %d\n", __func__
, name
, file
->start
,
79 file
->end
, file
->size
);
87 bfs_close(struct open_file
*f
)
89 struct bfs_file
*file
= f
->f_fsdata
;
97 bfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
99 struct bfs_file
*file
= f
->f_fsdata
;
105 if (cur
+ size
> file
->size
)
106 size
= file
->size
- cur
;
108 start
= file
->start
+ (cur
>> DEV_BSHIFT
);
109 end
= file
->start
+ ((cur
+ size
) >> DEV_BSHIFT
);
112 if (!sector_read(0, file
->buf
, start
))
114 n
= TRUNC_SECTOR(cur
) + DEV_BSIZE
- cur
;
116 memcpy(p
, file
->buf
+ DEV_BSIZE
- n
, size
);
119 memcpy(p
, file
->buf
+ DEV_BSIZE
- n
, n
);
122 if ((end
- start
- 1) > 0) {
123 if (!sector_read_n(0, p
, start
+ 1, end
- start
- 1))
125 p
+= (end
- start
- 1) * DEV_BSIZE
;
129 if (!sector_read(0, file
->buf
, end
))
131 n
= cur
+ size
- TRUNC_SECTOR(cur
+ size
);
132 memcpy(p
, file
->buf
, n
);
138 *resid
= bufsz
- size
;
144 bfs_write(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
151 bfs_seek(struct open_file
*f
, off_t offset
, int where
)
153 struct bfs_file
*file
= f
->f_fsdata
;
161 cur
= file
->cur
+ offset
;
164 cur
= file
->size
+ offset
;
170 if (cur
< 0 || cur
>= file
->size
) {
180 bfs_stat(struct open_file
*f
, struct stat
*stat
)
182 struct bfs_file
*file
= f
->f_fsdata
;
184 stat
->st_size
= file
->size
;