ext2fs.c revision 1.1.4.2 1 1.1.4.2 matt /* $NetBSD: ext2fs.c,v 1.1.4.2 2008/03/23 00:50:06 matt Exp $ */
2 1.1.4.2 matt
3 1.1.4.2 matt /*
4 1.1.4.2 matt * Copyright (c) 1997 Manuel Bouyer.
5 1.1.4.2 matt *
6 1.1.4.2 matt * Redistribution and use in source and binary forms, with or without
7 1.1.4.2 matt * modification, are permitted provided that the following conditions
8 1.1.4.2 matt * are met:
9 1.1.4.2 matt * 1. Redistributions of source code must retain the above copyright
10 1.1.4.2 matt * notice, this list of conditions and the following disclaimer.
11 1.1.4.2 matt * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.4.2 matt * notice, this list of conditions and the following disclaimer in the
13 1.1.4.2 matt * documentation and/or other materials provided with the distribution.
14 1.1.4.2 matt * 3. All advertising materials mentioning features or use of this software
15 1.1.4.2 matt * must display the following acknowledgement:
16 1.1.4.2 matt * This product includes software developed by Manuel Bouyer.
17 1.1.4.2 matt * 4. The name of the author may not be used to endorse or promote products
18 1.1.4.2 matt * derived from this software without specific prior written permission.
19 1.1.4.2 matt *
20 1.1.4.2 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1.4.2 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1.4.2 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1.4.2 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1.4.2 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1.4.2 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1.4.2 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1.4.2 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1.4.2 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1.4.2 matt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 matt */
31 1.1.4.2 matt
32 1.1.4.2 matt /*-
33 1.1.4.2 matt * Copyright (c) 2002 The NetBSD Foundation, Inc.
34 1.1.4.2 matt * All rights reserved.
35 1.1.4.2 matt *
36 1.1.4.2 matt * This code is derived from software contributed to The NetBSD Foundation
37 1.1.4.2 matt * by Matt Fredette.
38 1.1.4.2 matt *
39 1.1.4.2 matt * Redistribution and use in source and binary forms, with or without
40 1.1.4.2 matt * modification, are permitted provided that the following conditions
41 1.1.4.2 matt * are met:
42 1.1.4.2 matt * 1. Redistributions of source code must retain the above copyright
43 1.1.4.2 matt * notice, this list of conditions and the following disclaimer.
44 1.1.4.2 matt * 2. Redistributions in binary form must reproduce the above copyright
45 1.1.4.2 matt * notice, this list of conditions and the following disclaimer in the
46 1.1.4.2 matt * documentation and/or other materials provided with the distribution.
47 1.1.4.2 matt * 3. All advertising materials mentioning features or use of this software
48 1.1.4.2 matt * must display the following acknowledgement:
49 1.1.4.2 matt * This product includes software developed by the NetBSD
50 1.1.4.2 matt * Foundation, Inc. and its contributors.
51 1.1.4.2 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
52 1.1.4.2 matt * contributors may be used to endorse or promote products derived
53 1.1.4.2 matt * from this software without specific prior written permission.
54 1.1.4.2 matt *
55 1.1.4.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 1.1.4.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 1.1.4.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 1.1.4.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 1.1.4.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 1.1.4.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 1.1.4.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 1.1.4.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 1.1.4.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 1.1.4.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 1.1.4.2 matt * POSSIBILITY OF SUCH DAMAGE.
66 1.1.4.2 matt */
67 1.1.4.2 matt
68 1.1.4.2 matt #if HAVE_NBTOOL_CONFIG_H
69 1.1.4.2 matt #include "nbtool_config.h"
70 1.1.4.2 matt #endif
71 1.1.4.2 matt
72 1.1.4.2 matt #include <sys/cdefs.h>
73 1.1.4.2 matt #if defined(__RCSID) && !defined(__lint)
74 1.1.4.2 matt __RCSID("$NetBSD: ext2fs.c,v 1.1.4.2 2008/03/23 00:50:06 matt Exp $");
75 1.1.4.2 matt #endif /* !__lint */
76 1.1.4.2 matt
77 1.1.4.2 matt #include <sys/param.h>
78 1.1.4.2 matt
79 1.1.4.2 matt #if !HAVE_NBTOOL_CONFIG_H
80 1.1.4.2 matt #include <sys/mount.h>
81 1.1.4.2 matt #endif
82 1.1.4.2 matt
83 1.1.4.2 matt #include <assert.h>
84 1.1.4.2 matt #include <err.h>
85 1.1.4.2 matt #include <errno.h>
86 1.1.4.2 matt #include <fcntl.h>
87 1.1.4.2 matt #include <stdarg.h>
88 1.1.4.2 matt #include <stdio.h>
89 1.1.4.2 matt #include <stdlib.h>
90 1.1.4.2 matt #include <string.h>
91 1.1.4.2 matt #include <unistd.h>
92 1.1.4.2 matt
93 1.1.4.2 matt #include "installboot.h"
94 1.1.4.2 matt
95 1.1.4.2 matt #include <ufs/ext2fs/ext2fs_dinode.h>
96 1.1.4.2 matt #include <ufs/ext2fs/ext2fs_dir.h>
97 1.1.4.2 matt #include <ufs/ext2fs/ext2fs.h>
98 1.1.4.2 matt
99 1.1.4.2 matt static int ext2fs_read_disk_block(ib_params *, uint64_t, int, uint8_t []);
100 1.1.4.2 matt static int ext2fs_read_sblock(ib_params *, struct m_ext2fs *fs);
101 1.1.4.2 matt static int ext2fs_read_gdblock(ib_params *, struct m_ext2fs *fs);
102 1.1.4.2 matt static int ext2fs_find_disk_blocks(ib_params *, ino_t,
103 1.1.4.2 matt int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
104 1.1.4.2 matt static int ext2fs_findstage2_ino(ib_params *, void *, uint64_t, uint32_t);
105 1.1.4.2 matt static int ext2fs_findstage2_blocks(ib_params *, void *, uint64_t,
106 1.1.4.2 matt uint32_t);
107 1.1.4.2 matt
108 1.1.4.2 matt
109 1.1.4.2 matt /* This reads a disk block from the file system. */
110 1.1.4.2 matt /* XXX: should be shared with ffs.c? */
111 1.1.4.2 matt static int
112 1.1.4.2 matt ext2fs_read_disk_block(ib_params *params, uint64_t blkno, int size,
113 1.1.4.2 matt uint8_t blk[])
114 1.1.4.2 matt {
115 1.1.4.2 matt int rv;
116 1.1.4.2 matt
117 1.1.4.2 matt assert(params != NULL);
118 1.1.4.2 matt assert(params->filesystem != NULL);
119 1.1.4.2 matt assert(params->fsfd != -1);
120 1.1.4.2 matt assert(size > 0);
121 1.1.4.2 matt assert(blk != NULL);
122 1.1.4.2 matt
123 1.1.4.2 matt rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
124 1.1.4.2 matt if (rv == -1) {
125 1.1.4.2 matt warn("Reading block %llu in `%s'",
126 1.1.4.2 matt (unsigned long long)blkno, params->filesystem);
127 1.1.4.2 matt return 0;
128 1.1.4.2 matt } else if (rv != size) {
129 1.1.4.2 matt warnx("Reading block %llu in `%s': short read",
130 1.1.4.2 matt (unsigned long long)blkno, params->filesystem);
131 1.1.4.2 matt return 0;
132 1.1.4.2 matt }
133 1.1.4.2 matt
134 1.1.4.2 matt return 1;
135 1.1.4.2 matt }
136 1.1.4.2 matt
137 1.1.4.2 matt static int
138 1.1.4.2 matt ext2fs_read_sblock(ib_params *params, struct m_ext2fs *fs)
139 1.1.4.2 matt {
140 1.1.4.2 matt uint8_t sbbuf[SBSIZE];
141 1.1.4.2 matt
142 1.1.4.2 matt if (ext2fs_read_disk_block(params, SBOFF / DEV_BSIZE, SBSIZE,
143 1.1.4.2 matt sbbuf) == 0)
144 1.1.4.2 matt
145 1.1.4.2 matt e2fs_sbload((void *)sbbuf, &fs->e2fs);
146 1.1.4.2 matt
147 1.1.4.2 matt if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
148 1.1.4.2 matt return 0;
149 1.1.4.2 matt
150 1.1.4.2 matt if (fs->e2fs.e2fs_rev > E2FS_REV1 ||
151 1.1.4.2 matt (fs->e2fs.e2fs_rev == E2FS_REV1 &&
152 1.1.4.2 matt (fs->e2fs.e2fs_first_ino != EXT2_FIRSTINO ||
153 1.1.4.2 matt fs->e2fs.e2fs_inode_size != EXT2_DINODE_SIZE ||
154 1.1.4.2 matt (fs->e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) != 0)))
155 1.1.4.2 matt return 0;
156 1.1.4.2 matt
157 1.1.4.2 matt fs->e2fs_ncg =
158 1.1.4.2 matt howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
159 1.1.4.2 matt fs->e2fs.e2fs_bpg);
160 1.1.4.2 matt /* XXX assume hw bsize = 512 */
161 1.1.4.2 matt fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
162 1.1.4.2 matt fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
163 1.1.4.2 matt fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
164 1.1.4.2 matt fs->e2fs_qbmask = fs->e2fs_bsize - 1;
165 1.1.4.2 matt fs->e2fs_bmask = ~fs->e2fs_qbmask;
166 1.1.4.2 matt fs->e2fs_ngdb =
167 1.1.4.2 matt howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
168 1.1.4.2 matt fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
169 1.1.4.2 matt fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
170 1.1.4.2 matt
171 1.1.4.2 matt return 1;
172 1.1.4.2 matt }
173 1.1.4.2 matt
174 1.1.4.2 matt static int
175 1.1.4.2 matt ext2fs_read_gdblock(ib_params *params, struct m_ext2fs *fs)
176 1.1.4.2 matt {
177 1.1.4.2 matt uint8_t gdbuf[MAXBSIZE];
178 1.1.4.2 matt uint32_t gdpb;
179 1.1.4.2 matt int i;
180 1.1.4.2 matt
181 1.1.4.2 matt gdpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
182 1.1.4.2 matt
183 1.1.4.2 matt for (i = 0; i < fs->e2fs_ngdb; i++) {
184 1.1.4.2 matt if (ext2fs_read_disk_block(params, fsbtodb(fs,
185 1.1.4.2 matt fs->e2fs.e2fs_first_dblock + 1 /* superblock */ + i),
186 1.1.4.2 matt SBSIZE, gdbuf) == 0)
187 1.1.4.2 matt return 0;
188 1.1.4.2 matt
189 1.1.4.2 matt e2fs_cgload((struct ext2_gd *)gdbuf, &fs->e2fs_gd[gdpb * i],
190 1.1.4.2 matt (i == (fs->e2fs_ngdb - 1)) ?
191 1.1.4.2 matt (fs->e2fs_ncg - gdpb * i) * sizeof(struct ext2_gd):
192 1.1.4.2 matt fs->e2fs_bsize);
193 1.1.4.2 matt }
194 1.1.4.2 matt
195 1.1.4.2 matt return 1;
196 1.1.4.2 matt }
197 1.1.4.2 matt
198 1.1.4.2 matt /*
199 1.1.4.2 matt * This iterates over the data blocks belonging to an inode,
200 1.1.4.2 matt * making a callback each iteration with the disk block number
201 1.1.4.2 matt * and the size.
202 1.1.4.2 matt */
203 1.1.4.2 matt static int
204 1.1.4.2 matt ext2fs_find_disk_blocks(ib_params *params, ino_t ino,
205 1.1.4.2 matt int (*callback)(ib_params *, void *, uint64_t, uint32_t),
206 1.1.4.2 matt void *state)
207 1.1.4.2 matt {
208 1.1.4.2 matt uint8_t sbbuf[sizeof(struct m_ext2fs)];
209 1.1.4.2 matt struct m_ext2fs *fs;
210 1.1.4.2 matt uint8_t inodebuf[MAXBSIZE];
211 1.1.4.2 matt struct ext2fs_dinode inode_store, *inode;
212 1.1.4.2 matt int level_i;
213 1.1.4.2 matt int32_t blk, lblk, nblk;
214 1.1.4.2 matt int rv;
215 1.1.4.2 matt #define LEVELS 4
216 1.1.4.2 matt struct {
217 1.1.4.2 matt uint32_t *blknums;
218 1.1.4.2 matt unsigned long blkcount;
219 1.1.4.2 matt uint8_t diskbuf[MAXBSIZE];
220 1.1.4.2 matt } level[LEVELS];
221 1.1.4.2 matt
222 1.1.4.2 matt assert(params != NULL);
223 1.1.4.2 matt assert(params->fstype != NULL);
224 1.1.4.2 matt assert(callback != NULL);
225 1.1.4.2 matt assert(state != NULL);
226 1.1.4.2 matt
227 1.1.4.2 matt /* Read the superblock. */
228 1.1.4.2 matt fs = (void *)sbbuf;
229 1.1.4.2 matt if (ext2fs_read_sblock(params, fs) == 0)
230 1.1.4.2 matt return 0;
231 1.1.4.2 matt
232 1.1.4.2 matt fs->e2fs_gd = malloc(sizeof(struct ext2_gd) * fs->e2fs_ncg);
233 1.1.4.2 matt if (fs->e2fs_gd == NULL) {
234 1.1.4.2 matt warnx("Can't allocate memofy for group descriptors");
235 1.1.4.2 matt return 0;
236 1.1.4.2 matt }
237 1.1.4.2 matt
238 1.1.4.2 matt if (ext2fs_read_gdblock(params, fs) == 0) {
239 1.1.4.2 matt warnx("Can't read group descriptors");
240 1.1.4.2 matt return 0;
241 1.1.4.2 matt }
242 1.1.4.2 matt
243 1.1.4.2 matt if (fs->e2fs_ipb <= 0) {
244 1.1.4.2 matt warnx("Bad ipb %d in superblock in `%s'",
245 1.1.4.2 matt fs->e2fs_ipb, params->filesystem);
246 1.1.4.2 matt return 0;
247 1.1.4.2 matt }
248 1.1.4.2 matt
249 1.1.4.2 matt /* Read the inode. */
250 1.1.4.2 matt if (ext2fs_read_disk_block(params,
251 1.1.4.2 matt fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
252 1.1.4.2 matt fs->e2fs_bsize, inodebuf))
253 1.1.4.2 matt return 0;
254 1.1.4.2 matt inode = (void *)inodebuf;
255 1.1.4.2 matt e2fs_iload(&inode[ino_to_fsbo(fs, ino)], &inode_store);
256 1.1.4.2 matt inode = &inode_store;
257 1.1.4.2 matt
258 1.1.4.2 matt /* Get the block count and initialize for our block walk. */
259 1.1.4.2 matt nblk = howmany(inode->e2di_size, fs->e2fs_bsize);
260 1.1.4.2 matt lblk = 0;
261 1.1.4.2 matt level_i = 0;
262 1.1.4.2 matt level[0].blknums = &inode->e2di_blocks[0];
263 1.1.4.2 matt level[0].blkcount = NDADDR;
264 1.1.4.2 matt level[1].blknums = &inode->e2di_blocks[NDADDR + 0];
265 1.1.4.2 matt level[1].blkcount = 1;
266 1.1.4.2 matt level[2].blknums = &inode->e2di_blocks[NDADDR + 1];
267 1.1.4.2 matt level[2].blkcount = 1;
268 1.1.4.2 matt level[3].blknums = &inode->e2di_blocks[NDADDR + 2];
269 1.1.4.2 matt level[3].blkcount = 1;
270 1.1.4.2 matt
271 1.1.4.2 matt /* Walk the data blocks. */
272 1.1.4.2 matt while (nblk > 0) {
273 1.1.4.2 matt
274 1.1.4.2 matt /*
275 1.1.4.2 matt * If there are no more blocks at this indirection
276 1.1.4.2 matt * level, move up one indirection level and loop.
277 1.1.4.2 matt */
278 1.1.4.2 matt if (level[level_i].blkcount == 0) {
279 1.1.4.2 matt if (++level_i == LEVELS)
280 1.1.4.2 matt break;
281 1.1.4.2 matt continue;
282 1.1.4.2 matt }
283 1.1.4.2 matt
284 1.1.4.2 matt /* Get the next block at this level. */
285 1.1.4.2 matt blk = fs2h32(*(level[level_i].blknums++));
286 1.1.4.2 matt level[level_i].blkcount--;
287 1.1.4.2 matt
288 1.1.4.2 matt #if 0
289 1.1.4.2 matt fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
290 1.1.4.2 matt level_i);
291 1.1.4.2 matt #endif
292 1.1.4.2 matt
293 1.1.4.2 matt /*
294 1.1.4.2 matt * If we're not at the direct level, descend one
295 1.1.4.2 matt * level, read in that level's new block list,
296 1.1.4.2 matt * and loop.
297 1.1.4.2 matt */
298 1.1.4.2 matt if (level_i > 0) {
299 1.1.4.2 matt level_i--;
300 1.1.4.2 matt if (blk == 0)
301 1.1.4.2 matt memset(level[level_i].diskbuf, 0, MAXBSIZE);
302 1.1.4.2 matt else if (ext2fs_read_disk_block(params,
303 1.1.4.2 matt fsbtodb(fs, blk) + params->fstype->offset,
304 1.1.4.2 matt fs->e2fs_bsize, level[level_i].diskbuf) == 0)
305 1.1.4.2 matt return 0;
306 1.1.4.2 matt /* XXX ondisk32 */
307 1.1.4.2 matt level[level_i].blknums =
308 1.1.4.2 matt (uint32_t *)level[level_i].diskbuf;
309 1.1.4.2 matt level[level_i].blkcount = NINDIR(fs);
310 1.1.4.2 matt continue;
311 1.1.4.2 matt }
312 1.1.4.2 matt
313 1.1.4.2 matt /* blk is the next direct level block. */
314 1.1.4.2 matt #if 0
315 1.1.4.2 matt fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
316 1.1.4.2 matt fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
317 1.1.4.2 matt #endif
318 1.1.4.2 matt rv = (*callback)(params, state,
319 1.1.4.2 matt fsbtodb(fs, blk) + params->fstype->offset, fs->e2fs_bsize);
320 1.1.4.2 matt lblk++;
321 1.1.4.2 matt nblk--;
322 1.1.4.2 matt if (rv != 1)
323 1.1.4.2 matt return rv;
324 1.1.4.2 matt }
325 1.1.4.2 matt
326 1.1.4.2 matt if (nblk != 0) {
327 1.1.4.2 matt warnx("Inode %llu in `%s' ran out of blocks?",
328 1.1.4.2 matt (unsigned long long)ino, params->filesystem);
329 1.1.4.2 matt return 0;
330 1.1.4.2 matt }
331 1.1.4.2 matt
332 1.1.4.2 matt return 1;
333 1.1.4.2 matt }
334 1.1.4.2 matt
335 1.1.4.2 matt /*
336 1.1.4.2 matt * This callback reads a block of the root directory,
337 1.1.4.2 matt * searches for an entry for the secondary bootstrap,
338 1.1.4.2 matt * and saves the inode number if one is found.
339 1.1.4.2 matt */
340 1.1.4.2 matt static int
341 1.1.4.2 matt ext2fs_findstage2_ino(ib_params *params, void *_ino,
342 1.1.4.2 matt uint64_t blk, uint32_t blksize)
343 1.1.4.2 matt {
344 1.1.4.2 matt uint8_t dirbuf[MAXBSIZE];
345 1.1.4.2 matt struct ext2fs_direct *de, *ede;
346 1.1.4.2 matt uint32_t ino;
347 1.1.4.2 matt
348 1.1.4.2 matt assert(params != NULL);
349 1.1.4.2 matt assert(params->fstype != NULL);
350 1.1.4.2 matt assert(params->stage2 != NULL);
351 1.1.4.2 matt assert(_ino != NULL);
352 1.1.4.2 matt
353 1.1.4.2 matt /* Skip directory holes. */
354 1.1.4.2 matt if (blk == 0)
355 1.1.4.2 matt return 1;
356 1.1.4.2 matt
357 1.1.4.2 matt /* Read the directory block. */
358 1.1.4.2 matt if (ext2fs_read_disk_block(params, blk, blksize, dirbuf) == 0)
359 1.1.4.2 matt return 0;
360 1.1.4.2 matt
361 1.1.4.2 matt /* Loop over the directory entries. */
362 1.1.4.2 matt de = (struct ext2fs_direct *)&dirbuf[0];
363 1.1.4.2 matt ede = (struct ext2fs_direct *)&dirbuf[blksize];
364 1.1.4.2 matt while (de < ede) {
365 1.1.4.2 matt ino = fs2h32(de->e2d_ino);
366 1.1.4.2 matt if (ino != 0 && strcmp(de->e2d_name, params->stage2) == 0) {
367 1.1.4.2 matt *((uint32_t *)_ino) = ino;
368 1.1.4.2 matt return (2);
369 1.1.4.2 matt }
370 1.1.4.2 matt if (fs2h16(de->e2d_reclen) == 0)
371 1.1.4.2 matt break;
372 1.1.4.2 matt de = (struct ext2fs_direct *)((char *)de +
373 1.1.4.2 matt fs2h16(de->e2d_reclen));
374 1.1.4.2 matt }
375 1.1.4.2 matt
376 1.1.4.2 matt return 1;
377 1.1.4.2 matt }
378 1.1.4.2 matt
379 1.1.4.2 matt struct findblks_state {
380 1.1.4.2 matt uint32_t maxblk;
381 1.1.4.2 matt uint32_t nblk;
382 1.1.4.2 matt ib_block *blocks;
383 1.1.4.2 matt };
384 1.1.4.2 matt
385 1.1.4.2 matt /* This callback records the blocks of the secondary bootstrap. */
386 1.1.4.2 matt static int
387 1.1.4.2 matt ext2fs_findstage2_blocks(ib_params *params, void *_state,
388 1.1.4.2 matt uint64_t blk, uint32_t blksize)
389 1.1.4.2 matt {
390 1.1.4.2 matt struct findblks_state *state = _state;
391 1.1.4.2 matt
392 1.1.4.2 matt assert(params != NULL);
393 1.1.4.2 matt assert(params->stage2 != NULL);
394 1.1.4.2 matt assert(_state != NULL);
395 1.1.4.2 matt
396 1.1.4.2 matt if (state->nblk == state->maxblk) {
397 1.1.4.2 matt warnx("Secondary bootstrap `%s' has too many blocks (max %d)",
398 1.1.4.2 matt params->stage2, state->maxblk);
399 1.1.4.2 matt return (0);
400 1.1.4.2 matt }
401 1.1.4.2 matt state->blocks[state->nblk].block = blk;
402 1.1.4.2 matt state->blocks[state->nblk].blocksize = blksize;
403 1.1.4.2 matt state->nblk++;
404 1.1.4.2 matt return 1;
405 1.1.4.2 matt }
406 1.1.4.2 matt
407 1.1.4.2 matt /*
408 1.1.4.2 matt * publicly visible functions
409 1.1.4.2 matt */
410 1.1.4.2 matt
411 1.1.4.2 matt int
412 1.1.4.2 matt ext2fs_match(ib_params *params)
413 1.1.4.2 matt {
414 1.1.4.2 matt uint8_t sbbuf[sizeof(struct m_ext2fs)];
415 1.1.4.2 matt struct m_ext2fs *fs;
416 1.1.4.2 matt
417 1.1.4.2 matt assert(params != NULL);
418 1.1.4.2 matt assert(params->fstype != NULL);
419 1.1.4.2 matt
420 1.1.4.2 matt /* Read the superblock. */
421 1.1.4.2 matt fs = (void *)sbbuf;
422 1.1.4.2 matt if (ext2fs_read_sblock(params, fs) == 0)
423 1.1.4.2 matt return 0;
424 1.1.4.2 matt
425 1.1.4.2 matt params->fstype->needswap = 0;
426 1.1.4.2 matt params->fstype->blocksize = fs->e2fs_bsize;
427 1.1.4.2 matt params->fstype->offset = 0;
428 1.1.4.2 matt
429 1.1.4.2 matt return 1;
430 1.1.4.2 matt }
431 1.1.4.2 matt
432 1.1.4.2 matt int
433 1.1.4.2 matt ext2fs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
434 1.1.4.2 matt {
435 1.1.4.2 matt int rv;
436 1.1.4.2 matt uint32_t ino;
437 1.1.4.2 matt struct findblks_state state;
438 1.1.4.2 matt
439 1.1.4.2 matt assert(params != NULL);
440 1.1.4.2 matt assert(params->stage2 != NULL);
441 1.1.4.2 matt assert(maxblk != NULL);
442 1.1.4.2 matt assert(blocks != NULL);
443 1.1.4.2 matt
444 1.1.4.2 matt if (params->flags & IB_STAGE2START)
445 1.1.4.2 matt return hardcode_stage2(params, maxblk, blocks);
446 1.1.4.2 matt
447 1.1.4.2 matt /* The secondary bootstrap must be clearly in /. */
448 1.1.4.2 matt if (params->stage2[0] == '/')
449 1.1.4.2 matt params->stage2++;
450 1.1.4.2 matt if (strchr(params->stage2, '/') != NULL) {
451 1.1.4.2 matt warnx("The secondary bootstrap `%s' must be in /",
452 1.1.4.2 matt params->stage2);
453 1.1.4.2 matt return 0;
454 1.1.4.2 matt }
455 1.1.4.2 matt
456 1.1.4.2 matt /* Get the inode number of the secondary bootstrap. */
457 1.1.4.2 matt rv = ext2fs_find_disk_blocks(params, EXT2_ROOTINO,
458 1.1.4.2 matt ext2fs_findstage2_ino, &ino);
459 1.1.4.2 matt if (rv != 2) {
460 1.1.4.2 matt warnx("Could not find secondary bootstrap `%s' in `%s'",
461 1.1.4.2 matt params->stage2, params->filesystem);
462 1.1.4.2 matt return 0;
463 1.1.4.2 matt }
464 1.1.4.2 matt
465 1.1.4.2 matt /* Record the disk blocks of the secondary bootstrap. */
466 1.1.4.2 matt state.maxblk = *maxblk;
467 1.1.4.2 matt state.nblk = 0;
468 1.1.4.2 matt state.blocks = blocks;
469 1.1.4.2 matt rv = ext2fs_find_disk_blocks(params, ino,
470 1.1.4.2 matt ext2fs_findstage2_blocks, &state);
471 1.1.4.2 matt if (rv == 0)
472 1.1.4.2 matt return 0;
473 1.1.4.2 matt
474 1.1.4.2 matt *maxblk = state.nblk;
475 1.1.4.2 matt return 1;
476 1.1.4.2 matt }
477