1 /* $NetBSD: ffs.c,v 1.7 2003/01/24 21:55:31 fvdl Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matt Fredette. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(__RCSID) && !defined(__lint) 41 __RCSID("$NetBSD: ffs.c,v 1.7 2003/01/24 21:55:31 fvdl Exp $"); 42 #endif /* !__lint */ 43 44 #include <sys/param.h> 45 #include <sys/mount.h> 46 47 #include <assert.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <stdarg.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "installboot.h" 58 59 #undef DIRBLKSIZ 60 61 #include <ufs/ufs/dinode.h> 62 #include <ufs/ufs/dir.h> 63 #include <ufs/ffs/fs.h> 64 #include <ufs/ffs/ffs_extern.h> 65 #include <ufs/ufs/ufs_bswap.h> 66 67 static int ffs_read_disk_block(ib_params *, uint32_t, int, char *); 68 static int ffs_find_disk_blocks(ib_params *, uint32_t, 69 int (*)(ib_params *, void *, uint32_t, uint32_t), void *); 70 static int ffs_findstage2_ino(ib_params *, void *, uint32_t, uint32_t); 71 static int ffs_findstage2_blocks(ib_params *, void *, uint32_t, uint32_t); 72 73 74 /* This reads a disk block from the filesystem. */ 75 static int 76 ffs_read_disk_block(ib_params *params, uint32_t blkno, int size, char *blk) 77 { 78 int rv; 79 80 assert(params != NULL); 81 assert(blk != NULL); 82 assert(params->filesystem != NULL); 83 assert(params->fsfd != -1); 84 assert(blkno > 0); 85 assert(size > 0); 86 assert(blk != NULL); 87 88 rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE); 89 if (rv == -1) { 90 warn("Reading block %d in `%s'", blkno, params->filesystem); 91 return (0); 92 } else if (rv != size) { 93 warnx("Reading block %d in `%s': short read", blkno, 94 params->filesystem); 95 return (0); 96 } 97 98 return (1); 99 } 100 101 /* 102 * This iterates over the data blocks belonging to an inode, 103 * making a callback each iteration with the disk block number 104 * and the size. 105 */ 106 static int 107 ffs_find_disk_blocks(ib_params *params, uint32_t ino, 108 int (*callback)(ib_params *, void *, uint32_t, uint32_t), 109 void *state) 110 { 111 char sbbuf[SBSIZE]; 112 struct fs *fs; 113 char inodebuf[MAXBSIZE]; 114 struct dinode *inode; 115 int level_i; 116 daddr_t blk, lblk, nblk; 117 int rv; 118 #define LEVELS 4 119 struct { 120 /* XXX ondisk32 */ 121 int32_t *blknums; 122 unsigned long blkcount; 123 char diskbuf[MAXBSIZE]; 124 } level[LEVELS]; 125 126 assert(params != NULL); 127 assert(params->fstype != NULL); 128 assert(callback != NULL); 129 assert(state != NULL); 130 131 /* Read the superblock. */ 132 if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf)) 133 return (0); 134 fs = (struct fs *)sbbuf; 135 if (params->fstype->needswap) 136 ffs_sb_swap(fs, fs); 137 138 /* Sanity check the superblock. */ 139 if (fs->fs_magic != FS_MAGIC) { 140 warnx("Bad superblock magic number in `%s'", 141 params->filesystem); 142 return (0); 143 } 144 if (fs->fs_inopb <= 0) { 145 warnx("Bad inopb %d in superblock in `%s'", 146 fs->fs_inopb, params->filesystem); 147 return (0); 148 } 149 150 /* Read the inode. */ 151 if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)), 152 fs->fs_bsize, inodebuf)) 153 return (0); 154 inode = (struct dinode *)inodebuf; 155 inode += ino_to_fsbo(fs, ino); 156 if (params->fstype->needswap) 157 ffs_dinode_swap(inode, inode); 158 159 /* Get the block count and initialize for our block walk. */ 160 nblk = howmany(inode->di_size, fs->fs_bsize); 161 lblk = 0; 162 level_i = 0; 163 level[0].blknums = &inode->di_db[0]; 164 level[0].blkcount = NDADDR; 165 level[1].blknums = &inode->di_ib[0]; 166 level[1].blkcount = 1; 167 level[2].blknums = &inode->di_ib[1]; 168 level[2].blkcount = 1; 169 level[3].blknums = &inode->di_ib[2]; 170 level[3].blkcount = 1; 171 172 /* Walk the data blocks. */ 173 while (nblk > 0) { 174 175 /* 176 * If there are no more blocks at this indirection 177 * level, move up one indirection level and loop. 178 */ 179 if (level[level_i].blkcount == 0) { 180 if (++level_i == LEVELS) 181 break; 182 continue; 183 } 184 185 /* Get the next block at this level. */ 186 blk = *(level[level_i].blknums++); 187 level[level_i].blkcount--; 188 if (params->fstype->needswap) 189 blk = bswap32(blk); 190 191 #if 0 192 fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk, 193 level_i); 194 #endif 195 196 /* 197 * If we're not at the direct level, descend one 198 * level, read in that level's new block list, 199 * and loop. 200 */ 201 if (level_i > 0) { 202 level_i--; 203 if (blk == 0) 204 memset(level[level_i].diskbuf, 0, MAXBSIZE); 205 else if (! ffs_read_disk_block(params, 206 fsbtodb(fs, blk), 207 fs->fs_bsize, level[level_i].diskbuf)) 208 return (0); 209 /* XXX ondisk32 */ 210 level[level_i].blknums = 211 (int32_t *)level[level_i].diskbuf; 212 level[level_i].blkcount = NINDIR(fs); 213 continue; 214 } 215 216 /* blk is the next direct level block. */ 217 #if 0 218 fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino, 219 fsbtodb(fs, blk), dblksize(fs, inode, lblk)); 220 #endif 221 rv = (*callback)(params, state, 222 fsbtodb(fs, blk), dblksize(fs, inode, lblk)); 223 lblk++; 224 nblk--; 225 if (rv != 1) 226 return (rv); 227 } 228 229 if (nblk != 0) { 230 warnx("Inode %d in `%s' ran out of blocks?", ino, 231 params->filesystem); 232 return (0); 233 } 234 235 return (1); 236 } 237 238 /* 239 * This callback reads a block of the root directory, 240 * searches for an entry for the secondary bootstrap, 241 * and saves the inode number if one is found. 242 */ 243 static int 244 ffs_findstage2_ino(ib_params *params, void *_ino, 245 uint32_t blk, uint32_t blksize) 246 { 247 char dirbuf[MAXBSIZE]; 248 struct direct *de, *ede; 249 uint32_t ino; 250 251 assert(params != NULL); 252 assert(params->fstype != NULL); 253 assert(params->stage2 != NULL); 254 assert(_ino != NULL); 255 256 /* Skip directory holes. */ 257 if (blk == 0) 258 return (1); 259 260 /* Read the directory block. */ 261 if (! ffs_read_disk_block(params, blk, blksize, dirbuf)) 262 return (0); 263 264 /* Loop over the directory entries. */ 265 de = (struct direct *)&dirbuf[0]; 266 ede = (struct direct *)&dirbuf[blksize]; 267 while (de < ede) { 268 ino = de->d_ino; 269 if (params->fstype->needswap) { 270 ino = bswap32(ino); 271 de->d_reclen = bswap16(de->d_reclen); 272 } 273 if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) { 274 *((uint32_t *)_ino) = ino; 275 return (2); 276 } 277 if (de->d_reclen == 0) 278 break; 279 de = (struct direct *)((char *)de + de->d_reclen); 280 } 281 282 return (1); 283 } 284 285 struct findblks_state { 286 uint32_t maxblk; 287 uint32_t nblk; 288 ib_block *blocks; 289 }; 290 291 /* This callback records the blocks of the secondary bootstrap. */ 292 static int 293 ffs_findstage2_blocks(ib_params *params, void *_state, 294 uint32_t blk, uint32_t blksize) 295 { 296 struct findblks_state *state = _state; 297 298 assert(params != NULL); 299 assert(params->stage2 != NULL); 300 assert(_state != NULL); 301 302 if (state->nblk == state->maxblk) { 303 warnx("Secondary bootstrap `%s' has too many blocks (max %d)", 304 params->stage2, state->maxblk); 305 return (0); 306 } 307 state->blocks[state->nblk].block = blk; 308 state->blocks[state->nblk].blocksize = blksize; 309 state->nblk++; 310 return (1); 311 } 312 313 /* 314 * publically visible functions 315 */ 316 317 int 318 ffs_match(ib_params *params) 319 { 320 char sbbuf[SBSIZE]; 321 struct fs *fs; 322 323 assert(params != NULL); 324 assert(params->fstype != NULL); 325 326 /* Read and check the superblock. */ 327 if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf)) 328 return (0); 329 fs = (struct fs *)sbbuf; 330 if (fs->fs_magic == FS_MAGIC) { 331 params->fstype->needswap = 0; 332 params->fstype->blocksize = fs->fs_bsize; 333 } else if (fs->fs_magic == bswap32(FS_MAGIC)) { 334 params->fstype->needswap = 1; 335 params->fstype->blocksize = bswap32(fs->fs_bsize); 336 } else { 337 return (0); 338 } 339 340 return (1); 341 } 342 343 int 344 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks) 345 { 346 int rv; 347 uint32_t ino; 348 struct findblks_state state; 349 350 assert(params != NULL); 351 assert(params->stage2 != NULL); 352 assert(maxblk != NULL); 353 assert(blocks != NULL); 354 355 if (params->flags & IB_STAGE2START) 356 return (hardcode_stage2(params, maxblk, blocks)); 357 358 /* The secondary bootstrap must be clearly in /. */ 359 if (params->stage2[0] == '/') 360 params->stage2++; 361 if (strchr(params->stage2, '/') != NULL) { 362 warnx("The secondary bootstrap `%s' must be in /", 363 params->stage2); 364 return (0); 365 } 366 367 /* Get the inode number of the secondary bootstrap. */ 368 rv = ffs_find_disk_blocks(params, ROOTINO, ffs_findstage2_ino, &ino); 369 if (rv != 2) { 370 warnx("Could not find secondary bootstrap `%s' in `%s'", 371 params->stage2, params->filesystem); 372 return (0); 373 } 374 375 /* Record the disk blocks of the secondary bootstrap. */ 376 state.maxblk = *maxblk; 377 state.nblk = 0; 378 state.blocks = blocks; 379 rv = ffs_find_disk_blocks(params, ino, ffs_findstage2_blocks, &state); 380 if (! rv) { 381 return (0); 382 } 383 384 *maxblk = state.nblk; 385 return (1); 386 } 387