Home | History | Annotate | Line # | Download | only in common
fileread_bfs.c revision 1.1.80.1
      1 /*	$NetBSD: fileread_bfs.c,v 1.1.80.1 2008/05/16 02:22:20 yamt 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <lib/libsa/stand.h>
     33 #include <lib/libkern/libkern.h>
     34 
     35 #include <sys/types.h>
     36 #include <sys/param.h>
     37 #include "bootxx.h"
     38 
     39 #include <machine/pdinfo.h>
     40 #include <machine/vtoc.h>
     41 #include <machine/bfs.h>
     42 #include <machine/sbd.h>
     43 
     44 int
     45 fileread(const char *fname, size_t *size)
     46 {
     47 	struct pdinfo_sector *pdinfo = (void *)SDBOOT_PDINFOADDR;
     48 	struct vtoc_sector *vtoc = (void *)SDBOOT_VTOCADDR;
     49 	struct ux_partition *partition = vtoc->partition;
     50 	struct bfs_inode *inode = (void *)SDBOOT_INODEADDR;
     51 	struct bfs_dirent *dirent = (void *)SDBOOT_DIRENTADDR;
     52 	int i, n, err, block_size, bfs_sector;
     53 
     54 	if (pdinfo->magic != PDINFO_MAGIC)
     55 		return BERR_PDINFO;
     56 	block_size = pdinfo->geometory.bytes_per_sector;
     57 
     58 #if 0
     59 {
     60 	char msg[] = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f";
     61 	const char hex[] = "0123456789ABCDEF";
     62 	u_int v;
     63 	uint8_t *p = (void *)vtoc;
     64 
     65 	err = dk_read(pdinfo->logical_sector + VTOC_SECTOR, 1, vtoc);
     66 	for (n = 0; n < 16; n++) {
     67 		for (i = 0; i < 16; i++) {
     68 			v = (*p >> 4) & 0x0f;
     69 			msg[i*3] = hex[v];
     70 			v = *p & 0x0f;
     71 			msg[i*3+1] = hex[v];
     72 			p++;
     73 		}
     74 		for (i = 0; msg[i] != 0; i++)
     75 			ROM_PUTC(32 + i * 12, 0 + n * 24, msg[i]);
     76 		ROM_PUTC(0, 0 + n * 24, '\r');
     77 		ROM_PUTC(0, 0 + n * 24, '\n');
     78 	}
     79 }
     80 #endif
     81 	/* Read VTOC */
     82 	err = dk_read(pdinfo->logical_sector + VTOC_SECTOR, 1, vtoc);
     83 	if ((err & 0x7f) != 0)
     84 		return BERR_RDVTOC;
     85 
     86 	if (vtoc->magic != VTOC_MAGIC)
     87 		return BERR_VTOC;
     88 
     89 	/* Find BFS */
     90 	for (i = 0; i < VTOC_MAXPARTITIONS; i++, partition++)
     91 		if (partition->tag == VTOC_TAG_STAND)
     92 			break;
     93 
     94 	if (i == VTOC_MAXPARTITIONS)
     95 		return BERR_NOBFS;
     96 	bfs_sector = pdinfo->logical_sector + partition->start_sector;
     97 
     98 	/* Read inode */
     99 	err = dk_read(bfs_sector + 1/* skip super block */, 1, inode);
    100 	if ((err & 0x7f) != 0)
    101 		return BERR_RDINODE;
    102 
    103 	if (inode->number != BFS_ROOT_INODE)
    104 		return BERR_NOROOT;
    105 
    106 	/* Read root directory */
    107 	n = inode->eof_offset_byte - inode->start_sector * 512 + 1;
    108 
    109 	err = dk_read(bfs_sector + inode->start_sector, 1, dirent);
    110 	if ((err & 0x7f) != 0)
    111 		return BERR_RDDIRENT;
    112 
    113 	n /= sizeof(struct bfs_dirent);
    114 	DPRINTF("%d files.\n", n);
    115 	for (i = 0; i < n; i++, dirent++)
    116 		if (strcmp(dirent->name, fname) == 0)
    117 			break;
    118 
    119 	if (i == n)
    120 		return BERR_NOFILE;
    121 
    122 	/* Read file */
    123 	DPRINTF("%s (%d)\n", dirent->name, dirent->inode);
    124 	inode = &inode[dirent->inode - BFS_ROOT_INODE];
    125 
    126 	err = dk_read(bfs_sector + inode->start_sector,
    127 	    inode->end_sector - inode->start_sector + 1,
    128 	    (void *)SDBOOT_SCRATCHADDR);
    129 
    130 	if ((err & 0x7f) != 0)
    131 		return BERR_RDFILE;
    132 
    133 	*size = inode->eof_offset_byte - inode->start_sector * 512 + 1;
    134 	DPRINTF("read %dbyte\n", *size);
    135 
    136 	return 0;
    137 }
    138