Home | History | Annotate | Line # | Download | only in common
fileread_bfs.c revision 1.1
      1  1.1  tsutsui /*	$NetBSD: fileread_bfs.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*-
      4  1.1  tsutsui  * Copyright (c) 2004, 2005 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 <sys/types.h>
     43  1.1  tsutsui #include <sys/param.h>
     44  1.1  tsutsui #include "bootxx.h"
     45  1.1  tsutsui 
     46  1.1  tsutsui #include <machine/pdinfo.h>
     47  1.1  tsutsui #include <machine/vtoc.h>
     48  1.1  tsutsui #include <machine/bfs.h>
     49  1.1  tsutsui #include <machine/sbd.h>
     50  1.1  tsutsui 
     51  1.1  tsutsui int
     52  1.1  tsutsui fileread(const char *fname, size_t *size)
     53  1.1  tsutsui {
     54  1.1  tsutsui 	struct pdinfo_sector *pdinfo = (void *)SDBOOT_PDINFOADDR;
     55  1.1  tsutsui 	struct vtoc_sector *vtoc = (void *)SDBOOT_VTOCADDR;
     56  1.1  tsutsui 	struct ux_partition *partition = vtoc->partition;
     57  1.1  tsutsui 	struct bfs_inode *inode = (void *)SDBOOT_INODEADDR;
     58  1.1  tsutsui 	struct bfs_dirent *dirent = (void *)SDBOOT_DIRENTADDR;
     59  1.1  tsutsui 	int i, n, err, block_size, bfs_sector;
     60  1.1  tsutsui 
     61  1.1  tsutsui 	if (pdinfo->magic != PDINFO_MAGIC)
     62  1.1  tsutsui 		return BERR_PDINFO;
     63  1.1  tsutsui 	block_size = pdinfo->geometory.bytes_per_sector;
     64  1.1  tsutsui 
     65  1.1  tsutsui #if 0
     66  1.1  tsutsui {
     67  1.1  tsutsui 	char msg[] = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f";
     68  1.1  tsutsui 	const char hex[] = "0123456789ABCDEF";
     69  1.1  tsutsui 	u_int v;
     70  1.1  tsutsui 	uint8_t *p = (void *)vtoc;
     71  1.1  tsutsui 
     72  1.1  tsutsui 	err = dk_read(pdinfo->logical_sector + VTOC_SECTOR, 1, vtoc);
     73  1.1  tsutsui 	for (n = 0; n < 16; n++) {
     74  1.1  tsutsui 		for (i = 0; i < 16; i++) {
     75  1.1  tsutsui 			v = (*p >> 4) & 0x0f;
     76  1.1  tsutsui 			msg[i*3] = hex[v];
     77  1.1  tsutsui 			v = *p & 0x0f;
     78  1.1  tsutsui 			msg[i*3+1] = hex[v];
     79  1.1  tsutsui 			p++;
     80  1.1  tsutsui 		}
     81  1.1  tsutsui 		for (i = 0; msg[i] != 0; i++)
     82  1.1  tsutsui 			ROM_PUTC(32 + i * 12, 0 + n * 24, msg[i]);
     83  1.1  tsutsui 		ROM_PUTC(0, 0 + n * 24, '\r');
     84  1.1  tsutsui 		ROM_PUTC(0, 0 + n * 24, '\n');
     85  1.1  tsutsui 	}
     86  1.1  tsutsui }
     87  1.1  tsutsui #endif
     88  1.1  tsutsui 	/* Read VTOC */
     89  1.1  tsutsui 	err = dk_read(pdinfo->logical_sector + VTOC_SECTOR, 1, vtoc);
     90  1.1  tsutsui 	if ((err & 0x7f) != 0)
     91  1.1  tsutsui 		return BERR_RDVTOC;
     92  1.1  tsutsui 
     93  1.1  tsutsui 	if (vtoc->magic != VTOC_MAGIC)
     94  1.1  tsutsui 		return BERR_VTOC;
     95  1.1  tsutsui 
     96  1.1  tsutsui 	/* Find BFS */
     97  1.1  tsutsui 	for (i = 0; i < VTOC_MAXPARTITIONS; i++, partition++)
     98  1.1  tsutsui 		if (partition->tag == VTOC_TAG_STAND)
     99  1.1  tsutsui 			break;
    100  1.1  tsutsui 
    101  1.1  tsutsui 	if (i == VTOC_MAXPARTITIONS)
    102  1.1  tsutsui 		return BERR_NOBFS;
    103  1.1  tsutsui 	bfs_sector = pdinfo->logical_sector + partition->start_sector;
    104  1.1  tsutsui 
    105  1.1  tsutsui 	/* Read inode */
    106  1.1  tsutsui 	err = dk_read(bfs_sector + 1/* skip super block */, 1, inode);
    107  1.1  tsutsui 	if ((err & 0x7f) != 0)
    108  1.1  tsutsui 		return BERR_RDINODE;
    109  1.1  tsutsui 
    110  1.1  tsutsui 	if (inode->number != BFS_ROOT_INODE)
    111  1.1  tsutsui 		return BERR_NOROOT;
    112  1.1  tsutsui 
    113  1.1  tsutsui 	/* Read root directory */
    114  1.1  tsutsui 	n = inode->eof_offset_byte - inode->start_sector * 512 + 1;
    115  1.1  tsutsui 
    116  1.1  tsutsui 	err = dk_read(bfs_sector + inode->start_sector, 1, dirent);
    117  1.1  tsutsui 	if ((err & 0x7f) != 0)
    118  1.1  tsutsui 		return BERR_RDDIRENT;
    119  1.1  tsutsui 
    120  1.1  tsutsui 	n /= sizeof(struct bfs_dirent);
    121  1.1  tsutsui 	DPRINTF("%d files.\n", n);
    122  1.1  tsutsui 	for (i = 0; i < n; i++, dirent++)
    123  1.1  tsutsui 		if (strcmp(dirent->name, fname) == 0)
    124  1.1  tsutsui 			break;
    125  1.1  tsutsui 
    126  1.1  tsutsui 	if (i == n)
    127  1.1  tsutsui 		return BERR_NOFILE;
    128  1.1  tsutsui 
    129  1.1  tsutsui 	/* Read file */
    130  1.1  tsutsui 	DPRINTF("%s (%d)\n", dirent->name, dirent->inode);
    131  1.1  tsutsui 	inode = &inode[dirent->inode - BFS_ROOT_INODE];
    132  1.1  tsutsui 
    133  1.1  tsutsui 	err = dk_read(bfs_sector + inode->start_sector,
    134  1.1  tsutsui 	    inode->end_sector - inode->start_sector + 1,
    135  1.1  tsutsui 	    (void *)SDBOOT_SCRATCHADDR);
    136  1.1  tsutsui 
    137  1.1  tsutsui 	if ((err & 0x7f) != 0)
    138  1.1  tsutsui 		return BERR_RDFILE;
    139  1.1  tsutsui 
    140  1.1  tsutsui 	*size = inode->eof_offset_byte - inode->start_sector * 512 + 1;
    141  1.1  tsutsui 	DPRINTF("read %dbyte\n", *size);
    142  1.1  tsutsui 
    143  1.1  tsutsui 	return 0;
    144  1.1  tsutsui }
    145