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