Home | History | Annotate | Line # | Download | only in fstyp
ufs.c revision 1.1
      1 /*	$NetBSD: ufs.c,v 1.1 2018/01/09 03:31:15 christos 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.1 2018/01/09 03:31:15 christos 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 		 * Check for magic. We also need to check if file system size
     72 		 * is equal to providers size, because sysinstall(8) used to
     73 		 * bogusly put first partition at offset 0 instead of 16, and
     74 		 * glabel/ufs would find file system on slice instead of
     75 		 * partition.
     76 		 */
     77 #ifdef notyet
     78 		if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
     79 		    ((pp->mediasize / fs->fs_fsize == fs->fs_old_size) ||
     80 		    (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
     81 		    	/* Valid UFS1. */
     82 		} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
     83 		    ((pp->mediasize / fs->fs_fsize == fs->fs_size) ||
     84 		    (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
     85 		    	/* Valid UFS2. */
     86 		} else {
     87 			g_free(fs);
     88 			continue;
     89 		}
     90 #else
     91 		if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0) {
     92 		    	/* Valid UFS1. */
     93 		} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0) {
     94 		    	/* Valid UFS2. */
     95 		} else {
     96 			free(fs);
     97 			continue;
     98 		}
     99 #endif
    100 
    101 		if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
    102 		    fs->fs_bsize < MINBSIZE ||
    103 		    (size_t)fs->fs_bsize < sizeof(struct fs)) {
    104 			free(fs);
    105 			continue;
    106 		}
    107 
    108 		strlcpy(label, (char*)fs->fs_volname, labelsize);
    109 
    110 		free(fs);
    111 		return 0;
    112 	}
    113 
    114 	return 1;
    115 }
    116