xref: /netbsd-src/usr.sbin/installboot/fstypes.c (revision 4882e3f0bfab8dbcf8110d4f4fceeb1f38cc2366)
1*4882e3f0Stsutsui /*	$NetBSD: fstypes.c,v 1.14 2024/05/19 15:48:57 tsutsui Exp $	*/
28eb8919eSlukem 
38eb8919eSlukem /*-
48eb8919eSlukem  * Copyright (c) 2002 The NetBSD Foundation, Inc.
58eb8919eSlukem  * All rights reserved.
68eb8919eSlukem  *
78eb8919eSlukem  * This code is derived from software contributed to The NetBSD Foundation
880ae5fb6Slukem  * by Matt Fredette and Luke Mewburn.
98eb8919eSlukem  *
108eb8919eSlukem  * Redistribution and use in source and binary forms, with or without
118eb8919eSlukem  * modification, are permitted provided that the following conditions
128eb8919eSlukem  * are met:
138eb8919eSlukem  * 1. Redistributions of source code must retain the above copyright
148eb8919eSlukem  *    notice, this list of conditions and the following disclaimer.
158eb8919eSlukem  * 2. Redistributions in binary form must reproduce the above copyright
168eb8919eSlukem  *    notice, this list of conditions and the following disclaimer in the
178eb8919eSlukem  *    documentation and/or other materials provided with the distribution.
188eb8919eSlukem  *
198eb8919eSlukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
208eb8919eSlukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
218eb8919eSlukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
228eb8919eSlukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
238eb8919eSlukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
248eb8919eSlukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
258eb8919eSlukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
268eb8919eSlukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
278eb8919eSlukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
288eb8919eSlukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
298eb8919eSlukem  * POSSIBILITY OF SUCH DAMAGE.
308eb8919eSlukem  */
318eb8919eSlukem 
32b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
33b2f78261Sjmc #include "nbtool_config.h"
34b2f78261Sjmc #endif
35b2f78261Sjmc 
364e59fb5bSlukem #include <sys/cdefs.h>
37e5f39b5eStsutsui #if !defined(__lint)
38*4882e3f0Stsutsui __RCSID("$NetBSD: fstypes.c,v 1.14 2024/05/19 15:48:57 tsutsui Exp $");
394e59fb5bSlukem #endif	/* !__lint */
404e59fb5bSlukem 
418eb8919eSlukem #include <sys/types.h>
4280ae5fb6Slukem 
4380ae5fb6Slukem #include <assert.h>
441bdd92eeSlukem #include <err.h>
4580ae5fb6Slukem #include <stdio.h>
4680ae5fb6Slukem 
478eb8919eSlukem #include "installboot.h"
488eb8919eSlukem 
498eb8919eSlukem struct ib_fs fstypes[] = {
502fdbc8bdSdsl #ifndef NO_STAGE2
51*4882e3f0Stsutsui 	{
52*4882e3f0Stsutsui 		.name = "ffs",
53*4882e3f0Stsutsui 		.match = ffs_match,
54*4882e3f0Stsutsui 		.findstage2 = ffs_findstage2
55*4882e3f0Stsutsui 	},
56*4882e3f0Stsutsui 	{
57*4882e3f0Stsutsui 		.name = "raid",
58*4882e3f0Stsutsui 		.match = raid_match,
59*4882e3f0Stsutsui 		.findstage2 = ffs_findstage2
60*4882e3f0Stsutsui 	},
61*4882e3f0Stsutsui #ifdef SUPPORT_CD9660
62*4882e3f0Stsutsui 	{
63*4882e3f0Stsutsui 		.name = "cd9660",
64*4882e3f0Stsutsui 		.match = cd9660_match,
65*4882e3f0Stsutsui 		.findstage2 = cd9660_findstage2
66*4882e3f0Stsutsui 	},
672fdbc8bdSdsl #endif
68*4882e3f0Stsutsui 	/* raw_match() always matches, so raw should be at the end. */
69*4882e3f0Stsutsui 	{
70*4882e3f0Stsutsui 		.name = "raw",
71*4882e3f0Stsutsui 		.match = raw_match,
72*4882e3f0Stsutsui 		.findstage2 = raw_findstage2
73*4882e3f0Stsutsui 	},
74*4882e3f0Stsutsui #endif
75*4882e3f0Stsutsui 	{
76*4882e3f0Stsutsui 		.name = NULL
77*4882e3f0Stsutsui 	}
788eb8919eSlukem };
7980ae5fb6Slukem 
802fdbc8bdSdsl #ifndef NO_STAGE2
8180ae5fb6Slukem int
hardcode_stage2(ib_params * params,uint32_t * maxblk,ib_block * blocks)821bdd92eeSlukem hardcode_stage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
831bdd92eeSlukem {
841bdd92eeSlukem 	struct stat	s2sb;
851bdd92eeSlukem 	uint32_t	nblk, i;
861bdd92eeSlukem 
871bdd92eeSlukem 	assert(params != NULL);
881bdd92eeSlukem 	assert(params->stage2 != NULL);
891bdd92eeSlukem 	assert(maxblk != NULL);
901bdd92eeSlukem 	assert(blocks != NULL);
911bdd92eeSlukem 	assert((params->flags & IB_STAGE2START) != 0);
921bdd92eeSlukem 	assert(params->fstype != NULL);
931bdd92eeSlukem 	assert(params->fstype->blocksize != 0);
941bdd92eeSlukem 
951bdd92eeSlukem 	if (stat(params->stage2, &s2sb) == -1) {
961bdd92eeSlukem 		warn("Examining `%s'", params->stage2);
971bdd92eeSlukem 		return (0);
981bdd92eeSlukem 	}
991bdd92eeSlukem 	if (!S_ISREG(s2sb.st_mode)) {
1001bdd92eeSlukem 		warnx("`%s' must be a regular file", params->stage2);
1011bdd92eeSlukem 		return (0);
1021bdd92eeSlukem 	}
1031bdd92eeSlukem 
1041bdd92eeSlukem 	nblk = s2sb.st_size / params->fstype->blocksize;
1051bdd92eeSlukem 	if (s2sb.st_size % params->fstype->blocksize != 0)
1061bdd92eeSlukem 		nblk++;
1071bdd92eeSlukem #if 0
1081bdd92eeSlukem 	fprintf(stderr, "for %s got size %lld blksize %u blocks %u\n",
1091bdd92eeSlukem 	    params->stage2, s2sb.st_size, params->fstype->blocksize, nblk);
1101bdd92eeSlukem #endif
1111bdd92eeSlukem 	if (nblk > *maxblk) {
1121bdd92eeSlukem                 warnx("Secondary bootstrap `%s' has too many blocks "
113966b42a3Slukem                     "(calculated %u, maximum %u)",
1141bdd92eeSlukem 		    params->stage2, nblk, *maxblk);
1151bdd92eeSlukem                 return (0);
1161bdd92eeSlukem         }
1171bdd92eeSlukem 
1181bdd92eeSlukem 	for (i = 0; i < nblk; i++) {
1191bdd92eeSlukem 		blocks[i].block = params->s2start +
12017ad8eceStsutsui 		    i * (params->fstype->blocksize / params->sectorsize);
1211bdd92eeSlukem 		blocks[i].blocksize = params->fstype->blocksize;
1221bdd92eeSlukem 	}
1231bdd92eeSlukem 	*maxblk = nblk;
1241bdd92eeSlukem 
1251bdd92eeSlukem 	return (1);
1261bdd92eeSlukem }
1271bdd92eeSlukem 
1281bdd92eeSlukem 
1291bdd92eeSlukem int
raw_match(ib_params * params)13080ae5fb6Slukem raw_match(ib_params *params)
13180ae5fb6Slukem {
13280ae5fb6Slukem 
13380ae5fb6Slukem 	assert(params != NULL);
1341bdd92eeSlukem 	assert(params->fstype != NULL);
1351bdd92eeSlukem 
1361bdd92eeSlukem 	params->fstype->blocksize = 8192;		// XXX: hardcode
13780ae5fb6Slukem 	return (1);		/* can always write to a "raw" file system */
13880ae5fb6Slukem }
13980ae5fb6Slukem 
14080ae5fb6Slukem int
raw_findstage2(ib_params * params,uint32_t * maxblk,ib_block * blocks)14180ae5fb6Slukem raw_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
14280ae5fb6Slukem {
14380ae5fb6Slukem 
14480ae5fb6Slukem 	assert(params != NULL);
1451bdd92eeSlukem 	assert(params->stage2 != NULL);
14680ae5fb6Slukem 	assert(maxblk != NULL);
14780ae5fb6Slukem 	assert(blocks != NULL);
1481bdd92eeSlukem 
1491bdd92eeSlukem 	if ((params->flags & IB_STAGE2START) == 0) {
1501bdd92eeSlukem 		warnx("Need `-B bno' for raw file systems");
1511bdd92eeSlukem 		return (0);
1521bdd92eeSlukem 	}
1531bdd92eeSlukem 	return (hardcode_stage2(params, maxblk, blocks));
15480ae5fb6Slukem }
1552fdbc8bdSdsl #endif
156