Home | History | Annotate | Line # | Download | only in common
bootfs.c revision 1.2.50.1
      1  1.2.50.1      matt /*	$NetBSD: bootfs.c,v 1.2.50.1 2008/01/09 01:45:58 matt Exp $	*/
      2       1.1   tsutsui 
      3       1.1   tsutsui /*-
      4       1.1   tsutsui  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5       1.1   tsutsui  * All rights reserved.
      6       1.1   tsutsui  *
      7       1.1   tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1   tsutsui  * by UCHIYAMA Yasushi.
      9       1.1   tsutsui  *
     10       1.1   tsutsui  * Redistribution and use in source and binary forms, with or without
     11       1.1   tsutsui  * modification, are permitted provided that the following conditions
     12       1.1   tsutsui  * are met:
     13       1.1   tsutsui  * 1. Redistributions of source code must retain the above copyright
     14       1.1   tsutsui  *    notice, this list of conditions and the following disclaimer.
     15       1.1   tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   tsutsui  *    documentation and/or other materials provided with the distribution.
     18       1.1   tsutsui  * 3. All advertising materials mentioning features or use of this software
     19       1.1   tsutsui  *    must display the following acknowledgement:
     20       1.1   tsutsui  *        This product includes software developed by the NetBSD
     21       1.1   tsutsui  *        Foundation, Inc. and its contributors.
     22       1.1   tsutsui  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1   tsutsui  *    contributors may be used to endorse or promote products derived
     24       1.1   tsutsui  *    from this software without specific prior written permission.
     25       1.1   tsutsui  *
     26       1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1   tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1   tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1   tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1   tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1   tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1   tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1   tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1   tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1   tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1   tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1   tsutsui  */
     38       1.1   tsutsui 
     39       1.1   tsutsui #include <lib/libsa/stand.h>
     40       1.1   tsutsui #include <lib/libkern/libkern.h>
     41       1.1   tsutsui 
     42       1.1   tsutsui #include <machine/param.h>
     43       1.1   tsutsui #include <machine/bfs.h>
     44       1.1   tsutsui #include <machine/sector.h>
     45       1.1   tsutsui 
     46       1.1   tsutsui #include "local.h"
     47       1.1   tsutsui 
     48       1.1   tsutsui /* System V bfs */
     49       1.1   tsutsui 
     50       1.1   tsutsui FS_DEF(bfs);
     51       1.1   tsutsui 
     52       1.1   tsutsui struct fs_ops bfs_ops = {
     53       1.1   tsutsui 	bfs_open, bfs_close, bfs_read, bfs_write, bfs_seek, bfs_stat
     54       1.1   tsutsui };
     55       1.1   tsutsui 
     56       1.1   tsutsui struct bfs_file {
     57       1.1   tsutsui 	struct bfs *bfs;
     58       1.1   tsutsui 	int start, end;
     59       1.1   tsutsui 	size_t size;
     60       1.1   tsutsui 	int cur;
     61       1.1   tsutsui 	uint8_t buf[DEV_BSIZE];
     62       1.1   tsutsui };
     63       1.1   tsutsui 
     64       1.1   tsutsui int
     65       1.1   tsutsui bfs_open(const char *name, struct open_file *f)
     66       1.1   tsutsui {
     67       1.1   tsutsui 	struct bfs_file *file;
     68       1.1   tsutsui 
     69       1.1   tsutsui 	if ((file = alloc(sizeof *file)) == 0)
     70       1.1   tsutsui 		return -1;
     71       1.1   tsutsui 	memset(file, 0, sizeof *file);
     72       1.1   tsutsui 
     73       1.1   tsutsui 	if (bfs_init(&file->bfs) != 0) {
     74       1.2  christos 		dealloc(file, sizeof *file);
     75       1.1   tsutsui 		return -2;
     76       1.1   tsutsui 	}
     77       1.1   tsutsui 
     78       1.1   tsutsui 	if (!bfs_file_lookup(file->bfs, name, &file->start, &file->end,
     79       1.1   tsutsui 	    &file->size)) {
     80       1.1   tsutsui 		bfs_fini(file->bfs);
     81       1.2  christos 		dealloc(file, sizeof *file);
     82       1.1   tsutsui 		return -3;
     83       1.1   tsutsui 	}
     84       1.1   tsutsui 
     85  1.2.50.1      matt 	printf("%s: %s %d %d %d\n", __func__, name, file->start,
     86       1.1   tsutsui 	    file->end, file->size);
     87       1.1   tsutsui 
     88       1.1   tsutsui 	f->f_fsdata = file;
     89       1.1   tsutsui 
     90       1.1   tsutsui 	return 0;
     91       1.1   tsutsui }
     92       1.1   tsutsui 
     93       1.1   tsutsui int
     94       1.1   tsutsui bfs_close(struct open_file *f)
     95       1.1   tsutsui {
     96       1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
     97       1.1   tsutsui 
     98       1.1   tsutsui 	bfs_fini(file->bfs);
     99       1.1   tsutsui 
    100       1.1   tsutsui 	return 0;
    101       1.1   tsutsui }
    102       1.1   tsutsui 
    103       1.1   tsutsui int
    104       1.1   tsutsui bfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
    105       1.1   tsutsui {
    106       1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
    107       1.1   tsutsui 	int n, start, end;
    108       1.1   tsutsui 	uint8_t *p = buf;
    109       1.1   tsutsui 	size_t bufsz = size;
    110       1.1   tsutsui 	int cur = file->cur;
    111       1.1   tsutsui 
    112       1.1   tsutsui 	if (cur + size > file->size)
    113       1.1   tsutsui 		size = file->size - cur;
    114       1.1   tsutsui 
    115       1.1   tsutsui 	start = file->start + (cur >> DEV_BSHIFT);
    116       1.1   tsutsui 	end = file->start + ((cur + size) >> DEV_BSHIFT);
    117       1.1   tsutsui 
    118       1.1   tsutsui 	/* first sector */
    119       1.1   tsutsui 	if (!sector_read(0, file->buf, start))
    120       1.1   tsutsui 		return -2;
    121       1.1   tsutsui 	n = TRUNC_SECTOR(cur) + DEV_BSIZE - cur;
    122       1.1   tsutsui 	if (n >= size) {
    123       1.1   tsutsui 		memcpy(p, file->buf + DEV_BSIZE - n, size);
    124       1.1   tsutsui 		goto done;
    125       1.1   tsutsui 	}
    126       1.1   tsutsui 	memcpy(p, file->buf + DEV_BSIZE - n, n);
    127       1.1   tsutsui 	p += n;
    128       1.1   tsutsui 
    129       1.1   tsutsui 	if ((end - start - 1) > 0) {
    130       1.1   tsutsui 		if (!sector_read_n(0, p, start + 1, end - start - 1))
    131       1.1   tsutsui 			return -2;
    132       1.1   tsutsui 		p += (end - start - 1) * DEV_BSIZE;
    133       1.1   tsutsui 	}
    134       1.1   tsutsui 
    135       1.1   tsutsui 	/* last sector */
    136       1.1   tsutsui 	if (!sector_read(0, file->buf, end))
    137       1.1   tsutsui 		return -2;
    138       1.1   tsutsui 	n = cur + size - TRUNC_SECTOR(cur + size);
    139       1.1   tsutsui 	memcpy(p, file->buf, n);
    140       1.1   tsutsui 
    141       1.1   tsutsui  done:
    142       1.1   tsutsui 	file->cur += size;
    143       1.1   tsutsui 
    144       1.1   tsutsui 	if (resid)
    145       1.1   tsutsui 		*resid = bufsz - size;
    146       1.1   tsutsui 
    147       1.1   tsutsui 	return 0;
    148       1.1   tsutsui }
    149       1.1   tsutsui 
    150       1.1   tsutsui int
    151       1.1   tsutsui bfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
    152       1.1   tsutsui {
    153       1.1   tsutsui 
    154       1.1   tsutsui 	return -1;
    155       1.1   tsutsui }
    156       1.1   tsutsui 
    157       1.1   tsutsui off_t
    158       1.1   tsutsui bfs_seek(struct open_file *f, off_t offset, int where)
    159       1.1   tsutsui {
    160       1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
    161       1.1   tsutsui 	int cur;
    162       1.1   tsutsui 
    163       1.1   tsutsui 	switch (where) {
    164       1.1   tsutsui 	case SEEK_SET:
    165       1.1   tsutsui 		cur = offset;
    166       1.1   tsutsui 		break;
    167       1.1   tsutsui 	case SEEK_CUR:
    168       1.1   tsutsui 		cur = file->cur + offset;
    169       1.1   tsutsui 		break;
    170       1.1   tsutsui 	case SEEK_END:
    171       1.1   tsutsui 		cur = file->size + offset;
    172       1.1   tsutsui 		break;
    173       1.1   tsutsui 	default:
    174       1.1   tsutsui 		return EINVAL;
    175       1.1   tsutsui 	}
    176       1.1   tsutsui 
    177       1.1   tsutsui 	if (cur  < 0 || cur >= file->size) {
    178       1.1   tsutsui 		return EINVAL;
    179       1.1   tsutsui 	}
    180       1.1   tsutsui 
    181       1.1   tsutsui 	file->cur = cur;
    182       1.1   tsutsui 
    183       1.1   tsutsui 	return (off_t)cur;
    184       1.1   tsutsui }
    185       1.1   tsutsui 
    186       1.1   tsutsui int
    187       1.1   tsutsui bfs_stat(struct open_file *f, struct stat *stat)
    188       1.1   tsutsui {
    189       1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
    190       1.1   tsutsui 
    191       1.1   tsutsui 	stat->st_size = file->size;
    192       1.1   tsutsui 
    193       1.1   tsutsui 	return 0;
    194       1.1   tsutsui }
    195