xref: /netbsd-src/usr.sbin/installboot/ffs.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: ffs.c,v 1.21 2006/09/20 21:49:12 bad 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 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42 
43 #include <sys/cdefs.h>
44 #if defined(__RCSID) && !defined(__lint)
45 __RCSID("$NetBSD: ffs.c,v 1.21 2006/09/20 21:49:12 bad Exp $");
46 #endif	/* !__lint */
47 
48 #include <sys/param.h>
49 
50 #if !HAVE_NBTOOL_CONFIG_H
51 #include <sys/mount.h>
52 #endif
53 
54 #include <assert.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <stdarg.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "installboot.h"
65 
66 /* From <dev/raidframe/raidframevar.h> */
67 #define RF_PROTECTED_SECTORS 64L
68 
69 #undef DIRBLKSIZ
70 
71 #include <ufs/ufs/dinode.h>
72 #include <ufs/ufs/dir.h>
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/ffs_extern.h>
75 #ifndef NO_FFS_SWAP
76 #include <ufs/ufs/ufs_bswap.h>
77 #else
78 #define	ffs_sb_swap(fs_a, fs_b)
79 #define	ffs_dinode1_swap(inode_a, inode_b)
80 #define	ffs_dinode2_swap(inode_a, inode_b)
81 #endif
82 
83 static int	ffs_match_common(ib_params *, off_t);
84 static int	ffs_read_disk_block(ib_params *, uint64_t, int, char *);
85 static int	ffs_find_disk_blocks_ufs1(ib_params *, ino_t,
86 		    int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
87 static int	ffs_find_disk_blocks_ufs2(ib_params *, ino_t,
88 		    int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
89 static int	ffs_findstage2_ino(ib_params *, void *, uint64_t, uint32_t);
90 static int	ffs_findstage2_blocks(ib_params *, void *, uint64_t, uint32_t);
91 
92 static int is_ufs2;
93 
94 
95 /* This reads a disk block from the filesystem. */
96 static int
97 ffs_read_disk_block(ib_params *params, uint64_t blkno, int size, char *blk)
98 {
99 	int	rv;
100 
101 	assert(params != NULL);
102 	assert(blk != NULL);
103 	assert(params->filesystem != NULL);
104 	assert(params->fsfd != -1);
105 	assert(blkno >= 0);
106 	assert(size > 0);
107 	assert(blk != NULL);
108 
109 	rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
110 	if (rv == -1) {
111 		warn("Reading block %llu in `%s'",
112 		    (unsigned long long)blkno, params->filesystem);
113 		return (0);
114 	} else if (rv != size) {
115 		warnx("Reading block %llu in `%s': short read",
116 		    (unsigned long long)blkno, params->filesystem);
117 		return (0);
118 	}
119 
120 	return (1);
121 }
122 
123 /*
124  * This iterates over the data blocks belonging to an inode,
125  * making a callback each iteration with the disk block number
126  * and the size.
127  */
128 static int
129 ffs_find_disk_blocks_ufs1(ib_params *params, ino_t ino,
130 	int (*callback)(ib_params *, void *, uint64_t, uint32_t),
131 	void *state)
132 {
133 	char		sbbuf[SBLOCKSIZE];
134 	struct fs	*fs;
135 	char		inodebuf[MAXBSIZE];
136 	struct ufs1_dinode	*inode;
137 	int		level_i;
138 	int32_t	blk, lblk, nblk;
139 	int		rv;
140 #define LEVELS 4
141 	struct {
142 		int32_t		*blknums;
143 		unsigned long	blkcount;
144 		char		diskbuf[MAXBSIZE];
145 	} level[LEVELS];
146 
147 	assert(params != NULL);
148 	assert(params->fstype != NULL);
149 	assert(callback != NULL);
150 	assert(state != NULL);
151 
152 	/* Read the superblock. */
153 	if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
154 	    sbbuf))
155 		return (0);
156 	fs = (struct fs *)sbbuf;
157 	if (params->fstype->needswap)
158 		ffs_sb_swap(fs, fs);
159 
160 	if (fs->fs_inopb <= 0) {
161 		warnx("Bad inopb %d in superblock in `%s'",
162 		    fs->fs_inopb, params->filesystem);
163 		return (0);
164 	}
165 
166 	/* Read the inode. */
167 	if (! ffs_read_disk_block(params,
168 		fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
169 		fs->fs_bsize, inodebuf))
170 		return (0);
171 	inode = (struct ufs1_dinode *)inodebuf;
172 	inode += ino_to_fsbo(fs, ino);
173 	if (params->fstype->needswap)
174 		ffs_dinode1_swap(inode, inode);
175 
176 	/* Get the block count and initialize for our block walk. */
177 	nblk = howmany(inode->di_size, fs->fs_bsize);
178 	lblk = 0;
179 	level_i = 0;
180 	level[0].blknums = &inode->di_db[0];
181 	level[0].blkcount = NDADDR;
182 	level[1].blknums = &inode->di_ib[0];
183 	level[1].blkcount = 1;
184 	level[2].blknums = &inode->di_ib[1];
185 	level[2].blkcount = 1;
186 	level[3].blknums = &inode->di_ib[2];
187 	level[3].blkcount = 1;
188 
189 	/* Walk the data blocks. */
190 	while (nblk > 0) {
191 
192 		/*
193 		 * If there are no more blocks at this indirection
194 		 * level, move up one indirection level and loop.
195 		 */
196 		if (level[level_i].blkcount == 0) {
197 			if (++level_i == LEVELS)
198 				break;
199 			continue;
200 		}
201 
202 		/* Get the next block at this level. */
203 		blk = *(level[level_i].blknums++);
204 		level[level_i].blkcount--;
205 		if (params->fstype->needswap)
206 			blk = bswap32(blk);
207 
208 #if 0
209 		fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
210 		    level_i);
211 #endif
212 
213 		/*
214 		 * If we're not at the direct level, descend one
215 		 * level, read in that level's new block list,
216 		 * and loop.
217 		 */
218 		if (level_i > 0) {
219 			level_i--;
220 			if (blk == 0)
221 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
222 			else if (! ffs_read_disk_block(params,
223 				fsbtodb(fs, blk) + params->fstype->offset,
224 				fs->fs_bsize, level[level_i].diskbuf))
225 				return (0);
226 			/* XXX ondisk32 */
227 			level[level_i].blknums =
228 				(int32_t *)level[level_i].diskbuf;
229 			level[level_i].blkcount = NINDIR(fs);
230 			continue;
231 		}
232 
233 		/* blk is the next direct level block. */
234 #if 0
235 		fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
236 		    fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
237 #endif
238 		rv = (*callback)(params, state,
239 		    fsbtodb(fs, blk) + params->fstype->offset,
240 		    sblksize(fs, inode->di_size, lblk));
241 		lblk++;
242 		nblk--;
243 		if (rv != 1)
244 			return (rv);
245 	}
246 
247 	if (nblk != 0) {
248 		warnx("Inode %llu in `%s' ran out of blocks?",
249 		    (unsigned long long)ino, params->filesystem);
250 		return (0);
251 	}
252 
253 	return (1);
254 }
255 
256 /*
257  * This iterates over the data blocks belonging to an inode,
258  * making a callback each iteration with the disk block number
259  * and the size.
260  */
261 static int
262 ffs_find_disk_blocks_ufs2(ib_params *params, ino_t ino,
263 	int (*callback)(ib_params *, void *, uint64_t, uint32_t),
264 	void *state)
265 {
266 	char		sbbuf[SBLOCKSIZE];
267 	struct fs	*fs;
268 	char		inodebuf[MAXBSIZE];
269 	struct ufs2_dinode	*inode;
270 	int		level_i;
271 	int64_t	blk, lblk, nblk;
272 	int		rv;
273 #define LEVELS 4
274 	struct {
275 		int64_t		*blknums;
276 		unsigned long	blkcount;
277 		char		diskbuf[MAXBSIZE];
278 	} level[LEVELS];
279 
280 	assert(params != NULL);
281 	assert(params->fstype != NULL);
282 	assert(callback != NULL);
283 	assert(state != NULL);
284 
285 	/* Read the superblock. */
286 	if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
287 	    sbbuf))
288 		return (0);
289 	fs = (struct fs *)sbbuf;
290 	if (params->fstype->needswap)
291 		ffs_sb_swap(fs, fs);
292 
293 	if (fs->fs_inopb <= 0) {
294 		warnx("Bad inopb %d in superblock in `%s'",
295 		    fs->fs_inopb, params->filesystem);
296 		return (0);
297 	}
298 
299 	/* Read the inode. */
300 	if (! ffs_read_disk_block(params,
301 		fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
302 		fs->fs_bsize, inodebuf))
303 		return (0);
304 	inode = (struct ufs2_dinode *)inodebuf;
305 	inode += ino_to_fsbo(fs, ino);
306 	if (params->fstype->needswap)
307 		ffs_dinode2_swap(inode, inode);
308 
309 	/* Get the block count and initialize for our block walk. */
310 	nblk = howmany(inode->di_size, fs->fs_bsize);
311 	lblk = 0;
312 	level_i = 0;
313 	level[0].blknums = &inode->di_db[0];
314 	level[0].blkcount = NDADDR;
315 	level[1].blknums = &inode->di_ib[0];
316 	level[1].blkcount = 1;
317 	level[2].blknums = &inode->di_ib[1];
318 	level[2].blkcount = 1;
319 	level[3].blknums = &inode->di_ib[2];
320 	level[3].blkcount = 1;
321 
322 	/* Walk the data blocks. */
323 	while (nblk > 0) {
324 
325 		/*
326 		 * If there are no more blocks at this indirection
327 		 * level, move up one indirection level and loop.
328 		 */
329 		if (level[level_i].blkcount == 0) {
330 			if (++level_i == LEVELS)
331 				break;
332 			continue;
333 		}
334 
335 		/* Get the next block at this level. */
336 		blk = *(level[level_i].blknums++);
337 		level[level_i].blkcount--;
338 		if (params->fstype->needswap)
339 			blk = bswap64(blk);
340 
341 #if 0
342 		fprintf(stderr, "ino %lu blk %llu level %d\n", ino,
343 		    (unsigned long long)blk, level_i);
344 #endif
345 
346 		/*
347 		 * If we're not at the direct level, descend one
348 		 * level, read in that level's new block list,
349 		 * and loop.
350 		 */
351 		if (level_i > 0) {
352 			level_i--;
353 			if (blk == 0)
354 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
355 			else if (! ffs_read_disk_block(params,
356 				fsbtodb(fs, blk) + params->fstype->offset,
357 				fs->fs_bsize, level[level_i].diskbuf))
358 				return (0);
359 			level[level_i].blknums =
360 				(int64_t *)level[level_i].diskbuf;
361 			level[level_i].blkcount = NINDIR(fs);
362 			continue;
363 		}
364 
365 		/* blk is the next direct level block. */
366 #if 0
367 		fprintf(stderr, "ino %lu db %llu blksize %lu\n", ino,
368 		    fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
369 #endif
370 		rv = (*callback)(params, state,
371 		    fsbtodb(fs, blk) + params->fstype->offset,
372 		    sblksize(fs, inode->di_size, lblk));
373 		lblk++;
374 		nblk--;
375 		if (rv != 1)
376 			return (rv);
377 	}
378 
379 	if (nblk != 0) {
380 		warnx("Inode %llu in `%s' ran out of blocks?",
381 		    (unsigned long long)ino, params->filesystem);
382 		return (0);
383 	}
384 
385 	return (1);
386 }
387 
388 /*
389  * This callback reads a block of the root directory,
390  * searches for an entry for the secondary bootstrap,
391  * and saves the inode number if one is found.
392  */
393 static int
394 ffs_findstage2_ino(ib_params *params, void *_ino,
395 	uint64_t blk, uint32_t blksize)
396 {
397 	char		dirbuf[MAXBSIZE];
398 	struct direct	*de, *ede;
399 	uint32_t	ino;
400 
401 	assert(params != NULL);
402 	assert(params->fstype != NULL);
403 	assert(params->stage2 != NULL);
404 	assert(_ino != NULL);
405 
406 	/* Skip directory holes. */
407 	if (blk == 0)
408 		return (1);
409 
410 	/* Read the directory block. */
411 	if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
412 		return (0);
413 
414 	/* Loop over the directory entries. */
415 	de = (struct direct *)&dirbuf[0];
416 	ede = (struct direct *)&dirbuf[blksize];
417 	while (de < ede) {
418 		ino = de->d_fileno;
419 		if (params->fstype->needswap) {
420 			ino = bswap32(ino);
421 			de->d_reclen = bswap16(de->d_reclen);
422 		}
423 		if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
424 			*((uint32_t *)_ino) = ino;
425 			return (2);
426 		}
427 		if (de->d_reclen == 0)
428 			break;
429 		de = (struct direct *)((char *)de + de->d_reclen);
430 	}
431 
432 	return (1);
433 }
434 
435 struct findblks_state {
436 	uint32_t	maxblk;
437 	uint32_t	nblk;
438 	ib_block	*blocks;
439 };
440 
441 /* This callback records the blocks of the secondary bootstrap. */
442 static int
443 ffs_findstage2_blocks(ib_params *params, void *_state,
444 	uint64_t blk, uint32_t blksize)
445 {
446 	struct findblks_state *state = _state;
447 
448 	assert(params != NULL);
449 	assert(params->stage2 != NULL);
450 	assert(_state != NULL);
451 
452 	if (state->nblk == state->maxblk) {
453 		warnx("Secondary bootstrap `%s' has too many blocks (max %d)",
454 		    params->stage2, state->maxblk);
455 		return (0);
456 	}
457 	state->blocks[state->nblk].block = blk;
458 	state->blocks[state->nblk].blocksize = blksize;
459 	state->nblk++;
460 	return (1);
461 }
462 
463 /*
464  *	publicly visible functions
465  */
466 
467 static off_t sblock_try[] = SBLOCKSEARCH;
468 
469 int
470 ffs_match(ib_params *params)
471 {
472 	return ffs_match_common(params, (off_t) 0);
473 }
474 
475 int
476 raid_match(ib_params *params)
477 {
478 	/* XXX Assumes 512 bytes / sector */
479 	if (DEV_BSIZE != 512) {
480 		warnx("Media is %d bytes/sector."
481 			"  RAID is only supported on 512 bytes/sector media.",
482 			DEV_BSIZE);
483 		return 0;
484 	}
485 	return ffs_match_common(params, (off_t) RF_PROTECTED_SECTORS);
486 }
487 
488 int
489 ffs_match_common(ib_params *params, off_t offset)
490 {
491 	char		sbbuf[SBLOCKSIZE];
492 	struct fs	*fs;
493 	int i;
494 	off_t loc;
495 
496 	assert(params != NULL);
497 	assert(params->fstype != NULL);
498 
499 	fs = (struct fs *)sbbuf;
500 	for (i = 0; sblock_try[i] != -1; i++) {
501 		loc = sblock_try[i] / DEV_BSIZE + offset;
502 		if (!ffs_read_disk_block(params, loc, SBLOCKSIZE, sbbuf))
503 			continue;
504 		switch (fs->fs_magic) {
505 		case FS_UFS2_MAGIC:
506 			is_ufs2 = 1;
507 			/* FALLTHROUGH */
508 		case FS_UFS1_MAGIC:
509 			params->fstype->needswap = 0;
510 			params->fstype->blocksize = fs->fs_bsize;
511 			params->fstype->sblockloc = loc;
512 			params->fstype->offset = offset;
513 			break;
514 #ifndef FFS_NO_SWAP
515 		case FS_UFS2_MAGIC_SWAPPED:
516 			is_ufs2 = 1;
517 			/* FALLTHROUGH */
518 		case FS_UFS1_MAGIC_SWAPPED:
519 			params->fstype->needswap = 1;
520 			params->fstype->blocksize = bswap32(fs->fs_bsize);
521 			params->fstype->sblockloc = loc;
522 			params->fstype->offset = offset;
523 			break;
524 #endif
525 		default:
526 			continue;
527 		}
528 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
529 			continue;
530 		return 1;
531 	}
532 
533 	return (0);
534 }
535 
536 int
537 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
538 {
539 	int			rv;
540 	uint32_t		ino;
541 	struct findblks_state	state;
542 
543 	assert(params != NULL);
544 	assert(params->stage2 != NULL);
545 	assert(maxblk != NULL);
546 	assert(blocks != NULL);
547 
548 	if (params->flags & IB_STAGE2START)
549 		return (hardcode_stage2(params, maxblk, blocks));
550 
551 	/* The secondary bootstrap must be clearly in /. */
552 	if (params->stage2[0] == '/')
553 		params->stage2++;
554 	if (strchr(params->stage2, '/') != NULL) {
555 		warnx("The secondary bootstrap `%s' must be in /",
556 		    params->stage2);
557 		return (0);
558 	}
559 
560 	/* Get the inode number of the secondary bootstrap. */
561 	if (is_ufs2)
562 		rv = ffs_find_disk_blocks_ufs2(params, ROOTINO,
563 		    ffs_findstage2_ino, &ino);
564 	else
565 		rv = ffs_find_disk_blocks_ufs1(params, ROOTINO,
566 		    ffs_findstage2_ino, &ino);
567 	if (rv != 2) {
568 		warnx("Could not find secondary bootstrap `%s' in `%s'",
569 		    params->stage2, params->filesystem);
570 		return (0);
571 	}
572 
573 	/* Record the disk blocks of the secondary bootstrap. */
574 	state.maxblk = *maxblk;
575 	state.nblk = 0;
576 	state.blocks = blocks;
577 	if (is_ufs2)
578 		rv = ffs_find_disk_blocks_ufs2(params, ino,
579 		    ffs_findstage2_blocks, &state);
580 	else
581 		rv = ffs_find_disk_blocks_ufs1(params, ino,
582 		    ffs_findstage2_blocks, &state);
583 	if (! rv) {
584 		return (0);
585 	}
586 
587 	*maxblk = state.nblk;
588 	return (1);
589 }
590