ppc64: Don't set Kp bit on SLB
[openbios/afaerber.git] / fs / grubfs / fsys_ffs.c
blobc6804b8ba43ab5f5db8407338512f311246bd56a
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 * MA 02110-1301, USA.
22 * Elements of this file were originally from the FreeBSD "biosboot"
23 * bootloader file "disk.c" dated 4/12/95.
25 * The license and header comments from that file are included here.
29 * Mach Operating System
30 * Copyright (c) 1992, 1991 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
53 * from: Mach, Revision 2.2 92/04/04 11:35:49 rpd
54 * $Id: fsys_ffs.c,v 1.10 2001/11/12 06:57:29 okuji Exp $
57 #ifdef FSYS_FFS
59 #include "shared.h"
61 #include "filesys.h"
63 #include "defs.h"
64 #include "disk_inode.h"
65 #include "disk_inode_ffs.h"
66 #include "dir.h"
67 #include "fs.h"
69 /* used for filesystem map blocks */
70 static int mapblock;
71 static int mapblock_offset;
72 static int mapblock_bsize;
74 /* pointer to superblock */
75 #define SUPERBLOCK ((struct fs *) ( FSYS_BUF + 8192 ))
76 #define INODE ((struct icommon *) ( FSYS_BUF + 16384 ))
77 #define MAPBUF ( FSYS_BUF + 24576 )
78 #define MAPBUF_LEN 8192
81 int
82 ffs_mount (void)
84 int retval = 1;
86 if ((((current_drive & 0x80) || (current_slice != 0))
87 && ! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_BSDFFS))
88 || part_length < (SBLOCK + (SBSIZE / DEV_BSIZE))
89 || !devread (SBLOCK, 0, SBSIZE, (char *) SUPERBLOCK)
90 || SUPERBLOCK->fs_magic != FS_MAGIC)
91 retval = 0;
93 mapblock = -1;
94 mapblock_offset = -1;
96 return retval;
99 static int
100 block_map (int file_block)
102 int bnum, offset, bsize;
104 if (file_block < NDADDR)
105 return (INODE->i_db[file_block]);
107 /* If the blockmap loaded does not include FILE_BLOCK,
108 load a new blockmap. */
109 if ((bnum = fsbtodb (SUPERBLOCK, INODE->i_ib[0])) != mapblock
110 || (mapblock_offset <= bnum && bnum <= mapblock_offset + mapblock_bsize))
112 if (MAPBUF_LEN < SUPERBLOCK->fs_bsize)
114 offset = ((file_block - NDADDR) % NINDIR (SUPERBLOCK));
115 bsize = MAPBUF_LEN;
117 if (offset + MAPBUF_LEN > SUPERBLOCK->fs_bsize)
118 offset = (SUPERBLOCK->fs_bsize - MAPBUF_LEN) / sizeof (int);
120 else
122 bsize = SUPERBLOCK->fs_bsize;
123 offset = 0;
126 if (! devread (bnum, offset * sizeof (int), bsize, (char *) MAPBUF))
128 mapblock = -1;
129 mapblock_bsize = -1;
130 mapblock_offset = -1;
131 errnum = ERR_FSYS_CORRUPT;
132 return -1;
135 mapblock = bnum;
136 mapblock_bsize = bsize;
137 mapblock_offset = offset;
140 return (((int *) MAPBUF)[((file_block - NDADDR) % NINDIR (SUPERBLOCK))
141 - mapblock_offset]);
146 ffs_read (char *buf, int len)
148 int logno, off, size, map, ret = 0;
150 while (len && !errnum)
152 off = blkoff (SUPERBLOCK, filepos);
153 logno = lblkno (SUPERBLOCK, filepos);
154 size = blksize (SUPERBLOCK, INODE, logno);
156 if ((map = block_map (logno)) < 0)
157 break;
159 size -= off;
161 if (size > len)
162 size = len;
164 disk_read_func = disk_read_hook;
166 devread (fsbtodb (SUPERBLOCK, map), off, size, buf);
168 disk_read_func = NULL;
170 buf += size;
171 len -= size;
172 filepos += size;
173 ret += size;
176 if (errnum)
177 ret = 0;
179 return ret;
184 ffs_dir (char *dirname)
186 char *rest, ch;
187 int block, off, loc, map, ino = ROOTINO;
188 struct direct *dp;
190 /* main loop to find destination inode */
191 loop:
193 /* load current inode (defaults to the root inode) */
195 if (!devread (fsbtodb (SUPERBLOCK, itod (SUPERBLOCK, ino)),
196 ino % (SUPERBLOCK->fs_inopb) * sizeof (struct dinode),
197 sizeof (struct dinode), (char *) INODE))
198 return 0; /* XXX what return value? */
200 /* if we have a real file (and we're not just printing possibilities),
201 then this is where we want to exit */
203 if (!*dirname || isspace (*dirname))
205 if ((INODE->i_mode & IFMT) != IFREG)
207 errnum = ERR_BAD_FILETYPE;
208 return 0;
211 filemax = INODE->i_size;
213 /* incomplete implementation requires this! */
214 fsmax = (NDADDR + NINDIR (SUPERBLOCK)) * SUPERBLOCK->fs_bsize;
215 return 1;
218 /* continue with file/directory name interpretation */
220 while (*dirname == '/')
221 dirname++;
223 if (!(INODE->i_size) || ((INODE->i_mode & IFMT) != IFDIR))
225 errnum = ERR_BAD_FILETYPE;
226 return 0;
229 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
231 *rest = 0;
232 loc = 0;
234 /* loop for reading a the entries in a directory */
238 if (loc >= INODE->i_size)
240 #if 0
241 putchar ('\n');
242 #endif
244 if (print_possibilities < 0)
245 return 1;
247 errnum = ERR_FILE_NOT_FOUND;
248 *rest = ch;
249 return 0;
252 if (!(off = blkoff (SUPERBLOCK, loc)))
254 block = lblkno (SUPERBLOCK, loc);
256 if ((map = block_map (block)) < 0
257 || !devread (fsbtodb (SUPERBLOCK, map), 0,
258 blksize (SUPERBLOCK, INODE, block),
259 (char *) FSYS_BUF))
261 errnum = ERR_FSYS_CORRUPT;
262 *rest = ch;
263 return 0;
267 dp = (struct direct *) (FSYS_BUF + off);
268 loc += dp->d_reclen;
270 #ifndef STAGE1_5
271 if (dp->d_ino && print_possibilities && ch != '/'
272 && (!*dirname || substring (dirname, dp->d_name) <= 0))
274 if (print_possibilities > 0)
275 print_possibilities = -print_possibilities;
277 print_a_completion (dp->d_name);
279 #endif /* STAGE1_5 */
281 while (!dp->d_ino || (substring (dirname, dp->d_name) != 0
282 || (print_possibilities && ch != '/')));
284 /* only get here if we have a matching directory entry */
286 ino = dp->d_ino;
287 *(dirname = rest) = ch;
289 /* go back to main loop at top of function */
290 goto loop;
294 ffs_embed (int *start_sector, int needed_sectors)
296 /* XXX: I don't know if this is really correct. Someone who is
297 familiar with BSD should check for this. */
298 if (needed_sectors > 14)
299 return 0;
301 *start_sector = 1;
302 #if 1
303 /* FIXME: Disable the embedding in FFS until someone checks if
304 the code above is correct. */
305 return 0;
306 #else
307 return 1;
308 #endif
311 #endif /* FSYS_FFS */