xref: /netbsd-src/usr.sbin/installboot/fstypes.c (revision 4882e3f0bfab8dbcf8110d4f4fceeb1f38cc2366)
1 /*	$NetBSD: fstypes.c,v 1.14 2024/05/19 15:48:57 tsutsui 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 and Luke Mewburn.
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  *
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.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if !defined(__lint)
38 __RCSID("$NetBSD: fstypes.c,v 1.14 2024/05/19 15:48:57 tsutsui Exp $");
39 #endif	/* !__lint */
40 
41 #include <sys/types.h>
42 
43 #include <assert.h>
44 #include <err.h>
45 #include <stdio.h>
46 
47 #include "installboot.h"
48 
49 struct ib_fs fstypes[] = {
50 #ifndef NO_STAGE2
51 	{
52 		.name = "ffs",
53 		.match = ffs_match,
54 		.findstage2 = ffs_findstage2
55 	},
56 	{
57 		.name = "raid",
58 		.match = raid_match,
59 		.findstage2 = ffs_findstage2
60 	},
61 #ifdef SUPPORT_CD9660
62 	{
63 		.name = "cd9660",
64 		.match = cd9660_match,
65 		.findstage2 = cd9660_findstage2
66 	},
67 #endif
68 	/* raw_match() always matches, so raw should be at the end. */
69 	{
70 		.name = "raw",
71 		.match = raw_match,
72 		.findstage2 = raw_findstage2
73 	},
74 #endif
75 	{
76 		.name = NULL
77 	}
78 };
79 
80 #ifndef NO_STAGE2
81 int
hardcode_stage2(ib_params * params,uint32_t * maxblk,ib_block * blocks)82 hardcode_stage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
83 {
84 	struct stat	s2sb;
85 	uint32_t	nblk, i;
86 
87 	assert(params != NULL);
88 	assert(params->stage2 != NULL);
89 	assert(maxblk != NULL);
90 	assert(blocks != NULL);
91 	assert((params->flags & IB_STAGE2START) != 0);
92 	assert(params->fstype != NULL);
93 	assert(params->fstype->blocksize != 0);
94 
95 	if (stat(params->stage2, &s2sb) == -1) {
96 		warn("Examining `%s'", params->stage2);
97 		return (0);
98 	}
99 	if (!S_ISREG(s2sb.st_mode)) {
100 		warnx("`%s' must be a regular file", params->stage2);
101 		return (0);
102 	}
103 
104 	nblk = s2sb.st_size / params->fstype->blocksize;
105 	if (s2sb.st_size % params->fstype->blocksize != 0)
106 		nblk++;
107 #if 0
108 	fprintf(stderr, "for %s got size %lld blksize %u blocks %u\n",
109 	    params->stage2, s2sb.st_size, params->fstype->blocksize, nblk);
110 #endif
111 	if (nblk > *maxblk) {
112                 warnx("Secondary bootstrap `%s' has too many blocks "
113                     "(calculated %u, maximum %u)",
114 		    params->stage2, nblk, *maxblk);
115                 return (0);
116         }
117 
118 	for (i = 0; i < nblk; i++) {
119 		blocks[i].block = params->s2start +
120 		    i * (params->fstype->blocksize / params->sectorsize);
121 		blocks[i].blocksize = params->fstype->blocksize;
122 	}
123 	*maxblk = nblk;
124 
125 	return (1);
126 }
127 
128 
129 int
raw_match(ib_params * params)130 raw_match(ib_params *params)
131 {
132 
133 	assert(params != NULL);
134 	assert(params->fstype != NULL);
135 
136 	params->fstype->blocksize = 8192;		// XXX: hardcode
137 	return (1);		/* can always write to a "raw" file system */
138 }
139 
140 int
raw_findstage2(ib_params * params,uint32_t * maxblk,ib_block * blocks)141 raw_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
142 {
143 
144 	assert(params != NULL);
145 	assert(params->stage2 != NULL);
146 	assert(maxblk != NULL);
147 	assert(blocks != NULL);
148 
149 	if ((params->flags & IB_STAGE2START) == 0) {
150 		warnx("Need `-B bno' for raw file systems");
151 		return (0);
152 	}
153 	return (hardcode_stage2(params, maxblk, blocks));
154 }
155 #endif
156