memfs.c revision 1.1 1 1.1 phx /* $NetBSD: memfs.c,v 1.1 2011/03/06 18:22:13 phx Exp $ */
2 1.1 phx
3 1.1 phx /*-
4 1.1 phx * Copyright (c) 2011 Frank Wille.
5 1.1 phx * All rights reserved.
6 1.1 phx *
7 1.1 phx * Written by Frank Wille for The NetBSD Project.
8 1.1 phx *
9 1.1 phx * Redistribution and use in source and binary forms, with or without
10 1.1 phx * modification, are permitted provided that the following conditions
11 1.1 phx * are met:
12 1.1 phx * 1. Redistributions of source code must retain the above copyright
13 1.1 phx * notice, this list of conditions and the following disclaimer.
14 1.1 phx * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 phx * notice, this list of conditions and the following disclaimer in the
16 1.1 phx * documentation and/or other materials provided with the distribution.
17 1.1 phx *
18 1.1 phx * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 phx * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 phx * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 phx * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 phx * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 phx * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 phx * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 phx * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 phx * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 phx * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 phx * POSSIBILITY OF SUCH DAMAGE.
29 1.1 phx */
30 1.1 phx
31 1.1 phx #include <sys/param.h>
32 1.1 phx
33 1.1 phx #include <lib/libsa/stand.h>
34 1.1 phx
35 1.1 phx #include "globals.h"
36 1.1 phx #include "memfs.h"
37 1.1 phx
38 1.1 phx struct memhandle {
39 1.1 phx char *base;
40 1.1 phx off_t off;
41 1.1 phx };
42 1.1 phx
43 1.1 phx int
44 1.1 phx mem_open(const char *path, struct open_file *f)
45 1.1 phx {
46 1.1 phx struct memhandle *mh;
47 1.1 phx
48 1.1 phx mh = alloc(sizeof(struct memhandle));
49 1.1 phx if (mh == NULL)
50 1.1 phx return ENOMEM;
51 1.1 phx mh->base = (char *)read_hex(path);
52 1.1 phx mh->off = 0;
53 1.1 phx f->f_fsdata = mh;
54 1.1 phx return 0;
55 1.1 phx }
56 1.1 phx
57 1.1 phx #ifndef LIBSA_NO_FS_CLOSE
58 1.1 phx int
59 1.1 phx mem_close(struct open_file *f)
60 1.1 phx {
61 1.1 phx
62 1.1 phx dealloc(f->f_fsdata, sizeof(struct memhandle));
63 1.1 phx return 0;
64 1.1 phx }
65 1.1 phx #endif
66 1.1 phx
67 1.1 phx int
68 1.1 phx mem_read(struct open_file *f, void *buf, size_t size, size_t *resid)
69 1.1 phx {
70 1.1 phx struct memhandle *mh;
71 1.1 phx
72 1.1 phx mh = f->f_fsdata;
73 1.1 phx memcpy(buf, mh->base + mh->off, size);
74 1.1 phx mh->off += size;
75 1.1 phx if (resid)
76 1.1 phx *resid = 0;
77 1.1 phx return 0;
78 1.1 phx }
79 1.1 phx
80 1.1 phx #ifndef LIBSA_NO_FS_WRITE
81 1.1 phx int
82 1.1 phx mem_write(struct open_file *f, void *buf, size_t size, size_t *resid)
83 1.1 phx {
84 1.1 phx struct memhandle *mh;
85 1.1 phx
86 1.1 phx mh = f->f_fsdata;
87 1.1 phx memcpy(mh->base + mh->off, buf, size);
88 1.1 phx mh->off += size;
89 1.1 phx if (resid)
90 1.1 phx *resid = 0;
91 1.1 phx return 0;
92 1.1 phx }
93 1.1 phx #endif
94 1.1 phx
95 1.1 phx #ifndef LIBSA_NO_FS_SEEK
96 1.1 phx off_t
97 1.1 phx mem_seek(struct open_file *f, off_t offset, int where)
98 1.1 phx {
99 1.1 phx struct memhandle *mh;
100 1.1 phx
101 1.1 phx mh = f->f_fsdata;
102 1.1 phx switch (where) {
103 1.1 phx case SEEK_SET:
104 1.1 phx mh->off = offset;
105 1.1 phx break;
106 1.1 phx case SEEK_CUR:
107 1.1 phx mh->off += offset;
108 1.1 phx break;
109 1.1 phx default:
110 1.1 phx errno = EOFFSET;
111 1.1 phx return -1;
112 1.1 phx }
113 1.1 phx return mh->off;
114 1.1 phx }
115 1.1 phx #endif
116 1.1 phx
117 1.1 phx int
118 1.1 phx mem_stat(struct open_file *f, struct stat *sb)
119 1.1 phx {
120 1.1 phx
121 1.1 phx return EIO;
122 1.1 phx }
123