xref: /netbsd/usr.sbin/fstyp/ext2fs.c (revision 59a9e119)
1 /*	$NetBSD: ext2fs.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) 2005 Stanislav Sedov
7  * Copyright (c) 2014 The FreeBSD Foundation
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
12  *
13  * This software was developed by Edward Tomasz Napierala under sponsorship
14  * from the FreeBSD Foundation.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: ext2fs.c,v 1.1 2018/01/09 03:31:15 christos Exp $");
39 
40 #include <stdio.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "fstyp.h"
46 
47 #define EXT2FS_SB_OFFSET	1024
48 #define EXT2_SUPER_MAGIC	0xef53
49 #define EXT2_DYNAMIC_REV	1
50 
51 typedef struct e2sb {
52 	uint8_t		fake1[56];
53 	uint16_t	s_magic;
54 	uint8_t		fake2[18];
55 	uint32_t	s_rev_level;
56 	uint8_t		fake3[40];
57 	char		s_volume_name[16];
58 } e2sb_t;
59 
60 int
fstyp_ext2fs(FILE * fp,char * label,size_t size)61 fstyp_ext2fs(FILE *fp, char *label, size_t size)
62 {
63 	e2sb_t *fs;
64 	char *s_volume_name;
65 
66 	fs = read_buf(fp, EXT2FS_SB_OFFSET, 512);
67 	if (fs == NULL)
68 		return 1;
69 
70 	/* Check for magic and versio n*/
71 	if (fs->s_magic == EXT2_SUPER_MAGIC &&
72 	    fs->s_rev_level == EXT2_DYNAMIC_REV) {
73 		//G_LABEL_DEBUG(1, "ext2fs file system detected on %s.",
74 		//    pp->name);
75 	} else {
76 		free(fs);
77 		return 1;
78 	}
79 
80 	s_volume_name = fs->s_volume_name;
81 	/* Terminate label */
82 	s_volume_name[sizeof(fs->s_volume_name) - 1] = '\0';
83 
84 	if (s_volume_name[0] == '/')
85 		s_volume_name += 1;
86 
87 	strlcpy(label, s_volume_name, size);
88 	free(fs);
89 
90 	return 0;
91 }
92