fileread_bfs.c revision 1.1.18.2 1 1.1.18.2 yamt /* $NetBSD: fileread_bfs.c,v 1.1.18.2 2006/06/21 14:51:21 yamt Exp $ */
2 1.1.18.2 yamt
3 1.1.18.2 yamt /*-
4 1.1.18.2 yamt * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5 1.1.18.2 yamt * All rights reserved.
6 1.1.18.2 yamt *
7 1.1.18.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.18.2 yamt * by UCHIYAMA Yasushi.
9 1.1.18.2 yamt *
10 1.1.18.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.18.2 yamt * modification, are permitted provided that the following conditions
12 1.1.18.2 yamt * are met:
13 1.1.18.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.18.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.18.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.1.18.2 yamt * must display the following acknowledgement:
20 1.1.18.2 yamt * This product includes software developed by the NetBSD
21 1.1.18.2 yamt * Foundation, Inc. and its contributors.
22 1.1.18.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.18.2 yamt * contributors may be used to endorse or promote products derived
24 1.1.18.2 yamt * from this software without specific prior written permission.
25 1.1.18.2 yamt *
26 1.1.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.18.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.18.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.18.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.18.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.18.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.18.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.18.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.18.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.18.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.18.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.18.2 yamt */
38 1.1.18.2 yamt
39 1.1.18.2 yamt #include <lib/libsa/stand.h>
40 1.1.18.2 yamt #include <lib/libkern/libkern.h>
41 1.1.18.2 yamt
42 1.1.18.2 yamt #include <sys/types.h>
43 1.1.18.2 yamt #include <sys/param.h>
44 1.1.18.2 yamt #include "bootxx.h"
45 1.1.18.2 yamt
46 1.1.18.2 yamt #include <machine/pdinfo.h>
47 1.1.18.2 yamt #include <machine/vtoc.h>
48 1.1.18.2 yamt #include <machine/bfs.h>
49 1.1.18.2 yamt #include <machine/sbd.h>
50 1.1.18.2 yamt
51 1.1.18.2 yamt int
52 1.1.18.2 yamt fileread(const char *fname, size_t *size)
53 1.1.18.2 yamt {
54 1.1.18.2 yamt struct pdinfo_sector *pdinfo = (void *)SDBOOT_PDINFOADDR;
55 1.1.18.2 yamt struct vtoc_sector *vtoc = (void *)SDBOOT_VTOCADDR;
56 1.1.18.2 yamt struct ux_partition *partition = vtoc->partition;
57 1.1.18.2 yamt struct bfs_inode *inode = (void *)SDBOOT_INODEADDR;
58 1.1.18.2 yamt struct bfs_dirent *dirent = (void *)SDBOOT_DIRENTADDR;
59 1.1.18.2 yamt int i, n, err, block_size, bfs_sector;
60 1.1.18.2 yamt
61 1.1.18.2 yamt if (pdinfo->magic != PDINFO_MAGIC)
62 1.1.18.2 yamt return BERR_PDINFO;
63 1.1.18.2 yamt block_size = pdinfo->geometory.bytes_per_sector;
64 1.1.18.2 yamt
65 1.1.18.2 yamt #if 0
66 1.1.18.2 yamt {
67 1.1.18.2 yamt char msg[] = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f";
68 1.1.18.2 yamt const char hex[] = "0123456789ABCDEF";
69 1.1.18.2 yamt u_int v;
70 1.1.18.2 yamt uint8_t *p = (void *)vtoc;
71 1.1.18.2 yamt
72 1.1.18.2 yamt err = dk_read(pdinfo->logical_sector + VTOC_SECTOR, 1, vtoc);
73 1.1.18.2 yamt for (n = 0; n < 16; n++) {
74 1.1.18.2 yamt for (i = 0; i < 16; i++) {
75 1.1.18.2 yamt v = (*p >> 4) & 0x0f;
76 1.1.18.2 yamt msg[i*3] = hex[v];
77 1.1.18.2 yamt v = *p & 0x0f;
78 1.1.18.2 yamt msg[i*3+1] = hex[v];
79 1.1.18.2 yamt p++;
80 1.1.18.2 yamt }
81 1.1.18.2 yamt for (i = 0; msg[i] != 0; i++)
82 1.1.18.2 yamt ROM_PUTC(32 + i * 12, 0 + n * 24, msg[i]);
83 1.1.18.2 yamt ROM_PUTC(0, 0 + n * 24, '\r');
84 1.1.18.2 yamt ROM_PUTC(0, 0 + n * 24, '\n');
85 1.1.18.2 yamt }
86 1.1.18.2 yamt }
87 1.1.18.2 yamt #endif
88 1.1.18.2 yamt /* Read VTOC */
89 1.1.18.2 yamt err = dk_read(pdinfo->logical_sector + VTOC_SECTOR, 1, vtoc);
90 1.1.18.2 yamt if ((err & 0x7f) != 0)
91 1.1.18.2 yamt return BERR_RDVTOC;
92 1.1.18.2 yamt
93 1.1.18.2 yamt if (vtoc->magic != VTOC_MAGIC)
94 1.1.18.2 yamt return BERR_VTOC;
95 1.1.18.2 yamt
96 1.1.18.2 yamt /* Find BFS */
97 1.1.18.2 yamt for (i = 0; i < VTOC_MAXPARTITIONS; i++, partition++)
98 1.1.18.2 yamt if (partition->tag == VTOC_TAG_STAND)
99 1.1.18.2 yamt break;
100 1.1.18.2 yamt
101 1.1.18.2 yamt if (i == VTOC_MAXPARTITIONS)
102 1.1.18.2 yamt return BERR_NOBFS;
103 1.1.18.2 yamt bfs_sector = pdinfo->logical_sector + partition->start_sector;
104 1.1.18.2 yamt
105 1.1.18.2 yamt /* Read inode */
106 1.1.18.2 yamt err = dk_read(bfs_sector + 1/* skip super block */, 1, inode);
107 1.1.18.2 yamt if ((err & 0x7f) != 0)
108 1.1.18.2 yamt return BERR_RDINODE;
109 1.1.18.2 yamt
110 1.1.18.2 yamt if (inode->number != BFS_ROOT_INODE)
111 1.1.18.2 yamt return BERR_NOROOT;
112 1.1.18.2 yamt
113 1.1.18.2 yamt /* Read root directory */
114 1.1.18.2 yamt n = inode->eof_offset_byte - inode->start_sector * 512 + 1;
115 1.1.18.2 yamt
116 1.1.18.2 yamt err = dk_read(bfs_sector + inode->start_sector, 1, dirent);
117 1.1.18.2 yamt if ((err & 0x7f) != 0)
118 1.1.18.2 yamt return BERR_RDDIRENT;
119 1.1.18.2 yamt
120 1.1.18.2 yamt n /= sizeof(struct bfs_dirent);
121 1.1.18.2 yamt DPRINTF("%d files.\n", n);
122 1.1.18.2 yamt for (i = 0; i < n; i++, dirent++)
123 1.1.18.2 yamt if (strcmp(dirent->name, fname) == 0)
124 1.1.18.2 yamt break;
125 1.1.18.2 yamt
126 1.1.18.2 yamt if (i == n)
127 1.1.18.2 yamt return BERR_NOFILE;
128 1.1.18.2 yamt
129 1.1.18.2 yamt /* Read file */
130 1.1.18.2 yamt DPRINTF("%s (%d)\n", dirent->name, dirent->inode);
131 1.1.18.2 yamt inode = &inode[dirent->inode - BFS_ROOT_INODE];
132 1.1.18.2 yamt
133 1.1.18.2 yamt err = dk_read(bfs_sector + inode->start_sector,
134 1.1.18.2 yamt inode->end_sector - inode->start_sector + 1,
135 1.1.18.2 yamt (void *)SDBOOT_SCRATCHADDR);
136 1.1.18.2 yamt
137 1.1.18.2 yamt if ((err & 0x7f) != 0)
138 1.1.18.2 yamt return BERR_RDFILE;
139 1.1.18.2 yamt
140 1.1.18.2 yamt *size = inode->eof_offset_byte - inode->start_sector * 512 + 1;
141 1.1.18.2 yamt DPRINTF("read %dbyte\n", *size);
142 1.1.18.2 yamt
143 1.1.18.2 yamt return 0;
144 1.1.18.2 yamt }
145