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