1 /* $NetBSD: fileread_bfs.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $ */
4 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <lib/libsa/stand.h>
33 #include <lib/libkern/libkern.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
39 #include <machine/pdinfo.h>
40 #include <machine/vtoc.h>
41 #include <machine/bfs.h>
42 #include <machine/sbd.h>
45 fileread(const char *fname
, size_t *size
)
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
;
54 if (pdinfo
->magic
!= PDINFO_MAGIC
)
56 block_size
= pdinfo
->geometory
.bytes_per_sector
;
60 char msg
[] = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f";
61 const char hex
[] = "0123456789ABCDEF";
63 uint8_t *p
= (void *)vtoc
;
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
++) {
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');
82 err
= dk_read(pdinfo
->logical_sector
+ VTOC_SECTOR
, 1, vtoc
);
83 if ((err
& 0x7f) != 0)
86 if (vtoc
->magic
!= VTOC_MAGIC
)
90 for (i
= 0; i
< VTOC_MAXPARTITIONS
; i
++, partition
++)
91 if (partition
->tag
== VTOC_TAG_STAND
)
94 if (i
== VTOC_MAXPARTITIONS
)
96 bfs_sector
= pdinfo
->logical_sector
+ partition
->start_sector
;
99 err
= dk_read(bfs_sector
+ 1/* skip super block */, 1, inode
);
100 if ((err
& 0x7f) != 0)
103 if (inode
->number
!= BFS_ROOT_INODE
)
106 /* Read root directory */
107 n
= inode
->eof_offset_byte
- inode
->start_sector
* 512 + 1;
109 err
= dk_read(bfs_sector
+ inode
->start_sector
, 1, dirent
);
110 if ((err
& 0x7f) != 0)
111 return BERR_RDDIRENT
;
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)
123 DPRINTF("%s (%d)\n", dirent
->name
, dirent
->inode
);
124 inode
= &inode
[dirent
->inode
- BFS_ROOT_INODE
];
126 err
= dk_read(bfs_sector
+ inode
->start_sector
,
127 inode
->end_sector
- inode
->start_sector
+ 1,
128 (void *)SDBOOT_SCRATCHADDR
);
130 if ((err
& 0x7f) != 0)
133 *size
= inode
->eof_offset_byte
- inode
->start_sector
* 512 + 1;
134 DPRINTF("read %dbyte\n", *size
);