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