xref: /dragonfly/usr.sbin/fstyp/ext2fs.c (revision 65d969b4)
1551c4c36STomohiro Kusumi /*-
2551c4c36STomohiro Kusumi  * Copyright (c) 2016 The DragonFly Project
3551c4c36STomohiro Kusumi  * Copyright (c) 2005 Stanislav Sedov
4551c4c36STomohiro Kusumi  * Copyright (c) 2014 The FreeBSD Foundation
5551c4c36STomohiro Kusumi  * All rights reserved.
6551c4c36STomohiro Kusumi  *
7551c4c36STomohiro Kusumi  * This software was developed by Edward Tomasz Napierala under sponsorship
8551c4c36STomohiro Kusumi  * from the FreeBSD Foundation.
9551c4c36STomohiro Kusumi  *
10551c4c36STomohiro Kusumi  * Redistribution and use in source and binary forms, with or without
11551c4c36STomohiro Kusumi  * modification, are permitted provided that the following conditions
12551c4c36STomohiro Kusumi  * are met:
13551c4c36STomohiro Kusumi  * 1. Redistributions of source code must retain the above copyright
14551c4c36STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer.
15551c4c36STomohiro Kusumi  * 2. Redistributions in binary form must reproduce the above copyright
16551c4c36STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer in the
17551c4c36STomohiro Kusumi  *    documentation and/or other materials provided with the distribution.
18551c4c36STomohiro Kusumi  *
19551c4c36STomohiro Kusumi  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20551c4c36STomohiro Kusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21551c4c36STomohiro Kusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22551c4c36STomohiro Kusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23551c4c36STomohiro Kusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24551c4c36STomohiro Kusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25551c4c36STomohiro Kusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26551c4c36STomohiro Kusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27551c4c36STomohiro Kusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28551c4c36STomohiro Kusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29551c4c36STomohiro Kusumi  * SUCH DAMAGE.
30551c4c36STomohiro Kusumi  */
31551c4c36STomohiro Kusumi 
32551c4c36STomohiro Kusumi #include <stdio.h>
33551c4c36STomohiro Kusumi #include <stdint.h>
34551c4c36STomohiro Kusumi #include <stdlib.h>
35551c4c36STomohiro Kusumi #include <string.h>
36551c4c36STomohiro Kusumi 
37551c4c36STomohiro Kusumi #include "fstyp.h"
38551c4c36STomohiro Kusumi 
39551c4c36STomohiro Kusumi #define EXT2FS_SB_OFFSET	1024
40551c4c36STomohiro Kusumi #define EXT2_SUPER_MAGIC	0xef53
41551c4c36STomohiro Kusumi #define EXT2_DYNAMIC_REV	1
42551c4c36STomohiro Kusumi 
43551c4c36STomohiro Kusumi typedef struct e2sb {
44551c4c36STomohiro Kusumi 	uint8_t		fake1[56];
45551c4c36STomohiro Kusumi 	uint16_t	s_magic;
46551c4c36STomohiro Kusumi 	uint8_t		fake2[18];
47551c4c36STomohiro Kusumi 	uint32_t	s_rev_level;
48551c4c36STomohiro Kusumi 	uint8_t		fake3[40];
49551c4c36STomohiro Kusumi 	char		s_volume_name[16];
50551c4c36STomohiro Kusumi } e2sb_t;
51551c4c36STomohiro Kusumi 
52551c4c36STomohiro Kusumi int
fstyp_ext2fs(FILE * fp,char * label,size_t size,const char * devpath)53*65d969b4STomohiro Kusumi fstyp_ext2fs(FILE *fp, char *label, size_t size, const char *devpath)
54551c4c36STomohiro Kusumi {
55551c4c36STomohiro Kusumi 	e2sb_t *fs;
56551c4c36STomohiro Kusumi 	char *s_volume_name;
57551c4c36STomohiro Kusumi 
58551c4c36STomohiro Kusumi 	fs = (e2sb_t *)read_buf(fp, EXT2FS_SB_OFFSET, 512);
59551c4c36STomohiro Kusumi 	if (fs == NULL)
60551c4c36STomohiro Kusumi 		return (1);
61551c4c36STomohiro Kusumi 
62551c4c36STomohiro Kusumi 	/* Check for magic and versio n*/
63551c4c36STomohiro Kusumi 	if (fs->s_magic == EXT2_SUPER_MAGIC &&
64551c4c36STomohiro Kusumi 	    fs->s_rev_level == EXT2_DYNAMIC_REV) {
65551c4c36STomohiro Kusumi 		//G_LABEL_DEBUG(1, "ext2fs file system detected on %s.",
66551c4c36STomohiro Kusumi 		//    pp->name);
67551c4c36STomohiro Kusumi 	} else {
68551c4c36STomohiro Kusumi 		free(fs);
69551c4c36STomohiro Kusumi 		return (1);
70551c4c36STomohiro Kusumi 	}
71551c4c36STomohiro Kusumi 
72551c4c36STomohiro Kusumi 	s_volume_name = fs->s_volume_name;
73551c4c36STomohiro Kusumi 	/* Terminate label */
74551c4c36STomohiro Kusumi 	s_volume_name[sizeof(fs->s_volume_name) - 1] = '\0';
75551c4c36STomohiro Kusumi 
76551c4c36STomohiro Kusumi 	if (s_volume_name[0] == '/')
77551c4c36STomohiro Kusumi 		s_volume_name += 1;
78551c4c36STomohiro Kusumi 
79551c4c36STomohiro Kusumi 	strlcpy(label, s_volume_name, size);
80551c4c36STomohiro Kusumi 	free(fs);
81551c4c36STomohiro Kusumi 
82551c4c36STomohiro Kusumi 	return (0);
83551c4c36STomohiro Kusumi }
84