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