Home | History | Annotate | Line # | Download | only in common
bootfs.c revision 1.5
      1  1.5  christos /*	$NetBSD: bootfs.c,v 1.5 2019/01/09 03:28:31 christos 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  *
     19  1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   tsutsui  */
     31  1.1   tsutsui 
     32  1.1   tsutsui #include <lib/libsa/stand.h>
     33  1.1   tsutsui #include <lib/libkern/libkern.h>
     34  1.1   tsutsui 
     35  1.5  christos #include <sys/param.h>
     36  1.1   tsutsui #include <machine/bfs.h>
     37  1.1   tsutsui #include <machine/sector.h>
     38  1.1   tsutsui 
     39  1.1   tsutsui #include "local.h"
     40  1.1   tsutsui 
     41  1.1   tsutsui /* System V bfs */
     42  1.1   tsutsui 
     43  1.1   tsutsui FS_DEF(bfs);
     44  1.1   tsutsui 
     45  1.1   tsutsui struct fs_ops bfs_ops = {
     46  1.1   tsutsui 	bfs_open, bfs_close, bfs_read, bfs_write, bfs_seek, bfs_stat
     47  1.1   tsutsui };
     48  1.1   tsutsui 
     49  1.1   tsutsui struct bfs_file {
     50  1.1   tsutsui 	struct bfs *bfs;
     51  1.1   tsutsui 	int start, end;
     52  1.1   tsutsui 	size_t size;
     53  1.1   tsutsui 	int cur;
     54  1.1   tsutsui 	uint8_t buf[DEV_BSIZE];
     55  1.1   tsutsui };
     56  1.1   tsutsui 
     57  1.1   tsutsui int
     58  1.1   tsutsui bfs_open(const char *name, struct open_file *f)
     59  1.1   tsutsui {
     60  1.1   tsutsui 	struct bfs_file *file;
     61  1.1   tsutsui 
     62  1.1   tsutsui 	if ((file = alloc(sizeof *file)) == 0)
     63  1.1   tsutsui 		return -1;
     64  1.1   tsutsui 	memset(file, 0, sizeof *file);
     65  1.1   tsutsui 
     66  1.1   tsutsui 	if (bfs_init(&file->bfs) != 0) {
     67  1.2  christos 		dealloc(file, sizeof *file);
     68  1.1   tsutsui 		return -2;
     69  1.1   tsutsui 	}
     70  1.1   tsutsui 
     71  1.1   tsutsui 	if (!bfs_file_lookup(file->bfs, name, &file->start, &file->end,
     72  1.1   tsutsui 	    &file->size)) {
     73  1.1   tsutsui 		bfs_fini(file->bfs);
     74  1.2  christos 		dealloc(file, sizeof *file);
     75  1.1   tsutsui 		return -3;
     76  1.1   tsutsui 	}
     77  1.1   tsutsui 
     78  1.3     perry 	printf("%s: %s %d %d %d\n", __func__, name, file->start,
     79  1.1   tsutsui 	    file->end, file->size);
     80  1.1   tsutsui 
     81  1.1   tsutsui 	f->f_fsdata = file;
     82  1.1   tsutsui 
     83  1.1   tsutsui 	return 0;
     84  1.1   tsutsui }
     85  1.1   tsutsui 
     86  1.1   tsutsui int
     87  1.1   tsutsui bfs_close(struct open_file *f)
     88  1.1   tsutsui {
     89  1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
     90  1.1   tsutsui 
     91  1.1   tsutsui 	bfs_fini(file->bfs);
     92  1.1   tsutsui 
     93  1.1   tsutsui 	return 0;
     94  1.1   tsutsui }
     95  1.1   tsutsui 
     96  1.1   tsutsui int
     97  1.1   tsutsui bfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
     98  1.1   tsutsui {
     99  1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
    100  1.1   tsutsui 	int n, start, end;
    101  1.1   tsutsui 	uint8_t *p = buf;
    102  1.1   tsutsui 	size_t bufsz = size;
    103  1.1   tsutsui 	int cur = file->cur;
    104  1.1   tsutsui 
    105  1.1   tsutsui 	if (cur + size > file->size)
    106  1.1   tsutsui 		size = file->size - cur;
    107  1.1   tsutsui 
    108  1.1   tsutsui 	start = file->start + (cur >> DEV_BSHIFT);
    109  1.1   tsutsui 	end = file->start + ((cur + size) >> DEV_BSHIFT);
    110  1.1   tsutsui 
    111  1.1   tsutsui 	/* first sector */
    112  1.1   tsutsui 	if (!sector_read(0, file->buf, start))
    113  1.1   tsutsui 		return -2;
    114  1.1   tsutsui 	n = TRUNC_SECTOR(cur) + DEV_BSIZE - cur;
    115  1.1   tsutsui 	if (n >= size) {
    116  1.1   tsutsui 		memcpy(p, file->buf + DEV_BSIZE - n, size);
    117  1.1   tsutsui 		goto done;
    118  1.1   tsutsui 	}
    119  1.1   tsutsui 	memcpy(p, file->buf + DEV_BSIZE - n, n);
    120  1.1   tsutsui 	p += n;
    121  1.1   tsutsui 
    122  1.1   tsutsui 	if ((end - start - 1) > 0) {
    123  1.1   tsutsui 		if (!sector_read_n(0, p, start + 1, end - start - 1))
    124  1.1   tsutsui 			return -2;
    125  1.1   tsutsui 		p += (end - start - 1) * DEV_BSIZE;
    126  1.1   tsutsui 	}
    127  1.1   tsutsui 
    128  1.1   tsutsui 	/* last sector */
    129  1.1   tsutsui 	if (!sector_read(0, file->buf, end))
    130  1.1   tsutsui 		return -2;
    131  1.1   tsutsui 	n = cur + size - TRUNC_SECTOR(cur + size);
    132  1.1   tsutsui 	memcpy(p, file->buf, n);
    133  1.1   tsutsui 
    134  1.1   tsutsui  done:
    135  1.1   tsutsui 	file->cur += size;
    136  1.1   tsutsui 
    137  1.1   tsutsui 	if (resid)
    138  1.1   tsutsui 		*resid = bufsz - size;
    139  1.1   tsutsui 
    140  1.1   tsutsui 	return 0;
    141  1.1   tsutsui }
    142  1.1   tsutsui 
    143  1.1   tsutsui int
    144  1.1   tsutsui bfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
    145  1.1   tsutsui {
    146  1.1   tsutsui 
    147  1.1   tsutsui 	return -1;
    148  1.1   tsutsui }
    149  1.1   tsutsui 
    150  1.1   tsutsui off_t
    151  1.1   tsutsui bfs_seek(struct open_file *f, off_t offset, int where)
    152  1.1   tsutsui {
    153  1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
    154  1.1   tsutsui 	int cur;
    155  1.1   tsutsui 
    156  1.1   tsutsui 	switch (where) {
    157  1.1   tsutsui 	case SEEK_SET:
    158  1.1   tsutsui 		cur = offset;
    159  1.1   tsutsui 		break;
    160  1.1   tsutsui 	case SEEK_CUR:
    161  1.1   tsutsui 		cur = file->cur + offset;
    162  1.1   tsutsui 		break;
    163  1.1   tsutsui 	case SEEK_END:
    164  1.1   tsutsui 		cur = file->size + offset;
    165  1.1   tsutsui 		break;
    166  1.1   tsutsui 	default:
    167  1.1   tsutsui 		return EINVAL;
    168  1.1   tsutsui 	}
    169  1.1   tsutsui 
    170  1.1   tsutsui 	if (cur  < 0 || cur >= file->size) {
    171  1.1   tsutsui 		return EINVAL;
    172  1.1   tsutsui 	}
    173  1.1   tsutsui 
    174  1.1   tsutsui 	file->cur = cur;
    175  1.1   tsutsui 
    176  1.1   tsutsui 	return (off_t)cur;
    177  1.1   tsutsui }
    178  1.1   tsutsui 
    179  1.1   tsutsui int
    180  1.1   tsutsui bfs_stat(struct open_file *f, struct stat *stat)
    181  1.1   tsutsui {
    182  1.1   tsutsui 	struct bfs_file *file = f->f_fsdata;
    183  1.1   tsutsui 
    184  1.1   tsutsui 	stat->st_size = file->size;
    185  1.1   tsutsui 
    186  1.1   tsutsui 	return 0;
    187  1.1   tsutsui }
    188