1*87ba0e2aSchs /* $NetBSD: ufs.c,v 1.2 2022/11/17 06:40:40 chs Exp $ */
2b985414bSchristos
3b985414bSchristos /*-
4b985414bSchristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5b985414bSchristos * Copyright (c) 2016 The DragonFly Project
6b985414bSchristos * Copyright (c) 2002, 2003 Gordon Tetlow
7b985414bSchristos * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8b985414bSchristos * Copyright (c) 2014 The FreeBSD Foundation
9b985414bSchristos * All rights reserved.
10b985414bSchristos *
11b985414bSchristos * This code is derived from software contributed to The NetBSD Foundation
12b985414bSchristos * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
13b985414bSchristos *
14b985414bSchristos * This software was developed by Edward Tomasz Napierala under sponsorship
15b985414bSchristos * from the FreeBSD Foundation.
16b985414bSchristos *
17b985414bSchristos * Redistribution and use in source and binary forms, with or without
18b985414bSchristos * modification, are permitted provided that the following conditions
19b985414bSchristos * are met:
20b985414bSchristos * 1. Redistributions of source code must retain the above copyright
21b985414bSchristos * notice, this list of conditions and the following disclaimer.
22b985414bSchristos * 2. Redistributions in binary form must reproduce the above copyright
23b985414bSchristos * notice, this list of conditions and the following disclaimer in the
24b985414bSchristos * documentation and/or other materials provided with the distribution.
25b985414bSchristos *
26b985414bSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
27b985414bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28b985414bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29b985414bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
30b985414bSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31b985414bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32b985414bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33b985414bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34b985414bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35b985414bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36b985414bSchristos * SUCH DAMAGE.
37b985414bSchristos */
38b985414bSchristos #include <sys/cdefs.h>
39*87ba0e2aSchs __RCSID("$NetBSD: ufs.c,v 1.2 2022/11/17 06:40:40 chs Exp $");
40b985414bSchristos
41b985414bSchristos #include <sys/types.h>
42b985414bSchristos #include <stdint.h>
43b985414bSchristos #include <stdio.h>
44b985414bSchristos #include <stdlib.h>
45b985414bSchristos #include <string.h>
46b985414bSchristos
47b985414bSchristos #include <ufs/ffs/fs.h>
48b985414bSchristos
49b985414bSchristos #include "fstyp.h"
50b985414bSchristos
51b985414bSchristos static const int superblocks[] = SBLOCKSEARCH;
52b985414bSchristos
53b985414bSchristos int
fstyp_ufs(FILE * fp,char * label,size_t labelsize)54b985414bSchristos fstyp_ufs(FILE *fp, char *label, size_t labelsize)
55b985414bSchristos {
56b985414bSchristos int sb, superblock;
57b985414bSchristos struct fs *fs;
58b985414bSchristos
59b985414bSchristos /*
60b985414bSchristos * Walk through the standard places that superblocks hide and look
61b985414bSchristos * for UFS magic. If we find magic, then check that the size in the
62b985414bSchristos * superblock corresponds to the size of the underlying provider.
63b985414bSchristos * Finally, look for a volume label and create an appropriate
64b985414bSchristos * provider based on that.
65b985414bSchristos */
66b985414bSchristos for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
67b985414bSchristos fs = (struct fs *)read_buf(fp, superblock, SBLOCKSIZE);
68b985414bSchristos if (fs == NULL)
69b985414bSchristos continue;
70*87ba0e2aSchs
71*87ba0e2aSchs if (fs->fs_magic == FS_UFS2EA_MAGIC)
72*87ba0e2aSchs fs->fs_magic = FS_UFS2_MAGIC;
73*87ba0e2aSchs else if (fs->fs_magic == FS_UFS2EA_MAGIC_SWAPPED)
74*87ba0e2aSchs fs->fs_magic = FS_UFS2_MAGIC_SWAPPED;
75*87ba0e2aSchs
76b985414bSchristos /*
77b985414bSchristos * Check for magic. We also need to check if file system size
78b985414bSchristos * is equal to providers size, because sysinstall(8) used to
79b985414bSchristos * bogusly put first partition at offset 0 instead of 16, and
80b985414bSchristos * glabel/ufs would find file system on slice instead of
81b985414bSchristos * partition.
82b985414bSchristos */
83b985414bSchristos #ifdef notyet
84b985414bSchristos if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
85b985414bSchristos ((pp->mediasize / fs->fs_fsize == fs->fs_old_size) ||
86b985414bSchristos (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
87b985414bSchristos /* Valid UFS1. */
88b985414bSchristos } else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
89b985414bSchristos ((pp->mediasize / fs->fs_fsize == fs->fs_size) ||
90b985414bSchristos (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
91b985414bSchristos /* Valid UFS2. */
92b985414bSchristos } else {
93b985414bSchristos g_free(fs);
94b985414bSchristos continue;
95b985414bSchristos }
96b985414bSchristos #else
97b985414bSchristos if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0) {
98b985414bSchristos /* Valid UFS1. */
99b985414bSchristos } else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0) {
100b985414bSchristos /* Valid UFS2. */
101b985414bSchristos } else {
102b985414bSchristos free(fs);
103b985414bSchristos continue;
104b985414bSchristos }
105b985414bSchristos #endif
106b985414bSchristos
107b985414bSchristos if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
108b985414bSchristos fs->fs_bsize < MINBSIZE ||
109b985414bSchristos (size_t)fs->fs_bsize < sizeof(struct fs)) {
110b985414bSchristos free(fs);
111b985414bSchristos continue;
112b985414bSchristos }
113b985414bSchristos
114b985414bSchristos strlcpy(label, (char*)fs->fs_volname, labelsize);
115b985414bSchristos
116b985414bSchristos free(fs);
117b985414bSchristos return 0;
118b985414bSchristos }
119b985414bSchristos
120b985414bSchristos return 1;
121b985414bSchristos }
122