Home | History | Annotate | Line # | Download | only in common
datafs.c revision 1.1
      1 /*	$NetBSD: datafs.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <lib/libsa/stand.h>
     40 #include <lib/libkern/libkern.h>
     41 
     42 #include "local.h"
     43 
     44 FS_DEF(data);
     45 
     46 struct fs_ops datafs_ops = {
     47 	data_open, data_close, data_read, data_write, data_seek, data_stat
     48 };
     49 
     50 struct datafs {
     51 	uint8_t *ptr;
     52 	uint8_t *end;
     53 	uint8_t *start;
     54 	size_t size;
     55 } __data;
     56 
     57 void
     58 data_attach(void *addr, size_t size)
     59 {
     60 
     61 	__data.start = addr;
     62 	__data.size = size;
     63 }
     64 
     65 int
     66 data_open(const char *name, struct open_file *f)
     67 {
     68 
     69 	if (__data.size == 0) {
     70 		printf("no data\n");
     71 		return -1;
     72 	}
     73 
     74 	__data.ptr = __data.start;
     75 	__data.end = __data.ptr + __data.size;
     76 
     77 	return 0;
     78 }
     79 
     80 int
     81 data_close(struct open_file *f)
     82 {
     83 
     84 	return 0;
     85 }
     86 
     87 int
     88 data_read(struct open_file *f, void *buf, size_t size, size_t *resid)
     89 {
     90 	uint8_t *b = buf;
     91 	size_t sz;
     92 
     93 	twiddle();
     94 	if (__data.ptr + size < __data.end) {
     95 		sz = size;
     96 		memcpy(b, __data.ptr, sz);
     97 		__data.ptr += sz;
     98 	} else {
     99 		sz = (size_t)(__data.end - __data.ptr);
    100 		while (__data.ptr < __data.end)
    101 			*b++ = *__data.ptr++;
    102 	}
    103 
    104 	if (resid)
    105 		*resid = size - sz;
    106 
    107 	return 0;
    108 }
    109 
    110 int
    111 data_write(struct open_file *f, void *start, size_t size, size_t *resid)
    112 {
    113 
    114 	return 0;
    115 }
    116 
    117 off_t
    118 data_seek(struct open_file *f, off_t offset, int where)
    119 {
    120 	uint8_t *p = 0;
    121 
    122 	switch (where) {
    123 	case SEEK_SET:
    124 		p = __data.start + offset;
    125 		break;
    126 	case SEEK_CUR:
    127 		p = __data.ptr + offset;
    128 		break;
    129 	case SEEK_END:
    130 		p = __data.end + offset;
    131 		break;
    132 	default:
    133 		return EINVAL;
    134 	}
    135 
    136 	if (p < __data.start || p >= __data.end) {
    137 		return EINVAL;
    138 	}
    139 
    140 	__data.ptr = p;
    141 
    142 	return (off_t)(p - __data.start);
    143 }
    144 
    145 int
    146 data_stat(struct open_file *f, struct stat *stat)
    147 {
    148 
    149 	stat->st_size = __data.size;
    150 
    151 	return 0;
    152 }
    153